Read before write

Record in my db has the following bins : <uidx,last_updated, data> here uidx is the pk .

I am doing the following:

   func() {
    recordToBeInserted = newRecord(xyz);
         
    /*recordToBeInserted is a object that contains {uidx,timestamp,data} fileds */

    lastUpdatedInDb = select last_updated from namespace.set where pk = recordToBeInserted.uidx;

    if lastUpdatedInDb  < recordToBeInserted.timestamp{
    // update record in db
   }
 }

here i have to call aerospike two times in case the record has to be updated… can it be done in single call using filter/operation ?

Yes, you can use a predexp filter on either the records ‘last update time’ metadata or a bin containing a timestamp. Note that using the metadata will be much faster when the record isn’t updated or if you plan to replace the record because it avoids reading the record from disk. Also note that if you are replacing and not updating the record you need to send the replace flag.

Which client are you using?

I am using java client

So you are saying if i use records ‘last update time’ metadata, and use replace flag …

client will update/insert in case the the data is old/not present

can you share how the query would look like ?

ps : client also needs to send new record’s timestamp which can be compared with records ‘last update time’ metadata,

Would be something like:

WritePolicy updatePolicy = new WritePolicy();

// Assuming you only want updates here: I believe if you remove this line,
// all PredExps pass when the record doesn't exist (since you could
// specify this flag if you actually wanted update only).
updatePolicy.existsAction = policy.UPDATE_ONLY;

updatePolicy.setPredExp(PredExp.recLastUpdate(),
              PredExp.integerValue(UNIX_EPOCH_TIMESTAMP),
              PredExp.integerLess());

client.put(updatePolicy, key, bins);

The java client’s predexp APIs can be found here: https://www.aerospike.com/apidocs/java/com/aerospike/client/query/PredExp.html