mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-06-01 18:14:17 +00:00
ref[storage]: refactor storage
This commit is contained in:
@@ -471,7 +471,7 @@ public class OrmManager implements IOrmManager {
|
||||
ReflectionUtils.publicEmptyConstructor(clazz);
|
||||
|
||||
// 不能使用Storage的Index注解
|
||||
var storageIndexes = ReflectionUtils.getFieldsByAnnoNameInPOJOClass(clazz, "com.zfoo.storage.model.anno.Index");
|
||||
var storageIndexes = ReflectionUtils.getFieldsByAnnoNameInPOJOClass(clazz, "com.zfoo.storage.anno.Index");
|
||||
if (ArrayUtils.isNotEmpty(storageIndexes)) {
|
||||
throw new RunException("在Orm中只能使用Orm的Index注解,不能使用Storage的Index注解,为了避免不必要的误解和增强项目的健壮性,禁止这样使用");
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ public interface IStorageManager {
|
||||
|
||||
Map<Class<?>, IStorage<?, ?>> storageMap();
|
||||
|
||||
void updateStorage(Class<?> clazz, ObjectStorage<?, ?> storageObject);
|
||||
void updateStorage(Class<?> clazz, IStorage<?, ?> storageObject);
|
||||
|
||||
StorageConfig storageConfig();
|
||||
}
|
||||
|
||||
+2
-2
@@ -23,11 +23,11 @@ import java.util.Map;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class PrimitiveIntStorage<K, V> extends ObjectStorage<K, V> {
|
||||
public class StorageInt<K, V> extends StorageObject<K, V> {
|
||||
|
||||
private IntObjectHashMap<V> dataMap;
|
||||
|
||||
public PrimitiveIntStorage(ObjectStorage<K, V> storageObject) {
|
||||
public StorageInt(StorageObject<K, V> storageObject) {
|
||||
this.dataMap = new IntObjectHashMap<V>(storageObject.size());
|
||||
this.dataMap.putAll((Map<? extends Integer, ? extends V>) storageObject.getData());
|
||||
super.indexMap = storageObject.indexMap;
|
||||
+2
-2
@@ -23,11 +23,11 @@ import java.util.Map;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class PrimitiveLongStorage<K, V> extends ObjectStorage<K, V> {
|
||||
public class StorageLong<K, V> extends StorageObject<K, V> {
|
||||
|
||||
private LongObjectHashMap<V> dataMap;
|
||||
|
||||
public PrimitiveLongStorage(ObjectStorage<K, V> storageObject) {
|
||||
public StorageLong(StorageObject<K, V> storageObject) {
|
||||
this.dataMap = new LongObjectHashMap<V>(storageObject.size());
|
||||
this.dataMap.putAll((Map<? extends Long, ? extends V>) storageObject.getData());
|
||||
super.indexMap = storageObject.indexMap;
|
||||
@@ -125,7 +125,7 @@ public class StorageManager implements IStorageManager {
|
||||
var clazz = definition.getClazz();
|
||||
var resource = definition.getResource();
|
||||
var fileExtName = FileUtils.fileExtName(resource.getFilename());
|
||||
ObjectStorage<?, ?> storageObject = ObjectStorage.parse(resource.getInputStream(), clazz, fileExtName);
|
||||
StorageObject<?, ?> storageObject = StorageObject.parse(resource.getInputStream(), clazz, fileExtName);
|
||||
storageMap.putIfAbsent(clazz, storageObject);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -205,7 +205,7 @@ public class StorageManager implements IStorageManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateStorage(Class<?> clazz, ObjectStorage<?, ?> storageObject) {
|
||||
public void updateStorage(Class<?> clazz, IStorage<?, ?> storageObject) {
|
||||
storageMap.put(clazz, storageObject);
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -29,7 +29,7 @@ import java.util.*;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class ObjectStorage<K, V> implements IStorage<K, V> {
|
||||
public class StorageObject<K, V> implements IStorage<K, V> {
|
||||
|
||||
private Map<K, V> dataMap = new HashMap<>();
|
||||
// 非唯一索引
|
||||
@@ -44,9 +44,9 @@ public class ObjectStorage<K, V> implements IStorage<K, V> {
|
||||
protected boolean recycle = true;
|
||||
|
||||
|
||||
public static ObjectStorage<?, ?> parse(InputStream inputStream, Class<?> resourceClazz, String suffix) {
|
||||
public static StorageObject<?, ?> parse(InputStream inputStream, Class<?> resourceClazz, String suffix) {
|
||||
try {
|
||||
ObjectStorage<?, ?> storageObject = new ObjectStorage<>();
|
||||
StorageObject<?, ?> storageObject = new StorageObject<>();
|
||||
storageObject.clazz = resourceClazz;
|
||||
var idDef = IdDef.valueOf(resourceClazz);
|
||||
storageObject.idDef = idDef;
|
||||
@@ -57,9 +57,9 @@ public class ObjectStorage<K, V> implements IStorage<K, V> {
|
||||
}
|
||||
var idType = idDef.getField().getType();
|
||||
if (idType == int.class || idType == Integer.class) {
|
||||
return new PrimitiveIntStorage<>(storageObject);
|
||||
return new StorageInt<>(storageObject);
|
||||
} else if (idType == long.class || idType == Long.class) {
|
||||
return new PrimitiveLongStorage<>(storageObject);
|
||||
return new StorageLong<>(storageObject);
|
||||
} else {
|
||||
return storageObject;
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
package com.zfoo.storage;
|
||||
|
||||
import com.zfoo.storage.anno.StorageInjection;
|
||||
import com.zfoo.storage.manager.ObjectStorage;
|
||||
import com.zfoo.storage.manager.IStorage;
|
||||
import com.zfoo.storage.resource.StudentCsvResource;
|
||||
import com.zfoo.storage.resource.StudentResource;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -27,8 +27,8 @@ import org.springframework.stereotype.Component;
|
||||
public class StudentManager {
|
||||
|
||||
@StorageInjection
|
||||
public ObjectStorage<Integer, StudentResource> studentResources;
|
||||
public IStorage<Integer, StudentResource> studentResources;
|
||||
@StorageInjection
|
||||
public ObjectStorage<Integer, StudentCsvResource> studentCsvResources;
|
||||
public IStorage<Integer, StudentCsvResource> studentCsvResources;
|
||||
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@ import com.zfoo.storage.anno.AliasFieldName;
|
||||
import com.zfoo.storage.anno.Id;
|
||||
import com.zfoo.storage.anno.Storage;
|
||||
import com.zfoo.storage.config.StorageConfig;
|
||||
import com.zfoo.storage.manager.ObjectStorage;
|
||||
import com.zfoo.storage.manager.StorageManager;
|
||||
import com.zfoo.storage.manager.StorageObject;
|
||||
import com.zfoo.storage.util.ExportUtils;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import io.netty.buffer.UnpooledHeapByteBuf;
|
||||
@@ -105,7 +105,7 @@ public class ExportBinaryTest {
|
||||
var bytes = ByteBufUtils.readAllBytes(buffer);
|
||||
FileUtils.writeInputStreamToFile(new File("D:/github/godot-bird/binary_data.cfg"), new ByteArrayInputStream(bytes));
|
||||
|
||||
var storage = (ObjectStorage<Integer, StudentResource>) storageManager.getStorage(StudentResource.class);
|
||||
var storage = (StorageObject<Integer, StudentResource>) storageManager.getStorage(StudentResource.class);
|
||||
for (StudentResource resource : storage.getAll()) {
|
||||
System.out.println(JsonUtils.object2String(resource));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user