mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-12 18:17:02 +00:00
ref[storage]: package structure
This commit is contained in:
1 parent
536f255486
commit
1da6b324af
24 files changed
+139
-147
No files matched your search
@@ -15,8 +15,8 @@ package com.zfoo.boot;
|
||||
import com.zfoo.protocol.util.ClassUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import com.zfoo.storage.StorageContext;
|
||||
import com.zfoo.storage.anno.GraalvmNativeStorage;
|
||||
import com.zfoo.storage.manager.StorageManager;
|
||||
import com.zfoo.storage.model.anno.GraalvmNativeResource;
|
||||
import com.zfoo.storage.model.config.StorageConfig;
|
||||
import com.zfoo.storage.model.resource.ResourceData;
|
||||
import com.zfoo.storage.model.resource.ResourceEnum;
|
||||
@@ -77,7 +77,7 @@ public class StorageAutoConfiguration {
|
||||
for (var className : ClassUtils.getAllClasses("")) {
|
||||
try {
|
||||
var clazz = Class.forName(className);
|
||||
if (!clazz.isAnnotationPresent(GraalvmNativeResource.class)) {
|
||||
if (!clazz.isAnnotationPresent(GraalvmNativeStorage.class)) {
|
||||
continue;
|
||||
}
|
||||
classes.add(clazz);
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.storage.anno;
|
||||
|
||||
import org.springframework.aot.hint.annotation.Reflective;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* EN: Specify the file column name. If not specified, the default column name will be consistent with the field name.
|
||||
* CN: 指定文件列名,不指定则默认列名与字段名一致
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
@Reflective
|
||||
public @interface AliasFieldName {
|
||||
String value();
|
||||
}
|
||||
+2
-3
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
@@ -11,7 +10,7 @@
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.storage.model.anno;
|
||||
package com.zfoo.storage.anno;
|
||||
|
||||
import org.springframework.aot.hint.annotation.Reflective;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -29,7 +28,7 @@ import java.lang.annotation.*;
|
||||
@Target({ElementType.TYPE})
|
||||
@Reflective
|
||||
@Component
|
||||
public @interface GraalvmNativeResource {
|
||||
public @interface GraalvmNativeStorage {
|
||||
|
||||
/**
|
||||
* graalvm build must specify resource path
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
@@ -11,7 +10,7 @@
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.storage.model.anno;
|
||||
package com.zfoo.storage.anno;
|
||||
|
||||
|
||||
import org.springframework.aot.hint.annotation.Reflective;
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
@@ -11,7 +10,7 @@
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.storage.model.anno;
|
||||
package com.zfoo.storage.anno;
|
||||
|
||||
import org.springframework.aot.hint.annotation.Reflective;
|
||||
|
||||
+3
-9
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
@@ -11,7 +10,7 @@
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.storage.model.anno;
|
||||
package com.zfoo.storage.anno;
|
||||
|
||||
import org.springframework.aot.hint.annotation.Reflective;
|
||||
|
||||
@@ -29,11 +28,6 @@ import java.lang.annotation.*;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE})
|
||||
@Reflective
|
||||
public @interface Resource {
|
||||
String alias() default "";
|
||||
|
||||
/**
|
||||
* graalvm build must specified path
|
||||
*/
|
||||
String nativeClassPath() default "";
|
||||
public @interface Storage {
|
||||
String value() default "";
|
||||
}
|
||||
+2
-3
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
@@ -11,7 +10,7 @@
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.storage.model.anno;
|
||||
package com.zfoo.storage.anno;
|
||||
|
||||
import org.springframework.aot.hint.annotation.Reflective;
|
||||
|
||||
@@ -29,7 +28,7 @@ import java.lang.annotation.*;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
@Reflective
|
||||
public @interface ResInjection {
|
||||
public @interface StorageInjection {
|
||||
|
||||
String value() default "";
|
||||
|
||||
@@ -15,8 +15,8 @@ package com.zfoo.storage.interpreter;
|
||||
import com.zfoo.protocol.exception.RunException;
|
||||
import com.zfoo.protocol.util.ReflectionUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import com.zfoo.storage.model.anno.ExcelFieldName;
|
||||
import com.zfoo.storage.model.anno.Id;
|
||||
import com.zfoo.storage.anno.AliasFieldName;
|
||||
import com.zfoo.storage.anno.Id;
|
||||
import com.zfoo.storage.model.resource.ResourceData;
|
||||
import com.zfoo.storage.model.resource.ResourceEnum;
|
||||
import com.zfoo.storage.strategy.*;
|
||||
@@ -100,7 +100,7 @@ public class ResourceInterpreter {
|
||||
|
||||
// 优先使用ExcelFieldName注解表示的值当作列名
|
||||
private static String getExcelFieldName(Field field) {
|
||||
return field.isAnnotationPresent(ExcelFieldName.class) ? field.getAnnotation(ExcelFieldName.class).value() : field.getName();
|
||||
return field.isAnnotationPresent(AliasFieldName.class) ? field.getAnnotation(AliasFieldName.class).value() : field.getName();
|
||||
}
|
||||
|
||||
// 只读取代码里写的字段
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
package com.zfoo.storage.manager;
|
||||
|
||||
import com.zfoo.storage.model.config.StorageConfig;
|
||||
import com.zfoo.storage.model.vo.Storage;
|
||||
import com.zfoo.storage.model.vo.StorageObject;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -39,11 +39,11 @@ public interface IStorageManager {
|
||||
*/
|
||||
void initAfter();
|
||||
|
||||
Storage<?, ?> getStorage(Class<?> clazz);
|
||||
StorageObject<?, ?> getStorage(Class<?> clazz);
|
||||
|
||||
Map<Class<?>, Storage<?, ?>> storageMap();
|
||||
Map<Class<?>, StorageObject<?, ?>> storageMap();
|
||||
|
||||
void updateStorage(Class<?> clazz, Storage<?, ?> storage);
|
||||
void updateStorage(Class<?> clazz, StorageObject<?, ?> storageObject);
|
||||
|
||||
StorageConfig storageConfig();
|
||||
}
|
||||
@@ -16,19 +16,16 @@ 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.FileUtils;
|
||||
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.model.anno.Id;
|
||||
import com.zfoo.storage.model.anno.GraalvmNativeResource;
|
||||
import com.zfoo.storage.model.anno.ResInjection;
|
||||
import com.zfoo.storage.anno.GraalvmNativeStorage;
|
||||
import com.zfoo.storage.anno.Id;
|
||||
import com.zfoo.storage.anno.Storage;
|
||||
import com.zfoo.storage.anno.StorageInjection;
|
||||
import com.zfoo.storage.model.config.StorageConfig;
|
||||
import com.zfoo.storage.model.resource.ResourceEnum;
|
||||
import com.zfoo.storage.model.vo.ResourceDef;
|
||||
import com.zfoo.storage.model.vo.Storage;
|
||||
import com.zfoo.protocol.util.GraalVmUtils;
|
||||
import com.zfoo.storage.model.vo.StorageObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -70,7 +67,7 @@ public class StorageManager implements IStorageManager {
|
||||
/**
|
||||
* 在当前项目被依赖注入,被使用的Storage
|
||||
*/
|
||||
private final Map<Class<?>, Storage<?, ?>> storageMap = new HashMap<>();
|
||||
private final Map<Class<?>, StorageObject<?, ?>> storageMap = new HashMap<>();
|
||||
|
||||
public StorageConfig getStorageConfig() {
|
||||
return storageConfig;
|
||||
@@ -129,8 +126,8 @@ public class StorageManager implements IStorageManager {
|
||||
var clazz = definition.getClazz();
|
||||
var resource = definition.getResource();
|
||||
var fileExtName = FileUtils.fileExtName(resource.getFilename());
|
||||
Storage<?, ?> storage = Storage.parse(resource.getInputStream(), clazz, fileExtName);
|
||||
storageMap.putIfAbsent(clazz, storage);
|
||||
StorageObject<?, ?> storageObject = StorageObject.parse(resource.getInputStream(), clazz, fileExtName);
|
||||
storageMap.putIfAbsent(clazz, storageObject);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -143,7 +140,7 @@ public class StorageManager implements IStorageManager {
|
||||
var componentBeans = applicationContext.getBeansWithAnnotation(Component.class);
|
||||
for (var bean : componentBeans.values()) {
|
||||
var clazz = bean.getClass();
|
||||
ReflectionUtils.filterFieldsInClass(clazz, field -> field.isAnnotationPresent(ResInjection.class), field -> {
|
||||
ReflectionUtils.filterFieldsInClass(clazz, field -> field.isAnnotationPresent(StorageInjection.class), field -> {
|
||||
Type type = field.getGenericType();
|
||||
|
||||
if (!(type instanceof ParameterizedType)) {
|
||||
@@ -158,9 +155,9 @@ public class StorageManager implements IStorageManager {
|
||||
|
||||
Class<?> resourceClazz = (Class<?>) types[1];
|
||||
|
||||
Storage<?, ?> storage = storageMap.get(resourceClazz);
|
||||
StorageObject<?, ?> storageObject = storageMap.get(resourceClazz);
|
||||
|
||||
if (storage == null) {
|
||||
if (storageObject == null) {
|
||||
throw new RuntimeException(StringUtils.format("Static class [resource:{}] does not exist", resourceClazz.getSimpleName()));
|
||||
}
|
||||
|
||||
@@ -175,8 +172,8 @@ public class StorageManager implements IStorageManager {
|
||||
}
|
||||
|
||||
ReflectionUtils.makeAccessible(field);
|
||||
ReflectionUtils.setField(field, bean, storage);
|
||||
storage.setRecycle(false);
|
||||
ReflectionUtils.setField(field, bean, storageObject);
|
||||
storageObject.setRecycle(false);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -191,7 +188,7 @@ public class StorageManager implements IStorageManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Storage<?, ?> getStorage(Class<?> clazz) {
|
||||
public StorageObject<?, ?> getStorage(Class<?> clazz) {
|
||||
var storage = storageMap.get(clazz);
|
||||
if (storage == null) {
|
||||
throw new RunException("There is no [{}] defined Storage and unable to get it", clazz.getCanonicalName());
|
||||
@@ -204,13 +201,13 @@ public class StorageManager implements IStorageManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Class<?>, Storage<?, ?>> storageMap() {
|
||||
public Map<Class<?>, StorageObject<?, ?>> storageMap() {
|
||||
return storageMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateStorage(Class<?> clazz, Storage<?, ?> storage) {
|
||||
storageMap.put(clazz, storage);
|
||||
public void updateStorage(Class<?> clazz, StorageObject<?, ?> storageObject) {
|
||||
storageMap.put(clazz, storageObject);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -222,7 +219,7 @@ public class StorageManager implements IStorageManager {
|
||||
// graalvm环境 PathMatchingResourcePatternResolver/CachingMetadataReaderFactory 无法使用,所以直接在spring容器中获取
|
||||
if (GraalVmUtils.isGraalVM()) {
|
||||
var applicationContext = StorageContext.getApplicationContext();
|
||||
var nativeResources = applicationContext.getBeansWithAnnotation(GraalvmNativeResource.class);
|
||||
var nativeResources = applicationContext.getBeansWithAnnotation(GraalvmNativeStorage.class);
|
||||
return nativeResources.values().stream().map(it -> it.getClass()).collect(Collectors.toSet());
|
||||
} else {
|
||||
return scanResourceAnno();
|
||||
@@ -238,7 +235,7 @@ public class StorageManager implements IStorageManager {
|
||||
for (var scanPackage : scanPackages) {
|
||||
var packageSearchPath = "classpath*:" + scanPackage.replace(StringUtils.PERIOD, StringUtils.SLASH) + StringUtils.SLASH + SUFFIX_PATTERN;
|
||||
var resources = resourcePatternResolver.getResources(packageSearchPath);
|
||||
var resourceName = com.zfoo.storage.model.anno.Resource.class.getName();
|
||||
var resourceName = Storage.class.getName();
|
||||
for (var resource : resources) {
|
||||
if (resource.isReadable()) {
|
||||
var metadataReader = metadataReaderFactory.getMetadataReader(resource);
|
||||
@@ -260,7 +257,7 @@ public class StorageManager implements IStorageManager {
|
||||
private Resource resource(Class<?> clazz) {
|
||||
if (GraalVmUtils.isGraalVM()) {
|
||||
var resourceLoader = new DefaultResourceLoader();
|
||||
var resource = resourceLoader.getResource(clazz.getAnnotation(GraalvmNativeResource.class).value());
|
||||
var resource = resourceLoader.getResource(clazz.getAnnotation(GraalvmNativeStorage.class).value());
|
||||
return resource;
|
||||
} else {
|
||||
return scanResourceFile(clazz);
|
||||
@@ -271,8 +268,8 @@ public class StorageManager implements IStorageManager {
|
||||
var resourcePatternResolver = new PathMatchingResourcePatternResolver();
|
||||
var metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
|
||||
|
||||
var annoAlias = clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).alias();
|
||||
var fileName = StringUtils.isEmpty(annoAlias) ? clazz.getSimpleName() : annoAlias;
|
||||
var alias = clazz.getAnnotation(Storage.class).value();
|
||||
var fileName = StringUtils.isEmpty(alias) ? clazz.getSimpleName() : alias;
|
||||
|
||||
try {
|
||||
// 一个class类只能匹配一个资源文件,如果匹配多个则会有歧义
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.zfoo.storage.model.anno;
|
||||
|
||||
import org.springframework.aot.hint.annotation.Reflective;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* EN: Specify the file column name. If not specified, the default column name will be consistent with the field name.
|
||||
* CN: 指定文件列名,不指定则默认列名与字段名一致
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
@Reflective
|
||||
public @interface ExcelFieldName {
|
||||
String value();
|
||||
}
|
||||
@@ -15,7 +15,7 @@ package com.zfoo.storage.model.vo;
|
||||
|
||||
import com.zfoo.protocol.exception.RunException;
|
||||
import com.zfoo.protocol.util.ReflectionUtils;
|
||||
import com.zfoo.storage.model.anno.Id;
|
||||
import com.zfoo.storage.anno.Id;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ import com.zfoo.protocol.collection.ArrayUtils;
|
||||
import com.zfoo.protocol.exception.RunException;
|
||||
import com.zfoo.protocol.util.ReflectionUtils;
|
||||
import com.zfoo.protocol.util.StringUtils;
|
||||
import com.zfoo.storage.model.anno.Index;
|
||||
import com.zfoo.storage.anno.Index;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
|
||||
+10
-10
@@ -28,7 +28,7 @@ import java.util.*;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class Storage<K, V> {
|
||||
public class StorageObject<K, V> {
|
||||
|
||||
private Map<K, V> dataMap = new HashMap<>();
|
||||
// 非唯一索引
|
||||
@@ -43,24 +43,24 @@ public class Storage<K, V> {
|
||||
protected boolean recycle = true;
|
||||
|
||||
|
||||
public static Storage<?, ?> parse(InputStream inputStream, Class<?> resourceClazz, String suffix) {
|
||||
public static StorageObject<?, ?> parse(InputStream inputStream, Class<?> resourceClazz, String suffix) {
|
||||
try {
|
||||
Storage<?, ?> storage = new Storage<>();
|
||||
storage.clazz = resourceClazz;
|
||||
StorageObject<?, ?> storageObject = new StorageObject<>();
|
||||
storageObject.clazz = resourceClazz;
|
||||
var idDef = IdDef.valueOf(resourceClazz);
|
||||
storage.idDef = idDef;
|
||||
storage.indexDefMap = IndexDef.createResourceIndexes(resourceClazz);
|
||||
storageObject.idDef = idDef;
|
||||
storageObject.indexDefMap = IndexDef.createResourceIndexes(resourceClazz);
|
||||
var list = ResourceInterpreter.read(inputStream, resourceClazz, suffix);
|
||||
for (var object : list) {
|
||||
storage.put(object);
|
||||
storageObject.put(object);
|
||||
}
|
||||
var idType = idDef.getField().getType();
|
||||
if (idType == int.class || idType == Integer.class) {
|
||||
return new StorageInt<>(storage);
|
||||
return new StorageObjectInt<>(storageObject);
|
||||
} else if (idType == long.class || idType == Long.class) {
|
||||
return new StorageLong<>(storage);
|
||||
return new StorageObjectLong<>(storageObject);
|
||||
} else {
|
||||
return storage;
|
||||
return storageObject;
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
throw new RuntimeException(e.getMessage(), e);
|
||||
+14
-12
@@ -16,26 +16,28 @@ package com.zfoo.storage.model.vo;
|
||||
import com.zfoo.protocol.util.AssertionUtils;
|
||||
import io.netty.util.collection.IntObjectHashMap;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class StorageInt<K, V> extends Storage<K, V> {
|
||||
public class StorageObjectInt<K, V> extends StorageObject<K, V> {
|
||||
|
||||
private IntObjectHashMap<V> dataMap;
|
||||
|
||||
public StorageInt(Storage<K, V> storage) {
|
||||
this.dataMap = new IntObjectHashMap<V>(storage.size());
|
||||
this.dataMap.putAll((Map<? extends Integer, ? extends V>) storage.getData());
|
||||
super.indexMap = storage.indexMap;
|
||||
super.uniqueIndexMap = storage.uniqueIndexMap;
|
||||
super.clazz = storage.clazz;
|
||||
super.idDef = storage.idDef;
|
||||
super.indexDefMap = storage.indexDefMap;
|
||||
super.recycle = storage.recycle;
|
||||
storage.recycleStorage();
|
||||
public StorageObjectInt(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;
|
||||
super.uniqueIndexMap = storageObject.uniqueIndexMap;
|
||||
super.clazz = storageObject.clazz;
|
||||
super.idDef = storageObject.idDef;
|
||||
super.indexDefMap = storageObject.indexDefMap;
|
||||
super.recycle = storageObject.recycle;
|
||||
storageObject.recycleStorage();
|
||||
}
|
||||
|
||||
@Override
|
||||
+11
-11
@@ -24,20 +24,20 @@ import java.util.Map;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
public class StorageLong<K, V> extends Storage<K, V> {
|
||||
public class StorageObjectLong<K, V> extends StorageObject<K, V> {
|
||||
|
||||
private LongObjectHashMap<V> dataMap;
|
||||
|
||||
public StorageLong(Storage<K, V> storage) {
|
||||
this.dataMap = new LongObjectHashMap<V>(storage.size());
|
||||
this.dataMap.putAll((Map<? extends Long, ? extends V>) storage.getData());
|
||||
super.indexMap = storage.indexMap;
|
||||
super.uniqueIndexMap = storage.uniqueIndexMap;
|
||||
super.clazz = storage.clazz;
|
||||
super.idDef = storage.idDef;
|
||||
super.indexDefMap = storage.indexDefMap;
|
||||
super.recycle = storage.recycle;
|
||||
storage.recycleStorage();
|
||||
public StorageObjectLong(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;
|
||||
super.uniqueIndexMap = storageObject.uniqueIndexMap;
|
||||
super.clazz = storageObject.clazz;
|
||||
super.idDef = storageObject.idDef;
|
||||
super.indexDefMap = storageObject.indexDefMap;
|
||||
super.recycle = storageObject.recycle;
|
||||
storageObject.recycleStorage();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -20,7 +20,7 @@ import com.zfoo.protocol.util.StringUtils;
|
||||
import com.zfoo.storage.interpreter.CsvReader;
|
||||
import com.zfoo.storage.interpreter.ExcelReader;
|
||||
import com.zfoo.storage.model.resource.ResourceEnum;
|
||||
import com.zfoo.storage.model.vo.Storage;
|
||||
import com.zfoo.storage.model.vo.StorageObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@@ -94,7 +94,7 @@ public abstract class ExportUtils {
|
||||
}
|
||||
|
||||
// 将class里的map自动赋值storage
|
||||
public static <T> T autoWrapData(Class<T> clazz, Map<Class<?>, Storage<?, ?>> storageMap) {
|
||||
public static <T> T autoWrapData(Class<T> clazz, Map<Class<?>, StorageObject<?, ?>> storageMap) {
|
||||
var instance = ReflectionUtils.newInstance(clazz);
|
||||
|
||||
var filedList = ReflectionUtils.notStaticAndTransientFields(clazz);
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
|
||||
package com.zfoo.storage;
|
||||
|
||||
import com.zfoo.storage.model.anno.ResInjection;
|
||||
import com.zfoo.storage.model.vo.Storage;
|
||||
import com.zfoo.storage.anno.StorageInjection;
|
||||
import com.zfoo.storage.model.vo.StorageObject;
|
||||
import com.zfoo.storage.resource.StudentCsvResource;
|
||||
import com.zfoo.storage.resource.StudentResource;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -26,9 +26,9 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class StudentManager {
|
||||
|
||||
@ResInjection
|
||||
public Storage<Integer, StudentResource> studentResources;
|
||||
@ResInjection
|
||||
public Storage<Integer, StudentCsvResource> studentCsvResources;
|
||||
@StorageInjection
|
||||
public StorageObject<Integer, StudentResource> studentResources;
|
||||
@StorageInjection
|
||||
public StorageObject<Integer, StudentCsvResource> studentCsvResources;
|
||||
|
||||
}
|
||||
@@ -13,8 +13,8 @@
|
||||
|
||||
package com.zfoo.storage;
|
||||
|
||||
import com.zfoo.storage.model.anno.ResInjection;
|
||||
import com.zfoo.storage.model.vo.Storage;
|
||||
import com.zfoo.storage.anno.StorageInjection;
|
||||
import com.zfoo.storage.model.vo.StorageObject;
|
||||
import com.zfoo.storage.resource.TestResource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -25,7 +25,7 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class TestManager {
|
||||
|
||||
@ResInjection
|
||||
public Storage<Integer, TestResource> testResources;
|
||||
@StorageInjection
|
||||
public StorageObject<Integer, TestResource> testResources;
|
||||
|
||||
}
|
||||
@@ -58,15 +58,6 @@ public class ConversionTest {
|
||||
Assert.assertEquals(123, result.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string2Class() {
|
||||
ConversionService conversionService = csfb.getObject();
|
||||
// String to Class
|
||||
Class<?> clazz = (Class<?>) conversionService.convert("com.zfoo.storage.model.vo.Storage", TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Class.class));
|
||||
|
||||
Assert.assertEquals("com.zfoo.storage.model.vo.Storage", clazz.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void string2Map() {
|
||||
ConversionService conversionService = csfb.getObject();
|
||||
|
||||
@@ -19,12 +19,12 @@ import com.zfoo.protocol.generate.GenerateOperation;
|
||||
import com.zfoo.protocol.serializer.CodeLanguage;
|
||||
import com.zfoo.protocol.util.FileUtils;
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
import com.zfoo.storage.anno.AliasFieldName;
|
||||
import com.zfoo.storage.anno.Id;
|
||||
import com.zfoo.storage.anno.Storage;
|
||||
import com.zfoo.storage.manager.StorageManager;
|
||||
import com.zfoo.storage.model.anno.ExcelFieldName;
|
||||
import com.zfoo.storage.model.anno.Id;
|
||||
import com.zfoo.storage.model.anno.Resource;
|
||||
import com.zfoo.storage.model.config.StorageConfig;
|
||||
import com.zfoo.storage.model.vo.Storage;
|
||||
import com.zfoo.storage.model.vo.StorageObject;
|
||||
import com.zfoo.storage.util.ExportUtils;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import io.netty.buffer.UnpooledHeapByteBuf;
|
||||
@@ -54,14 +54,14 @@ public class ExportBinaryTest {
|
||||
|
||||
}
|
||||
|
||||
@Resource
|
||||
@Storage
|
||||
public static class StudentResource implements IPacket {
|
||||
|
||||
@Id
|
||||
public int id;
|
||||
|
||||
public String name;
|
||||
@ExcelFieldName("年龄")
|
||||
@AliasFieldName("年龄")
|
||||
public int age;
|
||||
public float score;
|
||||
public String[] courses;
|
||||
@@ -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 = (Storage<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));
|
||||
}
|
||||
|
||||
@@ -13,15 +13,15 @@
|
||||
|
||||
package com.zfoo.storage.resource;
|
||||
|
||||
import com.zfoo.storage.model.anno.Id;
|
||||
import com.zfoo.storage.model.anno.Index;
|
||||
import com.zfoo.storage.model.anno.Resource;
|
||||
import com.zfoo.storage.anno.Id;
|
||||
import com.zfoo.storage.anno.Index;
|
||||
import com.zfoo.storage.anno.Storage;
|
||||
|
||||
/**
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
@Resource(alias="StudentCsvResource")
|
||||
@Storage("StudentCsvResource")
|
||||
public class StudentCsvResource {
|
||||
|
||||
@Id
|
||||
|
||||
@@ -13,10 +13,10 @@
|
||||
|
||||
package com.zfoo.storage.resource;
|
||||
|
||||
import com.zfoo.storage.model.anno.ExcelFieldName;
|
||||
import com.zfoo.storage.model.anno.Id;
|
||||
import com.zfoo.storage.model.anno.Index;
|
||||
import com.zfoo.storage.model.anno.Resource;
|
||||
import com.zfoo.storage.anno.AliasFieldName;
|
||||
import com.zfoo.storage.anno.Id;
|
||||
import com.zfoo.storage.anno.Index;
|
||||
import com.zfoo.storage.anno.Storage;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -24,7 +24,7 @@ import java.util.List;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
@Resource
|
||||
@Storage
|
||||
public class StudentResource {
|
||||
|
||||
@Id
|
||||
@@ -36,7 +36,7 @@ public class StudentResource {
|
||||
@Index
|
||||
private String name;
|
||||
|
||||
@ExcelFieldName("年龄")
|
||||
@AliasFieldName("年龄")
|
||||
private int age;
|
||||
private float score;
|
||||
private String[] courses;
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
|
||||
package com.zfoo.storage.resource;
|
||||
|
||||
import com.zfoo.storage.model.anno.Id;
|
||||
import com.zfoo.storage.model.anno.Resource;
|
||||
import com.zfoo.storage.anno.Id;
|
||||
import com.zfoo.storage.anno.Storage;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -22,7 +22,7 @@ import java.util.Map;
|
||||
* @author godotg
|
||||
* @version 3.0
|
||||
*/
|
||||
@Resource
|
||||
@Storage
|
||||
public class TestResource {
|
||||
|
||||
@Id
|
||||
|
||||
Reference in new issue
Block a user