Package kvd.client
Class KvdClient
java.lang.Object
kvd.client.KvdClient
- All Implemented Interfaces:
AutoCloseable
,KvdOperations
KvdClient
is the public API that clients should use to interact with the server.
Example usage:
try(KvdClient client = new KvdClient("kvd.example.com:3030")) { try(DataOutputStream out = new DataOutputStream(client.put("test"))) { out.writeLong(42); } }All
KvdOperations
methods implemented in this class execute a single operation transaction on the server that
is automatically committed when the operation completes. They partake in resource locking the same way as
KvdTransaction
operations. The main difference between KvdTransaction
operations is that operations
in KvdClient
are auto committed. For manual commit/rollback start a new Transaction with beginTransaction()
Note: KvdClient(java.lang.String)
establishes a single socket connection to the server
that it keeps alive until close()
is called.
Note: KvdClient
is thread-safe.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionSeebeginTransaction(long)
except this method uses the default transaction timeoutbeginTransaction
(long timeoutMs) Begin a new transaction with the specified timeout and waits until the transaction has been created on the server.beginTransactionAsync
(long timeoutMs) Begin a new transaction with the specified timeout.void
close()
Waits for pending requests to finish and closes the connection to the server.containsAsync
(byte[] key) The returnedFuture
evaluates to true if the key exists on the server, false otherwisegetAsync
(byte[] key) Returns the value to which the specified key is mappedlong
Retrieve the default transaction timeout in milliseconds, 0 means no timeout.boolean
isClosed()
Check if the KvdClient can still be used.putAsync
(byte[] key) Put a new value or replace an existing.Remove all keys and values from the database.Removes all keys and values from the database.removeAsync
(byte[] key) Removes the mapping for the specified key from the server.<T> T
withTransaction
(KvdWork<T> work) Execute a new or join an existingKvdTransaction
that has been started either withwithTransaction(KvdWork)
orwithTransactionVoid(KvdVoidWork)
.void
Same aswithTransaction(KvdWork)
except this does not return a result
-
Constructor Details
-
KvdClient
Create a newKvdClient
instance connecting to the server. UseKvdClientBuilder
to create aKvdClient
with non standard options.- Parameters:
serverAddress
- in the formhost:port
. Port can be omitted and 3030 is used in this case.
-
-
Method Details
-
putAsync
Description copied from interface:KvdOperations
Put a new value or replace an existing.- Specified by:
putAsync
in interfaceKvdOperations
- Parameters:
key
- key with which the specified value is to be associated- Returns:
Future
that evaluates either to anOutputStream
to be used to stream the value in. or fails (e.g. on optimistic lock or deadlock). Close theOutputStream
to signal that the value is complete.
-
getAsync
Description copied from interface:KvdOperations
Returns the value to which the specified key is mapped- Specified by:
getAsync
in interfaceKvdOperations
- Parameters:
key
- the key whose associated value is to be returned- Returns:
Future
that evaluates either to anInputStream
for keys that exist ornull
for keys that don't exist on the server.
-
containsAsync
Description copied from interface:KvdOperations
The returnedFuture
evaluates to true if the key exists on the server, false otherwise- Specified by:
containsAsync
in interfaceKvdOperations
- Parameters:
key
- The key whose presence is to be tested- Returns:
Future
evaluates totrue
if the key exists on the server,false
otherwise
-
removeAsync
Description copied from interface:KvdOperations
Removes the mapping for the specified key from the server.- Specified by:
removeAsync
in interfaceKvdOperations
- Parameters:
key
- key whose mapping is to be removed- Returns:
Future
which evaluates totrue
if the key/value was removed from the server,false
otherwise.
-
close
public void close()Waits for pending requests to finish and closes the connection to the server. Once closed this instance can't be reused and must be discarded.- Specified by:
close
in interfaceAutoCloseable
-
isClosed
public boolean isClosed()Check if the KvdClient can still be used.- Returns:
true
if the instance is closed,false
otherwise.
-
beginTransactionAsync
Begin a new transaction with the specified timeout. Also see {beginTransaction(long)
- Parameters:
timeoutMs
- The transaction timeout in milliseconds or 0 for no timeout. If the timeout is exceeded the transaction is aborted (rollback).- Returns:
Future
that evaluates to aKvdTransaction
when the server has created the transaction
-
beginTransaction
Begin a new transaction with the specified timeout and waits until the transaction has been created on the server. Normally this method should be used in a try-with-resource block to make sure the transaction is closed. Note that you have to commit the transaction manually to make changes permanent before the try-with-resource block closes the transaction. Also consider usingwithTransaction(KvdWork)
which automatically commits transactions and also allows outer/inner units of work to share the same transaction to improve code reusability- Parameters:
timeoutMs
- The transaction timeout in milliseconds or 0 for no timeout. If the timeout is exceeded the transaction is aborted (rollback).- Returns:
KvdTransaction
-
beginTransaction
SeebeginTransaction(long)
except this method uses the default transaction timeout- Returns:
KvdTransaction
-
withTransaction
Execute a new or join an existingKvdTransaction
that has been started either withwithTransaction(KvdWork)
orwithTransactionVoid(KvdVoidWork)
. A new transaction is started with the default timeout. The transaction is automatically committed when the most outerKvdWork
finishes or aborted (rollback) when theKvdWork
throws an exceptions.- Type Parameters:
T
- Result type of theKvdWork
- Parameters:
work
- the unit of work to be executed within the transaction- Returns:
- The result of the unit of work
-
withTransactionVoid
Same aswithTransaction(KvdWork)
except this does not return a result- Parameters:
work
- the unit of work to be executed within the transaction
-
getTransactionDefaultTimeoutMs
public long getTransactionDefaultTimeoutMs()Retrieve the default transaction timeout in milliseconds, 0 means no timeout. This value can only be set when theKvdClient
is constructed through theKvdClientBuilder
- Returns:
- The default transaction timeout in milliseconds
-
removeAllAsync
Removes all keys and values from the database.- Returns:
Future
which evaluates totrue
if when all key/values have been removed from the server,false
otherwise.
-
removeAll
Remove all keys and values from the database.
-