SyncFileActionMetadata::SyncFileActionMetadata(const SyncMetadataManager& manager, Action action, const std::string& original_name, const std::string& url, const std::string& user_identity, util::Optional<std::string> new_name) : m_schema(manager.m_file_action_schema) { size_t raw_action = static_cast<size_t>(action); // Open the Realm. m_realm = Realm::get_shared_realm(manager.get_configuration()); // Retrieve or create the row for this object. TableRef table = ObjectStore::table_for_object_type(m_realm->read_group(), c_sync_fileActionMetadata); m_realm->begin_transaction(); size_t row_idx = table->find_first_string(m_schema.idx_original_name, original_name); if (row_idx == not_found) { row_idx = table->add_empty_row(); table->set_string(m_schema.idx_original_name, row_idx, original_name); } table->set_string(m_schema.idx_new_name, row_idx, new_name); table->set_int(m_schema.idx_action, row_idx, raw_action); table->set_string(m_schema.idx_url, row_idx, url); table->set_string(m_schema.idx_user_identity, row_idx, user_identity); m_realm->commit_transaction(); m_row = table->get(row_idx); }
void ObjectStore::set_primary_key_for_object(Group& group, StringData object_type, StringData primary_key) { TableRef table = group.get_table(c_primaryKeyTableName); size_t row = table->find_first_string(c_primaryKeyObjectClassColumnIndex, object_type); #if REALM_ENABLE_SYNC // sync::create_table* functions should have already updated the pk table. if (sync::has_object_ids(group)) { if (primary_key.size() == 0) REALM_ASSERT(row == not_found); else { REALM_ASSERT(row != not_found); REALM_ASSERT(table->get_string(c_primaryKeyPropertyNameColumnIndex, row) == primary_key); } return; } #endif // REALM_ENABLE_SYNC if (row == not_found && primary_key.size()) { row = table->add_empty_row(); table->set_string_unique(c_primaryKeyObjectClassColumnIndex, row, object_type); table->set_string(c_primaryKeyPropertyNameColumnIndex, row, primary_key); return; } // set if changing, or remove if setting to nil if (primary_key.size() == 0) { if (row != not_found) { table->move_last_over(row); } } else { table->set_string(c_primaryKeyPropertyNameColumnIndex, row, primary_key); } }
StringData ObjectStore::get_primary_key_for_object(Group *group, StringData object_type) { TableRef table = group->get_table(c_primaryKeyTableName); if (!table) { return ""; } size_t row = table->find_first_string(c_primaryKeyObjectClassColumnIndex, object_type); if (row == not_found) { return ""; } return table->get_string(c_primaryKeyPropertyNameColumnIndex, row); }
util::Optional<SyncFileActionMetadata> SyncFileActionMetadata::metadata_for_path(const std::string& original_name, const SyncMetadataManager& manager) { auto realm = Realm::get_shared_realm(manager.get_configuration()); auto schema = manager.m_file_action_schema; TableRef table = ObjectStore::table_for_object_type(realm->read_group(), c_sync_fileActionMetadata); size_t row_idx = table->find_first_string(schema.idx_original_name, original_name); if (row_idx == not_found) { return none; } return SyncFileActionMetadata(std::move(schema), std::move(realm), table->get(row_idx)); }
SyncUserMetadata::SyncUserMetadata(const SyncMetadataManager& manager, std::string identity, bool make_if_absent) : m_schema(manager.m_user_schema) { // Open the Realm. m_realm = Realm::get_shared_realm(manager.get_configuration()); // Retrieve or create the row for this object. TableRef table = ObjectStore::table_for_object_type(m_realm->read_group(), c_sync_userMetadata); size_t row_idx = table->find_first_string(m_schema.idx_identity, identity); if (row_idx == not_found) { if (!make_if_absent) { m_invalid = true; m_realm = nullptr; return; } m_realm->begin_transaction(); row_idx = table->find_first_string(m_schema.idx_identity, identity); if (row_idx == not_found) { row_idx = table->add_empty_row(); table->set_string(m_schema.idx_identity, row_idx, identity); table->set_bool(m_schema.idx_user_is_admin, row_idx, false); m_realm->commit_transaction(); } else { // Someone beat us to adding this user. m_realm->cancel_transaction(); } } m_row = table->get(row_idx); if (make_if_absent) { // User existed in the table, but had been marked for deletion. Unmark it. m_realm->begin_transaction(); table->set_bool(m_schema.idx_marked_for_removal, row_idx, false); m_realm->commit_transaction(); m_invalid = false; } else { m_invalid = m_row.get_bool(m_schema.idx_marked_for_removal); } }
void ObjectStore::set_primary_key_for_object(Group *group, StringData object_type, StringData primary_key) { TableRef table = group->get_table(c_primaryKeyTableName); // get row or create if new object and populate size_t row = table->find_first_string(c_primaryKeyObjectClassColumnIndex, object_type); if (row == not_found && primary_key.size()) { row = table->add_empty_row(); table->set_string(c_primaryKeyObjectClassColumnIndex, row, object_type); } // set if changing, or remove if setting to nil if (primary_key.size() == 0) { if (row != not_found) { table->remove(row); } } else { table->set_string(c_primaryKeyPropertyNameColumnIndex, row, primary_key); } }