public class K2hash extends Object implements Closeable
K2hash encapsulates the K2hashLibrary class 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;
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());
}
}
}
| Modifier and Type | Class and Description |
|---|---|
static class |
K2hash.OPEN_MODE
Defines access modes used to open a file.
|
static class |
K2hash.STATS_DUMP_LEVEL
Defines levels used to dump a k2hash data.
|
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_CMASK_BITS
Default key collision mask bits.
|
static boolean |
DEFAULT_IS_FULLMAP
The library maps the whole file to memory using mmap.
|
static boolean |
DEFAULT_IS_READONLY
The library opens a k2hash file with read only access.
|
static boolean |
DEFAULT_IS_REMOVE_FILE
The library doesn't remove a k2hash file when no process has attached it.
|
static int |
DEFAULT_MASK_BITS
Default key mask bits.
|
static int |
DEFAULT_MAX_ELEMENT
Default maximum elements of a collision key.
|
static int |
DEFAULT_PAGE_SIZE
Default page size.
|
static long |
K2H_INVALID_HANDLE
Default k2hash data handle.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
addAttributePluginLib(String path)
Adds a shared library that handles an attribute.
|
boolean |
addDecryptionPassword(String password)
Adds a passphrase to decrypt a value.
|
boolean |
addSubkey(String key,
String subkey)
Adds a subkey to a key.
|
boolean |
addSubkeys(String key,
String[] newKeys)
Adds subkeys to a key.
|
boolean |
beginTx(String txFile)
Starts a transaction.
|
boolean |
beginTx(String txFile,
String prefix,
String param,
long expirationDuration)
Starts a transaction logging.
|
void |
close()
Closes a k2h file.
|
static boolean |
create(String pathname)
Creates a k2hash file.
|
static boolean |
create(String pathname,
int maskBit,
int cmaskBit,
int maxElement,
int pageSize)
Creates a k2hash file.
|
boolean |
dumpToFile(String path)
Dumps k2hash data to a file.
|
boolean |
dumpToFile(String path,
boolean isSkipError)
Dumps data to a file.
|
boolean |
enableEncryption(boolean enable)
Enables a feature to encrypt a value.
|
boolean |
enableHistory(boolean enable)
Enables a feature to record a key modification history.
|
boolean |
enableMtime(boolean enable)
Enables a feature to record value modification time.
|
String |
getAttribute(String key,
String attribute)
Gets an attribute of a key.
|
Map<String,String> |
getAttributes(String key)
Gets attributes of a key.
|
static CLibrary |
getCLibrary()
Creates a C Library instance.
|
long |
getHandle()
Returns a k2hash data handle.
|
static ax.antpick.k2hash.K2hashLibrary |
getLibrary()
Creates a K2hashLibrary instance.
|
List<String> |
getSubkeys(String key)
Gets keys of subkeys of a key.
|
int |
getTxFileFd()
Gets a transaction log file descriptor.
|
int |
getTxPoolSize()
Gets the number of transaction thread pool.
|
byte[] |
getValue(byte[] key,
String pass)
Gets the value of a key in binary format.
|
String |
getValue(String key)
Gets the value of a key.
|
String |
getValue(String key,
String pass)
Gets the value of a key.
|
boolean |
loadFromFile(String path)
Loads data from a file.
|
boolean |
loadFromFile(String path,
boolean isSkipError)
Loads data from a file.
|
static K2hash |
of(String pathname)
Creates a k2hash instance.
|
static K2hash |
of(String pathname,
K2hash.OPEN_MODE mode)
Creates a k2hash instance.
|
void |
printAttributePlugins()
Prints attribute plugins to stderr.
|
void |
printAttributes()
Prints attributes to stderr.
|
void |
printDataStats()
Prints data statistics.
|
void |
printTableStats(K2hash.STATS_DUMP_LEVEL level)
Prints k2hash key table information.
|
boolean |
remove(String key)
Removes a key.
|
boolean |
remove(String key,
boolean removeAllSubkeys)
Removes a key.
|
boolean |
remove(String key,
String subkey)
Removes a key from a subkey list and removes the subkey itself.
|
boolean |
rename(String key,
String newkey)
Renames a key with a new key.
|
boolean |
setAttribute(String key,
String attrName,
String attrVal)
Sets an attribute of a key.
|
boolean |
setDefaultEncryptionPassword(String password)
Sets the default encryption passphrase.
|
boolean |
setEncryptionPasswordFile(String path)
Sets the data encryption password file.
|
boolean |
setExpirationDuration(int duration,
TimeUnit unit)
Sets the duration to expire a value.
|
boolean |
setSubkey(String key,
String subkey)
Sets a subkey of a key.
|
boolean |
setSubkeys(String key,
String[] subkeys)
Sets subkeys.
|
boolean |
setTxPoolSize(int txPoolSize)
Sets the number of transaction thread pool.
|
boolean |
setValue(String key,
String val)
Sets a key with a value.
|
boolean |
setValue(String key,
String val,
String password,
long duration,
TimeUnit unit)
Sets a key with a value.
|
boolean |
stopTx()
Stops a transaction logging.
|
String |
toString()
Returns full of members as a string.
|
void |
version()
Prints version information.
|
public static final long K2H_INVALID_HANDLE
public static final boolean DEFAULT_IS_FULLMAP
public static final boolean DEFAULT_IS_READONLY
public static final boolean DEFAULT_IS_REMOVE_FILE
public static final int DEFAULT_MASK_BITS
public static final int DEFAULT_CMASK_BITS
public static final int DEFAULT_MAX_ELEMENT
public static final int DEFAULT_PAGE_SIZE
public String toString()
public static ax.antpick.k2hash.K2hashLibrary getLibrary()
throws IOException
IOException - if failed to load the k2hash librarypublic static CLibrary getCLibrary() throws IOException
IOException - if failed to load the C librarypublic static K2hash of(String pathname) throws IOException
pathname - a k2hash file path stringIllegalArgumentException - if pathname is nullIOException - if creating a k2hash file failedpublic static K2hash of(String pathname, K2hash.OPEN_MODE mode) throws IOException
pathname - a k2hash file path stringmode - an access mode to open a fileIllegalArgumentException - if pathname is nullIOException - if creating a k2hash file failedpublic static boolean create(String pathname)
pathname - a k2hash file path stringtrue if success false otherwiseIllegalArgumentException - if an illegal augment existspublic static boolean create(String pathname, int maskBit, int cmaskBit, int maxElement, int pageSize)
pathname - a k2hash file path stringmaskBit - key mask bitscmaskBit - key collision mask bitsmaxElement - maximum elements of a collision keypageSize - page sizetrue if success false otherwiseIllegalArgumentException - if an illegal augment existspublic long getHandle()
public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOExceptionpublic boolean beginTx(String txFile)
txFile - a transaction log file path stringtrue if success false otherwiseIllegalArgumentException - if an illegal augment existspublic boolean beginTx(String txFile, String prefix, String param, long expirationDuration)
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)true if success false otherwiseIllegalArgumentException - if an illegal augment existspublic int getTxFileFd()
public boolean stopTx()
true if success false otherwisepublic int getTxPoolSize()
public boolean setTxPoolSize(int txPoolSize)
txPoolSize - the number of threadstrue if success false otherwiseIllegalArgumentException - if an illegal augment existspublic boolean dumpToFile(String path)
path - a path string to dump data totrue if success false otherwiseIllegalArgumentException - if an illegal augment existspublic boolean dumpToFile(String path, boolean isSkipError)
path - a path string to dump data toisSkipError - true if a dump process continues on errorstrue if success false otherwiseIllegalArgumentException - if an illegal augment existspublic boolean loadFromFile(String path) throws IOException
path - a path string to load data fromtrue if success false otherwiseIllegalArgumentException - if an illegal augment existsIOException - if a path doesn't existpublic boolean loadFromFile(String path, boolean isSkipError) throws IOException
path - a path string to load data fromisSkipError - true if a dump process continues on errorstrue if success false otherwiseIllegalArgumentException - if an illegal augment existsIOException - if a path doesn't existpublic boolean enableMtime(boolean enable)
enable - true if enable false if disable null set
K2H_ATTR_DEFAULTtrue if success false otherwisepublic boolean enableEncryption(boolean enable)
enable - true if enable false if disable null set
K2H_ATTR_DEFAULTtrue if success false otherwisepublic boolean setEncryptionPasswordFile(String path) throws IOException
path - file string that contains passphrase. null for initializing the
current value.true if success false otherwiseIllegalArgumentException - if an illegal augment existsIOException - if a path doesn't existpublic boolean enableHistory(boolean enable)
enable - true if enable false if disable null set
K2H_ATTR_DEFAULTtrue if success false otherwisepublic boolean setExpirationDuration(int duration,
TimeUnit unit)
duration - the duration to expire a value in second null set K2H_ATTR_DEFAULTunit - unit of duration. 0 for initializing the current value.true if success false otherwiseIllegalArgumentException - if an illegal augment existspublic boolean addDecryptionPassword(String password)
password - a passphrase string to decrypt a valuetrue if success false otherwiseIllegalArgumentException - if an illegal augment existspublic boolean setDefaultEncryptionPassword(String password)
password - a passphrase string to encrypt a valuetrue if success false otherwiseIllegalArgumentException - if an illegal augment existspublic boolean addAttributePluginLib(String path) throws IOException
path - a path string to the librarytrue if success false otherwiseIllegalArgumentException - if an illegal augment existsIOException - if a path does't existpublic void printAttributePlugins()
public void printAttributes()
public String getValue(String key)
key - a key string to retrieve the valueIllegalArgumentException - if an illegal augment existspublic String getValue(String key, String pass)
The 2nd parameter is a password to decrypt the value. The value is same with either the 2nd
augment of the setValue or the 1st augment of the setDefaultEncryptionPassword.
key - a key string to retrieve the valuepass - a passphrase string to decrypt the valueIllegalArgumentException - if an illegal augment existspublic byte[] getValue(byte[] key,
String pass)
The 2nd parameter is a password to decrypt the value. The value is same with either the 2nd
augment of the setValue or the 1st augment of the setDefaultEncryptionPassword.
Note: TODO add a test.
key - a key string to retrieve the valuepass - a passphrase string to decrypt the valueIllegalArgumentException - if an illegal augment existspublic List<String> getSubkeys(String key)
key - a key string to retrieve the valueIllegalArgumentException - if an illegal augment existspublic boolean setSubkey(String key, String subkey)
key - a key stringsubkey - the value of a subkeytrue if succeeded. false otherwise.IllegalArgumentException - if an illegal augment existspublic boolean setSubkeys(String key, String[] subkeys)
key - a key stringsubkeys - an array of subkeystrue if succeeded. false otherwise.IllegalArgumentException - if an illegal augment existspublic boolean addSubkey(String key, String subkey)
key - a key stringsubkey - a subkey stringtrue if succeeded. false otherwise.IllegalArgumentException - if an illegal augment existspublic boolean addSubkeys(String key, String[] newKeys)
key - a key stringnewKeys - an array of subkey stringtrue if succeeded. false otherwise.IllegalArgumentException - if an illegal augment existspublic boolean setAttribute(String key, String attrName, String attrVal)
key - a key stringattrName - an attribute nameattrVal - an attribute valuetrue if succeeded. false otherwise.IllegalArgumentException - if an illegal augment existspublic Map<String,String> getAttributes(String key)
key - a key string to retrieve the valueIllegalArgumentException - if an illegal augment existspublic String getAttribute(String key, String attribute)
key - a key stringattribute - an attribute name of the keyIllegalArgumentException - if an illegal augment existspublic boolean setValue(String key, String val)
key - a key stringval - the value of the keytrue if succeeded. false otherwise.IllegalArgumentException - if an illegal augment existspublic boolean setValue(String key, String val, String password, long duration, TimeUnit unit)
key - the key stringval - the value of the keypassword - the passwordduration - duration to expireunit - unit of durationtrue if succeeded. false otherwise.IllegalArgumentException - if an illegal augment existspublic boolean remove(String key)
key - a key stringtrue if success false otherwiseIllegalArgumentException - if an illegal augment existspublic boolean remove(String key, boolean removeAllSubkeys)
key - a key stringremoveAllSubkeys - true if removes all subkeystrue if success false otherwiseIllegalArgumentException - if an illegal augment existspublic boolean remove(String key, String subkey)
Note: TODO add a test.
key - a key stringsubkey - a subkey stringtrue if success false otherwiseIllegalArgumentException - if an illegal augment existspublic boolean rename(String key, String newkey)
key - a key stringnewkey - a subkey stringtrue if success false otherwiseIllegalArgumentException - if an illegal augment existspublic void printTableStats(K2hash.STATS_DUMP_LEVEL level)
level - level of details of dump table informationpublic void printDataStats()
public void version()
Copyright © 2021. All rights reserved.