Search in sources :

Example 11 with RealmObjectProxy

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

the class BooleansRealmProxy method copyOrUpdate.

public static some.test.Booleans copyOrUpdate(Realm realm, some.test.Booleans object, boolean update, Map<RealmModel, RealmObjectProxy> cache) {
    if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().threadId != realm.threadId) {
        throw new IllegalArgumentException("Objects which belong to Realm instances in other threads cannot be copied into this Realm instance.");
    }
    if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) {
        return object;
    }
    final BaseRealm.RealmObjectContext objectContext = BaseRealm.objectContext.get();
    RealmObjectProxy cachedRealmObject = cache.get(object);
    if (cachedRealmObject != null) {
        return (some.test.Booleans) cachedRealmObject;
    } else {
        return copy(realm, object, update, cache);
    }
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Example 12 with RealmObjectProxy

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

the class AllTypesRealmProxy method insertOrUpdate.

public static long insertOrUpdate(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);
    }
    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);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.columnDateIndex, rowIndex, false);
    }
    byte[] realmGet$columnBinary = ((AllTypesRealmProxyInterface) object).realmGet$columnBinary();
    if (realmGet$columnBinary != null) {
        Table.nativeSetByteArray(tableNativePtr, columnInfo.columnBinaryIndex, rowIndex, realmGet$columnBinary, false);
    } else {
        Table.nativeSetNull(tableNativePtr, columnInfo.columnBinaryIndex, rowIndex, false);
    }
    some.test.AllTypes columnObjectObj = ((AllTypesRealmProxyInterface) object).realmGet$columnObject();
    if (columnObjectObj != null) {
        Long cachecolumnObject = cache.get(columnObjectObj);
        if (cachecolumnObject == null) {
            cachecolumnObject = AllTypesRealmProxy.insertOrUpdate(realm, columnObjectObj, cache);
        }
        Table.nativeSetLink(tableNativePtr, columnInfo.columnObjectIndex, rowIndex, cachecolumnObject, false);
    } else {
        Table.nativeNullifyLink(tableNativePtr, columnInfo.columnObjectIndex, rowIndex);
    }
    long columnRealmListNativeLinkViewPtr = Table.nativeGetLinkView(tableNativePtr, columnInfo.columnRealmListIndex, rowIndex);
    LinkView.nativeClear(columnRealmListNativeLinkViewPtr);
    RealmList<some.test.AllTypes> columnRealmListList = ((AllTypesRealmProxyInterface) object).realmGet$columnRealmList();
    if (columnRealmListList != null) {
        for (some.test.AllTypes columnRealmListItem : columnRealmListList) {
            Long cacheItemIndexcolumnRealmList = cache.get(columnRealmListItem);
            if (cacheItemIndexcolumnRealmList == null) {
                cacheItemIndexcolumnRealmList = AllTypesRealmProxy.insertOrUpdate(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 13 with RealmObjectProxy

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

the class AllTypesRealmProxy method realmSet$columnRealmList.

public void realmSet$columnRealmList(RealmList<some.test.AllTypes> value) {
    if (proxyState.isUnderConstruction()) {
        if (!proxyState.getAcceptDefaultValue$realm()) {
            return;
        }
        if (proxyState.getExcludeFields$realm().contains("columnRealmList")) {
            return;
        }
        if (value != null && !value.isManaged()) {
            final Realm realm = (Realm) proxyState.getRealm$realm();
            final RealmList<some.test.AllTypes> original = value;
            value = new RealmList<some.test.AllTypes>();
            for (some.test.AllTypes item : original) {
                if (item == null || RealmObject.isManaged(item)) {
                    value.add(item);
                } else {
                    value.add(realm.copyToRealm(item));
                }
            }
        }
    }
    proxyState.getRealm$realm().checkIfValid();
    LinkView links = proxyState.getRow$realm().getLinkList(columnInfo.columnRealmListIndex);
    links.clear();
    if (value == null) {
        return;
    }
    for (RealmModel linkedObject : (RealmList<? extends RealmModel>) value) {
        if (!(RealmObject.isManaged(linkedObject) && RealmObject.isValid(linkedObject))) {
            throw new IllegalArgumentException("Each element of 'value' must be a valid managed object.");
        }
        if (((RealmObjectProxy) linkedObject).realmGet$proxyState().getRealm$realm() != proxyState.getRealm$realm()) {
            throw new IllegalArgumentException("Each element of 'value' must belong to the same Realm.");
        }
        links.add(((RealmObjectProxy) linkedObject).realmGet$proxyState().getRow$realm().getIndex());
    }
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy) LinkView(io.realm.internal.LinkView) SharedRealm(io.realm.internal.SharedRealm)

Example 14 with RealmObjectProxy

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

the class AllTypesRealmProxy method copyOrUpdate.

public static some.test.AllTypes copyOrUpdate(Realm realm, some.test.AllTypes object, boolean update, Map<RealmModel, RealmObjectProxy> cache) {
    if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().threadId != realm.threadId) {
        throw new IllegalArgumentException("Objects which belong to Realm instances in other threads cannot be copied into this Realm instance.");
    }
    if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) {
        return object;
    }
    final BaseRealm.RealmObjectContext objectContext = BaseRealm.objectContext.get();
    RealmObjectProxy cachedRealmObject = cache.get(object);
    if (cachedRealmObject != null) {
        return (some.test.AllTypes) cachedRealmObject;
    } else {
        some.test.AllTypes realmObject = null;
        boolean canUpdate = update;
        if (canUpdate) {
            Table table = realm.getTable(some.test.AllTypes.class);
            long pkColumnIndex = table.getPrimaryKey();
            String value = ((AllTypesRealmProxyInterface) object).realmGet$columnString();
            long rowIndex = Table.NO_MATCH;
            if (value == null) {
                rowIndex = table.findFirstNull(pkColumnIndex);
            } else {
                rowIndex = table.findFirstString(pkColumnIndex, value);
            }
            if (rowIndex != Table.NO_MATCH) {
                try {
                    objectContext.set(realm, table.getUncheckedRow(rowIndex), realm.schema.getColumnInfo(some.test.AllTypes.class), false, Collections.<String>emptyList());
                    realmObject = new io.realm.AllTypesRealmProxy();
                    cache.put(object, (RealmObjectProxy) realmObject);
                } finally {
                    objectContext.clear();
                }
            } else {
                canUpdate = false;
            }
        }
        if (canUpdate) {
            return update(realm, realmObject, object, cache);
        } else {
            return copy(realm, object, update, cache);
        }
    }
}
Also used : Table(io.realm.internal.Table) RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Example 15 with RealmObjectProxy

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

the class AllTypesRealmProxy method insertOrUpdate.

public static void insertOrUpdate(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);
            }
            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);
            } else {
                Table.nativeSetNull(tableNativePtr, columnInfo.columnDateIndex, rowIndex, false);
            }
            byte[] realmGet$columnBinary = ((AllTypesRealmProxyInterface) object).realmGet$columnBinary();
            if (realmGet$columnBinary != null) {
                Table.nativeSetByteArray(tableNativePtr, columnInfo.columnBinaryIndex, rowIndex, realmGet$columnBinary, false);
            } else {
                Table.nativeSetNull(tableNativePtr, columnInfo.columnBinaryIndex, rowIndex, false);
            }
            some.test.AllTypes columnObjectObj = ((AllTypesRealmProxyInterface) object).realmGet$columnObject();
            if (columnObjectObj != null) {
                Long cachecolumnObject = cache.get(columnObjectObj);
                if (cachecolumnObject == null) {
                    cachecolumnObject = AllTypesRealmProxy.insertOrUpdate(realm, columnObjectObj, cache);
                }
                Table.nativeSetLink(tableNativePtr, columnInfo.columnObjectIndex, rowIndex, cachecolumnObject, false);
            } else {
                Table.nativeNullifyLink(tableNativePtr, columnInfo.columnObjectIndex, rowIndex);
            }
            long columnRealmListNativeLinkViewPtr = Table.nativeGetLinkView(tableNativePtr, columnInfo.columnRealmListIndex, rowIndex);
            LinkView.nativeClear(columnRealmListNativeLinkViewPtr);
            RealmList<some.test.AllTypes> columnRealmListList = ((AllTypesRealmProxyInterface) object).realmGet$columnRealmList();
            if (columnRealmListList != null) {
                for (some.test.AllTypes columnRealmListItem : columnRealmListList) {
                    Long cacheItemIndexcolumnRealmList = cache.get(columnRealmListItem);
                    if (cacheItemIndexcolumnRealmList == null) {
                        cacheItemIndexcolumnRealmList = AllTypesRealmProxy.insertOrUpdate(realm, columnRealmListItem, cache);
                    }
                    LinkView.nativeAdd(columnRealmListNativeLinkViewPtr, cacheItemIndexcolumnRealmList);
                }
            }
        }
    }
}
Also used : Table(io.realm.internal.Table) Date(java.util.Date) RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Aggregations

RealmObjectProxy (io.realm.internal.RealmObjectProxy)43 Table (io.realm.internal.Table)20 Date (java.util.Date)8 Row (io.realm.internal.Row)6 InvalidRow (io.realm.internal.InvalidRow)2 LinkView (io.realm.internal.LinkView)2 UiThreadTest (android.support.test.annotation.UiThreadTest)1 AllTypes (io.realm.entities.AllTypes)1 Collection (io.realm.internal.Collection)1 PendingRow (io.realm.internal.PendingRow)1 SharedRealm (io.realm.internal.SharedRealm)1 UncheckedRow (io.realm.internal.UncheckedRow)1 Test (org.junit.Test)1 Observable (rx.Observable)1