- Remove unused data manager classes.

PiperOrigin-RevId: 596628112
This commit is contained in:
Francis Tsui
2024-01-08 10:08:39 -08:00
committed by Copybara-Service
parent 6ae46a9496
commit 97a3ceb311
7 changed files with 0 additions and 479 deletions
-3
View File
@@ -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 = [
-53
View File
@@ -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 <memory>
#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 <typename T>
std::unique_ptr<DataSet<T>> GetDataSet(absl::string_view path) {
if (data_storage_type_ == DataStorageType::kMemory) {
return std::make_unique<MemoryDataSet<T>>(path);
} else if (data_storage_type_ == DataStorageType::kLevelDb) {
return std::make_unique<LeveldbDataSet<T>>(path);
} else {
return nullptr;
}
}
private:
DataStorageType data_storage_type_;
};
} // namespace data
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_DATA_DATA_MANAGER_H_
-109
View File
@@ -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 <memory>
#include <string>
#include <utility>
#include <vector>
#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 <typename T>
class MemoryDataSet : public DataSet<T> {
public:
using KeyEntryVector = std::vector<std::pair<std::string, T>>;
explicit MemoryDataSet(absl::string_view path) : path_(path) {}
~MemoryDataSet() override = default;
void Initialize(absl::AnyInvocable<void(InitStatus) &&> callback) override;
void LoadEntries(
absl::AnyInvocable<void(bool, std::unique_ptr<std::vector<T>>) &&>
callback) override;
void UpdateEntries(std::unique_ptr<KeyEntryVector> entries_to_save,
std::unique_ptr<std::vector<std::string>> keys_to_remove,
absl::AnyInvocable<void(bool) &&> callback) override;
void Destroy(absl::AnyInvocable<void(bool) &&> callback) override;
private:
std::string path_;
absl::Mutex mutex_;
absl::flat_hash_map<std::string, T> entries_;
};
template <typename T>
void MemoryDataSet<T>::Initialize(
absl::AnyInvocable<void(InitStatus) &&> callback) {
std::move(callback)(InitStatus::kOK);
}
template <typename T>
void MemoryDataSet<T>::LoadEntries(
absl::AnyInvocable<void(bool, std::unique_ptr<std::vector<T>>) &&>
callback) {
auto result = std::make_unique<std::vector<T>>();
auto it = entries_.begin();
while (it != entries_.end()) {
result->push_back(it->second);
++it;
}
std::move(callback)(true, std::move(result));
}
template <typename T>
void MemoryDataSet<T>::UpdateEntries(
std::unique_ptr<KeyEntryVector> entries_to_save,
std::unique_ptr<std::vector<std::string>> keys_to_remove,
absl::AnyInvocable<void(bool) &&> 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 <typename T>
void MemoryDataSet<T>::Destroy(absl::AnyInvocable<void(bool) &&> callback) {
entries_.clear();
std::move(callback)(true);
}
} // namespace data
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_DATA_MEMORY_DATA_SET_H_
-71
View File
@@ -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 <algorithm>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#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<std::string> string_set{""};
auto temp = MemoryDataSet<std::string>::KeyEntryVector(
{{"id1", "string1"}, {"id2", "string2"}});
auto data =
std::make_unique<MemoryDataSet<std::string>::KeyEntryVector>(temp);
string_set.UpdateEntries(std::move(data), nullptr,
[&result](bool res) { result = res; });
EXPECT_TRUE(result);
}
TEST(MemoryDataSet, TestLoadEntries) {
std::vector<std::string> result = {};
MemoryDataSet<std::string> string_set{""};
auto temp = MemoryDataSet<std::string>::KeyEntryVector(
{{"id1", "string1"}, {"id2", "string2"}});
auto data =
std::make_unique<MemoryDataSet<std::string>::KeyEntryVector>(temp);
string_set.UpdateEntries(std::move(data), nullptr, [](bool ans) {});
string_set.LoadEntries(
[&result](bool ans, std::unique_ptr<std::vector<std::string>> 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<std::string>({"string1", "string2"}));
}
} // namespace
} // namespace data
} // namespace nearby
-4
View File
@@ -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",
-132
View File
@@ -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 <functional>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/functional/any_invocable.h"
#include "internal/data/data_set.h"
namespace nearby {
namespace data {
template <typename T>
class FakeDataSet : public DataSet<T> {
public:
using KeyEntryVector = std::vector<std::pair<std::string, T>>;
explicit FakeDataSet(const absl::flat_hash_map<std::string, T>& entries_map)
: entries_map_(entries_map) {}
void Initialize(absl::AnyInvocable<void(InitStatus) &&> callback) override {
init_callback_ = std::move(callback);
}
void LoadEntries(
absl::AnyInvocable<void(bool, std::unique_ptr<std::vector<T>>) &&>
callback) override {
load_callback_ = std::move(callback);
}
void UpdateEntries(std::unique_ptr<KeyEntryVector> entries_to_save,
std::unique_ptr<std::vector<std::string>> keys_to_remove,
absl::AnyInvocable<void(bool) &&> 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<void(bool) &&> 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<std::vector<T>>();
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<std::string, T>& entries_map() { return entries_map_; }
private:
absl::flat_hash_map<std::string, T> entries_map_ = nullptr;
absl::AnyInvocable<void(InitStatus) &&> init_callback_ = nullptr;
absl::AnyInvocable<void(bool, std::unique_ptr<std::vector<T>>) &&>
load_callback_ = nullptr;
std::unique_ptr<KeyEntryVector> entries_to_save_ = nullptr;
std::unique_ptr<std::vector<std::string>> keys_to_remove_ = nullptr;
absl::AnyInvocable<void(bool) &&> update_callback_ = nullptr;
absl::AnyInvocable<void(bool) &&> destroy_callback_ = nullptr;
};
} // namespace data
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_TEST_FAKE_DATA_SET_H_
-107
View File
@@ -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 <algorithm>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#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<std::string> 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<std::string> string_set({});
auto temp = FakeDataSet<std::string>::KeyEntryVector(
{{"id1", "string1"}, {"id2", "string2"}});
auto data = std::make_unique<FakeDataSet<std::string>::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<FakeDataSet<std::string>::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<std::string> result = {};
FakeDataSet<std::string> string_set({});
auto temp = FakeDataSet<std::string>::KeyEntryVector(
{{"id1", "string1"}, {"id2", "string2"}});
auto data = std::make_unique<FakeDataSet<std::string>::KeyEntryVector>(temp);
string_set.UpdateEntries(std::move(data), nullptr, [](bool ans) {});
string_set.UpdateCallback(true);
string_set.LoadEntries(
[&result](bool ans, std::unique_ptr<std::vector<std::string>> 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<std::string>({"string1", "string2"}));
}
TEST(MockDataSet, TestDestroy) {
bool result;
std::vector<std::string> data = {};
FakeDataSet<std::string> 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<std::vector<std::string>> 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