Skip to content
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

Expose db memory address and path to libduckdb_java.so V2 #115

Open
wants to merge 3 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
6 changes: 6 additions & 0 deletions src/jni/duckdb_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ struct ConnectionHolder {
}
};

// Returns the pointer's memory address of duckdb::DuckDB as a jlong
jlong _duckdb_jdbc_db_memory_address(JNIEnv *env, jclass, jobject conn_ref_buf) {
auto conn_ref = (ConnectionHolder *)env->GetDirectBufferAddress(conn_ref_buf);
return (jlong)conn_ref->db.get();
}

/**
* 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 jlong 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 -1;
}
}

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);

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

JNIEXPORT jlong 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
22 changes: 22 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,26 @@ 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 Java long. 0 indicates no open connection.
*/
public long getDuckDBMemoryAddress() {
if (this.conn_ref != null) {
return DuckDBNative.duckdb_jdbc_db_memory_address(this.conn_ref);
}
return 0;
}

/**
* <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();
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/duckdb/DuckDBDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,8 @@ public ResultSet getCatalogs() throws SQLException {
public ResultSet getSchemas() throws SQLException {
Statement statement = conn.createStatement();
statement.closeOnCompletion();
return statement.executeQuery(
"SELECT schema_name AS 'TABLE_SCHEM', catalog_name AS 'TABLE_CATALOG' FROM information_schema.schemata ORDER BY \"TABLE_CATALOG\", \"TABLE_SCHEM\"");
return statement.executeQuery("SELECT schema_name AS 'TABLE_SCHEM', catalog_name AS 'TABLE_CATALOG' FROM "
+ "information_schema.schemata ORDER BY \"TABLE_CATALOG\", \"TABLE_SCHEM\"");
}

@Override
Expand Down
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 long 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
4 changes: 2 additions & 2 deletions src/main/java/org/duckdb/DuckDBPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ public int executeUpdate() throws SQLException {
requireNonBatch();
execute();
if (!(returnsChangedRows || returnsNothing)) {
throw new SQLException(
"executeUpdate() can only be used with queries that return nothing (eg, a DDL statement), or update rows");
throw new SQLException("executeUpdate() can only be used with queries that return nothing (eg, a DDL "
+ "statement), or update rows");
}
return getUpdateCountInternal();
}
Expand Down
Loading
Loading