public class CasSetCmd extends CmdBase implements Cmd<Boolean>
An Usage Example:
Supposing you want to initialize a variable to zero at first and then you want to change the value to "1", you could write this as:
package com.example;
import ax.antpick.k2hdkc.*;
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import java.io.IOException;
import java.util.*;
import java.util.stream.*;
public class App {
public static void main(String[] args) {
try (Cluster c = Cluster.of("cluster/slave.yaml");
Session s = Session.of(c); ) {
// 1. init
CasInitCmd init = CasInitCmd.of("key", 0);
System.out.println(init.execute(s).get().getValue()); // true
// 2. set
CasSetCmd set = CasSetCmd.of("key", 0, 1); // CasIncDecCmd should be used here.
System.out.println(set.execute(s).get().getValue()); // true
// 3. get
CasGetCmd get = CasGetCmd.of("key", Cmd.DataType.INT);
ByteArrayOutputStream bos = (ByteArrayOutputStream) get.execute(s).get().getValue();
assert (Cmd.getValusAsInt(bos.toByteArray()) == 1);
} catch (IOException ex) {
System.out.println(ex.getMessage());
assert (false);
}
}
}
You shoule see the "true" message twice on the stdout if you could successfully run it. Before running the code above, You should run three processes.
The following commands in this repository will run all processes you need in localhost.
$ cd cluster
$ sh start_server.sh
Cmd.DataType
DEFAULT_CHECK_PARENT_ATTRS, DEFAULT_EXPIRATION_DURATION, DEFAULT_IS_CHECK_PARENT_ATTRS, DEFAULT_IS_CLEAR_SUBKEYS, DEFAULT_IS_FIFO, DEFAULT_IS_INCREMENT, DEFAULT_NEED_RETURN_VALUE, DEFAULT_PARENT_KEY, DEFAULT_PASS, DEFAULT_REMOVE_ELEMENT_SIZE, DEFAULT_REMOVE_RECURSIVELY, DEFAULT_SUBKEYS
Modifier and Type | Method and Description |
---|---|
<T> Optional<Result<T>> |
execute(Session session)
Sets a variable by a CAS operation.
|
static CasSetCmd |
of(String key,
byte oldval,
byte newval)
Constructs a CasSetCmd instance.
|
static CasSetCmd |
of(String key,
byte oldval,
byte newval,
String pass,
long expirationDuration)
Constructs a CasSetCmd instance.
|
static CasSetCmd |
of(String key,
int oldval,
int newval)
Constructs a CasSetCmd instance.
|
static CasSetCmd |
of(String key,
int oldval,
int newval,
String pass,
long expirationDuration)
Constructs a CasSetCmd instance.
|
static CasSetCmd |
of(String key,
long oldval,
long newval)
Constructs a CasSetCmd instance.
|
static CasSetCmd |
of(String key,
long oldval,
long newval,
String pass,
long expirationDuration)
Constructs a CasSetCmd instance.
|
static CasSetCmd |
of(String key,
short oldval,
short newval)
Constructs a CasSetCmd instance.
|
static CasSetCmd |
of(String key,
short oldval,
short newval,
String pass,
long expirationDuration)
Constructs a CasSetCmd instance.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
bytesToInt, getValusAsInt
public static CasSetCmd of(String key, byte oldval, byte newval, String pass, long expirationDuration)
key
- a key stringoldval
- an old bytenewval
- a new bytepass
- a password stringexpirationDuration
- a duration to expire.IllegalArgumentException
- if a key or a val string is null or emptypublic static CasSetCmd of(String key, byte oldval, byte newval)
key
- a key stringoldval
- an old bytenewval
- a new byteIllegalArgumentException
- if a key or a val string is null or emptypublic static CasSetCmd of(String key, short oldval, short newval, String pass, long expirationDuration)
key
- a key stringoldval
- an old val(short)newval
- a new val(short)pass
- a password stringexpirationDuration
- a duration to expire.IllegalArgumentException
- if a key or a val string is null or emptypublic static CasSetCmd of(String key, short oldval, short newval)
key
- a key stringoldval
- an old shortnewval
- a new shortIllegalArgumentException
- if a key or a val string is null or emptypublic static CasSetCmd of(String key, int oldval, int newval, String pass, long expirationDuration)
key
- a key stringoldval
- an old val(int)newval
- a new val(int)pass
- a password stringexpirationDuration
- a duration to expire.IllegalArgumentException
- if a key or a val string is null or emptypublic static CasSetCmd of(String key, int oldval, int newval)
key
- a key stringoldval
- an old val(int)newval
- a new val(int)IllegalArgumentException
- if a key or a val string is null or emptypublic static CasSetCmd of(String key, long oldval, long newval, String pass, long expirationDuration)
key
- a key stringoldval
- an old val(long)newval
- a new val(long)pass
- a password stringexpirationDuration
- a duration to expire.IllegalArgumentException
- if a key or a val string is null or emptypublic static CasSetCmd of(String key, long oldval, long newval)
key
- a key stringoldval
- an old val(long)newval
- a new val(long)IllegalArgumentException
- if a key or a val string is null or emptypublic <T> Optional<Result<T>> execute(Session session) throws IOException
execute
in interface Cmd<Boolean>
session
- a session instanceIllegalArgumentException
- if an illegal augment existsIOException
- if underlying library errors occur.Copyright © 2020. All rights reserved.