mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-18 10:15:11 +00:00
ref[storage]: refactor the storage module
This commit is contained in:
1 parent
2499c4dc14
commit
c466b5d1f2
6 files changed
+58
-71
No files matched your search
@@ -56,10 +56,6 @@ public class StorageContext implements ApplicationListener<ApplicationContextEve
|
|||||||
return instance.storageManager.getStorage(clazz).get(id);
|
return instance.storageManager.getStorage(clazz).get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <V> List<V> getList(Class<V> clazz) {
|
|
||||||
return instance.storageManager.getStorage(clazz).getList();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <INDEX, V> List<V> getIndexes(Class<V> clazz, Func1<V, INDEX> func, INDEX index) {
|
public static <INDEX, V> List<V> getIndexes(Class<V> clazz, Func1<V, INDEX> func, INDEX index) {
|
||||||
return instance.storageManager.getStorage(clazz).getIndexes(func, index);
|
return instance.storageManager.getStorage(clazz).getIndexes(func, index);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,17 +72,6 @@ public class StorageInt<K, V> extends StorageObject<K, V> {
|
|||||||
return get((int) id);
|
return get((int) id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int size() {
|
|
||||||
return dataMap.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void recycleStorage() {
|
|
||||||
super.recycleStorage();
|
|
||||||
dataMap = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<V> getAll() {
|
public Collection<V> getAll() {
|
||||||
return dataMap.values().stream().toList();
|
return dataMap.values().stream().toList();
|
||||||
@@ -95,4 +84,15 @@ public class StorageInt<K, V> extends StorageObject<K, V> {
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return dataMap.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void recycleStorage() {
|
||||||
|
super.recycleStorage();
|
||||||
|
dataMap = null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -72,17 +72,6 @@ public class StorageLong<K, V> extends StorageObject<K, V> {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int size() {
|
|
||||||
return dataMap.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void recycleStorage() {
|
|
||||||
super.recycleStorage();
|
|
||||||
dataMap = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<V> getAll() {
|
public Collection<V> getAll() {
|
||||||
return dataMap.values().stream().toList();
|
return dataMap.values().stream().toList();
|
||||||
@@ -95,4 +84,15 @@ public class StorageLong<K, V> extends StorageObject<K, V> {
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return dataMap.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void recycleStorage() {
|
||||||
|
super.recycleStorage();
|
||||||
|
dataMap = null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -73,11 +73,11 @@ public class StorageObject<K, V> implements IStorage<K, V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public StorageObject(Class<?> clazz, IdDef idDef, Map<String, IndexDef> indexDefMap, List<?> values) {
|
public StorageObject(Class<?> clazz, IdDef idDef, Map<String, IndexDef> indexDefMap, List<?> values) {
|
||||||
this.dataMap = new HashMap<>(CollectionUtils.capacity(values.size()));
|
this.dataMap = new HashMap<>(CollectionUtils.capacity(values.size()));
|
||||||
this.clazz = clazz;
|
this.clazz = clazz;
|
||||||
this.idDef = idDef;
|
this.idDef = idDef;
|
||||||
this.indexDefMap = indexDefMap;
|
this.indexDefMap = indexDefMap;
|
||||||
for (var value: values) {
|
for (var value : values) {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
var id = (K) ReflectionUtils.getField(idDef.getField(), value);
|
var id = (K) ReflectionUtils.getField(idDef.getField(), value);
|
||||||
|
|
||||||
@@ -150,47 +150,16 @@ public class StorageObject<K, V> implements IStorage<K, V> {
|
|||||||
return get(key);
|
return get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void recycleStorage() {
|
|
||||||
recycle = true;
|
|
||||||
dataMap = null;
|
|
||||||
indexMap = null;
|
|
||||||
uniqueIndexMap = null;
|
|
||||||
idDef = null;
|
|
||||||
indexDefMap = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isRecycle() {
|
|
||||||
return recycle;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setRecycle(boolean recycle) {
|
|
||||||
this.recycle = recycle;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<V> getAll() {
|
public Collection<V> getAll() {
|
||||||
return dataMap.values();
|
return dataMap.values();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<V> getList() {
|
|
||||||
Collection<V> all = getAll();
|
|
||||||
return all.stream().toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<K, V> getData() {
|
public Map<K, V> getData() {
|
||||||
return Collections.unmodifiableMap(dataMap);
|
return Collections.unmodifiableMap(dataMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public IdDef getIdDef() {
|
|
||||||
return idDef;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <INDEX> List<V> getIndexes(Func1<V, INDEX> func, INDEX index) {
|
public <INDEX> List<V> getIndexes(Func1<V, INDEX> func, INDEX index) {
|
||||||
String indexName = LambdaUtils.getFieldName(func);
|
String indexName = LambdaUtils.getFieldName(func);
|
||||||
@@ -213,6 +182,31 @@ public class StorageObject<K, V> implements IStorage<K, V> {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void recycleStorage() {
|
||||||
|
recycle = true;
|
||||||
|
dataMap = null;
|
||||||
|
indexMap = null;
|
||||||
|
uniqueIndexMap = null;
|
||||||
|
idDef = null;
|
||||||
|
indexDefMap = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isRecycle() {
|
||||||
|
return recycle;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRecycle(boolean recycle) {
|
||||||
|
this.recycle = recycle;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public IdDef getIdDef() {
|
||||||
|
return idDef;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int size() {
|
public int size() {
|
||||||
return dataMap.size();
|
return dataMap.size();
|
||||||
|
|||||||
@@ -36,24 +36,23 @@ public interface IStorage<K, V> {
|
|||||||
|
|
||||||
V get(long id);
|
V get(long id);
|
||||||
|
|
||||||
void recycleStorage();
|
|
||||||
|
|
||||||
boolean isRecycle();
|
|
||||||
|
|
||||||
void setRecycle(boolean recycle);
|
|
||||||
|
|
||||||
Collection<V> getAll();
|
Collection<V> getAll();
|
||||||
|
|
||||||
List<V> getList();
|
|
||||||
|
|
||||||
Map<K, V> getData();
|
Map<K, V> getData();
|
||||||
|
|
||||||
IdDef getIdDef();
|
|
||||||
|
|
||||||
<INDEX> List<V> getIndexes(Func1<V, INDEX> func, INDEX index);
|
<INDEX> List<V> getIndexes(Func1<V, INDEX> func, INDEX index);
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
<INDEX> V getUniqueIndex(Func1<V, INDEX> func, INDEX uindex);
|
<INDEX> V getUniqueIndex(Func1<V, INDEX> func, INDEX uindex);
|
||||||
|
|
||||||
int size();
|
int size();
|
||||||
|
|
||||||
|
void recycleStorage();
|
||||||
|
|
||||||
|
boolean isRecycle();
|
||||||
|
|
||||||
|
void setRecycle(boolean recycle);
|
||||||
|
|
||||||
|
IdDef getIdDef();
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -51,8 +51,6 @@ public class ApplicationTest {
|
|||||||
|
|
||||||
IStorage<Integer, StudentResource> storage1 = StorageContext.getStorageManager().getStorage(StudentResource.class);
|
IStorage<Integer, StudentResource> storage1 = StorageContext.getStorageManager().getStorage(StudentResource.class);
|
||||||
|
|
||||||
//获取所有数据
|
|
||||||
List<StudentResource> list = StorageContext.getList(StudentResource.class);
|
|
||||||
//根据主键获取数据
|
//根据主键获取数据
|
||||||
StudentResource studentResource = StorageContext.get(StudentResource.class, 1001);
|
StudentResource studentResource = StorageContext.get(StudentResource.class, 1001);
|
||||||
TeacherResource teacherResource = StorageContext.get(TeacherResource.class, 1001);
|
TeacherResource teacherResource = StorageContext.get(TeacherResource.class, 1001);
|
||||||
|
|||||||
Reference in new issue
Block a user