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;
}
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();
}
}
}
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());
}
Aggregations