Skip to content
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
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ set(DUCKDB_SRC_FILES
src/duckdb/extension/json/json_serializer.cpp
src/duckdb/ub_extension_json_json_functions.cpp)

set(JEMACLLOC_SRC_FILES
set(JEMALLOC_SRC_FILES
src/duckdb/extension/jemalloc/jemalloc_extension.cpp
src/duckdb/extension/jemalloc/jemalloc/src/jemalloc.c
src/duckdb/extension/jemalloc/jemalloc/src/arena.c
Expand Down Expand Up @@ -553,7 +553,7 @@ add_jar(duckdb_jdbc_tests ${JAVA_TEST_FILES} INCLUDE_JARS duckdb_jdbc)
if(MSVC)
list(APPEND DUCKDB_SRC_FILES duckdb_java.def)
else()
list(APPEND DUCKDB_SRC_FILES ${JEMACLLOC_SRC_FILES})
list(APPEND DUCKDB_SRC_FILES ${JEMALLOC_SRC_FILES})
endif()

add_library(duckdb_java SHARED
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ set(DUCKDB_DEFINITIONS
set(DUCKDB_SRC_FILES
${SOURCES})

set(JEMACLLOC_SRC_FILES
set(JEMALLOC_SRC_FILES
${JEMALLOC_SOURCES})


Expand Down Expand Up @@ -95,7 +95,7 @@ add_jar(duckdb_jdbc_tests ${JAVA_TEST_FILES} INCLUDE_JARS duckdb_jdbc)
if(MSVC)
list(APPEND DUCKDB_SRC_FILES duckdb_java.def)
else()
list(APPEND DUCKDB_SRC_FILES ${JEMACLLOC_SRC_FILES})
list(APPEND DUCKDB_SRC_FILES ${JEMALLOC_SRC_FILES})
endif()

add_library(duckdb_java SHARED
Expand Down
45 changes: 2 additions & 43 deletions src/jni/duckdb_java.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "duckdb/main/extension_util.hpp"
#include "duckdb/parser/parsed_data/create_type_info.hpp"
#include "functions.hpp"
#include "holders.hpp"
#include "refs.hpp"
#include "types.hpp"
#include "util.hpp"
Expand Down Expand Up @@ -59,40 +60,6 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
delete_global_refs(env);
}

/**
* Associates a duckdb::Connection with a duckdb::DuckDB. The DB may be shared amongst many ConnectionHolders, but the
* Connection is unique to this holder. Every Java DuckDBConnection has exactly 1 of these holders, and they are never
* shared. The holder is freed when the DuckDBConnection is closed. When the last holder sharing a DuckDB is freed, the
* DuckDB is released as well.
*/
struct ConnectionHolder {
const duckdb::shared_ptr<duckdb::DuckDB> db;
const duckdb::unique_ptr<duckdb::Connection> connection;

ConnectionHolder(duckdb::shared_ptr<duckdb::DuckDB> _db)
: db(_db), connection(make_uniq<duckdb::Connection>(*_db)) {
}
};

/**
* Throws a SQLException and returns nullptr if a valid Connection can't be retrieved from the buffer.
*/
static Connection *get_connection(JNIEnv *env, jobject conn_ref_buf) {
if (!conn_ref_buf) {
throw ConnectionException("Invalid connection");
}
auto conn_holder = (ConnectionHolder *)env->GetDirectBufferAddress(conn_ref_buf);
if (!conn_holder) {
throw ConnectionException("Invalid connection");
}
auto conn_ref = conn_holder->connection.get();
if (!conn_ref || !conn_ref->context) {
throw ConnectionException("Invalid connection");
}

return conn_ref;
}

//! The database instance cache, used so that multiple connections to the same file point to the same database object
duckdb::DBInstanceCache instance_cache;

Expand Down Expand Up @@ -189,10 +156,6 @@ void _duckdb_jdbc_disconnect(JNIEnv *env, jclass, jobject conn_ref_buf) {
}
}

struct StatementHolder {
duckdb::unique_ptr<PreparedStatement> stmt;
};

#include "utf8proc_wrapper.hpp"

jobject _duckdb_jdbc_prepare(JNIEnv *env, jclass, jobject conn_ref_buf, jbyteArray query_j) {
Expand Down Expand Up @@ -233,11 +196,6 @@ jobject _duckdb_jdbc_prepare(JNIEnv *env, jclass, jobject conn_ref_buf, jbyteArr
return env->NewDirectByteBuffer(stmt_ref, 0);
}

struct ResultHolder {
duckdb::unique_ptr<QueryResult> res;
duckdb::unique_ptr<DataChunk> chunk;
};

Value ToValue(JNIEnv *env, jobject param, duckdb::shared_ptr<ClientContext> context) {
param = env->CallStaticObjectMethod(J_Timestamp, J_Timestamp_valueOf, param);

Expand Down Expand Up @@ -930,6 +888,7 @@ static ProfilerPrintFormat GetProfilerPrintFormat(JNIEnv *env, jobject format) {
if (env->IsSameObject(format, J_ProfilerPrintFormat_GRAPHVIZ)) {
return ProfilerPrintFormat::GRAPHVIZ;
}
throw InvalidInputException("Invalid profiling format");
}

jstring _duckdb_jdbc_get_profiling_information(JNIEnv *env, jclass, jobject conn_ref_buf, jobject j_format) {
Expand Down
1 change: 1 addition & 0 deletions src/jni/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,5 +404,6 @@ JNIEXPORT jstring JNICALL Java_org_duckdb_DuckDBNative_duckdb_1jdbc_1get_1profil
duckdb::ErrorData error(e);
ThrowJNI(env, error.Message().c_str());

return nullptr;
}
}
48 changes: 48 additions & 0 deletions src/jni/holders.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include "duckdb.hpp"

#include <jni.h>

/**
* Associates a duckdb::Connection with a duckdb::DuckDB. The DB may be shared amongst many ConnectionHolders, but the
* Connection is unique to this holder. Every Java DuckDBConnection has exactly 1 of these holders, and they are never
* shared. The holder is freed when the DuckDBConnection is closed. When the last holder sharing a DuckDB is freed, the
* DuckDB is released as well.
*/
struct ConnectionHolder {
const duckdb::shared_ptr<duckdb::DuckDB> db;
const duckdb::unique_ptr<duckdb::Connection> connection;

ConnectionHolder(duckdb::shared_ptr<duckdb::DuckDB> _db)
: db(_db), connection(duckdb::make_uniq<duckdb::Connection>(*_db)) {
}
};

struct StatementHolder {
duckdb::unique_ptr<duckdb::PreparedStatement> stmt;
};

struct ResultHolder {
duckdb::unique_ptr<duckdb::QueryResult> res;
duckdb::unique_ptr<duckdb::DataChunk> chunk;
};

/**
* Throws a SQLException and returns nullptr if a valid Connection can't be retrieved from the buffer.
*/
inline duckdb::Connection *get_connection(JNIEnv *env, jobject conn_ref_buf) {
if (!conn_ref_buf) {
throw duckdb::ConnectionException("Invalid connection");
}
auto conn_holder = (ConnectionHolder *)env->GetDirectBufferAddress(conn_ref_buf);
if (!conn_holder) {
throw duckdb::ConnectionException("Invalid connection");
}
auto conn_ref = conn_holder->connection.get();
if (!conn_ref || !conn_ref->context) {
throw duckdb::ConnectionException("Invalid connection");
}

return conn_ref;
}
2 changes: 1 addition & 1 deletion src/main/java/org/duckdb/DuckDBAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public DuckDBAppender(DuckDBConnection con, String schemaName, String tableName)
throw new SQLException("Invalid connection");
}
appender_ref = DuckDBNative.duckdb_jdbc_create_appender(
con.conn_ref, schemaName.getBytes(StandardCharsets.UTF_8), tableName.getBytes(StandardCharsets.UTF_8));
con.connRef, schemaName.getBytes(StandardCharsets.UTF_8), tableName.getBytes(StandardCharsets.UTF_8));
}

public void beginRow() throws SQLException {
Expand Down
Loading
Loading