Skip to content

Added ByteBuffer support for setSubarray() #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/c/generated/tiledb_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/tiledb/java/api/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/tiledb/libtiledb/tiledb.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/tiledb/libtiledb/tiledbJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
69 changes: 59 additions & 10 deletions src/test/java/io/tiledb/java/api/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public void arrayWithVarAttrWrite() throws Exception {
}

@Test
public void queryTestNIOReadArray() throws Exception {
public void queryTestNIOReadArrayRange() throws Exception {
arrayCreate();
arrayWrite();

Expand Down Expand Up @@ -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();
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down