Package tkrzw
Class AsyncDBM
Object
AsyncDBM
Asynchronous database manager adapter.
- Note:
- This class is a wrapper of DBM for asynchronous operations. A task queue with a thread pool is used inside. Every method except for the constructor and the destructor is run by a thread in the thread pool and the result is set in the future oject of the return value. The caller can ignore the future object if it is not necessary. The destruct method waits for all tasks to be done. Therefore, the destructor should be called before the database is closed.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionappend
(byte[] key, byte[] value, byte[] delim) Appends data at the end of a record of a key.Appends data at the end of a record of a key, with string data.appendMulti
(Map<byte[], byte[]> records, byte[] delim) Appends data to multiple recordsappendMulti
(Map<String, String> records, String delim) Appends data to multiple records, with string data.clear()
Removes all records.compareExchange
(byte[] key, byte[] expected, byte[] desired) Compares the value of a record and exchanges if the condition meets.compareExchange
(String key, String expected, String desired) Compares the value of a record and exchanges if the condition meets, with string data.compareExchangeMulti
(Map<byte[], byte[]> expected, Map<byte[], byte[]> desired) Compares the values of records and exchanges if the condition meets.Compares the values of records and exchanges if the condition meets, with string data.copyFileData
(String destPath, boolean syncHard) Copies the content of the database file to another file.void
destruct()
Destructs the object and releases resources.Exports all records to another database.exportToFlatRecords
(File destFile) Exports all records of a database to a flat record file.Future<Status.And<byte[]>>
get
(byte[] key) Gets the value of a record of a key.Gets the value of a record of a key, with string data.Future<Status.And<Map<byte[],
byte[]>>> getMulti
(byte[][] keys) Gets the values of multiple records of keys.Gets the values of multiple records of keys, with string data.importFromFlatRecords
(File srcFile) Imports records to a database from a flat record file.increment
(byte[] key, long inc, long init) Increments the numeric value of a record.Increments the numeric value of a record, with string data.Future<Status.And<byte[][]>>
popFirst()
Gets the first record and removes it.Future<Status.And<String[]>>
Gets the first record as strings and removes it.pushLast
(byte[] value, double wtime) Adds a record with a key of the current timestamp.Adds a record with a key of the current timestamp.rebuild()
Rebuilds the entire database, without optional parameters.Rebuilds the entire database.rekey
(byte[] oldKey, byte[] newKey, boolean overwrite, boolean copying) Changes the key of a record.Changes the key of a record, with string data.remove
(byte[] key) Removes a record of a key.Removes a record of a key, with string data.removeMulti
(byte[][] keys) Removes records of keys.removeMulti
(String[] keys) Removes records of keys, with string data.Future<Status.And<byte[][]>>
Searches the database and get keys which match a pattern.Future<Status.And<String[]>>
Searches the database and get keys which match a pattern, with string data.set
(byte[] key, byte[] value) Sets a record of a key and a value, with overwriting.set
(byte[] key, byte[] value, boolean overwrite) Sets a record of a key and a value.Sets a record of a key and a value, with string data, with overwriting.Sets a record of a key and a value, with string data.Sets multiple records.setMultiString
(Map<String, String> records, boolean overwrite) Sets multiple records, with string data.synchronize
(boolean hard) Synchronizes the content of the database to the file system.synchronize
(boolean hard, Map<String, String> params) Synchronizes the content of the database to the file system.toString()
Gets a string representation of the database.
-
Constructor Details
-
AsyncDBM
Constructor.- Parameters:
dbm
- A database object which has been opened.numWorkerThreads
- The number of threads in the internal thread pool.
-
-
Method Details
-
destruct
public void destruct()Destructs the object and releases resources.- Note:
- This method waits for all tasks to be done.
-
get
Gets the value of a record of a key.- Parameters:
key
- The key of the record.- Returns:
- The future for the status and the value data of the record. If there's no matching record, NOT_FOUND_ERROR is set.
-
get
Gets the value of a record of a key, with string data.- Parameters:
key
- The key of the record.- Returns:
- The future for the status and the value data of the record. If there's no matching record, NOT_FOUND_ERROR is set.
-
getMulti
Gets the values of multiple records of keys.- Parameters:
keys
- The keys of records to retrieve.- Returns:
- A map of retrieved records. Keys which don't match existing records are ignored.
-
getMulti
Gets the values of multiple records of keys, with string data.- Parameters:
keys
- The keys of records to retrieve.- Returns:
- A map of retrieved records. Keys which don't match existing records are ignored.
-
set
Sets a record of a key and a value.- Parameters:
key
- The key of the record.value
- The value of the record.overwrite
- Whether to overwrite the existing value if there's a record with the same key. If true, the existing value is overwritten by the new value. If false, the operation is given up and an error status is returned.- Returns:
- The future for the result status. If overwriting is abandoned, DUPLICATION_ERROR is set.
-
set
Sets a record of a key and a value, with overwriting.- Parameters:
key
- The key of the record.value
- The value of the record.- Returns:
- The future for the result status.
-
set
Sets a record of a key and a value, with string data.- Parameters:
key
- The key of the record.value
- The value of the record.overwrite
- Whether to overwrite the existing value if there's a record with the same key. If true, the existing value is overwritten by the new value. If false, the operation is given up and an error status is returned.- Returns:
- The future for the result status. If overwriting is abandoned, DUPLICATION_ERROR is set.
-
set
Sets a record of a key and a value, with string data, with overwriting.- Parameters:
key
- The key of the record.value
- The value of the record.- Returns:
- The future for the result status.
-
setMulti
Sets multiple records.- Parameters:
records
- The records to store.overwrite
- Whether to overwrite the existing value if there's a record with the same key. If true, the existing value is overwritten by the new value. If false, the operation is given up and an error status is returned.- Returns:
- The future for the result status. If there are records avoiding overwriting, DUPLICATION_ERROR is set.
-
setMultiString
Sets multiple records, with string data.- Parameters:
records
- The records to store.overwrite
- Whether to overwrite the existing value if there's a record with the same key. If true, the existing value is overwritten by the new value. If false, the operation is given up and an error status is returned.- Returns:
- The future for the result status. If there are records avoiding overwriting, DUPLICATION_ERROR is set.
-
remove
Removes a record of a key.- Parameters:
key
- The key of the record.- Returns:
- The future for the result status. If there's no matching record, NOT_FOUND_ERROR is set.
-
remove
Removes a record of a key, with string data.- Parameters:
key
- The key of the record.- Returns:
- The future for the result status. If there's no matching record, NOT_FOUND_ERROR is set.
-
removeMulti
Removes records of keys.- Parameters:
keys
- The keys of records to remove.- Returns:
- The future for the result status. If there are missing records, NOT_FOUND_ERROR is set.
-
removeMulti
Removes records of keys, with string data.- Parameters:
keys
- The keys of records to remove.- Returns:
- The future for the result status. If there are missing records, NOT_FOUND_ERROR is set.
-
append
Appends data at the end of a record of a key.- Parameters:
key
- The key of the record.value
- The value to append.delim
- The delimiter to put after the existing record.- Returns:
- The future for the result status.
- Note:
- If there's no existing record, the value is set without the delimiter.
-
append
Appends data at the end of a record of a key, with string data.- Parameters:
key
- The key of the record.value
- The value to append.delim
- The delimiter to put after the existing record.- Returns:
- The future for the result status.
- Note:
- If there's no existing record, the value is set without the delimiter.
-
appendMulti
Appends data to multiple records- Parameters:
records
- The records to append.delim
- The delimiter to put after the existing record.- Returns:
- The future for the result status.
- Note:
- If there's no existing record, the value is set without the delimiter.
-
appendMulti
Appends data to multiple records, with string data.- Parameters:
records
- The records to append.delim
- The delimiter to put after the existing record.- Returns:
- The future for the result status.
- Note:
- If there's no existing record, the value is set without the delimiter.
-
compareExchange
Compares the value of a record and exchanges if the condition meets.- Parameters:
key
- The key of the record.expected
- The expected value. If it is null, no existing record is expected. If it is DBM.ANY_BYTES, an existing record with any value is expacted.desired
- The desired value. If it is null, the record is to be removed. If it is DBM.ANY_BYTES, no update is done.- Returns:
- The future for the result status. If the condition doesn't meet, INFEASIBLE_ERROR is set.
-
compareExchange
Compares the value of a record and exchanges if the condition meets, with string data.- Parameters:
key
- The key of the record.expected
- The expected value. If it is null, no existing record is expected. If it is DBM.ANY_STRING, an existing record with any value is expacted.desired
- The desired value. If it is null, the record is to be removed. If it is DBM.ANY_STRING, no update is done.- Returns:
- The future for the result status. If the condition doesn't meet, INFEASIBLE_ERROR is set.
-
increment
Increments the numeric value of a record.- Parameters:
key
- The key of the record.inc
- The incremental value. If it is Long.MIN_VALUE, the current value is not changed and a new record is not created.init
- The initial value.- Returns:
- The future for the result status and the current value.
- Note:
- The record value is stored as an 8-byte big-endian integer. Negative is also supported.
-
increment
Increments the numeric value of a record, with string data.- Parameters:
key
- The key of the record.inc
- The incremental value.init
- The initial value.- Returns:
- The future for the result status and the current value.
- Note:
- The record value is stored as an 8-byte big-endian integer. Negative is also supported.
-
compareExchangeMulti
Compares the values of records and exchanges if the condition meets.- Parameters:
expected
- The record keys and their expected values. If the value is null, no existing record is expected. If the value is DBM.ANY_BYTES, an existing record with any value is expacted.desired
- The record keys and their desired values. If the value is null, the record is to be removed.- Returns:
- The future for the result status. If the condition doesn't meet, INFEASIBLE_ERROR is set.
-
compareExchangeMultiString
public Future<Status> compareExchangeMultiString(Map<String, String> expected, Map<String, String> desired) Compares the values of records and exchanges if the condition meets, with string data.- Parameters:
expected
- The record keys and their expected values. If the data is null, no existing record is expected. If the value is DBM.ANY_STRING, an existing record with any value is expacted.desired
- The record keys and their desired values. If the data is null, the record is to be removed.- Returns:
- The future for the result status. If the condition doesn't meet, INFEASIBLE_ERROR is set.
-
rekey
Changes the key of a record.- Parameters:
oldKey
- The old key of the record.newKey
- The new key of the record.overwrite
- Whether to overwrite the existing record of the new key.copying
- Whether to retain the record of the old key.- Returns:
- The future for the result status. If there's no matching record to the old key, NOT_FOUND_ERROR is set. If the overwrite flag is false and there is an existing record of the new key, DUPLICATION ERROR is set.
- Note:
- This method is done atomically. The other threads observe that the record has either the old key or the new key. No intermediate states are observed.
-
rekey
Changes the key of a record, with string data.- Parameters:
oldKey
- The old key of the record.newKey
- The new key of the record.overwrite
- Whether to overwrite the existing record of the new key.copying
- Whether to retain the record of the old key.- Returns:
- The future for the result status.
-
popFirst
Gets the first record and removes it.- Returns:
- The future for the result status and a pair of the key and the value of the first record.
-
popFirstString
Gets the first record as strings and removes it.- Returns:
- The future for the result status and a pair of the key and the value of the first record.
-
pushLast
Adds a record with a key of the current timestamp.- Parameters:
value
- The value of the record.wtime
- The current wall time used to generate the key. If it is negative, the system clock is used.- Returns:
- The future for the result status.
- Note:
- The key is generated as an 8-bite big-endian binary string of the timestamp. If there is an existing record matching the generated key, the key is regenerated and the attempt is repeated until it succeeds.
-
pushLast
Adds a record with a key of the current timestamp.- Parameters:
value
- The value of the record.wtime
- The current wall time used to generate the key. If it is negative, the system clock is used.- Returns:
- The future for the result status.
- Note:
- The key is generated as an 8-bite big-endian binary string of the timestamp. If there is an existing record matching the generated key, the key is regenerated and the attempt is repeated until it succeeds.
-
clear
Removes all records.- Returns:
- The future for the result status.
-
rebuild
Rebuilds the entire database.- Parameters:
params
- Optional parameters. If it is null, it is ignored.- Returns:
- The future for the result status.
- Note:
- The parameters work in the same way as with DBM::rebuild.
-
rebuild
Rebuilds the entire database, without optional parameters.- Returns:
- The future for the result status.
-
synchronize
Synchronizes the content of the database to the file system.- Parameters:
hard
- True to do physical synchronization with the hardware or false to do only logical synchronization with the file system.params
- Optional parameters. If it is null, it is ignored.- Returns:
- The future for the result status.
- Note:
- The parameters work in the same way as with DBM::synchronize.
-
synchronize
Synchronizes the content of the database to the file system.- Parameters:
hard
- True to do physical synchronization with the hardware or false to do only logical synchronization with the file system.- Returns:
- The future for the result status.
-
copyFileData
Copies the content of the database file to another file.- Parameters:
destPath
- A path to the destination file.syncHard
- True to do physical synchronization with the hardware.- Returns:
- The future for the result status.
-
export
Exports all records to another database.- Parameters:
destDBM
- The destination database. The lefetime of the database object must last until the task finishes.- Returns:
- The future for the result status.
-
exportToFlatRecords
Exports all records of a database to a flat record file.- Parameters:
destFile
- The file object to write records in. The lefetime of the file object must last until the task finishes.- Returns:
- The future for the result status.
- Note:
- A flat record file contains a sequence of binary records without any high level structure so it is useful as a intermediate file for data migration.
-
importFromFlatRecords
Imports records to a database from a flat record file.- Parameters:
srcFile
- The file object to read records from. The lefetime of the file object must last until the task finishes.- Returns:
- The future for the result status.
-
search
Searches the database and get keys which match a pattern.- Parameters:
mode
- The search mode. "contain" extracts keys containing the pattern. "begin" extracts keys beginning with the pattern. "end" extracts keys ending with the pattern. "regex" extracts keys partially matches the pattern of a regular expression. "edit" extracts keys whose edit distance to the pattern is the least. "editbin" extracts keys whose edit distance to the binary pattern is the least.pattern
- The pattern for matching.capacity
- The maximum records to obtain. 0 means unlimited.- Returns:
- The future for the result status and an array of keys matching the condition.
-
search
Searches the database and get keys which match a pattern, with string data.- Parameters:
mode
- The search mode. "contain" extracts keys containing the pattern. "begin" extracts keys beginning with the pattern. "end" extracts keys ending with the pattern. "regex" extracts keys partially matches the pattern of a regular expression. "edit" extracts keys whose edit distance to the UTF-8 pattern is the least. "editbin" extracts keys whose edit distance to the binary pattern is the least.pattern
- The pattern for matching.capacity
- The maximum records to obtain. 0 means unlimited.- Returns:
- The future for the result status and an array of keys matching the condition.
-
toString
Gets a string representation of the database.
-