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>
using namespace realm;
using namespace std;
ObjectSchema::ObjectSchema(Group *group, const std::string &name) : name(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);
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);

View File

@ -27,7 +27,6 @@
#include <string.h>
using namespace realm;
using namespace std;
template <typename T>
static inline
@ -65,10 +64,10 @@ const size_t c_primaryKeyPropertyNameColumnIndex = 1;
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 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) {
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) {
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;
}
@ -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) {
vector<ObjectStoreException> exceptions;
std::vector<ObjectStoreException> exceptions;
ObjectSchema table_schema(group, target_schema.name);
// 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;
// first pass to create missing tables
vector<ObjectSchema *> to_update;
std::vector<ObjectSchema *> to_update;
for (auto& object_schema : target_schema) {
bool created = false;
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) {
TableRef table = table_for_object_type(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
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 schema;
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()) {
schema.emplace_back(group, move(object_type));
}

View File

@ -23,7 +23,6 @@
#include <regex>
using namespace realm;
using namespace std;
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) {
m_info[InfoKeyOldVersion] = to_string(old_version);
m_info[InfoKeyNewVersion] = to_string(new_version);
m_info[InfoKeyOldVersion] = std::to_string(old_version);
m_info[InfoKeyNewVersion] = std::to_string(new_version);
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_info[InfoKeyObjectType] = object_type;
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);
if (format_string != s_custom_format_strings.end()) {
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));
}
string ObjectStoreException::validation_errors_string() const {
string errors_string;
std::string ObjectStoreException::validation_errors_string() const {
std::string errors_string;
for (auto error : m_validation_errors) {
errors_string += string("\n- ") + error.what();
errors_string += std::string("\n- ") + error.what();
}
return errors_string;
}
std::string ObjectStoreException::populate_format_string(const std::string & format_string) const {
string out_string, current(format_string);
smatch sm;
regex re("\\{(\\w+)\\}");
std::string out_string, current(format_string);
std::smatch sm;
std::regex re("\\{(\\w+)\\}");
while(regex_search(current, sm, re)) {
out_string += sm.prefix();
const string &key = sm[1];
const std::string &key = sm[1];
if (key == "ValidationErrors") {
out_string += validation_errors_string();
}

View File

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

View File

@ -20,11 +20,10 @@
#include <realm/commit_log.hpp>
using namespace std;
using namespace realm;
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)
{
@ -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 {
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();
}
else {
m_history = realm::make_client_history(config.path, config.encryption_key.data());
SharedGroup::DurabilityLevel durability = config.in_memory ? SharedGroup::durability_MemOnly :
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;
}
}
@ -87,12 +86,12 @@ SharedRealm Realm::get_shared_realm(Config &config)
realm = SharedRealm(new Realm(config));
// 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) {
// get schema from group and skip validation
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) {
// 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)) {
// 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 = make_unique<ObjectStore::Schema>(*existing->m_config.schema);
realm->m_config.schema = std::make_unique<ObjectStore::Schema>(*existing->m_config.schema);
}
else {
// 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();
m_config.schema_version = version;
if (m_config.schema.get() != &schema) {
m_config.schema = make_unique<ObjectStore::Schema>(schema);
m_config.schema = std::make_unique<ObjectStore::Schema>(schema);
}
}
catch (...) {
@ -147,7 +146,7 @@ static void check_read_write(Realm *realm)
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.");
}
}
@ -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();
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)
{
lock_guard<mutex> lock(m_mutex);
std::lock_guard<std::mutex> lock(m_mutex);
auto path_iter = m_cache.find(path);
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)
{
lock_guard<mutex> lock(m_mutex);
std::lock_guard<std::mutex> lock(m_mutex);
auto path_iter = m_cache.find(path);
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)
{
lock_guard<mutex> lock(m_mutex);
std::lock_guard<std::mutex> lock(m_mutex);
auto path_iter = m_cache.find(path);
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)
{
lock_guard<mutex> lock(m_mutex);
std::lock_guard<std::mutex> lock(m_mutex);
auto path_iter = m_cache.find(realm->config().path);
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 {
path_iter->second.emplace(thread_id, realm);