From 3f3da195280b725addf1ac7cd8396cbbe6be1c2c Mon Sep 17 00:00:00 2001 From: gsvic Date: Fri, 17 Jul 2020 16:33:58 +0300 Subject: [PATCH] Added ByteBuffer support for setSubarray() --- src/main/c/generated/tiledb_wrap.cxx | 19 +++++ src/main/java/io/tiledb/java/api/Query.java | 11 +++ src/main/java/io/tiledb/libtiledb/tiledb.java | 6 ++ .../java/io/tiledb/libtiledb/tiledbJNI.java | 3 + .../java/io/tiledb/java/api/QueryTest.java | 69 ++++++++++++++++--- 5 files changed, 98 insertions(+), 10 deletions(-) diff --git a/src/main/c/generated/tiledb_wrap.cxx b/src/main/c/generated/tiledb_wrap.cxx index 47a4c1b3..3ca7ea3f 100644 --- a/src/main/c/generated/tiledb_wrap.cxx +++ b/src/main/c/generated/tiledb_wrap.cxx @@ -6989,6 +6989,25 @@ SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1query_1set_1s return jresult; } +SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1query_1set_1subarray_1nio(JNIEnv *jenv, jclass jcls, jlong jarg1, jlong jarg2, jobject jarg3) { + void* buffer = (void *)jenv->GetDirectBufferAddress(jarg3); + + jint jresult = 0 ; + tiledb_ctx_t *arg1 = (tiledb_ctx_t *) 0 ; + tiledb_query_t *arg2 = (tiledb_query_t *) 0 ; + void *arg3 = (void *) 0 ; + int32_t result; + + (void)jenv; + (void)jcls; + arg1 = *(tiledb_ctx_t **)&jarg1; + arg2 = *(tiledb_query_t **)&jarg2; + arg3 = *(void **)&jarg3; + result = (int32_t)tiledb_query_set_subarray(arg1,arg2,buffer); + jresult = (jint)result; + return jresult; +} + SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1query_1set_1buffer(JNIEnv *jenv, jclass jcls, jlong jarg1, jlong jarg2, jstring jarg3, jlong jarg4, jlong jarg5) { jint jresult = 0 ; diff --git a/src/main/java/io/tiledb/java/api/Query.java b/src/main/java/io/tiledb/java/api/Query.java index 88cb28e9..b54affb1 100644 --- a/src/main/java/io/tiledb/java/api/Query.java +++ b/src/main/java/io/tiledb/java/api/Query.java @@ -188,6 +188,17 @@ public synchronized Query setSubarray(NativeArray subarray) throws TileDBError { return this; } + /** + * Sets a subarray, defined in the order dimensions were added. Coordinates are inclusive. + * + * @param subarray The targeted subarray. + * @exception TileDBError A TileDB exception + */ + public synchronized Query setSubarray(ByteBuffer subarray) throws TileDBError { + ctx.handleError(tiledb.tiledb_query_set_subarray_nio(ctx.getCtxp(), queryp, subarray)); + return this; + } + /** * Adds a 1D range along a subarray dimension, which is in the form (start, end). The datatype of * the range components must be the same as the type of the domain of the array in the query. diff --git a/src/main/java/io/tiledb/libtiledb/tiledb.java b/src/main/java/io/tiledb/libtiledb/tiledb.java index 27d58ae3..be8ae9a6 100644 --- a/src/main/java/io/tiledb/libtiledb/tiledb.java +++ b/src/main/java/io/tiledb/libtiledb/tiledb.java @@ -2119,6 +2119,12 @@ public static int tiledb_query_set_subarray( SWIGTYPE_p_void.getCPtr(subarray)); } + public static int tiledb_query_set_subarray_nio( + SWIGTYPE_p_tiledb_ctx_t ctx, SWIGTYPE_p_tiledb_query_t query, ByteBuffer subarray) { + return tiledbJNI.tiledb_query_set_subarray_nio( + SWIGTYPE_p_tiledb_ctx_t.getCPtr(ctx), SWIGTYPE_p_tiledb_query_t.getCPtr(query), subarray); + } + public static int tiledb_query_set_buffer( SWIGTYPE_p_tiledb_ctx_t ctx, SWIGTYPE_p_tiledb_query_t query, diff --git a/src/main/java/io/tiledb/libtiledb/tiledbJNI.java b/src/main/java/io/tiledb/libtiledb/tiledbJNI.java index 4862f2a7..9388edbc 100644 --- a/src/main/java/io/tiledb/libtiledb/tiledbJNI.java +++ b/src/main/java/io/tiledb/libtiledb/tiledbJNI.java @@ -919,6 +919,9 @@ public static final native int tiledb_array_schema_has_attribute( public static final native int tiledb_query_set_subarray(long jarg1, long jarg2, long jarg3); + public static final native int tiledb_query_set_subarray_nio( + long jarg1, long jarg2, ByteBuffer jarg3); + public static final native int tiledb_query_set_buffer( long jarg1, long jarg2, String jarg3, long jarg4, long jarg5); diff --git a/src/test/java/io/tiledb/java/api/QueryTest.java b/src/test/java/io/tiledb/java/api/QueryTest.java index 57a83e44..1eb34ad6 100644 --- a/src/test/java/io/tiledb/java/api/QueryTest.java +++ b/src/test/java/io/tiledb/java/api/QueryTest.java @@ -304,7 +304,7 @@ public void arrayWithVarAttrWrite() throws Exception { } @Test - public void queryTestNIOReadArray() throws Exception { + public void queryTestNIOReadArrayRange() throws Exception { arrayCreate(); arrayWrite(); @@ -346,6 +346,52 @@ public void queryTestNIOReadArray() throws Exception { new int[] {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}, d2_result); } + @Test + public void queryTestNIOReadArraySubArray() throws Exception { + arrayCreate(); + arrayWrite(); + + Array array = new Array(ctx, arrayURI, TILEDB_READ); + + Query query = new Query(array, TILEDB_READ); + + int bufferSize = 4; + + query.setBuffer("rows", bufferSize); + query.setBuffer("cols", bufferSize); + ByteBuffer d1 = query.getByteBuffer("rows").getSecond(); + ByteBuffer d2 = query.getByteBuffer("cols").getSecond(); + + ByteBuffer subarray = ByteBuffer.allocateDirect(4 * Datatype.TILEDB_INT32.getNativeSize()); + + subarray.order(ByteOrder.nativeOrder()).putInt(1).putInt(4).putInt(1).putInt(4); + + query.setSubarray(subarray); + + query.setLayout(TILEDB_ROW_MAJOR); + + int[] d1_result = new int[16]; + int[] d2_result = new int[16]; + int idx = 0; + + while (query.getQueryStatus() != QueryStatus.TILEDB_COMPLETED) { + query.submit(); + + while (d1.hasRemaining() && d2.hasRemaining()) { + d1_result[idx] = d1.getInt(); + d2_result[idx] = d2.getInt(); + idx++; + } + d1.clear(); + d2.clear(); + } + + Assert.assertArrayEquals( + new int[] {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4}, d1_result); + Assert.assertArrayEquals( + new int[] {1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4}, d2_result); + } + @Test public void queryTestNIOReadArrayArbitrarySize() throws Exception { arrayCreate(); @@ -361,8 +407,9 @@ public void queryTestNIOReadArrayArbitrarySize() throws Exception { ByteBuffer d1 = query.getByteBuffer("rows").getSecond(); ByteBuffer d2 = query.getByteBuffer("cols").getSecond(); - query.addRange(0, 1, 4); - query.addRange(1, 1, 4); + ByteBuffer subarray = ByteBuffer.allocateDirect(4 * Datatype.TILEDB_INT32.getNativeSize()); + subarray.order(ByteOrder.nativeOrder()).putInt(1).putInt(4).putInt(1).putInt(4); + query.setSubarray(subarray); query.setLayout(TILEDB_ROW_MAJOR); @@ -397,9 +444,9 @@ public void arrayReadTest() throws Exception { try (Array array = new Array(ctx, arrayURI, TILEDB_READ); Query query = new Query(array, TILEDB_READ)) { - // Slice only rows 1, 2 and cols 2, 3, 4 - query.addRange(0, 1, 2); - query.addRange(1, 2, 4); + ByteBuffer subarray = ByteBuffer.allocateDirect(4 * Datatype.TILEDB_INT32.getNativeSize()); + subarray.order(ByteOrder.nativeOrder()).putInt(1).putInt(2).putInt(2).putInt(4); + query.setSubarray(subarray); query.setLayout(TILEDB_ROW_MAJOR); query.setBuffer("rows", 3).setBuffer("cols", 3).setBuffer("a1", 3).setBuffer("a2", 6); @@ -453,9 +500,9 @@ public void arrayReadTestCustomBufferWithDifferentOrder() throws Exception { try (Array array = new Array(ctx, arrayURI, TILEDB_READ); Query query = new Query(array, TILEDB_READ)) { - // Slice only rows 1, 2 and cols 2, 3, 4 - query.addRange(0, 1, 2); - query.addRange(1, 2, 4); + ByteBuffer subarray = ByteBuffer.allocateDirect(4 * Datatype.TILEDB_INT32.getNativeSize()); + subarray.order(ByteOrder.nativeOrder()).putInt(1).putInt(2).putInt(2).putInt(4); + query.setSubarray(subarray); query.setLayout(TILEDB_ROW_MAJOR); // Set the opposite byte order from the native system @@ -523,7 +570,9 @@ public void queryTestNIOSetBufferVarChar() throws Exception { ByteBuffer dataBuffer = ByteBuffer.allocateDirect(1000); Query q = new Query(new Array(ctx, arrayURI), TILEDB_READ); - q.addRange(0, 1, 8); + ByteBuffer subarray = ByteBuffer.allocateDirect(4 * Datatype.TILEDB_INT32.getNativeSize()); + subarray.order(ByteOrder.nativeOrder()).putInt(1).putInt(8); + q.setSubarray(subarray); q.setBuffer("a1", offsetsBuffer, dataBuffer); q.submit();