Skip to content

Commit c8efcd6

Browse files
committed
NIO nullable ByteBuffers
1 parent 8f36bbb commit c8efcd6

File tree

7 files changed

+443
-1
lines changed

7 files changed

+443
-1
lines changed

src/main/c/custom/tiledb_custom.cxx

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,68 @@ extern "C" {
12121212
return jresult;
12131213
}
12141214

1215+
SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1query_1set_1buffer_1nullable_1nio(JNIEnv *jenv, jclass jcls, jlong jarg1, jlong jarg2, jstring jarg3, jobject jarg4, jlong jarg5, jobject jarg6, jlong jarg7) {
1216+
jint jresult = 0 ;
1217+
tiledb_ctx_t *arg1 = (tiledb_ctx_t *) 0 ;
1218+
tiledb_query_t *arg2 = (tiledb_query_t *) 0 ;
1219+
char *arg3 = (char *) 0 ;
1220+
void *arg4 = (void *) 0 ;
1221+
uint64_t *arg5 = (uint64_t *) 0 ;
1222+
uint8_t *arg6 = (uint8_t *) 0 ;
1223+
uint64_t *arg7 = (uint64_t *) 0 ;
1224+
int32_t result;
1225+
1226+
(void)jenv;
1227+
(void)jcls;
1228+
arg1 = *(tiledb_ctx_t **)&jarg1;
1229+
arg2 = *(tiledb_query_t **)&jarg2;
1230+
arg3 = 0;
1231+
if (jarg3) {
1232+
arg3 = (char *)jenv->GetStringUTFChars(jarg3, 0);
1233+
if (!arg3) return 0;
1234+
}
1235+
arg4 = *(void **)&jarg4;
1236+
arg5 = *(uint64_t **)&jarg5;
1237+
arg6 = *(uint8_t **)&jarg6;
1238+
arg7 = *(uint64_t **)&jarg7;
1239+
result = (int32_t)tiledb_query_set_buffer_nullable(arg1,arg2,(char const *)arg3,(void *)jenv->GetDirectBufferAddress(jarg4),arg5,(uint8_t *)jenv->GetDirectBufferAddress(jarg6),arg7);
1240+
jresult = (jint)result;
1241+
if (arg3) jenv->ReleaseStringUTFChars(jarg3, (const char *)arg3);
1242+
return jresult;
1243+
}
1244+
1245+
SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1query_1set_1buffer_1var_1nullable_1nio(JNIEnv *jenv, jclass jcls, jlong jarg1, jlong jarg2, jstring jarg3, jobject jarg4, jlong jarg5, jobject jarg6, jlong jarg7, jobject jarg8, jlong jarg9) {
1246+
uint64_t * offsets = (uint64_t *)jenv->GetDirectBufferAddress(jarg4);
1247+
void * values = (void *)jenv->GetDirectBufferAddress(jarg6);
1248+
uint8_t * validity_bytemap = (uint8_t *)jenv->GetDirectBufferAddress(jarg8);
1249+
1250+
jint jresult = 0 ;
1251+
tiledb_ctx_t *arg1 = (tiledb_ctx_t *) 0 ;
1252+
tiledb_query_t *arg2 = (tiledb_query_t *) 0 ;
1253+
char *arg3 = (char *) 0 ;
1254+
uint64_t *arg5 = (uint64_t *) 0 ;
1255+
uint64_t *arg7 = (uint64_t *) 0 ;
1256+
uint64_t *arg9 = (uint64_t *) 0 ;
1257+
int32_t result;
1258+
1259+
(void)jenv;
1260+
(void)jcls;
1261+
arg1 = *(tiledb_ctx_t **)&jarg1;
1262+
arg2 = *(tiledb_query_t **)&jarg2;
1263+
arg3 = 0;
1264+
if (jarg3) {
1265+
arg3 = (char *)jenv->GetStringUTFChars(jarg3, 0);
1266+
if (!arg3) return 0;
1267+
}
1268+
arg5 = *(uint64_t **)&jarg5;
1269+
arg7 = *(uint64_t **)&jarg7;
1270+
arg9 = *(uint64_t **)&jarg9;
1271+
result = (int32_t)tiledb_query_set_buffer_var_nullable(arg1,arg2,(char const *)arg3, offsets, arg5, values, arg7, validity_bytemap, arg9);
1272+
jresult = (jint)result;
1273+
if (arg3) jenv->ReleaseStringUTFChars(jarg3, (const char *)arg3);
1274+
return jresult;
1275+
}
1276+
12151277

12161278
#ifdef __cplusplus
12171279
}

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

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public class Query implements AutoCloseable {
6262
private Map<String, Pair<NativeArray, NativeArray>> buffers_;
6363
private Map<String, Pair<uint64_tArray, uint64_tArray>> buffer_sizes_;
6464
private Map<String, NativeArray> validityByteMaps_;
65+
private Map<String, ByteBuffer> validityByteMapsByteBuffers_;
6566

6667
public Query(Array array, QueryType type) throws TileDBError {
6768
Context _ctx = array.getCtx();
@@ -83,6 +84,7 @@ public Query(Array array, QueryType type) throws TileDBError {
8384
this.byteBuffers_ = Collections.synchronizedMap(new HashMap<>());
8485
this.buffer_sizes_ = Collections.synchronizedMap(new HashMap<>());
8586
this.validityByteMaps_ = Collections.synchronizedMap(new HashMap<>());
87+
this.validityByteMapsByteBuffers_ = Collections.synchronizedMap(new HashMap<>());
8688
}
8789

8890
public Query(Array array) throws TileDBError {
@@ -604,6 +606,67 @@ public synchronized Query setBufferNullable(
604606
return this;
605607
}
606608

609+
/**
610+
* Sets a nullable buffer for a fixed-sized attribute.
611+
*
612+
* @param attr The attribute name.
613+
* @param buffer NativeBuffer to be used for the attribute values.
614+
* @param bytemap The byte-map
615+
* @exception TileDBError A TileDB exception
616+
*/
617+
public synchronized Query setBufferNullableNIO(String attr, ByteBuffer buffer, ByteBuffer bytemap)
618+
throws TileDBError {
619+
620+
if (buffer.capacity() <= 0) {
621+
throw new TileDBError("Number of buffer elements must be >= 1");
622+
}
623+
624+
if (!buffer.isDirect()) {
625+
throw new TileDBError(
626+
"The ByteBuffer provided is not direct. Please provide a direct buffer (ByteBuffer.allocateDirect(...))");
627+
}
628+
629+
if (!buffer.order().equals(ByteOrder.nativeOrder())) {
630+
throw new TileDBError(
631+
"The order of the data ByteBuffer should be the same as the native order (ByteOrder.nativeOrder()).");
632+
}
633+
634+
this.byteBuffers_.put(attr, new Pair<>(null, buffer));
635+
636+
uint64_tArray offsets_array_size = new uint64_tArray(1);
637+
uint64_tArray values_array_size = new uint64_tArray(1);
638+
uint64_tArray buffer_validity_bytemap_size = new uint64_tArray(1);
639+
640+
offsets_array_size.setitem(0, BigInteger.valueOf(0));
641+
values_array_size.setitem(0, BigInteger.valueOf(buffer.capacity()));
642+
buffer_validity_bytemap_size.setitem(0, BigInteger.valueOf(bytemap.capacity()));
643+
644+
buffer_sizes_.put(attr, new Pair<>(offsets_array_size, values_array_size));
645+
646+
Pair<uint64_tArray, uint64_tArray> buffer_sizes =
647+
new Pair<>(offsets_array_size, values_array_size);
648+
649+
this.byteBuffers_.put(attr, new Pair<>(null, buffer));
650+
651+
buffer_sizes_.put(attr, buffer_sizes);
652+
validityByteMapsByteBuffers_.put(attr, bytemap);
653+
654+
// Set the actual TileDB buffer
655+
uint64_tArray buffer_size = buffer_sizes.getSecond();
656+
657+
ctx.handleError(
658+
Utils.tiledb_query_set_buffer_nullable_nio(
659+
ctx.getCtxp(),
660+
queryp,
661+
attr,
662+
buffer,
663+
buffer_size.cast(),
664+
bytemap,
665+
buffer_validity_bytemap_size.cast()));
666+
667+
return this;
668+
}
669+
607670
/**
608671
* Sets a nullable buffer for a variable-sized getAttribute.
609672
*
@@ -681,6 +744,70 @@ public synchronized Query setBufferNullable(
681744
return this;
682745
}
683746

747+
/**
748+
* Sets a ByteBuffer buffer for a variable-sized getAttribute.
749+
*
750+
* @param attr Attribute name
751+
* @param offsets Offsets where a new element begins in the data buffer.
752+
* @param buffer Buffer vector with elements of the attribute type.
753+
* @exception TileDBError A TileDB exception
754+
*/
755+
public synchronized Query setBufferNullableNIO(
756+
String attr, ByteBuffer offsets, ByteBuffer buffer, ByteBuffer bytemap) throws TileDBError {
757+
758+
if (attr.equals(tiledb.tiledb_coords())) {
759+
throw new TileDBError("Cannot set coordinate buffer as variable sized.");
760+
}
761+
762+
if (!offsets.order().equals(ByteOrder.nativeOrder()) && offsets.position() > 0) {
763+
throw new TileDBError(
764+
"The order of the offsets ByteBuffer should be the same as the native order (ByteOrder.nativeOrder()) before values are inserted.");
765+
}
766+
767+
if (!buffer.order().equals(ByteOrder.nativeOrder()) && buffer.position() > 0) {
768+
throw new TileDBError(
769+
"The order of the data ByteBuffer should be the same as the native order (ByteOrder.nativeOrder()) before values are inserted.");
770+
}
771+
772+
offsets.order(ByteOrder.nativeOrder());
773+
buffer.order(ByteOrder.nativeOrder());
774+
775+
uint64_tArray offsets_array_size = new uint64_tArray(0);
776+
uint64_tArray values_array_size = new uint64_tArray(0);
777+
uint64_tArray buffer_validity_bytemap_size = new uint64_tArray(0);
778+
779+
offsets_array_size.setitem(0, BigInteger.valueOf(offsets.capacity()));
780+
values_array_size.setitem(0, BigInteger.valueOf(buffer.capacity()));
781+
buffer_validity_bytemap_size.setitem(0, BigInteger.valueOf(bytemap.capacity()));
782+
783+
Pair<uint64_tArray, uint64_tArray> buffer_sizes =
784+
new Pair<>(offsets_array_size, values_array_size);
785+
786+
// Close previous buffers if they exist for this attribute
787+
if (buffers_.containsKey(attr)) {
788+
Pair<NativeArray, NativeArray> prev_buffers = buffers_.get(attr);
789+
prev_buffers.getFirst().close();
790+
prev_buffers.getSecond().close();
791+
}
792+
793+
buffer_sizes_.put(attr, buffer_sizes);
794+
this.byteBuffers_.put(attr, new Pair(offsets, buffer));
795+
796+
ctx.handleError(
797+
Utils.tiledb_query_set_buffer_var_nullable_nio(
798+
ctx.getCtxp(),
799+
queryp,
800+
attr,
801+
offsets,
802+
offsets_array_size.cast(),
803+
buffer,
804+
values_array_size.cast(),
805+
bytemap,
806+
buffer_validity_bytemap_size.cast()));
807+
808+
return this;
809+
}
810+
684811
/**
685812
* * Sets a NIO ByteBuffer
686813
*
@@ -699,7 +826,6 @@ public synchronized Query setBuffer(String attr, long bufferElements) throws Til
699826
int size = Util.castLongToInt(bufferElements * dt.getNativeSize());
700827

701828
ByteBuffer buffer = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
702-
;
703829

704830
this.setBuffer(attr, buffer);
705831

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,24 @@ public static int tiledb_query_set_buffer_nio(
202202
SWIGTYPE_p_unsigned_long_long.getCPtr(buffer_size));
203203
}
204204

205+
public static int tiledb_query_set_buffer_nullable_nio(
206+
SWIGTYPE_p_tiledb_ctx_t ctx,
207+
SWIGTYPE_p_tiledb_query_t query,
208+
String name,
209+
ByteBuffer buffer,
210+
SWIGTYPE_p_unsigned_long_long buffer_size,
211+
ByteBuffer validity_bytemap,
212+
SWIGTYPE_p_unsigned_long_long validity_bytemap_size) {
213+
return tiledbJNI.tiledb_query_set_buffer_nullable_nio(
214+
SWIGTYPE_p_tiledb_ctx_t.getCPtr(ctx),
215+
SWIGTYPE_p_tiledb_query_t.getCPtr(query),
216+
name,
217+
buffer,
218+
SWIGTYPE_p_unsigned_long_long.getCPtr(buffer_size),
219+
validity_bytemap,
220+
SWIGTYPE_p_unsigned_long_long.getCPtr(validity_bytemap_size));
221+
}
222+
205223
public static int tiledb_query_set_buffer_var_nio(
206224
SWIGTYPE_p_tiledb_ctx_t ctx,
207225
SWIGTYPE_p_tiledb_query_t query,
@@ -220,6 +238,28 @@ public static int tiledb_query_set_buffer_var_nio(
220238
SWIGTYPE_p_unsigned_long_long.getCPtr(buffer_val_size));
221239
}
222240

241+
public static int tiledb_query_set_buffer_var_nullable_nio(
242+
SWIGTYPE_p_tiledb_ctx_t ctx,
243+
SWIGTYPE_p_tiledb_query_t query,
244+
String name,
245+
ByteBuffer offsets,
246+
SWIGTYPE_p_unsigned_long_long offsets_size,
247+
ByteBuffer buffer,
248+
SWIGTYPE_p_unsigned_long_long buffer_size,
249+
ByteBuffer validity_bytemap,
250+
SWIGTYPE_p_unsigned_long_long validity_bytemap_size) {
251+
return tiledbJNI.tiledb_query_set_buffer_var_nullable_nio(
252+
SWIGTYPE_p_tiledb_ctx_t.getCPtr(ctx),
253+
SWIGTYPE_p_tiledb_query_t.getCPtr(query),
254+
name,
255+
offsets,
256+
SWIGTYPE_p_unsigned_long_long.getCPtr(offsets_size),
257+
buffer,
258+
SWIGTYPE_p_unsigned_long_long.getCPtr(buffer_size),
259+
validity_bytemap,
260+
SWIGTYPE_p_unsigned_long_long.getCPtr(validity_bytemap_size));
261+
}
262+
223263
public static int tiledb_object_ls(
224264
SWIGTYPE_p_tiledb_ctx_t ctx, String path, PathCallback callback) {
225265
return tiledbJNI.tiledb_object_ls_java(

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ public static final native int tiledb_query_set_buffer_var_nio(
3939
ByteBuffer jarg6,
4040
long jarg7);
4141

42+
public static final native int tiledb_query_set_buffer_nullable_nio(
43+
long jarg1,
44+
long jarg2,
45+
String jarg3,
46+
ByteBuffer jarg4,
47+
long jarg5,
48+
ByteBuffer jarg6,
49+
long jarg7);
50+
51+
public static final native int tiledb_query_set_buffer_var_nullable_nio(
52+
long jarg1,
53+
long jarg2,
54+
String jarg3,
55+
ByteBuffer jarg4,
56+
long jarg5,
57+
ByteBuffer jarg6,
58+
long jarg7,
59+
ByteBuffer jarg8,
60+
long jarg9);
61+
4262
public static final native int tiledb_query_submit_async_java(
4363
long jarg1,
4464
SWIGTYPE_p_tiledb_ctx_t jarg1_,

0 commit comments

Comments
 (0)