From 0a41c85d0a42e5b8c7cb43ec3fad3fa640ca3049 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Tue, 1 Sep 2015 16:21:59 -0700 Subject: [PATCH] Improve performance of realm_requires_update() and make more things const --- object_schema.cpp | 9 ++++++--- object_schema.hpp | 5 ++++- object_store.cpp | 37 +++++++++++++++---------------------- object_store.hpp | 5 +---- property.hpp | 2 +- shared_realm.cpp | 2 +- shared_realm.hpp | 2 +- 7 files changed, 29 insertions(+), 33 deletions(-) diff --git a/object_schema.cpp b/object_schema.cpp index 41e34685..cfb65f5d 100644 --- a/object_schema.cpp +++ b/object_schema.cpp @@ -60,12 +60,15 @@ ObjectSchema::ObjectSchema(Group *group, const std::string &name) : name(name) { } } -Property *ObjectSchema::property_for_name(const std::string &name) { - for (auto& prop:properties) { - if (prop.name == name) { +Property *ObjectSchema::property_for_name(StringData name) { + for (auto& prop : properties) { + if (StringData(prop.name) == name) { return ∝ } } return nullptr; } +const Property *ObjectSchema::property_for_name(StringData name) const { + return const_cast(this)->property_for_name(name); +} diff --git a/object_schema.hpp b/object_schema.hpp index 51a4c584..5b7bf343 100644 --- a/object_schema.hpp +++ b/object_schema.hpp @@ -19,6 +19,8 @@ #ifndef REALM_OBJECT_SCHEMA_HPP #define REALM_OBJECT_SCHEMA_HPP +#include + #include #include @@ -39,7 +41,8 @@ namespace realm { std::vector properties; std::string primary_key; - Property *property_for_name(const std::string &name); + Property *property_for_name(StringData name); + const Property *property_for_name(StringData name) const; Property *primary_key_property() { return property_for_name(primary_key); } diff --git a/object_store.cpp b/object_store.cpp index 8dee8d74..c751f75f 100644 --- a/object_store.cpp +++ b/object_store.cpp @@ -336,19 +336,29 @@ bool ObjectStore::is_schema_at_version(Group *group, uint64_t version) { return old_version == version; } -bool ObjectStore::realm_requires_update(Group *group, uint64_t version, Schema &schema) { +bool ObjectStore::realm_requires_update(Group *group, uint64_t version, Schema const& schema) { if (!is_schema_at_version(group, version)) { return true; } - for (auto& target_schema : schema) { + + for (auto const& target_schema : schema) { TableRef table = table_for_object_type(group, target_schema.name); if (!table) { return true; } + + // Check that all of the property indexes are up to date + size_t count = table->get_column_count(); + for (size_t col = 0; col < count; ++col) { + StringData name = table->get_column_name(col); + bool is_indexed = table->has_search_index(col); + auto prop = target_schema.property_for_name(name); + if (prop && prop->requires_index() != is_indexed) { + return true; + } + } } - if (!indexes_are_up_to_date(group, schema)) { - return true; - } + return false; } @@ -395,23 +405,6 @@ Schema ObjectStore::schema_from_group(Group *group) { return schema; } -bool ObjectStore::indexes_are_up_to_date(Group *group, Schema &schema) { - for (auto &object_schema : schema) { - TableRef table = table_for_object_type(group, object_schema.name); - if (!table) { - continue; - } - - update_column_mapping(group, object_schema); - for (auto& property : object_schema.properties) { - if (property.requires_index() != table->has_search_index(property.table_column)) { - return false; - } - } - } - return true; -} - bool ObjectStore::update_indexes(Group *group, Schema &schema) { bool changed = false; for (auto& object_schema : schema) { diff --git a/object_store.hpp b/object_store.hpp index 42b8749e..5361ec37 100644 --- a/object_store.hpp +++ b/object_store.hpp @@ -53,7 +53,7 @@ namespace realm { // determines if you must call update_realm_with_schema for a given realm. // returns true if there is a schema version mismatch, if there tables which still need to be created, // or if file format or other changes/updates need to be made - static bool realm_requires_update(Group *group, uint64_t version, Schema &schema); + static bool realm_requires_update(Group *group, uint64_t version, Schema const& schema); // updates a Realm to a given target schema/version creating tables and updating indexes as necessary // returns if any changes were made @@ -104,9 +104,6 @@ namespace realm { static std::string table_name_for_object_type(const std::string &class_name); static std::string object_type_for_table_name(const std::string &table_name); - // check if indexes are up to date - if false you need to call update_realm_with_schema - static bool indexes_are_up_to_date(Group *group, Schema &schema); - // returns if any indexes were changed static bool update_indexes(Group *group, Schema &schema); diff --git a/property.hpp b/property.hpp index 893f772d..3883e805 100644 --- a/property.hpp +++ b/property.hpp @@ -54,7 +54,7 @@ namespace realm { bool is_nullable = false; size_t table_column; - bool requires_index() { return is_primary || is_indexed; } + bool requires_index() const { return is_primary || is_indexed; } }; static inline const char *string_for_property_type(PropertyType type) { diff --git a/shared_realm.cpp b/shared_realm.cpp index e2753d85..6acd7593 100644 --- a/shared_realm.cpp +++ b/shared_realm.cpp @@ -430,7 +430,7 @@ SharedRealm Realm::get_shared_realm(Config config) return realm; } -bool Realm::update_schema(Schema &schema, uint64_t version) +bool Realm::update_schema(Schema const& schema, uint64_t version) { bool changed = false; Config old_config(m_config); diff --git a/shared_realm.hpp b/shared_realm.hpp index cdbf4d51..3ef4bdf1 100644 --- a/shared_realm.hpp +++ b/shared_realm.hpp @@ -74,7 +74,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(Schema &schema, uint64_t version); + bool update_schema(Schema const& schema, uint64_t version); static uint64_t get_schema_version(Config const& config);