From 2499c4dc14f95c4e6f28854e345a90ab4e8c494e Mon Sep 17 00:00:00 2001 From: godotg Date: Sat, 21 Oct 2023 11:09:16 +0800 Subject: [PATCH] ref[storage]: refactor the storage module --- .../zfoo/storage/manager/StorageObject.java | 103 +++++++++--------- .../com/zfoo/storage/util/LambdaUtils.java | 6 +- .../zfoo/storage/util/LambdaFunctionTest.java | 2 +- 3 files changed, 59 insertions(+), 52 deletions(-) diff --git a/storage/src/main/java/com/zfoo/storage/manager/StorageObject.java b/storage/src/main/java/com/zfoo/storage/manager/StorageObject.java index 1ac6daec1..ae6a6b87f 100644 --- a/storage/src/main/java/com/zfoo/storage/manager/StorageObject.java +++ b/storage/src/main/java/com/zfoo/storage/manager/StorageObject.java @@ -32,11 +32,11 @@ * @author godotg */ public class StorageObject implements IStorage { - private Map dataMap = new HashMap<>(64); + private Map dataMap; // 非唯一索引 - protected Map>> indexMap = new HashMap<>(16); + protected Map>> indexMap = new HashMap<>(); // 唯一索引 - protected Map> uniqueIndexMap = new HashMap<>(16); + protected Map> uniqueIndexMap = new HashMap<>(); protected Class clazz; protected IdDef idDef; @@ -46,28 +46,67 @@ public class StorageObject implements IStorage { public static StorageObject parse(InputStream inputStream, Class resourceClazz, String suffix) { - var storage = new StorageObject<>(); - storage.clazz = resourceClazz; var idDef = IdDef.valueOf(resourceClazz); - storage.idDef = idDef; - storage.indexDefMap = Collections.unmodifiableMap(IndexDef.createResourceIndexes(resourceClazz)); + var indexDefMap = Collections.unmodifiableMap(IndexDef.createResourceIndexes(resourceClazz)); try { var list = ResourceInterpreter.read(inputStream, resourceClazz, suffix); - storage.putAll(list); + var storage = new StorageObject<>(resourceClazz, idDef, indexDefMap, list); + + var idType = idDef.getField().getType(); + if (idType == int.class || idType == Integer.class) { + return new StorageInt<>(storage); + } else if (idType == long.class || idType == Long.class) { + return new StorageLong<>(storage); + } else { + return storage; + } + } catch (Throwable e) { throw new RuntimeException(e.getMessage(), e); } finally { IOUtils.closeIO(inputStream); } + } + + protected StorageObject() { + } + + public StorageObject(Class clazz, IdDef idDef, Map indexDefMap, List values) { + this.dataMap = new HashMap<>(CollectionUtils.capacity(values.size())); + this.clazz = clazz; + this.idDef = idDef; + this.indexDefMap = indexDefMap; + for (var value: values) { + @SuppressWarnings("unchecked") + var id = (K) ReflectionUtils.getField(idDef.getField(), value); - var idType = idDef.getField().getType(); - if (idType == int.class || idType == Integer.class) { - return new StorageInt<>(storage); - } else if (idType == long.class || idType == Long.class) { - return new StorageLong<>(storage); - } else { - return storage; + if (id == null) { + throw new RuntimeException("There is an item with an unconfigured id in the static resource"); + } + if (dataMap.containsKey(id)) { + throw new RuntimeException(StringUtils.format("Duplicate [id:{}] of static resource [resource:{}]", id, clazz.getSimpleName())); + } + // 添加资源 + @SuppressWarnings("unchecked") + var v = (V) value; + dataMap.put(id, v); + // 添加索引 + for (var def : indexDefMap.values()) { + // 使用field的名称作为索引的名称 + var indexKey = def.getField().getName(); + var indexValue = ReflectionUtils.getField(def.getField(), v); + if (def.isUnique()) { + var uniqueIndex = uniqueIndexMap.computeIfAbsent(indexKey, it -> new HashMap<>(values.size())); + if (uniqueIndex.put(indexValue, v) != null) { + throw new RuntimeException(StringUtils.format("Duplicate unique index [index:{}][value:{}] of static resource [class:{}]", indexKey, indexValue, clazz.getName())); + } + } else { + var index = indexMap.computeIfAbsent(indexKey, it -> new HashMap<>(values.size())); + var list = index.computeIfAbsent(indexValue, it -> new ArrayList()); + list.add(v); + } + } } } @@ -179,38 +218,4 @@ public int size() { return dataMap.size(); } - public void putAll(List values) { - for (var value: values) { - @SuppressWarnings("unchecked") - var id = (K) ReflectionUtils.getField(idDef.getField(), value); - - if (id == null) { - throw new RuntimeException("There is an item with an unconfigured id in the static resource"); - } - if (dataMap.containsKey(id)) { - throw new RuntimeException(StringUtils.format("Duplicate [id:{}] of static resource [resource:{}]", id, clazz.getSimpleName())); - } - // 添加资源 - @SuppressWarnings("unchecked") - var v = (V) value; - dataMap.put(id, v); - // 添加索引 - for (var def : indexDefMap.values()) { - // 使用field的名称作为索引的名称 - var indexKey = def.getField().getName(); - var indexValue = ReflectionUtils.getField(def.getField(), v); - if (def.isUnique()) { - var uniqueIndex = uniqueIndexMap.computeIfAbsent(indexKey, it -> new HashMap<>(values.size())); - if (uniqueIndex.put(indexValue, v) != null) { - throw new RuntimeException(StringUtils.format("Duplicate unique index [index:{}][value:{}] of static resource [class:{}]", indexKey, indexValue, clazz.getName())); - } - } else { - var index = indexMap.computeIfAbsent(indexKey, it -> new HashMap<>(values.size())); - var list = index.computeIfAbsent(indexValue, it -> new ArrayList()); - list.add(v); - } - } - } - } - } diff --git a/storage/src/main/java/com/zfoo/storage/util/LambdaUtils.java b/storage/src/main/java/com/zfoo/storage/util/LambdaUtils.java index 17a7a7e6a..d8f396c16 100644 --- a/storage/src/main/java/com/zfoo/storage/util/LambdaUtils.java +++ b/storage/src/main/java/com/zfoo/storage/util/LambdaUtils.java @@ -28,6 +28,7 @@ public static LambdaMeta extract(Serializable func) { if (func instanceof Proxy) { return new IdeaProxyLambdaMeta((Proxy) func); } + // 2. 反射读取 try { Class clazz = func.getClass(); @@ -35,9 +36,10 @@ public static LambdaMeta extract(Serializable func) { ReflectionUtils.makeAccessible(method); return new ReflectLambdaMeta((java.lang.invoke.SerializedLambda) method.invoke(func)); } catch (Throwable e) { - // 3. 反射失败使用序列化的方式读取 - return new ShadowLambdaMeta(SerializedLambda.extract(func)); } + + // 3. 反射失败使用序列化的方式读取 + return new ShadowLambdaMeta(SerializedLambda.extract(func)); } diff --git a/storage/src/test/java/com/zfoo/storage/util/LambdaFunctionTest.java b/storage/src/test/java/com/zfoo/storage/util/LambdaFunctionTest.java index 9b8850788..465988035 100644 --- a/storage/src/test/java/com/zfoo/storage/util/LambdaFunctionTest.java +++ b/storage/src/test/java/com/zfoo/storage/util/LambdaFunctionTest.java @@ -24,7 +24,7 @@ * @author veione */ public class LambdaFunctionTest { - //https://blog.csdn.net/iteye_19045/article/details/119299015 + // https://blog.csdn.net/iteye_19045/article/details/119299015 @Test public void testFuncSerialization() throws Exception { Func1 func = TeacherResource::name;