Skip to content

Commit

Permalink
Cleanup and format
Browse files Browse the repository at this point in the history
  • Loading branch information
roimenashe committed Jun 17, 2024
1 parent b5b3fec commit d9d156b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 83 deletions.
66 changes: 35 additions & 31 deletions src/main/java/com/aerospike/mapper/tools/ClassCacheEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,12 @@ private Method findConstructorFactoryMethod() {
if (!StringUtils.isBlank(this.factoryClass) || !StringUtils.isBlank(this.factoryMethod)) {
// Both must be specified
if (StringUtils.isBlank(this.factoryClass)) {
throw new AerospikeException("Missing factoryClass definition when factoryMethod is specified on class " +
clazz.getSimpleName());
throw new AerospikeException(String.format("Missing factoryClass definition when factoryMethod is specified on class %s",
clazz.getSimpleName()));
}
if (StringUtils.isBlank(this.factoryClass)) {
throw new AerospikeException("Missing factoryMethod definition when factoryClass is specified on class " +
clazz.getSimpleName());
throw new AerospikeException(String.format("Missing factoryMethod definition when factoryClass is specified on class %s",
clazz.getSimpleName()));
}
// Load the class and check for the method
try {
Expand Down Expand Up @@ -480,8 +480,8 @@ private void setConstructorFactoryMethod(Method method) {
private void findConstructor() {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
if (constructors.length == 0) {
throw new AerospikeException("Class " + clazz.getSimpleName() +
" has no constructors and hence cannot be mapped to Aerospike");
throw new AerospikeException(String.format("Class %s has no constructors and hence cannot be mapped to Aerospike",
clazz.getSimpleName()));
}
Constructor<?> desiredConstructor = null;
Constructor<?> noArgConstructor = null;
Expand All @@ -495,9 +495,9 @@ private void findConstructor() {
AerospikeConstructor aerospikeConstructor = thisConstructor.getAnnotation(AerospikeConstructor.class);
if (aerospikeConstructor != null) {
if (desiredConstructor != null) {
throw new AerospikeException("Class " + clazz.getSimpleName() +
" has multiple constructors annotated with @AerospikeConstructor. " +
"Only one constructor can be so annotated.");
throw new AerospikeException(String.format("Class %s" +
" has multiple constructors annotated with @AerospikeConstructor." +
" Only one constructor can be so annotated.", clazz.getSimpleName()));
} else {
desiredConstructor = thisConstructor;
}
Expand All @@ -510,8 +510,9 @@ private void findConstructor() {
}

if (desiredConstructor == null) {
throw new AerospikeException("Class " + clazz.getSimpleName() + " has neither a no-arg constructor, " +
"nor a constructor annotated with @AerospikeConstructor so cannot be mapped to Aerospike.");
throw new AerospikeException(String.format("Class %s has neither a no-arg constructor, " +
"nor a constructor annotated with @AerospikeConstructor so cannot be mapped to Aerospike.",
clazz.getSimpleName()));
}

Parameter[] params = desiredConstructor.getParameters();
Expand Down Expand Up @@ -552,10 +553,11 @@ private void findConstructor() {
}
Class<?> type = thisParam.getType();
if (!type.isAssignableFrom(allValues.get(binName).getType())) {
throw new AerospikeException("Class " + clazz.getSimpleName() + " has a preferred constructor of " +
desiredConstructor + ". However, parameter " + count +
" is of type " + type + " but assigned from bin \"" + binName + "\" of type " +
values.get(binName).getType() + ". These types are incompatible.");
throw new AerospikeException(String.format("Class %s has a preferred constructor of" +
" %s. However, parameter %s" +
" is of type %s but assigned from bin \"%s\" of type %s." +
" These types are incompatible.",
clazz.getSimpleName(), desiredConstructor, count, type, binName, values.get(binName).getType()));
}
constructorParamBins[count - 1] = binName;
constructorParamDefaults[count - 1] = PrimitiveDefaults.getDefaultValue(thisParam.getType());
Expand Down Expand Up @@ -629,7 +631,7 @@ private void loadPropertiesFromClass() {
if (keyProperty != null) {
keyProperty.validate(clazz.getName(), config, true);
if (key != null) {
throw new AerospikeException("Class " + clazz.getName() + " cannot have a more than one key");
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);
Expand All @@ -639,8 +641,8 @@ private void loadPropertiesFromClass() {
PropertyDefinition thisProperty = properties.get(thisPropertyName);
thisProperty.validate(clazz.getName(), config, false);
if (this.values.get(thisPropertyName) != null) {
throw new AerospikeException("Class " + clazz.getName() + " cannot define the mapped name " +
thisPropertyName + " more than once");
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);
Expand All @@ -661,18 +663,21 @@ private void loadFieldsFromClass() {
}
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("Class " + clazz.getName() + " cannot have a field which is both a key and excluded.");
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("Class " + clazz.getName() + " cannot have a more than one key");
throw new AerospikeException(String.format("Class %s cannot have a more than one key",
clazz.getName()));
}
AerospikeKey keyAnnotation = thisField.getAnnotation(AerospikeKey.class);
boolean storeInPkOnly = (keyAnnotation != null && keyAnnotation.storeInPkOnly());
if (keyConfig != null && keyConfig.getStoreInPkOnly() != null) {
storeInPkOnly = keyConfig.getStoreInPkOnly();
}
if (storeInPkOnly && (this.sendKey == null || !this.sendKey)) {
throw new AerospikeException("Class " + clazz.getName() + " attempts to store primary key information inside the aerospike key, but sendKey is not true at the record level");
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);
Expand Down Expand Up @@ -704,7 +709,8 @@ private void loadFieldsFromClass() {
}

if (this.values.get(name) != null) {
throw new AerospikeException("Class " + clazz.getName() + " cannot define the mapped name " + name + " more than once");
throw new AerospikeException(String.format("Class %s cannot define the mapped name %s more than once",
clazz.getName(), name));
}
if ((bin != null && bin.useAccessors()) || (thisBin != null && thisBin.getUseAccessors() != null && thisBin.getUseAccessors())) {
validateAccessorsForField(name, thisField);
Expand Down Expand Up @@ -788,8 +794,8 @@ public Object getKey(Object object) {
try {
Object key = this._getKey(object);
if (key == null) {
throw new AerospikeException("Null key from annotated object of class " + this.clazz.getSimpleName() +
". Did you forget an @AerospikeKey annotation?");
throw new AerospikeException(String.format("Null key from annotated object of class %s." +
" Did you forget an @AerospikeKey annotation?", this.clazz.getSimpleName()));
}
return key;
} catch (ReflectiveOperationException re) {
Expand Down Expand Up @@ -993,16 +999,14 @@ 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.keyOnlyInPK) {
if (key.userKey != null) {
aerospikeValue = key.userKey.getObject();
} else {
throw new AerospikeException(String.format("Key field on class %s was <null> for key %s." +
" Was the record saved passing 'sendKey = true'? ", className, key));
}
else {
throw new AerospikeException("Key field on class " + className + " was <null> for key " + key + ". Was the record saved passing 'sendKey = true'? ");
}
}
else {
} else {
aerospikeValue = record.getValue(name);
}
valueMap.put(name, value.getTypeMapper().fromAerospikeFormat(aerospikeValue));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public <T> Mono<Boolean> delete(WritePolicy writePolicy, @NotNull Class<T> clazz
if (writePolicy == null) {
writePolicy = entry.getWritePolicy();
if (entry.getDurableDelete() != null) {
// Clone the write policy so we're not changing the original one
// Clone the write policy, so we're not changing the original one
writePolicy = new WritePolicy(writePolicy);
writePolicy.durableDelete = entry.getDurableDelete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private Key createKey(ClassCacheEntry<?> entry, DeferredObjectLoader.DeferredObj
public void resolveDependencies(ClassCacheEntry<?> parentEntity) {
List<DeferredObjectLoader.DeferredObjectSetter> deferredObjects = DeferredObjectLoader.getAndClear();

if (deferredObjects.size() == 0) {
if (deferredObjects.isEmpty()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.aerospike.mapper;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import com.aerospike.client.Record;
import com.aerospike.mapper.annotations.AerospikeKey;
import com.aerospike.mapper.annotations.AerospikeRecord;
import com.aerospike.mapper.tools.AeroMapper;
import com.aerospike.mapper.tools.configuration.ClassConfig;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.jupiter.api.Test;

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

public class UsePKColumnInsteadOfBinForKeyTest extends AeroMapperBaseTest {

@Data
@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -40,38 +38,38 @@ public void runTest() {
AeroMapper mapper = new AeroMapper.Builder(client).build();
A a = new A(1, "test");
mapper.save(a);
A readA = mapper.read(A.class,1);
assertTrue(readA.key1 == 1);

A readA = mapper.read(A.class, 1);
assertEquals(1, readA.key1);

Record rawObject = client.get(null, mapper.getRecordKey(a));
assertFalse(rawObject.bins.containsKey("key1"));
}

@Test
public void runTestViaYaml() throws Exception {
String yaml = "---\n" +
"classes:\n" +
" - class: com.aerospike.mapper.UsePKColumnInsteadOfBinForKeyTest$B\n" +
" namespace: test\n" +
" set: testSet\n" +
" sendKey: true\n" +
" namespace: test\n" +
" set: testSet\n" +
" sendKey: true\n" +
" key:\n" +
" field: keyStr\n" +
" field: keyStr\n" +
" storeInPkOnly: true\n";
AeroMapper mapper = new AeroMapper.Builder(client).withConfiguration(yaml).build();
B b = new B("key1", "test1");
mapper.save(b);
B readB = mapper.read(B.class,"key1");
assertTrue("key1".equals(readB.keyStr));

B readB = mapper.read(B.class, "key1");
assertEquals("key1", readB.keyStr);

Record rawObject = client.get(null, mapper.getRecordKey(b));
assertFalse(rawObject.bins.containsKey("keyStr"));
}

@Test
public void runTestViaConfig() throws Exception {
public void runTestViaConfig() {
ClassConfig classBConfig = new ClassConfig.Builder(B.class)
.withNamespace("test")
.withSet("testSet")
Expand All @@ -80,10 +78,10 @@ public void runTestViaConfig() throws Exception {
AeroMapper mapper = new AeroMapper.Builder(client).withClassConfigurations(classBConfig).build();
B b = new B("key2", "test2");
mapper.save(b);
B readB = mapper.read(B.class,"key2");
assertTrue("key2".equals(readB.keyStr));

B readB = mapper.read(B.class, "key2");
assertEquals("key2", readB.keyStr);

Record rawObject = client.get(null, mapper.getRecordKey(b));
assertFalse(rawObject.bins.containsKey("keyStr"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package com.aerospike.mapper.reactive;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import com.aerospike.client.Record;
import com.aerospike.client.query.KeyRecord;
import com.aerospike.mapper.UsePKColumnInsteadOfBinForKeyTest;
import com.aerospike.mapper.UsePKColumnInsteadOfBinForKeyTest.B;
import com.aerospike.mapper.annotations.AerospikeKey;
import com.aerospike.mapper.annotations.AerospikeRecord;
import com.aerospike.mapper.tools.AeroMapper;
import com.aerospike.mapper.tools.ReactiveAeroMapper;
import com.aerospike.mapper.tools.configuration.ClassConfig;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.jupiter.api.Test;
import reactor.core.scheduler.Schedulers;

import static org.junit.jupiter.api.Assertions.*;

public class ReactiveUsePKColumnInsteadOfBinForKeyTest extends ReactiveAeroMapperBaseTest {

@Data
@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -45,38 +38,42 @@ public void runTest() {
ReactiveAeroMapper reactiveMapper = new ReactiveAeroMapper.Builder(reactorClient).build();
A a = new A(1, "test");
reactiveMapper.save(a).subscribeOn(Schedulers.parallel()).block();

A readA = reactiveMapper.read(A.class, 1).subscribeOn(Schedulers.parallel()).block();
assertTrue(readA.key1 == 1);

assertNotNull(readA);
assertEquals(1, readA.key1);

KeyRecord rawObject = reactorClient.get(null, reactiveMapper.getRecordKey(a).block()).block();
assertNotNull(rawObject);
assertFalse(rawObject.record.bins.containsKey("key1"));
}

@Test
public void runTestViaYaml() throws Exception {
String yaml = "---\n" +
"classes:\n" +
" - class: com.aerospike.mapper.reactive.ReactiveUsePKColumnInsteadOfBinForKeyTest$B\n" +
" namespace: test\n" +
" set: testSet\n" +
" sendKey: true\n" +
" namespace: test\n" +
" set: testSet\n" +
" sendKey: true\n" +
" key:\n" +
" field: keyStr\n" +
" field: keyStr\n" +
" storeInPkOnly: true\n";
ReactiveAeroMapper reactiveMapper = new ReactiveAeroMapper.Builder(reactorClient).withConfiguration(yaml).build();
B b = new B("key1", "test1");
reactiveMapper.save(b).subscribeOn(Schedulers.parallel()).block();

B readB = reactiveMapper.read(B.class, "key1").subscribeOn(Schedulers.parallel()).block();
assertTrue("key1".equals(readB.keyStr));

assertNotNull(readB);
assertEquals("key1", readB.keyStr);

KeyRecord rawObject = reactorClient.get(null, reactiveMapper.getRecordKey(b).block()).block();
assertNotNull(rawObject);
assertFalse(rawObject.record.bins.containsKey("keyStr"));
}

@Test
public void runTestViaConfig() throws Exception {
public void runTestViaConfig() {
ClassConfig classBConfig = new ClassConfig.Builder(B.class)
.withNamespace("test")
.withSet("testSet")
Expand All @@ -85,12 +82,13 @@ public void runTestViaConfig() throws Exception {
ReactiveAeroMapper reactiveMapper = new ReactiveAeroMapper.Builder(reactorClient).withClassConfigurations(classBConfig).build();
B b = new B("key2", "test2");
reactiveMapper.save(b).subscribeOn(Schedulers.parallel()).block();

B readB = reactiveMapper.read(B.class, "key2").subscribeOn(Schedulers.parallel()).block();
assertTrue("key2".equals(readB.keyStr));

assertNotNull(readB);
assertEquals("key2", readB.keyStr);

KeyRecord rawObject = reactorClient.get(null, reactiveMapper.getRecordKey(b).block()).block();
assertNotNull(rawObject);
assertFalse(rawObject.record.bins.containsKey("keyStr"));
}

}

0 comments on commit d9d156b

Please sign in to comment.