Skip to content

Commit

Permalink
ref[storage]: refactor the storage module
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Oct 21, 2023
1 parent 2499c4d commit c466b5d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 71 deletions.
4 changes: 0 additions & 4 deletions storage/src/main/java/com/zfoo/storage/StorageContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ public static <V, K> V get(Class<V> clazz, K 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) {
return instance.storageManager.getStorage(clazz).getIndexes(func, index);
}
Expand Down
22 changes: 11 additions & 11 deletions storage/src/main/java/com/zfoo/storage/manager/StorageInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,6 @@ public V get(long id) {
return get((int) id);
}

@Override
public int size() {
return dataMap.size();
}

@Override
public void recycleStorage() {
super.recycleStorage();
dataMap = null;
}

@Override
public Collection<V> getAll() {
return dataMap.values().stream().toList();
Expand All @@ -95,4 +84,15 @@ public Map<K, V> getData() {
return map;
}

@Override
public int size() {
return dataMap.size();
}

@Override
public void recycleStorage() {
super.recycleStorage();
dataMap = null;
}

}
22 changes: 11 additions & 11 deletions storage/src/main/java/com/zfoo/storage/manager/StorageLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,6 @@ public V get(long id) {
return result;
}

@Override
public int size() {
return dataMap.size();
}

@Override
public void recycleStorage() {
super.recycleStorage();
dataMap = null;
}

@Override
public Collection<V> getAll() {
return dataMap.values().stream().toList();
Expand All @@ -95,4 +84,15 @@ public Map<K, V> getData() {
return map;
}

@Override
public int size() {
return dataMap.size();
}

@Override
public void recycleStorage() {
super.recycleStorage();
dataMap = null;
}

}
60 changes: 27 additions & 33 deletions storage/src/main/java/com/zfoo/storage/manager/StorageObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ protected StorageObject() {
}

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.idDef = idDef;
this.indexDefMap = indexDefMap;
for (var value: values) {
for (var value : values) {
@SuppressWarnings("unchecked")
var id = (K) ReflectionUtils.getField(idDef.getField(), value);

Expand Down Expand Up @@ -150,47 +150,16 @@ public V get(long id) {
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
public Collection<V> getAll() {
return dataMap.values();
}

@Override
public List<V> getList() {
Collection<V> all = getAll();
return all.stream().toList();
}

@Override
public Map<K, V> getData() {
return Collections.unmodifiableMap(dataMap);
}

@Override
public IdDef getIdDef() {
return idDef;
}

@Override
public <INDEX> List<V> getIndexes(Func1<V, INDEX> func, INDEX index) {
String indexName = LambdaUtils.getFieldName(func);
Expand All @@ -213,6 +182,31 @@ public <INDEX> V getUniqueIndex(Func1<V, INDEX> func, INDEX index) {
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
public int size() {
return dataMap.size();
Expand Down
19 changes: 9 additions & 10 deletions storage/src/main/java/com/zfoo/storage/model/IStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,23 @@ public interface IStorage<K, V> {

V get(long id);

void recycleStorage();

boolean isRecycle();

void setRecycle(boolean recycle);

Collection<V> getAll();

List<V> getList();

Map<K, V> getData();

IdDef getIdDef();

<INDEX> List<V> getIndexes(Func1<V, INDEX> func, INDEX index);

@Nullable
<INDEX> V getUniqueIndex(Func1<V, INDEX> func, INDEX uindex);

int size();

void recycleStorage();

boolean isRecycle();

void setRecycle(boolean recycle);

IdDef getIdDef();

}
2 changes: 0 additions & 2 deletions storage/src/test/java/com/zfoo/storage/ApplicationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public void startStorageTest() {

IStorage<Integer, StudentResource> storage1 = StorageContext.getStorageManager().getStorage(StudentResource.class);

//获取所有数据
List<StudentResource> list = StorageContext.getList(StudentResource.class);
//根据主键获取数据
StudentResource studentResource = StorageContext.get(StudentResource.class, 1001);
TeacherResource teacherResource = StorageContext.get(TeacherResource.class, 1001);
Expand Down

0 comments on commit c466b5d

Please sign in to comment.