Last active
August 29, 2015 14:15
-
-
Save mbarker/45b8233d7f4c8628abbb to your computer and use it in GitHub Desktop.
Aerospike Dedupe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean isDuplicate(String id) { | |
Key key = new Key(namespace, set, id); | |
LOG.debug("Checking if seen: " + id); | |
// Check the record/bin a few times incase there is some write contention | |
int count = 0; | |
Boolean duplicate; | |
do { | |
duplicate = checkDuplicate(key); | |
} while (duplicate == null && count++ < RETRY_COUNT); | |
return duplicate == null || duplicate.booleanValue(); | |
} | |
private Boolean checkDuplicate(Key key) { | |
Record rec = client.get(null, key, bin); | |
// Already written | |
if (rec != null && rec.getValue(bin) != null && rec.getInt(bin) == 1) { | |
return true; | |
} | |
WritePolicy wp = new WritePolicy(); | |
if (rec != null) { // Record exists, bin is either 0 or missing, use check-and-set functionality to ensure same record is being updated | |
wp.generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL; | |
wp.generation = rec.generation; | |
} else { // Record doesn't exist, use create only functionaity to ensure nothing else writes to the record first | |
wp.recordExistsAction = RecordExistsAction.CREATE_ONLY; | |
} | |
try { | |
client.put(wp, key, new Bin(bin, 1)); | |
return false; | |
} catch (AerospikeException e) { | |
LOG.warn("Error", e); | |
// TODO: Check the error code for the Create Only/Invalid Generation error, throw RuntimeException if it's another error. | |
return null; | |
} | |
} | |
public void rollback(String id) { | |
Key key = new Key(namespace, set, id); | |
LOG.debug("Rolling back: " + id); | |
client.put(null, key, new Bin(bin, 0)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment