Search in sources :

Example 6 with ColumnIndices

use of io.realm.internal.ColumnIndices 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 7 with ColumnIndices

use of io.realm.internal.ColumnIndices 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)

Example 8 with ColumnIndices

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

the class ColumnIndicesTests method copyFrom.

@Test
public void copyFrom() {
    final long sourceSchemaVersion = 101;
    final long targetSchemaVersion = 100;
    final ColumnIndices source = create(sourceSchemaVersion);
    final ColumnIndices target = create(targetSchemaVersion);
    final CatRealmProxy.CatColumnInfo catColumnInfoInSource = (CatRealmProxy.CatColumnInfo) source.getColumnInfo(Cat.class);
    final CatRealmProxy.CatColumnInfo catColumnInfoInTarget = (CatRealmProxy.CatColumnInfo) target.getColumnInfo(Cat.class);
    catColumnInfoInSource.nameIndex++;
    // Checks preconditions.
    assertNotEquals(catColumnInfoInSource.nameIndex, catColumnInfoInTarget.nameIndex);
    assertNotSame(catColumnInfoInSource.getIndicesMap(), catColumnInfoInTarget.getIndicesMap());
    target.copyFrom(source, mediator);
    assertEquals(sourceSchemaVersion, target.getSchemaVersion());
    assertEquals(catColumnInfoInSource.nameIndex, catColumnInfoInTarget.nameIndex);
    assertSame(catColumnInfoInSource.getIndicesMap(), catColumnInfoInTarget.getIndicesMap());
}
Also used : ColumnIndices(io.realm.internal.ColumnIndices) Cat(io.realm.entities.Cat) Test(org.junit.Test)

Aggregations

ColumnIndices (io.realm.internal.ColumnIndices)8 RealmMigrationNeededException (io.realm.exceptions.RealmMigrationNeededException)4 ColumnInfo (io.realm.internal.ColumnInfo)3 RealmProxyMediator (io.realm.internal.RealmProxyMediator)3 Cat (io.realm.entities.Cat)2 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 Test (org.junit.Test)2 Dog (io.realm.entities.Dog)1 SharedRealm (io.realm.internal.SharedRealm)1 ArrayList (java.util.ArrayList)1