Skip to content

Leverage NIO ByteBuffers in TileDB Java [ch2662] #180

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
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
56 changes: 56 additions & 0 deletions src/main/c/generated/tiledb_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7016,6 +7016,34 @@ SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1query_1set_1b
return jresult;
}

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) {
void* buffer = (void *)jenv->GetDirectBufferAddress(jarg4);

jint jresult = 0 ;
tiledb_ctx_t *arg1 = (tiledb_ctx_t *) 0 ;
tiledb_query_t *arg2 = (tiledb_query_t *) 0 ;
char *arg3 = (char *) 0 ;
void *arg4 = (void *) 0 ;
uint64_t *arg5 = (uint64_t *) 0 ;
int32_t result;

(void)jenv;
(void)jcls;
arg1 = *(tiledb_ctx_t **)&jarg1;
arg2 = *(tiledb_query_t **)&jarg2;
arg3 = 0;
if (jarg3) {
arg3 = (char *)jenv->GetStringUTFChars(jarg3, 0);
if (!arg3) return 0;
}
arg4 = *(void **)&jarg4;
arg5 = *(uint64_t **)&jarg5;
result = (int32_t)tiledb_query_set_buffer(arg1,arg2,(char const *)arg3, buffer,arg5);
jresult = (jint)result;
if (arg3) jenv->ReleaseStringUTFChars(jarg3, (const char *)arg3);
return jresult;
}


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) {
jint jresult = 0 ;
Expand Down Expand Up @@ -9503,6 +9531,34 @@ SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1stats_1dump_1
}


SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1stats_1raw_1dump(JNIEnv *jenv, jclass jcls, jlong jarg1) {
jint jresult = 0 ;
FILE *arg1 = (FILE *) 0 ;
int32_t result;

(void)jenv;
(void)jcls;
arg1 = *(FILE **)&jarg1;
result = (int32_t)tiledb_stats_raw_dump(arg1);
jresult = (jint)result;
return jresult;
}


SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1stats_1raw_1dump_1str(JNIEnv *jenv, jclass jcls, jlong jarg1) {
jint jresult = 0 ;
char **arg1 = (char **) 0 ;
int32_t result;

(void)jenv;
(void)jcls;
arg1 = *(char ***)&jarg1;
result = (int32_t)tiledb_stats_raw_dump_str(arg1);
jresult = (jint)result;
return jresult;
}


SWIGEXPORT jint JNICALL Java_io_tiledb_libtiledb_tiledbJNI_tiledb_1stats_1free_1str(JNIEnv *jenv, jclass jcls, jlong jarg1) {
jint jresult = 0 ;
char **arg1 = (char **) 0 ;
Expand Down
92 changes: 92 additions & 0 deletions src/main/java/io/tiledb/java/api/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import io.tiledb.libtiledb.*;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -58,6 +60,7 @@ public class Query implements AutoCloseable {
private NativeArray subarray;

private Map<String, NativeArray> buffers_;
private Map<String, ByteBuffer> byteBuffers_;
private Map<String, Pair<NativeArray, NativeArray>> var_buffers_;
private Map<String, Pair<uint64_tArray, uint64_tArray>> buffer_sizes_;

Expand All @@ -78,6 +81,7 @@ public Query(Array array, QueryType type) throws TileDBError {
this.querypp = _querypp;
this.queryp = tiledb.tiledb_query_tpp_value(_querypp);
this.buffers_ = Collections.synchronizedMap(new HashMap<>());
this.byteBuffers_ = Collections.synchronizedMap(new HashMap<>());
this.var_buffers_ = Collections.synchronizedMap(new HashMap<>());
this.buffer_sizes_ = Collections.synchronizedMap(new HashMap<>());
}
Expand Down Expand Up @@ -467,6 +471,82 @@ public synchronized Query setBuffer(String attr, NativeArray buffer, long buffer
return this;
}

/**
* * Sets a NIO ByteBuffer
*
* @param attr The attribute
* @param bufferElements
* @return The NIO ByteBuffer
* @throws TileDBError
*/
public synchronized ByteBuffer setBuffer(String attr, long bufferElements) throws TileDBError {
if (bufferElements <= 0) {
throw new TileDBError("Number of buffer elements must be >= 1");
}

Datatype dt;

try (ArraySchema schema = array.getSchema()) {
try (Domain domain = schema.getDomain()) {
if (domain.hasDimension(attr)) {
dt = domain.getDimension(attr).getType();
} else {
try (Attribute attribute = schema.getAttribute(attr)) {
dt = attribute.getType();
}
}
}
}

int size = Util.castLongToInt(bufferElements * dt.getNativeSize());

ByteBuffer buffer = ByteBuffer.allocateDirect(size);

// Set the byte order to the native system's native order
buffer.order(ByteOrder.nativeOrder());

this.byteBuffers_.put(attr, buffer);

this.setBuffer(attr, buffer);

return buffer;
}

/**
* * Sets a NIO ByteBuffer
*
* @param attr The attribute
* @param buffer The input NIO ByteBuffer
* @return The NIO ByteBuffer
* @throws TileDBError
*/
public synchronized ByteBuffer setBuffer(String attr, ByteBuffer buffer) throws TileDBError {
if (buffer.capacity() <= 0) {
throw new TileDBError("Number of buffer elements must be >= 1");
}

if (!buffer.isDirect()) {
throw new TileDBError(
"The ByteBuffer provided is not direct. Please provide a direct buffer (ByteBuffer.allocateDirect(...))");
}

if (!buffer.order().equals(ByteOrder.nativeOrder())) {
// TODO: Add a logger component to Query class and a WARN here
buffer.order(ByteOrder.nativeOrder());
}

this.byteBuffers_.put(attr, buffer);

uint64_tArray values_array_size = new uint64_tArray(1);
values_array_size.setitem(0, BigInteger.valueOf(buffer.capacity()));

ctx.handleError(
tiledb.tiledb_query_set_buffer_nio(
ctx.getCtxp(), queryp, attr, buffer, values_array_size.cast()));

return buffer;
}

/**
* Sets a buffer for a variable-sized getAttribute.
*
Expand Down Expand Up @@ -885,6 +965,18 @@ public Object getBuffer(String attr) throws TileDBError {
}
}

/**
* Retrieves the ByteBuffer of attribute attr
*
* @param attr The attribute name
* @return The ByteBuffer
* @throws TileDBError A TileDB exception
*/
public ByteBuffer getByteBuffer(String attr) throws TileDBError {
if (byteBuffers_.containsKey(attr)) return byteBuffers_.get(attr);
else throw new TileDBError("ByteBuffer does not exist for attribute: " + attr);
}

/**
* Return an array containing offsets for a variable attribute buffer
*
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/tiledb/java/api/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ public static String[] bytesToStrings(long[] offsets, byte[] data) {

return results;
}

public static int castLongToInt(long num) throws TileDBError {
if (num > Integer.MAX_VALUE)
throw new TileDBError(num + " is larger that the integer max value");

return (int) num;
}
}
24 changes: 24 additions & 0 deletions src/main/java/io/tiledb/libtiledb/tiledb.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package io.tiledb.libtiledb;

import java.nio.ByteBuffer;

public class tiledb implements tiledbConstants {
public static SWIGTYPE_p_p_char new_charpArray(int nelements) {
long cPtr = tiledbJNI.new_charpArray(nelements);
Expand Down Expand Up @@ -2131,6 +2133,20 @@ public static int tiledb_query_set_buffer(
SWIGTYPE_p_unsigned_long_long.getCPtr(buffer_size));
}

public static int tiledb_query_set_buffer_nio(
SWIGTYPE_p_tiledb_ctx_t ctx,
SWIGTYPE_p_tiledb_query_t query,
String name,
ByteBuffer buffer,
SWIGTYPE_p_unsigned_long_long buffer_size) {
return tiledbJNI.tiledb_query_set_buffer_nio(
SWIGTYPE_p_tiledb_ctx_t.getCPtr(ctx),
SWIGTYPE_p_tiledb_query_t.getCPtr(query),
name,
buffer,
SWIGTYPE_p_unsigned_long_long.getCPtr(buffer_size));
}

public static int tiledb_query_set_buffer_var(
SWIGTYPE_p_tiledb_ctx_t ctx,
SWIGTYPE_p_tiledb_query_t query,
Expand Down Expand Up @@ -3155,6 +3171,14 @@ public static int tiledb_stats_dump_str(SWIGTYPE_p_p_char out) {
return tiledbJNI.tiledb_stats_dump_str(SWIGTYPE_p_p_char.getCPtr(out));
}

public static int tiledb_stats_raw_dump(SWIGTYPE_p_FILE out) {
return tiledbJNI.tiledb_stats_raw_dump(SWIGTYPE_p_FILE.getCPtr(out));
}

public static int tiledb_stats_raw_dump_str(SWIGTYPE_p_p_char out) {
return tiledbJNI.tiledb_stats_raw_dump_str(SWIGTYPE_p_p_char.getCPtr(out));
}

public static int tiledb_stats_free_str(SWIGTYPE_p_p_char out) {
return tiledbJNI.tiledb_stats_free_str(SWIGTYPE_p_p_char.getCPtr(out));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/tiledb/libtiledb/tiledbConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public interface tiledbConstants {
public static final int TILEDB_OOM = (-2);
public static final int TILEDB_VERSION_MAJOR = 2;
public static final int TILEDB_VERSION_MINOR = 0;
public static final int TILEDB_VERSION_PATCH = 0;
public static final int TILEDB_VERSION_PATCH = 5;
}
9 changes: 9 additions & 0 deletions src/main/java/io/tiledb/libtiledb/tiledbJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package io.tiledb.libtiledb;

import java.nio.ByteBuffer;

public class tiledbJNI {

static {
Expand Down Expand Up @@ -920,6 +922,9 @@ public static final native int tiledb_array_schema_has_attribute(
public static final native int tiledb_query_set_buffer(
long jarg1, long jarg2, String jarg3, long jarg4, long jarg5);

public static final native int tiledb_query_set_buffer_nio(
long jarg1, long jarg2, String jarg3, ByteBuffer jarg4, long jarg5);

public static final native int tiledb_query_set_buffer_var(
long jarg1, long jarg2, String jarg3, long jarg4, long jarg5, long jarg6, long jarg7);

Expand Down Expand Up @@ -1188,6 +1193,10 @@ public static final native int tiledb_uri_to_path(

public static final native int tiledb_stats_dump_str(long jarg1);

public static final native int tiledb_stats_raw_dump(long jarg1);

public static final native int tiledb_stats_raw_dump_str(long jarg1);

public static final native int tiledb_stats_free_str(long jarg1);

public static final native int tiledb_dimension_dump_stdout(long jarg1, long jarg2);
Expand Down
Loading