aerospike/aerospike-client-ruby

Atomic validation of values and write policies when using operate?

naveedf opened this issue · 4 comments

A couple questions:

  1. Is it possible to only write a value only when the current values meets a certain condition? For example, if I have a bin value that represents a decrementing counter, and I only want to be able to decrement the counter if it is greater than 0, how can I accomplish this atomically? Do I have to leverage some type of "check-and-set," or is there a simpler method where I can perform some type of validation as part of an atomic operation.

  2. I see you can set a write policy during a put, e.g.

client.put(key, bins, meta, { 'exists': aerospike.POLICY_EXISTS_CREATE })

It doesn't appear the client supports such policies when performing a client.operate with a write operation. Is there a way to accomplish this?

Thanks

khaf commented
  1. You can use a UDF. UDF's are executed atomically.
  2. It should work. If not, that's probably a bug.

Thanks,

re 2: The following will overwrite the count bin when it exists rather than throwing an exception.

    operations = [
        {
            'op' : aerospike.OPERATOR_WRITE,
            'bin' : 'count',
            'val' : 123
        },
        {
            'op' : aerospike.OPERATOR_READ,
            'bin' : 'count'
        }
    ]

    (ret_key,ret_meta,ret_bin) =  self.aerospike_client.operate(aerospike_key, operations, meta, { 'exists': aerospike.POLICY_EXISTS_CREATE } )

https://pythonhosted.org/aerospike/client.html#aerospike-operate-policies seems to imply that this isn't one of the policies you can add to an operate

@naveedf, the exists policy attribute only applies to the write policy used by the put command. The operate policy does not support this.

Closing this. Feel free to open a new issue if there are further questions.