mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Add support for removing preferences using key prefix.
PiperOrigin-RevId: 875366484
This commit is contained in:
committed by
Copybara-Service
parent
f5a6ec8a6e
commit
44679a49e1
@@ -90,6 +90,8 @@ class PreferencesManager : public nearby::api::PreferencesManager {
|
||||
|
||||
// Removes preferences
|
||||
void Remove(absl::string_view key) override;
|
||||
// TODO: b/485304482 - Implement this method if needed..
|
||||
bool RemoveKeyPrefix(absl::string_view prefix) override { return false; }
|
||||
};
|
||||
|
||||
} // namespace nearby::apple
|
||||
|
||||
@@ -177,6 +177,19 @@ void PreferencesManager::Remove(absl::string_view key) {
|
||||
value_.erase(absl::StrCat(key));
|
||||
}
|
||||
|
||||
bool PreferencesManager::RemoveKeyPrefix(absl::string_view prefix) {
|
||||
absl::MutexLock lock(mutex_);
|
||||
auto it = value_.begin();
|
||||
while (it != value_.end()) {
|
||||
if (it.key().starts_with(prefix)) {
|
||||
it = value_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Private methods
|
||||
|
||||
// Writes data to storage.
|
||||
|
||||
@@ -108,6 +108,8 @@ class PreferencesManager : public api::PreferencesManager {
|
||||
|
||||
// Removes preferences
|
||||
void Remove(absl::string_view key) override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool RemoveKeyPrefix(absl::string_view prefix) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
// Writes data to storage.
|
||||
|
||||
@@ -85,6 +85,10 @@ class PreferencesManager {
|
||||
|
||||
// Removes preferences
|
||||
virtual void Remove(absl::string_view key) = 0;
|
||||
|
||||
// Removes all preferences that start with the given prefix.
|
||||
// Returns false on error.
|
||||
virtual bool RemoveKeyPrefix(absl::string_view prefix) = 0;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
@@ -176,6 +176,19 @@ void PreferencesManager::Remove(absl::string_view key) {
|
||||
value_.erase(absl::StrCat(key));
|
||||
}
|
||||
|
||||
bool PreferencesManager::RemoveKeyPrefix(absl::string_view prefix) {
|
||||
absl::MutexLock lock(mutex_);
|
||||
auto it = value_.begin();
|
||||
while (it != value_.end()) {
|
||||
if (it.key().starts_with(prefix)) {
|
||||
it = value_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Private methods
|
||||
|
||||
// Writes data to storage.
|
||||
|
||||
@@ -108,6 +108,8 @@ class PreferencesManager : public api::PreferencesManager {
|
||||
|
||||
// Removes preferences
|
||||
void Remove(absl::string_view key) override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool RemoveKeyPrefix(absl::string_view prefix) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
// Writes data to storage.
|
||||
|
||||
@@ -21,7 +21,10 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/time/clock.h"
|
||||
#include "absl/time/time.h"
|
||||
@@ -35,6 +38,8 @@
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
namespace {
|
||||
using ::testing::IsEmpty;
|
||||
|
||||
using json = ::nlohmann::json;
|
||||
constexpr absl::string_view kPreferencesFilePath = "Google/Nearby/Sharing";
|
||||
} // namespace
|
||||
@@ -184,5 +189,23 @@ TEST(PreferencesManager, RemoveKey) {
|
||||
EXPECT_EQ(result, "default key");
|
||||
}
|
||||
|
||||
TEST(PreferencesManager, RemoveKeyPrefix) {
|
||||
constexpr absl::string_view kKeyPrefix = "test_key_prefix.";
|
||||
auto pm = PreferencesManager(FilePath{kPreferencesFilePath});
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
pm.SetString(absl::StrCat(kKeyPrefix, i), absl::StrCat("value", i));
|
||||
}
|
||||
constexpr absl::string_view string_key = "string_key";
|
||||
pm.SetString(string_key, "this is a test string");
|
||||
EXPECT_EQ(pm.GetString(string_key, ""), "this is a test string");
|
||||
|
||||
EXPECT_TRUE(pm.RemoveKeyPrefix(kKeyPrefix));
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
EXPECT_THAT(pm.GetString(absl::StrCat(kKeyPrefix, i), ""),
|
||||
IsEmpty());
|
||||
}
|
||||
EXPECT_EQ(pm.GetString(string_key, ""), "this is a test string");
|
||||
}
|
||||
|
||||
} // namespace windows
|
||||
} // namespace nearby
|
||||
|
||||
Reference in New Issue
Block a user