Make a bunch of things const

This commit is contained in:
Thomas Goyne 2015-09-04 09:40:29 -07:00
parent 14004504b1
commit dbac77f69b
6 changed files with 52 additions and 43 deletions

View File

@ -28,9 +28,8 @@ using namespace realm;
ObjectSchema::~ObjectSchema() = default; ObjectSchema::~ObjectSchema() = default;
ObjectSchema::ObjectSchema(Group *group, const std::string &name) : name(name) { ObjectSchema::ObjectSchema(const Group *group, const std::string &name) : name(name) {
TableRef tableRef = ObjectStore::table_for_object_type(group, name); ConstTableRef table = ObjectStore::table_for_object_type(group, name);
Table *table = tableRef.get();
size_t count = table->get_column_count(); size_t count = table->get_column_count();
properties.reserve(count); properties.reserve(count);
@ -44,7 +43,7 @@ ObjectSchema::ObjectSchema(Group *group, const std::string &name) : name(name) {
property.table_column = col; property.table_column = col;
if (property.type == PropertyTypeObject || property.type == PropertyTypeArray) { if (property.type == PropertyTypeObject || property.type == PropertyTypeArray) {
// set link type for objects and arrays // set link type for objects and arrays
realm::TableRef linkTable = table->get_link_target(col); ConstTableRef 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(std::move(property)); properties.push_back(std::move(property));

View File

@ -35,7 +35,7 @@ namespace realm {
// create object schema from existing table // create object schema from existing table
// if no table is provided it is looked up in the group // if no table is provided it is looked up in the group
ObjectSchema(Group *group, const std::string &name); ObjectSchema(const Group *group, const std::string &name);
std::string name; std::string name;
std::vector<Property> properties; std::vector<Property> properties;
@ -46,6 +46,9 @@ namespace realm {
Property *primary_key_property() { Property *primary_key_property() {
return property_for_name(primary_key); return property_for_name(primary_key);
} }
const Property *primary_key_property() const {
return property_for_name(primary_key);
}
}; };
} }

View File

@ -45,7 +45,7 @@ const size_t c_object_table_prefix_length = c_object_table_prefix.length();
const uint64_t ObjectStore::NotVersioned = std::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(const Group *group) {
return group->get_table(c_primaryKeyTableName) && group->get_table(c_metadataTableName); return group->get_table(c_primaryKeyTableName) && group->get_table(c_metadataTableName);
} }
@ -71,8 +71,8 @@ bool ObjectStore::create_metadata_tables(Group *group) {
return changed; return changed;
} }
uint64_t ObjectStore::get_schema_version(Group *group) { uint64_t ObjectStore::get_schema_version(const Group *group) {
TableRef table = group->get_table(c_metadataTableName); ConstTableRef table = group->get_table(c_metadataTableName);
if (!table || table->get_column_count() == 0) { if (!table || table->get_column_count() == 0) {
return ObjectStore::NotVersioned; return ObjectStore::NotVersioned;
} }
@ -84,8 +84,8 @@ void ObjectStore::set_schema_version(Group *group, uint64_t version) {
table->set_int(c_versionColumnIndex, c_zeroRowIndex, version); table->set_int(c_versionColumnIndex, c_zeroRowIndex, version);
} }
StringData ObjectStore::get_primary_key_for_object(Group *group, StringData object_type) { StringData ObjectStore::get_primary_key_for_object(const Group *group, StringData object_type) {
TableRef table = group->get_table(c_primaryKeyTableName); ConstTableRef table = group->get_table(c_primaryKeyTableName);
if (!table) { if (!table) {
return ""; return "";
} }
@ -132,6 +132,10 @@ TableRef ObjectStore::table_for_object_type(Group *group, StringData object_type
return group->get_table(table_name_for_object_type(object_type)); return group->get_table(table_name_for_object_type(object_type));
} }
ConstTableRef ObjectStore::table_for_object_type(const Group *group, StringData object_type) {
return group->get_table(table_name_for_object_type(object_type));
}
TableRef ObjectStore::table_for_object_type_create_if_needed(Group *group, const StringData &object_type, bool &created) { TableRef ObjectStore::table_for_object_type_create_if_needed(Group *group, const StringData &object_type, bool &created) {
return group->get_or_add_table(table_name_for_object_type(object_type), &created); return group->get_or_add_table(table_name_for_object_type(object_type), &created);
} }
@ -147,7 +151,7 @@ static bool compare_by_name(ObjectSchema const& lft, ObjectSchema const& rgt) {
return lft.name < rgt.name; return lft.name < rgt.name;
} }
void ObjectStore::verify_schema(Schema const& actual_schema, Schema &target_schema, bool allow_missing_tables) { void ObjectStore::verify_schema(Schema const& actual_schema, Schema& target_schema, bool allow_missing_tables) {
std::sort(begin(target_schema), end(target_schema), compare_by_name); std::sort(begin(target_schema), end(target_schema), compare_by_name);
std::vector<ObjectSchemaValidationException> errors; std::vector<ObjectSchemaValidationException> errors;
@ -169,7 +173,9 @@ void ObjectStore::verify_schema(Schema const& actual_schema, Schema &target_sche
} }
} }
std::vector<ObjectSchemaValidationException> ObjectStore::verify_object_schema(ObjectSchema const& table_schema, ObjectSchema &target_schema, Schema &schema) { std::vector<ObjectSchemaValidationException> ObjectStore::verify_object_schema(ObjectSchema const& table_schema,
ObjectSchema& target_schema,
Schema const& schema) {
std::vector<ObjectSchemaValidationException> exceptions; std::vector<ObjectSchemaValidationException> exceptions;
ObjectSchema cmp; ObjectSchema cmp;
@ -334,7 +340,7 @@ bool ObjectStore::create_tables(Group *group, Schema &target_schema, bool update
return changed; return changed;
} }
bool ObjectStore::is_schema_at_version(Group *group, uint64_t version) { bool ObjectStore::is_schema_at_version(const Group *group, uint64_t version) {
uint64_t old_version = get_schema_version(group); uint64_t old_version = get_schema_version(group);
if (old_version > version && old_version != NotVersioned) { if (old_version > version && old_version != NotVersioned) {
throw InvalidSchemaVersionException(old_version, version); throw InvalidSchemaVersionException(old_version, version);
@ -342,7 +348,7 @@ bool ObjectStore::is_schema_at_version(Group *group, uint64_t version) {
return old_version == version; return old_version == version;
} }
bool ObjectStore::needs_update(Schema const& old_schema, Schema& schema) { bool ObjectStore::needs_update(Schema const& old_schema, Schema const& schema) {
for (auto const& target_schema : schema) { for (auto const& target_schema : schema) {
auto matching_schema = std::lower_bound(begin(old_schema), end(old_schema), target_schema, compare_by_name); auto matching_schema = std::lower_bound(begin(old_schema), end(old_schema), target_schema, compare_by_name);
if (matching_schema == end(old_schema) || matching_schema->name != target_schema.name) { if (matching_schema == end(old_schema) || matching_schema->name != target_schema.name) {
@ -401,7 +407,7 @@ bool ObjectStore::update_realm_with_schema(Group *group, Schema const& old_schem
return true; return true;
} }
Schema ObjectStore::schema_from_group(Group *group) { Schema ObjectStore::schema_from_group(const Group *group) {
Schema schema; Schema schema;
for (size_t i = 0; i < group->size(); i++) { for (size_t i = 0; i < group->size(); i++) {
std::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));
@ -443,14 +449,14 @@ bool ObjectStore::update_indexes(Group *group, Schema &schema) {
return changed; return changed;
} }
void ObjectStore::validate_primary_column_uniqueness(Group *group, Schema &schema) { void ObjectStore::validate_primary_column_uniqueness(const Group *group, Schema const& schema) {
for (auto& object_schema : schema) { for (auto& object_schema : schema) {
auto primary_prop = object_schema.primary_key_property(); auto primary_prop = object_schema.primary_key_property();
if (!primary_prop) { if (!primary_prop) {
continue; continue;
} }
TableRef table = table_for_object_type(group, object_schema.name); ConstTableRef table = table_for_object_type(group, object_schema.name);
if (table->get_distinct_view(primary_prop->table_column).size() != table->size()) { if (table->get_distinct_view(primary_prop->table_column).size() != table->size()) {
throw DuplicatePrimaryKeyValueException(object_schema.name, *primary_prop); throw DuplicatePrimaryKeyValueException(object_schema.name, *primary_prop);
} }
@ -567,4 +573,3 @@ DuplicatePrimaryKeysException::DuplicatePrimaryKeysException(std::string object_
{ {
m_what = "Duplicate primary keys for object '" + object_type + "'."; m_what = "Duplicate primary keys for object '" + object_type + "'.";
} }

View File

@ -37,19 +37,19 @@ namespace realm {
static const uint64_t NotVersioned; static const uint64_t NotVersioned;
// get the last set schema version // get the last set schema version
static uint64_t get_schema_version(Group *group); static uint64_t get_schema_version(const Group *group);
// checks if the schema in the group is at the given version // checks if the schema in the group is at the given version
static bool is_schema_at_version(realm::Group *group, uint64_t version); static bool is_schema_at_version(const Group *group, uint64_t version);
// verify that schema from a group and a target schema are compatible // verify that schema from a group and a target schema are compatible
// updates the column mapping on all ObjectSchema properties of the target schema // updates the column mapping on all ObjectSchema properties of the target schema
// throws if the schema is invalid or does not match // throws if the schema is invalid or does not match
static void verify_schema(Schema const& actual_schema, Schema &target_schema, bool allow_missing_tables = false); static void verify_schema(Schema const& actual_schema, Schema& target_schema, bool allow_missing_tables = false);
// determines if a realm with the given old schema needs non-migration // determines if a realm with the given old schema needs non-migration
// changes to make it compatible with the given target schema // changes to make it compatible with the given target schema
static bool needs_update(Schema const& old_schema, Schema& schema); static bool needs_update(Schema const& old_schema, Schema const& schema);
// updates a Realm from old_schema to the given target schema, creating and updating tables as needed // updates a Realm from old_schema to the given target schema, creating and updating tables as needed
// returns if any changes were made // returns if any changes were made
@ -62,9 +62,10 @@ namespace realm {
// get a table for an object type // get a table for an object type
static realm::TableRef table_for_object_type(Group *group, StringData object_type); static realm::TableRef table_for_object_type(Group *group, StringData object_type);
static realm::ConstTableRef table_for_object_type(const Group *group, StringData object_type);
// get existing Schema from a group // get existing Schema from a group
static Schema schema_from_group(Group *group); static Schema schema_from_group(const Group *group);
// deletes the table for the given type // deletes the table for the given type
static void delete_data_for_object(Group *group, const StringData &object_type); static void delete_data_for_object(Group *group, const StringData &object_type);
@ -74,7 +75,7 @@ namespace realm {
static void set_schema_version(Group *group, uint64_t version); static void set_schema_version(Group *group, uint64_t version);
// check if the realm already has all metadata tables // check if the realm already has all metadata tables
static bool has_metadata_tables(Group *group); static bool has_metadata_tables(const Group *group);
// create any metadata tables that don't already exist // create any metadata tables that don't already exist
// must be in write transaction to set // must be in write transaction to set
@ -88,10 +89,12 @@ namespace realm {
// verify a target schema against an expected schema, setting the table_column property on each schema object // verify a target schema against an expected schema, setting the table_column property on each schema object
// updates the column mapping on the target_schema // updates the column mapping on the target_schema
// returns array of validation errors // returns array of validation errors
static std::vector<ObjectSchemaValidationException> verify_object_schema(ObjectSchema const& expected, ObjectSchema &target_schema, Schema &schema); static std::vector<ObjectSchemaValidationException> verify_object_schema(ObjectSchema const& expected,
ObjectSchema &target_schema,
Schema const& schema);
// get primary key property name for object type // get primary key property name for object type
static StringData get_primary_key_for_object(Group *group, StringData object_type); static StringData get_primary_key_for_object(const Group *group, StringData object_type);
// sets primary key property for object type // sets primary key property for object type
// must be in write transaction to set // must be in write transaction to set
@ -105,7 +108,7 @@ namespace realm {
static bool update_indexes(Group *group, Schema &schema); static bool update_indexes(Group *group, Schema &schema);
// validates that all primary key properties have unique values // validates that all primary key properties have unique values
static void validate_primary_column_uniqueness(Group *group, Schema &schema); static void validate_primary_column_uniqueness(const Group *group, Schema const& schema);
friend ObjectSchema; friend ObjectSchema;
}; };
@ -126,8 +129,8 @@ namespace realm {
class InvalidSchemaVersionException : public MigrationException { class InvalidSchemaVersionException : public MigrationException {
public: public:
InvalidSchemaVersionException(uint64_t old_version, uint64_t new_version); InvalidSchemaVersionException(uint64_t old_version, uint64_t new_version);
uint64_t old_version() { return m_old_version; } uint64_t old_version() const { return m_old_version; }
uint64_t new_version() { return m_new_version; } uint64_t new_version() const { return m_new_version; }
private: private:
uint64_t m_old_version, m_new_version; uint64_t m_old_version, m_new_version;
}; };
@ -135,8 +138,8 @@ namespace realm {
class DuplicatePrimaryKeyValueException : public MigrationException { class DuplicatePrimaryKeyValueException : public MigrationException {
public: public:
DuplicatePrimaryKeyValueException(std::string object_type, Property const& property); DuplicatePrimaryKeyValueException(std::string object_type, Property const& property);
std::string object_type() { return m_object_type; } std::string object_type() const { return m_object_type; }
Property const& property() { return m_property; } Property const& property() const { return m_property; }
private: private:
std::string m_object_type; std::string m_object_type;
Property m_property; Property m_property;
@ -156,7 +159,7 @@ namespace realm {
ObjectSchemaValidationException(std::string object_type) : m_object_type(object_type) {} ObjectSchemaValidationException(std::string object_type) : m_object_type(object_type) {}
ObjectSchemaValidationException(std::string object_type, std::string message) : ObjectSchemaValidationException(std::string object_type, std::string message) :
m_object_type(object_type) { m_what = message; } m_object_type(object_type) { m_what = message; }
std::string object_type() { return m_object_type; } std::string object_type() const { return m_object_type; }
protected: protected:
std::string m_object_type; std::string m_object_type;
}; };
@ -165,7 +168,7 @@ namespace realm {
public: public:
ObjectSchemaPropertyException(std::string object_type, Property const& property) : ObjectSchemaPropertyException(std::string object_type, Property const& property) :
ObjectSchemaValidationException(object_type), m_property(property) {} ObjectSchemaValidationException(object_type), m_property(property) {}
Property const& property() { return m_property; } Property const& property() const { return m_property; }
private: private:
Property m_property; Property m_property;
}; };
@ -203,8 +206,8 @@ namespace realm {
class MismatchedPropertiesException : public ObjectSchemaValidationException { class MismatchedPropertiesException : public ObjectSchemaValidationException {
public: public:
MismatchedPropertiesException(std::string object_type, Property const& old_property, Property const& new_property); MismatchedPropertiesException(std::string object_type, Property const& old_property, Property const& new_property);
Property const& old_property() { return m_old_property; } Property const& old_property() const { return m_old_property; }
Property const& new_property() { return m_new_property; } Property const& new_property() const { return m_new_property; }
private: private:
Property m_old_property, m_new_property; Property m_old_property, m_new_property;
}; };
@ -212,8 +215,8 @@ namespace realm {
class ChangedPrimaryKeyException : public ObjectSchemaValidationException { class ChangedPrimaryKeyException : public ObjectSchemaValidationException {
public: public:
ChangedPrimaryKeyException(std::string object_type, std::string old_primary, std::string new_primary); ChangedPrimaryKeyException(std::string object_type, std::string old_primary, std::string new_primary);
std::string old_primary() { return m_old_primary; } std::string old_primary() const { return m_old_primary; }
std::string new_primary() { return m_new_primary; } std::string new_primary() const { return m_new_primary; }
private: private:
std::string m_old_primary, m_new_primary; std::string m_old_primary, m_new_primary;
}; };
@ -221,11 +224,10 @@ namespace realm {
class InvalidPrimaryKeyException : public ObjectSchemaValidationException { class InvalidPrimaryKeyException : public ObjectSchemaValidationException {
public: public:
InvalidPrimaryKeyException(std::string object_type, std::string primary_key); InvalidPrimaryKeyException(std::string object_type, std::string primary_key);
std::string primary_key() { return m_primary_key; } std::string primary_key() const { return m_primary_key; }
private: private:
std::string m_primary_key; std::string m_primary_key;
}; };
} }
#endif /* defined(REALM_OBJECT_STORE_HPP) */ #endif /* defined(REALM_OBJECT_STORE_HPP) */

View File

@ -221,7 +221,7 @@ static void check_read_write(Realm *realm)
} }
} }
void Realm::verify_thread() void Realm::verify_thread() const
{ {
if (m_thread_id != std::this_thread::get_id()) { if (m_thread_id != std::this_thread::get_id()) {
throw IncorrectThreadException("Realm accessed from incorrect thread."); throw IncorrectThreadException("Realm accessed from incorrect thread.");

View File

@ -84,18 +84,18 @@ namespace realm {
void begin_transaction(); void begin_transaction();
void commit_transaction(); void commit_transaction();
void cancel_transaction(); void cancel_transaction();
bool is_in_transaction() { return m_in_transaction; } bool is_in_transaction() const { return m_in_transaction; }
bool refresh(); bool refresh();
void set_auto_refresh(bool auto_refresh) { m_auto_refresh = auto_refresh; } void set_auto_refresh(bool auto_refresh) { m_auto_refresh = auto_refresh; }
bool auto_refresh() { return m_auto_refresh; } bool auto_refresh() const { return m_auto_refresh; }
void notify(); void notify();
void invalidate(); void invalidate();
bool compact(); bool compact();
std::thread::id thread_id() const { return m_thread_id; } std::thread::id thread_id() const { return m_thread_id; }
void verify_thread(); void verify_thread() const;
~Realm(); ~Realm();