Skip to content
88 changes: 85 additions & 3 deletions database/src/desktop/database_desktop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@

#include "database/src/desktop/database_desktop.h"

#include <algorithm>
#include <cassert>
#include <cctype>
#include <map>
#include <memory>
#include <queue>
#include <stack>
#include <unordered_map>
#include <utility>

#include "app/src/app_common.h"
#include "app/src/assert.h"
Expand All @@ -43,6 +48,40 @@ namespace internal {

Mutex g_database_reference_constructor_mutex; // NOLINT

namespace {

struct AppPersistenceState {
explicit AppPersistenceState(App* app) : app(app) {}
App* app;
std::unordered_map<std::string, bool> by_url;
};

std::string NormalizeDatabaseUrl(const std::string& url) {
ParseUrl parser;
if (parser.Parse(url) != ParseUrl::kParseOk) {
return url;
}
std::string normalized_host = parser.hostname;
std::transform(
normalized_host.begin(), normalized_host.end(), normalized_host.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
return std::string(parser.secure ? "https://" : "http://") + normalized_host;
}
Comment thread
dconeybe marked this conversation as resolved.
Resolved

// Leaf lock guarding GetPersistenceEnabledMap(). Never acquire another lock
// while holding g_persistence_enabled_mutex. Intentionally leaked so it
// outlives static teardown.
Mutex* g_persistence_enabled_mutex = new Mutex();

std::unordered_map<App*, std::unique_ptr<AppPersistenceState>>*
GetPersistenceEnabledMap() {
static auto* persistence_enabled_map =
new std::unordered_map<App*, std::unique_ptr<AppPersistenceState>>();
return persistence_enabled_map;
}

} // namespace

SingleValueListener::SingleValueListener(DatabaseInternal* database,
const QuerySpec& query_spec,
ReferenceCountedFutureImpl* future,
Expand Down Expand Up @@ -79,6 +118,21 @@ DatabaseInternal::DatabaseInternal(App* app, const char* url)
assert(app);
assert(url);

{
// Restore persistence_enabled_ for this (App, URL) if it was configured
// on a prior Database instance.
MutexLock lock(*g_persistence_enabled_mutex);
auto* persistence_map = GetPersistenceEnabledMap();
auto app_it = persistence_map->find(app_);
if (app_it != persistence_map->end()) {
const auto& by_url = app_it->second->by_url;
auto url_it = by_url.find(NormalizeDatabaseUrl(database_url_));
if (url_it != by_url.end()) {
persistence_enabled_ = url_it->second;
}
}
}

// Spin up the token auto-update thread in Auth.
app_->function_registry()->CallFunction(
::firebase::internal::FnAuthStartTokenListener, app_, nullptr, nullptr);
Expand Down Expand Up @@ -180,10 +234,38 @@ const char* DatabaseInternal::GetSdkVersion() {
}

void DatabaseInternal::SetPersistenceEnabled(bool enabled) {
MutexLock lock(repo_mutex_);
// Only set persistence if the repo has not yet been initialized.
if (!repo_) {
AppPersistenceState* state_to_register = nullptr;
{
MutexLock lock(repo_mutex_);
// Only set persistence if the repo has not yet been initialized.
if (repo_) {
return;
}
persistence_enabled_ = enabled;
MutexLock persistence_lock(*g_persistence_enabled_mutex);
auto* persistence_map = GetPersistenceEnabledMap();
auto it = persistence_map->find(app_);
if (it == persistence_map->end()) {
auto state = std::make_unique<AppPersistenceState>(app_);
state_to_register = state.get();
it = persistence_map->emplace(app_, std::move(state)).first;
}
it->second->by_url[NormalizeDatabaseUrl(database_url_)] = enabled;
}

// Register cleanup outside repo_mutex_ and g_persistence_enabled_mutex so
// CleanupNotifier::CleanupAll() (which holds its own mutex while invoking
// callbacks) cannot invert lock order with g_persistence_enabled_mutex.
if (state_to_register != nullptr) {
CleanupNotifier* app_notifier = CleanupNotifier::FindByOwner(app_);
if (app_notifier != nullptr) {
app_notifier->RegisterObject(state_to_register, [](void* object) {
auto* state = static_cast<AppPersistenceState*>(object);
App* app = state->app;
MutexLock lock(*g_persistence_enabled_mutex);
GetPersistenceEnabledMap()->erase(app);
});
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions database/src/desktop/database_desktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class DatabaseInternal {

void SetPersistenceEnabled(bool enabled);

bool persistence_enabled() const { return persistence_enabled_; }

// Set the logging verbosity.
void set_log_level(LogLevel log_level);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,13 @@ static void VariantAddCachedValue(Variant* variant, const Path& path,
for (const std::string& directory : path.GetDirectories()) {
// Ensure we're operating on a map.
if (!variant->is_map()) {
// Special case: If we are adding a priority, then ensure we do not blow
// away the value, which at this point will be directly in the
// variant and not in a .value field. Note that values will never be
// stored in a .value peudofield.
if (IsPriorityKey(directory)) {
// Special case: If we are adding a priority to an existing scalar value,
// ensure we do not blow away the value, which at this point will be
// directly in the variant and not in a .value field. Note that values
// will never be stored in a .value pseudofield. If the variant is null,
// this is a map node whose .priority key sorted before its child keys,
// so initialize it as an empty map instead of wrapping Null in .value.
if (IsPriorityKey(directory) && !variant->is_null()) {
*variant = std::map<Variant, Variant>{
std::make_pair(kValueKey, std::move(*variant)),
};
Expand Down
20 changes: 20 additions & 0 deletions database/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,16 @@ firebase_cpp_cc_test(
firebase_testing
)

firebase_cpp_cc_test(
firebase_rtdb_desktop_level_db_persistence_storage_engine_test
SOURCES
desktop/persistence/level_db_persistence_storage_engine_test.cc
DEPENDS
firebase_database
firebase_testing
leveldb
)

firebase_cpp_cc_test(
firebase_rtdb_desktop_flatbuffer_conversion_test
SOURCES
Expand Down Expand Up @@ -327,6 +337,16 @@ firebase_cpp_cc_test(
firebase_testing
)

firebase_cpp_cc_test(
firebase_rtdb_desktop_database_desktop_test
SOURCES
desktop/database_desktop_test.cc
DEPENDS
firebase_app_for_testing
firebase_database
firebase_testing
)

firebase_cpp_cc_test(
firebase_rtdb_common_database_reference_test
SOURCES
Expand Down
Loading