How to POP UP first data or any random data from aero spike without key?
I don't want to use scan as it will reduce performance.If Pop UP is not possible, can we just fetch data and delete it ? If it is possible , can we implement this as thread safe?
.
You can use a query (basically a scan) with maxRecords=1
specified in the Statement
to get just a single "random" record as a result - this should be fast (similar to SELECT * FROM table LIMIT 1
or SELECT TOP 1 * FROM table
in relational databases).
If you want to improve it further you can also configure the QueryPolicy
to:
setIncludeBinData(false)
if you only care about getting the PK/Digest and not about fetching the actual bins (will reduce size of the result set and network traffic).setShortQuery(true)
this will mark the query as short query (basically recommended when the results are expected to always be smaller than 100 records), Aerospike will not track short queries and will only use a single query thread (If i'm not mistaken as opposed to 4 by default of long queries), which can lead to better performance for short queries with small result sets. I suggest you check: Differences between a Short Query and a Long Query and also single-query-threads.Regrading thread safety of fetching and deleting - what is the purpose of deleting random records? if you can it is preferred to use truncate
but it will delete all the records within a set/namespace, not sure if it fits your use case.
You can also create a query like I suggested above to fetch lets say 50 random digests (no binData needed and marked as a short query) and then delete these records with a batch delete and if the record was already deleted by another thread just ignore the error (via Policy or via code), to avoid collisions (because its a waste of resources) you can query a range of partitions and change the range for the next batch.