Skip to content

Expose db memory address and path to libduckdb_java.so #87

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/jni/duckdb_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,15 @@ struct ConnectionHolder {
}
};

// Returns the pointer's memory address of duckdb::DuckDB as a string
jstring _duckdb_jdbc_db_memory_address(JNIEnv *env, jclass, jobject conn_ref_buf) {
auto conn_ref = (ConnectionHolder *)env->GetDirectBufferAddress(conn_ref_buf);
auto db_ptr = conn_ref->db.get();
std::ostringstream db_ptr_address;
db_ptr_address << db_ptr;
return env->NewStringUTF(db_ptr_address.str().c_str());
}

/**
* Throws a SQLException and returns nullptr if a valid Connection can't be retrieved from the buffer.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/jni/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ JNIEXPORT jobject JNICALL Java_org_duckdb_DuckDBNative_duckdb_1jdbc_1connect(JNI
}
}

JNIEXPORT jstring JNICALL Java_org_duckdb_DuckDBNative_duckdb_1jdbc_1db_1memory_1address(JNIEnv * env, jclass param0, jobject param1) {
try {
return _duckdb_jdbc_db_memory_address(env, param0, param1);
} catch (const std::exception &e) {
duckdb::ErrorData error(e);
ThrowJNI(env, error.Message().c_str());

return nullptr;
}
}

JNIEXPORT void JNICALL Java_org_duckdb_DuckDBNative_duckdb_1jdbc_1set_1auto_1commit(JNIEnv * env, jclass param0, jobject param1, jboolean param2) {
try {
return _duckdb_jdbc_set_auto_commit(env, param0, param1, param2);
Expand Down
4 changes: 4 additions & 0 deletions src/jni/functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ jobject _duckdb_jdbc_connect(JNIEnv * env, jclass param0, jobject param1);

JNIEXPORT jobject JNICALL Java_org_duckdb_DuckDBNative_duckdb_1jdbc_1connect(JNIEnv * env, jclass param0, jobject param1);

jstring _duckdb_jdbc_db_memory_address(JNIEnv * env, jclass param0, jobject param1);

JNIEXPORT jstring JNICALL Java_org_duckdb_DuckDBNative_duckdb_1jdbc_1db_1memory_1address(JNIEnv * env, jclass param0, jobject param1);

void _duckdb_jdbc_set_auto_commit(JNIEnv * env, jclass param0, jobject param1, jboolean param2);

JNIEXPORT void JNICALL Java_org_duckdb_DuckDBNative_duckdb_1jdbc_1set_1auto_1commit(JNIEnv * env, jclass param0, jobject param1, jboolean param2);
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/duckdb/DuckDBConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,27 @@ public void registerArrowStream(String name, Object arrow_array_stream) {
long array_stream_address = getArrowStreamAddress(arrow_array_stream);
DuckDBNative.duckdb_jdbc_arrow_register(conn_ref, array_stream_address, name.getBytes(StandardCharsets.UTF_8));
}

/**
* <p>Gets the memory address of the DuckDB C++ object from within JNI. With this it is possible to create
* database connections outside the JDBC driver.</p>
* <p><b>Big warning:</b> The JDBC driver does not know anything about these outside connection! Closing the
* database must happen with JDBC! Using outside connections implies keeping at least one JDBC connection open to
* avoid accessing freed memory!</p>
* @return The memory address as a hexadecimal number represented by a string like '0x12345678'
* or null if the connection is closed
*/
public String getDuckDBMemoryAddress() {
if (this.conn_ref != null) {
return DuckDBNative.duckdb_jdbc_db_memory_address(this.conn_ref);
}
return null;
}

/**
* <p>Gets the path and temporal file name of the duckdb_java lib as it was loaded via System.load().</p>
*/
public String getDuckDBResourceFile() {
return DuckDBNative.getDuckDBResourceFile();
}
}
12 changes: 12 additions & 0 deletions src/main/java/org/duckdb/DuckDBNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,27 @@ class DuckDBNative {
URL lib_res = DuckDBNative.class.getResource(lib_res_name);
if (lib_res == null) {
System.load(Paths.get("build/debug", lib_res_name).normalize().toAbsolutePath().toString());
duckDBResourceFile = Paths.get("build/debug", lib_res_name).normalize().toAbsolutePath().toString();
} else {
try (final InputStream lib_res_input_stream = lib_res.openStream()) {
Files.copy(lib_res_input_stream, lib_file, StandardCopyOption.REPLACE_EXISTING);
}
new File(lib_file.toString()).deleteOnExit();
System.load(lib_file.toAbsolutePath().toString());
duckDBResourceFile = lib_file.toAbsolutePath().toString();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

// Full path and name of the libduckdb_java.so
private static final String duckDBResourceFile;

protected static String getDuckDBResourceFile() {
return duckDBResourceFile;
}

// We use zero-length ByteBuffer-s as a hacky but cheap way to pass C++ pointers
// back and forth

Expand All @@ -76,6 +86,8 @@ protected static native ByteBuffer duckdb_jdbc_startup(byte[] path, boolean read
// returns conn_ref connection reference object
protected static native ByteBuffer duckdb_jdbc_connect(ByteBuffer db_ref) throws SQLException;

protected static native String duckdb_jdbc_db_memory_address(ByteBuffer conn_ref);

protected static native void duckdb_jdbc_set_auto_commit(ByteBuffer conn_ref, boolean auto_commit)
throws SQLException;

Expand Down
Loading
Loading