-
Notifications
You must be signed in to change notification settings - Fork 13
Implement AerospikeGeneration annotation #182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.aerospike.mapper.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Mark a field or property to be used for optimistic concurrency control using Aerospike's generation field. | ||
* <p/> | ||
* The field or property must be of Integer or int type. When reading an object which has a field marked | ||
* with @Version, the returned record's generation field will be mapped into the @Version field. | ||
* When writing the record, if the @Version field is non-zero, the generation will be set in the | ||
* writePolicy.generation field and the writePolicy.generationPolicy will be set to | ||
* GenerationPolicy.EXPECT_GEN_EQUAL. | ||
* <p/> | ||
* Example usage: | ||
* <pre> | ||
* @AerospikeRecord(namespace = "test", set = "account") | ||
* public class Account { | ||
* @AerospikeKey | ||
* private int id; | ||
* @AerospikeBin | ||
* private String name; | ||
* @Version | ||
* private int version; | ||
* } | ||
* </pre> | ||
* | ||
* @author timfaulkes | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.FIELD, ElementType.METHOD}) | ||
public @interface AerospikeGeneration { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
import com.aerospike.client.Record; | ||
import com.aerospike.client.Value; | ||
import com.aerospike.client.policy.BatchPolicy; | ||
import com.aerospike.client.policy.GenerationPolicy; | ||
import com.aerospike.client.policy.Policy; | ||
import com.aerospike.client.policy.QueryPolicy; | ||
import com.aerospike.client.policy.RecordExistsAction; | ||
|
@@ -121,6 +122,14 @@ private <T> WritePolicy generateWritePolicyFromObject(T object) { | |
if (sendKey != null) { | ||
writePolicy.sendKey = sendKey; | ||
} | ||
|
||
// #181 Handle @Version field for optimistic concurrency control | ||
Integer versionValue = entry.getGenerationValue(object); | ||
if (versionValue != null && versionValue > 0) { | ||
writePolicy.generation = versionValue; | ||
writePolicy.generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If for some reason, the class's write policy is set to GenerationPolicy.EXPECT_GEN_GT will we be overriding it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I have never found any reason to use EXPECT_GEN_GT, it makes no sense as generations wrap around. Good catches on those couple of trailing references to version instead of generation! |
||
} | ||
|
||
return writePolicy; | ||
} | ||
|
||
|
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.