Changeset: 7e0d71a22677 for monetdb-java URL: http://dev.monetdb.org/hg/monetdb-java?cmd=changeset;node=7e0d71a22677 Added Files: src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedConnection.java src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedInstance.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/EmbeddedQueryResult.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/BigintColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/BlobColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/BooleanColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/CharColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/ClobColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/Column.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/DateColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/DecimalColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/DoubleColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/GeometryColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/HugeintColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/InetColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/IntColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/JSONColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/MonthIntervalColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/RealColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/SecondIntervalColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/SmallintColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/TimeColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/TimestampColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/TinyintColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/URLColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/UUIDColumn.java src/main/java/nl/cwi/monetdb/mcl/embedded/result/column/VarcharColumn.java Modified Files: src/main/java/nl/cwi/monetdb/jdbc/MonetBlob.java src/main/java/nl/cwi/monetdb/jdbc/MonetClob.java src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java src/main/java/nl/cwi/monetdb/mcl/net/MapiSocket.java Branch: embedded Log Message:
Moved the embedded stuff for this repository diffs (truncated from 1704 to 300 lines): diff --git a/src/main/java/nl/cwi/monetdb/jdbc/MonetBlob.java b/src/main/java/nl/cwi/monetdb/jdbc/MonetBlob.java --- a/src/main/java/nl/cwi/monetdb/jdbc/MonetBlob.java +++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetBlob.java @@ -25,7 +25,7 @@ import java.io.*; public class MonetBlob implements Blob { private byte[] buf; - protected MonetBlob(byte[] data) { + public MonetBlob(byte[] data) { buf = data; } diff --git a/src/main/java/nl/cwi/monetdb/jdbc/MonetClob.java b/src/main/java/nl/cwi/monetdb/jdbc/MonetClob.java --- a/src/main/java/nl/cwi/monetdb/jdbc/MonetClob.java +++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetClob.java @@ -26,7 +26,7 @@ public class MonetClob implements Clob { private StringBuilder buf; - protected MonetClob(String in) { + public MonetClob(String in) { buf = new StringBuilder(in); } diff --git a/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java b/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java --- a/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java +++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetConnection.java @@ -2354,7 +2354,7 @@ public class MonetConnection extends Mon * Internal executor of queries. * * @param templ the template to fill in - * @param the query to execute + * @param query the query to execute * @throws SQLException if a database error occurs */ @SuppressWarnings("fallthrough") @@ -2655,7 +2655,7 @@ public class MonetConnection extends Mon * Constructor which immediately starts this thread and sets it * into daemon mode. * - * @param monet the socket to write to + * @param out the socket to write to */ public SendThread(BufferedMCLWriter out) { super("SendThread"); diff --git a/src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java b/src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java --- a/src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java +++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetDatabaseMetaData.java @@ -17,8 +17,6 @@ import java.sql.ResultSetMetaData; import java.sql.RowIdLifetime; import java.sql.Types; -import java.util.ArrayList; - /** * A DatabaseMetaData object suitable for the MonetDB database. * diff --git a/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java b/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java --- a/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java +++ b/src/main/java/nl/cwi/monetdb/jdbc/MonetResultSet.java @@ -148,7 +148,7 @@ public class MonetResultSet extends Mone * @param columns the column names * @param types the column types * @param results the number of rows in the ResultSet - * @throws IOException if communicating with monet failed + * @throws IllegalArgumentException if communicating with monet failed * @throws SQLException is a protocol error occurs */ MonetResultSet( @@ -537,7 +537,7 @@ public class MonetResultSet extends Mone * of this ResultSet object as a Blob object in the Java programming * language. * - * @param i the first column is 1, the second is 2, ... + * @param columnIndex the first column is 1, the second is 2, ... * @return a Blob object representing the SQL BLOB value in the * specified column * @throws SQLException if a database access error occurs @@ -578,7 +578,7 @@ public class MonetResultSet extends Mone * of this ResultSet object as a Clob object in the * Java programming language. * - * @param i the first column is 1, the second is 2, ... + * @param columnIndex the first column is 1, the second is 2, ... * @return a Clob object representing the SQL CLOB value in the * specified column * @throws SQLException if a database access error occurs @@ -1977,7 +1977,7 @@ public class MonetResultSet extends Mone * structured values are not supported, and distinct values are mapped to the * default Java class as determined by the underlying SQL type of the DISTINCT type. * - * @param i the first column is 1, the second is 2, ... + * @param columnIndex the first column is 1, the second is 2, ... * @param map a java.util.Map object that contains the mapping from SQL * type names to classes in the Java programming language * @return an Object in the Java programming language representing the SQL @@ -2586,7 +2586,7 @@ public class MonetResultSet extends Mone * The dates are parsed with the given Calendar. * * @param cal the Calendar to use/fill when parsing the date/time - * @param col the column to parse + * @param columnIndex the column to parse * @param type the corresponding java.sql.Types type of the calling * function * @return the fractional seconds (nanos) or -1 if the value is NULL diff --git a/src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedConnection.java b/src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedConnection.java new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedConnection.java @@ -0,0 +1,58 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2008-2015 MonetDB B.V. + */ + +package nl.cwi.monetdb.mcl.embedded; + +import java.io.*; +import java.sql.SQLException; + +import nl.cwi.monetdb.mcl.embedded.result.EmbeddedQueryResult; + +/** + * A single connection to a MonetDB database instance + * Communication between Java and native C is done via JNI. + * <br/> + * <strong>Note</strong>: You can have only one nl.cwi.monetdb.embedded MonetDB database running per JVM process. + */ +public class MonetDBEmbeddedConnection { + + private final long connectionPointer; + + public MonetDBEmbeddedConnection(long connectionPointer) { + this.connectionPointer = connectionPointer; + } + + protected long getConnectionPointer() { + return connectionPointer; + } + + /** + * Execute an SQL query in an nl.cwi.monetdb.embedded database. + * + * @param query The SQL query string + * @return The query result object, {@code null} if the database is not running + * @throws SQLException + */ + public EmbeddedQueryResult createQuery(String query) throws SQLException, IOException { + String queryString = query; + if (!queryString.endsWith(";")) { + queryString += ";"; + } + return queryWrapper(queryString, true, this.connectionPointer); + } + + /** + * Execute an SQL query in an nl.cwi.monetdb.embedded database. + * + * @param query The SQL query string + * @return The query result object, {@code null} if the database is not running + * @throws SQLException + */ + private native EmbeddedQueryResult queryWrapper(String query, boolean execute, long connectionPointer) throws SQLException, IOException; + +} diff --git a/src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedInstance.java b/src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedInstance.java new file mode 100644 --- /dev/null +++ b/src/main/java/nl/cwi/monetdb/mcl/embedded/MonetDBEmbeddedInstance.java @@ -0,0 +1,177 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * Copyright 2008-2015 MonetDB B.V. + */ + +package nl.cwi.monetdb.mcl.embedded; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Embedded version of MonetDB. + * Communication between Java and native C is done via JNI. + * <br/> + * <strong>Note</strong>: You can have only one nl.cwi.monetdb.embedded MonetDB database running per JVM process. + */ +public class MonetDBEmbeddedInstance { + + private final static String NATIVE_LIB_PATH_IN_JAR = "src" + File.separatorChar + "main" + + File.separatorChar + "resources"; + private final static String NATIVE_LIB_NAME = "libmonetdb5.so"; + + /** + * The native nl.cwi.monetdb.embedded MonetDB library. + */ + static { + try { + // Try load the nl.cwi.monetdb.embedded library + System.loadLibrary("monetdb5"); + } catch (UnsatisfiedLinkError e) { + // Still no, then get the resources.lib bundled in the jar + loadLibFromJar(NATIVE_LIB_NAME); + } + } + + private static void loadLibFromJar(String fileName) { + String pathToLib = NATIVE_LIB_PATH_IN_JAR + File.separatorChar + fileName; + try { + InputStream in = MonetDBEmbeddedInstance.class.getResourceAsStream(File.separatorChar + pathToLib); + if (in == null) { + // OK, the input stream is null, hence no .jar + // This was probably a test and/or in an IDE + // Just read the files from the src/main/resources dir + in = new FileInputStream(new File(pathToLib)); + } + // Set a temp location to extract (and load from later) + final Path tempLibsDir = Files.createTempDirectory("nl.cwi.monetdb.embedded"); + File fileOut = new File(tempLibsDir.toString() + File.separatorChar + fileName); + try (OutputStream out = new FileOutputStream(fileOut)) { + byte[] buffer = new byte[in.available()]; + while (in.read(buffer) != -1) { + out.write(buffer); + } + out.flush(); + in.close(); + // Load the resources.lib from the extracted file + System.load(fileOut.toString()); + } + } catch (IOException e) { + throw new UnsatisfiedLinkError("Unable to extract native library from JAR:" + e.getMessage()); + } + } + + public static native MonetDBEmbeddedInstance StartDatabase(String dbDirectory, boolean silentFlag, boolean sequentialFlag) throws SQLException; + + private final File databaseDirectory; + + private final boolean silentFlag; + + private final boolean sequentialFlag; + + private boolean isRunning; + + private final List<MonetDBEmbeddedConnection> connections = new ArrayList<>(); + + private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + + public MonetDBEmbeddedInstance(String dbDirectory, boolean silentFlag, boolean sequentialFlag, boolean isRunning) { + this.databaseDirectory = new File(dbDirectory); + this.silentFlag = silentFlag; + this.sequentialFlag = sequentialFlag; + this.isRunning = isRunning; + } + + public File getDatabaseDirectory() { + return databaseDirectory; + } + + public boolean isSilentFlagSet() { + return silentFlag; + } + + public boolean isSequentialFlagSet() { + return sequentialFlag; + } + + public boolean isRunning() { + boolean result; + lock.readLock().lock(); + result = isRunning; + lock.readLock().unlock(); + return result; + } + + private void setRunning(boolean running) { + lock.writeLock().lock(); + isRunning = running; + lock.writeLock().unlock(); + } + + public void stopDatabase() throws SQLException { + lock.writeLock().lock(); + try { + if(this.isRunning) { + for(MonetDBEmbeddedConnection mdbec : connections) { + this.shutdownConnection(mdbec); + } + this.connections.clear(); + this.stopDatabaseInternal(); _______________________________________________ checkin-list mailing list checkin-list@monetdb.org https://www.monetdb.org/mailman/listinfo/checkin-list