Command like redis incr :atomic increment operations


#1

` final Set set = new HashSet<>();

        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!

redis command: incr

http://redis.io/commands/incr

How I implement it in aerospike? use UDF?


#2

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?

For example, the Python client’s aerospike.Client.increment

The docs for the various clients are linked to from here: http://www.aerospike.com/docs/client/


#3

i use java and i don’t found native increment operation

I found the answer on github,use above code.


#4

In the Java client documentation on our site there’s a link to the API reference where you would find it. It’s the ‘add’ operation.


#5

I think you misunderstood what I mean

I need to increase the value of multi-threading and get to the increased

JAVA API [add] method can increase the value and can not be returned,so i use operate method add and return .

if the bin base value is 1 add i use multiple threads to add and get ,the return value maybe dirty data .

i test my code,the final value is no problem.

i mean like this:

base bin value is 1

 multi-threading 
{
    add "key" 1
    read "key" #Multiple threads may be return 3
 }

i wan’t thead-1 return 2,thead-2 return 3. current may be thead-1 reuturn 3 and thead-3 reutrn 3;


#6

I want to give the user a non-repetition of id

I do not want to use uuid,

I want to use auto-incremented long value


#7

Here is some example code that

  • increments a counter
  • decrements a counter
  • increments a counter and returns the new value
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);

I hope this helps


#8

Thank you for your answer,

But I think in multiple threads,increments a counter and returns the new value,This result will be problems


#9

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.


#10

Does increment operation in Aerospike updates TTL of record?


#11

Yes, increment will update a record’s TTL.