SyncMetadataManager::SyncMetadataManager(std::string path, bool should_encrypt, util::Optional<std::vector<char>> encryption_key) { constexpr uint64_t SCHEMA_VERSION = 1; std::lock_guard<std::mutex> lock(m_metadata_lock); Realm::Config config; config.path = std::move(path); config.schema = make_schema(); config.schema_version = SCHEMA_VERSION; config.schema_mode = SchemaMode::Automatic; #if REALM_PLATFORM_APPLE if (should_encrypt && !encryption_key) { encryption_key = keychain::metadata_realm_encryption_key(); } #endif if (should_encrypt) { if (!encryption_key) { throw std::invalid_argument("Metadata Realm encryption was specified, but no encryption key was provided."); } config.encryption_key = std::move(*encryption_key); } // Open the Realm. SharedRealm realm = Realm::get_shared_realm(config); // Get data about the (hardcoded) schemas. DescriptorRef descriptor = ObjectStore::table_for_object_type(realm->read_group(), c_sync_userMetadata)->get_descriptor(); m_user_schema = { descriptor->get_column_index(c_sync_identity), descriptor->get_column_index(c_sync_marked_for_removal), descriptor->get_column_index(c_sync_user_token), descriptor->get_column_index(c_sync_auth_server_url), descriptor->get_column_index(c_sync_user_is_admin), }; descriptor = ObjectStore::table_for_object_type(realm->read_group(), c_sync_fileActionMetadata)->get_descriptor(); m_file_action_schema = { descriptor->get_column_index(c_sync_original_name), descriptor->get_column_index(c_sync_new_name), descriptor->get_column_index(c_sync_action), descriptor->get_column_index(c_sync_url), descriptor->get_column_index(c_sync_identity) }; m_metadata_config = std::move(config); }
SyncFileActionMetadataResults SyncMetadataManager::all_pending_actions() const { SharedRealm realm = Realm::get_shared_realm(get_configuration()); TableRef table = ObjectStore::table_for_object_type(realm->read_group(), c_sync_fileActionMetadata); Results results(realm, table->where()); return SyncFileActionMetadataResults(std::move(results), std::move(realm), m_file_action_schema); }
SyncUserMetadataResults SyncMetadataManager::get_users(bool marked) const { // Open the Realm. SharedRealm realm = Realm::get_shared_realm(get_configuration()); TableRef table = ObjectStore::table_for_object_type(realm->read_group(), c_sync_userMetadata); Query query = table->where().equal(m_user_schema.idx_marked_for_removal, marked); Results results(realm, std::move(query)); return SyncUserMetadataResults(std::move(results), std::move(realm), m_user_schema); }