Improve performance of realm_requires_update() and make more things const

This commit is contained in:
Thomas Goyne 2015-09-01 16:21:59 -07:00 committed by Ari Lazier
parent fc0de384a6
commit fc48c88e69
7 changed files with 29 additions and 33 deletions

View File

@ -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<ObjectSchema *>(this)->property_for_name(name);
}

View File

@ -19,6 +19,8 @@
#ifndef REALM_OBJECT_SCHEMA_HPP
#define REALM_OBJECT_SCHEMA_HPP
#include <realm/string_data.hpp>
#include <string>
#include <vector>
@ -39,7 +41,8 @@ namespace realm {
std::vector<Property> 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);
}

View File

@ -337,19 +337,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;
}
}
if (!indexes_are_up_to_date(group, schema)) {
// 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;
}
}
}
return false;
}
@ -396,23 +406,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) {

View File

@ -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);

View File

@ -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) {

View File

@ -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);

View File

@ -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);