Skip to content

Commit c180319

Browse files
committed
switch Cursor.getRelationTargetCursor() to use nativeGetCursorFor() and secondary Cursors; this should be more lightweight and allows secondary cursors to be re-used from the main Cursor
1 parent b07bbed commit c180319

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

objectbox-java/src/main/java/io/objectbox/Cursor.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import io.objectbox.annotation.apihint.Beta;
2626
import io.objectbox.annotation.apihint.Internal;
27+
import io.objectbox.internal.CursorFactory;
2728
import io.objectbox.relation.ToMany;
2829

2930
@SuppressWarnings({"unchecked", "SameParameterValue", "unused", "WeakerAccess", "UnusedReturnValue"})
@@ -121,6 +122,8 @@ protected static native long collect004000(long cursor, long keyIfComplete, int
121122

122123
native void nativeSetBoxStoreForEntities(long cursor, Object boxStore);
123124

125+
native long nativeGetCursorFor(long cursor, int entityId);
126+
124127
protected final Transaction tx;
125128
protected final long cursor;
126129
protected final EntityInfo entityInfo;
@@ -248,11 +251,16 @@ public boolean isClosed() {
248251
return closed;
249252
}
250253

254+
/**
255+
* Note: this returns a secondary cursor, which does not survive standalone.
256+
* Secondary native cursors are destroyed once their hosting Cursor is destroyed.
257+
* Thus, use it only locally and don't store it long term.
258+
*/
251259
protected <TARGET> Cursor<TARGET> getRelationTargetCursor(Class<TARGET> targetClass) {
252-
// minor to do: optimize by using existing native cursor handle?
253-
// (Note: Cursor should not destroy the native cursor then.)
254-
255-
return tx.createCursor(targetClass);
260+
EntityInfo entityInfo = boxStoreForEntities.getEntityInfo(targetClass);
261+
long cursorHandle = nativeGetCursorFor(cursor, entityInfo.getEntityId());
262+
CursorFactory<TARGET> factory = entityInfo.getCursorFactory();
263+
return factory.createCursor(tx, cursorHandle, boxStoreForEntities);
256264
}
257265

258266
/**

0 commit comments

Comments
 (0)