Search in sources :

Example 1 with RealmProxyMediator

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

the class RealmProxyMediatorTests method validateTable_noDuplicateIndexInIndicesMap.

@Test
public void validateTable_noDuplicateIndexInIndicesMap() {
    RealmProxyMediator mediator = realm.getConfiguration().getSchemaMediator();
    CatRealmProxy.CatColumnInfo columnInfo;
    columnInfo = (CatRealmProxy.CatColumnInfo) mediator.validateTable(Cat.class, realm.sharedRealm, false);
    final Set<Long> indexSet = new HashSet<Long>();
    int indexCount = 0;
    // Gets index for each field and then put into set.
    for (Field field : Cat.class.getDeclaredFields()) {
        if (Modifier.isStatic(field.getModifiers())) {
            continue;
        }
        indexSet.add(columnInfo.getIndicesMap().get(field.getName()));
        indexCount++;
    }
    assertEquals("if no duplicates, size of set equals to field count.", indexCount, indexSet.size());
}
Also used : Field(java.lang.reflect.Field) RealmProxyMediator(io.realm.internal.RealmProxyMediator) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with RealmProxyMediator

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

the class CompositeMediator method insertOrUpdate.

@Override
public void insertOrUpdate(Realm realm, Collection<? extends RealmModel> objects) {
    RealmProxyMediator mediator = getMediator(Util.getOriginalModelClass(Util.getOriginalModelClass(objects.iterator().next().getClass())));
    mediator.insertOrUpdate(realm, objects);
}
Also used : RealmProxyMediator(io.realm.internal.RealmProxyMediator)

Example 3 with RealmProxyMediator

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

the class CompositeMediator method insert.

@Override
public void insert(Realm realm, Collection<? extends RealmModel> objects) {
    RealmProxyMediator mediator = getMediator(Util.getOriginalModelClass(Util.getOriginalModelClass(objects.iterator().next().getClass())));
    mediator.insert(realm, objects);
}
Also used : RealmProxyMediator(io.realm.internal.RealmProxyMediator)

Example 4 with RealmProxyMediator

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

the class CompositeMediator method insert.

@Override
public void insert(Realm realm, RealmModel object, Map<RealmModel, Long> cache) {
    RealmProxyMediator mediator = getMediator(Util.getOriginalModelClass(object.getClass()));
    mediator.insert(realm, object, cache);
}
Also used : RealmProxyMediator(io.realm.internal.RealmProxyMediator)

Example 5 with RealmProxyMediator

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

Aggregations

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