@ -5,13 +5,9 @@
package org.rocksdb ;
package org.rocksdb ;
import java.util.List ;
import java.util.* ;
import java.util.Map ;
import java.util.HashMap ;
import java.io.Closeable ;
import java.io.IOException ;
import java.io.IOException ;
import org.rocksdb.util.Environment ;
import org.rocksdb.util.Environment ;
import org.rocksdb.NativeLibraryLoader ;
/ * *
/ * *
* A RocksDB is a persistent ordered map from keys to values . It is safe for
* A RocksDB is a persistent ordered map from keys to values . It is safe for
@ -84,7 +80,7 @@ public class RocksDB extends RocksObject {
err = e ;
err = e ;
}
}
}
}
if ( success = = false ) {
if ( ! success ) {
throw err ;
throw err ;
}
}
}
}
@ -95,11 +91,11 @@ public class RocksDB extends RocksObject {
* set to true .
* set to true .
*
*
* @param path the path to the rocksdb .
* @param path the path to the rocksdb .
* @return a rocksdb instance on success , null if the specified rocksdb can
* @return a { @link RocksDB } instance on success , null if the specified
* not be opened .
* { @link RocksDB } can not be opened .
*
*
* @throws org . rocksdb . RocksDBException
* @see Options # setCreateIfMissing ( boolean )
* @see Options # setCreateIfMissing ( boolean )
* @see org . rocksdb . Options # createIfMissing ( )
* /
* /
public static RocksDB open ( String path ) throws RocksDBException {
public static RocksDB open ( String path ) throws RocksDBException {
// This allows to use the rocksjni default Options instead of
// This allows to use the rocksjni default Options instead of
@ -108,18 +104,63 @@ public class RocksDB extends RocksObject {
return open ( options , path ) ;
return open ( options , path ) ;
}
}
/ * *
* The factory constructor of RocksDB that opens a RocksDB instance given
* the path to the database using the specified options and db path and a list
* of column family names .
* < p >
* If opened in read write mode every existing column family name must be passed
* within the list to this method . < / p >
* < p >
* If opened in read - only mode only a subset of existing column families must
* be passed to this method . < / p >
* < p >
* Options instance * should * not be disposed before all DBs using this options
* instance have been closed . If user doesn ' t call options dispose explicitly ,
* then this options instance will be GC ' d automatically < / p >
* < p >
* ColumnFamily handles are disposed when the RocksDB instance is disposed .
* < / p >
*
* @param path the path to the rocksdb .
* @param columnFamilyNames list of column family names
* @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
* on open .
* @return a { @link RocksDB } instance on success , null if the specified
* { @link RocksDB } can not be opened .
*
* @throws org . rocksdb . RocksDBException
* @see Options # setCreateIfMissing ( boolean )
* /
public static RocksDB open ( String path , List < String > columnFamilyNames ,
List < ColumnFamilyHandle > columnFamilyHandles ) throws RocksDBException {
// This allows to use the rocksjni default Options instead of
// the c++ one.
Options options = new Options ( ) ;
return open ( options , path , columnFamilyNames , columnFamilyHandles ) ;
}
/ * *
/ * *
* The factory constructor of RocksDB that opens a RocksDB instance given
* The factory constructor of RocksDB that opens a RocksDB instance given
* the path to the database using the specified options and db path .
* the path to the database using the specified options and db path .
*
*
* < p >
* Options instance * should * not be disposed before all DBs using this options
* Options instance * should * not be disposed before all DBs using this options
* instance have been closed . If user doesn ' t call options dispose explicitly ,
* instance have been closed . If user doesn ' t call options dispose explicitly ,
* then this options instance will be GC ' d automatically .
* then this options instance will be GC ' d automatically . < / p >
*
* < p >
* Options instance can be re - used to open multiple DBs if DB statistics is
* Options instance can be re - used to open multiple DBs if DB statistics is
* not used . If DB statistics are required , then its recommended to open DB
* not used . If DB statistics are required , then its recommended to open DB
* with new Options instance as underlying native statistics instance does not
* with new Options instance as underlying native statistics instance does not
* use any locks to prevent concurrent updates .
* use any locks to prevent concurrent updates . < / p >
*
* @param options { @link org . rocksdb . Options } instance .
* @param path the path to the rocksdb .
* @return a { @link RocksDB } instance on success , null if the specified
* { @link RocksDB } can not be opened .
*
* @throws org . rocksdb . RocksDBException
* @see Options # setCreateIfMissing ( boolean )
* /
* /
public static RocksDB open ( Options options , String path )
public static RocksDB open ( Options options , String path )
throws RocksDBException {
throws RocksDBException {
@ -133,6 +174,169 @@ public class RocksDB extends RocksObject {
return db ;
return db ;
}
}
/ * *
* The factory constructor of RocksDB that opens a RocksDB instance given
* the path to the database using the specified options and db path and a list
* of column family names .
* < p >
* If opened in read write mode every existing column family name must be passed
* within the list to this method . < / p >
* < p >
* If opened in read - only mode only a subset of existing column families must
* be passed to this method . < / p >
* < p >
* Options instance * should * not be disposed before all DBs using this options
* instance have been closed . If user doesn ' t call options dispose explicitly ,
* then this options instance will be GC ' d automatically . < / p >
* < p >
* Options instance can be re - used to open multiple DBs if DB statistics is
* not used . If DB statistics are required , then its recommended to open DB
* with new Options instance as underlying native statistics instance does not
* use any locks to prevent concurrent updates . < / p >
* < p >
* ColumnFamily handles are disposed when the RocksDB instance is disposed . < / p >
*
* @param options { @link org . rocksdb . Options } instance .
* @param path the path to the rocksdb .
* @param columnFamilyNames list of column family names
* @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
* on open .
* @return a { @link RocksDB } instance on success , null if the specified
* { @link RocksDB } can not be opened .
*
* @throws org . rocksdb . RocksDBException
* @see Options # setCreateIfMissing ( boolean )
* /
public static RocksDB open ( Options options , String path , List < String > columnFamilyNames ,
List < ColumnFamilyHandle > columnFamilyHandles )
throws RocksDBException {
RocksDB db = new RocksDB ( ) ;
List < Long > cfReferences = db . open ( options . nativeHandle_ , path ,
columnFamilyNames , columnFamilyNames . size ( ) ) ;
for ( int i = 0 ; i < columnFamilyNames . size ( ) ; i + + ) {
columnFamilyHandles . add ( new ColumnFamilyHandle ( cfReferences . get ( i ) ) ) ;
}
db . storeOptionsInstance ( options ) ;
return db ;
}
/ * *
* The factory constructor of RocksDB that opens a RocksDB instance in
* Read - Only mode given the path to the database using the default
* options .
*
* @param path the path to the RocksDB .
* @return a { @link RocksDB } instance on success , null if the specified
* { @link RocksDB } can not be opened .
* @throws RocksDBException
* /
public static RocksDB openReadOnly ( String path )
throws RocksDBException {
// This allows to use the rocksjni default Options instead of
// the c++ one.
Options options = new Options ( ) ;
return openReadOnly ( options , path ) ;
}
/ * *
* The factory constructor of RocksDB that opens a RocksDB instance in
* Read - Only mode given the path to the database using the default
* options .
*
* @param path the path to the RocksDB .
* @param columnFamilyNames list of column family names
* @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
* on open .
* @return a { @link RocksDB } instance on success , null if the specified
* { @link RocksDB } can not be opened .
* @throws RocksDBException
* /
public static RocksDB openReadOnly ( String path , List < String > columnFamilyNames ,
List < ColumnFamilyHandle > columnFamilyHandles ) throws RocksDBException {
// This allows to use the rocksjni default Options instead of
// the c++ one.
Options options = new Options ( ) ;
return openReadOnly ( options , path , columnFamilyNames , columnFamilyHandles ) ;
}
/ * *
* The factory constructor of RocksDB that opens a RocksDB instance in
* Read - Only mode given the path to the database using the specified
* options and db path .
*
* Options instance * should * not be disposed before all DBs using this options
* instance have been closed . If user doesn ' t call options dispose explicitly ,
* then this options instance will be GC ' d automatically .
*
* @param options { @link Options } instance .
* @param path the path to the RocksDB .
* @return a { @link RocksDB } instance on success , null if the specified
* { @link RocksDB } can not be opened .
* @throws RocksDBException
* /
public static RocksDB openReadOnly ( Options options , String path )
throws RocksDBException {
// when non-default Options is used, keeping an Options reference
// in RocksDB can prevent Java to GC during the life-time of
// the currently-created RocksDB.
RocksDB db = new RocksDB ( ) ;
db . openROnly ( options . nativeHandle_ , path ) ;
db . storeOptionsInstance ( options ) ;
return db ;
}
/ * *
* The factory constructor of RocksDB that opens a RocksDB instance in
* Read - Only mode given the path to the database using the specified
* options and db path .
*
* < p > This open method allows to open RocksDB using a subset of available
* column families < / p >
* < p > Options instance * should * not be disposed before all DBs using this
* options instance have been closed . If user doesn ' t call options dispose
* explicitly , then this options instance will be GC ' d automatically . < / p >
*
* @param options { @link Options } instance .
* @param path the path to the RocksDB .
* @param columnFamilyNames list of column family names
* @param columnFamilyHandles will be filled with ColumnFamilyHandle instances
* on open .
* @return a { @link RocksDB } instance on success , null if the specified
* { @link RocksDB } can not be opened .
* @throws RocksDBException
* /
public static RocksDB openReadOnly ( Options options , String path ,
List < String > columnFamilyNames , List < ColumnFamilyHandle > columnFamilyHandles )
throws RocksDBException {
// when non-default Options is used, keeping an Options reference
// in RocksDB can prevent Java to GC during the life-time of
// the currently-created RocksDB.
RocksDB db = new RocksDB ( ) ;
List < Long > cfReferences = db . openROnly ( options . nativeHandle_ , path ,
columnFamilyNames , columnFamilyNames . size ( ) ) ;
for ( int i = 0 ; i < columnFamilyNames . size ( ) ; i + + ) {
columnFamilyHandles . add ( new ColumnFamilyHandle ( cfReferences . get ( i ) ) ) ;
}
db . storeOptionsInstance ( options ) ;
return db ;
}
/ * *
* Static method to determine all available column families for a
* rocksdb database identified by path
*
* @param options Options for opening the database
* @param path Absolute path to rocksdb database
* @return List < byte [ ] > List containing the column family names
*
* @throws RocksDBException
* /
public static List < byte [ ] > listColumnFamilies ( Options options , String path )
throws RocksDBException {
return RocksDB . listColumnFamilies ( options . nativeHandle_ , path ) ;
}
private void storeOptionsInstance ( Options options ) {
private void storeOptionsInstance ( Options options ) {
options_ = options ;
options_ = options ;
}
}
@ -155,16 +359,39 @@ public class RocksDB extends RocksObject {
*
*
* @param key the specified key to be inserted .
* @param key the specified key to be inserted .
* @param value the value associated with the specified key .
* @param value the value associated with the specified key .
*
* @see RocksDBException
* /
* /
public void put ( byte [ ] key , byte [ ] value ) throws RocksDBException {
public void put ( byte [ ] key , byte [ ] value ) throws RocksDBException {
put ( nativeHandle_ , key , key . length , value , value . length ) ;
put ( nativeHandle_ , key , key . length , value , value . length ) ;
}
}
/ * *
* Set the database entry for "key" to "value" in the specified
* column family .
*
* @param columnFamilyHandle { @link org . rocksdb . ColumnFamilyHandle }
* instance
* @param key the specified key to be inserted .
* @param value the value associated with the specified key .
*
* throws IllegalArgumentException if column family is not present
*
* @see RocksDBException
* /
public void put ( ColumnFamilyHandle columnFamilyHandle , byte [ ] key ,
byte [ ] value ) throws RocksDBException {
put ( nativeHandle_ , key , key . length , value , value . length ,
columnFamilyHandle . nativeHandle_ ) ;
}
/ * *
/ * *
* Set the database entry for "key" to "value" .
* Set the database entry for "key" to "value" .
*
*
* @param key the specified key to be inserted .
* @param key the specified key to be inserted .
* @param value the value associated with the specified key .
* @param value the value associated with the specified key .
*
* @see RocksDBException
* /
* /
public void put ( WriteOptions writeOpts , byte [ ] key , byte [ ] value )
public void put ( WriteOptions writeOpts , byte [ ] key , byte [ ] value )
throws RocksDBException {
throws RocksDBException {
@ -172,8 +399,73 @@ public class RocksDB extends RocksObject {
key , key . length , value , value . length ) ;
key , key . length , value , value . length ) ;
}
}
/ * *
* Set the database entry for "key" to "value" for the specified
* column family .
*
* @param columnFamilyHandle { @link org . rocksdb . ColumnFamilyHandle }
* instance
* @param key the specified key to be inserted .
* @param value the value associated with the specified key .
*
* throws IllegalArgumentException if column family is not present
*
* @see RocksDBException
* @see IllegalArgumentException
* /
public void put ( ColumnFamilyHandle columnFamilyHandle , WriteOptions writeOpts ,
byte [ ] key , byte [ ] value ) throws RocksDBException {
put ( nativeHandle_ , writeOpts . nativeHandle_ , key , key . length , value , value . length ,
columnFamilyHandle . nativeHandle_ ) ;
}
/ * *
* If the key definitely does not exist in the database , then this method
* returns false , else true .
*
* This check is potentially lighter - weight than invoking DB : : Get ( ) . One way
* to make this lighter weight is to avoid doing any IOs .
*
* @param columnFamilyHandle { @link ColumnFamilyHandle } instnace
* @param key byte array of a key to search for
* @param value StringBuffer instance which is a out parameter if a value is
* found in block - cache .
* @return boolean value indicating if key does not exist or might exist .
* /
public boolean keyMayExist ( ColumnFamilyHandle columnFamilyHandle ,
byte [ ] key , StringBuffer value ) {
return keyMayExist ( key , key . length , columnFamilyHandle . nativeHandle_ ,
value ) ;
}
/ * *
* If the key definitely does not exist in the database , then this method
* returns false , else true .
*
* This check is potentially lighter - weight than invoking DB : : Get ( ) . One way
* to make this lighter weight is to avoid doing any IOs .
*
* @param readOptions { @link ReadOptions } instance
* @param columnFamilyHandle { @link ColumnFamilyHandle } instnace
* @param key byte array of a key to search for
* @param value StringBuffer instance which is a out parameter if a value is
* found in block - cache .
* @return boolean value indicating if key does not exist or might exist .
* /
public boolean keyMayExist ( ReadOptions readOptions ,
ColumnFamilyHandle columnFamilyHandle , byte [ ] key , StringBuffer value ) {
return keyMayExist ( readOptions . nativeHandle_ ,
key , key . length , columnFamilyHandle . nativeHandle_ ,
value ) ;
}
/ * *
/ * *
* Apply the specified updates to the database .
* Apply the specified updates to the database .
*
* @param writeOpts WriteOptions instance
* @param updates WriteBatch instance
*
* @see RocksDBException
* /
* /
public void write ( WriteOptions writeOpts , WriteBatch updates )
public void write ( WriteOptions writeOpts , WriteBatch updates )
throws RocksDBException {
throws RocksDBException {
@ -181,7 +473,7 @@ public class RocksDB extends RocksObject {
}
}
/ * *
/ * *
* Get the value associated with the specified key .
* Get the value associated with the specified key within column family
*
*
* @param key the key to retrieve the value .
* @param key the key to retrieve the value .
* @param value the out - value to receive the retrieved value .
* @param value the out - value to receive the retrieved value .
@ -191,11 +483,35 @@ public class RocksDB extends RocksObject {
* input buffer { @code value } is insufficient and partial result will
* input buffer { @code value } is insufficient and partial result will
* be returned . RocksDB . NOT_FOUND will be returned if the value not
* be returned . RocksDB . NOT_FOUND will be returned if the value not
* found .
* found .
*
* @see RocksDBException
* /
* /
public int get ( byte [ ] key , byte [ ] value ) throws RocksDBException {
public int get ( byte [ ] key , byte [ ] value ) throws RocksDBException {
return get ( nativeHandle_ , key , key . length , value , value . length ) ;
return get ( nativeHandle_ , key , key . length , value , value . length ) ;
}
}
/ * *
* Get the value associated with the specified key within column family .
*
* @param columnFamilyHandle { @link org . rocksdb . ColumnFamilyHandle }
* instance
* @param key the key to retrieve the value .
* @param value the out - value to receive the retrieved value .
* @return The size of the actual value that matches the specified
* { @code key } in byte . If the return value is greater than the
* length of { @code value } , then it indicates that the size of the
* input buffer { @code value } is insufficient and partial result will
* be returned . RocksDB . NOT_FOUND will be returned if the value not
* found .
*
* @throws RocksDBException
* /
public int get ( ColumnFamilyHandle columnFamilyHandle , byte [ ] key , byte [ ] value )
throws RocksDBException , IllegalArgumentException {
return get ( nativeHandle_ , key , key . length , value , value . length ,
columnFamilyHandle . nativeHandle_ ) ;
}
/ * *
/ * *
* Get the value associated with the specified key .
* Get the value associated with the specified key .
*
*
@ -207,12 +523,35 @@ public class RocksDB extends RocksObject {
* input buffer { @code value } is insufficient and partial result will
* input buffer { @code value } is insufficient and partial result will
* be returned . RocksDB . NOT_FOUND will be returned if the value not
* be returned . RocksDB . NOT_FOUND will be returned if the value not
* found .
* found .
*
* @throws RocksDBException
* /
* /
public int get ( ReadOptions opt , byte [ ] key , byte [ ] value )
public int get ( ReadOptions opt , byte [ ] key , byte [ ] value )
throws RocksDBException {
throws RocksDBException {
return get ( nativeHandle_ , opt . nativeHandle_ ,
return get ( nativeHandle_ , opt . nativeHandle_ ,
key , key . length , value , value . length ) ;
key , key . length , value , value . length ) ;
}
}
/ * *
* Get the value associated with the specified key within column family .
*
* @param columnFamilyHandle { @link org . rocksdb . ColumnFamilyHandle }
* instance
* @param key the key to retrieve the value .
* @param value the out - value to receive the retrieved value .
* @return The size of the actual value that matches the specified
* { @code key } in byte . If the return value is greater than the
* length of { @code value } , then it indicates that the size of the
* input buffer { @code value } is insufficient and partial result will
* be returned . RocksDB . NOT_FOUND will be returned if the value not
* found .
*
* @throws RocksDBException
* /
public int get ( ColumnFamilyHandle columnFamilyHandle , ReadOptions opt , byte [ ] key ,
byte [ ] value ) throws RocksDBException {
return get ( nativeHandle_ , opt . nativeHandle_ , key , key . length , value ,
value . length , columnFamilyHandle . nativeHandle_ ) ;
}
/ * *
/ * *
* The simplified version of get which returns a new byte array storing
* The simplified version of get which returns a new byte array storing
@ -223,12 +562,30 @@ public class RocksDB extends RocksObject {
* @return a byte array storing the value associated with the input key if
* @return a byte array storing the value associated with the input key if
* any . null if it does not find the specified key .
* any . null if it does not find the specified key .
*
*
* @see RocksDBException
* @throw s RocksDBException
* /
* /
public byte [ ] get ( byte [ ] key ) throws RocksDBException {
public byte [ ] get ( byte [ ] key ) throws RocksDBException {
return get ( nativeHandle_ , key , key . length ) ;
return get ( nativeHandle_ , key , key . length ) ;
}
}
/ * *
* The simplified version of get which returns a new byte array storing
* the value associated with the specified input key if any . null will be
* returned if the specified key is not found .
*
* @param columnFamilyHandle { @link org . rocksdb . ColumnFamilyHandle }
* instance
* @param key the key retrieve the value .
* @return a byte array storing the value associated with the input key if
* any . null if it does not find the specified key .
*
* @throws RocksDBException
* /
public byte [ ] get ( ColumnFamilyHandle columnFamilyHandle , byte [ ] key )
throws RocksDBException {
return get ( nativeHandle_ , key , key . length , columnFamilyHandle . nativeHandle_ ) ;
}
/ * *
/ * *
* The simplified version of get which returns a new byte array storing
* The simplified version of get which returns a new byte array storing
* the value associated with the specified input key if any . null will be
* the value associated with the specified input key if any . null will be
@ -239,12 +596,32 @@ public class RocksDB extends RocksObject {
* @return a byte array storing the value associated with the input key if
* @return a byte array storing the value associated with the input key if
* any . null if it does not find the specified key .
* any . null if it does not find the specified key .
*
*
* @see RocksDBException
* @throw s RocksDBException
* /
* /
public byte [ ] get ( ReadOptions opt , byte [ ] key ) throws RocksDBException {
public byte [ ] get ( ReadOptions opt , byte [ ] key ) throws RocksDBException {
return get ( nativeHandle_ , opt . nativeHandle_ , key , key . length ) ;
return get ( nativeHandle_ , opt . nativeHandle_ , key , key . length ) ;
}
}
/ * *
* The simplified version of get which returns a new byte array storing
* the value associated with the specified input key if any . null will be
* returned if the specified key is not found .
*
* @param columnFamilyHandle { @link org . rocksdb . ColumnFamilyHandle }
* instance
* @param key the key retrieve the value .
* @param opt Read options .
* @return a byte array storing the value associated with the input key if
* any . null if it does not find the specified key .
*
* @throws RocksDBException
* /
public byte [ ] get ( ColumnFamilyHandle columnFamilyHandle , ReadOptions opt ,
byte [ ] key ) throws RocksDBException {
return get ( nativeHandle_ , opt . nativeHandle_ , key , key . length ,
columnFamilyHandle . nativeHandle_ ) ;
}
/ * *
/ * *
* Returns a map of keys for which values were found in DB .
* Returns a map of keys for which values were found in DB .
*
*
@ -252,7 +629,7 @@ public class RocksDB extends RocksObject {
* @return Map where key of map is the key passed by user and value for map
* @return Map where key of map is the key passed by user and value for map
* entry is the corresponding value in DB .
* entry is the corresponding value in DB .
*
*
* @see RocksDBException
* @throw s RocksDBException
* /
* /
public Map < byte [ ] , byte [ ] > multiGet ( List < byte [ ] > keys )
public Map < byte [ ] , byte [ ] > multiGet ( List < byte [ ] > keys )
throws RocksDBException {
throws RocksDBException {
@ -273,6 +650,43 @@ public class RocksDB extends RocksObject {
return keyValueMap ;
return keyValueMap ;
}
}
/ * *
* Returns a map of keys for which values were found in DB .
* < p >
* Note : Every key needs to have a related column family name in
* { @code columnFamilyHandleList } .
* < / p >
*
* @param columnFamilyHandleList { @link java . util . List } containing
* { @link org . rocksdb . ColumnFamilyHandle } instances .
* @param keys List of keys for which values need to be retrieved .
* @return Map where key of map is the key passed by user and value for map
* entry is the corresponding value in DB .
*
* @throws RocksDBException
* @throws IllegalArgumentException
* /
public Map < byte [ ] , byte [ ] > multiGet ( List < ColumnFamilyHandle > columnFamilyHandleList ,
List < byte [ ] > keys ) throws RocksDBException , IllegalArgumentException {
assert ( keys . size ( ) ! = 0 ) ;
// Check if key size equals cfList size. If not a exception must be
// thrown. If not a Segmentation fault happens.
if ( keys . size ( ) ! = columnFamilyHandleList . size ( ) ) {
throw new IllegalArgumentException (
"For each key there must be a ColumnFamilyHandle." ) ;
}
List < byte [ ] > values = multiGet ( nativeHandle_ , keys , keys . size ( ) ,
columnFamilyHandleList ) ;
Map < byte [ ] , byte [ ] > keyValueMap = new HashMap < byte [ ] , byte [ ] > ( ) ;
for ( int i = 0 ; i < values . size ( ) ; i + + ) {
if ( values . get ( i ) = = null ) {
continue ;
}
keyValueMap . put ( keys . get ( i ) , values . get ( i ) ) ;
}
return keyValueMap ;
}
/ * *
/ * *
* Returns a map of keys for which values were found in DB .
* Returns a map of keys for which values were found in DB .
@ -282,7 +696,7 @@ public class RocksDB extends RocksObject {
* @return Map where key of map is the key passed by user and value for map
* @return Map where key of map is the key passed by user and value for map
* entry is the corresponding value in DB .
* entry is the corresponding value in DB .
*
*
* @see RocksDBException
* @throw s RocksDBException
* /
* /
public Map < byte [ ] , byte [ ] > multiGet ( ReadOptions opt , List < byte [ ] > keys )
public Map < byte [ ] , byte [ ] > multiGet ( ReadOptions opt , List < byte [ ] > keys )
throws RocksDBException {
throws RocksDBException {
@ -303,10 +717,56 @@ public class RocksDB extends RocksObject {
return keyValueMap ;
return keyValueMap ;
}
}
/ * *
* Returns a map of keys for which values were found in DB .
* < p >
* Note : Every key needs to have a related column family name in
* { @code columnFamilyHandleList } .
* < / p >
*
* @param opt Read options .
* @param columnFamilyHandleList { @link java . util . List } containing
* { @link org . rocksdb . ColumnFamilyHandle } instances .
* @param keys of keys for which values need to be retrieved .
* @return Map where key of map is the key passed by user and value for map
* entry is the corresponding value in DB .
*
* @throws RocksDBException
* @throws java . lang . IllegalArgumentException
* /
public Map < byte [ ] , byte [ ] > multiGet ( ReadOptions opt ,
List < ColumnFamilyHandle > columnFamilyHandleList , List < byte [ ] > keys )
throws RocksDBException {
assert ( keys . size ( ) ! = 0 ) ;
// Check if key size equals cfList size. If not a exception must be
// thrown. If not a Segmentation fault happens.
if ( keys . size ( ) ! = columnFamilyHandleList . size ( ) ) {
throw new IllegalArgumentException (
"For each key there must be a ColumnFamilyHandle." ) ;
}
List < byte [ ] > values = multiGet ( nativeHandle_ , opt . nativeHandle_ ,
keys , keys . size ( ) , columnFamilyHandleList ) ;
Map < byte [ ] , byte [ ] > keyValueMap = new HashMap < byte [ ] , byte [ ] > ( ) ;
for ( int i = 0 ; i < values . size ( ) ; i + + ) {
if ( values . get ( i ) = = null ) {
continue ;
}
keyValueMap . put ( keys . get ( i ) , values . get ( i ) ) ;
}
return keyValueMap ;
}
/ * *
/ * *
* Remove the database entry ( if any ) for "key" . Returns OK on
* Remove the database entry ( if any ) for "key" . Returns OK on
* success , and a non - OK status on error . It is not an error if "key"
* success , and a non - OK status on error . It is not an error if "key"
* did not exist in the database .
* did not exist in the database .
*
* @param key Key to delete within database
*
* @throws RocksDBException
* /
* /
public void remove ( byte [ ] key ) throws RocksDBException {
public void remove ( byte [ ] key ) throws RocksDBException {
remove ( nativeHandle_ , key , key . length ) ;
remove ( nativeHandle_ , key , key . length ) ;
@ -316,6 +776,27 @@ public class RocksDB extends RocksObject {
* Remove the database entry ( if any ) for "key" . Returns OK on
* Remove the database entry ( if any ) for "key" . Returns OK on
* success , and a non - OK status on error . It is not an error if "key"
* success , and a non - OK status on error . It is not an error if "key"
* did not exist in the database .
* did not exist in the database .
*
* @param columnFamilyHandle { @link org . rocksdb . ColumnFamilyHandle }
* instance
* @param key Key to delete within database
*
* @throws RocksDBException
* /
public void remove ( ColumnFamilyHandle columnFamilyHandle , byte [ ] key )
throws RocksDBException {
remove ( nativeHandle_ , key , key . length , columnFamilyHandle . nativeHandle_ ) ;
}
/ * *
* Remove the database entry ( if any ) for "key" . Returns OK on
* success , and a non - OK status on error . It is not an error if "key"
* did not exist in the database .
*
* @param writeOpt WriteOptions to be used with delete operation
* @param key Key to delete within database
*
* @throws RocksDBException
* /
* /
public void remove ( WriteOptions writeOpt , byte [ ] key )
public void remove ( WriteOptions writeOpt , byte [ ] key )
throws RocksDBException {
throws RocksDBException {
@ -323,20 +804,74 @@ public class RocksDB extends RocksObject {
}
}
/ * *
/ * *
* DB implementations can export properties about their state
* Remove the database entry ( if any ) for "key" . Returns OK on
via this method . If "property" is a valid property understood by this
* success , and a non - OK status on error . It is not an error if "key"
DB implementation , fills "*value" with its current value and returns
* did not exist in the database .
true . Otherwise returns false .
*
* @param columnFamilyHandle { @link org . rocksdb . ColumnFamilyHandle }
* instance
* @param writeOpt WriteOptions to be used with delete operation
* @param key Key to delete within database
*
* @throws RocksDBException
* /
public void remove ( ColumnFamilyHandle columnFamilyHandle , WriteOptions writeOpt ,
byte [ ] key ) throws RocksDBException {
remove ( nativeHandle_ , writeOpt . nativeHandle_ , key , key . length ,
columnFamilyHandle . nativeHandle_ ) ;
}
Valid property names include :
/ * *
* DB implements can export properties about their state
* via this method on a per column family level .
*
* < p > If { @code property } is a valid property understood by this DB
* implementation , fills { @code value } with its current value and
* returns true . Otherwise returns false . < / p >
*
* < p > Valid property names include :
* < ul >
* < li > "rocksdb.num-files-at-level<N>" - return the number of files at level < N > ,
* where < N > is an ASCII representation of a level number ( e . g . "0" ) . < / li >
* < li > "rocksdb.stats" - returns a multi - line string that describes statistics
* about the internal operation of the DB . < / li >
* < li > "rocksdb.sstables" - returns a multi - line string that describes all
* of the sstables that make up the db contents . < / li >
* < / ul > < / p >
*
* @param columnFamilyHandle { @link org . rocksdb . ColumnFamilyHandle }
* instance
* @param property to be fetched . See above for examples
* @return property value
*
* @throws RocksDBException
* /
public String getProperty ( ColumnFamilyHandle columnFamilyHandle , String property )
throws RocksDBException {
return getProperty0 ( nativeHandle_ , columnFamilyHandle . nativeHandle_ , property ,
property . length ( ) ) ;
}
"rocksdb.num-files-at-level<N>" - return the number of files at level < N > ,
/ * *
where < N > is an ASCII representation of a level number ( e . g . "0" ) .
* DB implementations can export properties about their state
"rocksdb.stats" - returns a multi - line string that describes statistics
* via this method . If "property" is a valid property understood by this
about the internal operation of the DB .
* DB implementation , fills "*value" with its current value and returns
"rocksdb.sstables" - returns a multi - line string that describes all
* true . Otherwise returns false .
of the sstables that make up the db contents .
*
* < p > Valid property names include :
* < ul >
* < li > "rocksdb.num-files-at-level<N>" - return the number of files at level < N > ,
* where < N > is an ASCII representation of a level number ( e . g . "0" ) . < / li >
* < li > "rocksdb.stats" - returns a multi - line string that describes statistics
* about the internal operation of the DB . < / li >
* < li > "rocksdb.sstables" - returns a multi - line string that describes all
* of the sstables that make up the db contents . < / li >
* < / ul > < / p >
*
* @param property to be fetched . See above for examples
* @return property value
*
* @throws RocksDBException
* /
* /
public String getProperty ( String property ) throws RocksDBException {
public String getProperty ( String property ) throws RocksDBException {
return getProperty0 ( nativeHandle_ , property , property . length ( ) ) ;
return getProperty0 ( nativeHandle_ , property , property . length ( ) ) ;
@ -356,6 +891,77 @@ public class RocksDB extends RocksObject {
return new RocksIterator ( iterator0 ( nativeHandle_ ) ) ;
return new RocksIterator ( iterator0 ( nativeHandle_ ) ) ;
}
}
/ * *
* Return a heap - allocated iterator over the contents of the database .
* The result of newIterator ( ) is initially invalid ( caller must
* call one of the Seek methods on the iterator before using it ) .
*
* Caller should close the iterator when it is no longer needed .
* The returned iterator should be closed before this db is closed .
*
* @param columnFamilyHandle { @link org . rocksdb . ColumnFamilyHandle }
* instance
* @return instance of iterator object .
* /
public RocksIterator newIterator ( ColumnFamilyHandle columnFamilyHandle ) {
return new RocksIterator ( iterator0 ( nativeHandle_ , columnFamilyHandle . nativeHandle_ ) ) ;
}
/ * *
* Returns iterators from a consistent database state across multiple
* column families . Iterators are heap allocated and need to be deleted
* before the db is deleted
*
* @param columnFamilyHandleList { @link java . util . List } containing
* { @link org . rocksdb . ColumnFamilyHandle } instances .
* @return { @link java . util . List } containing { @link org . rocksdb . RocksIterator }
* instances
*
* @throws RocksDBException
* /
public List < RocksIterator > newIterators (
List < ColumnFamilyHandle > columnFamilyHandleList ) throws RocksDBException {
List < RocksIterator > iterators =
new ArrayList < RocksIterator > ( columnFamilyHandleList . size ( ) ) ;
long [ ] iteratorRefs = iterators ( nativeHandle_ , columnFamilyHandleList ) ;
for ( int i = 0 ; i < columnFamilyHandleList . size ( ) ; i + + ) {
iterators . add ( new RocksIterator ( iteratorRefs [ i ] ) ) ;
}
return iterators ;
}
/ * *
* Creates a new column family with the name columnFamilyName and
* allocates a ColumnFamilyHandle within an internal structure .
* The ColumnFamilyHandle is automatically disposed with DB disposal .
*
* @param columnFamilyName Name of column family to be created .
* @return { @link org . rocksdb . ColumnFamilyHandle } instance
* @see RocksDBException
* /
public ColumnFamilyHandle createColumnFamily ( String columnFamilyName )
throws RocksDBException {
return new ColumnFamilyHandle ( createColumnFamily ( nativeHandle_ ,
columnFamilyName ) ) ;
}
/ * *
* Drops the column family identified by columnFamilyName . Internal
* handles to this column family will be disposed . If the column family
* is not known removal will fail .
*
* @param columnFamilyHandle { @link org . rocksdb . ColumnFamilyHandle }
* instance
*
* @throws RocksDBException
* /
public void dropColumnFamily ( ColumnFamilyHandle columnFamilyHandle )
throws RocksDBException , IllegalArgumentException {
// throws RocksDBException if something goes wrong
dropColumnFamily ( nativeHandle_ , columnFamilyHandle . nativeHandle_ ) ;
}
/ * *
/ * *
* Private constructor .
* Private constructor .
* /
* /
@ -366,39 +972,90 @@ public class RocksDB extends RocksObject {
// native methods
// native methods
protected native void open (
protected native void open (
long optionsHandle , String path ) throws RocksDBException ;
long optionsHandle , String path ) throws RocksDBException ;
protected native List < Long > open ( long optionsHandle , String path ,
List < String > columnFamilyNames , int columnFamilyNamesLength )
throws RocksDBException ;
protected native static List < byte [ ] > listColumnFamilies (
long optionsHandle , String path ) throws RocksDBException ;
protected native void openROnly (
long optionsHandle , String path ) throws RocksDBException ;
protected native List < Long > openROnly (
long optionsHandle , String path , List < String > columnFamilyNames ,
int columnFamilyNamesLength ) throws RocksDBException ;
protected native void put (
protected native void put (
long handle , byte [ ] key , int keyLen ,
long handle , byte [ ] key , int keyLen ,
byte [ ] value , int valueLen ) throws RocksDBException ;
byte [ ] value , int valueLen ) throws RocksDBException ;
protected native void put (
long handle , byte [ ] key , int keyLen ,
byte [ ] value , int valueLen , long cfHandle ) throws RocksDBException ;
protected native void put (
protected native void put (
long handle , long writeOptHandle ,
long handle , long writeOptHandle ,
byte [ ] key , int keyLen ,
byte [ ] key , int keyLen ,
byte [ ] value , int valueLen ) throws RocksDBException ;
byte [ ] value , int valueLen ) throws RocksDBException ;
protected native void put (
long handle , long writeOptHandle ,
byte [ ] key , int keyLen ,
byte [ ] value , int valueLen , long cfHandle ) throws RocksDBException ;
protected native void write (
protected native void write (
long writeOptHandle , long batchHandle ) throws RocksDBException ;
long writeOptHandle , long batchHandle ) throws RocksDBException ;
protected native boolean keyMayExist ( byte [ ] key , int keyLen ,
long cfHandle , StringBuffer stringBuffer ) ;
protected native boolean keyMayExist ( long optionsHandle , byte [ ] key , int keyLen ,
long cfHandle , StringBuffer stringBuffer ) ;
protected native int get (
protected native int get (
long handle , byte [ ] key , int keyLen ,
long handle , byte [ ] key , int keyLen ,
byte [ ] value , int valueLen ) throws RocksDBException ;
byte [ ] value , int valueLen ) throws RocksDBException ;
protected native int get (
long handle , byte [ ] key , int keyLen ,
byte [ ] value , int valueLen , long cfHandle ) throws RocksDBException ;
protected native int get (
protected native int get (
long handle , long readOptHandle , byte [ ] key , int keyLen ,
long handle , long readOptHandle , byte [ ] key , int keyLen ,
byte [ ] value , int valueLen ) throws RocksDBException ;
byte [ ] value , int valueLen ) throws RocksDBException ;
protected native int get (
long handle , long readOptHandle , byte [ ] key , int keyLen ,
byte [ ] value , int valueLen , long cfHandle ) throws RocksDBException ;
protected native List < byte [ ] > multiGet (
protected native List < byte [ ] > multiGet (
long dbHandle , List < byte [ ] > keys , int keysCount ) ;
long dbHandle , List < byte [ ] > keys , int keysCount ) ;
protected native List < byte [ ] > multiGet (
long dbHandle , List < byte [ ] > keys , int keysCount , List < ColumnFamilyHandle >
cfHandles ) ;
protected native List < byte [ ] > multiGet (
protected native List < byte [ ] > multiGet (
long dbHandle , long rOptHandle , List < byte [ ] > keys , int keysCount ) ;
long dbHandle , long rOptHandle , List < byte [ ] > keys , int keysCount ) ;
protected native List < byte [ ] > multiGet (
long dbHandle , long rOptHandle , List < byte [ ] > keys , int keysCount ,
List < ColumnFamilyHandle > cfHandles ) ;
protected native byte [ ] get (
protected native byte [ ] get (
long handle , byte [ ] key , int keyLen ) throws RocksDBException ;
long handle , byte [ ] key , int keyLen ) throws RocksDBException ;
protected native byte [ ] get (
long handle , byte [ ] key , int keyLen , long cfHandle ) throws RocksDBException ;
protected native byte [ ] get (
protected native byte [ ] get (
long handle , long readOptHandle ,
long handle , long readOptHandle ,
byte [ ] key , int keyLen ) throws RocksDBException ;
byte [ ] key , int keyLen ) throws RocksDBException ;
protected native byte [ ] get (
long handle , long readOptHandle ,
byte [ ] key , int keyLen , long cfHandle ) throws RocksDBException ;
protected native void remove (
protected native void remove (
long handle , byte [ ] key , int keyLen ) throws RocksDBException ;
long handle , byte [ ] key , int keyLen ) throws RocksDBException ;
protected native void remove (
long handle , byte [ ] key , int keyLen , long cfHandle ) throws RocksDBException ;
protected native void remove (
protected native void remove (
long handle , long writeOptHandle ,
long handle , long writeOptHandle ,
byte [ ] key , int keyLen ) throws RocksDBException ;
byte [ ] key , int keyLen ) throws RocksDBException ;
protected native void remove (
long handle , long writeOptHandle ,
byte [ ] key , int keyLen , long cfHandle ) throws RocksDBException ;
protected native String getProperty0 ( long nativeHandle ,
protected native String getProperty0 ( long nativeHandle ,
String property , int propertyLength ) throws RocksDBException ;
String property , int propertyLength ) throws RocksDBException ;
protected native long iterator0 ( long optHandle ) ;
protected native String getProperty0 ( long nativeHandle , long cfHandle ,
String property , int propertyLength ) throws RocksDBException ;
protected native long iterator0 ( long handle ) ;
protected native long iterator0 ( long handle , long cfHandle ) ;
protected native long [ ] iterators ( long handle ,
List < ColumnFamilyHandle > columnFamilyNames ) throws RocksDBException ;
private native void disposeInternal ( long handle ) ;
private native void disposeInternal ( long handle ) ;
private native long createColumnFamily ( long handle , String name ) throws RocksDBException ;
private native void dropColumnFamily ( long handle , long cfHandle ) throws RocksDBException ;
protected Options options_ ;
protected Options options_ ;
}
}