Search in sources :

Example 6 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, RealmModel object, Map<RealmModel, Long> cache) {
    RealmProxyMediator mediator = getMediator(Util.getOriginalModelClass(object.getClass()));
    mediator.insertOrUpdate(realm, object, cache);
}
Also used : RealmProxyMediator(io.realm.internal.RealmProxyMediator)

Example 7 with RealmProxyMediator

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

the class ColumnInfoTests method copyColumnInfoFrom_checkIndex.

@Test
public void copyColumnInfoFrom_checkIndex() {
    final RealmProxyMediator mediator = realm.getConfiguration().getSchemaMediator();
    final CatRealmProxy.CatColumnInfo sourceColumnInfo, targetColumnInfo;
    sourceColumnInfo = (CatRealmProxy.CatColumnInfo) mediator.validateTable(Cat.class, realm.sharedRealm, false);
    targetColumnInfo = (CatRealmProxy.CatColumnInfo) mediator.validateTable(Cat.class, realm.sharedRealm, false);
    // Checks precondition.
    assertNotSame(sourceColumnInfo, targetColumnInfo);
    assertNotSame(sourceColumnInfo.getIndicesMap(), targetColumnInfo.getIndicesMap());
    sourceColumnInfo.nameIndex = 1;
    sourceColumnInfo.ageIndex = 2;
    sourceColumnInfo.heightIndex = 3;
    sourceColumnInfo.weightIndex = 4;
    sourceColumnInfo.hasTailIndex = 5;
    sourceColumnInfo.birthdayIndex = 6;
    sourceColumnInfo.ownerIndex = 7;
    sourceColumnInfo.scaredOfDogIndex = 8;
    targetColumnInfo.nameIndex = 0;
    targetColumnInfo.ageIndex = 0;
    targetColumnInfo.heightIndex = 0;
    targetColumnInfo.weightIndex = 0;
    targetColumnInfo.hasTailIndex = 0;
    targetColumnInfo.birthdayIndex = 0;
    targetColumnInfo.ownerIndex = 0;
    targetColumnInfo.scaredOfDogIndex = 0;
    targetColumnInfo.copyColumnInfoFrom(sourceColumnInfo);
    assertEquals(1, targetColumnInfo.nameIndex);
    assertEquals(2, targetColumnInfo.ageIndex);
    assertEquals(3, targetColumnInfo.heightIndex);
    assertEquals(4, targetColumnInfo.weightIndex);
    assertEquals(5, targetColumnInfo.hasTailIndex);
    assertEquals(6, targetColumnInfo.birthdayIndex);
    assertEquals(7, targetColumnInfo.ownerIndex);
    assertEquals(8, targetColumnInfo.scaredOfDogIndex);
    // Current implementation shares the indices map.
    assertSame(sourceColumnInfo.getIndicesMap(), targetColumnInfo.getIndicesMap());
}
Also used : RealmProxyMediator(io.realm.internal.RealmProxyMediator) Test(org.junit.Test)

Example 8 with RealmProxyMediator

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

the class RealmProxyMediatorTests method validateTable_noDuplicateIndexInIndexFields.

@Test
public void validateTable_noDuplicateIndexInIndexFields() {
    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;
    indexSet.add(columnInfo.nameIndex);
    indexCount++;
    indexSet.add(columnInfo.ageIndex);
    indexCount++;
    indexSet.add(columnInfo.heightIndex);
    indexCount++;
    indexSet.add(columnInfo.weightIndex);
    indexCount++;
    indexSet.add(columnInfo.hasTailIndex);
    indexCount++;
    indexSet.add(columnInfo.birthdayIndex);
    indexCount++;
    indexSet.add(columnInfo.ownerIndex);
    indexCount++;
    indexSet.add(columnInfo.scaredOfDogIndex);
    indexCount++;
    assertEquals(indexCount, indexSet.size());
}
Also used : RealmProxyMediator(io.realm.internal.RealmProxyMediator) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 9 with RealmProxyMediator

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

the class RealmConfiguration method getModuleMediator.

// Finds the mediator associated with a given module.
private static RealmProxyMediator getModuleMediator(String fullyQualifiedModuleClassName) {
    String[] moduleNameParts = fullyQualifiedModuleClassName.split("\\.");
    String moduleSimpleName = moduleNameParts[moduleNameParts.length - 1];
    String mediatorName = String.format("io.realm.%s%s", moduleSimpleName, "Mediator");
    Class<?> clazz;
    //noinspection TryWithIdenticalCatches
    try {
        clazz = Class.forName(mediatorName);
        Constructor<?> constructor = clazz.getDeclaredConstructors()[0];
        constructor.setAccessible(true);
        return (RealmProxyMediator) constructor.newInstance();
    } catch (ClassNotFoundException e) {
        throw new RealmException("Could not find " + mediatorName, e);
    } catch (InvocationTargetException e) {
        throw new RealmException("Could not create an instance of " + mediatorName, e);
    } catch (InstantiationException e) {
        throw new RealmException("Could not create an instance of " + mediatorName, e);
    } catch (IllegalAccessException e) {
        throw new RealmException("Could not create an instance of " + mediatorName, e);
    }
}
Also used : RealmProxyMediator(io.realm.internal.RealmProxyMediator) RealmException(io.realm.exceptions.RealmException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 10 with RealmProxyMediator

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

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