2015-06-18 23:04:41 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2015 Realm Inc.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
// http://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 "shared_realm.hpp"
|
2015-09-04 18:51:19 +00:00
|
|
|
|
2015-09-03 00:53:21 +00:00
|
|
|
#include "external_commit_helper.hpp"
|
2015-08-31 18:20:20 +00:00
|
|
|
#include "realm_delegate.hpp"
|
2015-09-04 21:03:58 +00:00
|
|
|
#include "schema.hpp"
|
2015-09-02 20:51:28 +00:00
|
|
|
#include "transact_log_handler.hpp"
|
2015-08-31 18:20:20 +00:00
|
|
|
|
2015-09-04 18:51:19 +00:00
|
|
|
#include <realm/commit_log.hpp>
|
2015-08-10 17:49:59 +00:00
|
|
|
#include <realm/group_shared.hpp>
|
2015-09-04 18:51:19 +00:00
|
|
|
|
2015-08-24 20:14:59 +00:00
|
|
|
#include <mutex>
|
2015-06-18 23:04:41 +00:00
|
|
|
|
|
|
|
using namespace realm;
|
|
|
|
|
|
|
|
RealmCache Realm::s_global_cache;
|
|
|
|
|
2015-08-25 23:39:32 +00:00
|
|
|
Realm::Config::Config(const Config& c)
|
|
|
|
: path(c.path)
|
|
|
|
, read_only(c.read_only)
|
|
|
|
, in_memory(c.in_memory)
|
|
|
|
, cache(c.cache)
|
|
|
|
, encryption_key(c.encryption_key)
|
|
|
|
, schema_version(c.schema_version)
|
|
|
|
, migration_function(c.migration_function)
|
2015-06-18 23:04:41 +00:00
|
|
|
{
|
|
|
|
if (c.schema) {
|
2015-07-27 19:42:55 +00:00
|
|
|
schema = std::make_unique<Schema>(*c.schema);
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
2015-08-25 23:39:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 21:03:58 +00:00
|
|
|
Realm::Config::~Config() = default;
|
|
|
|
|
2015-08-25 23:39:32 +00:00
|
|
|
Realm::Config& Realm::Config::operator=(realm::Realm::Config const& c)
|
|
|
|
{
|
|
|
|
if (&c != this) {
|
|
|
|
*this = Config(c);
|
2015-09-04 18:51:19 +00:00
|
|
|
}
|
2015-08-25 23:39:32 +00:00
|
|
|
return *this;
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 00:53:21 +00:00
|
|
|
Realm::Realm(Config config)
|
|
|
|
: m_config(std::move(config))
|
2015-06-18 23:04:41 +00:00
|
|
|
{
|
|
|
|
try {
|
2015-08-25 23:39:32 +00:00
|
|
|
if (m_config.read_only) {
|
|
|
|
m_read_only_group = std::make_unique<Group>(m_config.path, m_config.encryption_key.data(), Group::mode_ReadOnly);
|
2015-06-18 23:04:41 +00:00
|
|
|
m_group = m_read_only_group.get();
|
|
|
|
}
|
|
|
|
else {
|
2015-08-25 23:39:32 +00:00
|
|
|
m_history = realm::make_client_history(m_config.path, m_config.encryption_key.data());
|
|
|
|
SharedGroup::DurabilityLevel durability = m_config.in_memory ? SharedGroup::durability_MemOnly :
|
|
|
|
SharedGroup::durability_Full;
|
|
|
|
m_shared_group = std::make_unique<SharedGroup>(*m_history, durability, m_config.encryption_key.data());
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (util::File::PermissionDenied const& ex) {
|
2015-08-25 23:39:32 +00:00
|
|
|
throw RealmFileException(RealmFileException::Kind::PermissionDenied, "Unable to open a realm at path '" + m_config.path +
|
|
|
|
"'. Please use a path where your app has " + (m_config.read_only ? "read" : "read-write") + " permissions.");
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
catch (util::File::Exists const& ex) {
|
2015-08-25 23:39:32 +00:00
|
|
|
throw RealmFileException(RealmFileException::Kind::Exists, "Unable to open a realm at path '" + m_config.path + "'");
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
catch (util::File::AccessError const& ex) {
|
2015-08-25 23:39:32 +00:00
|
|
|
throw RealmFileException(RealmFileException::Kind::AccessError, "Unable to open a realm at path '" + m_config.path + "'");
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
catch (IncompatibleLockFile const&) {
|
2015-07-27 19:42:55 +00:00
|
|
|
throw RealmFileException(RealmFileException::Kind::IncompatibleLockFile, "Realm file is currently open in another process "
|
2015-06-18 23:04:41 +00:00
|
|
|
"which cannot share access with this process. All processes sharing a single file must be the same architecture.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 00:59:20 +00:00
|
|
|
Realm::~Realm() {
|
|
|
|
if (m_notifier) { // might not exist yet if an error occurred during init
|
|
|
|
m_notifier->remove_realm(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-27 19:42:55 +00:00
|
|
|
Group *Realm::read_group()
|
|
|
|
{
|
2015-06-18 23:04:41 +00:00
|
|
|
if (!m_group) {
|
|
|
|
m_group = &const_cast<Group&>(m_shared_group->begin_read());
|
|
|
|
}
|
|
|
|
return m_group;
|
|
|
|
}
|
|
|
|
|
2015-08-25 23:39:32 +00:00
|
|
|
SharedRealm Realm::get_shared_realm(Config config)
|
2015-06-18 23:04:41 +00:00
|
|
|
{
|
2015-09-01 17:59:28 +00:00
|
|
|
if (config.cache) {
|
|
|
|
if (SharedRealm realm = s_global_cache.get_realm(config.path)) {
|
|
|
|
if (realm->config().read_only != config.read_only) {
|
|
|
|
throw MismatchedConfigException("Realm at path already opened with different read permissions.");
|
|
|
|
}
|
|
|
|
if (realm->config().in_memory != config.in_memory) {
|
|
|
|
throw MismatchedConfigException("Realm at path already opened with different inMemory settings.");
|
|
|
|
}
|
|
|
|
if (realm->config().encryption_key != config.encryption_key) {
|
|
|
|
throw MismatchedConfigException("Realm at path already opened with a different encryption key.");
|
|
|
|
}
|
|
|
|
if (realm->config().schema_version != config.schema_version && config.schema_version != ObjectStore::NotVersioned) {
|
|
|
|
throw MismatchedConfigException("Realm at path already opened with different schema version.");
|
|
|
|
}
|
|
|
|
// FIXME - enable schma comparison
|
|
|
|
/*if (realm->config().schema != config.schema) {
|
|
|
|
throw MismatchedConfigException("Realm at path already opened with different schema");
|
|
|
|
}*/
|
|
|
|
realm->m_config.migration_function = config.migration_function;
|
2015-07-27 19:42:55 +00:00
|
|
|
|
2015-09-01 17:59:28 +00:00
|
|
|
return realm;
|
|
|
|
}
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-01 17:59:28 +00:00
|
|
|
SharedRealm realm(new Realm(std::move(config)));
|
2015-06-18 23:04:41 +00:00
|
|
|
|
2015-09-03 17:37:24 +00:00
|
|
|
auto target_schema = std::move(realm->m_config.schema);
|
|
|
|
auto target_schema_version = realm->m_config.schema_version;
|
|
|
|
realm->m_config.schema_version = ObjectStore::get_schema_version(realm->read_group());
|
|
|
|
|
2015-06-18 23:04:41 +00:00
|
|
|
// we want to ensure we are only initializing a single realm at a time
|
2015-08-24 20:14:59 +00:00
|
|
|
static std::mutex s_init_mutex;
|
2015-07-20 23:11:15 +00:00
|
|
|
std::lock_guard<std::mutex> lock(s_init_mutex);
|
2015-08-12 18:07:06 +00:00
|
|
|
if (auto existing = s_global_cache.get_any_realm(realm->config().path)) {
|
|
|
|
// if there is an existing realm at the current path steal its schema/column mapping
|
|
|
|
// FIXME - need to validate that schemas match
|
|
|
|
realm->m_config.schema = std::make_unique<Schema>(*existing->m_config.schema);
|
2015-09-04 00:59:20 +00:00
|
|
|
|
|
|
|
realm->m_notifier = existing->m_notifier;
|
|
|
|
realm->m_notifier->add_realm(realm.get());
|
2015-08-12 18:07:06 +00:00
|
|
|
}
|
2015-09-03 17:37:24 +00:00
|
|
|
else {
|
2015-09-04 00:59:20 +00:00
|
|
|
realm->m_notifier = std::make_shared<ExternalCommitHelper>(realm.get());
|
|
|
|
|
2015-09-03 17:37:24 +00:00
|
|
|
// otherwise get the schema from the group
|
2015-07-27 19:42:55 +00:00
|
|
|
realm->m_config.schema = std::make_unique<Schema>(ObjectStore::schema_from_group(realm->read_group()));
|
2015-09-03 17:37:24 +00:00
|
|
|
|
|
|
|
// if a target schema is supplied, verify that it matches or migrate to
|
|
|
|
// it, as neeeded
|
|
|
|
if (target_schema) {
|
|
|
|
if (realm->m_config.read_only) {
|
|
|
|
if (realm->m_config.schema_version == ObjectStore::NotVersioned) {
|
|
|
|
throw UnitializedRealmException("Can't open an un-initialized Realm without a Schema");
|
|
|
|
}
|
2015-09-04 21:03:58 +00:00
|
|
|
target_schema->validate();
|
2015-09-03 17:37:24 +00:00
|
|
|
ObjectStore::verify_schema(*realm->m_config.schema, *target_schema, true);
|
|
|
|
realm->m_config.schema = std::move(target_schema);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
realm->update_schema(std::move(target_schema), target_schema_version);
|
|
|
|
}
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 18:32:13 +00:00
|
|
|
if (config.cache) {
|
|
|
|
s_global_cache.cache_realm(realm, realm->m_thread_id);
|
|
|
|
}
|
2015-06-18 23:04:41 +00:00
|
|
|
return realm;
|
|
|
|
}
|
|
|
|
|
2015-09-03 17:37:24 +00:00
|
|
|
bool Realm::update_schema(std::unique_ptr<Schema> schema, uint64_t version)
|
2015-06-22 17:32:31 +00:00
|
|
|
{
|
2015-09-04 21:03:58 +00:00
|
|
|
schema->validate();
|
|
|
|
|
2015-09-03 17:37:24 +00:00
|
|
|
bool needs_update = !m_config.read_only && (m_config.schema_version != version || ObjectStore::needs_update(*m_config.schema, *schema));
|
|
|
|
if (!needs_update) {
|
|
|
|
ObjectStore::verify_schema(*m_config.schema, *schema, m_config.read_only);
|
|
|
|
m_config.schema = std::move(schema);
|
|
|
|
m_config.schema_version = version;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the old config/schema for the migration function, and update
|
|
|
|
// our schema to the new one
|
|
|
|
auto old_schema = std::move(m_config.schema);
|
2015-07-27 19:42:55 +00:00
|
|
|
Config old_config(m_config);
|
2015-09-03 17:37:24 +00:00
|
|
|
old_config.read_only = true;
|
|
|
|
old_config.schema = std::move(old_schema);
|
2015-07-27 19:42:55 +00:00
|
|
|
|
2015-09-03 17:37:24 +00:00
|
|
|
m_config.schema = std::move(schema);
|
2015-07-27 19:42:55 +00:00
|
|
|
m_config.schema_version = version;
|
|
|
|
|
2015-09-03 17:37:24 +00:00
|
|
|
auto migration_function = [&](Group*, Schema&) {
|
|
|
|
SharedRealm old_realm(new Realm(old_config));
|
|
|
|
auto updated_realm = shared_from_this();
|
|
|
|
m_config.migration_function(old_realm, updated_realm);
|
|
|
|
};
|
|
|
|
|
2015-06-22 17:32:31 +00:00
|
|
|
try {
|
2015-09-03 17:37:24 +00:00
|
|
|
// update and migrate
|
|
|
|
begin_transaction();
|
|
|
|
bool changed = ObjectStore::update_realm_with_schema(read_group(), *old_config.schema,
|
|
|
|
version, *m_config.schema,
|
|
|
|
migration_function);
|
|
|
|
commit_transaction();
|
|
|
|
return changed;
|
2015-06-22 17:32:31 +00:00
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
if (is_in_transaction()) {
|
|
|
|
cancel_transaction();
|
|
|
|
}
|
2015-07-27 19:42:55 +00:00
|
|
|
m_config.schema_version = old_config.schema_version;
|
2015-09-04 21:54:41 +00:00
|
|
|
m_config.schema = std::move(old_config.schema);
|
2015-06-22 17:32:31 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_read_write(Realm *realm)
|
|
|
|
{
|
2015-06-18 23:04:41 +00:00
|
|
|
if (realm->config().read_only) {
|
2015-07-27 19:42:55 +00:00
|
|
|
throw InvalidTransactionException("Can't perform transactions on read-only Realms.");
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 16:40:29 +00:00
|
|
|
void Realm::verify_thread() const
|
2015-06-22 17:32:31 +00:00
|
|
|
{
|
2015-07-20 23:11:15 +00:00
|
|
|
if (m_thread_id != std::this_thread::get_id()) {
|
2015-07-27 19:42:55 +00:00
|
|
|
throw IncorrectThreadException("Realm accessed from incorrect thread.");
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Realm::begin_transaction()
|
|
|
|
{
|
|
|
|
check_read_write(this);
|
|
|
|
verify_thread();
|
|
|
|
|
|
|
|
if (m_in_transaction) {
|
2015-07-27 19:42:55 +00:00
|
|
|
throw InvalidTransactionException("The Realm is already in a write transaction");
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we have a read transaction
|
|
|
|
read_group();
|
|
|
|
|
2015-09-02 20:51:28 +00:00
|
|
|
transaction::begin(*m_shared_group, *m_history, m_delegate.get());
|
2015-06-18 23:04:41 +00:00
|
|
|
m_in_transaction = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Realm::commit_transaction()
|
|
|
|
{
|
|
|
|
check_read_write(this);
|
|
|
|
verify_thread();
|
|
|
|
|
|
|
|
if (!m_in_transaction) {
|
2015-07-27 19:42:55 +00:00
|
|
|
throw InvalidTransactionException("Can't commit a non-existing write transaction");
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_in_transaction = false;
|
2015-09-02 20:51:28 +00:00
|
|
|
transaction::commit(*m_shared_group, *m_history, m_delegate.get());
|
2015-09-03 00:53:21 +00:00
|
|
|
m_notifier->notify_others();
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Realm::cancel_transaction()
|
|
|
|
{
|
|
|
|
check_read_write(this);
|
|
|
|
verify_thread();
|
|
|
|
|
|
|
|
if (!m_in_transaction) {
|
2015-07-27 19:42:55 +00:00
|
|
|
throw InvalidTransactionException("Can't cancel a non-existing write transaction");
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_in_transaction = false;
|
2015-09-02 20:51:28 +00:00
|
|
|
transaction::cancel(*m_shared_group, *m_history, m_delegate.get());
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Realm::invalidate()
|
|
|
|
{
|
|
|
|
verify_thread();
|
|
|
|
check_read_write(this);
|
|
|
|
|
|
|
|
if (m_in_transaction) {
|
|
|
|
cancel_transaction();
|
|
|
|
}
|
|
|
|
if (!m_group) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_shared_group->end_read();
|
|
|
|
m_group = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Realm::compact()
|
|
|
|
{
|
|
|
|
verify_thread();
|
|
|
|
|
2015-09-04 18:34:02 +00:00
|
|
|
if (m_config.read_only) {
|
|
|
|
throw InvalidTransactionException("Can't compact a read-only Realm");
|
|
|
|
}
|
2015-06-18 23:04:41 +00:00
|
|
|
if (m_in_transaction) {
|
2015-07-27 19:42:55 +00:00
|
|
|
throw InvalidTransactionException("Can't compact a Realm within a write transaction");
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 18:34:02 +00:00
|
|
|
Group* group = read_group();
|
2015-06-18 23:04:41 +00:00
|
|
|
for (auto &object_schema : *m_config.schema) {
|
2015-09-04 18:34:02 +00:00
|
|
|
ObjectStore::table_for_object_type(group, object_schema.name)->optimize();
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
m_shared_group->end_read();
|
2015-09-04 18:34:02 +00:00
|
|
|
m_group = nullptr;
|
2015-06-18 23:04:41 +00:00
|
|
|
|
2015-09-04 18:34:02 +00:00
|
|
|
return m_shared_group->compact();
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Realm::notify()
|
|
|
|
{
|
|
|
|
verify_thread();
|
|
|
|
|
|
|
|
if (m_shared_group->has_changed()) { // Throws
|
2015-09-10 20:49:50 +00:00
|
|
|
if (m_delegate) {
|
|
|
|
m_delegate->changes_available();
|
|
|
|
}
|
2015-06-18 23:04:41 +00:00
|
|
|
if (m_auto_refresh) {
|
|
|
|
if (m_group) {
|
2015-09-02 20:51:28 +00:00
|
|
|
transaction::advance(*m_shared_group, *m_history, m_delegate.get());
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
2015-08-31 18:20:20 +00:00
|
|
|
else if (m_delegate) {
|
|
|
|
m_delegate->did_change({}, {});
|
2015-08-26 23:11:45 +00:00
|
|
|
}
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Realm::refresh()
|
|
|
|
{
|
|
|
|
verify_thread();
|
|
|
|
check_read_write(this);
|
|
|
|
|
|
|
|
// can't be any new changes if we're in a write transaction
|
|
|
|
if (m_in_transaction) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// advance transaction if database has changed
|
|
|
|
if (!m_shared_group->has_changed()) { // Throws
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_group) {
|
2015-09-02 20:51:28 +00:00
|
|
|
transaction::advance(*m_shared_group, *m_history, m_delegate.get());
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Create the read transaction
|
|
|
|
read_group();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-27 18:38:30 +00:00
|
|
|
uint64_t Realm::get_schema_version(const realm::Realm::Config &config)
|
|
|
|
{
|
2015-09-04 18:30:00 +00:00
|
|
|
if (auto existing_realm = s_global_cache.get_any_realm(config.path)) {
|
2015-08-27 18:38:30 +00:00
|
|
|
return existing_realm->config().schema_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ObjectStore::get_schema_version(Realm(config).read_group());
|
|
|
|
}
|
|
|
|
|
2015-06-18 23:04:41 +00:00
|
|
|
SharedRealm RealmCache::get_realm(const std::string &path, std::thread::id thread_id)
|
|
|
|
{
|
2015-07-20 23:11:15 +00:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2015-06-18 23:04:41 +00:00
|
|
|
|
|
|
|
auto path_iter = m_cache.find(path);
|
|
|
|
if (path_iter == m_cache.end()) {
|
|
|
|
return SharedRealm();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto thread_iter = path_iter->second.find(thread_id);
|
|
|
|
if (thread_iter == path_iter->second.end()) {
|
|
|
|
return SharedRealm();
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread_iter->second.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedRealm RealmCache::get_any_realm(const std::string &path)
|
|
|
|
{
|
2015-07-20 23:11:15 +00:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2015-06-18 23:04:41 +00:00
|
|
|
|
|
|
|
auto path_iter = m_cache.find(path);
|
|
|
|
if (path_iter == m_cache.end()) {
|
|
|
|
return SharedRealm();
|
|
|
|
}
|
|
|
|
|
2015-08-12 18:07:06 +00:00
|
|
|
auto thread_iter = path_iter->second.begin();
|
|
|
|
while (thread_iter != path_iter->second.end()) {
|
2015-06-18 23:04:41 +00:00
|
|
|
if (auto realm = thread_iter->second.lock()) {
|
|
|
|
return realm;
|
|
|
|
}
|
2015-08-12 18:07:06 +00:00
|
|
|
path_iter->second.erase(thread_iter++);
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SharedRealm();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RealmCache::remove(const std::string &path, std::thread::id thread_id)
|
|
|
|
{
|
2015-07-20 23:11:15 +00:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2015-06-18 23:04:41 +00:00
|
|
|
|
|
|
|
auto path_iter = m_cache.find(path);
|
|
|
|
if (path_iter == m_cache.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto thread_iter = path_iter->second.find(thread_id);
|
2015-07-27 19:42:55 +00:00
|
|
|
if (thread_iter != path_iter->second.end()) {
|
2015-06-18 23:04:41 +00:00
|
|
|
path_iter->second.erase(thread_iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path_iter->second.size() == 0) {
|
|
|
|
m_cache.erase(path_iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RealmCache::cache_realm(SharedRealm &realm, std::thread::id thread_id)
|
|
|
|
{
|
2015-07-20 23:11:15 +00:00
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
2015-06-18 23:04:41 +00:00
|
|
|
|
|
|
|
auto path_iter = m_cache.find(realm->config().path);
|
|
|
|
if (path_iter == m_cache.end()) {
|
2015-07-20 23:11:15 +00:00
|
|
|
m_cache.emplace(realm->config().path, std::map<std::thread::id, WeakRealm>{{thread_id, realm}});
|
2015-06-18 23:04:41 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
path_iter->second.emplace(thread_id, realm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 19:02:56 +00:00
|
|
|
void RealmCache::clear()
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
|
|
|
|
|
|
m_cache.clear();
|
|
|
|
}
|