Skip to content

Commit

Permalink
Remove old "storeInPkOnly" terminology, add javadocs, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
roimenashe committed Jun 19, 2024
1 parent fb8de6e commit bcd0970
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,14 @@ aql> select * from test.testSet
If it is desired to force the primary key to be stored in the database and NOT have key added explicitly as a column then two things must be set:
1. The `@AerospikeRecord` annotation must have `sendKey = true`
2. The `@AerospikeKey` annotation must have `storeInPkOnly = true`
2. The `@AerospikeKey` annotation must have `storeAsBin = false`
So the object would look like:
```java
@AerospikeRecord(namespace = "test", set = "testSet", sendKey = true)
public static class A {
@AerospikeKey(storeInPkOnly = true)
@AerospikeKey(storeAsBin = false)
private long key;
private String value;
}
Expand Down Expand Up @@ -2025,7 +2025,7 @@ The key structure is used to specify the key to a record. Keys are optional in s

The key structure contains:
- **field**: The name of the field which to which this key is mapped. If this is provided, the getter and setter cannot be provided.
- **storeInPkOnly**: Do not store the primary key column in the database, but rather use the `sendKey` facility related to Aerospike to save the key in the database. When the record is read, the value will be pulled back and place in the key field.
- **storeAsBin**: Store the primary key as a bin in the database, alternatively it is recommended to use the `sendKey` facility related to Aerospike to save the key in the record's metadata (and set this flag to false). When the record is read, the value will be pulled back and place in the key field.
- **getter**: The getter method used to populate the key. This must be used in conjunction with a setter method, and excludes the use of the field attribute.
- **setter**: The setter method used to map data back to the Java key. This is used in conjunction with the getter method and precludes the use of the field attribute. Note that the return type of the getter must match the type of the first parameter of the setter, and the setter can have either 1 or 2 parameters, with the second (optional) parameter being either of type [com.aerospike.client.Key](https://www.aerospike.com/apidocs/java/com/aerospike/client/Key.html) or Object.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
* The setter attribute is used only on Methods where the method is used to set the key on lazy object instantiation
*/
boolean setter() default false;


/**
* Store the key as an Aerospike Bin, alternatively you can use @AerospikeRecord.sendKey to store the key in the record's metadata
*/
boolean storeAsBin() default true;
}
10 changes: 10 additions & 0 deletions src/main/java/com/aerospike/mapper/tools/ClassCacheEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -630,20 +630,24 @@ private void loadPropertiesFromClass() {

if (keyProperty != null) {
keyProperty.validate(clazz.getName(), config, true);

if (key != null) {
throw new AerospikeException(String.format("Class %s cannot have a more than one key", clazz.getName()));
}

AnnotatedType annotatedType = new AnnotatedType(config, keyProperty.getGetter());
TypeMapper typeMapper = TypeUtils.getMapper(keyProperty.getType(), annotatedType, this.mapper);
this.key = new ValueType.MethodValue(keyProperty, typeMapper, annotatedType);
}
for (String thisPropertyName : properties.keySet()) {
PropertyDefinition thisProperty = properties.get(thisPropertyName);
thisProperty.validate(clazz.getName(), config, false);

if (this.values.get(thisPropertyName) != null) {
throw new AerospikeException(String.format("Class %s cannot define the mapped name %s more than once",
clazz.getName(), thisPropertyName));
}

AnnotatedType annotatedType = new AnnotatedType(config, thisProperty.getGetter());
TypeMapper typeMapper = TypeUtils.getMapper(thisProperty.getType(), annotatedType, this.mapper);
ValueType value = new ValueType.MethodValue(thisProperty, typeMapper, annotatedType);
Expand All @@ -657,28 +661,34 @@ private void loadFieldsFromClass() {
for (Field thisField : this.clazz.getDeclaredFields()) {
boolean isKey = false;
BinConfig thisBin = getBinFromField(thisField);

if (Modifier.isFinal(thisField.getModifiers()) && Modifier.isStatic(thisField.getModifiers())) {
// We cannot map static final fields
continue;
}

if (thisField.isAnnotationPresent(AerospikeKey.class) || (!StringUtils.isBlank(keyField) && keyField.equals(thisField.getName()))) {
if (thisField.isAnnotationPresent(AerospikeExclude.class) || (thisBin != null && thisBin.isExclude() != null && thisBin.isExclude())) {
throw new AerospikeException(String.format("Class %s cannot have a field which is both a key and excluded.",
clazz.getName()));
}

if (key != null) {
throw new AerospikeException(String.format("Class %s cannot have a more than one key",
clazz.getName()));
}
AerospikeKey keyAnnotation = thisField.getAnnotation(AerospikeKey.class);
boolean storeAsBin = (keyAnnotation == null) || (keyAnnotation != null && keyAnnotation.storeAsBin());

if (keyConfig != null && keyConfig.getStoreAsBin() != null) {
storeAsBin = keyConfig.getStoreAsBin();
}

if (!storeAsBin && (this.sendKey == null || !this.sendKey)) {
throw new AerospikeException(String.format("Class %s attempts to store primary key information" +
" inside the aerospike key, but sendKey is not true at the record level", clazz.getName()));
}

AnnotatedType annotatedType = new AnnotatedType(config, thisField);
TypeMapper typeMapper = TypeUtils.getMapper(thisField.getType(), annotatedType, this.mapper);
this.key = new ValueType.FieldValue(thisField, typeMapper, annotatedType);
Expand Down

0 comments on commit bcd0970

Please sign in to comment.