This commit is contained in:
Ari Lazier 2015-07-20 16:11:15 -07:00
parent d8e9d36c88
commit eb2b079e2a
5 changed files with 38 additions and 42 deletions

View File

@ -23,7 +23,6 @@
#include <realm/table.hpp> #include <realm/table.hpp>
using namespace realm; using namespace realm;
using namespace std;
ObjectSchema::ObjectSchema(Group *group, const std::string &name) : name(name) { ObjectSchema::ObjectSchema(Group *group, const std::string &name) : name(name) {
TableRef tableRef = ObjectStore::table_for_object_type(group, name); TableRef tableRef = ObjectStore::table_for_object_type(group, name);
@ -48,7 +47,7 @@ ObjectSchema::ObjectSchema(Group *group, const std::string &name) : name(name) {
realm::TableRef linkTable = table->get_link_target(col); realm::TableRef linkTable = table->get_link_target(col);
property.object_type = ObjectStore::object_type_for_table_name(linkTable->get_name().data()); property.object_type = ObjectStore::object_type_for_table_name(linkTable->get_name().data());
} }
properties.push_back(move(property)); properties.push_back(std::move(property));
} }
primary_key = realm::ObjectStore::get_primary_key_for_object(group, name); primary_key = realm::ObjectStore::get_primary_key_for_object(group, name);

View File

@ -27,7 +27,6 @@
#include <string.h> #include <string.h>
using namespace realm; using namespace realm;
using namespace std;
template <typename T> template <typename T>
static inline static inline
@ -65,10 +64,10 @@ const size_t c_primaryKeyPropertyNameColumnIndex = 1;
const size_t c_zeroRowIndex = 0; const size_t c_zeroRowIndex = 0;
const string c_object_table_prefix = "class_"; const std::string c_object_table_prefix = "class_";
const size_t c_object_table_prefix_length = c_object_table_prefix.length(); const size_t c_object_table_prefix_length = c_object_table_prefix.length();
const uint64_t ObjectStore::NotVersioned = numeric_limits<uint64_t>::max(); const uint64_t ObjectStore::NotVersioned = std::numeric_limits<uint64_t>::max();
bool ObjectStore::has_metadata_tables(Group *group) { bool ObjectStore::has_metadata_tables(Group *group) {
return group->get_table(c_primaryKeyTableName) && group->get_table(c_metadataTableName); return group->get_table(c_primaryKeyTableName) && group->get_table(c_metadataTableName);
@ -142,14 +141,14 @@ void ObjectStore::set_primary_key_for_object(Group *group, StringData object_typ
} }
} }
string ObjectStore::object_type_for_table_name(const string &table_name) { std::string ObjectStore::object_type_for_table_name(const std::string &table_name) {
if (table_name.size() >= c_object_table_prefix_length && table_name.compare(0, c_object_table_prefix_length, c_object_table_prefix) == 0) { if (table_name.size() >= c_object_table_prefix_length && table_name.compare(0, c_object_table_prefix_length, c_object_table_prefix) == 0) {
return table_name.substr(c_object_table_prefix_length, table_name.length() - c_object_table_prefix_length); return table_name.substr(c_object_table_prefix_length, table_name.length() - c_object_table_prefix_length);
} }
return string(); return std::string();
} }
string ObjectStore::table_name_for_object_type(const string &object_type) { std::string ObjectStore::table_name_for_object_type(const std::string &object_type) {
return c_object_table_prefix + object_type; return c_object_table_prefix + object_type;
} }
@ -162,7 +161,7 @@ TableRef ObjectStore::table_for_object_type_create_if_needed(Group *group, const
} }
std::vector<ObjectStoreException> ObjectStore::validate_object_schema(Group *group, ObjectSchema &target_schema) { std::vector<ObjectStoreException> ObjectStore::validate_object_schema(Group *group, ObjectSchema &target_schema) {
vector<ObjectStoreException> exceptions; std::vector<ObjectStoreException> exceptions;
ObjectSchema table_schema(group, target_schema.name); ObjectSchema table_schema(group, target_schema.name);
// check to see if properties are the same // check to see if properties are the same
@ -259,7 +258,7 @@ bool ObjectStore::create_tables(Group *group, ObjectStore::Schema &target_schema
bool changed = false; bool changed = false;
// first pass to create missing tables // first pass to create missing tables
vector<ObjectSchema *> to_update; std::vector<ObjectSchema *> to_update;
for (auto& object_schema : target_schema) { for (auto& object_schema : target_schema) {
bool created = false; bool created = false;
ObjectStore::table_for_object_type_create_if_needed(group, object_schema.name, created); ObjectStore::table_for_object_type_create_if_needed(group, object_schema.name, created);
@ -275,7 +274,7 @@ bool ObjectStore::create_tables(Group *group, ObjectStore::Schema &target_schema
for (auto& target_object_schema : to_update) { for (auto& target_object_schema : to_update) {
TableRef table = table_for_object_type(group, target_object_schema->name); TableRef table = table_for_object_type(group, target_object_schema->name);
ObjectSchema current_schema(group, target_object_schema->name); ObjectSchema current_schema(group, target_object_schema->name);
vector<Property> &target_props = target_object_schema->properties; std::vector<Property> &target_props = target_object_schema->properties;
// add missing columns // add missing columns
for (auto& target_prop : target_props) { for (auto& target_prop : target_props) {
@ -399,7 +398,7 @@ bool ObjectStore::update_realm_with_schema(Group *group,
ObjectStore::Schema ObjectStore::schema_from_group(Group *group) { ObjectStore::Schema ObjectStore::schema_from_group(Group *group) {
ObjectStore::Schema schema; ObjectStore::Schema schema;
for (size_t i = 0; i < group->size(); i++) { for (size_t i = 0; i < group->size(); i++) {
string object_type = object_type_for_table_name(group->get_table_name(i)); std::string object_type = object_type_for_table_name(group->get_table_name(i));
if (object_type.length()) { if (object_type.length()) {
schema.emplace_back(group, move(object_type)); schema.emplace_back(group, move(object_type));
} }

View File

@ -23,7 +23,6 @@
#include <regex> #include <regex>
using namespace realm; using namespace realm;
using namespace std;
ObjectStoreException::ObjectStoreException(Kind kind, Info info) : m_kind(kind), m_info(info), m_what(generate_what()) {} ObjectStoreException::ObjectStoreException(Kind kind, Info info) : m_kind(kind), m_info(info), m_what(generate_what()) {}
@ -53,18 +52,18 @@ ObjectStoreException::ObjectStoreException(Kind kind, const std::string &object_
} }
ObjectStoreException::ObjectStoreException(uint64_t old_version, uint64_t new_version) : m_kind(Kind::RealmVersionGreaterThanSchemaVersion) { ObjectStoreException::ObjectStoreException(uint64_t old_version, uint64_t new_version) : m_kind(Kind::RealmVersionGreaterThanSchemaVersion) {
m_info[InfoKeyOldVersion] = to_string(old_version); m_info[InfoKeyOldVersion] = std::to_string(old_version);
m_info[InfoKeyNewVersion] = to_string(new_version); m_info[InfoKeyNewVersion] = std::to_string(new_version);
m_what = generate_what(); m_what = generate_what();
} }
ObjectStoreException::ObjectStoreException(vector<ObjectStoreException> validation_errors, const string &object_type) : ObjectStoreException::ObjectStoreException(std::vector<ObjectStoreException> validation_errors, const std::string &object_type) :
m_validation_errors(validation_errors), m_kind(Kind::ObjectStoreValidationFailure) { m_validation_errors(validation_errors), m_kind(Kind::ObjectStoreValidationFailure) {
m_info[InfoKeyObjectType] = object_type; m_info[InfoKeyObjectType] = object_type;
m_what = generate_what(); m_what = generate_what();
} }
string ObjectStoreException::generate_what() const { std::string ObjectStoreException::generate_what() const {
auto format_string = s_custom_format_strings.find(m_kind); auto format_string = s_custom_format_strings.find(m_kind);
if (format_string != s_custom_format_strings.end()) { if (format_string != s_custom_format_strings.end()) {
return populate_format_string(format_string->second); return populate_format_string(format_string->second);
@ -72,21 +71,21 @@ string ObjectStoreException::generate_what() const {
return populate_format_string(s_default_format_strings.at(m_kind)); return populate_format_string(s_default_format_strings.at(m_kind));
} }
string ObjectStoreException::validation_errors_string() const { std::string ObjectStoreException::validation_errors_string() const {
string errors_string; std::string errors_string;
for (auto error : m_validation_errors) { for (auto error : m_validation_errors) {
errors_string += string("\n- ") + error.what(); errors_string += std::string("\n- ") + error.what();
} }
return errors_string; return errors_string;
} }
std::string ObjectStoreException::populate_format_string(const std::string & format_string) const { std::string ObjectStoreException::populate_format_string(const std::string & format_string) const {
string out_string, current(format_string); std::string out_string, current(format_string);
smatch sm; std::smatch sm;
regex re("\\{(\\w+)\\}"); std::regex re("\\{(\\w+)\\}");
while(regex_search(current, sm, re)) { while(regex_search(current, sm, re)) {
out_string += sm.prefix(); out_string += sm.prefix();
const string &key = sm[1]; const std::string &key = sm[1];
if (key == "ValidationErrors") { if (key == "ValidationErrors") {
out_string += validation_errors_string(); out_string += validation_errors_string();
} }

View File

@ -43,8 +43,8 @@ namespace realm {
ObjectStoreValidationFailure, // ObjectType, vector<ObjectStoreException> ObjectStoreValidationFailure, // ObjectType, vector<ObjectStoreException>
}; };
typedef const std::string InfoKey; using InfoKey = std::string;
typedef std::map<InfoKey, std::string> Info; using Info = std::map<InfoKey, std::string>;
ObjectStoreException(Kind kind, const std::string &object_type, const Property &prop); ObjectStoreException(Kind kind, const std::string &object_type, const Property &prop);

View File

@ -20,11 +20,10 @@
#include <realm/commit_log.hpp> #include <realm/commit_log.hpp>
using namespace std;
using namespace realm; using namespace realm;
RealmCache Realm::s_global_cache; RealmCache Realm::s_global_cache;
mutex Realm::s_init_mutex; std::mutex Realm::s_init_mutex;
Realm::Config::Config(const Config& c) : path(c.path), read_only(c.read_only), in_memory(c.in_memory), schema_version(c.schema_version), encryption_key(c.encryption_key), migration_function(c.migration_function) Realm::Config::Config(const Config& c) : path(c.path), read_only(c.read_only), in_memory(c.in_memory), schema_version(c.schema_version), encryption_key(c.encryption_key), migration_function(c.migration_function)
{ {
@ -33,18 +32,18 @@ Realm::Config::Config(const Config& c) : path(c.path), read_only(c.read_only), i
} }
} }
Realm::Realm(Config &config) : m_config(config), m_thread_id(this_thread::get_id()), m_auto_refresh(true), m_in_transaction(false) Realm::Realm(Config &config) : m_config(config), m_thread_id(std::this_thread::get_id()), m_auto_refresh(true), m_in_transaction(false)
{ {
try { try {
if (config.read_only) { if (config.read_only) {
m_read_only_group = make_unique<Group>(config.path, config.encryption_key.data(), Group::mode_ReadOnly); m_read_only_group = std::make_unique<Group>(config.path, config.encryption_key.data(), Group::mode_ReadOnly);
m_group = m_read_only_group.get(); m_group = m_read_only_group.get();
} }
else { else {
m_history = realm::make_client_history(config.path, config.encryption_key.data()); m_history = realm::make_client_history(config.path, config.encryption_key.data());
SharedGroup::DurabilityLevel durability = config.in_memory ? SharedGroup::durability_MemOnly : SharedGroup::DurabilityLevel durability = config.in_memory ? SharedGroup::durability_MemOnly :
SharedGroup::durability_Full; SharedGroup::durability_Full;
m_shared_group = make_unique<SharedGroup>(*m_history, durability, config.encryption_key.data()); m_shared_group = std::make_unique<SharedGroup>(*m_history, durability, config.encryption_key.data());
m_group = nullptr; m_group = nullptr;
} }
} }
@ -87,12 +86,12 @@ SharedRealm Realm::get_shared_realm(Config &config)
realm = SharedRealm(new Realm(config)); realm = SharedRealm(new Realm(config));
// we want to ensure we are only initializing a single realm at a time // we want to ensure we are only initializing a single realm at a time
lock_guard<mutex> lock(s_init_mutex); std::lock_guard<std::mutex> lock(s_init_mutex);
if (!config.schema) { if (!config.schema) {
// get schema from group and skip validation // get schema from group and skip validation
realm->m_config.schema_version = ObjectStore::get_schema_version(realm->read_group()); realm->m_config.schema_version = ObjectStore::get_schema_version(realm->read_group());
realm->m_config.schema = make_unique<ObjectStore::Schema>(ObjectStore::schema_from_group(realm->read_group())); realm->m_config.schema = std::make_unique<ObjectStore::Schema>(ObjectStore::schema_from_group(realm->read_group()));
} }
else if (config.read_only) { else if (config.read_only) {
// for read-only validate all existing tables // for read-only validate all existing tables
@ -105,7 +104,7 @@ SharedRealm Realm::get_shared_realm(Config &config)
else if(auto existing = s_global_cache.get_any_realm(realm->config().path)) { else 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 // if there is an existing realm at the current path steal its schema/column mapping
// FIXME - need to validate that schemas match // FIXME - need to validate that schemas match
realm->m_config.schema = make_unique<ObjectStore::Schema>(*existing->m_config.schema); realm->m_config.schema = std::make_unique<ObjectStore::Schema>(*existing->m_config.schema);
} }
else { else {
// its a non-cached realm so update/migrate if needed // its a non-cached realm so update/migrate if needed
@ -125,7 +124,7 @@ bool Realm::update_schema(ObjectStore::Schema &schema, uint64_t version)
commit_transaction(); commit_transaction();
m_config.schema_version = version; m_config.schema_version = version;
if (m_config.schema.get() != &schema) { if (m_config.schema.get() != &schema) {
m_config.schema = make_unique<ObjectStore::Schema>(schema); m_config.schema = std::make_unique<ObjectStore::Schema>(schema);
} }
} }
catch (...) { catch (...) {
@ -147,7 +146,7 @@ static void check_read_write(Realm *realm)
void Realm::verify_thread() void Realm::verify_thread()
{ {
if (m_thread_id != this_thread::get_id()) { if (m_thread_id != std::this_thread::get_id()) {
throw RealmException(RealmException::Kind::IncorrectThread, "Realm accessed from incorrect thread."); throw RealmException(RealmException::Kind::IncorrectThread, "Realm accessed from incorrect thread.");
} }
} }
@ -259,7 +258,7 @@ void Realm::notify()
} }
void Realm::send_local_notifications(const string &type) void Realm::send_local_notifications(const std::string &type)
{ {
verify_thread(); verify_thread();
for (NotificationFunction notification : m_notifications) { for (NotificationFunction notification : m_notifications) {
@ -297,7 +296,7 @@ bool Realm::refresh()
SharedRealm RealmCache::get_realm(const std::string &path, std::thread::id thread_id) SharedRealm RealmCache::get_realm(const std::string &path, std::thread::id thread_id)
{ {
lock_guard<mutex> lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
auto path_iter = m_cache.find(path); auto path_iter = m_cache.find(path);
if (path_iter == m_cache.end()) { if (path_iter == m_cache.end()) {
@ -314,7 +313,7 @@ SharedRealm RealmCache::get_realm(const std::string &path, std::thread::id threa
SharedRealm RealmCache::get_any_realm(const std::string &path) SharedRealm RealmCache::get_any_realm(const std::string &path)
{ {
lock_guard<mutex> lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
auto path_iter = m_cache.find(path); auto path_iter = m_cache.find(path);
if (path_iter == m_cache.end()) { if (path_iter == m_cache.end()) {
@ -333,7 +332,7 @@ SharedRealm RealmCache::get_any_realm(const std::string &path)
void RealmCache::remove(const std::string &path, std::thread::id thread_id) void RealmCache::remove(const std::string &path, std::thread::id thread_id)
{ {
lock_guard<mutex> lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
auto path_iter = m_cache.find(path); auto path_iter = m_cache.find(path);
if (path_iter == m_cache.end()) { if (path_iter == m_cache.end()) {
@ -352,11 +351,11 @@ void RealmCache::remove(const std::string &path, std::thread::id thread_id)
void RealmCache::cache_realm(SharedRealm &realm, std::thread::id thread_id) void RealmCache::cache_realm(SharedRealm &realm, std::thread::id thread_id)
{ {
lock_guard<mutex> lock(m_mutex); std::lock_guard<std::mutex> lock(m_mutex);
auto path_iter = m_cache.find(realm->config().path); auto path_iter = m_cache.find(realm->config().path);
if (path_iter == m_cache.end()) { if (path_iter == m_cache.end()) {
m_cache.emplace(realm->config().path, map<std::thread::id, WeakRealm>{{thread_id, realm}}); m_cache.emplace(realm->config().path, std::map<std::thread::id, WeakRealm>{{thread_id, realm}});
} }
else { else {
path_iter->second.emplace(thread_id, realm); path_iter->second.emplace(thread_id, realm);