perf[storage]: perfect generic methods

This commit is contained in:
godotg
2023-09-19 20:43:48 +08:00
parent f045d44d37
commit cf33697abe
7 changed files with 18 additions and 75 deletions
@@ -60,12 +60,12 @@ public class StorageContext implements ApplicationListener<ApplicationContextEve
return instance.storageManager.getStorage(clazz).getList();
}
public static <K, V> List<V> getIndexes(Class<V> clazz, Func1<V, ?> function, K keyId) {
return instance.storageManager.getStorage(clazz).getIndexes(function, keyId);
public static <Index, V> List<V> getIndexes(Class<V> clazz, Func1<V, Index> function, Index index) {
return instance.storageManager.getStorage(clazz).getIndexes(function, index);
}
public static <K, V> V getUniqueIndex(Class<V> clazz, Func1<V, ?> func, K keyId) {
return instance.storageManager.getStorage(clazz).getUniqueIndex(func, keyId);
public static <INDEX, V> V getUniqueIndex(Class<V> clazz, Func1<V, INDEX> func, INDEX uindex) {
return instance.storageManager.getStorage(clazz).getUniqueIndex(func, uindex);
}
@Override
@@ -16,12 +16,7 @@ package com.zfoo.storage.manager;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.exception.ExceptionUtils;
import com.zfoo.protocol.exception.RunException;
import com.zfoo.protocol.util.ClassUtils;
import com.zfoo.protocol.util.FieldUtils;
import com.zfoo.protocol.util.FileUtils;
import com.zfoo.protocol.util.GraalVmUtils;
import com.zfoo.protocol.util.ReflectionUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.protocol.util.*;
import com.zfoo.storage.StorageContext;
import com.zfoo.storage.anno.GraalvmNativeStorage;
import com.zfoo.storage.anno.Id;
@@ -48,13 +43,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -207,14 +196,14 @@ public class StorageManager implements IStorageManager {
return (List<T>) storage.getAll();
}
public <T, K> List<T> getIndexes(Class<T> clazz, Func1<T, ?> func, K indexId) {
public <T, INDEX> List<T> getIndexes(Class<T> clazz, Func1<T, INDEX> func, INDEX index) {
var storage = getStorage(clazz);
return storage.getIndexes(func, indexId);
return storage.getIndexes(func, index);
}
public <T, UQ> T get(Class<T> clazz, UQ keyId) {
IStorage<UQ, T> storage = getStorage(clazz);
return storage.get(keyId);
public <T, K> T get(Class<T> clazz, K key) {
var storage = getStorage(clazz);
return storage.get(key);
}
@Override
@@ -26,12 +26,7 @@ import com.zfoo.storage.util.function.Func1;
import org.springframework.lang.Nullable;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* @author godotg
@@ -158,11 +153,11 @@ public class StorageObject<K, V> implements IStorage<K, V> {
}
@Override
public <INDEX> List<V> getIndexes(Func1<V, ?> func, INDEX key) {
public <INDEX> List<V> getIndexes(Func1<V, INDEX> func, INDEX index) {
String indexName = LambdaUtils.getFieldName(func);
var indexValues = indexMap.get(indexName);
AssertionUtils.notNull(indexValues, "The index of [indexName:{}] does not exist in the static resource [resource:{}]", indexName, clazz.getSimpleName());
var values = indexValues.get(key);
var values = indexValues.get(index);
if (CollectionUtils.isEmpty(values)) {
return Collections.emptyList();
}
@@ -171,11 +166,11 @@ public class StorageObject<K, V> implements IStorage<K, V> {
@Nullable
@Override
public <UINDEX> V getUniqueIndex(Func1<V, ?> func, UINDEX key) {
public <INDEX> V getUniqueIndex(Func1<V, INDEX> func, INDEX index) {
String uniqueIndexName = LambdaUtils.getFieldName(func);
var indexValueMap = uniqueIndexMap.get(uniqueIndexName);
AssertionUtils.notNull(indexValueMap, "There is no a unique index for [uniqueIndexName:{}] in the static resource [resource:{}]", uniqueIndexName, clazz.getSimpleName());
var value = indexValueMap.get(key);
var value = indexValueMap.get(index);
return value;
}
@@ -50,10 +50,10 @@ public interface IStorage<K, V> {
IdDef getIdDef();
<INDEX> List<V> getIndexes(Func1<V, ?> function, INDEX key);
<INDEX> List<V> getIndexes(Func1<V, INDEX> function, INDEX index);
@Nullable
<UINDEX> V getUniqueIndex(Func1<V, ?> function, UINDEX key);
<INDEX> V getUniqueIndex(Func1<V, INDEX> function, INDEX uindex);
int size();
}
@@ -26,18 +26,4 @@ public interface Func<P, R> extends Serializable {
@SuppressWarnings("unchecked")
R call(P... parameters) throws Exception;
/**
* 执行函数,异常包装为RuntimeException
*
* @param parameters 参数列表
* @return 函数执行结果
*/
@SuppressWarnings("unchecked")
default R callWithRuntimeException(P... parameters){
try {
return call(parameters);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@@ -23,17 +23,4 @@ public interface Func0<R> extends Serializable {
*/
R call() throws Exception;
/**
* 执行函数,异常包装为RuntimeException
*
* @return 函数执行结果
* @since 5.3.6
*/
default R callWithRuntimeException(){
try {
return call();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@@ -26,18 +26,4 @@ public interface Func1<P, R> extends Serializable {
*/
R call(P parameter) throws Exception;
/**
* 执行函数,异常包装为RuntimeException
*
* @param parameter 参数
* @return 函数执行结果
* @since 5.3.6
*/
default R callWithRuntimeException(P parameter){
try {
return call(parameter);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}