Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 53 additions & 69 deletions src/main/java/com/aerospike/mapper/tools/AeroMapper.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
package com.aerospike.mapper.tools;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;

import com.aerospike.client.AerospikeException;
import com.aerospike.client.Bin;
import com.aerospike.client.IAerospikeClient;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.Value;
import com.aerospike.client.policy.BatchPolicy;
import com.aerospike.client.policy.Policy;
import com.aerospike.client.policy.QueryPolicy;
import com.aerospike.client.policy.RecordExistsAction;
import com.aerospike.client.policy.ScanPolicy;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.client.*;
import com.aerospike.client.policy.*;
import com.aerospike.client.query.RecordSet;
import com.aerospike.client.query.Statement;
import com.aerospike.mapper.tools.ClassCache.PolicyType;
Expand All @@ -32,18 +10,26 @@
import com.aerospike.mapper.tools.TypeUtils.AnnotatedType;
import com.aerospike.mapper.tools.configuration.ClassConfig;
import com.aerospike.mapper.tools.configuration.Configuration;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class AeroMapper {

IAerospikeClient mClient;
final IAerospikeClient mClient;

public static class Builder {
private AeroMapper mapper;
private final AeroMapper mapper;
private List<Class<?>> classesToPreload = null;

public Builder(IAerospikeClient client) {
Expand All @@ -52,9 +38,8 @@ public Builder(IAerospikeClient client) {
}

/**
* Add in a custom type converter. The converter must have methods which implement the ToAerospike and FromAerospike annotation
*
* @param converter
* Add in a custom type converter. The converter must have methods which implement the ToAerospike and FromAerospike annotation.
* @param converter The custom converter
* @return this object
*/
public Builder addConverter(Object converter) {
Expand All @@ -72,22 +57,22 @@ public Builder preLoadClass(Class<?> clazz) {
return this;
}

public Builder withConfigurationFile(File file) throws JsonParseException, JsonMappingException, IOException {
public Builder withConfigurationFile(File file) throws IOException {
return this.withConfigurationFile(file, false);
}

public Builder withConfigurationFile(File file, boolean allowsInvalid) throws JsonParseException, JsonMappingException, IOException {
public Builder withConfigurationFile(File file, boolean allowsInvalid) throws IOException {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
Configuration configuration = objectMapper.readValue(file, Configuration.class);
this.loadConfiguration(configuration, allowsInvalid);
return this;
}

public Builder withConfiguration(String configurationYaml) throws JsonMappingException, JsonProcessingException {
public Builder withConfiguration(String configurationYaml) throws JsonProcessingException {
return this.withConfiguration(configurationYaml, false);
}

public Builder withConfiguration(String configurationYaml, boolean allowsInvalid) throws JsonMappingException, JsonProcessingException {
public Builder withConfiguration(String configurationYaml, boolean allowsInvalid) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
Configuration configuration = objectMapper.readValue(configurationYaml, Configuration.class);
this.loadConfiguration(configuration, allowsInvalid);
Expand All @@ -105,7 +90,7 @@ private void loadConfiguration(@NotNull Configuration configuration, boolean all
try {
Class.forName(config.getClassName());
} catch (ClassNotFoundException e) {
throw new AerospikeException("Canot find a class with name " + name);
throw new AerospikeException("Cannot find a class with name " + name);
}
}
}
Expand Down Expand Up @@ -224,8 +209,8 @@ private <T> void save(WritePolicy writePolicy, @NotNull T object, RecordExistsAc
/**
* Translate a Java object to an Aerospike format object. Note that this could potentially have performance issues as
* the type information of the passed object must be determined on every call.
* @param obj
* @return
* @param obj A given Java object.
* @return An Aerospike format object.
*/
public Object translateToAerospike(Object obj) {
if (obj == null) {
Expand All @@ -238,8 +223,8 @@ public Object translateToAerospike(Object obj) {
/**
* Translate an Aerospike object to a Java object. Note that this could potentially have performance issues as
* the type information of the passed object must be determined on every call.
* @param obj
* @return
* @param obj A given Java object.
* @return An Aerospike format object.
*/
@SuppressWarnings("unchecked")
public <T> T translateFromAerospike(@NotNull Object obj, @NotNull Class<T> expectedClazz) {
Expand All @@ -259,8 +244,8 @@ public <T> T translateFromAerospike(@NotNull Object obj, @NotNull Class<T> expec
* </pre>
* Not that no transactionality is implied by this method -- if any of the save methods fail, the exception will be
* thrown without trying the other objects, nor attempting to roll back previously saved objects
* @param object
* @throws AerospikeException
* @param objects One or two objects to save.
* @throws AerospikeException an AerospikeException will be thrown in case of an error.
*/
public void save(@NotNull Object ... objects) throws AerospikeException {
for (Object thisObject : objects) {
Expand All @@ -271,8 +256,8 @@ public void save(@NotNull Object ... objects) throws AerospikeException {
/**
* Save an object in the database. This method will perform a REPLACE on the existing record so any existing
* data will be overwritten by the data in the passed object
* @param object
* @throws AerospikeException
* @param object The object to save.
* @throws AerospikeException an AerospikeException will be thrown in case of an error.
*/
public void save(@NotNull Object object, String ...binNames) throws AerospikeException {
save(null, object, RecordExistsAction.REPLACE, binNames);
Expand All @@ -281,19 +266,19 @@ public void save(@NotNull Object object, String ...binNames) throws AerospikeExc
/**
* Save an object in the database with the given WritePolicy. This write policy will override any other set writePolicy so
* is effectively an upsert operation
* @param object
* @throws AerospikeException
* @param writePolicy The write policy for the save operation.
* @param object The object to save.
* @throws AerospikeException an AerospikeException will be thrown in case of an error.
*/
public void save(@NotNull WritePolicy writePolicy, @NotNull Object object, String ...binNames) throws AerospikeException {
save(writePolicy, object, null, binNames);
}


/**
* Updates the object in the database, merging the record with the existing record. This uses the RecordExistsAction
* of UPDATE. If bins are specified, only bins with the passed names will be updated (or all of them if null is passed)
* @param object
* @throws AerospikeException
* @param object The object to update.
* @throws AerospikeException an AerospikeException will be thrown in case of an error.
*/
public void update(@NotNull Object object, String ... binNames) throws AerospikeException {
save(null, object, RecordExistsAction.UPDATE, binNames);
Expand Down Expand Up @@ -351,7 +336,7 @@ public <T> T read(@NotNull Class<T> clazz, @NotNull Object userKey) throws Aeros
}

/**
* This method should not be used: It is used by mappers to correctly resolved dependencies. Use read(clazz, userkey) instead
* This method should not be used: It is used by mappers to correctly resolved dependencies. Use read(clazz, userKey) instead
*/
public <T> T read(@NotNull Class<T> clazz, @NotNull Object userKey, boolean resolveDependencies) throws AerospikeException {
ClassCacheEntry<T> entry = getEntryAndValidateNamespace(clazz);
Expand All @@ -360,7 +345,7 @@ public <T> T read(@NotNull Class<T> clazz, @NotNull Object userKey, boolean reso
return read(null, clazz, key, entry, resolveDependencies);
}

private <T> T read(Policy readPolicy, @NotNull Class<T> clazz, @NotNull Key key, @NotNull ClassCacheEntry<T> entry, boolean resolveDepenencies) {
private <T> T read(Policy readPolicy, @NotNull Class<T> clazz, @NotNull Key key, @NotNull ClassCacheEntry<T> entry, boolean resolveDependencies) {
if (readPolicy == null) {
readPolicy = entry.getReadPolicy();
}
Expand All @@ -371,8 +356,7 @@ private <T> T read(Policy readPolicy, @NotNull Class<T> clazz, @NotNull Key key,
} else {
try {
ThreadLocalKeySaver.save(key);
T result = convertToObject(clazz, record, entry, resolveDepenencies);
return result;
return convertToObject(clazz, record, entry, resolveDependencies);
} catch (ReflectiveOperationException e) {
throw new AerospikeException(e);
}
Expand Down Expand Up @@ -413,7 +397,7 @@ public <T> T[] read(BatchPolicy batchPolicy, @NotNull Class<T> clazz, @NotNull O
keys[i] = new Key(entry.getNamespace(), set, Value.get(entry.translateKeyToAerospikeKey(userKeys[i])));
}
}

return this.readBatch(batchPolicy, clazz, keys, entry);
}

Expand Down Expand Up @@ -495,13 +479,13 @@ public boolean delete(WritePolicy writePolicy, @NotNull Object object) throws Ae
* </ul>
* These operation can all be done without having the full set of transactions
* @param <T> the type of the elements in the list.
* @param object
* @param binName
* @param elementClazz
* @return
* @param object The object that will use as a base for the virtual list.
* @param binName The Aerospike bin name.
* @param elementClazz The class of the elements in the list.
* @return A virtual list.
*/
public <T> VirtualList<T> asBackedList(@NotNull Object object, @NotNull String binName, Class<T> elementClazz) {
return new VirtualList<T>(this, object, binName, elementClazz);
return new VirtualList<>(this, object, binName, elementClazz);
}

/**
Expand All @@ -521,13 +505,14 @@ public <T> VirtualList<T> asBackedList(@NotNull Object object, @NotNull String b
* </ul>
* These operation can all be done without having the full set of transactions
* @param <T> the type of the elements in the list.
* @param object
* @param binName
* @param elementClazz
* @return
* @param owningClazz Used for the definitions of how to map the list elements.
* @param key The key to map the object to the database.
* @param binName The Aerospike bin name.
* @param elementClazz The class of the elements in the list.
* @return A virtual list.
*/
public <T> VirtualList<T> asBackedList(@NotNull Class<?> owningClazz, @NotNull Object key, @NotNull String binName, Class<T> elementClazz) {
return new VirtualList<T>(this, owningClazz, key, binName, elementClazz);
return new VirtualList<>(this, owningClazz, key, binName, elementClazz);
}

public <T> void find(@NotNull Class<T> clazz, Function<T, Boolean> function) throws AerospikeException {
Expand Down Expand Up @@ -565,11 +550,10 @@ public <T> void find(@NotNull Class<T> clazz, Function<T, Boolean> function) thr
/**
* Given a record loaded from Aerospike and a class type, attempt to convert the record to
* an instance of the passed class.
* @param <T>
* @param clazz
* @param record
* @return
* @throws ReflectiveOperationException
* @param clazz The class type to convert the Aerospike record to.
* @param record The Aerospike record to convert.
* @return A virtual list.
* @throws AerospikeException an AerospikeException will be thrown in case of an encountering a ReflectiveOperationException.
*/
public <T> T convertToObject(Class<T> clazz, Record record) {
try {
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/aerospike/mapper/tools/ClassCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.aerospike.mapper.tools.configuration.Configuration;

public class ClassCache {
private static ClassCache instance = new ClassCache();
private static final ClassCache instance = new ClassCache();

public static ClassCache getInstance() {
return instance;
Expand All @@ -30,12 +30,12 @@ enum PolicyType {
QUERY
}

private Map<Class<?>, ClassCacheEntry> cacheMap = new HashMap<>();
private Map<String, ClassConfig> classesConfig = new HashMap<>();
private Map<PolicyType, Policy> defaultPolicies = new HashMap<>();
private Map<String, ClassCacheEntry> storedNameToCacheEntry = new HashMap<>();
private Map<PolicyType, Map<Class<?>, Policy>> childrenPolicies = new HashMap<>();
private Map<PolicyType, Map<Class<?>, Policy>> specificPolicies = new HashMap<>();
private final Map<Class<?>, ClassCacheEntry> cacheMap = new HashMap<>();
private final Map<String, ClassConfig> classesConfig = new HashMap<>();
private final Map<PolicyType, Policy> defaultPolicies = new HashMap<>();
private final Map<String, ClassCacheEntry> storedNameToCacheEntry = new HashMap<>();
private final Map<PolicyType, Map<Class<?>, Policy>> childrenPolicies = new HashMap<>();
private final Map<PolicyType, Map<Class<?>, Policy>> specificPolicies = new HashMap<>();

private ClassCache() {
for (PolicyType thisType : PolicyType.values()) {
Expand All @@ -51,12 +51,12 @@ public <T> ClassCacheEntry<T> loadClass(@NotNull Class<T> clazz, AeroMapper mapp
ClassCacheEntry<T> entry = cacheMap.get(clazz);
if (entry == null) {
try {
entry = new ClassCacheEntry<T>(clazz, mapper, getClassConfig(clazz),
determinePolicy(clazz, PolicyType.READ),
(WritePolicy)determinePolicy(clazz, PolicyType.WRITE),
(BatchPolicy)determinePolicy(clazz, PolicyType.BATCH),
(QueryPolicy)determinePolicy(clazz, PolicyType.QUERY),
(ScanPolicy)determinePolicy(clazz, PolicyType.SCAN));
entry = new ClassCacheEntry<>(clazz, mapper, getClassConfig(clazz),
determinePolicy(clazz, PolicyType.READ),
(WritePolicy) determinePolicy(clazz, PolicyType.WRITE),
(BatchPolicy) determinePolicy(clazz, PolicyType.BATCH),
(QueryPolicy) determinePolicy(clazz, PolicyType.QUERY),
(ScanPolicy) determinePolicy(clazz, PolicyType.SCAN));
}
catch (IllegalArgumentException iae) {
return null;
Expand Down Expand Up @@ -114,7 +114,7 @@ private Policy determinePolicy(@NotNull Class<?> clazz, @NotNull PolicyType poli
if (result != null) {
return result;
}
// Otherwise, iterate up class heirarchy looking for the policy.
// Otherwise, iterate up class hierarchy looking for the policy.
Class<?> thisClass = clazz;
while (thisClass != null) {
Policy aPolicy = childrenPolicies.get(policyType).get(thisClass);
Expand Down
Loading