Search in sources :

Example 46 with Table

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

the class RealmTests method schemaIndexCacheIsUpdatedAfterSchemaChange.

@Test
public void schemaIndexCacheIsUpdatedAfterSchemaChange() {
    final CatRealmProxy.CatColumnInfo catColumnInfo;
    catColumnInfo = (CatRealmProxy.CatColumnInfo) realm.schema.columnIndices.getColumnInfo(Cat.class);
    final long nameIndex = catColumnInfo.nameIndex;
    final AtomicLong nameIndexNew = new AtomicLong(-1L);
    // Changes column index of "name".
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            final Table catTable = realm.getSchema().getTable(Cat.CLASS_NAME);
            final long nameIndex = catTable.getColumnIndex(Cat.FIELD_NAME);
            catTable.removeColumn(nameIndex);
            final long newIndex = catTable.addColumn(RealmFieldType.STRING, Cat.FIELD_NAME, true);
            realm.setVersion(realm.getConfiguration().getSchemaVersion() + 1);
            nameIndexNew.set(newIndex);
        }
    });
    // We need to update index cache if the schema version was changed in the same thread.
    realm.sharedRealm.invokeSchemaChangeListenerIfSchemaChanged();
    // Checks if the index was changed.
    assertNotEquals(nameIndex, nameIndexNew);
    // Checks if index in the ColumnInfo is updated.
    assertEquals(nameIndexNew.get(), catColumnInfo.nameIndex);
    assertEquals(nameIndexNew.get(), (long) catColumnInfo.getIndicesMap().get(Cat.FIELD_NAME));
    // Checks by actual get and set.
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            final Cat cat = realm.createObject(Cat.class);
            cat.setName("pochi");
        }
    });
    //noinspection ConstantConditions
    assertEquals("pochi", realm.where(Cat.class).findFirst().getName());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Table(io.realm.internal.Table) Cat(io.realm.entities.Cat) SharedRealm(io.realm.internal.SharedRealm) Test(org.junit.Test)

Example 47 with Table

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

the class AllTypesRealmProxy method insert.

public static long insert(Realm realm, some.test.AllTypes 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.AllTypes.class);
    long tableNativePtr = table.getNativeTablePointer();
    AllTypesColumnInfo columnInfo = (AllTypesColumnInfo) realm.schema.getColumnInfo(some.test.AllTypes.class);
    long pkColumnIndex = table.getPrimaryKey();
    String primaryKeyValue = ((AllTypesRealmProxyInterface) object).realmGet$columnString();
    long rowIndex = Table.NO_MATCH;
    if (primaryKeyValue == null) {
        rowIndex = Table.nativeFindFirstNull(tableNativePtr, pkColumnIndex);
    } else {
        rowIndex = Table.nativeFindFirstString(tableNativePtr, pkColumnIndex, primaryKeyValue);
    }
    if (rowIndex == Table.NO_MATCH) {
        rowIndex = table.addEmptyRowWithPrimaryKey(primaryKeyValue, false);
    } else {
        Table.throwDuplicatePrimaryKeyException(primaryKeyValue);
    }
    cache.put(object, rowIndex);
    Table.nativeSetLong(tableNativePtr, columnInfo.columnLongIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnLong(), false);
    Table.nativeSetFloat(tableNativePtr, columnInfo.columnFloatIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnFloat(), false);
    Table.nativeSetDouble(tableNativePtr, columnInfo.columnDoubleIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnDouble(), false);
    Table.nativeSetBoolean(tableNativePtr, columnInfo.columnBooleanIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnBoolean(), false);
    java.util.Date realmGet$columnDate = ((AllTypesRealmProxyInterface) object).realmGet$columnDate();
    if (realmGet$columnDate != null) {
        Table.nativeSetTimestamp(tableNativePtr, columnInfo.columnDateIndex, rowIndex, realmGet$columnDate.getTime(), false);
    }
    byte[] realmGet$columnBinary = ((AllTypesRealmProxyInterface) object).realmGet$columnBinary();
    if (realmGet$columnBinary != null) {
        Table.nativeSetByteArray(tableNativePtr, columnInfo.columnBinaryIndex, rowIndex, realmGet$columnBinary, false);
    }
    some.test.AllTypes columnObjectObj = ((AllTypesRealmProxyInterface) object).realmGet$columnObject();
    if (columnObjectObj != null) {
        Long cachecolumnObject = cache.get(columnObjectObj);
        if (cachecolumnObject == null) {
            cachecolumnObject = AllTypesRealmProxy.insert(realm, columnObjectObj, cache);
        }
        Table.nativeSetLink(tableNativePtr, columnInfo.columnObjectIndex, rowIndex, cachecolumnObject, false);
    }
    RealmList<some.test.AllTypes> columnRealmListList = ((AllTypesRealmProxyInterface) object).realmGet$columnRealmList();
    if (columnRealmListList != null) {
        long columnRealmListNativeLinkViewPtr = Table.nativeGetLinkView(tableNativePtr, columnInfo.columnRealmListIndex, rowIndex);
        for (some.test.AllTypes columnRealmListItem : columnRealmListList) {
            Long cacheItemIndexcolumnRealmList = cache.get(columnRealmListItem);
            if (cacheItemIndexcolumnRealmList == null) {
                cacheItemIndexcolumnRealmList = AllTypesRealmProxy.insert(realm, columnRealmListItem, cache);
            }
            LinkView.nativeAdd(columnRealmListNativeLinkViewPtr, cacheItemIndexcolumnRealmList);
        }
    }
    return rowIndex;
}
Also used : Table(io.realm.internal.Table) Date(java.util.Date) RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Example 48 with Table

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

the class AllTypesRealmProxy method createOrUpdateUsingJsonObject.

@SuppressWarnings("cast")
public static some.test.AllTypes createOrUpdateUsingJsonObject(Realm realm, JSONObject json, boolean update) throws JSONException {
    final List<String> excludeFields = new ArrayList<String>(2);
    some.test.AllTypes obj = null;
    if (update) {
        Table table = realm.getTable(some.test.AllTypes.class);
        long pkColumnIndex = table.getPrimaryKey();
        long rowIndex = Table.NO_MATCH;
        if (json.isNull("columnString")) {
            rowIndex = table.findFirstNull(pkColumnIndex);
        } else {
            rowIndex = table.findFirstString(pkColumnIndex, json.getString("columnString"));
        }
        if (rowIndex != Table.NO_MATCH) {
            final BaseRealm.RealmObjectContext objectContext = BaseRealm.objectContext.get();
            try {
                objectContext.set(realm, table.getUncheckedRow(rowIndex), realm.schema.getColumnInfo(some.test.AllTypes.class), false, Collections.<String>emptyList());
                obj = new io.realm.AllTypesRealmProxy();
            } finally {
                objectContext.clear();
            }
        }
    }
    if (obj == null) {
        if (json.has("columnObject")) {
            excludeFields.add("columnObject");
        }
        if (json.has("columnRealmList")) {
            excludeFields.add("columnRealmList");
        }
        if (json.has("columnString")) {
            if (json.isNull("columnString")) {
                obj = (io.realm.AllTypesRealmProxy) realm.createObjectInternal(some.test.AllTypes.class, null, true, excludeFields);
            } else {
                obj = (io.realm.AllTypesRealmProxy) realm.createObjectInternal(some.test.AllTypes.class, json.getString("columnString"), true, excludeFields);
            }
        } else {
            throw new IllegalArgumentException("JSON object doesn't have the primary key field 'columnString'.");
        }
    }
    if (json.has("columnLong")) {
        if (json.isNull("columnLong")) {
            throw new IllegalArgumentException("Trying to set non-nullable field 'columnLong' to null.");
        } else {
            ((AllTypesRealmProxyInterface) obj).realmSet$columnLong((long) json.getLong("columnLong"));
        }
    }
    if (json.has("columnFloat")) {
        if (json.isNull("columnFloat")) {
            throw new IllegalArgumentException("Trying to set non-nullable field 'columnFloat' to null.");
        } else {
            ((AllTypesRealmProxyInterface) obj).realmSet$columnFloat((float) json.getDouble("columnFloat"));
        }
    }
    if (json.has("columnDouble")) {
        if (json.isNull("columnDouble")) {
            throw new IllegalArgumentException("Trying to set non-nullable field 'columnDouble' to null.");
        } else {
            ((AllTypesRealmProxyInterface) obj).realmSet$columnDouble((double) json.getDouble("columnDouble"));
        }
    }
    if (json.has("columnBoolean")) {
        if (json.isNull("columnBoolean")) {
            throw new IllegalArgumentException("Trying to set non-nullable field 'columnBoolean' to null.");
        } else {
            ((AllTypesRealmProxyInterface) obj).realmSet$columnBoolean((boolean) json.getBoolean("columnBoolean"));
        }
    }
    if (json.has("columnDate")) {
        if (json.isNull("columnDate")) {
            ((AllTypesRealmProxyInterface) obj).realmSet$columnDate(null);
        } else {
            Object timestamp = json.get("columnDate");
            if (timestamp instanceof String) {
                ((AllTypesRealmProxyInterface) obj).realmSet$columnDate(JsonUtils.stringToDate((String) timestamp));
            } else {
                ((AllTypesRealmProxyInterface) obj).realmSet$columnDate(new Date(json.getLong("columnDate")));
            }
        }
    }
    if (json.has("columnBinary")) {
        if (json.isNull("columnBinary")) {
            ((AllTypesRealmProxyInterface) obj).realmSet$columnBinary(null);
        } else {
            ((AllTypesRealmProxyInterface) obj).realmSet$columnBinary(JsonUtils.stringToBytes(json.getString("columnBinary")));
        }
    }
    if (json.has("columnObject")) {
        if (json.isNull("columnObject")) {
            ((AllTypesRealmProxyInterface) obj).realmSet$columnObject(null);
        } else {
            some.test.AllTypes columnObjectObj = AllTypesRealmProxy.createOrUpdateUsingJsonObject(realm, json.getJSONObject("columnObject"), update);
            ((AllTypesRealmProxyInterface) obj).realmSet$columnObject(columnObjectObj);
        }
    }
    if (json.has("columnRealmList")) {
        if (json.isNull("columnRealmList")) {
            ((AllTypesRealmProxyInterface) obj).realmSet$columnRealmList(null);
        } else {
            ((AllTypesRealmProxyInterface) obj).realmGet$columnRealmList().clear();
            JSONArray array = json.getJSONArray("columnRealmList");
            for (int i = 0; i < array.length(); i++) {
                some.test.AllTypes item = AllTypesRealmProxy.createOrUpdateUsingJsonObject(realm, array.getJSONObject(i), update);
                ((AllTypesRealmProxyInterface) obj).realmGet$columnRealmList().add(item);
            }
        }
    }
    return obj;
}
Also used : Table(io.realm.internal.Table) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Date(java.util.Date) JSONObject(org.json.JSONObject)

Example 49 with Table

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

the class AllTypesRealmProxy method insert.

public static void insert(Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel, Long> cache) {
    Table table = realm.getTable(some.test.AllTypes.class);
    long tableNativePtr = table.getNativeTablePointer();
    AllTypesColumnInfo columnInfo = (AllTypesColumnInfo) realm.schema.getColumnInfo(some.test.AllTypes.class);
    long pkColumnIndex = table.getPrimaryKey();
    some.test.AllTypes object = null;
    while (objects.hasNext()) {
        object = (some.test.AllTypes) 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;
            }
            String primaryKeyValue = ((AllTypesRealmProxyInterface) object).realmGet$columnString();
            long rowIndex = Table.NO_MATCH;
            if (primaryKeyValue == null) {
                rowIndex = Table.nativeFindFirstNull(tableNativePtr, pkColumnIndex);
            } else {
                rowIndex = Table.nativeFindFirstString(tableNativePtr, pkColumnIndex, primaryKeyValue);
            }
            if (rowIndex == Table.NO_MATCH) {
                rowIndex = table.addEmptyRowWithPrimaryKey(primaryKeyValue, false);
            } else {
                Table.throwDuplicatePrimaryKeyException(primaryKeyValue);
            }
            cache.put(object, rowIndex);
            Table.nativeSetLong(tableNativePtr, columnInfo.columnLongIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnLong(), false);
            Table.nativeSetFloat(tableNativePtr, columnInfo.columnFloatIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnFloat(), false);
            Table.nativeSetDouble(tableNativePtr, columnInfo.columnDoubleIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnDouble(), false);
            Table.nativeSetBoolean(tableNativePtr, columnInfo.columnBooleanIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnBoolean(), false);
            java.util.Date realmGet$columnDate = ((AllTypesRealmProxyInterface) object).realmGet$columnDate();
            if (realmGet$columnDate != null) {
                Table.nativeSetTimestamp(tableNativePtr, columnInfo.columnDateIndex, rowIndex, realmGet$columnDate.getTime(), false);
            }
            byte[] realmGet$columnBinary = ((AllTypesRealmProxyInterface) object).realmGet$columnBinary();
            if (realmGet$columnBinary != null) {
                Table.nativeSetByteArray(tableNativePtr, columnInfo.columnBinaryIndex, rowIndex, realmGet$columnBinary, false);
            }
            some.test.AllTypes columnObjectObj = ((AllTypesRealmProxyInterface) object).realmGet$columnObject();
            if (columnObjectObj != null) {
                Long cachecolumnObject = cache.get(columnObjectObj);
                if (cachecolumnObject == null) {
                    cachecolumnObject = AllTypesRealmProxy.insert(realm, columnObjectObj, cache);
                }
                table.setLink(columnInfo.columnObjectIndex, rowIndex, cachecolumnObject, false);
            }
            RealmList<some.test.AllTypes> columnRealmListList = ((AllTypesRealmProxyInterface) object).realmGet$columnRealmList();
            if (columnRealmListList != null) {
                long columnRealmListNativeLinkViewPtr = Table.nativeGetLinkView(tableNativePtr, columnInfo.columnRealmListIndex, rowIndex);
                for (some.test.AllTypes columnRealmListItem : columnRealmListList) {
                    Long cacheItemIndexcolumnRealmList = cache.get(columnRealmListItem);
                    if (cacheItemIndexcolumnRealmList == null) {
                        cacheItemIndexcolumnRealmList = AllTypesRealmProxy.insert(realm, columnRealmListItem, cache);
                    }
                    LinkView.nativeAdd(columnRealmListNativeLinkViewPtr, cacheItemIndexcolumnRealmList);
                }
            }
        }
    }
}
Also used : Table(io.realm.internal.Table) Date(java.util.Date) RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Example 50 with Table

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

the class BooleansRealmProxy method insert.

public static long insert(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)

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