for (int i = 0; i < number; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
Record operate = client.operate(policy, key, Operation.add(bin), Operation.get());
int value = operate.getInt(bin.name);
set.add(value);
}
});
thread.start();
}`
the set.size() less than number,itâs mean will get duplicate values
I want to increase the value of atom,In multithreaded environment,the code above some problems!
Every language client for Aerospike includes an increment operation which will initialize the record if it does not exist. Why would you use UDF for this?
Key coounterKey = new Key(this.namespace, this.set, "my-counter");
/*
* increment the counter by 1
*/
Bin counterBin = new Bin("some-counter", 1);
this.client.add(null, coounterKey, counterBin);
/*
* increment the counter by 15
*/
counterBin = new Bin("some-counter", 15);
this.client.add(null, coounterKey, counterBin);
/*
* increment the counter by 1 and return the new value
*/
counterBin = new Bin("some-counter", 1);
Record record = this.client.operate(null, coounterKey, Operation.add(counterBin), Operation.get("some-counter"));
System.out.println("New value: " + record.getLong("some-counter"));
/*
* decrement the counter by 5
*/
counterBin = new Bin("some-counter", -5);
this.client.add(null, coounterKey, counterBin);
It will not, because the increment operation is atomic. If you are using multi-ops you can tell it to both increment and read the value, so there is no time between those operations for something else to happen. You will perform both operations together. So you can increment a counter and get its value at that time, post-increment.
The code in your snippet looks correct for an increment of a bin, and a read of the new value.
The operate method in the PHP Client will cause all of the provided operations to be applied atomically. The value of the "age" bin will be incremented, and its new value will be returned to the client.
So one more question - is in Aerospike function like bRPopLPush (in Redis)?
I need to move element from List1 to List2. So it should be atomic.
I mean there are 600 operations that will do the same, but I need that only one operation can do it and then return element back, and so on. Its like queue. Is this implementation in php language for this operation?
Presently, there isnât an âoperationâ to do such. You can write a UDF in LUA to do this atomically. UDF is currently the only solution for atomic multi-bin inter-bin operations.
Even if I will make pop operation? When I make pop operation - so it must be atomic operation. In manual there is: Atomic list operations only work against top level elements, not nested values (https://www.aerospike.com/docs/guide/cdt-list.html)
So will it be atomic? And can I use it for queue?
That is saying that an operation such as list_pop is able to remove an element from the list, but not able to remove an element from a sublist.
For example
list = [1, 2, 3, [4, 5, 6]]
remove_by_index(list, -1) ## List = [1,2,3] now
But if list is:
list = [1, 2, 3, [4, 5, 6]]
With our current operations, you can not do something like remove a single element from the list stored in the last position of the main list. So, with our current operations, you cannot change list to:
[1, 2, 3, [4, 5]]
Using UDFs you can do these types of operations, and operate on multiple bins in the same record atomically.