From 86e95fb9b120cf6063552e273ed7473976e26b31 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Thu, 17 Dec 2015 15:59:34 -0800 Subject: [PATCH] Schema stored in Realm::Config should be const --- src/js_object.cpp | 2 +- src/js_realm.cpp | 2 +- src/js_results.cpp | 2 +- src/object-store/object_accessor.hpp | 6 +++--- src/object-store/parser/query_builder.cpp | 12 ++++++------ src/object-store/parser/query_builder.hpp | 2 +- src/object-store/shared_realm.cpp | 14 ++++++-------- src/object-store/shared_realm.hpp | 4 ++-- 8 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/js_object.cpp b/src/js_object.cpp index 9b0f95c9..9ee910c5 100644 --- a/src/js_object.cpp +++ b/src/js_object.cpp @@ -215,7 +215,7 @@ template<> JSValueRef RJSAccessor::from_datetime(JSContextRef ctx, DateTime dt) return JSObjectMakeDate(ctx, 1, &time, NULL); } -extern JSObjectRef RJSDictForPropertyArray(JSContextRef ctx, ObjectSchema &object_schema, JSObjectRef array); +extern JSObjectRef RJSDictForPropertyArray(JSContextRef ctx, const ObjectSchema &object_schema, JSObjectRef array); template<> size_t RJSAccessor::to_existing_object_index(JSContextRef ctx, JSValueRef &val) { JSObjectRef object = RJSValidatedValueToObject(ctx, val); diff --git a/src/js_realm.cpp b/src/js_realm.cpp index e31754c5..641e0792 100644 --- a/src/js_realm.cpp +++ b/src/js_realm.cpp @@ -245,7 +245,7 @@ JSValueRef RealmObjects(JSContextRef ctx, JSObjectRef function, JSObjectRef this } } -JSObjectRef RJSDictForPropertyArray(JSContextRef ctx, ObjectSchema &object_schema, JSObjectRef array) { +JSObjectRef RJSDictForPropertyArray(JSContextRef ctx, const ObjectSchema &object_schema, JSObjectRef array) { // copy to dictionary if (object_schema.properties.size() != RJSValidatedListLength(ctx, array)) { throw std::runtime_error("Array must contain values for all object properties"); diff --git a/src/js_results.cpp b/src/js_results.cpp index f77f55b0..789d18da 100644 --- a/src/js_results.cpp +++ b/src/js_results.cpp @@ -109,7 +109,7 @@ JSObjectRef RJSResultsCreate(JSContextRef ctx, SharedRealm realm, std::string cl JSObjectRef RJSResultsCreate(JSContextRef ctx, SharedRealm realm, std::string className, std::string queryString, std::vector args) { TableRef table = ObjectStore::table_for_object_type(realm->read_group(), className); Query query = table->where(); - Schema &schema = *realm->config().schema; + const Schema &schema = *realm->config().schema; auto object_schema = schema.find(className); if (object_schema == schema.end()) { throw std::runtime_error("Object type '" + className + "' not present in Realm."); diff --git a/src/object-store/object_accessor.hpp b/src/object-store/object_accessor.hpp index 2f52619c..5d290d43 100644 --- a/src/object-store/object_accessor.hpp +++ b/src/object-store/object_accessor.hpp @@ -25,7 +25,7 @@ namespace realm { // create an Object from a native representation template - static inline Object create(ContextType ctx, SharedRealm realm, ObjectSchema &object_schema, ValueType value, bool try_update); + static inline Object create(ContextType ctx, SharedRealm realm, const ObjectSchema &object_schema, ValueType value, bool try_update); const ObjectSchema object_schema; SharedRealm realm() { return m_realm; } @@ -243,7 +243,7 @@ namespace realm { } template - inline Object Object::create(ContextType ctx, SharedRealm realm, ObjectSchema &object_schema, ValueType value, bool try_update) + inline Object Object::create(ContextType ctx, SharedRealm realm, const ObjectSchema &object_schema, ValueType value, bool try_update) { using Accessor = NativeAccessor; @@ -283,7 +283,7 @@ namespace realm { // populate Object object(realm, object_schema, table->get(row_index)); - for (Property &prop : object_schema.properties) { + for (const Property &prop : object_schema.properties) { if (created || !prop.is_primary) { if (Accessor::dict_has_value_for_key(ctx, value, prop.name)) { object.set_property_value_impl(ctx, prop, Accessor::dict_value_for_key(ctx, value, prop.name), try_update); diff --git a/src/object-store/parser/query_builder.cpp b/src/object-store/parser/query_builder.cpp index d7f26e07..6f74f3b2 100644 --- a/src/object-store/parser/query_builder.cpp +++ b/src/object-store/parser/query_builder.cpp @@ -66,11 +66,11 @@ KeyPath key_path_from_string(const std::string &s) { struct PropertyExpression { - Property *prop = nullptr; + const Property *prop = nullptr; std::vector indexes; std::function table_getter; - PropertyExpression(Query &query, Schema &schema, Schema::iterator desc, const std::string &key_path_string) + PropertyExpression(Query &query, const Schema &schema, Schema::const_iterator desc, const std::string &key_path_string) { KeyPath key_path = key_path_from_string(key_path_string); for (size_t index = 0; index < key_path.size(); index++) { @@ -375,7 +375,7 @@ auto value_of_type_for_query(TableGetter&& tables, Value&& value, Arguments &arg } template -void do_add_comparison_to_query(Query &query, Schema &schema, ObjectSchema &object_schema, Predicate::Operator op, +void do_add_comparison_to_query(Query &query, const Schema &schema, const ObjectSchema &object_schema, Predicate::Operator op, PropertyExpression &expr, A &lhs, B &rhs, Arguments &args) { auto type = expr.prop->type; @@ -418,7 +418,7 @@ void do_add_comparison_to_query(Query &query, Schema &schema, ObjectSchema &obje } } -void add_comparison_to_query(Query &query, Predicate &pred, Arguments &args, Schema &schema, const std::string &type) +void add_comparison_to_query(Query &query, Predicate &pred, Arguments &args, const Schema &schema, const std::string &type) { Predicate::Comparison &cmpr = pred.cmpr; auto t0 = cmpr.expr[0].type, t1 = cmpr.expr[1].type; @@ -436,7 +436,7 @@ void add_comparison_to_query(Query &query, Predicate &pred, Arguments &args, Sch } } -void update_query_with_predicate(Query &query, Predicate &pred, Arguments &arguments, Schema &schema, const std::string &type) +void update_query_with_predicate(Query &query, Predicate &pred, Arguments &arguments, const Schema &schema, const std::string &type) { if (pred.negate) { query.Not(); @@ -484,7 +484,7 @@ void update_query_with_predicate(Query &query, Predicate &pred, Arguments &argum } } -void apply_predicate(Query &query, Predicate &predicate, Arguments &arguments, Schema &schema, std::string objectType) +void apply_predicate(Query &query, Predicate &predicate, Arguments &arguments, const Schema &schema, std::string objectType) { update_query_with_predicate(query, predicate, arguments, schema, objectType); diff --git a/src/object-store/parser/query_builder.hpp b/src/object-store/parser/query_builder.hpp index 2d7f9d7c..713ef6ac 100644 --- a/src/object-store/parser/query_builder.hpp +++ b/src/object-store/parser/query_builder.hpp @@ -30,7 +30,7 @@ namespace realm { namespace query_builder { class Arguments; - void apply_predicate(Query &query, parser::Predicate &predicate, Arguments &arguments, Schema &schema, std::string objectType); + void apply_predicate(Query &query, parser::Predicate &predicate, Arguments &arguments, const Schema &schema, std::string objectType); class Arguments { diff --git a/src/object-store/shared_realm.cpp b/src/object-store/shared_realm.cpp index d2919f0c..1dd3f068 100644 --- a/src/object-store/shared_realm.cpp +++ b/src/object-store/shared_realm.cpp @@ -165,7 +165,7 @@ SharedRealm Realm::get_shared_realm(Config config) throw UnitializedRealmException("Can't open an un-initialized Realm without a Schema"); } target_schema->validate(); - ObjectStore::verify_schema(*realm->m_config.schema, *target_schema, true); + ObjectStore::verify_schema(*realm->m_config.schema, const_cast(*target_schema), true); realm->m_config.schema = std::move(target_schema); } else { @@ -180,13 +180,13 @@ SharedRealm Realm::get_shared_realm(Config config) return realm; } -bool Realm::update_schema(std::unique_ptr schema, uint64_t version) +bool Realm::update_schema(std::unique_ptr schema, uint64_t version) { schema->validate(); 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); + ObjectStore::verify_schema(*m_config.schema, const_cast(*schema), m_config.read_only); m_config.schema = std::move(schema); m_config.schema_version = version; return false; @@ -199,9 +199,6 @@ bool Realm::update_schema(std::unique_ptr schema, uint64_t version) old_config.read_only = true; old_config.schema = std::move(old_schema); - m_config.schema = std::move(schema); - m_config.schema_version = version; - auto migration_function = [&](Group*, Schema&) { SharedRealm old_realm(new Realm(old_config)); auto updated_realm = shared_from_this(); @@ -214,8 +211,9 @@ bool Realm::update_schema(std::unique_ptr schema, uint64_t version) // update and migrate begin_transaction(); bool changed = ObjectStore::update_realm_with_schema(read_group(), *old_config.schema, - version, *m_config.schema, - migration_function); + version, const_cast(*schema), migration_function); + m_config.schema = std::move(schema); + m_config.schema_version = version; commit_transaction(); return changed; } diff --git a/src/object-store/shared_realm.hpp b/src/object-store/shared_realm.hpp index 0503282a..49d35cf0 100644 --- a/src/object-store/shared_realm.hpp +++ b/src/object-store/shared_realm.hpp @@ -50,7 +50,7 @@ namespace realm { bool cache = true; std::vector encryption_key; - std::unique_ptr schema; + std::unique_ptr schema; uint64_t schema_version = ObjectStore::NotVersioned; MigrationFunction migration_function; @@ -78,7 +78,7 @@ namespace realm { // on the Config, and the resulting Schema and version with updated // column mappings are set on the realms config upon success. // returns if any changes were made - bool update_schema(std::unique_ptr schema, uint64_t version); + bool update_schema(std::unique_ptr schema, uint64_t version); static uint64_t get_schema_version(Config const& config);