Package ax.antpick.k2hash
Class K2hash
- java.lang.Object
-
- ax.antpick.k2hash.K2hash
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class K2hash extends Object implements Closeable
The classK2hashencapsulates theK2hashLibraryclass and represents a K2hash key-value store.Usage Examples. Suppose you want to set a key of "keystring" with a value of "valuestring". You could write this as:
import com.sun.jna.*; import com.sun.jna.ptr.*; import java.io.IOException; import ax.antpick.k2hash.K2hash; public class App { public static void main(String[] args) { try (K2hash db = K2hash.of("App.k2h")) { db.setValue("keystring", "valstring"); System.out.println(db.getValue("keystring")); } catch (IOException ex) { System.out.println(ex.getMessage()); } } }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classK2hash.OPEN_MODEDefines access modes used to open a file.static classK2hash.STATS_DUMP_LEVELDefines levels used to dump a k2hash data.
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_CMASK_BITSDefault key collision mask bits.static booleanDEFAULT_IS_FULLMAPThe library maps the whole file to memory using mmap.static booleanDEFAULT_IS_READONLYThe library opens a k2hash file with read only access.static booleanDEFAULT_IS_REMOVE_FILEThe library doesn't remove a k2hash file when no process has attached it.static intDEFAULT_MASK_BITSDefault key mask bits.static intDEFAULT_MAX_ELEMENTDefault maximum elements of a collision key.static intDEFAULT_PAGE_SIZEDefault page size.static longK2H_INVALID_HANDLEDefault k2hash data handle.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddAttributePluginLib(String path)Adds a shared library that handles an attribute.booleanaddDecryptionPassword(String password)Adds a passphrase to decrypt a value.booleanaddSubkey(String key, String subkey)Adds a subkey to a key.booleanaddSubkeys(String key, String[] newKeys)Adds subkeys to a key.booleanbeginTx(String txFile)Starts a transaction.booleanbeginTx(String txFile, String prefix, String param, long expirationDuration)Starts a transaction logging.voidclose()Closes a k2h file.static booleancreate(String pathname)Creates a k2hash file.static booleancreate(String pathname, int maskBit, int cmaskBit, int maxElement, int pageSize)Creates a k2hash file.booleandumpToFile(String path)Dumps k2hash data to a file.booleandumpToFile(String path, boolean isSkipError)Dumps data to a file.booleanenableEncryption(boolean enable)Enables a feature to encrypt a value.booleanenableHistory(boolean enable)Enables a feature to record a key modification history.booleanenableMtime(boolean enable)Enables a feature to record value modification time.StringgetAttribute(String key, String attribute)Gets an attribute of a key.Map<String,String>getAttributes(String key)Gets attributes of a key.static CLibrarygetCLibrary()Creates a C Library instance.longgetHandle()Returns a k2hash data handle.static ax.antpick.k2hash.K2hashLibrarygetLibrary()Creates a K2hashLibrary instance.List<String>getSubkeys(String key)Gets keys of subkeys of a key.intgetTxFileFd()Gets a transaction log file descriptor.intgetTxPoolSize()Gets the number of transaction thread pool.byte[]getValue(byte[] key, String pass)Gets the value of a key in binary format.StringgetValue(String key)Gets the value of a key.StringgetValue(String key, String pass)Gets the value of a key.booleanloadFromFile(String path)Loads data from a file.booleanloadFromFile(String path, boolean isSkipError)Loads data from a file.static K2hashof(String pathname)Creates a k2hash instance.static K2hashof(String pathname, K2hash.OPEN_MODE mode)Creates a k2hash instance.voidprintAttributePlugins()Prints attribute plugins to stderr.voidprintAttributes()Prints attributes to stderr.voidprintDataStats()Prints data statistics.voidprintTableStats(K2hash.STATS_DUMP_LEVEL level)Prints k2hash key table information.booleanremove(String key)Removes a key.booleanremove(String key, boolean removeAllSubkeys)Removes a key.booleanremove(String key, String subkey)Removes a key from a subkey list and removes the subkey itself.booleanrename(String key, String newkey)Renames a key with a new key.booleansetAttribute(String key, String attrName, String attrVal)Sets an attribute of a key.booleansetDefaultEncryptionPassword(String password)Sets the default encryption passphrase.booleansetEncryptionPasswordFile(String path)Sets the data encryption password file.booleansetExpirationDuration(int duration, TimeUnit unit)Sets the duration to expire a value.booleansetSubkey(String key, String subkey)Sets a subkey of a key.booleansetSubkeys(String key, String[] subkeys)Sets subkeys.booleansetTxPoolSize(int txPoolSize)Sets the number of transaction thread pool.booleansetValue(String key, String val)Sets a key with a value.booleansetValue(String key, String val, String password, long duration, TimeUnit unit)Sets a key with a value.booleanstopTx()Stops a transaction logging.StringtoString()Returns full of members as a string.voidversion()Prints version information.
-
-
-
Field Detail
-
K2H_INVALID_HANDLE
public static final long K2H_INVALID_HANDLE
Default k2hash data handle.- See Also:
- Constant Field Values
-
DEFAULT_IS_FULLMAP
public static final boolean DEFAULT_IS_FULLMAP
The library maps the whole file to memory using mmap.- See Also:
- Constant Field Values
-
DEFAULT_IS_READONLY
public static final boolean DEFAULT_IS_READONLY
The library opens a k2hash file with read only access.- See Also:
- Constant Field Values
-
DEFAULT_IS_REMOVE_FILE
public static final boolean DEFAULT_IS_REMOVE_FILE
The library doesn't remove a k2hash file when no process has attached it.- See Also:
- Constant Field Values
-
DEFAULT_MASK_BITS
public static final int DEFAULT_MASK_BITS
Default key mask bits.- See Also:
- Constant Field Values
-
DEFAULT_CMASK_BITS
public static final int DEFAULT_CMASK_BITS
Default key collision mask bits.- See Also:
- Constant Field Values
-
DEFAULT_MAX_ELEMENT
public static final int DEFAULT_MAX_ELEMENT
Default maximum elements of a collision key.- See Also:
- Constant Field Values
-
DEFAULT_PAGE_SIZE
public static final int DEFAULT_PAGE_SIZE
Default page size.- See Also:
- Constant Field Values
-
-
Method Detail
-
toString
public String toString()
Returns full of members as a string.
-
getLibrary
public static ax.antpick.k2hash.K2hashLibrary getLibrary() throws IOExceptionCreates a K2hashLibrary instance.- Returns:
- a K2hashLibrary instance
- Throws:
IOException- if failed to load the k2hash library
-
getCLibrary
public static CLibrary getCLibrary() throws IOException
Creates a C Library instance.- Returns:
- a C library instance
- Throws:
IOException- if failed to load the C library
-
of
public static K2hash of(String pathname) throws IOException
Creates a k2hash instance.- Parameters:
pathname- a k2hash file path string- Returns:
- a K2hash instance
- Throws:
IllegalArgumentException- if pathname is nullIOException- if creating a k2hash file failed
-
of
public static K2hash of(String pathname, K2hash.OPEN_MODE mode) throws IOException
Creates a k2hash instance.- Parameters:
pathname- a k2hash file path stringmode- an access mode to open a file- Returns:
- a K2hash instance
- Throws:
IllegalArgumentException- if pathname is nullIOException- if creating a k2hash file failed
-
create
public static boolean create(String pathname)
Creates a k2hash file.- Parameters:
pathname- a k2hash file path string- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
create
public static boolean create(String pathname, int maskBit, int cmaskBit, int maxElement, int pageSize)
Creates a k2hash file.- Parameters:
pathname- a k2hash file path stringmaskBit- key mask bitscmaskBit- key collision mask bitsmaxElement- maximum elements of a collision keypageSize- page size- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
getHandle
public long getHandle()
Returns a k2hash data handle.- Returns:
- a k2hash data handle.
-
close
public void close() throws IOExceptionCloses a k2h file.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
beginTx
public boolean beginTx(String txFile)
Starts a transaction.- Parameters:
txFile- a transaction log file path string- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
beginTx
public boolean beginTx(String txFile, String prefix, String param, long expirationDuration)
Starts a transaction logging.- Parameters:
txFile- a transaction log file pathprefix- a prefix of transaction log entryparam- a parameter string to pass to a transaction processing handlerexpirationDuration- transaction log entry expiration(second)- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
getTxFileFd
public int getTxFileFd()
Gets a transaction log file descriptor.- Returns:
- a transaction file descriptor
-
stopTx
public boolean stopTx()
Stops a transaction logging.- Returns:
trueif successfalseotherwise
-
getTxPoolSize
public int getTxPoolSize()
Gets the number of transaction thread pool.- Returns:
- the number of transaction thread pool
-
setTxPoolSize
public boolean setTxPoolSize(int txPoolSize)
Sets the number of transaction thread pool.- Parameters:
txPoolSize- the number of threads- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
dumpToFile
public boolean dumpToFile(String path)
Dumps k2hash data to a file.- Parameters:
path- a path string to dump data to- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
dumpToFile
public boolean dumpToFile(String path, boolean isSkipError)
Dumps data to a file.- Parameters:
path- a path string to dump data toisSkipError- true if a dump process continues on errors- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
loadFromFile
public boolean loadFromFile(String path) throws IOException
Loads data from a file.- Parameters:
path- a path string to load data from- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment existsIOException- if a path doesn't exist
-
loadFromFile
public boolean loadFromFile(String path, boolean isSkipError) throws IOException
Loads data from a file.- Parameters:
path- a path string to load data fromisSkipError- true if a dump process continues on errors- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment existsIOException- if a path doesn't exist
-
enableMtime
public boolean enableMtime(boolean enable)
Enables a feature to record value modification time.- Parameters:
enable-trueif enablefalseif disablenullset K2H_ATTR_DEFAULT- Returns:
trueif successfalseotherwise
-
enableEncryption
public boolean enableEncryption(boolean enable)
Enables a feature to encrypt a value.- Parameters:
enable-trueif enablefalseif disablenullset K2H_ATTR_DEFAULT- Returns:
trueif successfalseotherwise
-
setEncryptionPasswordFile
public boolean setEncryptionPasswordFile(String path) throws IOException
Sets the data encryption password file.- Parameters:
path- file string that contains passphrase.nullfor initializing the current value.- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment existsIOException- if a path doesn't exist
-
enableHistory
public boolean enableHistory(boolean enable)
Enables a feature to record a key modification history.- Parameters:
enable-trueif enablefalseif disablenullset K2H_ATTR_DEFAULT- Returns:
trueif successfalseotherwise
-
setExpirationDuration
public boolean setExpirationDuration(int duration, TimeUnit unit)Sets the duration to expire a value.- Parameters:
duration- the duration to expire a value in secondnullset K2H_ATTR_DEFAULTunit- unit of duration.0for initializing the current value.- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
addDecryptionPassword
public boolean addDecryptionPassword(String password)
Adds a passphrase to decrypt a value.- Parameters:
password- a passphrase string to decrypt a value- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
setDefaultEncryptionPassword
public boolean setDefaultEncryptionPassword(String password)
Sets the default encryption passphrase.- Parameters:
password- a passphrase string to encrypt a value- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
addAttributePluginLib
public boolean addAttributePluginLib(String path) throws IOException
Adds a shared library that handles an attribute.- Parameters:
path- a path string to the library- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment existsIOException- if a path does't exist
-
printAttributePlugins
public void printAttributePlugins()
Prints attribute plugins to stderr.
-
printAttributes
public void printAttributes()
Prints attributes to stderr.
-
getValue
public String getValue(String key)
Gets the value of a key.- Parameters:
key- a key string to retrieve the value- Returns:
- the value of the key
- Throws:
IllegalArgumentException- if an illegal augment exists
-
getValue
public String getValue(String key, String pass)
Gets the value of a key.The 2nd parameter is a password to decrypt the value. The value is same with either the 2nd augment of the
setValue(java.lang.String,java.lang.String)or the 1st augment of thesetDefaultEncryptionPassword(java.lang.String).- Parameters:
key- a key string to retrieve the valuepass- a passphrase string to decrypt the value- Returns:
- value of the key
- Throws:
IllegalArgumentException- if an illegal augment exists
-
getValue
public byte[] getValue(byte[] key, String pass)Gets the value of a key in binary format.The 2nd parameter is a password to decrypt the value. The value is same with either the 2nd augment of the
setValue(java.lang.String,java.lang.String)or the 1st augment of thesetDefaultEncryptionPassword(java.lang.String).Note: TODO add a test.
- Parameters:
key- a key string to retrieve the valuepass- a passphrase string to decrypt the value- Returns:
- value of the key
- Throws:
IllegalArgumentException- if an illegal augment exists
-
getSubkeys
public List<String> getSubkeys(String key)
Gets keys of subkeys of a key. Values of keys are not returned.- Parameters:
key- a key string to retrieve the value- Returns:
- list of subkeys of the key. null if no subkeys exist.
- Throws:
IllegalArgumentException- if an illegal augment exists
-
setSubkey
public boolean setSubkey(String key, String subkey)
Sets a subkey of a key. The current key will be replaces with new one.- Parameters:
key- a key stringsubkey- the value of a subkey- Returns:
trueif succeeded.falseotherwise.- Throws:
IllegalArgumentException- if an illegal augment exists
-
setSubkeys
public boolean setSubkeys(String key, String[] subkeys)
Sets subkeys.- Parameters:
key- a key stringsubkeys- an array of subkeys- Returns:
trueif succeeded.falseotherwise.- Throws:
IllegalArgumentException- if an illegal augment exists
-
addSubkey
public boolean addSubkey(String key, String subkey)
Adds a subkey to a key.- Parameters:
key- a key stringsubkey- a subkey string- Returns:
trueif succeeded.falseotherwise.- Throws:
IllegalArgumentException- if an illegal augment exists
-
addSubkeys
public boolean addSubkeys(String key, String[] newKeys)
Adds subkeys to a key.- Parameters:
key- a key stringnewKeys- an array of subkey string- Returns:
trueif succeeded.falseotherwise.- Throws:
IllegalArgumentException- if an illegal augment exists
-
setAttribute
public boolean setAttribute(String key, String attrName, String attrVal)
Sets an attribute of a key.- Parameters:
key- a key stringattrName- an attribute nameattrVal- an attribute value- Returns:
trueif succeeded.falseotherwise.- Throws:
IllegalArgumentException- if an illegal augment exists
-
getAttributes
public Map<String,String> getAttributes(String key)
Gets attributes of a key.- Parameters:
key- a key string to retrieve the value- Returns:
- attributes of the key
- Throws:
IllegalArgumentException- if an illegal augment exists
-
getAttribute
public String getAttribute(String key, String attribute)
Gets an attribute of a key.- Parameters:
key- a key stringattribute- an attribute name of the key- Returns:
- the value of the attribute name of the key
- Throws:
IllegalArgumentException- if an illegal augment exists
-
setValue
public boolean setValue(String key, String val)
Sets a key with a value.- Parameters:
key- a key stringval- the value of the key- Returns:
trueif succeeded.falseotherwise.- Throws:
IllegalArgumentException- if an illegal augment exists
-
setValue
public boolean setValue(String key, String val, String password, long duration, TimeUnit unit)
Sets a key with a value.- Parameters:
key- the key stringval- the value of the keypassword- the passwordduration- duration to expireunit- unit of duration- Returns:
trueif succeeded.falseotherwise.- Throws:
IllegalArgumentException- if an illegal augment exists
-
remove
public boolean remove(String key)
Removes a key.- Parameters:
key- a key string- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
remove
public boolean remove(String key, boolean removeAllSubkeys)
Removes a key.- Parameters:
key- a key stringremoveAllSubkeys-trueif removes all subkeys- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
remove
public boolean remove(String key, String subkey)
Removes a key from a subkey list and removes the subkey itself.Note: TODO add a test.
- Parameters:
key- a key stringsubkey- a subkey string- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
rename
public boolean rename(String key, String newkey)
Renames a key with a new key.- Parameters:
key- a key stringnewkey- a subkey string- Returns:
trueif successfalseotherwise- Throws:
IllegalArgumentException- if an illegal augment exists
-
printTableStats
public void printTableStats(K2hash.STATS_DUMP_LEVEL level)
Prints k2hash key table information.- Parameters:
level- level of details of dump table information
-
printDataStats
public void printDataStats()
Prints data statistics.
-
version
public void version()
Prints version information.
-
-