Skip to content

Commit

Permalink
Updated field name to storeAsBin
Browse files Browse the repository at this point in the history
Changed the config field name from `storeInPkOnly` to `storeAsBin`,
added unit test to ensure config validation works.
  • Loading branch information
tim-aero committed Jun 18, 2024
1 parent d9d156b commit 83da81a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
*/
boolean setter() default false;

boolean storeInPkOnly() default false;
boolean storeAsBin() default true;
}
16 changes: 8 additions & 8 deletions src/main/java/com/aerospike/mapper/tools/ClassCacheEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class ClassCacheEntry<T> {
private final Class<T> clazz;
private ValueType key;
private String keyName = null;
private boolean keyOnlyInPK = false;
private boolean keyAsBin = true;
private final TreeMap<String, ValueType> values = new TreeMap<>();
private ClassCacheEntry<?> superClazz;
private int binCount;
Expand Down Expand Up @@ -671,18 +671,18 @@ private void loadFieldsFromClass() {
clazz.getName()));
}
AerospikeKey keyAnnotation = thisField.getAnnotation(AerospikeKey.class);
boolean storeInPkOnly = (keyAnnotation != null && keyAnnotation.storeInPkOnly());
if (keyConfig != null && keyConfig.getStoreInPkOnly() != null) {
storeInPkOnly = keyConfig.getStoreInPkOnly();
boolean storeAsBin = (keyAnnotation == null) || (keyAnnotation != null && keyAnnotation.storeAsBin());
if (keyConfig != null && keyConfig.getStoreAsBin() != null) {
storeAsBin = keyConfig.getStoreAsBin();
}
if (storeInPkOnly && (this.sendKey == null || !this.sendKey)) {
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);
this.keyOnlyInPK = storeInPkOnly;
this.keyAsBin = storeAsBin;
isKey = true;
}

Expand Down Expand Up @@ -866,7 +866,7 @@ public Bin[] getBins(Object instance, boolean allowNullBins, String[] binNames)
while (thisClass != null) {
Set<String> keys = thisClass.values.keySet();
for (String name : keys) {
if (name.equals(thisClass.keyName) && thisClass.keyOnlyInPK) {
if (name.equals(thisClass.keyName) && !thisClass.keyAsBin) {
// Do not explicitly write the key to the bin
continue;
}
Expand Down Expand Up @@ -999,7 +999,7 @@ private T constructAndHydrate(Key key, Record record, Map<String, Object> map) {
Object aerospikeValue;
if (record == null) {
aerospikeValue = map.get(name);
} else if (name.equals(thisClass.keyName) && thisClass.keyOnlyInPK) {
} else if (name.equals(thisClass.keyName) && !thisClass.keyAsBin) {
if (key.userKey != null) {
aerospikeValue = key.userKey.getObject();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ public Builder withKeyField(String fieldName) {
return this;
}

public Builder withKeyFieldAndStorePkOnly(String fieldName, boolean storePkOnly) {
public Builder withKeyFieldAndStoreAsBin(String fieldName, boolean storeAsBin) {
if (this.classConfig.getKey() == null) {
this.classConfig.setKey(new KeyConfig());
}
this.validateFieldExists(fieldName);
this.classConfig.getKey().setField(fieldName);
this.classConfig.getKey().setStoreInPkOnly(storePkOnly);
this.classConfig.getKey().setStoreAsBin(storeAsBin);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class KeyConfig {
private String field;
private String getter;
private String setter;
private Boolean storeInPkOnly;
private Boolean storeAsBin;

public String getField() {
return field;
Expand All @@ -20,12 +20,12 @@ public String getSetter() {
return setter;
}

public Boolean getStoreInPkOnly() {
return storeInPkOnly;
public Boolean getStoreAsBin() {
return storeAsBin;
}

public void setStoreInPkOnly(boolean value) {
this.storeInPkOnly = value;
public void setStoreAsBin(boolean value) {
this.storeAsBin = value;
}

public void setField(String field) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/aerospike/mapper/AeroMapperBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void setupClass() {
ClientPolicy policy = new ClientPolicy();
// Set event loops to use in asynchronous commands.
policy.eventLoops = new NioEventLoops(1);
client = new AerospikeClient(policy, "localhost", 3000);
client = new AerospikeClient(policy, "localhost", 3100);
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.aerospike.mapper;

import com.aerospike.client.AerospikeException;
import com.aerospike.client.Record;
import com.aerospike.mapper.annotations.AerospikeKey;
import com.aerospike.mapper.annotations.AerospikeRecord;
Expand All @@ -12,6 +13,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class UsePKColumnInsteadOfBinForKeyTest extends AeroMapperBaseTest {

Expand All @@ -20,7 +22,7 @@ public class UsePKColumnInsteadOfBinForKeyTest extends AeroMapperBaseTest {
@NoArgsConstructor
@AerospikeRecord(namespace = "test", set = "testSet", sendKey = true)
public static class A {
@AerospikeKey(storeInPkOnly = true)
@AerospikeKey(storeAsBin = false)
private long key1;
private String value;
}
Expand All @@ -33,6 +35,16 @@ public static class B {
private String value;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
@AerospikeRecord(namespace = "test", set = "testSet", sendKey = false)
public static class C {
@AerospikeKey(storeAsBin = false)
private long key1;
private String value;
}

@Test
public void runTest() {
AeroMapper mapper = new AeroMapper.Builder(client).build();
Expand All @@ -56,7 +68,7 @@ public void runTestViaYaml() throws Exception {
" sendKey: true\n" +
" key:\n" +
" field: keyStr\n" +
" storeInPkOnly: true\n";
" storeAsBin: false\n";
AeroMapper mapper = new AeroMapper.Builder(client).withConfiguration(yaml).build();
B b = new B("key1", "test1");
mapper.save(b);
Expand All @@ -74,7 +86,9 @@ public void runTestViaConfig() {
.withNamespace("test")
.withSet("testSet")
.withSendKey(true)
.withKeyFieldAndStorePkOnly("keyStr", true).build();
.withKeyFieldAndStoreAsBin("keyStr", false).build();

client.truncate(null, NAMESPACE, "testSet", null);
AeroMapper mapper = new AeroMapper.Builder(client).withClassConfigurations(classBConfig).build();
B b = new B("key2", "test2");
mapper.save(b);
Expand All @@ -85,4 +99,13 @@ public void runTestViaConfig() {
Record rawObject = client.get(null, mapper.getRecordKey(b));
assertFalse(rawObject.bins.containsKey("keyStr"));
}

@Test
public void runTestWithInvalidConfig() {
AeroMapper mapper = new AeroMapper.Builder(client).build();
C c = new C(1, "test");
assertThrows(AerospikeException.class, () -> {
mapper.save(c);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ReactiveUsePKColumnInsteadOfBinForKeyTest extends ReactiveAeroMappe
@NoArgsConstructor
@AerospikeRecord(namespace = "test", set = "testSet", sendKey = true)
public static class A {
@AerospikeKey(storeInPkOnly = true)
@AerospikeKey(storeAsBin = false)
private long key1;
private String value;
}
Expand Down Expand Up @@ -58,7 +58,7 @@ public void runTestViaYaml() throws Exception {
" sendKey: true\n" +
" key:\n" +
" field: keyStr\n" +
" storeInPkOnly: true\n";
" storeAsBin: false\n";
ReactiveAeroMapper reactiveMapper = new ReactiveAeroMapper.Builder(reactorClient).withConfiguration(yaml).build();
B b = new B("key1", "test1");
reactiveMapper.save(b).subscribeOn(Schedulers.parallel()).block();
Expand All @@ -78,7 +78,7 @@ public void runTestViaConfig() {
.withNamespace("test")
.withSet("testSet")
.withSendKey(true)
.withKeyFieldAndStorePkOnly("keyStr", true).build();
.withKeyFieldAndStoreAsBin("keyStr", false).build();
ReactiveAeroMapper reactiveMapper = new ReactiveAeroMapper.Builder(reactorClient).withClassConfigurations(classBConfig).build();
B b = new B("key2", "test2");
reactiveMapper.save(b).subscribeOn(Schedulers.parallel()).block();
Expand Down

0 comments on commit 83da81a

Please sign in to comment.