Search in sources :

Example 1 with ColumnIndices

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

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

the class RealmCache method updateSchemaCache.

/**
     * Updates the schema cache in the typed Realm for {@code pathOfRealm}.
     *
     * @param realm the instance that contains the schema cache to be updated.
     */
static synchronized void updateSchemaCache(Realm realm) {
    final RealmCache cache = cachesMap.get(realm.getPath());
    if (cache == null) {
        // Called during initialization. just skip it.
        return;
    }
    final RefAndCount refAndCount = cache.refAndCountMap.get(RealmCacheType.TYPED_REALM);
    if (refAndCount.localRealm.get() == null) {
        // We can reach here if the DynamicRealm instance is initialized first.
        return;
    }
    final ColumnIndices[] globalCacheArray = cache.typedColumnIndicesArray;
    final ColumnIndices createdCacheEntry = realm.updateSchemaCache(globalCacheArray);
    if (createdCacheEntry != null) {
        RealmCache.storeColumnIndices(globalCacheArray, createdCacheEntry);
    }
}
Also used : ColumnIndices(io.realm.internal.ColumnIndices)

Example 3 with ColumnIndices

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

the class RealmCache method storeColumnIndices.

/**
     * Stores the schema cache to the array.
     * <p>
     * If the {@code array} has an empty slot ({@code == null}), this method stores
     * the {@code columnIndices} to it. Otherwise, the entry of the oldest schema version is
     * replaced.
     *
     * @param array target array.
     * @param columnIndices the item to be stored into the {@code array}.
     * @return the index in the {@code array} where the {@code columnIndices} was stored.
     */
private static int storeColumnIndices(ColumnIndices[] array, ColumnIndices columnIndices) {
    long oldestSchemaVersion = Long.MAX_VALUE;
    int candidateIndex = -1;
    for (int i = array.length - 1; 0 <= i; i--) {
        if (array[i] == null) {
            array[i] = columnIndices;
            return i;
        }
        ColumnIndices target = array[i];
        if (target.getSchemaVersion() <= oldestSchemaVersion) {
            oldestSchemaVersion = target.getSchemaVersion();
            candidateIndex = i;
        }
    }
    array[candidateIndex] = columnIndices;
    return candidateIndex;
}
Also used : ColumnIndices(io.realm.internal.ColumnIndices)

Example 4 with ColumnIndices

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

the class ColumnIndicesTests method copyDeeply.

@Test
public void copyDeeply() {
    final long schemaVersion = 100;
    final ColumnIndices columnIndices = create(schemaVersion);
    final ColumnIndices deepCopy = columnIndices.clone();
    assertEquals(schemaVersion, deepCopy.getSchemaVersion());
    assertEquals(columnIndices.getColumnIndex(Cat.class, Cat.FIELD_NAME), deepCopy.getColumnIndex(Cat.class, Cat.FIELD_NAME));
    assertEquals(columnIndices.getColumnIndex(Dog.class, Dog.FIELD_AGE), deepCopy.getColumnIndex(Dog.class, Dog.FIELD_AGE));
    // Checks if those are different instance.
    assertNotSame(columnIndices, deepCopy);
    assertNotSame(columnIndices.getColumnInfo(Cat.class), deepCopy.getColumnInfo(Cat.class));
    assertNotSame(columnIndices.getColumnInfo(Dog.class), deepCopy.getColumnInfo(Dog.class));
}
Also used : ColumnIndices(io.realm.internal.ColumnIndices) Cat(io.realm.entities.Cat) Dog(io.realm.entities.Dog) Test(org.junit.Test)

Example 5 with ColumnIndices

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

the class Realm method createAndValidate.

private static Realm createAndValidate(RealmConfiguration configuration, ColumnIndices[] globalCacheArray) {
    Realm realm = new Realm(configuration);
    final long currentVersion = realm.getVersion();
    final long requiredVersion = configuration.getSchemaVersion();
    final ColumnIndices columnIndices = RealmCache.findColumnIndices(globalCacheArray, requiredVersion);
    if (columnIndices != null) {
        // Copies global cache as a Realm local indices cache.
        realm.schema.columnIndices = columnIndices.clone();
    } else {
        final boolean syncingConfig = configuration.isSyncConfiguration();
        if (!syncingConfig && (currentVersion != UNVERSIONED)) {
            if (currentVersion < requiredVersion) {
                realm.doClose();
                throw new RealmMigrationNeededException(configuration.getPath(), String.format("Realm on disk need to migrate from v%s to v%s", currentVersion, requiredVersion));
            }
            if (requiredVersion < currentVersion) {
                realm.doClose();
                throw new IllegalArgumentException(String.format("Realm on disk is newer than the one specified: v%s vs. v%s", currentVersion, requiredVersion));
            }
        }
        // Initializes Realm schema if needed.
        try {
            if (!syncingConfig) {
                initializeRealm(realm);
            } else {
                initializeSyncedRealm(realm);
            }
        } catch (RuntimeException e) {
            realm.doClose();
            throw e;
        }
    }
    return realm;
}
Also used : ColumnIndices(io.realm.internal.ColumnIndices) RealmMigrationNeededException(io.realm.exceptions.RealmMigrationNeededException) SharedRealm(io.realm.internal.SharedRealm)

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