I have to implement cursor-based pagination and am a bit confused on how to go about this given that the primary key of my entities is not an auto-increment, like for example Aerospike.
The most obvious alternative when comparison operator isn't available on the primary key in a distributed system where we don't use auto-increments, would be the use of a timestamp. But how reliable is this?
That is, two users may make an upload at exactly the same time, which basically screws up the logic behind cursor based pagination.
For example, give me the next 10 items as from a certain timestamp that was sent as cursor for fetching the next results. When this timestamp has two posts, 1 post may be dropped and neglected if it didn't fit in the previous requested count range (e.g. 10 posts of which the duplicate post would be at location 11).
How do you circumvent this problem?
The most obvious way would be to have a secondary field next to a timestamp with additional counter when a timestamp already exists, and handle the additional logic at application level, but all of this seems to add a lot of bloat.
Any insight highly appreciated!
Use Capped Lists or Capped Maps as your data bin.
Capped Map Code Snippet or a variation thereof - retains last 10 updates:
public class CappedMap {
public static int insert(AerospikeClient client, int i) {
Key key = new Key("test", "testMap", "user1");
MapPolicy mPolicy = new MapPolicy();
int retVal=0;
try {
client.operate(null, key,
MapOperation.removeByIndexRange("myMap",-10,10,MapReturnType.INVERTED),
// INVERTED introduced in server version 3.16.0.1
MapOperation.put(mPolicy, "myMap", Value.get(i),
Value.get("A quick brown fox jumps right over a lazy dog") ));
}
catch (AerospikeException e) {
System.out.println("Error Code: "+e.getResultCode());
}
return i;
}
public static void main(String[] args) {
AerospikeClient client = new AerospikeClient("127.0.0.1", 3000);
int retVal = 0;
for (int i = 0; i < 123; i++) {
System.out.println("Inserting k = "+i);
i = insert(client, i);
}
client.close();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With