diff --git a/internal/data/BUILD b/internal/data/BUILD index dce7d36c..a8342387 100644 --- a/internal/data/BUILD +++ b/internal/data/BUILD @@ -9,10 +9,8 @@ package(default_visibility = [ cc_library( name = "data_manager", hdrs = [ - "data_manager.h", "data_set.h", "leveldb_data_set.h", - "memory_data_set.h", ], deps = [ "//internal/platform:types", @@ -43,7 +41,6 @@ cc_test( timeout = "short", srcs = [ "leveldb_data_set_test.cc", - "memory_data_set_test.cc", ], shard_count = 8, deps = [ diff --git a/internal/data/data_manager.h b/internal/data/data_manager.h deleted file mode 100644 index 3f42f456..00000000 --- a/internal/data/data_manager.h +++ /dev/null @@ -1,53 +0,0 @@ -// 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_DATA_MANAGER_H_ -#define THIRD_PARTY_NEARBY_INTERNAL_DATA_DATA_MANAGER_H_ - -#include - -#include "absl/strings/string_view.h" -#include "internal/data/data_set.h" -#include "internal/data/leveldb_data_set.h" -#include "internal/data/memory_data_set.h" - -namespace nearby { -namespace data { - -class DataManager { - public: - enum class DataStorageType : int { kMemory = 0, kLevelDb = 1 }; - explicit DataManager(DataStorageType data_storage_type) - : data_storage_type_(data_storage_type) {} - ~DataManager() = default; - - template - std::unique_ptr> GetDataSet(absl::string_view path) { - if (data_storage_type_ == DataStorageType::kMemory) { - return std::make_unique>(path); - } else if (data_storage_type_ == DataStorageType::kLevelDb) { - return std::make_unique>(path); - } else { - return nullptr; - } - } - - private: - DataStorageType data_storage_type_; -}; - -} // namespace data -} // namespace nearby - -#endif // THIRD_PARTY_NEARBY_INTERNAL_DATA_DATA_MANAGER_H_ diff --git a/internal/data/memory_data_set.h b/internal/data/memory_data_set.h deleted file mode 100644 index 9cf5fca2..00000000 --- a/internal/data/memory_data_set.h +++ /dev/null @@ -1,109 +0,0 @@ -// 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_ diff --git a/internal/data/memory_data_set_test.cc b/internal/data/memory_data_set_test.cc deleted file mode 100644 index e5e3a13d..00000000 --- a/internal/data/memory_data_set_test.cc +++ /dev/null @@ -1,71 +0,0 @@ -// 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. - -#include "internal/data/memory_data_set.h" - -#include -#include -#include -#include -#include - -#include "gmock/gmock.h" -#include "protobuf-matchers/protocol-buffer-matchers.h" -#include "gtest/gtest.h" - -namespace nearby { -namespace data { -namespace { - -TEST(MemoryDataSet, TestUpdateEntries) { - bool result = false; - MemoryDataSet string_set{""}; - - auto temp = MemoryDataSet::KeyEntryVector( - {{"id1", "string1"}, {"id2", "string2"}}); - - auto data = - std::make_unique::KeyEntryVector>(temp); - string_set.UpdateEntries(std::move(data), nullptr, - [&result](bool res) { result = res; }); - EXPECT_TRUE(result); -} - -TEST(MemoryDataSet, TestLoadEntries) { - std::vector result = {}; - MemoryDataSet string_set{""}; - - auto temp = MemoryDataSet::KeyEntryVector( - {{"id1", "string1"}, {"id2", "string2"}}); - auto data = - std::make_unique::KeyEntryVector>(temp); - - string_set.UpdateEntries(std::move(data), nullptr, [](bool ans) {}); - string_set.LoadEntries( - [&result](bool ans, std::unique_ptr> res) { - auto it = res->begin(); - while (it != res->end()) { - result.push_back(*it); - ++it; - } - }); - - EXPECT_THAT(result, testing::SizeIs(2)); - std::sort(result.begin(), result.end()); - EXPECT_EQ(result, std::vector({"string1", "string2"})); -} - -} // namespace -} // namespace data -} // namespace nearby diff --git a/internal/test/BUILD b/internal/test/BUILD index 5d9b2c8f..1bb96584 100644 --- a/internal/test/BUILD +++ b/internal/test/BUILD @@ -27,7 +27,6 @@ cc_library( hdrs = [ "fake_account_manager.h", "fake_clock.h", - "fake_data_set.h", "fake_device_info.h", "fake_http_client.h", "fake_http_client_factory.h", @@ -42,7 +41,6 @@ cc_library( visibility = ["//visibility:public"], deps = [ "//internal/base", - "//internal/data:data_manager", "//internal/network:types", "//internal/platform:comm", "//internal/platform:types", @@ -65,7 +63,6 @@ cc_test( timeout = "short", srcs = [ "fake_clock_test.cc", - "fake_data_set_test.cc", "fake_device_info_test.cc", "fake_http_client_test.cc", "fake_task_runner_test.cc", @@ -77,7 +74,6 @@ cc_test( shard_count = 8, deps = [ ":test", - "//internal/data:data_manager", "//internal/network:types", "//internal/platform:types", "//internal/platform/implementation:types", diff --git a/internal/test/fake_data_set.h b/internal/test/fake_data_set.h deleted file mode 100644 index 43494207..00000000 --- a/internal/test/fake_data_set.h +++ /dev/null @@ -1,132 +0,0 @@ -// 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_TEST_FAKE_DATA_SET_H_ -#define THIRD_PARTY_NEARBY_INTERNAL_TEST_FAKE_DATA_SET_H_ - -#include -#include -#include -#include -#include - -#include "absl/container/flat_hash_map.h" -#include "absl/functional/any_invocable.h" -#include "internal/data/data_set.h" - -namespace nearby { -namespace data { - -template -class FakeDataSet : public DataSet { - public: - using KeyEntryVector = std::vector>; - - explicit FakeDataSet(const absl::flat_hash_map& entries_map) - : entries_map_(entries_map) {} - - void Initialize(absl::AnyInvocable callback) override { - init_callback_ = std::move(callback); - } - - void LoadEntries( - absl::AnyInvocable>) &&> - callback) override { - load_callback_ = std::move(callback); - } - - void UpdateEntries(std::unique_ptr entries_to_save, - std::unique_ptr> keys_to_remove, - absl::AnyInvocable callback) override { - entries_to_save_ = std::move(entries_to_save); - keys_to_remove_ = std::move(keys_to_remove); - update_callback_ = std::move(callback); - } - - void Destroy(absl::AnyInvocable callback) override { - destroy_callback_ = std::move(callback); - } - - // Mocked methods - void InitStatusCallback(InitStatus status) { - if (auto callback = std::move(init_callback_)) { - std::move(callback)(status); - } - } - - void LoadCallback(bool success) { - if (auto callback = std::move(load_callback_)) { - auto entries = std::make_unique>(); - for (auto it = entries_map_.begin(); it != entries_map_.end(); ++it) { - entries->push_back(it->second); - } - std::move(callback)(success, std::move(entries)); - } - } - - void UpdateCallback(bool success) { - if (success) { - if (entries_to_save_ != nullptr) { - for (auto it = entries_to_save_->begin(); it != entries_to_save_->end(); - ++it) { - auto entry = entries_map_.find(it->first); - if (entry == entries_map_.end()) { - entries_map_.emplace(it->first, it->second); - } else { - entry->second = it->second; - } - } - } - - if (keys_to_remove_ != nullptr) { - for (auto it = keys_to_remove_->begin(); it != keys_to_remove_->end(); - ++it) { - entries_map_.erase(*it); - } - } - } - - entries_to_save_ = nullptr; - keys_to_remove_ = nullptr; - if (auto callback = std::move(update_callback_)) { - std::move(callback)(success); - } - } - - void DestroyCallback(bool success) { - if (success) { - entries_map_.clear(); - } - if (auto callback = std::move(destroy_callback_)) { - std::move(callback)(success); - } - } - - absl::flat_hash_map& entries_map() { return entries_map_; } - - private: - absl::flat_hash_map entries_map_ = nullptr; - absl::AnyInvocable init_callback_ = nullptr; - absl::AnyInvocable>) &&> - load_callback_ = nullptr; - std::unique_ptr entries_to_save_ = nullptr; - std::unique_ptr> keys_to_remove_ = nullptr; - absl::AnyInvocable update_callback_ = nullptr; - absl::AnyInvocable destroy_callback_ = nullptr; -}; - -} // namespace data -} // namespace nearby - -#endif // THIRD_PARTY_NEARBY_INTERNAL_TEST_FAKE_DATA_SET_H_ diff --git a/internal/test/fake_data_set_test.cc b/internal/test/fake_data_set_test.cc deleted file mode 100644 index fb7da29d..00000000 --- a/internal/test/fake_data_set_test.cc +++ /dev/null @@ -1,107 +0,0 @@ -// 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. - -#include "internal/test/fake_data_set.h" - -#include -#include -#include -#include -#include - -#include "gmock/gmock.h" -#include "protobuf-matchers/protocol-buffer-matchers.h" -#include "gtest/gtest.h" -#include "internal/data/data_set.h" - -namespace nearby { -namespace data { -namespace { - -TEST(FakeDataSet, TestInitialize) { - InitStatus result = InitStatus::kNotInitialized; - FakeDataSet string_set({}); - - string_set.Initialize([&result](InitStatus res) { result = res; }); - string_set.InitStatusCallback(InitStatus::kOK); - EXPECT_EQ(result, InitStatus::kOK); -} - -TEST(FakeDataSet, TestUpdateEntries) { - bool result = false; - FakeDataSet string_set({}); - - auto temp = FakeDataSet::KeyEntryVector( - {{"id1", "string1"}, {"id2", "string2"}}); - - auto data = std::make_unique::KeyEntryVector>(temp); - - string_set.UpdateEntries(std::move(data), nullptr, - [&result](bool res) { result = res; }); - - string_set.UpdateCallback(true); - EXPECT_TRUE(result); - data = std::make_unique::KeyEntryVector>(temp); - string_set.UpdateEntries(std::move(data), nullptr, - [&result](bool res) { result = res; }); - string_set.UpdateCallback(false); - EXPECT_FALSE(result); -} - -TEST(FakeDataSet, TestLoadEntries) { - std::vector result = {}; - FakeDataSet string_set({}); - - auto temp = FakeDataSet::KeyEntryVector( - {{"id1", "string1"}, {"id2", "string2"}}); - auto data = std::make_unique::KeyEntryVector>(temp); - - string_set.UpdateEntries(std::move(data), nullptr, [](bool ans) {}); - string_set.UpdateCallback(true); - string_set.LoadEntries( - [&result](bool ans, std::unique_ptr> res) { - auto it = res->begin(); - while (it != res->end()) { - result.push_back(*it); - ++it; - } - }); - string_set.LoadCallback(true); - EXPECT_THAT(result, testing::SizeIs(2)); - std::sort(result.begin(), result.end()); - EXPECT_EQ(result, std::vector({"string1", "string2"})); -} - -TEST(MockDataSet, TestDestroy) { - bool result; - std::vector data = {}; - FakeDataSet string_set({{"id1", "string1"}, {"id2", "string2"}}); - string_set.Destroy([&result](bool res) { result = res; }); - string_set.DestroyCallback(true); - EXPECT_TRUE(result); - string_set.LoadEntries( - [&data](bool ans, std::unique_ptr> res) { - auto it = res->begin(); - while (it != res->end()) { - data.push_back(*it); - ++it; - } - }); - string_set.LoadCallback(true); - EXPECT_THAT(data, ::testing::SizeIs(0)); -} - -} // namespace -} // namespace data -} // namespace nearby