Skip to content

Commit 5e1ccc1

Browse files
authored
Merge pull request #180 from TileDB-Inc/victorgiannakouris/ch2662/leverage-nio-bytebuffers-in-tiledb-java
Leverage NIO ByteBuffers in TileDB Java [ch2662]
2 parents ed94e85 + 0570207 commit 5e1ccc1

File tree

7 files changed

+456
-1
lines changed

7 files changed

+456
-1
lines changed

src/main/c/generated/tiledb_wrap.cxx

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7016,6 +7016,34 @@ SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1query_1set_1b
70167016
return jresult;
70177017
}
70187018

7019+
SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1query_1set_1buffer_1nio(JNIEnv *jenv, jclass jcls, jlong jarg1, jlong jarg2, jstring jarg3, jobject jarg4, jlong jarg5) {
7020+
void* buffer = (void *)jenv->GetDirectBufferAddress(jarg4);
7021+
7022+
jint jresult = 0 ;
7023+
tiledb_ctx_t *arg1 = (tiledb_ctx_t *) 0 ;
7024+
tiledb_query_t *arg2 = (tiledb_query_t *) 0 ;
7025+
char *arg3 = (char *) 0 ;
7026+
void *arg4 = (void *) 0 ;
7027+
uint64_t *arg5 = (uint64_t *) 0 ;
7028+
int32_t result;
7029+
7030+
(void)jenv;
7031+
(void)jcls;
7032+
arg1 = *(tiledb_ctx_t **)&jarg1;
7033+
arg2 = *(tiledb_query_t **)&jarg2;
7034+
arg3 = 0;
7035+
if (jarg3) {
7036+
arg3 = (char *)jenv->GetStringUTFChars(jarg3, 0);
7037+
if (!arg3) return 0;
7038+
}
7039+
arg4 = *(void **)&jarg4;
7040+
arg5 = *(uint64_t **)&jarg5;
7041+
result = (int32_t)tiledb_query_set_buffer(arg1,arg2,(char const *)arg3, buffer,arg5);
7042+
jresult = (jint)result;
7043+
if (arg3) jenv->ReleaseStringUTFChars(jarg3, (const char *)arg3);
7044+
return jresult;
7045+
}
7046+
70197047

70207048
SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1query_1set_1buffer_1var(JNIEnv *jenv, jclass jcls, jlong jarg1, jlong jarg2, jstring jarg3, jlong jarg4, jlong jarg5, jlong jarg6, jlong jarg7) {
70217049
jint jresult = 0 ;
@@ -9503,6 +9531,34 @@ SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1stats_1dump_1
95039531
}
95049532

95059533

9534+
SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1stats_1raw_1dump(JNIEnv *jenv, jclass jcls, jlong jarg1) {
9535+
jint jresult = 0 ;
9536+
FILE *arg1 = (FILE *) 0 ;
9537+
int32_t result;
9538+
9539+
(void)jenv;
9540+
(void)jcls;
9541+
arg1 = *(FILE **)&jarg1;
9542+
result = (int32_t)tiledb_stats_raw_dump(arg1);
9543+
jresult = (jint)result;
9544+
return jresult;
9545+
}
9546+
9547+
9548+
SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1stats_1raw_1dump_1str(JNIEnv *jenv, jclass jcls, jlong jarg1) {
9549+
jint jresult = 0 ;
9550+
char **arg1 = (char **) 0 ;
9551+
int32_t result;
9552+
9553+
(void)jenv;
9554+
(void)jcls;
9555+
arg1 = *(char ***)&jarg1;
9556+
result = (int32_t)tiledb_stats_raw_dump_str(arg1);
9557+
jresult = (jint)result;
9558+
return jresult;
9559+
}
9560+
9561+
95069562
SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1stats_1free_1str(JNIEnv *jenv, jclass jcls, jlong jarg1) {
95079563
jint jresult = 0 ;
95089564
char **arg1 = (char **) 0 ;

src/main/java/io/tiledb/java/api/Query.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
import io.tiledb.libtiledb.*;
3030
import java.math.BigInteger;
31+
import java.nio.ByteBuffer;
32+
import java.nio.ByteOrder;
3133
import java.util.Collections;
3234
import java.util.HashMap;
3335
import java.util.Map;
@@ -58,6 +60,7 @@ public class Query implements AutoCloseable {
5860
private NativeArray subarray;
5961

6062
private Map<String, NativeArray> buffers_;
63+
private Map<String, ByteBuffer> byteBuffers_;
6164
private Map<String, Pair<NativeArray, NativeArray>> var_buffers_;
6265
private Map<String, Pair<uint64_tArray, uint64_tArray>> buffer_sizes_;
6366

@@ -78,6 +81,7 @@ public Query(Array array, QueryType type) throws TileDBError {
7881
this.querypp = _querypp;
7982
this.queryp = tiledb.tiledb_query_tpp_value(_querypp);
8083
this.buffers_ = Collections.synchronizedMap(new HashMap<>());
84+
this.byteBuffers_ = Collections.synchronizedMap(new HashMap<>());
8185
this.var_buffers_ = Collections.synchronizedMap(new HashMap<>());
8286
this.buffer_sizes_ = Collections.synchronizedMap(new HashMap<>());
8387
}
@@ -467,6 +471,82 @@ public synchronized Query setBuffer(String attr, NativeArray buffer, long buffer
467471
return this;
468472
}
469473

474+
/**
475+
* * Sets a NIO ByteBuffer
476+
*
477+
* @param attr The attribute
478+
* @param bufferElements
479+
* @return The NIO ByteBuffer
480+
* @throws TileDBError
481+
*/
482+
public synchronized ByteBuffer setBuffer(String attr, long bufferElements) throws TileDBError {
483+
if (bufferElements <= 0) {
484+
throw new TileDBError("Number of buffer elements must be >= 1");
485+
}
486+
487+
Datatype dt;
488+
489+
try (ArraySchema schema = array.getSchema()) {
490+
try (Domain domain = schema.getDomain()) {
491+
if (domain.hasDimension(attr)) {
492+
dt = domain.getDimension(attr).getType();
493+
} else {
494+
try (Attribute attribute = schema.getAttribute(attr)) {
495+
dt = attribute.getType();
496+
}
497+
}
498+
}
499+
}
500+
501+
int size = Util.castLongToInt(bufferElements * dt.getNativeSize());
502+
503+
ByteBuffer buffer = ByteBuffer.allocateDirect(size);
504+
505+
// Set the byte order to the native system's native order
506+
buffer.order(ByteOrder.nativeOrder());
507+
508+
this.byteBuffers_.put(attr, buffer);
509+
510+
this.setBuffer(attr, buffer);
511+
512+
return buffer;
513+
}
514+
515+
/**
516+
* * Sets a NIO ByteBuffer
517+
*
518+
* @param attr The attribute
519+
* @param buffer The input NIO ByteBuffer
520+
* @return The NIO ByteBuffer
521+
* @throws TileDBError
522+
*/
523+
public synchronized ByteBuffer setBuffer(String attr, ByteBuffer buffer) throws TileDBError {
524+
if (buffer.capacity() <= 0) {
525+
throw new TileDBError("Number of buffer elements must be >= 1");
526+
}
527+
528+
if (!buffer.isDirect()) {
529+
throw new TileDBError(
530+
"The ByteBuffer provided is not direct. Please provide a direct buffer (ByteBuffer.allocateDirect(...))");
531+
}
532+
533+
if (!buffer.order().equals(ByteOrder.nativeOrder())) {
534+
// TODO: Add a logger component to Query class and a WARN here
535+
buffer.order(ByteOrder.nativeOrder());
536+
}
537+
538+
this.byteBuffers_.put(attr, buffer);
539+
540+
uint64_tArray values_array_size = new uint64_tArray(1);
541+
values_array_size.setitem(0, BigInteger.valueOf(buffer.capacity()));
542+
543+
ctx.handleError(
544+
tiledb.tiledb_query_set_buffer_nio(
545+
ctx.getCtxp(), queryp, attr, buffer, values_array_size.cast()));
546+
547+
return buffer;
548+
}
549+
470550
/**
471551
* Sets a buffer for a variable-sized getAttribute.
472552
*
@@ -885,6 +965,18 @@ public Object getBuffer(String attr) throws TileDBError {
885965
}
886966
}
887967

968+
/**
969+
* Retrieves the ByteBuffer of attribute attr
970+
*
971+
* @param attr The attribute name
972+
* @return The ByteBuffer
973+
* @throws TileDBError A TileDB exception
974+
*/
975+
public ByteBuffer getByteBuffer(String attr) throws TileDBError {
976+
if (byteBuffers_.containsKey(attr)) return byteBuffers_.get(attr);
977+
else throw new TileDBError("ByteBuffer does not exist for attribute: " + attr);
978+
}
979+
888980
/**
889981
* Return an array containing offsets for a variable attribute buffer
890982
*

src/main/java/io/tiledb/java/api/Util.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,11 @@ public static String[] bytesToStrings(long[] offsets, byte[] data) {
2929

3030
return results;
3131
}
32+
33+
public static int castLongToInt(long num) throws TileDBError {
34+
if (num > Integer.MAX_VALUE)
35+
throw new TileDBError(num + " is larger that the integer max value");
36+
37+
return (int) num;
38+
}
3239
}

src/main/java/io/tiledb/libtiledb/tiledb.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
package io.tiledb.libtiledb;
1010

11+
import java.nio.ByteBuffer;
12+
1113
public class tiledb implements tiledbConstants {
1214
public static SWIGTYPE_p_p_char new_charpArray(int nelements) {
1315
long cPtr = tiledbJNI.new_charpArray(nelements);
@@ -2131,6 +2133,20 @@ public static int tiledb_query_set_buffer(
21312133
SWIGTYPE_p_unsigned_long_long.getCPtr(buffer_size));
21322134
}
21332135

2136+
public static int tiledb_query_set_buffer_nio(
2137+
SWIGTYPE_p_tiledb_ctx_t ctx,
2138+
SWIGTYPE_p_tiledb_query_t query,
2139+
String name,
2140+
ByteBuffer buffer,
2141+
SWIGTYPE_p_unsigned_long_long buffer_size) {
2142+
return tiledbJNI.tiledb_query_set_buffer_nio(
2143+
SWIGTYPE_p_tiledb_ctx_t.getCPtr(ctx),
2144+
SWIGTYPE_p_tiledb_query_t.getCPtr(query),
2145+
name,
2146+
buffer,
2147+
SWIGTYPE_p_unsigned_long_long.getCPtr(buffer_size));
2148+
}
2149+
21342150
public static int tiledb_query_set_buffer_var(
21352151
SWIGTYPE_p_tiledb_ctx_t ctx,
21362152
SWIGTYPE_p_tiledb_query_t query,
@@ -3155,6 +3171,14 @@ public static int tiledb_stats_dump_str(SWIGTYPE_p_p_char out) {
31553171
return tiledbJNI.tiledb_stats_dump_str(SWIGTYPE_p_p_char.getCPtr(out));
31563172
}
31573173

3174+
public static int tiledb_stats_raw_dump(SWIGTYPE_p_FILE out) {
3175+
return tiledbJNI.tiledb_stats_raw_dump(SWIGTYPE_p_FILE.getCPtr(out));
3176+
}
3177+
3178+
public static int tiledb_stats_raw_dump_str(SWIGTYPE_p_p_char out) {
3179+
return tiledbJNI.tiledb_stats_raw_dump_str(SWIGTYPE_p_p_char.getCPtr(out));
3180+
}
3181+
31583182
public static int tiledb_stats_free_str(SWIGTYPE_p_p_char out) {
31593183
return tiledbJNI.tiledb_stats_free_str(SWIGTYPE_p_p_char.getCPtr(out));
31603184
}

src/main/java/io/tiledb/libtiledb/tiledbConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ public interface tiledbConstants {
1414
public static final int TILEDB_OOM = (-2);
1515
public static final int TILEDB_VERSION_MAJOR = 2;
1616
public static final int TILEDB_VERSION_MINOR = 0;
17-
public static final int TILEDB_VERSION_PATCH = 0;
17+
public static final int TILEDB_VERSION_PATCH = 5;
1818
}

src/main/java/io/tiledb/libtiledb/tiledbJNI.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
package io.tiledb.libtiledb;
1010

11+
import java.nio.ByteBuffer;
12+
1113
public class tiledbJNI {
1214

1315
static {
@@ -920,6 +922,9 @@ public static final native int tiledb_array_schema_has_attribute(
920922
public static final native int tiledb_query_set_buffer(
921923
long jarg1, long jarg2, String jarg3, long jarg4, long jarg5);
922924

925+
public static final native int tiledb_query_set_buffer_nio(
926+
long jarg1, long jarg2, String jarg3, ByteBuffer jarg4, long jarg5);
927+
923928
public static final native int tiledb_query_set_buffer_var(
924929
long jarg1, long jarg2, String jarg3, long jarg4, long jarg5, long jarg6, long jarg7);
925930

@@ -1188,6 +1193,10 @@ public static final native int tiledb_uri_to_path(
11881193

11891194
public static final native int tiledb_stats_dump_str(long jarg1);
11901195

1196+
public static final native int tiledb_stats_raw_dump(long jarg1);
1197+
1198+
public static final native int tiledb_stats_raw_dump_str(long jarg1);
1199+
11911200
public static final native int tiledb_stats_free_str(long jarg1);
11921201

11931202
public static final native int tiledb_dimension_dump_stdout(long jarg1, long jarg2);

0 commit comments

Comments
 (0)