Search in sources :

Example 1 with ColumnInfo

use of io.realm.internal.ColumnInfo in project realm-java by realm.

the class Realm method initializeRealm.

private static void initializeRealm(Realm realm) {
    // Everything in this method needs to be behind a transaction lock to prevent multi-process interaction while
    // the Realm is initialized.
    boolean commitChanges = false;
    try {
        realm.beginTransaction();
        long currentVersion = realm.getVersion();
        boolean unversioned = currentVersion == UNVERSIONED;
        commitChanges = unversioned;
        if (unversioned) {
            realm.setVersion(realm.configuration.getSchemaVersion());
        }
        final RealmProxyMediator mediator = realm.configuration.getSchemaMediator();
        final Set<Class<? extends RealmModel>> modelClasses = mediator.getModelClasses();
        final Map<Class<? extends RealmModel>, ColumnInfo> columnInfoMap = new HashMap<>(modelClasses.size());
        if (unversioned) {
            // Create all of the tables.
            for (Class<? extends RealmModel> modelClass : modelClasses) {
                mediator.createTable(modelClass, realm.sharedRealm);
            }
        }
        for (Class<? extends RealmModel> modelClass : modelClasses) {
            // Now that they have all been created, validate them.
            columnInfoMap.put(modelClass, mediator.validateTable(modelClass, realm.sharedRealm, false));
        }
        realm.schema.columnIndices = new ColumnIndices((unversioned) ? realm.configuration.getSchemaVersion() : currentVersion, columnInfoMap);
        if (unversioned) {
            final Transaction transaction = realm.configuration.getInitialDataTransaction();
            if (transaction != null) {
                transaction.execute(realm);
            }
        }
    } catch (Exception e) {
        commitChanges = false;
        throw e;
    } finally {
        if (commitChanges) {
            realm.commitTransaction();
        } else {
            realm.cancelTransaction();
        }
    }
}
Also used : ColumnIndices(io.realm.internal.ColumnIndices) HashMap(java.util.HashMap) ColumnInfo(io.realm.internal.ColumnInfo) RealmFileException(io.realm.exceptions.RealmFileException) RealmException(io.realm.exceptions.RealmException) JSONException(org.json.JSONException) RealmMigrationNeededException(io.realm.exceptions.RealmMigrationNeededException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RealmProxyMediator(io.realm.internal.RealmProxyMediator)

Example 2 with ColumnInfo

use of io.realm.internal.ColumnInfo in project realm-java by realm.

the class Realm method updateSchemaCache.

/**
     * Updates own schema cache.
     *
     * @param globalCacheArray global cache of column indices. If it contains an entry for current
     *                         schema version, this method only copies the indices information in the entry.
     * @return newly created indices information for current schema version. Or {@code null} if {@code globalCacheArray}
     * already contains the entry for current schema version.
     */
ColumnIndices updateSchemaCache(ColumnIndices[] globalCacheArray) {
    final long currentSchemaVersion = sharedRealm.getSchemaVersion();
    final long cacheSchemaVersion = schema.columnIndices.getSchemaVersion();
    if (currentSchemaVersion == cacheSchemaVersion) {
        return null;
    }
    ColumnIndices createdGlobalCache = null;
    final RealmProxyMediator mediator = getConfiguration().getSchemaMediator();
    ColumnIndices cacheForCurrentVersion = RealmCache.findColumnIndices(globalCacheArray, currentSchemaVersion);
    if (cacheForCurrentVersion == null) {
        // Not found in global cache. create it.
        final Set<Class<? extends RealmModel>> modelClasses = mediator.getModelClasses();
        final Map<Class<? extends RealmModel>, ColumnInfo> map;
        map = new HashMap<Class<? extends RealmModel>, ColumnInfo>(modelClasses.size());
        try {
            for (Class<? extends RealmModel> clazz : modelClasses) {
                final ColumnInfo columnInfo = mediator.validateTable(clazz, sharedRealm, true);
                map.put(clazz, columnInfo);
            }
        } catch (RealmMigrationNeededException e) {
            throw e;
        }
        cacheForCurrentVersion = createdGlobalCache = new ColumnIndices(currentSchemaVersion, map);
    }
    schema.columnIndices.copyFrom(cacheForCurrentVersion, mediator);
    return createdGlobalCache;
}
Also used : ColumnIndices(io.realm.internal.ColumnIndices) RealmProxyMediator(io.realm.internal.RealmProxyMediator) ColumnInfo(io.realm.internal.ColumnInfo) RealmMigrationNeededException(io.realm.exceptions.RealmMigrationNeededException)

Example 3 with ColumnInfo

use of io.realm.internal.ColumnInfo in project realm-java by realm.

the class Realm method initializeSyncedRealm.

private static void initializeSyncedRealm(Realm realm) {
    // Everything in this method needs to be behind a transaction lock to prevent multi-process interaction while
    // the Realm is initialized.
    boolean commitChanges = false;
    try {
        realm.beginTransaction();
        long currentVersion = realm.getVersion();
        final boolean unversioned = (currentVersion == UNVERSIONED);
        final RealmProxyMediator mediator = realm.configuration.getSchemaMediator();
        final Set<Class<? extends RealmModel>> modelClasses = mediator.getModelClasses();
        final ArrayList<RealmObjectSchema> realmObjectSchemas = new ArrayList<>();
        final RealmSchema realmSchemaCache = new RealmSchema();
        for (Class<? extends RealmModel> modelClass : modelClasses) {
            RealmObjectSchema realmObjectSchema = mediator.createRealmObjectSchema(modelClass, realmSchemaCache);
            realmObjectSchemas.add(realmObjectSchema);
        }
        // Assumption: When SyncConfiguration then additive schema update mode.
        final RealmSchema schema = new RealmSchema(realmObjectSchemas);
        long newVersion = realm.configuration.getSchemaVersion();
        if (realm.sharedRealm.requiresMigration(schema)) {
            if (currentVersion >= newVersion) {
                throw new IllegalArgumentException(String.format("The schema was changed but the schema version " + "was not updated. The configured schema version (%d) must be higher than the one in the Realm " + "file (%d) in order to update the schema.", newVersion, currentVersion));
            }
            realm.sharedRealm.updateSchema(schema, newVersion);
            // The OS currently does not handle setting the schema version. We have to do it manually.
            realm.setVersion(newVersion);
            commitChanges = true;
        }
        final Map<Class<? extends RealmModel>, ColumnInfo> columnInfoMap = new HashMap<>(modelClasses.size());
        for (Class<? extends RealmModel> modelClass : modelClasses) {
            columnInfoMap.put(modelClass, mediator.validateTable(modelClass, realm.sharedRealm, false));
        }
        realm.schema.columnIndices = new ColumnIndices((unversioned) ? newVersion : currentVersion, columnInfoMap);
        if (unversioned) {
            final Transaction transaction = realm.configuration.getInitialDataTransaction();
            if (transaction != null) {
                transaction.execute(realm);
            }
        }
    } catch (Exception e) {
        commitChanges = false;
        throw e;
    } finally {
        if (commitChanges) {
            realm.commitTransaction();
        } else {
            realm.cancelTransaction();
        }
    }
}
Also used : ColumnIndices(io.realm.internal.ColumnIndices) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ColumnInfo(io.realm.internal.ColumnInfo) RealmFileException(io.realm.exceptions.RealmFileException) RealmException(io.realm.exceptions.RealmException) JSONException(org.json.JSONException) RealmMigrationNeededException(io.realm.exceptions.RealmMigrationNeededException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RealmProxyMediator(io.realm.internal.RealmProxyMediator)

Aggregations

RealmMigrationNeededException (io.realm.exceptions.RealmMigrationNeededException)3 ColumnIndices (io.realm.internal.ColumnIndices)3 ColumnInfo (io.realm.internal.ColumnInfo)3 RealmProxyMediator (io.realm.internal.RealmProxyMediator)3 RealmException (io.realm.exceptions.RealmException)2 RealmFileException (io.realm.exceptions.RealmFileException)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashMap (java.util.HashMap)2 JSONException (org.json.JSONException)2 ArrayList (java.util.ArrayList)1