diff --git a/README.md b/README.md index 6c2c465..f9d1e06 100644 --- a/README.md +++ b/README.md @@ -1,355 +1,47 @@ -# LevelDB JNI +#NOTE +this repo is fork from [leveldbjni](https://github.com/fusesource/leveldbjni) -## Description +#Build +```shell +wget https://src.fedoraproject.org/lookaside/pkgs/snappy/snappy-1.0.5.tar.gz/4c0af044e654f5983f4acbf00d1ac236/snappy-1.0.5.tar.gz -LevelDB JNI gives you a Java interface to the -[LevelDB](https://github.com/google/leveldb) C++ library -which is a fast key-value storage library written at Google -that provides an ordered mapping from string keys to string values. +tar -zxvf snappy-1.0.5.tar.gz +git clone git@github.com:chirino/leveldb.git +git clone git@github.com:fusesource/leveldbjni.git -## Declaration -This LevelDB JNI is based on [leveldb-1.23](https://github.com/halibobor/leveldb/tree/leveldbjni/v1.23) and [snappy-1.1.9](https://github.com/halibobor/snappy/tree/leveldbjni/v1.1.9) . +export SNAPPY_HOME=`cd snappy-1.0.5; pwd` +export LEVELDB_HOME=`cd leveldb; pwd` +export LEVELDBJNI_HOME=`cd leveldbjni; pwd` -Maybe you need this [LevelDB JNI](https://github.com/fusesource/leveldbjni) ? +cd ${SNAPPY_HOME} +./configure --disable-shared --with-pic +make -### Strong Attention - -> NOTE: If you are using [org.fusesource.leveldbjni/leveldbjni-all/1.8](https://mvnrepository.com/artifact/org.fusesource.leveldbjni/leveldbjni-all/1.8) -> Here's what you need to know this jni: -> * ``suspendCompactions`` and ``resumeCompactions`` are no longer supported. -> * 1.23.1 +, iterator.prev() changed, see [change](https://github.com/halibobor/leveldbjni-all/commit/ca2b1b2575ce11b0c98c66a74520da56ad7a1eb1) - -## Getting the JAR - -Just add the following jar to your java project: -[leveldbjni-all-1.23.1.jar](https://repo1.maven.org/maven2/com/halibobor/leveldbjni-all/1.23.1/leveldbjni-all-1.23.1.jar) - -## Using the Dependency - -### gradle - -```gradle - dependencies { - implementation 'com.halibobor:leveldbjni-all:1.23.1' - } -``` - -### maven - -```maven - - com.halibobor - leveldbjni-all - 1.23.1 - -``` - - -## API Usage: - -Recommended Package imports: - -```java - import org.iq80.leveldb.*; - import static org.fusesource.leveldbjni.JniDBFactory.*; - import java.io.*; -``` - -Opening and closing the database. - -```java - Options options = new Options(); - options.createIfMissing(true); - DB db = factory.open(new File("example"), options); - try { - // Use the db in here.... - } finally { - // Make sure you close the db to shutdown the - // database and avoid resource leaks. - db.close(); - } -``` -Putting, Getting, and Deleting key/values. - -```java - db.put(bytes("Tampa"), bytes("rocks")); - String value = asString(db.get(bytes("Tampa"))); - db.delete(bytes("Tampa")); -``` - -Performing Batch/Bulk/Atomic Updates. - -```java - WriteBatch batch = db.createWriteBatch(); - try { - batch.delete(bytes("Denver")); - batch.put(bytes("Tampa"), bytes("green")); - batch.put(bytes("London"), bytes("red")); - - db.write(batch); - } finally { - // Make sure you close the batch to avoid resource leaks. - batch.close(); - } -``` - -Iterating key/values. - -```java - DBIterator iterator = db.iterator(); - try { - for(iterator.seekToFirst(); iterator.Valid(); iterator.next()) { - String key = asString(iterator.key()); - String value = asString(iterator.value()); - System.out.println(key+" = "+value); - } - } finally { - // Make sure you close the iterator to avoid resource leaks. - iterator.close(); - } - try { - for(iterator.seekToLast(); iterator.Valid(); iterator.prev()) { - String key = asString(iterator.key()); - String value = asString(iterator.value()); - System.out.println(key+" = "+value); - } - } finally { - // Make sure you close the iterator to avoid resource leaks. - iterator.close(); - } -``` - -Working against a Snapshot view of the Database. - -```java - ReadOptions ro = new ReadOptions(); - ro.snapshot(db.getSnapshot()); - try { - - // All read operations will now use the same - // consistent view of the data. - ... = db.iterator(ro); - ... = db.get(bytes("Tampa"), ro); - - } finally { - // Make sure you close the snapshot to avoid resource leaks. - ro.snapshot().close(); - } -``` - -Using a custom Comparator. - -```java - DBComparator comparator = new DBComparator(){ - public int compare(byte[] key1, byte[] key2) { - return new String(key1).compareTo(new String(key2)); - } - public String name() { - return "simple"; - } - public byte[] findShortestSeparator(byte[] start, byte[] limit) { - return start; - } - public byte[] findShortSuccessor(byte[] key) { - return key; - } - }; - Options options = new Options(); - options.comparator(comparator); - DB db = factory.open(new File("example"), options); -``` -Disabling Compression. - -```java - Options options = new Options(); - options.compressionType(CompressionType.NONE); - DB db = factory.open(new File("example"), options); -``` - -Configuring the Cache. - -```java - Options options = new Options(); - options.cacheSize(100 * 1048576); // 100MB cache - DB db = factory.open(new File("example"), options); -``` - -Configuring the BloomFilter. - -```java - Options options = new Options(); - options.bitsPerKey(10); - DB db = factory.open(new File("example"), options); +cd ${LEVELDB_HOME} +export LIBRARY_PATH=${SNAPPY_HOME} +export C_INCLUDE_PATH=${LIBRARY_PATH} +export CPLUS_INCLUDE_PATH=${LIBRARY_PATH} +git apply ../leveldbjni/leveldb.patch +make libleveldb.a ``` -Getting approximate sizes. - -```java - long[] sizes = db.getApproximateSizes(new Range(bytes("a"), bytes("k")), new Range(bytes("k"), bytes("z"))); - System.out.println("Size: "+sizes[0]+", "+sizes[1]); -``` -Getting database status. - -```java - String stats = db.getProperty("leveldb.stats"); - System.out.println(stats); -``` - -Getting database approximate-memory-usage. - -```java - String approximate = db.getProperty("leveldb.approximate-memory-usage"); - System.out.println(approximate); -``` - - -Getting informational log messages. - -```java - Logger logger = new Logger() { - public void log(String message) { - System.out.println(message); - } - }; - Options options = new Options(); - options.logger(logger); - DB db = factory.open(new File("example"), options); -``` - -Destroying a database. - -```java - Options options = new Options(); - factory.destroy(new File("example"), options); -``` - -Repairing a database. - -```java - Options options = new Options(); - factory.repair(new File("example"), options); -``` - -Using a memory pool to make native memory allocations more efficient: - -```java - JniDBFactory.pushMemoryPool(1024 * 512); - try { - // .. work with the DB in here, - } finally { - JniDBFactory.popMemoryPool(); - } -``` - -## Building -> The following Steps work on mac, linux well, other platforms are not verified yet. -> -> Welcome to contribute to other platforms. - - ### Supported Platforms - - The following worked for me on: - - * OS X Mojave with X Code 11 (x86_64) - * OS X Monterey with X Code 13 (aarch64) - * CentOS (x86_64 | i386 | aarch64) - - -### Prerequisites -* GNU compiler toolchain,gcc-c++ -* openssl, openssl-devel(cmake required) -* JAVA(1.8+),make sure the current session $JAVA_HOME is set. -* [cmake3.1+](https://cmake.org/download/) -* git -* maven3 -* automake -* [autoconf](https://github.com/asdf-vm/asdf-erlang/issues/195#issuecomment-815999279) -* pkg-configls,build-essential,libtool (linux maybe required) - - -### Build Procedure -Then download the snappy, leveldb, leveldbjni,leveldbjni-all project source code. -```shell script -git clone https://github.com/halibobor/leveldbjni-all.git -cd leveldbjni-all -git submodule update --init --recursive -cd third_party -``` - -#### 1. snappy +#Special for M1 ```shell script - cd snappy - mkdir build && cd build - cmake -DCMAKE_BUILD_TYPE=Release ../ - make && make DESTDIR=/tmp install - cd ../../ +brew install maven +set $JAVA_HOME +update pom.xml + 1. 1.5 -> 1.8 + 2. add java_home, remove with-universal + --with-jni-jdk=${env.JAVA_HOME} + --with-leveldb=${env.LEVELDB_HOME} + --with-snappy=${env.SNAPPY_HOME} + ``` -#### 2. leveldb -```shell script - cd leveldb - export LIBRARY_PATH=/tmp/usr/local/lib - # export LIBRARY_PATH=/tmp/usr/local/lib64 - export CPLUS_INCLUDE_PATH=/tmp/usr/local/include - mkdir build && cd build - cmake -DCMAKE_BUILD_TYPE=Release .. && cmake --build . - ./db_test - ./db_bench - make DESTDIR=/tmp install - cd ../../ -``` - -#### 3. leveldbjni -```shell script - cd leveldbjni - export SNAPPY_HOME=/tmp/usr/local - export LEVELDB_HOME=/tmp/usr/local - mvn clean package -P ${platform} -``` -Replace ${platform} with one of the following platform identifiers (depending on the platform your building on): - -* osx -* linux32 -* linux64 -* linux64-aarch64 -* linux64-ppc64le -* linux64-sunos64-amd64 -* linux64-sunos64-sparcv9 -* win32 -* win64 -* freebsd64 - -##### Build Results - -* `leveldbjni-${platform}/target/native-build/target/lib/leveldbjni-${version}.[jnilib|so|dll]` : native library using your currently platform. -* `leveldbjni/target/leveldbjni-${version}-native-src.zip` : A GNU style source project which you can use to build the native library on other systems. -* `leveldbjni-${platform}/target/leveldbjni-${platform}-${version}.jar` : A jar file containing the built native library using your currently platform. - -#### 4. leveldbjni-all -```shell script - git clone https://github.com/halibobor/leveldbjni-all.git - cd leveldbjni-all - cd src/main/resources/META-INF/ - # mkdir for native library - mkdir -p ${os}${bit-model}/${arch} - # cp native library to ${os}${bit-model}/${arch} - cp ${native_library_path}/leveldbjni-1.23.jnilib ${os}${bit-model}/${arch} - # update pom.xml - # add new config to Bundle-NativeCode - # such as META-INF/native/osx64/aarch64/libleveldbjni.jnilib;osname=macosx;processor=aarch64 - mvn clean install +#Build jni +```shell + mvn clean install -P download -P osx ``` - -> ${os},${bit-model},${arch} values, such as osx,64,x86_64. -> -> ${platform}=${os}${bit-model},such as osx64. -> -> Please refer to [Library.java](https://github.com/fusesource/hawtjni/blob/master/hawtjni-runtime/src/main/java/org/fusesource/hawtjni/runtime/Library.java) to get more. - - -### Build Results - -* `leveldbjni/target/leveldbjni-all-${version}.jar` : The java class file to the library. \ No newline at end of file diff --git a/pom.xml b/pom.xml index ae6fb5f..1044ecf 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,7 @@ com.halibobor leveldbjni-all - 1.23.2 + 1.18.1 bundle @@ -49,7 +49,8 @@ LEVELDBJNI UTF-8 1.18 - 1.23.2 + 0.6 + 1.8 4.13.2 0.8.8 0xEF7F2D6C @@ -108,9 +109,15 @@ ${hawtjni-version} - com.halibobor - leveldb-api - ${leveldb-api-version} + org.fusesource.leveldbjni + leveldbjni + ${leveldbjni-version} + + + org.fusesource.hawtjni + hawtjni-runtime + + junit @@ -225,14 +232,13 @@ true - - - META-INF/native/osx64/x86_64/libleveldbjni.jnilib;osname=macosx;processor=x86-64, + META-INF/native/windows32/leveldbjni.dll;osname=Win32;processor=x86, + META-INF/native/windows64/leveldbjni.dll;osname=Win32;processor=x86-64, + META-INF/native/osx/libleveldbjni.jnilib;osname=macosx;processor=x86, + META-INF/native/osx/libleveldbjni.jnilib;osname=macosx;processor=x86-64, META-INF/native/osx64/aarch64/libleveldbjni.jnilib;osname=macosx;processor=aarch64, META-INF/native/linux32/libleveldbjni.so;osname=Linux;processor=x86, META-INF/native/linux64/libleveldbjni.so;osname=Linux;processor=x86-64, - META-INF/native/linux64/aarch64/libleveldbjni.so;osname=Linux;processor=aarch64 diff --git a/src/main/java/org/fusesource/leveldbjni/JniDBFactory.java b/src/main/java/org/fusesource/leveldbjni/JniDBFactory.java deleted file mode 100644 index eba00b1..0000000 --- a/src/main/java/org/fusesource/leveldbjni/JniDBFactory.java +++ /dev/null @@ -1,225 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni; - -import java.io.BufferedReader; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import org.fusesource.leveldbjni.internal.JniDB; -import org.fusesource.leveldbjni.internal.NativeBuffer; -import org.fusesource.leveldbjni.internal.NativeCache; -import org.fusesource.leveldbjni.internal.NativeComparator; -import org.fusesource.leveldbjni.internal.NativeCompressionType; -import org.fusesource.leveldbjni.internal.NativeDB; -import org.fusesource.leveldbjni.internal.NativeFilter; -import org.fusesource.leveldbjni.internal.NativeLogger; -import org.fusesource.leveldbjni.internal.NativeOptions; -import org.iq80.leveldb.DB; -import org.iq80.leveldb.DBComparator; -import org.iq80.leveldb.DBFactory; -import org.iq80.leveldb.Logger; -import org.iq80.leveldb.Options; - -/** - * @author Hiram Chirino - */ -public class JniDBFactory implements DBFactory { - - public static final JniDBFactory factory = new JniDBFactory(); - public static final String VERSION; - - static { - NativeDB.LIBRARY.load(); - } - - static { - String v = "unknown"; - try (InputStream is = JniDBFactory.class.getResourceAsStream("version.txt")) { - v = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)).readLine(); - } catch (Throwable e) { - } - VERSION = v; - } - - public static byte[] bytes(String value) { - if (value == null) { - return null; - } - return value.getBytes(StandardCharsets.UTF_8); - } - - public static String asString(byte[] value) { - if (value == null) { - return null; - } - return new String(value, StandardCharsets.UTF_8); - } - - public static void pushMemoryPool(int size) { - NativeBuffer.pushMemoryPool(size); - } - - public static void popMemoryPool() { - NativeBuffer.popMemoryPool(); - } - - public DB open(File path, Options options) throws IOException { - NativeDB db = null; - OptionsResourceHolder holder = new OptionsResourceHolder(); - try { - holder.init(options); - db = NativeDB.open(holder.options, path); - } finally { - // if we could not open up the DB, then clean up the - // other allocated native resouces.. - if (db == null) { - holder.close(); - } - } - return new JniDB(db, holder.cache, holder.filter, holder.comparator, holder.logger); - } - - public void destroy(File path, Options options) throws IOException { - OptionsResourceHolder holder = new OptionsResourceHolder(); - try { - holder.init(options); - NativeDB.destroy(path, holder.options); - } finally { - holder.close(); - } - } - - public void repair(File path, Options options) throws IOException { - OptionsResourceHolder holder = new OptionsResourceHolder(); - try { - holder.init(options); - NativeDB.repair(path, holder.options); - } finally { - holder.close(); - } - } - - @Override - public String toString() { - return String.format("leveldbjni version %s", VERSION); - } - - private static class OptionsResourceHolder { - - NativeCache cache = null; - NativeFilter filter = null; - NativeComparator comparator = null; - NativeLogger logger = null; - NativeOptions options; - - public void init(Options value) { - - options = new NativeOptions(); - options.blockRestartInterval(value.blockRestartInterval()); - options.blockSize(value.blockSize()); - options.createIfMissing(value.createIfMissing()); - options.errorIfExists(value.errorIfExists()); - options.maxOpenFiles(value.maxOpenFiles()); - options.paranoidChecks(value.paranoidChecks()); - options.writeBufferSize(value.writeBufferSize()); - options.reuseLogs(value.reuseLogs()); - options.maxFileSize(value.maxFileSize()); - - switch (value.compressionType()) { - case NONE: - options.compression(NativeCompressionType.kNoCompression); - break; - case SNAPPY: - options.compression(NativeCompressionType.kSnappyCompression); - break; - } - - - if (value.cacheSize() > 0) { - cache = new NativeCache(value.cacheSize()); - options.cache(cache); - } - - if (value.bitsPerKey() > 0) { - filter = new NativeFilter(value.bitsPerKey()); - options.filter(filter); - } - - final DBComparator userComparator = value.comparator(); - if (userComparator != null) { - comparator = new NativeComparator() { - @Override - public int compare(byte[] key1, byte[] key2) { - return userComparator.compare(key1, key2); - } - - @Override - public String name() { - return userComparator.name(); - } - }; - options.comparator(comparator); - } - - final Logger userLogger = value.logger(); - if (userLogger != null) { - logger = new NativeLogger() { - @Override - public void log(String message) { - userLogger.log(message); - } - }; - options.infoLog(logger); - } - - } - - public void close() { - if (cache != null) { - cache.delete(); - } - if (filter != null) { - filter.delete(); - } - if (comparator != null) { - comparator.delete(); - } - if (logger != null) { - logger.delete(); - } - } - } -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/JniDB.java b/src/main/java/org/fusesource/leveldbjni/internal/JniDB.java deleted file mode 100644 index 0b58d5b..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/JniDB.java +++ /dev/null @@ -1,295 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import org.iq80.leveldb.DB; -import org.iq80.leveldb.DBException; -import org.iq80.leveldb.DBIterator; -import org.iq80.leveldb.Range; -import org.iq80.leveldb.ReadOptions; -import org.iq80.leveldb.Snapshot; -import org.iq80.leveldb.WriteBatch; -import org.iq80.leveldb.WriteOptions; - -/** - * @author Hiram Chirino - */ -public class JniDB implements DB { - - private NativeDB db; - private NativeCache cache; - private NativeFilter filter; - private NativeComparator comparator; - private NativeLogger logger; - - public JniDB(NativeDB db, NativeCache cache, NativeFilter filter, NativeComparator comparator, - NativeLogger logger) { - this.db = db; - this.cache = cache; - this.filter = filter; - this.comparator = comparator; - this.logger = logger; - } - - public void close() { - if (db != null) { - db.delete(); - db = null; - if (cache != null) { - cache.delete(); - cache = null; - } - if (filter != null) { - filter.delete(); - filter = null; - } - if (comparator != null) { - comparator.delete(); - comparator = null; - } - if (logger != null) { - logger.delete(); - logger = null; - } - } - } - - - public byte[] get(byte[] key) throws DBException { - if (db == null) { - throw new DBException("Closed"); - } - return get(key, new ReadOptions()); - } - - public byte[] get(byte[] key, ReadOptions options) throws DBException { - if (db == null) { - throw new DBException("Closed"); - } - try { - return db.get(convert(options), key); - } catch (NativeDB.DBException e) { - if (e.isNotFound()) { - return null; - } - throw new DBException(e.getMessage(), e); - } - } - - public DBIterator iterator() { - return iterator(new ReadOptions()); - } - - public DBIterator iterator(ReadOptions options) { - if (db == null) { - throw new DBException("Closed"); - } - return new JniDBIterator(db.iterator(convert(options))); - } - - public void put(byte[] key, byte[] value) throws DBException { - put(key, value, new WriteOptions()); - } - - public Snapshot put(byte[] key, byte[] value, WriteOptions options) throws DBException { - if (db == null) { - throw new DBException("Closed"); - } - try { - db.put(convert(options), key, value); - return null; - } catch (NativeDB.DBException e) { - throw new DBException(e.getMessage(), e); - } - } - - public void delete(byte[] key) throws DBException { - delete(key, new WriteOptions()); - } - - public Snapshot delete(byte[] key, WriteOptions options) throws DBException { - if (db == null) { - throw new DBException("Closed"); - } - try { - db.delete(convert(options), key); - return null; - } catch (NativeDB.DBException e) { - throw new DBException(e.getMessage(), e); - } - } - - public void write(WriteBatch updates) throws DBException { - write(updates, new WriteOptions()); - } - - public Snapshot write(WriteBatch updates, WriteOptions options) throws DBException { - if (db == null) { - throw new DBException("Closed"); - } - try { - db.write(convert(options), ((JniWriteBatch) updates).writeBatch()); - return null; - } catch (NativeDB.DBException e) { - throw new DBException(e.getMessage(), e); - } - } - - public WriteBatch createWriteBatch() { - return new JniWriteBatch(new NativeWriteBatch()); - } - - public Snapshot getSnapshot() { - if (db == null) { - throw new DBException("Closed"); - } - return new JniSnapshot(db, db.getSnapshot()); - } - - public long[] getApproximateSizes(Range... ranges) { - if (db == null) { - throw new DBException("Closed"); - } - NativeRange[] args = new NativeRange[ranges.length]; - for (int i = 0; i < args.length; i++) { - args[i] = new NativeRange(ranges[i].start(), ranges[i].limit()); - } - return db.getApproximateSizes(args); - } - - public String getProperty(String name) { - if (db == null) { - throw new DBException("Closed"); - } - return db.getProperty(name); - } - - private NativeReadOptions convert(ReadOptions options) { - if (options == null) { - return null; - } - NativeReadOptions rc = new NativeReadOptions(); - rc.fillCache(options.fillCache()); - rc.verifyChecksums(options.verifyChecksums()); - if (options.snapshot() != null) { - rc.snapshot(((JniSnapshot) options.snapshot()).snapshot()); - } - return rc; - } - - private NativeWriteOptions convert(WriteOptions options) { - if (options == null) { - return null; - } - NativeWriteOptions rc = new NativeWriteOptions(); - rc.sync(options.sync()); - if (options.snapshot()) { - throw new UnsupportedOperationException("WriteOptions snapshot not supported"); - } - return rc; - } - - public void compactRange(byte[] begin, byte[] end) throws DBException { - if (db == null) { - throw new DBException("Closed"); - } - db.compactRange(begin, end); - } - - // - // Using a fork of leveldb with db Suspend / Resume methods to avoid - // having to callback into java. - // - public void suspendCompactions() throws InterruptedException { - /*if( db==null ) { - throw new DBException("Closed"); - } - db.suspendCompactions();*/ - throw new DBException("Not Implemented"); - } - - public void resumeCompactions() { - /*if( db==null ) { - throw new DBException("Closed"); - } - db.resumeCompactions();*/ - throw new DBException("Not Implemented"); - } - - // private static class Suspension { - // static long env = Util.EnvJNI.Default(); - // - // CountDownLatch suspended = new CountDownLatch(1); - // CountDownLatch resumed = new CountDownLatch(1); - // Callback callback = new Callback(this, "suspended", 1); - // - // public Suspension() { - // Util.EnvJNI.Schedule(env, callback.getAddress(), 0); - // } - // - // private long suspended(long arg) { - // suspended.countDown(); - // try { - // resumed.await(); - // } catch (InterruptedException e) { - // } finally { - // callback.dispose(); - // } - // return 0; - // } - // } - // - // int suspendCounter = 0; - // Suspension suspension = null; - // - // public void suspendCompactions() throws InterruptedException { - // Suspension s = null; - // synchronized (this) { - // suspendCounter++; - // if( suspendCounter==1 ) { - // suspension = new Suspension(); - // } - // s = suspension; - // } - // // Don't return until the compactions have suspended. - // s.suspended.await(); - // } - // - // synchronized public void resumeCompactions() { - // suspendCounter--; - // if( suspendCounter==0 ) { - // suspension.resumed.countDown(); - // suspension = null; - // } - // } -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/JniDBIterator.java b/src/main/java/org/fusesource/leveldbjni/internal/JniDBIterator.java index 58a1be8..4de1bac 100644 --- a/src/main/java/org/fusesource/leveldbjni/internal/JniDBIterator.java +++ b/src/main/java/org/fusesource/leveldbjni/internal/JniDBIterator.java @@ -6,7 +6,7 @@ * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: - * + * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above @@ -16,7 +16,7 @@ * * Neither the name of FuseSource Corp. nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR @@ -29,11 +29,9 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - package org.fusesource.leveldbjni.internal; import java.util.AbstractMap; -import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import org.iq80.leveldb.DBIterator; @@ -43,194 +41,111 @@ */ public class JniDBIterator implements DBIterator { - private final NativeIterator iterator; - - JniDBIterator(NativeIterator iterator) { - this.iterator = iterator; - } - - public void close() { - iterator.delete(); - } - - public void remove() { - throw new UnsupportedOperationException(); - } - - /** - * Position at the first key in the source that is at or past target. - * The iterator is Valid() after this call iff the source contains - * an entry that comes at or past target. - * If key larger than max key, Valid() return false. - * @param key target - */ - public void seek(byte[] key) { - try { - iterator.seek(key); - } catch (NativeDB.DBException e) { - if (e.isNotFound()) { - throw new NoSuchElementException(); - } else { - throw new RuntimeException(e); - } + private final NativeIterator iterator; + + JniDBIterator(NativeIterator iterator) { + this.iterator = iterator; } - } - - /** - * Seek to the last key that is less than or equal to the target key. - * { @link } - * @param key target - */ - public void seekForPrev(byte[] key) { - try { - iterator.seek(key); - if (!Valid()) { - iterator.seekToLast(); - } else if (!Arrays.equals(iterator.key(), key)) { - iterator.prev(); - } - } catch (NativeDB.DBException e) { - if (e.isNotFound()) { - throw new NoSuchElementException(); - } else { - throw new RuntimeException(e); - } + + public void close() { + iterator.delete(); + } + + public void remove() { + throw new UnsupportedOperationException(); + } + + public void seek(byte[] key) { + try { + iterator.seek(key); + } catch (NativeDB.DBException e) { + if( e.isNotFound() ) { + throw new NoSuchElementException(); + } else { + throw new RuntimeException(e); + } + } } - } - - /** - * Position at the first key in the source. The iterator is {@link #Valid()} - * after this call iff the source is not empty. - */ - public void seekToFirst() { - iterator.seekToFirst(); - } - - /** - * Position at the last key in the source. The iterator is {@link #Valid()} - * after this call iff the source is not empty. - */ - public void seekToLast() { - iterator.seekToLast(); - } - - /** - * An iterator is either positioned at a key/value pair, or - * not valid. This method returns true iff the iterator is valid. - */ - @Override - public boolean Valid() { - return iterator.isValid(); - } - - /** - * The returned slice is valid only until the next modification of - * the iterator. - * REQUIRES: {@link #Valid()} - * @return the key for the current entry - */ - @Override - public byte[] key() { - try { - return iterator.key(); - } catch (NativeDB.DBException e) { - throw new RuntimeException(e); + + public void seekToFirst() { + iterator.seekToFirst(); } - } - - /** - * The returned slice is valid only until the next modification of - * the iterator. - * REQUIRES: {@link #Valid()} - * @return the value for the current entry - */ - @Override - public byte[] value() { - try { - return iterator.value(); - } catch (NativeDB.DBException e) { - throw new RuntimeException(e); + + public void seekToLast() { + iterator.seekToLast(); } - } - - /** - * Keep same as {@link #peekPrev()}. - * @see #key() - * @see #value() - * @return the current entry - */ - @Deprecated - public Map.Entry peekNext() { - if (!iterator.isValid()) { - throw new NoSuchElementException(); + + + public Map.Entry peekNext() { + if(!iterator.isValid()) { + throw new NoSuchElementException(); + } + try { + return new AbstractMap.SimpleImmutableEntry(iterator.key(), iterator.value()); + } catch (NativeDB.DBException e) { + throw new RuntimeException(e); + } } - try { - return new AbstractMap.SimpleImmutableEntry<>(iterator.key(), iterator.value()); - } catch (NativeDB.DBException e) { - throw new RuntimeException(e); + + public boolean hasNext() { + return iterator.isValid(); } - } - - public boolean hasNext() { - return iterator.isValid(); - } - - /** - * Moves to the next entry in the source. After this call, {@link #Valid()} is - * true iff the iterator was not positioned at the last entry in the source. - * REQUIRES: {@link #Valid()} - * @return the current entry - */ - public Map.Entry next() { - Map.Entry rc = this.peekNext(); - try { - iterator.next(); - } catch (NativeDB.DBException e) { - throw new RuntimeException(e); + + public Map.Entry next() { + Map.Entry rc = this.peekNext(); + try { + iterator.next(); + } catch (NativeDB.DBException e) { + throw new RuntimeException(e); + } + return rc; } - return rc; - } - - /** - * Keep same as {@link #hasNext()}. - * Used in combination with {@link #seekToLast()},{@link #seekForPrev(byte[])},{@link #prev()}. - * @return An iterator is either positioned at a key/value pair - */ - public boolean hasPrev() { - return iterator.isValid(); - } - - /** - * Keep same as {@link #peekNext()}. - * @see #key() - * @see #value() - * @return the current entry - */ - @Deprecated - public Map.Entry peekPrev() { - if (!iterator.isValid()) { - throw new NoSuchElementException(); + + public boolean hasPrev() { + if( !iterator.isValid() ) { + return false; + } + try { + iterator.prev(); + try { + return iterator.isValid(); + } finally { + if (iterator.isValid()) { + iterator.next(); + } else { + iterator.seekToFirst(); + } + } + } catch (NativeDB.DBException e) { + throw new RuntimeException(e); + } } - try { - return new AbstractMap.SimpleImmutableEntry<>(iterator.key(), iterator.value()); - } catch (NativeDB.DBException e) { - throw new RuntimeException(e); + + public Map.Entry peekPrev() { + try { + try { + return this.prev(); + } finally { + if (iterator.isValid()) { + iterator.next(); + } else { + iterator.seekToFirst(); + } + } + } catch (NativeDB.DBException e) { + throw new RuntimeException(e); + } } - } - - /** - * Moves to the previous entry in the source. After this call, {@link #Valid()} is - * true iff the iterator was not positioned at the first entry in source. - * REQUIRES: {@link #Valid()} - * @return the current entry - */ - public Map.Entry prev() { - Map.Entry rc = this.peekPrev(); - try { - iterator.prev(); - } catch (NativeDB.DBException e) { - throw new RuntimeException(e); + + public Map.Entry prev() { + if(!iterator.isValid()) { + throw new NoSuchElementException(); + } + try { + iterator.prev(); + return this.peekNext(); + } catch (NativeDB.DBException e) { + throw new RuntimeException(e); + } } - return rc; - } } diff --git a/src/main/java/org/fusesource/leveldbjni/internal/JniSnapshot.java b/src/main/java/org/fusesource/leveldbjni/internal/JniSnapshot.java deleted file mode 100644 index 93b3138..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/JniSnapshot.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import org.iq80.leveldb.Snapshot; - -/** - * @author Hiram Chirino - */ -public class JniSnapshot implements Snapshot { - - private final NativeDB db; - private final NativeSnapshot snapshot; - - JniSnapshot(NativeDB db, NativeSnapshot snapshot) { - this.db = db; - this.snapshot = snapshot; - } - - public void close() { - db.releaseSnapshot(snapshot); - } - - NativeSnapshot snapshot() { - return snapshot; - } -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/JniWriteBatch.java b/src/main/java/org/fusesource/leveldbjni/internal/JniWriteBatch.java deleted file mode 100644 index 9fb9d03..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/JniWriteBatch.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import org.iq80.leveldb.WriteBatch; - -/** - * @author Hiram Chirino - */ -public class JniWriteBatch implements WriteBatch { - - private final NativeWriteBatch writeBatch; - - JniWriteBatch(NativeWriteBatch writeBatch) { - this.writeBatch = writeBatch; - } - - public void close() { - writeBatch.delete(); - } - - public WriteBatch put(byte[] key, byte[] value) { - writeBatch.put(key, value); - return this; - } - - public WriteBatch delete(byte[] key) { - writeBatch.delete(key); - return this; - } - - @Override - public WriteBatch clear() { - writeBatch.clear(); - return this; - } - - @Override - public long approximateSize() { - return writeBatch.approximateSize(); - } - - @Override - public WriteBatch append(WriteBatch source) { - if (source instanceof JniWriteBatch) { - writeBatch.append(((JniWriteBatch) source).writeBatch); - } else { - throw new UnsupportedOperationException(); - } - return this; - } - - public NativeWriteBatch writeBatch() { - return writeBatch; - } -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeBuffer.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeBuffer.java deleted file mode 100644 index c0343b1..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeBuffer.java +++ /dev/null @@ -1,316 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ArgFlag.CRITICAL; -import static org.fusesource.hawtjni.runtime.ArgFlag.NO_IN; -import static org.fusesource.hawtjni.runtime.ArgFlag.NO_OUT; - -import java.util.concurrent.atomic.AtomicInteger; -import org.fusesource.hawtjni.runtime.JniArg; -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniMethod; -import org.fusesource.hawtjni.runtime.PointerMath; - -/** - * A NativeBuffer allocates a native buffer on the heap. It supports - * creating sub slices/views of that buffer and manages reference tracking - * so that the the native buffer is freed once all NativeBuffer views - * are deleted. - * - * @author Hiram Chirino - */ -public class NativeBuffer extends NativeObject { - private static final ThreadLocal CURRENT_POOL = new ThreadLocal(); - private final Allocation allocation; - private final long capacity; - - private NativeBuffer(Allocation allocation, long self, long capacity) { - super(self); - this.capacity = capacity; - this.allocation = allocation; - this.allocation.retain(); - } - - public static NativeBuffer create(long capacity) { - Pool pool = CURRENT_POOL.get(); - if (pool == null) { - Allocation allocation = new Allocation(capacity); - return new NativeBuffer(allocation, allocation.self, capacity); - } else { - return pool.create(capacity); - } - } - - public static NativeBuffer create(byte[] data) { - if (data == null) { - return null; - } else { - return create(data, 0, data.length); - } - } - - public static NativeBuffer create(String data) { - return create(cbytes(data)); - } - - public static NativeBuffer create(byte[] data, int offset, int length) { - NativeBuffer rc = create(length); - rc.write(0, data, offset, length); - return rc; - } - - public static void pushMemoryPool(int size) { - Pool original = CURRENT_POOL.get(); - Pool next = new Pool(size, original); - CURRENT_POOL.set(next); - } - - public static void popMemoryPool() { - Pool next = CURRENT_POOL.get(); - next.delete(); - if (next.prev == null) { - CURRENT_POOL.remove(); - } else { - CURRENT_POOL.set(next.prev); - } - } - - static byte[] cbytes(String strvalue) { - byte[] value = strvalue.getBytes(); - // expand by 1 so we get a null at the end. - byte[] rc = new byte[value.length + 1]; - System.arraycopy(value, 0, rc, 0, value.length); - return rc; - } - - public NativeBuffer slice(long offset, long length) { - assertAllocated(); - if (length < 0) { - throw new IllegalArgumentException("length cannot be negative"); - } - if (offset < 0) { - throw new IllegalArgumentException("offset cannot be negative"); - } - if (offset + length >= capacity) { - throw new ArrayIndexOutOfBoundsException( - "offset + length exceed the length of this buffer"); - } - return new NativeBuffer(allocation, PointerMath.add(self, offset), length); - } - - public NativeBuffer head(long length) { - return slice(0, length); - } - - public NativeBuffer tail(long length) { - if (capacity - length < 0) { - throw new ArrayIndexOutOfBoundsException("capacity-length cannot be less than zero"); - } - return slice(capacity - length, length); - } - - public void delete() { - allocation.release(); - } - - public long capacity() { - return capacity; - } - - public void write(long at, byte[] source, int offset, int length) { - assertAllocated(); - if (length < 0) { - throw new IllegalArgumentException("length cannot be negative"); - } - if (offset < 0) { - throw new IllegalArgumentException("offset cannot be negative"); - } - if (at < 0) { - throw new IllegalArgumentException("at cannot be negative"); - } - if (at + length > capacity) { - throw new ArrayIndexOutOfBoundsException( - "at + length exceeds the capacity of this object"); - } - if (offset + length > source.length) { - throw new ArrayIndexOutOfBoundsException( - "offset + length exceed the length of the source buffer"); - } - NativeBufferJNI.buffer_copy(source, offset, self, at, length); - } - - public void read(long at, byte[] target, int offset, int length) { - assertAllocated(); - if (length < 0) { - throw new IllegalArgumentException("length cannot be negative"); - } - if (offset < 0) { - throw new IllegalArgumentException("offset cannot be negative"); - } - if (at < 0) { - throw new IllegalArgumentException("at cannot be negative"); - } - if (at + length > capacity) { - throw new ArrayIndexOutOfBoundsException( - "at + length exceeds the capacity of this object"); - } - if (offset + length > target.length) { - throw new ArrayIndexOutOfBoundsException( - "offset + length exceed the length of the target buffer"); - } - NativeBufferJNI.buffer_copy(self, at, target, offset, length); - } - - public byte[] toByteArray() { - if (capacity > Integer.MAX_VALUE) { - throw new OutOfMemoryError("Native buffer larger than the largest allowed Java byte[]"); - } - byte[] rc = new byte[(int) capacity]; - read(0, rc, 0, rc.length); - return rc; - } - - @JniClass - static class NativeBufferJNI { - static { - NativeDB.LIBRARY.load(); - } - - @JniMethod(cast = "void *") - public static final native long malloc( - @JniArg(cast = "size_t") long size); - - public static final native void free( - @JniArg(cast = "void *") long self); - - // public static final native void buffer_copy ( - // @JniArg(cast="const void *") long src, - // @JniArg(cast="size_t") long srcPos, - // @JniArg(cast="void *") long dest, - // @JniArg(cast="size_t") long destPos, - // @JniArg(cast="size_t") long length); - - public static final native void buffer_copy( - @JniArg(cast = "const void *", flags = {NO_OUT, CRITICAL}) byte[] src, - @JniArg(cast = "size_t") long srcPos, - @JniArg(cast = "void *") long dest, - @JniArg(cast = "size_t") long destPos, - @JniArg(cast = "size_t") long length); - - public static final native void buffer_copy( - @JniArg(cast = "const void *") long src, - @JniArg(cast = "size_t") long srcPos, - @JniArg(cast = "void *", flags = {NO_IN, CRITICAL}) byte[] dest, - @JniArg(cast = "size_t") long destPos, - @JniArg(cast = "size_t") long length); - - // @JniMethod(cast="void *") - // public static final native long memset ( - // @JniArg(cast="void *") long buffer, - // int c, - // @JniArg(cast="size_t") long num); - - } - - private static class Allocation extends NativeObject { - private final AtomicInteger retained = new AtomicInteger(0); - - private Allocation(long size) { - super(NativeBufferJNI.malloc(size)); - } - - void retain() { - assertAllocated(); - retained.incrementAndGet(); - } - - void release() { - assertAllocated(); - int r = retained.decrementAndGet(); - if (r < 0) { - throw new Error("The object has already been deleted."); - } else if (r == 0) { - NativeBufferJNI.free(self); - self = 0; - } - } - } - - private static class Pool { - private final Pool prev; - Allocation allocation; - long pos; - long remaining; - int chunk; - - public Pool(int chunk, Pool prev) { - this.chunk = chunk; - this.prev = prev; - } - - NativeBuffer create(long size) { - if (size >= chunk) { - Allocation allocation = new Allocation(size); - return new NativeBuffer(allocation, allocation.self, size); - } - - if (remaining < size) { - delete(); - } - - if (allocation == null) { - allocate(); - } - - NativeBuffer rc = new NativeBuffer(allocation, pos, size); - pos = PointerMath.add(pos, size); - remaining -= size; - return rc; - } - - private void allocate() { - allocation = new Allocation(chunk); - allocation.retain(); - remaining = chunk; - pos = allocation.self; - } - - public void delete() { - if (allocation != null) { - allocation.release(); - allocation = null; - } - } - } -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeCache.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeCache.java deleted file mode 100644 index 7085ca3..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeCache.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_DELETE; - -import org.fusesource.hawtjni.runtime.JniArg; -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniMethod; - -/** - * Provides a java interface to the C++ leveldb::Cache class. - * - * @author Hiram Chirino - */ -public class NativeCache extends NativeObject { - - public NativeCache(long capacity) { - super(CacheJNI.NewLRUCache(capacity)); - } - - /** - * delete jni object. - */ - public void delete() { - assertAllocated(); - CacheJNI.delete(self); - self = 0; - } - - @JniClass(name = "leveldb::Cache", flags = {CPP}) - private static class CacheJNI { - static { - NativeDB.LIBRARY.load(); - } - - @JniMethod(cast = "leveldb::Cache *", accessor = "leveldb::NewLRUCache") - public static final native long NewLRUCache( - @JniArg(cast = "size_t") long capacity); - - @JniMethod(flags = {CPP_DELETE}) - public static final native void delete(long self); - } - -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeComparator.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeComparator.java deleted file mode 100644 index 0530a9b..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeComparator.java +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ArgFlag.CRITICAL; -import static org.fusesource.hawtjni.runtime.ArgFlag.NO_IN; -import static org.fusesource.hawtjni.runtime.ArgFlag.NO_OUT; -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.ClassFlag.STRUCT; -import static org.fusesource.hawtjni.runtime.FieldFlag.CONSTANT; -import static org.fusesource.hawtjni.runtime.FieldFlag.POINTER_FIELD; -import static org.fusesource.hawtjni.runtime.MethodFlag.CONSTANT_INITIALIZER; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_DELETE; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_NEW; - -import org.fusesource.hawtjni.runtime.JniArg; -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniField; -import org.fusesource.hawtjni.runtime.JniMethod; - -/** - *

- * Provides a java interface to the C++ leveldb::Comparator class. - *

- * - * @author Hiram Chirino - */ -public abstract class NativeComparator extends NativeObject { - - public static final NativeComparator BYTEWISE_COMPARATOR = - new NativeComparator(ComparatorJNI.BYTEWISE_COMPARATOR) { - @Override - public void delete() { - // we won't really delete this one since it's static. - } - - @Override - public int compare(byte[] key1, byte[] key2) { - throw new UnsupportedOperationException(); - } - - @Override - public String name() { - throw new UnsupportedOperationException(); - } - }; - private NativeBuffer name_buffer; - private long globalRef; - - public NativeComparator() { - super(ComparatorJNI.create()); - try { - name_buffer = NativeBuffer.create(name()); - globalRef = NativeDB.DBJNI.NewGlobalRef(this); - if (globalRef == 0) { - throw new RuntimeException("jni call failed: NewGlobalRef"); - } - ComparatorJNI struct = new ComparatorJNI(); - struct.compare_method = NativeDB.DBJNI.GetMethodID(this.getClass(), "compare", "(JJ)I"); - if (struct.compare_method == 0) { - throw new RuntimeException("jni call failed: GetMethodID"); - } - struct.target = globalRef; - struct.name = name_buffer.pointer(); - ComparatorJNI.memmove(self, struct, ComparatorJNI.SIZEOF); - - } catch (RuntimeException e) { - delete(); - throw e; - } - } - - NativeComparator(long ptr) { - super(ptr); - } - - public void delete() { - if (name_buffer != null) { - name_buffer.delete(); - name_buffer = null; - } - if (globalRef != 0) { - NativeDB.DBJNI.DeleteGlobalRef(globalRef); - globalRef = 0; - } - } - - private int compare(long ptr1, long ptr2) { - NativeSlice s1 = new NativeSlice(); - s1.read(ptr1, 0); - NativeSlice s2 = new NativeSlice(); - s2.read(ptr2, 0); - return compare(s1.toByteArray(), s2.toByteArray()); - } - - public abstract int compare(byte[] key1, byte[] key2); - - public abstract String name(); - - @JniClass(name = "JNIComparator", flags = {STRUCT, CPP}) - public static class ComparatorJNI { - - @JniField(flags = {CONSTANT}, accessor = "sizeof(struct JNIComparator)") - static int SIZEOF; - @JniField(flags = { - CONSTANT}, cast = "const Comparator*", accessor = "leveldb::BytewiseComparator()") - private static long BYTEWISE_COMPARATOR; - - static { - NativeDB.LIBRARY.load(); - init(); - } - - @JniField(cast = "jobject", flags = {POINTER_FIELD}) - long target; - @JniField(cast = "jmethodID", flags = {POINTER_FIELD}) - long compare_method; - @JniField(cast = "const char *") - long name; - - @JniMethod(flags = {CPP_NEW}) - public static final native long create(); - - @JniMethod(flags = {CPP_DELETE}) - public static final native void delete(long ptr); - - public static final native void memmove( - @JniArg(cast = "void *") long dest, - @JniArg(cast = "const void *", flags = {NO_OUT, CRITICAL}) ComparatorJNI src, - @JniArg(cast = "size_t") long size); - - public static final native void memmove( - @JniArg(cast = "void *", flags = {NO_IN, CRITICAL}) ComparatorJNI dest, - @JniArg(cast = "const void *") long src, - @JniArg(cast = "size_t") long size); - - @JniMethod(flags = {CONSTANT_INITIALIZER}) - private static final native void init(); - - } - -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeCompressionType.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeCompressionType.java deleted file mode 100644 index 21defef..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeCompressionType.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -/** - * Provides a java interface to the C++ leveldb::CompressionType enum. - * - * @author Hiram Chirino - */ -public enum NativeCompressionType { - kNoCompression(0x0), kSnappyCompression(0x1); - - static final int t = kNoCompression.value; - final int value; - - NativeCompressionType(int value) { - this.value = value; - } -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeDB.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeDB.java deleted file mode 100644 index dc61571..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeDB.java +++ /dev/null @@ -1,445 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ArgFlag.BY_VALUE; -import static org.fusesource.hawtjni.runtime.ArgFlag.NO_OUT; -import static org.fusesource.hawtjni.runtime.ArgFlag.POINTER_ARG; -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_DELETE; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_METHOD; -import static org.fusesource.hawtjni.runtime.MethodFlag.JNI; -import static org.fusesource.hawtjni.runtime.MethodFlag.POINTER_RETURN; - -import java.io.File; -import java.io.IOException; -import org.fusesource.hawtjni.runtime.JniArg; -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniMethod; -import org.fusesource.hawtjni.runtime.Library; - -/** - * The DB object provides the main interface to acessing LevelDB. - * - * @author Hiram Chirino - */ -public class NativeDB extends NativeObject { - - public static final Library LIBRARY = new Library("leveldbjni", NativeDB.class); - - private NativeDB(long self) { - super(self); - } - - static void checkStatus(long s) throws DBException { - NativeStatus status = new NativeStatus(s); - try { - if (!status.isOk()) { - throw new DBException(status.toString(), status.isNotFound()); - } - } finally { - status.delete(); - } - } - - static void checkArgNotNull(Object value, String name) { - if (value == null) { - throw new IllegalArgumentException("The " + name + " argument cannot be null"); - } - } - - public static NativeDB open(NativeOptions options, File path) throws IOException { - checkArgNotNull(options, "options"); - checkArgNotNull(path, "path"); - long[] rc = new long[1]; - try { - checkStatus(DBJNI.Open(options, path.getCanonicalPath(), rc)); - } catch (IOException e) { - if (rc[0] != 0) { - DBJNI.delete(rc[0]); - } - throw e; - } - return new NativeDB(rc[0]); - } - - public static void destroy(File path, NativeOptions options) throws IOException { - checkArgNotNull(options, "options"); - checkArgNotNull(path, "path"); - checkStatus(DBJNI.DestroyDB(path.getCanonicalPath(), options)); - } - - public static void repair(File path, NativeOptions options) throws IOException { - checkArgNotNull(options, "options"); - checkArgNotNull(path, "path"); - checkStatus(DBJNI.RepairDB(path.getCanonicalPath(), options)); - } - - public void delete() { - assertAllocated(); - DBJNI.delete(self); - self = 0; - } - - public void delete(NativeWriteOptions options, byte[] key) throws DBException { - checkArgNotNull(options, "options"); - checkArgNotNull(key, "key"); - NativeBuffer keyBuffer = NativeBuffer.create(key); - try { - delete(options, keyBuffer); - } finally { - keyBuffer.delete(); - } - } - - private void delete(NativeWriteOptions options, NativeBuffer keyBuffer) throws DBException { - delete(options, new NativeSlice(keyBuffer)); - } - - private void delete(NativeWriteOptions options, NativeSlice keySlice) throws DBException { - assertAllocated(); - checkStatus(DBJNI.Delete(self, options, keySlice)); - } - - /*public void suspendCompactions() { - DBJNI.SuspendCompactions(self); - } - - public void resumeCompactions() { - DBJNI.ResumeCompactions(self); - }*/ - - public void put(NativeWriteOptions options, byte[] key, byte[] value) throws DBException { - checkArgNotNull(options, "options"); - checkArgNotNull(key, "key"); - checkArgNotNull(value, "value"); - NativeBuffer keyBuffer = NativeBuffer.create(key); - try { - NativeBuffer valueBuffer = NativeBuffer.create(value); - try { - put(options, keyBuffer, valueBuffer); - } finally { - valueBuffer.delete(); - } - } finally { - keyBuffer.delete(); - } - } - - private void put(NativeWriteOptions options, NativeBuffer keyBuffer, NativeBuffer valueBuffer) - throws DBException { - put(options, new NativeSlice(keyBuffer), new NativeSlice(valueBuffer)); - } - - private void put(NativeWriteOptions options, NativeSlice keySlice, NativeSlice valueSlice) - throws DBException { - assertAllocated(); - checkStatus(DBJNI.Put(self, options, keySlice, valueSlice)); - } - - - public void write(NativeWriteOptions options, NativeWriteBatch updates) throws DBException { - checkArgNotNull(options, "options"); - checkArgNotNull(updates, "updates"); - checkStatus(DBJNI.Write(self, options, updates.pointer())); - } - - public byte[] get(NativeReadOptions options, byte[] key) throws DBException { - checkArgNotNull(options, "options"); - checkArgNotNull(key, "key"); - NativeBuffer keyBuffer = NativeBuffer.create(key); - try { - return get(options, keyBuffer); - } finally { - keyBuffer.delete(); - } - } - - private byte[] get(NativeReadOptions options, NativeBuffer keyBuffer) throws DBException { - return get(options, new NativeSlice(keyBuffer)); - } - - private byte[] get(NativeReadOptions options, NativeSlice keySlice) throws DBException { - assertAllocated(); - NativeStdString result = new NativeStdString(); - try { - long s = DBJNI.Get(self, options, keySlice, result.pointer()); - NativeStatus status = new NativeStatus(s); - try { - if (status.isOk()) { - return result.toByteArray(); - } - if (status.isNotFound()) { - return null; - } - throw new DBException(status.toString(), status.isNotFound()); - } finally { - status.delete(); - } - } finally { - result.delete(); - } - } - - public NativeSnapshot getSnapshot() { - return new NativeSnapshot(DBJNI.GetSnapshot(self)); - } - - public void releaseSnapshot(NativeSnapshot snapshot) { - checkArgNotNull(snapshot, "snapshot"); - DBJNI.ReleaseSnapshot(self, snapshot.pointer()); - } - - public NativeIterator iterator(NativeReadOptions options) { - checkArgNotNull(options, "options"); - return new NativeIterator(DBJNI.NewIterator(self, options)); - } - - public long[] getApproximateSizes(NativeRange... ranges) { - if (ranges == null) { - return null; - } - - long[] rc = new long[ranges.length]; - NativeRange.RangeJNI[] structs = new NativeRange.RangeJNI[ranges.length]; - if (rc.length > 0) { - NativeBuffer range_array = NativeRange.RangeJNI.arrayCreate(ranges.length); - try { - for (int i = 0; i < ranges.length; i++) { - structs[i] = new NativeRange.RangeJNI(ranges[i]); - structs[i].arrayWrite(range_array.pointer(), i); - } - DBJNI.GetApproximateSizes(self, range_array.pointer(), ranges.length, rc); - } finally { - for (int i = 0; i < ranges.length; i++) { - if (structs[i] != null) { - structs[i].delete(); - } - } - range_array.delete(); - } - } - return rc; - } - - public String getProperty(String name) { - checkArgNotNull(name, "name"); - NativeBuffer keyBuffer = NativeBuffer.create(name.getBytes()); - try { - byte[] property = getProperty(keyBuffer); - if (property == null) { - return null; - } else { - return new String(property); - } - } finally { - keyBuffer.delete(); - } - } - - private byte[] getProperty(NativeBuffer nameBuffer) { - return getProperty(new NativeSlice(nameBuffer)); - } - - private byte[] getProperty(NativeSlice nameSlice) { - assertAllocated(); - NativeStdString result = new NativeStdString(); - try { - if (DBJNI.GetProperty(self, nameSlice, result.pointer())) { - return result.toByteArray(); - } else { - return null; - } - } finally { - result.delete(); - } - } - - public void compactRange(byte[] begin, byte[] end) { - NativeBuffer keyBuffer = NativeBuffer.create(begin); - try { - NativeBuffer valueBuffer = NativeBuffer.create(end); - try { - compactRange(keyBuffer, valueBuffer); - } finally { - if (valueBuffer != null) { - valueBuffer.delete(); - } - } - } finally { - if (keyBuffer != null) { - keyBuffer.delete(); - } - } - } - - private void compactRange(NativeBuffer beginBuffer, NativeBuffer endBuffer) { - compactRange(NativeSlice.create(beginBuffer), NativeSlice.create(endBuffer)); - } - - private void compactRange(NativeSlice beginSlice, NativeSlice endSlice) { - assertAllocated(); - DBJNI.CompactRange(self, beginSlice, endSlice); - } - - @JniClass(name = "leveldb::DB", flags = {CPP}) - static class DBJNI { - static { - NativeDB.LIBRARY.load(); - } - - @JniMethod(flags = {JNI, POINTER_RETURN}, cast = "jobject") - public static final native long NewGlobalRef( - Object target); - - @JniMethod(flags = {JNI}, cast = "jobject") - public static final native void DeleteGlobalRef( - @JniArg(cast = "jobject", flags = {POINTER_ARG}) - long target); - - @JniMethod(flags = {JNI, POINTER_RETURN}, cast = "jmethodID") - public static final native long GetMethodID( - @JniArg(cast = "jclass", flags = {POINTER_ARG}) - Class clazz, - String name, - String signature); - - @JniMethod(flags = {CPP_DELETE}) - static final native void delete( - long self - ); - - @JniMethod(copy = "leveldb::Status", accessor = "leveldb::DB::Open") - static final native long Open( - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeOptions options, - @JniArg(cast = "const char*") String path, - @JniArg(cast = "leveldb::DB**") long[] self); - - @JniMethod(copy = "leveldb::Status", flags = {CPP_METHOD}) - static final native long Put( - long self, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeWriteOptions options, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeSlice key, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeSlice value - ); - - @JniMethod(copy = "leveldb::Status", flags = {CPP_METHOD}) - static final native long Delete( - long self, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeWriteOptions options, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeSlice key - ); - - @JniMethod(copy = "leveldb::Status", flags = {CPP_METHOD}) - static final native long Write( - long self, - @JniArg(flags = {BY_VALUE}) NativeWriteOptions options, - @JniArg(cast = "leveldb::WriteBatch *") long updates - ); - - @JniMethod(copy = "leveldb::Status", flags = {CPP_METHOD}) - static final native long Get( - long self, - @JniArg(flags = {NO_OUT, BY_VALUE}) NativeReadOptions options, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeSlice key, - @JniArg(cast = "std::string *") long value - ); - - @JniMethod(cast = "leveldb::Iterator *", flags = {CPP_METHOD}) - static final native long NewIterator( - long self, - @JniArg(flags = {NO_OUT, BY_VALUE}) NativeReadOptions options - ); - - @JniMethod(cast = "leveldb::Snapshot *", flags = {CPP_METHOD}) - static final native long GetSnapshot( - long self); - - @JniMethod(flags = {CPP_METHOD}) - static final native void ReleaseSnapshot( - long self, - @JniArg(cast = "const leveldb::Snapshot *") long snapshot - ); - - @JniMethod(flags = {CPP_METHOD}) - static final native void GetApproximateSizes( - long self, - @JniArg(cast = "const leveldb::Range *") long range, - int n, - @JniArg(cast = "uint64_t*") long[] sizes - ); - - @JniMethod(flags = {CPP_METHOD}) - static final native boolean GetProperty( - long self, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeSlice property, - @JniArg(cast = "std::string *") long value - ); - - @JniMethod(copy = "leveldb::Status", accessor = "leveldb::DestroyDB") - static final native long DestroyDB( - @JniArg(cast = "const char*") String path, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeOptions options); - - @JniMethod(copy = "leveldb::Status", accessor = "leveldb::RepairDB") - static final native long RepairDB( - @JniArg(cast = "const char*") String path, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeOptions options); - - @JniMethod(flags = {CPP_METHOD}) - static final native void CompactRange( - long self, - @JniArg(flags = {NO_OUT}) NativeSlice begin, - @JniArg(flags = {NO_OUT}) NativeSlice end - ); - - /* @JniMethod(flags={CPP_METHOD}) - static final native void SuspendCompactions(long self); - - @JniMethod(flags={CPP_METHOD}) - static final native void ResumeCompactions(long self);*/ - } - - public static class DBException extends IOException { - private final boolean notFound; - - DBException(String s, boolean notFound) { - super(s); - this.notFound = notFound; - } - - public boolean isNotFound() { - return notFound; - } - } -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeFilter.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeFilter.java deleted file mode 100644 index d65e7dc..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeFilter.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_DELETE; - -import org.fusesource.hawtjni.runtime.JniArg; -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniMethod; - -/** - * Provides a java interface to the C++ leveldb::FilterPolicy class. - * - * @author Hiram Chirino - */ -public class NativeFilter extends NativeObject { - - public NativeFilter(int bitsPerKey) { - super(FilterPolicyJNI.NewBloomFilterPolicy(bitsPerKey)); - } - - /** - * delete jni object. - */ - public void delete() { - assertAllocated(); - FilterPolicyJNI.delete(self); - self = 0; - } - - @JniClass(name = "leveldb::FilterPolicy", flags = {CPP}) - private static class FilterPolicyJNI { - static { - NativeDB.LIBRARY.load(); - } - - @JniMethod(cast = "const leveldb::FilterPolicy *", accessor = "leveldb::NewBloomFilterPolicy") - public static final native long NewBloomFilterPolicy( - @JniArg(cast = "int") int bits_per_key); - - @JniMethod(flags = {CPP_DELETE}) - public static final native void delete(long self); - } - -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeIterator.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeIterator.java deleted file mode 100644 index ad0a30f..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeIterator.java +++ /dev/null @@ -1,196 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ArgFlag.BY_VALUE; -import static org.fusesource.hawtjni.runtime.ArgFlag.NO_OUT; -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_DELETE; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_METHOD; - -import org.fusesource.hawtjni.runtime.JniArg; -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniMethod; - -/** - * Provides a java interface to the C++ leveldb::Iterator class. - * - * @author Hiram Chirino - */ -public class NativeIterator extends NativeObject { - - NativeIterator(long self) { - super(self); - } - - public void delete() { - assertAllocated(); - IteratorJNI.delete(self); - self = 0; - } - - public boolean isValid() { - assertAllocated(); - return IteratorJNI.Valid(self); - } - - private void checkStatus() throws NativeDB.DBException { - NativeDB.checkStatus(IteratorJNI.status(self)); - } - - public void seekToFirst() { - assertAllocated(); - IteratorJNI.SeekToFirst(self); - } - - public void seekToLast() { - assertAllocated(); - IteratorJNI.SeekToLast(self); - } - - public void seek(byte[] key) throws NativeDB.DBException { - NativeDB.checkArgNotNull(key, "key"); - NativeBuffer keyBuffer = NativeBuffer.create(key); - try { - seek(keyBuffer); - } finally { - keyBuffer.delete(); - } - } - - private void seek(NativeBuffer keyBuffer) throws NativeDB.DBException { - seek(new NativeSlice(keyBuffer)); - } - - private void seek(NativeSlice keySlice) throws NativeDB.DBException { - assertAllocated(); - IteratorJNI.Seek(self, keySlice); - checkStatus(); - } - - public void next() throws NativeDB.DBException { - assertAllocated(); - IteratorJNI.Next(self); - checkStatus(); - } - - public void prev() throws NativeDB.DBException { - assertAllocated(); - IteratorJNI.Prev(self); - checkStatus(); - } - - public byte[] key() throws NativeDB.DBException { - assertAllocated(); - long slice_ptr = IteratorJNI.key(self); - checkStatus(); - try { - NativeSlice slice = new NativeSlice(); - slice.read(slice_ptr, 0); - return slice.toByteArray(); - } finally { - NativeSlice.SliceJNI.delete(slice_ptr); - } - } - - public byte[] value() throws NativeDB.DBException { - assertAllocated(); - long slice_ptr = IteratorJNI.value(self); - checkStatus(); - try { - NativeSlice slice = new NativeSlice(); - slice.read(slice_ptr, 0); - return slice.toByteArray(); - } finally { - NativeSlice.SliceJNI.delete(slice_ptr); - } - } - - @JniClass(name = "leveldb::Iterator", flags = {CPP}) - private static class IteratorJNI { - static { - NativeDB.LIBRARY.load(); - } - - @JniMethod(flags = {CPP_DELETE}) - public static final native void delete( - long self - ); - - @JniMethod(flags = {CPP_METHOD}) - static final native boolean Valid( - long self - ); - - @JniMethod(flags = {CPP_METHOD}) - static final native void SeekToFirst( - long self - ); - - @JniMethod(flags = {CPP_METHOD}) - static final native void SeekToLast( - long self - ); - - @JniMethod(flags = {CPP_METHOD}) - static final native void Seek( - long self, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeSlice target - ); - - @JniMethod(flags = {CPP_METHOD}) - static final native void Next( - long self - ); - - @JniMethod(flags = {CPP_METHOD}) - static final native void Prev( - long self - ); - - @JniMethod(copy = "leveldb::Slice", flags = {CPP_METHOD}) - static final native long key( - long self - ); - - @JniMethod(copy = "leveldb::Slice", flags = {CPP_METHOD}) - static final native long value( - long self - ); - - @JniMethod(copy = "leveldb::Status", flags = {CPP_METHOD}) - static final native long status( - long self - ); - } -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeLogger.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeLogger.java deleted file mode 100644 index c3c4741..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeLogger.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ArgFlag.CRITICAL; -import static org.fusesource.hawtjni.runtime.ArgFlag.NO_OUT; -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.ClassFlag.STRUCT; -import static org.fusesource.hawtjni.runtime.FieldFlag.CONSTANT; -import static org.fusesource.hawtjni.runtime.FieldFlag.POINTER_FIELD; -import static org.fusesource.hawtjni.runtime.MethodFlag.CONSTANT_INITIALIZER; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_DELETE; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_NEW; - -import org.fusesource.hawtjni.runtime.JniArg; -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniField; -import org.fusesource.hawtjni.runtime.JniMethod; - -/** - *

- * Provides a java interface to the C++ leveldb::Logger class. - *

- * - * @author Hiram Chirino - */ -public abstract class NativeLogger extends NativeObject { - - private long globalRef; - - public NativeLogger() { - super(LoggerJNI.create()); - try { - globalRef = NativeDB.DBJNI.NewGlobalRef(this); - if (globalRef == 0) { - throw new RuntimeException("jni call failed: NewGlobalRef"); - } - LoggerJNI struct = new LoggerJNI(); - struct.log_method = - NativeDB.DBJNI.GetMethodID(this.getClass(), "log", "(Ljava/lang/String;)V"); - if (struct.log_method == 0) { - throw new RuntimeException("jni call failed: GetMethodID"); - } - struct.target = globalRef; - LoggerJNI.memmove(self, struct, LoggerJNI.SIZEOF); - - } catch (RuntimeException e) { - delete(); - throw e; - } - } - - NativeLogger(long ptr) { - super(ptr); - } - - public void delete() { - if (globalRef != 0) { - NativeDB.DBJNI.DeleteGlobalRef(globalRef); - globalRef = 0; - } - } - - public abstract void log(String message); - - @JniClass(name = "JNILogger", flags = {STRUCT, CPP}) - public static class LoggerJNI { - - @JniField(flags = {CONSTANT}, accessor = "sizeof(struct JNILogger)") - static int SIZEOF; - - static { - NativeDB.LIBRARY.load(); - init(); - } - - @JniField(cast = "jobject", flags = {POINTER_FIELD}) - long target; - @JniField(cast = "jmethodID", flags = {POINTER_FIELD}) - long log_method; - - @JniMethod(flags = {CPP_NEW}) - public static final native long create(); - - @JniMethod(flags = {CPP_DELETE}) - public static final native void delete( - long self - ); - - public static final native void memmove( - @JniArg(cast = "void *") long dest, - @JniArg(cast = "const void *", flags = {NO_OUT, CRITICAL}) LoggerJNI src, - @JniArg(cast = "size_t") long size); - - @JniMethod(flags = {CONSTANT_INITIALIZER}) - private static final native void init(); - } - -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeObject.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeObject.java deleted file mode 100644 index fdf5455..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeObject.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -/** - * A helper base class which is used to track a pointer to a native - * structure or class. - * - * @author Hiram Chirino - */ -class NativeObject { - - protected long self; - - protected NativeObject(long self) { - this.self = self; - if (self == 0) { - throw new OutOfMemoryError("Failure allocating native heap memory"); - } - } - - long pointer() { - return self; - } - - public boolean isAllocated() { - return self != 0; - } - - protected void assertAllocated() { - assert isAllocated() : "This object has been deleted"; - } - -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeOptions.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeOptions.java deleted file mode 100644 index c637e28..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeOptions.java +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.ClassFlag.STRUCT; -import static org.fusesource.hawtjni.runtime.FieldFlag.CONSTANT; -import static org.fusesource.hawtjni.runtime.FieldFlag.FIELD_SKIP; -import static org.fusesource.hawtjni.runtime.MethodFlag.CONSTANT_INITIALIZER; - -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniField; -import org.fusesource.hawtjni.runtime.JniMethod; - -/** - * Provides a java interface to the C++ leveldb::Options class. - * - * @author Hiram Chirino - */ -@JniClass(name = "leveldb::Options", flags = {STRUCT, CPP}) -public class NativeOptions { - - @JniField(flags = {CONSTANT}, cast = "Env*", accessor = "leveldb::Env::Default()") - private static long DEFAULT_ENV; - - static { - NativeDB.LIBRARY.load(); - init(); - } - - private boolean create_if_missing = false; - private boolean error_if_exists = false; - private boolean paranoid_checks = false; - private boolean reuse_logs = false; - @JniField(cast = "size_t") - private long write_buffer_size = 4 << 20; - @JniField(cast = "size_t") - private long block_size = 4 * 1024; - private int max_open_files = 1000; - private int block_restart_interval = 16; - @JniField(cast = "size_t") - private long max_file_size = 2 * 1024 * 1024; - @JniField(flags = {FIELD_SKIP}) - private NativeComparator comparatorObject = NativeComparator.BYTEWISE_COMPARATOR; - @JniField(cast = "const leveldb::Comparator*") - private long comparator = comparatorObject.pointer(); - @JniField(flags = {FIELD_SKIP}) - private NativeLogger infoLogObject = null; - @JniField(cast = "leveldb::Logger*") - private long info_log = 0; - @JniField(cast = "leveldb::Env*") - private final long env = DEFAULT_ENV; - @JniField(cast = "leveldb::Cache*") - private long block_cache = 0; - @JniField(flags = {FIELD_SKIP}) - private NativeCache cache; - @JniField(cast = "const leveldb::FilterPolicy*") - private long filter_policy = 0; - @JniField(flags = {FIELD_SKIP}) - private NativeFilter filter; - @JniField(cast = "leveldb::CompressionType") - private int compression = NativeCompressionType.kSnappyCompression.value; - - @JniMethod(flags = {CONSTANT_INITIALIZER}) - private static final native void init(); - - public NativeOptions createIfMissing(boolean value) { - this.create_if_missing = value; - return this; - } - - public boolean createIfMissing() { - return create_if_missing; - } - - public NativeOptions errorIfExists(boolean value) { - this.error_if_exists = value; - return this; - } - - public boolean errorIfExists() { - return error_if_exists; - } - - public NativeOptions paranoidChecks(boolean value) { - this.paranoid_checks = value; - return this; - } - - public boolean paranoidChecks() { - return paranoid_checks; - } - - public NativeOptions reuseLogs(boolean value) { - this.reuse_logs = value; - return this; - } - - public boolean reuseLogs() { - return reuse_logs; - } - - public NativeOptions writeBufferSize(long value) { - this.write_buffer_size = value; - return this; - } - - public long writeBufferSize() { - return write_buffer_size; - } - - public NativeOptions maxFileSize(long value) { - this.max_file_size = value; - return this; - } - - public long maxFileSize() { - return max_file_size; - } - - public NativeOptions maxOpenFiles(int value) { - this.max_open_files = value; - return this; - } - - public int maxOpenFiles() { - return max_open_files; - } - - public NativeOptions blockRestartInterval(int value) { - this.block_restart_interval = value; - return this; - } - - public int blockRestartInterval() { - return block_restart_interval; - } - - public NativeOptions blockSize(long value) { - this.block_size = value; - return this; - } - - public long blockSize() { - return block_size; - } - - // @JniField(cast="Env*") - // private long env = DEFAULT_ENV; - - public NativeComparator comparator() { - return comparatorObject; - } - - public NativeOptions comparator(NativeComparator comparator) { - if (comparator == null) { - throw new IllegalArgumentException("comparator cannot be null"); - } - this.comparatorObject = comparator; - this.comparator = comparator.pointer(); - return this; - } - - public NativeLogger infoLog() { - return infoLogObject; - } - - public NativeOptions infoLog(NativeLogger logger) { - this.infoLogObject = logger; - if (logger == null) { - this.info_log = 0; - } else { - this.info_log = logger.pointer(); - } - return this; - } - - public NativeCompressionType compression() { - if (compression == NativeCompressionType.kNoCompression.value) { - return NativeCompressionType.kNoCompression; - } else if (compression == NativeCompressionType.kSnappyCompression.value) { - return NativeCompressionType.kSnappyCompression; - } else { - return NativeCompressionType.kSnappyCompression; - } - } - - public NativeOptions compression(NativeCompressionType compression) { - this.compression = compression.value; - return this; - } - - public NativeCache cache() { - return cache; - } - - public NativeOptions cache(NativeCache cache) { - this.cache = cache; - if (cache != null) { - this.block_cache = cache.pointer(); - } else { - this.block_cache = 0; - } - return this; - } - - public NativeFilter filter() { - return filter; - } - - public NativeOptions filter(NativeFilter filter) { - this.filter = filter; - if (filter != null) { - this.filter_policy = filter.pointer(); - } else { - this.filter_policy = 0; - } - return this; - } -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeRange.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeRange.java deleted file mode 100644 index 2861721..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeRange.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ArgFlag.CRITICAL; -import static org.fusesource.hawtjni.runtime.ArgFlag.NO_IN; -import static org.fusesource.hawtjni.runtime.ArgFlag.NO_OUT; -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.ClassFlag.STRUCT; -import static org.fusesource.hawtjni.runtime.FieldFlag.CONSTANT; -import static org.fusesource.hawtjni.runtime.FieldFlag.FIELD_SKIP; -import static org.fusesource.hawtjni.runtime.MethodFlag.CONSTANT_INITIALIZER; - -import org.fusesource.hawtjni.runtime.JniArg; -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniField; -import org.fusesource.hawtjni.runtime.JniMethod; -import org.fusesource.hawtjni.runtime.PointerMath; - -/** - * Provides a java interface to the C++ leveldb::ReadOptions class. - * - * @author Hiram Chirino - */ -public class NativeRange { - - private final byte[] start; - private final byte[] limit; - - public NativeRange(byte[] start, byte[] limit) { - NativeDB.checkArgNotNull(start, "start"); - NativeDB.checkArgNotNull(limit, "limit"); - this.limit = limit; - this.start = start; - } - - public byte[] limit() { - return limit; - } - - public byte[] start() { - return start; - } - - @JniClass(name = "leveldb::Range", flags = {STRUCT, CPP}) - public static class RangeJNI { - - @JniField(flags = {CONSTANT}, accessor = "sizeof(struct leveldb::Range)") - static int SIZEOF; - - static { - NativeDB.LIBRARY.load(); - init(); - } - - @JniField - NativeSlice start = new NativeSlice(); - @JniField(flags = {FIELD_SKIP}) - NativeBuffer start_buffer; - @JniField - NativeSlice limit = new NativeSlice(); - @JniField(flags = {FIELD_SKIP}) - NativeBuffer limit_buffer; - - public RangeJNI(NativeRange range) { - start_buffer = NativeBuffer.create(range.start()); - start.set(start_buffer); - try { - limit_buffer = NativeBuffer.create(range.limit()); - } catch (OutOfMemoryError e) { - start_buffer.delete(); - throw e; - } - limit.set(limit_buffer); - } - - public static final native void memmove( - @JniArg(cast = "void *") long dest, - @JniArg(cast = "const void *", flags = {NO_OUT, CRITICAL}) RangeJNI src, - @JniArg(cast = "size_t") long size); - - public static final native void memmove( - @JniArg(cast = "void *", flags = {NO_IN, CRITICAL}) RangeJNI dest, - @JniArg(cast = "const void *") long src, - @JniArg(cast = "size_t") long size); - - @JniMethod(flags = {CONSTANT_INITIALIZER}) - private static final native void init(); - - static NativeBuffer arrayCreate(int dimension) { - return NativeBuffer.create(dimension * SIZEOF); - } - - public void delete() { - start_buffer.delete(); - limit_buffer.delete(); - } - - void arrayWrite(long buffer, int index) { - RangeJNI.memmove(PointerMath.add(buffer, SIZEOF * index), this, SIZEOF); - } - - void arrayRead(long buffer, int index) { - RangeJNI.memmove(this, PointerMath.add(buffer, SIZEOF * index), SIZEOF); - } - - } -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeReadOptions.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeReadOptions.java deleted file mode 100644 index b1d3820..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeReadOptions.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.ClassFlag.STRUCT; - -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniField; - -/** - * Provides a java interface to the C++ leveldb::ReadOptions class. - * - * @author Hiram Chirino - */ -@JniClass(name = "leveldb::ReadOptions", flags = {STRUCT, CPP}) -public class NativeReadOptions { - - @JniField - private boolean verify_checksums = false; - - @JniField - private boolean fill_cache = true; - - @JniField(cast = "const leveldb::Snapshot*") - private long snapshot = 0; - - public boolean fillCache() { - return fill_cache; - } - - public NativeReadOptions fillCache(boolean fill_cache) { - this.fill_cache = fill_cache; - return this; - } - - public NativeSnapshot snapshot() { - if (snapshot == 0) { - return null; - } else { - return new NativeSnapshot(snapshot); - } - } - - public NativeReadOptions snapshot(NativeSnapshot snapshot) { - if (snapshot == null) { - this.snapshot = 0; - } else { - this.snapshot = snapshot.pointer(); - } - return this; - } - - public boolean verifyChecksums() { - return verify_checksums; - } - - public NativeReadOptions verifyChecksums(boolean verify_checksums) { - this.verify_checksums = verify_checksums; - return this; - } -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeSlice.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeSlice.java deleted file mode 100644 index 80596a9..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeSlice.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ArgFlag.CRITICAL; -import static org.fusesource.hawtjni.runtime.ArgFlag.NO_IN; -import static org.fusesource.hawtjni.runtime.ArgFlag.NO_OUT; -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.ClassFlag.STRUCT; -import static org.fusesource.hawtjni.runtime.FieldFlag.CONSTANT; -import static org.fusesource.hawtjni.runtime.MethodFlag.CONSTANT_INITIALIZER; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_DELETE; - -import org.fusesource.hawtjni.runtime.JniArg; -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniField; -import org.fusesource.hawtjni.runtime.JniMethod; -import org.fusesource.hawtjni.runtime.PointerMath; - -/** - * Provides a java interface to the C++ leveldb::Slice class. - * - * @author Hiram Chirino - */ -@JniClass(name = "leveldb::Slice", flags = {STRUCT, CPP}) -class NativeSlice { - - @JniField(cast = "const char*") - private long data_; - @JniField(cast = "size_t") - private long size_; - - public NativeSlice() { - } - - public NativeSlice(long data, long length) { - this.data_ = data; - this.size_ = length; - } - - public NativeSlice(NativeBuffer buffer) { - this(buffer.pointer(), buffer.capacity()); - } - - public static NativeSlice create(NativeBuffer buffer) { - if (buffer == null) { - return null; - } else { - return new NativeSlice(buffer); - } - } - - static NativeBuffer arrayCreate(int dimension) { - return NativeBuffer.create(dimension * SliceJNI.SIZEOF); - } - - public long data() { - return data_; - } - - public NativeSlice data(long data) { - this.data_ = data; - return this; - } - - public long size() { - return size_; - } - - public NativeSlice size(long size) { - this.size_ = size; - return this; - } - - public NativeSlice set(NativeSlice buffer) { - this.size_ = buffer.size_; - this.data_ = buffer.data_; - return this; - } - - public NativeSlice set(NativeBuffer buffer) { - this.size_ = buffer.capacity(); - this.data_ = buffer.pointer(); - return this; - } - - public byte[] toByteArray() { - if (size_ > Integer.MAX_VALUE) { - throw new ArrayIndexOutOfBoundsException( - "Native slice is larger than the maximum Java array"); - } - byte[] rc = new byte[(int) size_]; - NativeBuffer.NativeBufferJNI.buffer_copy(data_, 0, rc, 0, rc.length); - return rc; - } - - void write(long buffer, int index) { - SliceJNI.memmove(PointerMath.add(buffer, SliceJNI.SIZEOF * index), this, SliceJNI.SIZEOF); - } - - void read(long buffer, int index) { - SliceJNI.memmove(this, PointerMath.add(buffer, SliceJNI.SIZEOF * index), SliceJNI.SIZEOF); - } - - @JniClass(name = "leveldb::Slice", flags = {CPP}) - static class SliceJNI { - @JniField(flags = {CONSTANT}, accessor = "sizeof(struct leveldb::Slice)") - static int SIZEOF; - - static { - NativeDB.LIBRARY.load(); - init(); - } - - @JniMethod(flags = {CPP_DELETE}) - public static final native void delete( - long self - ); - - public static final native void memmove( - @JniArg(cast = "void *") long dest, - @JniArg(cast = "const void *", flags = {NO_OUT, CRITICAL}) NativeSlice src, - @JniArg(cast = "size_t") long size); - - public static final native void memmove( - @JniArg(cast = "void *", flags = {NO_IN, CRITICAL}) NativeSlice dest, - @JniArg(cast = "const void *") long src, - @JniArg(cast = "size_t") long size); - - @JniMethod(flags = {CONSTANT_INITIALIZER}) - private static final native void init(); - - } - - -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeSnapshot.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeSnapshot.java deleted file mode 100644 index 71f2dd4..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeSnapshot.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -/** - * Provides a java interface to the C++ leveldb::Snapshot class. - * - * @author Hiram Chirino - */ -public class NativeSnapshot extends NativeObject { - - NativeSnapshot(long self) { - super(self); - } - -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeStatus.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeStatus.java deleted file mode 100644 index e5ba402..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeStatus.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_DELETE; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_METHOD; - -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniMethod; - -/** - * Provides a java interface to the C++ leveldb::Status class. - * - * @author Hiram Chirino - */ -class NativeStatus extends NativeObject { - - public NativeStatus(long self) { - super(self); - } - - public void delete() { - assertAllocated(); - StatusJNI.delete(self); - self = 0; - } - - public boolean isOk() { - assertAllocated(); - return StatusJNI.ok(self); - } - - public boolean isNotFound() { - assertAllocated(); - return StatusJNI.IsNotFound(self); - } - - public String toString() { - assertAllocated(); - long strptr = StatusJNI.ToString(self); - if (strptr == 0) { - return null; - } else { - NativeStdString rc = new NativeStdString(strptr); - try { - return rc.toString(); - } finally { - rc.delete(); - } - } - } - - @JniClass(name = "leveldb::Status", flags = {CPP}) - static class StatusJNI { - static { - NativeDB.LIBRARY.load(); - } - - @JniMethod(flags = {CPP_DELETE}) - public static final native void delete( - long self); - - @JniMethod(flags = {CPP_METHOD}) - public static final native boolean ok( - long self); - - @JniMethod(flags = {CPP_METHOD}) - public static final native boolean IsNotFound( - long self); - - @JniMethod(copy = "std::string", flags = {CPP_METHOD}) - public static final native long ToString( - long self); - } - -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeStdString.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeStdString.java deleted file mode 100644 index 663dbd6..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeStdString.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_DELETE; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_METHOD; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_NEW; - -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniMethod; - -/** - * Provides a java interface to the C++ std::string class. - * - * @author Hiram Chirino - */ -class NativeStdString extends NativeObject { - - public NativeStdString(long self) { - super(self); - } - - public NativeStdString() { - super(StdStringJNI.create()); - } - - public void delete() { - assertAllocated(); - StdStringJNI.delete(self); - self = 0; - } - - public String toString() { - return new String(toByteArray()); - } - - public long length() { - assertAllocated(); - return StdStringJNI.length(self); - } - - public byte[] toByteArray() { - long l = length(); - if (l > Integer.MAX_VALUE) { - throw new ArrayIndexOutOfBoundsException( - "Native string is larger than the maximum Java array"); - } - byte[] rc = new byte[(int) l]; - NativeBuffer.NativeBufferJNI.buffer_copy(StdStringJNI.c_str_ptr(self), 0, rc, 0, rc.length); - return rc; - } - - @JniClass(name = "std::string", flags = {CPP}) - private static class StdStringJNI { - static { - NativeDB.LIBRARY.load(); - } - - @JniMethod(flags = {CPP_NEW}) - public static final native long create(); - - @JniMethod(flags = {CPP_NEW}) - public static final native long create(String value); - - @JniMethod(flags = {CPP_DELETE}) - static final native void delete( - long self); - - @JniMethod(flags = {CPP_METHOD}, accessor = "c_str", cast = "const char*") - public static final native long c_str_ptr( - long self); - - @JniMethod(flags = {CPP_METHOD}, cast = "size_t") - public static final native long length( - long self); - - } -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeWriteBatch.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeWriteBatch.java deleted file mode 100644 index 74e1664..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeWriteBatch.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ArgFlag.BY_VALUE; -import static org.fusesource.hawtjni.runtime.ArgFlag.NO_OUT; -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_DELETE; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_METHOD; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_NEW; - -import org.fusesource.hawtjni.runtime.JniArg; -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniMethod; - -/** - * Provides a java interface to the C++ leveldb::WriteBatch class. - * - * @author Hiram Chirino - */ -public class NativeWriteBatch extends NativeObject { - - public NativeWriteBatch() { - super(WriteBatchJNI.create()); - } - - public void delete() { - assertAllocated(); - WriteBatchJNI.delete(self); - self = 0; - } - - public void delete(byte[] key) { - NativeDB.checkArgNotNull(key, "key"); - NativeBuffer keyBuffer = NativeBuffer.create(key); - try { - delete(keyBuffer); - } finally { - keyBuffer.delete(); - } - } - - private void delete(NativeBuffer keyBuffer) { - delete(new NativeSlice(keyBuffer)); - } - - private void delete(NativeSlice keySlice) { - assertAllocated(); - WriteBatchJNI.Delete(self, keySlice); - } - - public void put(byte[] key, byte[] value) { - NativeDB.checkArgNotNull(key, "key"); - NativeDB.checkArgNotNull(value, "value"); - NativeBuffer keyBuffer = NativeBuffer.create(key); - try { - NativeBuffer valueBuffer = NativeBuffer.create(value); - try { - put(keyBuffer, valueBuffer); - } finally { - valueBuffer.delete(); - } - } finally { - keyBuffer.delete(); - } - } - - private void put(NativeBuffer keyBuffer, NativeBuffer valueBuffer) { - put(new NativeSlice(keyBuffer), new NativeSlice(valueBuffer)); - } - - private void put(NativeSlice keySlice, NativeSlice valueSlice) { - assertAllocated(); - WriteBatchJNI.Put(self, keySlice, valueSlice); - } - - public void clear() { - assertAllocated(); - WriteBatchJNI.Clear(self); - } - - public long approximateSize() { - assertAllocated(); - return WriteBatchJNI.ApproximateSize(self); - } - - public void append(NativeWriteBatch source) { - assertAllocated(); - WriteBatchJNI.Append(self, source.self); - } - - @JniClass(name = "leveldb::WriteBatch", flags = {CPP}) - private static class WriteBatchJNI { - static { - NativeDB.LIBRARY.load(); - } - - @JniMethod(flags = {CPP_NEW}) - public static final native long create(); - - @JniMethod(flags = {CPP_DELETE}) - public static final native void delete( - long self); - - @JniMethod(flags = {CPP_METHOD}) - static final native void Put( - long self, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeSlice key, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeSlice value - ); - - @JniMethod(flags = {CPP_METHOD}) - static final native void Delete( - long self, - @JniArg(flags = {BY_VALUE, NO_OUT}) NativeSlice key - ); - - @JniMethod(flags = {CPP_METHOD}) - static final native void Clear( - long self - ); - - @JniMethod(flags = {CPP_METHOD}) - public static final native long ApproximateSize( - long self - ); - - @JniMethod(flags = {CPP_METHOD}) - static final native void Append( - long self, - @JniArg(cast = "const leveldb::WriteBatch *") long source - ); - } - -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/NativeWriteOptions.java b/src/main/java/org/fusesource/leveldbjni/internal/NativeWriteOptions.java deleted file mode 100644 index c0d9dca..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/NativeWriteOptions.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.ClassFlag.STRUCT; - -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniField; - -/** - * Provides a java interface to the C++ leveldb::WriteOptions class. - * - * @author Hiram Chirino - */ -@JniClass(name = "leveldb::WriteOptions", flags = {STRUCT, CPP}) -public class NativeWriteOptions { - - @JniField - boolean sync; - - public boolean sync() { - return sync; - } - - public NativeWriteOptions sync(boolean sync) { - this.sync = sync; - return this; - } - -} diff --git a/src/main/java/org/fusesource/leveldbjni/internal/Util.java b/src/main/java/org/fusesource/leveldbjni/internal/Util.java deleted file mode 100644 index 29e6dcf..0000000 --- a/src/main/java/org/fusesource/leveldbjni/internal/Util.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright (C) 2011, FuseSource Corp. All rights reserved. - * - * http://fusesource.com - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of FuseSource Corp. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package org.fusesource.leveldbjni.internal; - -import static org.fusesource.hawtjni.runtime.ArgFlag.POINTER_ARG; -import static org.fusesource.hawtjni.runtime.ArgFlag.UNICODE; -import static org.fusesource.hawtjni.runtime.ClassFlag.CPP; -import static org.fusesource.hawtjni.runtime.FieldFlag.CONSTANT; -import static org.fusesource.hawtjni.runtime.MethodFlag.CONSTANT_GETTER; -import static org.fusesource.hawtjni.runtime.MethodFlag.CONSTANT_INITIALIZER; -import static org.fusesource.hawtjni.runtime.MethodFlag.CPP_METHOD; - -import java.io.File; -import java.io.IOException; -import org.fusesource.hawtjni.runtime.JniArg; -import org.fusesource.hawtjni.runtime.JniClass; -import org.fusesource.hawtjni.runtime.JniField; -import org.fusesource.hawtjni.runtime.JniMethod; - -/** - * Some miscellaneous utility functions. - * - * @author Hiram Chirino - */ -public class Util { - - /** - * Creates a hard link from source to target. - * - * @param source source file - * @param target target file - * @throws IOException if link failed - */ - public static void link(File source, File target) throws IOException { - if (UtilJNI.ON_WINDOWS == 1) { - if (UtilJNI.CreateHardLinkW(target.getCanonicalPath(), source.getCanonicalPath(), 0) == 0) { - throw new IOException("link failed"); - } - } else { - if (UtilJNI.link(source.getCanonicalPath(), target.getCanonicalPath()) != 0) { - throw new IOException("link failed: " + strerror()); - } - } - } - - static int errno() { - return UtilJNI.errno(); - } - - static String strerror() { - return string(UtilJNI.strerror(errno())); - } - - static String string(long ptr) { - if (ptr == 0) { - return null; - } - return new String(new NativeSlice(ptr, UtilJNI.strlen(ptr)).toByteArray()); - } - - @JniClass(name = "leveldb::Env", flags = {CPP}) - static class EnvJNI { - - static { - NativeDB.LIBRARY.load(); - } - - @JniMethod(cast = "leveldb::Env *", accessor = "leveldb::Env::Default") - public static final native long Default(); - - @JniMethod(flags = {CPP_METHOD}) - public static final native void Schedule( - long self, - @JniArg(cast = "void (*)(void*)") long fp, - @JniArg(cast = "void *") long arg); - - } - - @JniClass(flags = {CPP}) - static class UtilJNI { - - @JniField(flags = { - CONSTANT}, accessor = "1", conditional = "defined(_WIN32) || defined(_WIN64)") - static int ON_WINDOWS; - - static { - NativeDB.LIBRARY.load(); - init(); - } - - @JniMethod(flags = {CONSTANT_INITIALIZER}) - private static final native void init(); - - @JniMethod(conditional = "!defined(_WIN32) && !defined(_WIN64)") - static final native int link( - @JniArg(cast = "const char*") String source, - @JniArg(cast = "const char*") String target); - - @JniMethod(conditional = "defined(_WIN32) || defined(_WIN64)") - static final native int CreateHardLinkW( - @JniArg(cast = "LPCWSTR", flags = {POINTER_ARG, UNICODE}) String target, - @JniArg(cast = "LPCWSTR", flags = {POINTER_ARG, UNICODE}) String source, - @JniArg(cast = "LPSECURITY_ATTRIBUTES", flags = {POINTER_ARG}) long lpSecurityAttributes); - - @JniMethod(flags = {CONSTANT_GETTER}) - public static final native int errno(); - - @JniMethod(cast = "char *") - public static final native long strerror(int errnum); - - public static final native int strlen( - @JniArg(cast = "const char *") long s); - - } - -} diff --git a/src/main/resources/META-INF/native/linux32/arm/.gitkeep b/src/main/resources/META-INF/native/linux32/arm/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/META-INF/native/linux32/libleveldbjni.so b/src/main/resources/META-INF/native/linux32/libleveldbjni.so old mode 100755 new mode 100644 index c2ff527..9000896 Binary files a/src/main/resources/META-INF/native/linux32/libleveldbjni.so and b/src/main/resources/META-INF/native/linux32/libleveldbjni.so differ diff --git a/src/main/resources/META-INF/native/linux64/aarch64/libleveldbjni.so b/src/main/resources/META-INF/native/linux64/aarch64/libleveldbjni.so old mode 100755 new mode 100644 index 5935616..b478d85 Binary files a/src/main/resources/META-INF/native/linux64/aarch64/libleveldbjni.so and b/src/main/resources/META-INF/native/linux64/aarch64/libleveldbjni.so differ diff --git a/src/main/resources/META-INF/native/linux64/libleveldbjni.so b/src/main/resources/META-INF/native/linux64/libleveldbjni.so index 4b683c2..91b1a49 100644 Binary files a/src/main/resources/META-INF/native/linux64/libleveldbjni.so and b/src/main/resources/META-INF/native/linux64/libleveldbjni.so differ diff --git a/src/main/resources/META-INF/native/linux64/ppc64le/.gitkeep b/src/main/resources/META-INF/native/linux64/ppc64le/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/META-INF/native/osx/.gitkeep b/src/main/resources/META-INF/native/osx/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/META-INF/native/osx/libleveldbjni.jnilib b/src/main/resources/META-INF/native/osx/libleveldbjni.jnilib new file mode 100644 index 0000000..e67c7a2 Binary files /dev/null and b/src/main/resources/META-INF/native/osx/libleveldbjni.jnilib differ diff --git a/src/main/resources/META-INF/native/osx64/aarch64/libleveldbjni.jnilib b/src/main/resources/META-INF/native/osx64/aarch64/libleveldbjni.jnilib index 5930c2e..dae2fd1 100755 Binary files a/src/main/resources/META-INF/native/osx64/aarch64/libleveldbjni.jnilib and b/src/main/resources/META-INF/native/osx64/aarch64/libleveldbjni.jnilib differ diff --git a/src/main/resources/META-INF/native/osx64/x86_64/libleveldbjni.jnilib b/src/main/resources/META-INF/native/osx64/x86_64/libleveldbjni.jnilib deleted file mode 100755 index 6043d04..0000000 Binary files a/src/main/resources/META-INF/native/osx64/x86_64/libleveldbjni.jnilib and /dev/null differ diff --git a/src/main/resources/META-INF/native/windows32/.gitkeep b/src/main/resources/META-INF/native/windows32/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/META-INF/native/windows32/leveldbjni.dll b/src/main/resources/META-INF/native/windows32/leveldbjni.dll new file mode 100644 index 0000000..88a8e5b Binary files /dev/null and b/src/main/resources/META-INF/native/windows32/leveldbjni.dll differ diff --git a/src/main/resources/META-INF/native/windows64/.gitkeep b/src/main/resources/META-INF/native/windows64/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/META-INF/native/windows64/leveldbjni.dll b/src/main/resources/META-INF/native/windows64/leveldbjni.dll new file mode 100644 index 0000000..8dd9fb8 Binary files /dev/null and b/src/main/resources/META-INF/native/windows64/leveldbjni.dll differ diff --git a/src/main/resources/org/fusesource/leveldbjni/version.txt b/src/main/resources/org/fusesource/leveldbjni/version.txt deleted file mode 100644 index db8b4ac..0000000 --- a/src/main/resources/org/fusesource/leveldbjni/version.txt +++ /dev/null @@ -1 +0,0 @@ -1.23.2 \ No newline at end of file diff --git a/src/test/java/org/fusesource/leveldbjni/test/DBTest.java b/src/test/java/org/fusesource/leveldbjni/test/DBTest.java index 2fb4595..4682e90 100644 --- a/src/test/java/org/fusesource/leveldbjni/test/DBTest.java +++ b/src/test/java/org/fusesource/leveldbjni/test/DBTest.java @@ -6,7 +6,7 @@ * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: - * + * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above @@ -16,7 +16,7 @@ * * Neither the name of FuseSource Corp. nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR @@ -31,44 +31,30 @@ */ package org.fusesource.leveldbjni.test; -import static org.fusesource.leveldbjni.JniDBFactory.asString; -import static org.fusesource.leveldbjni.JniDBFactory.bytes; -import static org.fusesource.leveldbjni.JniDBFactory.factory; - -import java.io.File; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Random; -import java.util.UUID; +import java.util.stream.Collectors; import junit.framework.TestCase; import org.fusesource.leveldbjni.JniDBFactory; import org.fusesource.leveldbjni.internal.JniDB; -import org.iq80.leveldb.DB; -import org.iq80.leveldb.DBComparator; -import org.iq80.leveldb.DBException; -import org.iq80.leveldb.DBIterator; -import org.iq80.leveldb.Logger; -import org.iq80.leveldb.Options; -import org.iq80.leveldb.Range; -import org.iq80.leveldb.ReadOptions; -import org.iq80.leveldb.WriteBatch; -import org.iq80.leveldb.WriteOptions; +import org.iq80.leveldb.*; import org.junit.Assert; import org.junit.Test; +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.*; + +import static org.fusesource.leveldbjni.JniDBFactory.asString; +import static org.fusesource.leveldbjni.JniDBFactory.bytes; +import static org.fusesource.leveldbjni.JniDBFactory.factory; + /** * A Unit test for the DB class implementation. * * @author Hiram Chirino */ public class DBTest extends TestCase { + DBFactory factory = JniDBFactory.factory; File getTestDirectory(String name) throws IOException { File rc = new File(new File("test-data"), name); @@ -131,18 +117,6 @@ public void testCRUD() throws IOException, DBException { db.close(); } - @Test - public void testBloomFilter() throws IOException, DBException { - - try (DB db = factory.open(getTestDirectory(getName()), - new Options().createIfMissing(true).bitsPerKey(10))){ - for (int i = 0; i <100 ; i++) { - String v = UUID.randomUUID().toString(); - db.put(bytes(v), bytes(v)); - } - } - } - @Test public void testIterator() throws IOException, DBException { @@ -219,16 +193,7 @@ public void testWriteBatch() throws IOException, DBException { batch.put(bytes("Tampa"), bytes("green")); batch.put(bytes("London"), bytes("red")); batch.put(bytes("New York"), bytes("blue")); - WriteBatch source = db.createWriteBatch(); - source.append(batch); - source.append(batch); - db.write(source); - assertEquals( batch.approximateSize() * 2 - 12, source.approximateSize()); - source.clear(); - batch.clear(); - assertEquals(source.approximateSize(), batch.approximateSize()); - assertEquals(12, batch.approximateSize()); - source.close(); + db.write(batch); batch.close(); ArrayList expecting = new ArrayList(); @@ -291,9 +256,7 @@ public void testGetProperty() throws IOException, DBException { String stats = db.getProperty("leveldb.stats"); assertNotNull(stats); assertTrue(stats.contains("Compactions")); - String memory_usage = db.getProperty("leveldb.approximate-memory-usage"); - assertNotNull(memory_usage); - assertTrue(Integer.parseInt(memory_usage) > 0); + db.close(); } @@ -442,22 +405,22 @@ public void testCompactRanges() throws IOException, InterruptedException, DBExce // Compactions // Level Files Size(MB) Time(sec) Read(MB) Write(MB) // -------------------------------------------------- - assertFalse(stats.contains(" 1 0 0 ")); - assertFalse(stats.contains(" 2 0 0 ")); + assertFalse(stats.contains("1 0 0 0")); + assertFalse(stats.contains("2 0 0 0")); // After the compaction, level 1 and 2 should not have any files in it.. ((JniDB) db).compactRange(null, null); stats = db.getProperty("leveldb.stats"); System.out.println(stats); - assertTrue(stats.contains(" 1 0 0 ")); - assertTrue(stats.contains(" 2 0 0 ")); + assertTrue(stats.contains("1 0 0 0")); + assertTrue(stats.contains("2 0 0 0")); } db.close(); } - /*@Test + @Test public void testSuspendAndResumeCompactions() throws Exception { Options options = new Options().createIfMissing(true); File path = getTestDirectory(getName()); @@ -465,7 +428,7 @@ public void testSuspendAndResumeCompactions() throws Exception { db.suspendCompactions(); db.resumeCompactions(); db.close(); - }*/ + } public void assertEquals(byte[] arg1, byte[] arg2) { assertTrue(Arrays.equals(arg1, arg2)); @@ -634,8 +597,6 @@ public void testSeekAndIterator() throws IOException { final byte[] key_001 = newKey((byte) 1); final byte[] key_025 = newKey((byte) 25); final byte[] key_050 = newKey((byte) 50); - final byte[] key_055 = newKey((byte) 55); - final byte[] key_065 = newKey((byte) 65); final byte[] key_075 = newKey((byte) 75); final byte[] key_100 = newKey((byte) 100); final byte[] value_025 = bytes("25"); @@ -661,66 +622,15 @@ public void testSeekAndIterator() throws IOException { // it.seek(key_001); assertTrue(it.hasNext()); - assertEquals(key_025, it.key()); - assertEquals(value_025, it.value()); - - it.seekForPrev(key_001); - assertFalse(it.hasNext()); - assertFalse(it.hasPrev()); - - - it.seek(key_025); assertTrue(it.hasNext()); - assertEquals(key_025, it.key()); - assertEquals(value_025, it.value()); - - it.seekForPrev(key_025); - assertEquals(key_025, it.key()); - assertEquals(value_025, it.value()); - it.seek(key_050); assertTrue(it.hasNext()); - assertEquals(key_050, it.key()); - assertEquals(value_050, it.value()); - - it.seekForPrev(key_050); - assertEquals(key_050, it.key()); - assertEquals(value_050, it.value()); - - it.seek(key_055); - assertTrue(it.hasNext()); - assertEquals(key_075, it.key()); - assertEquals(value_075, it.value()); - - it.seekForPrev(key_055); - assertEquals(key_050, it.key()); - assertEquals(value_050, it.value()); - - it.seek(key_065); - assertTrue(it.hasNext()); - assertEquals(key_075, it.key()); - assertEquals(value_075, it.value()); - - it.seekForPrev(key_065); - assertEquals(key_050, it.key()); - assertEquals(value_050, it.value()); - it.seek(key_075); assertTrue(it.hasNext()); - assertEquals(key_075, it.key()); - assertEquals(value_075, it.value()); - - it.seekForPrev(key_075); - assertEquals(key_075, it.key()); - assertEquals(value_075, it.value()); - it.seek(key_100); assertFalse(it.hasNext()); - it.seekForPrev(key_100); - assertEquals(key_075, it.key()); - assertEquals(value_075, it.value()); // // check next: // @@ -739,16 +649,6 @@ public void testSeekAndIterator() throws IOException { assertEquals(key_050, entry.getKey()); assertEquals(value_050, entry.getValue()); - it.seek(key_055); - entry = it.next(); - assertEquals(key_075, entry.getKey()); - assertEquals(value_075, entry.getValue()); - - it.seek(key_065); - entry = it.next(); - assertEquals(key_075, entry.getKey()); - assertEquals(value_075, entry.getValue()); - it.seek(key_075); entry = it.next(); assertEquals(key_075, entry.getKey()); @@ -779,16 +679,6 @@ public void testSeekAndIterator() throws IOException { assertEquals(key_050, entry.getKey()); assertEquals(value_050, entry.getValue()); - it.seek(key_055); - entry = it.peekNext(); - assertEquals(key_075, entry.getKey()); - assertEquals(value_075, entry.getValue()); - - it.seek(key_065); - entry = it.peekNext(); - assertEquals(key_075, entry.getKey()); - assertEquals(value_075, entry.getValue()); - it.seek(key_075); entry = it.peekNext(); assertEquals(key_075, entry.getKey()); @@ -805,27 +695,20 @@ public void testSeekAndIterator() throws IOException { // check hasPrev // it.seek(key_001); - assertTrue(it.hasPrev()); + assertFalse(it.hasPrev()); it.seek(key_025); - assertTrue(it.hasPrev()); + assertFalse(it.hasPrev()); it.seek(key_050); assertTrue(it.hasPrev()); - it.seek(key_055); - assertTrue(it.hasPrev()); it.seek(key_075); assertTrue(it.hasPrev()); it.seek(key_100); assertFalse(it.hasPrev()); // TODO: Expected result? - it.seekForPrev(key_100); - assertTrue(it.hasPrev()); + // // check prev: // it.seek(key_001); - entry = it.prev(); - assertEquals(key_025, entry.getKey()); - assertEquals(value_025, entry.getValue()); - try { it.prev(); fail("NoSuchElementException is expected"); @@ -833,9 +716,6 @@ public void testSeekAndIterator() throws IOException { } it.seek(key_025); - entry = it.prev(); - assertEquals(key_025, entry.getKey()); - assertEquals(value_025, entry.getValue()); try { it.prev(); fail("NoSuchElementException is expected"); @@ -844,45 +724,13 @@ public void testSeekAndIterator() throws IOException { it.seek(key_050); entry = it.prev(); - assertEquals(key_050, entry.getKey()); - assertEquals(value_050, entry.getValue()); - assertEquals(key_025, it.key()); - assertEquals(value_025, it.value()); - - it.seek(key_055); - entry = it.prev(); - assertEquals(key_075, entry.getKey()); - assertEquals(value_075, entry.getValue()); - assertEquals(key_050, it.key()); - assertEquals(value_050, it.value()); - - it.seekForPrev(key_055); - entry = it.prev(); - assertEquals(key_050, entry.getKey()); - assertEquals(value_050, entry.getValue()); - assertEquals(key_025, it.key()); - assertEquals(value_025, it.value()); - - it.seek(key_065); - entry = it.prev(); - assertEquals(key_075, entry.getKey()); - assertEquals(value_075, entry.getValue()); - assertEquals(key_050, it.key()); - assertEquals(value_050, it.value()); + assertEquals(key_025, entry.getKey()); + assertEquals(value_025, entry.getValue()); - it.seekForPrev(key_065); + it.seek(key_075); entry = it.prev(); assertEquals(key_050, entry.getKey()); assertEquals(value_050, entry.getValue()); - assertEquals(key_025, it.key()); - assertEquals(value_025, it.value()); - - it.seek(key_075); - entry = it.prev(); - assertEquals(key_075, entry.getKey()); - assertEquals(value_075, entry.getValue()); - assertEquals(key_050, it.key()); - assertEquals(value_050, it.value()); it.seek(key_100); try { @@ -895,41 +743,32 @@ public void testSeekAndIterator() throws IOException { // check peekPrev: // it.seek(key_001); - it.peekPrev(); - + try { + it.peekPrev(); + fail("NoSuchElementException is expected"); + } catch (NoSuchElementException ex) { + } it.seek(key_025); - it.peekPrev(); - + try { + it.peekPrev(); + fail("NoSuchElementException is expected"); + } catch (NoSuchElementException ex) { + } it.seek(key_050); entry = it.peekPrev(); - assertEquals(key_050, entry.getKey()); - assertEquals(value_050, entry.getValue()); + assertEquals(key_025, entry.getKey()); + assertEquals(value_025, entry.getValue()); it.seek(key_075); entry = it.peekPrev(); - assertEquals(key_075, entry.getKey()); - assertEquals(value_075, entry.getValue()); - - it.seekForPrev(key_055); - entry = it.peekPrev(); assertEquals(key_050, entry.getKey()); assertEquals(value_050, entry.getValue()); - it.seekForPrev(key_065); - entry = it.peekPrev(); - assertEquals(key_050, entry.getKey()); - assertEquals(value_050, entry.getValue()); - - it.seekForPrev(key_075); - entry = it.peekPrev(); - assertEquals(key_075, entry.getKey()); - assertEquals(value_075, entry.getValue()); - it.seek(key_100); try { - it.peekPrev(); + it.peekPrev(); // TODO: Expected result? fail("NoSuchElementException is expected"); } catch (NoSuchElementException ex) { } @@ -989,17 +828,9 @@ public void testIteratorNegative() throws IOException { // check prev: // it.seekToLast(); - entry = it.prev(); - assertEquals(key_075, entry.getKey()); - assertEquals(value_075, entry.getValue()); - assertEquals(key_050, it.key()); - assertEquals(value_050, it.value()); - entry = it.prev(); assertEquals(key_050, entry.getKey()); assertEquals(value_050, entry.getValue()); - assertEquals(key_025, it.key()); - assertEquals(value_025, it.value()); entry = it.prev(); assertEquals(key_025, entry.getKey()); @@ -1015,79 +846,69 @@ public void testIteratorNegative() throws IOException { db.close(); } - private static byte[] toByteArray(int value) { - return new byte[]{(byte)(value >> 24), (byte)(value >> 16), (byte)(value >> 8), (byte)value}; - } - public static int fromByteArray(byte[] bytes) { - return bytes[0] << 24 | (bytes[1] & 255) << 16 | (bytes[2] & 255) << 8 | bytes[3] & 255; - } @Test - public void testIteratorNegative2() throws IOException { - - File path = getTestDirectory(getName()); - Options options = new Options(); - DB database = factory.open(path, options); - - for (int i = 0; i < 10; i++) { - byte[] bytes = toByteArray(i); - database.put(bytes, bytes); - } - ReadOptions readOptions = new ReadOptions().fillCache(false); - - DBIterator iterable = database.iterator(readOptions); - DBIterator iterable2 = database.iterator(readOptions); - DBIterator iterable3 = database.iterator(readOptions); - DBIterator iterable4 = database.iterator(readOptions); - iterable.seekToFirst(); - while (iterable.hasNext()) { - System.out.println("iterable:" + fromByteArray(iterable.next().getKey())); - } - System.out.println(); + public void testWriteBatch2() throws IOException, DBException { - for (iterable2.seekToFirst(); iterable2.hasNext(); iterable2.next()) { - System.out.println("iterable2:" + fromByteArray(iterable2.key())); - - } - System.out.println(); + Options options = new Options().createIfMissing(true); + options.comparator(new DBComparator() { + public int compare(byte[] o1, byte[] o2) { + return Byte.compare(o1[0],o2[0]); + } + public String name() { + return getName(); + } + public byte[] findShortestSeparator(byte[] start, byte[] limit) { + return new byte[0]; + } + public byte[] findShortSuccessor(byte[] key) { + return new byte[0]; + } + }); - iterable3.seekToLast(); - while (iterable3.hasPrev()) { - System.out.println("iterable3:" + fromByteArray(iterable3.prev().getKey())); + File path = getTestDirectory(getName()); + DB db = factory.open(path, options); + long start = System.currentTimeMillis(); + Set bytes = new HashSet<>(); + for (int i = 0; i < 256 ; i++) { + bytes.add(bytes(i +"")[0]); + try (WriteBatch batch = db.createWriteBatch()) { + StringBuilder s = new StringBuilder(); + for (int j = 0; j < 1024 * 1024 * 3; j++) { + s.append((char)(new Random().nextInt(128))); + } + batch.put(bytes(i +""),bytes(s.toString())); + db.write(batch); + db.compactRange(null, null); + statProperty(db, i); + } } - System.out.println(); - for (iterable4.seekToLast(); iterable4.hasPrev(); iterable4.prev()) { - byte[] key = iterable4.key(); - System.out.println("iterable4:" + fromByteArray(key)); - + int c = 0; + try (DBIterator iterator = db.iterator()){ + for (iterator.seekToFirst();iterator.hasNext();iterator.next()) { + c++; + } } - iterable.close(); - iterable2.close(); - iterable3.close(); - iterable4.close(); - database.close(); - } - @Test - public void testReuseLogs() throws IOException { - Options options = new Options().createIfMissing(true).reuseLogs(true); - File path = getTestDirectory(getName()); - DB db = factory.open(path, options); - db.put("halibobo".getBytes(StandardCharsets.UTF_8),"hello".getBytes(StandardCharsets.UTF_8)); + Assert.assertEquals(bytes.size(), 10); + Assert.assertEquals(bytes.size(), c); + long e = System.currentTimeMillis(); + System.out.println(e -start); db.close(); - DB reopenDb = factory.open(path, options); - Assert.assertTrue(Arrays.equals("hello".getBytes(StandardCharsets.UTF_8), - reopenDb.get("halibobo".getBytes(StandardCharsets.UTF_8)))); - reopenDb.close(); + factory.destroy(path, new Options()); } - @Test - public void testMaxFileSize() throws IOException { - Options options = new Options().createIfMissing(true).maxFileSize(4 * 1024 * 1024); - File path = getTestDirectory(getName()); - DB db = factory.open(path, options); - for (int i = 0; i < 10000; i++) { - byte[] bytes = UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8); - db.put(bytes, bytes); + + private void statProperty(DB db, int i) { + try { + List stats = Arrays.stream(db.getProperty("leveldb.stats") + .split("\n")).skip(3).collect(Collectors.toList()); + double total = 0; + for (String stat : stats) { + String[] tmp = stat.trim().replaceAll(" +", ",").split(","); + total += Double.parseDouble(tmp[2]); + } + System.out.println(i + ":" + total + " M"); + } catch (Exception e) { + } - db.close(); } -} +} \ No newline at end of file diff --git a/src/test/resources/tools/leveldbutil b/src/test/resources/tools/leveldbutil deleted file mode 100755 index b8a8040..0000000 Binary files a/src/test/resources/tools/leveldbutil and /dev/null differ diff --git a/third_party/leveldb b/third_party/leveldb deleted file mode 160000 index a10dfc5..0000000 --- a/third_party/leveldb +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a10dfc5b64f7f7860d35026aa198c5f04f22eb56 diff --git a/third_party/leveldbjni b/third_party/leveldbjni deleted file mode 160000 index 0a5cf90..0000000 --- a/third_party/leveldbjni +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 0a5cf9084cb83df7f97c8de699099daa590e3403 diff --git a/third_party/snappy b/third_party/snappy deleted file mode 160000 index a6c88fa..0000000 --- a/third_party/snappy +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a6c88fadc3519ff4020741d5ca1320999b554c62