// Copyright 2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef THIRD_PARTY_NEARBY_INTERNAL_DATA_MEMORY_DATA_SET_H_ #define THIRD_PARTY_NEARBY_INTERNAL_DATA_MEMORY_DATA_SET_H_ #include #include #include #include #include "absl/container/flat_hash_map.h" #include "absl/functional/any_invocable.h" #include "absl/strings/string_view.h" #include "absl/synchronization/mutex.h" #include "internal/data/data_set.h" namespace nearby { namespace data { template class MemoryDataSet : public DataSet { public: using KeyEntryVector = std::vector>; explicit MemoryDataSet(absl::string_view path) : path_(path) {} ~MemoryDataSet() override = default; void Initialize(absl::AnyInvocable callback) override; void LoadEntries( absl::AnyInvocable>) &&> callback) override; void UpdateEntries(std::unique_ptr entries_to_save, std::unique_ptr> keys_to_remove, absl::AnyInvocable callback) override; void Destroy(absl::AnyInvocable callback) override; private: std::string path_; absl::Mutex mutex_; absl::flat_hash_map entries_; }; template void MemoryDataSet::Initialize( absl::AnyInvocable callback) { std::move(callback)(InitStatus::kOK); } template void MemoryDataSet::LoadEntries( absl::AnyInvocable>) &&> callback) { auto result = std::make_unique>(); auto it = entries_.begin(); while (it != entries_.end()) { result->push_back(it->second); ++it; } std::move(callback)(true, std::move(result)); } template void MemoryDataSet::UpdateEntries( std::unique_ptr entries_to_save, std::unique_ptr> keys_to_remove, absl::AnyInvocable callback) { if (entries_to_save != nullptr) { auto it = entries_to_save->begin(); while (it != entries_to_save->end()) { entries_.emplace(it->first, it->second); ++it; } } if (keys_to_remove != nullptr) { auto it = keys_to_remove->begin(); while (it != keys_to_remove->end()) { entries_.erase(*it); ++it; } } std::move(callback)(true); } template void MemoryDataSet::Destroy(absl::AnyInvocable callback) { entries_.clear(); std::move(callback)(true); } } // namespace data } // namespace nearby #endif // THIRD_PARTY_NEARBY_INTERNAL_DATA_MEMORY_DATA_SET_H_