Search in sources :

Example 51 with Table

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

the class BooleansRealmProxy method insertOrUpdate.

public static long insertOrUpdate(Realm realm, some.test.Booleans object, Map<RealmModel, Long> cache) {
    if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) {
        return ((RealmObjectProxy) object).realmGet$proxyState().getRow$realm().getIndex();
    }
    Table table = realm.getTable(some.test.Booleans.class);
    long tableNativePtr = table.getNativeTablePointer();
    BooleansColumnInfo columnInfo = (BooleansColumnInfo) realm.schema.getColumnInfo(some.test.Booleans.class);
    long rowIndex = Table.nativeAddEmptyRow(tableNativePtr, 1);
    cache.put(object, rowIndex);
    Table.nativeSetBoolean(tableNativePtr, columnInfo.doneIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$done(), false);
    Table.nativeSetBoolean(tableNativePtr, columnInfo.isReadyIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$isReady(), false);
    Table.nativeSetBoolean(tableNativePtr, columnInfo.mCompletedIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$mCompleted(), false);
    Table.nativeSetBoolean(tableNativePtr, columnInfo.anotherBooleanIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$anotherBoolean(), false);
    return rowIndex;
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy) Table(io.realm.internal.Table)

Example 52 with Table

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

the class BooleansRealmProxy method initTable.

public static Table initTable(SharedRealm sharedRealm) {
    if (!sharedRealm.hasTable("class_Booleans")) {
        Table table = sharedRealm.getTable("class_Booleans");
        table.addColumn(RealmFieldType.BOOLEAN, "done", Table.NOT_NULLABLE);
        table.addColumn(RealmFieldType.BOOLEAN, "isReady", Table.NOT_NULLABLE);
        table.addColumn(RealmFieldType.BOOLEAN, "mCompleted", Table.NOT_NULLABLE);
        table.addColumn(RealmFieldType.BOOLEAN, "anotherBoolean", Table.NOT_NULLABLE);
        table.setPrimaryKey("");
        return table;
    }
    return sharedRealm.getTable("class_Booleans");
}
Also used : Table(io.realm.internal.Table)

Example 53 with Table

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

the class BooleansRealmProxy method insert.

public static void insert(Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel, Long> cache) {
    Table table = realm.getTable(some.test.Booleans.class);
    long tableNativePtr = table.getNativeTablePointer();
    BooleansColumnInfo columnInfo = (BooleansColumnInfo) realm.schema.getColumnInfo(some.test.Booleans.class);
    some.test.Booleans object = null;
    while (objects.hasNext()) {
        object = (some.test.Booleans) objects.next();
        if (!cache.containsKey(object)) {
            if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) {
                cache.put(object, ((RealmObjectProxy) object).realmGet$proxyState().getRow$realm().getIndex());
                continue;
            }
            long rowIndex = Table.nativeAddEmptyRow(tableNativePtr, 1);
            cache.put(object, rowIndex);
            Table.nativeSetBoolean(tableNativePtr, columnInfo.doneIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$done(), false);
            Table.nativeSetBoolean(tableNativePtr, columnInfo.isReadyIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$isReady(), false);
            Table.nativeSetBoolean(tableNativePtr, columnInfo.mCompletedIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$mCompleted(), false);
            Table.nativeSetBoolean(tableNativePtr, columnInfo.anotherBooleanIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$anotherBoolean(), false);
        }
    }
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy) Table(io.realm.internal.Table)

Example 54 with Table

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

the class RealmMigrationTests method removeFieldsBeforePrimaryKey.

// Removing fields before a pk field does not affect the pk.
@Test
public void removeFieldsBeforePrimaryKey() {
    buildInitialMigrationSchema(MigrationPosteriorIndexOnly.CLASS_NAME, false);
    RealmMigration migration = new RealmMigration() {

        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            realm.getSchema().get(MigrationPosteriorIndexOnly.CLASS_NAME).removeField(MigrationPrimaryKey.FIELD_FIRST).removeField(MigrationPrimaryKey.FIELD_SECOND);
        }
    };
    RealmConfiguration realmConfig = configFactory.createConfigurationBuilder().schemaVersion(1).schema(MigrationPosteriorIndexOnly.class).migration(migration).build();
    Realm realm = Realm.getInstance(realmConfig);
    Table table = realm.getSchema().getTable(MigrationPosteriorIndexOnly.class);
    assertTrue(table.hasPrimaryKey());
    assertEquals(MigrationPosteriorIndexOnly.DEFAULT_FIELDS_COUNT, table.getColumnCount());
    assertEquals(MigrationPosteriorIndexOnly.DEFAULT_PRIMARY_INDEX, table.getPrimaryKey());
    assertEquals(MigrationPosteriorIndexOnly.FIELD_PRIMARY, table.getColumnName(table.getPrimaryKey()));
}
Also used : Table(io.realm.internal.Table) Test(org.junit.Test)

Example 55 with Table

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

the class RealmMigrationTests method addingSearchIndexTwice.

// Adding search index is idempotent.
@Test
public void addingSearchIndexTwice() throws IOException {
    final Class[] classes = { PrimaryKeyAsLong.class, PrimaryKeyAsString.class };
    for (final Class clazz : classes) {
        final AtomicBoolean didMigrate = new AtomicBoolean(false);
        RealmMigration migration = new RealmMigration() {

            @Override
            public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
                RealmObjectSchema schema = realm.getSchema().getSchemaForClass(clazz.getSimpleName());
                schema.addIndex("id");
                // @PrimaryKey fields in PrimaryKeyAsLong and PrimaryKeyAsString.class should be set 'nullable'.
                schema.setNullable("name", true);
                didMigrate.set(true);
            }
        };
        RealmConfiguration realmConfig = configFactory.createConfigurationBuilder().schemaVersion(42).schema(clazz).migration(migration).build();
        Realm.deleteRealm(realmConfig);
        configFactory.copyRealmFromAssets(context, "default-before-migration.realm", Realm.DEFAULT_REALM_NAME);
        Realm.migrateRealm(realmConfig);
        realm = Realm.getInstance(realmConfig);
        assertEquals(42, realm.getVersion());
        assertTrue(didMigrate.get());
        Table table = realm.getTable(clazz);
        assertEquals(true, table.hasSearchIndex(table.getColumnIndex("id")));
        realm.close();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Table(io.realm.internal.Table) Test(org.junit.Test)

Aggregations

Table (io.realm.internal.Table)68 RealmObjectProxy (io.realm.internal.RealmObjectProxy)20 Test (org.junit.Test)19 Date (java.util.Date)10 PrimaryKeyAsString (io.realm.entities.PrimaryKeyAsString)5 RealmMigrationNeededException (io.realm.exceptions.RealmMigrationNeededException)4 HashMap (java.util.HashMap)4 PrimaryKeyAsLong (io.realm.entities.PrimaryKeyAsLong)3 RealmException (io.realm.exceptions.RealmException)3 UncheckedRow (io.realm.internal.UncheckedRow)2 JSONObject (org.json.JSONObject)2 TargetApi (android.annotation.TargetApi)1 JsonReader (android.util.JsonReader)1 AllTypes (io.realm.entities.AllTypes)1 Cat (io.realm.entities.Cat)1 IOSAllTypes (io.realm.entities.IOSAllTypes)1 StringOnly (io.realm.entities.StringOnly)1 MigrationFieldTypeToInt (io.realm.entities.migration.MigrationFieldTypeToInt)1 MigrationFieldTypeToInteger (io.realm.entities.migration.MigrationFieldTypeToInteger)1 LinkView (io.realm.internal.LinkView)1