ref[storage]: refactor storage

This commit is contained in:
godotg
2023-09-02 22:35:17 +08:00
parent 1da6b324af
commit 9a35193a9a
23 changed files with 178 additions and 112 deletions
@@ -16,10 +16,10 @@ 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.config.StorageConfig;
import com.zfoo.storage.interpreter.data.StorageData;
import com.zfoo.storage.interpreter.data.StorageEnum;
import com.zfoo.storage.manager.StorageManager;
import com.zfoo.storage.model.config.StorageConfig;
import com.zfoo.storage.model.resource.ResourceData;
import com.zfoo.storage.model.resource.ResourceEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
@@ -70,7 +70,7 @@ public class StorageAutoConfiguration {
logger.info("storage graalvm aot runtime hints register");
var classes = new HashSet<Class<?>>();
classes.add(ResourceData.class);
classes.add(StorageData.class);
classes.add(StorageConfig.class);
try {
@@ -94,7 +94,7 @@ public class StorageAutoConfiguration {
logger.info("storage graalvm aot hints register serialization [{}]", clazz);
}
for (var resource : ResourceEnum.values()) {
for (var resource : StorageEnum.values()) {
var include = StringUtils.format("*.{}", resource.getType());
hints.resources().registerPattern(include);
logger.info("storage graalvm aot hints register resources [{}]", include);
@@ -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.config;
package com.zfoo.storage.config;
/**
* @author godotg
@@ -14,8 +14,8 @@ package com.zfoo.storage.interpreter;
import com.zfoo.protocol.exception.RunException;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.storage.model.resource.ResourceData;
import com.zfoo.storage.model.resource.ResourceHeader;
import com.zfoo.storage.interpreter.data.StorageData;
import com.zfoo.storage.interpreter.data.StorageHeader;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
@@ -33,7 +33,7 @@ import java.util.List;
*/
public abstract class CsvReader {
public static ResourceData readResourceDataFromCSV(InputStream input, String fileName) {
public static StorageData readResourceDataFromCSV(InputStream input, String fileName) {
var records = parseCsv(input, fileName);
var iterator = records.iterator();
var headers = getHeaders(iterator, fileName);
@@ -50,14 +50,14 @@ public abstract class CsvReader {
}
rows.add(data);
}
return ResourceData.valueOf(fileName, headers, rows);
return StorageData.valueOf(fileName, headers, rows);
}
/**
* 构建配置表消息头
*/
private static List<ResourceHeader> getHeaders(Iterator<CSVRecord> iterator, String fileName) {
private static List<StorageHeader> getHeaders(Iterator<CSVRecord> iterator, String fileName) {
// 获取配置表的有效列名称,默认第一行就是字段名称
var fieldRow = iterator.next();
if (fieldRow == null) {
@@ -71,7 +71,7 @@ public abstract class CsvReader {
// 默认第三行为描述,需要的时候再使用
var descRow = iterator.next();
var headers = new ArrayList<ResourceHeader>();
var headers = new ArrayList<StorageHeader>();
for (var i = 0; i < fieldRow.size(); i++) {
var fieldName = fieldRow.get(i);
if (fieldName == null) {
@@ -81,7 +81,7 @@ public abstract class CsvReader {
if (filedType == null) {
throw new RunException("The column type of {} cannot be empty, and column {} has no configured type", fileName, i + 1);
}
headers.add(ResourceHeader.valueOf(fieldName, filedType, i));
headers.add(StorageHeader.valueOf(fieldName, filedType, i));
}
return headers;
}
@@ -14,8 +14,8 @@ package com.zfoo.storage.interpreter;
import com.zfoo.protocol.exception.RunException;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.storage.model.resource.ResourceData;
import com.zfoo.storage.model.resource.ResourceHeader;
import com.zfoo.storage.interpreter.data.StorageData;
import com.zfoo.storage.interpreter.data.StorageHeader;
import com.zfoo.storage.util.CellUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
@@ -31,7 +31,7 @@ import java.util.*;
*/
public abstract class ExcelReader {
public static ResourceData readResourceDataFromExcel(InputStream inputStream, String resourceClassName) {
public static StorageData readResourceDataFromExcel(InputStream inputStream, String resourceClassName) {
// 只读取代码里写的字段
var wb = createWorkbook(inputStream, resourceClassName);
// 默认取到第一个sheet页
@@ -57,10 +57,10 @@ public abstract class ExcelReader {
}
rows.add(columns);
}
return ResourceData.valueOf(resourceClassName, headers, rows);
return StorageData.valueOf(resourceClassName, headers, rows);
}
private static List<ResourceHeader> getHeaders(Iterator<Row> iterator, String resourceClassName) {
private static List<StorageHeader> getHeaders(Iterator<Row> iterator, String resourceClassName) {
// 获取配置表的有效列名称,默认第一行就是字段名称
var fieldRow = iterator.next();
if (fieldRow == null) {
@@ -73,7 +73,7 @@ public abstract class ExcelReader {
}
// 默认第三行为描述,需要的时候再使用
var desRow = iterator.next();
var headerList = new ArrayList<ResourceHeader>();
var headerList = new ArrayList<StorageHeader>();
var cellFieldMap = new HashMap<String, Integer>();
for (var i = 0; i < fieldRow.getLastCellNum(); i++) {
var fieldCell = fieldRow.getCell(i);
@@ -96,7 +96,7 @@ public abstract class ExcelReader {
if (Objects.nonNull(previousValue)) {
throw new RunException("There are duplicate attribute control columns [field:{}] in the Excel file of the resource [class:{}]", excelFieldName,resourceClassName);
}
headerList.add(ResourceHeader.valueOf(excelFieldName, typeName, i));
headerList.add(StorageHeader.valueOf(excelFieldName, typeName, i));
}
return headerList;
}
@@ -15,7 +15,7 @@ package com.zfoo.storage.interpreter;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.storage.model.resource.ResourceData;
import com.zfoo.storage.interpreter.data.StorageData;
import java.io.IOException;
import java.io.InputStream;
@@ -26,9 +26,9 @@ import java.io.InputStream;
*/
public abstract class JsonReader {
public static ResourceData readResourceDataFromJson(InputStream input) {
public static StorageData readResourceDataFromJson(InputStream input) {
try {
var resourceData= JsonUtils.string2Object(StringUtils.bytesToString(IOUtils.toByteArray(input)), ResourceData.class);
var resourceData= JsonUtils.string2Object(StringUtils.bytesToString(IOUtils.toByteArray(input)), StorageData.class);
for(int i=0;i<resourceData.getHeaders().size();i++) {
resourceData.getHeaders().get(i).setIndex(i);
}
@@ -17,8 +17,8 @@ import com.zfoo.protocol.util.ReflectionUtils;
import com.zfoo.protocol.util.StringUtils;
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.interpreter.data.StorageData;
import com.zfoo.storage.interpreter.data.StorageEnum;
import com.zfoo.storage.strategy.*;
import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.core.convert.TypeDescriptor;
@@ -53,13 +53,13 @@ public class ResourceInterpreter {
}
public static <T> List<T> read(InputStream inputStream, Class<T> clazz, String suffix) throws IOException {
ResourceData resource = null;
var resourceEnum = ResourceEnum.getResourceEnumByType(suffix);
if (resourceEnum == ResourceEnum.JSON) {
StorageData resource = null;
var resourceEnum = StorageEnum.getResourceEnumByType(suffix);
if (resourceEnum == StorageEnum.JSON) {
resource = JsonReader.readResourceDataFromJson(inputStream);
} else if (resourceEnum == ResourceEnum.EXCEL_XLS || resourceEnum == ResourceEnum.EXCEL_XLSX) {
} else if (resourceEnum == StorageEnum.EXCEL_XLS || resourceEnum == StorageEnum.EXCEL_XLSX) {
resource = ExcelReader.readResourceDataFromExcel(inputStream, clazz.getSimpleName());
} else if (resourceEnum == ResourceEnum.CSV) {
} else if (resourceEnum == StorageEnum.CSV) {
resource = CsvReader.readResourceDataFromCSV(inputStream, clazz.getSimpleName());
} else {
throw new RunException("Configuration type [{}] of file [{}] is not supported", suffix, clazz.getSimpleName());
@@ -133,7 +133,7 @@ public class ResourceInterpreter {
}
}
public static Map<String, Integer> getCellFieldMap(ResourceData resource, Class<?> clazz) {
public static Map<String, Integer> getCellFieldMap(StorageData resource, Class<?> clazz) {
var header = resource.getHeaders();
if (header == null) {
throw new RunException("Failed to get attribute control column from excel file of resource [class:{}]", clazz.getSimpleName());
@@ -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
*
@@ -10,7 +9,7 @@
* 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.model.resource;
package com.zfoo.storage.interpreter.data;
import java.util.ArrayList;
import java.util.List;
@@ -20,17 +19,17 @@ import java.util.List;
*
* @author meiwei666
*/
public class ResourceData {
public class StorageData {
// 文件名
private String name;
// 配置表字段名
private List<ResourceHeader> headers = new ArrayList<>();
private List<StorageHeader> headers = new ArrayList<>();
// 配置表数据
private List<List<String>> rows = new ArrayList<>();
public static ResourceData valueOf(String name, List<ResourceHeader> headers, List<List<String>> rows) {
var resourceData = new ResourceData();
public static StorageData valueOf(String name, List<StorageHeader> headers, List<List<String>> rows) {
var resourceData = new StorageData();
resourceData.name = name;
resourceData.headers = headers;
resourceData.rows = rows;
@@ -45,11 +44,11 @@ public class ResourceData {
this.name = name;
}
public List<ResourceHeader> getHeaders() {
public List<StorageHeader> getHeaders() {
return headers;
}
public void setHeaders(List<ResourceHeader> headers) {
public void setHeaders(List<StorageHeader> headers) {
this.headers = headers;
}
@@ -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
*
@@ -10,7 +9,7 @@
* 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.model.resource;
package com.zfoo.storage.interpreter.data;
import com.zfoo.protocol.util.AssertionUtils;
import com.zfoo.protocol.util.StringUtils;
@@ -25,7 +24,7 @@ import java.util.Map;
* @author godotg
* @version 3.0
*/
public enum ResourceEnum {
public enum StorageEnum {
EXCEL_XLS("xls"),
@@ -37,10 +36,10 @@ public enum ResourceEnum {
;
private static Map<String, ResourceEnum> typeMap = new HashMap<>();
private static Map<String, StorageEnum> typeMap = new HashMap<>();
static {
for (var resourceEnum : ResourceEnum.values()) {
for (var resourceEnum : StorageEnum.values()) {
var previousValue = typeMap.putIfAbsent(resourceEnum.type, resourceEnum);
AssertionUtils.isNull(previousValue, "ResourceEnum should not contain enumeration classes [{}] and [{}] of repeated type", resourceEnum, previousValue);
}
@@ -48,12 +47,12 @@ public enum ResourceEnum {
private String type;
ResourceEnum(String type) {
StorageEnum(String type) {
this.type = type;
}
@Nullable
public static ResourceEnum getResourceEnumByType(String type) {
public static StorageEnum getResourceEnumByType(String type) {
return typeMap.get(type);
}
@@ -63,7 +62,7 @@ public enum ResourceEnum {
public static boolean isExcel(String type) {
var resourceEnum = getResourceEnumByType(type);
return resourceEnum == ResourceEnum.EXCEL_XLS || resourceEnum == ResourceEnum.EXCEL_XLSX;
return resourceEnum == StorageEnum.EXCEL_XLS || resourceEnum == StorageEnum.EXCEL_XLSX;
}
public static String typesToString() {
@@ -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
*
@@ -10,13 +9,13 @@
* 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.model.resource;
package com.zfoo.storage.interpreter.data;
/**
* @author godotg
* @version 3.0
*/
public class ResourceHeader {
public class StorageHeader {
//字段名
private String name;
@@ -25,8 +24,8 @@ public class ResourceHeader {
//
private int index;
public static ResourceHeader valueOf(String name, String type, int index) {
var resourceHeader = new ResourceHeader();
public static StorageHeader valueOf(String name, String type, int index) {
var resourceHeader = new StorageHeader();
resourceHeader.name = name;
resourceHeader.type = type;
resourceHeader.index = index;
@@ -0,0 +1,61 @@
/*
* 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.manager;
import com.zfoo.storage.model.IdDef;
import org.springframework.lang.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* @author godotg
*/
public interface IStorage<K, V> {
boolean contain(K key);
boolean contain(int key);
boolean contain(long key);
V get(K id);
V get(int id);
V get(long id);
void recycleStorage();
boolean isRecycle();
void setRecycle(boolean recycle);
Collection<V> getAll();
Map<K, V> getData();
IdDef getIdDef();
List<V> getIndex(String indexName, Object key);
@Nullable
V getUniqueIndex(String uniqueIndexName, Object key);
int size();
V put(Object value);
}
@@ -13,8 +13,7 @@
package com.zfoo.storage.manager;
import com.zfoo.storage.model.config.StorageConfig;
import com.zfoo.storage.model.vo.StorageObject;
import com.zfoo.storage.config.StorageConfig;
import java.util.Map;
@@ -39,11 +38,11 @@ public interface IStorageManager {
*/
void initAfter();
StorageObject<?, ?> getStorage(Class<?> clazz);
IStorage<?, ?> getStorage(Class<?> clazz);
Map<Class<?>, StorageObject<?, ?>> storageMap();
Map<Class<?>, IStorage<?, ?>> storageMap();
void updateStorage(Class<?> clazz, StorageObject<?, ?> storageObject);
void updateStorage(Class<?> clazz, ObjectStorage<?, ?> storageObject);
StorageConfig storageConfig();
}
@@ -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.vo;
package com.zfoo.storage.manager;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.util.AssertionUtils;
@@ -19,6 +18,8 @@ import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.ReflectionUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.storage.interpreter.ResourceInterpreter;
import com.zfoo.storage.model.IdDef;
import com.zfoo.storage.model.IndexDef;
import org.springframework.lang.Nullable;
import java.io.InputStream;
@@ -28,7 +29,7 @@ import java.util.*;
* @author godotg
* @version 3.0
*/
public class StorageObject<K, V> {
public class ObjectStorage<K, V> implements IStorage<K, V> {
private Map<K, V> dataMap = new HashMap<>();
// 非唯一索引
@@ -43,9 +44,9 @@ public class StorageObject<K, V> {
protected boolean recycle = true;
public static StorageObject<?, ?> parse(InputStream inputStream, Class<?> resourceClazz, String suffix) {
public static ObjectStorage<?, ?> parse(InputStream inputStream, Class<?> resourceClazz, String suffix) {
try {
StorageObject<?, ?> storageObject = new StorageObject<>();
ObjectStorage<?, ?> storageObject = new ObjectStorage<>();
storageObject.clazz = resourceClazz;
var idDef = IdDef.valueOf(resourceClazz);
storageObject.idDef = idDef;
@@ -56,9 +57,9 @@ public class StorageObject<K, V> {
}
var idType = idDef.getField().getType();
if (idType == int.class || idType == Integer.class) {
return new StorageObjectInt<>(storageObject);
return new PrimitiveIntStorage<>(storageObject);
} else if (idType == long.class || idType == Long.class) {
return new StorageObjectLong<>(storageObject);
return new PrimitiveLongStorage<>(storageObject);
} else {
return storageObject;
}
@@ -69,32 +70,39 @@ public class StorageObject<K, V> {
}
}
@Override
public boolean contain(K key) {
return dataMap.containsKey(key);
}
@Override
public boolean contain(int key) {
return contain((K) Integer.valueOf(key));
}
@Override
public boolean contain(long key) {
return contain((K) Long.valueOf(key));
}
@Override
public V get(K id) {
V result = dataMap.get(id);
AssertionUtils.notNull(result, "The static resource represented as [id:{}] in the static resource [resource:{}] does not exist", id, clazz.getSimpleName());
return result;
}
@Override
public V get(int id) {
return get((K) Integer.valueOf(id));
}
@Override
public V get(long id) {
return get((K) Long.valueOf(id));
}
@Override
public void recycleStorage() {
recycle = true;
dataMap = null;
@@ -104,26 +112,32 @@ public class StorageObject<K, V> {
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 Map<K, V> getData() {
return Collections.unmodifiableMap(dataMap);
}
@Override
public IdDef getIdDef() {
return idDef;
}
@Override
public List<V> getIndex(String indexName, Object key) {
var indexValues = indexMap.get(indexName);
AssertionUtils.notNull(indexValues, "The index of [indexName:{}] does not exist in the static resource [resource:{}]", indexName, clazz.getSimpleName());
@@ -135,6 +149,7 @@ public class StorageObject<K, V> {
}
@Nullable
@Override
public V getUniqueIndex(String uniqueIndexName, Object key) {
var indexValueMap = uniqueIndexMap.get(uniqueIndexName);
AssertionUtils.notNull(indexValueMap, "There is no a unique index for [uniqueIndexName:{}] in the static resource [resource:{}]", uniqueIndexName, clazz.getSimpleName());
@@ -142,11 +157,12 @@ public class StorageObject<K, V> {
return value;
}
@Override
public int size() {
return dataMap.size();
}
private V put(Object value) {
public V put(Object value) {
var key = (K) ReflectionUtils.getField(idDef.getField(), value);
if (key == null) {
@@ -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.vo;
package com.zfoo.storage.manager;
import com.zfoo.protocol.util.AssertionUtils;
import io.netty.util.collection.IntObjectHashMap;
@@ -24,11 +23,11 @@ import java.util.Map;
* @author godotg
* @version 3.0
*/
public class StorageObjectInt<K, V> extends StorageObject<K, V> {
public class PrimitiveIntStorage<K, V> extends ObjectStorage<K, V> {
private IntObjectHashMap<V> dataMap;
public StorageObjectInt(StorageObject<K, V> storageObject) {
public PrimitiveIntStorage(ObjectStorage<K, V> storageObject) {
this.dataMap = new IntObjectHashMap<V>(storageObject.size());
this.dataMap.putAll((Map<? extends Integer, ? extends V>) storageObject.getData());
super.indexMap = storageObject.indexMap;
@@ -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.vo;
package com.zfoo.storage.manager;
import com.zfoo.protocol.util.AssertionUtils;
import io.netty.util.collection.LongObjectHashMap;
@@ -24,11 +23,11 @@ import java.util.Map;
* @author godotg
* @version 3.0
*/
public class StorageObjectLong<K, V> extends StorageObject<K, V> {
public class PrimitiveLongStorage<K, V> extends ObjectStorage<K, V> {
private LongObjectHashMap<V> dataMap;
public StorageObjectLong(StorageObject<K, V> storageObject) {
public PrimitiveLongStorage(ObjectStorage<K, V> storageObject) {
this.dataMap = new LongObjectHashMap<V>(storageObject.size());
this.dataMap.putAll((Map<? extends Long, ? extends V>) storageObject.getData());
super.indexMap = storageObject.indexMap;
@@ -22,10 +22,9 @@ 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.StorageObject;
import com.zfoo.storage.config.StorageConfig;
import com.zfoo.storage.interpreter.data.StorageEnum;
import com.zfoo.storage.model.StorageDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
@@ -67,7 +66,7 @@ public class StorageManager implements IStorageManager {
/**
* 在当前项目被依赖注入,被使用的Storage
*/
private final Map<Class<?>, StorageObject<?, ?>> storageMap = new HashMap<>();
private final Map<Class<?>, IStorage<?, ?>> storageMap = new HashMap<>();
public StorageConfig getStorageConfig() {
return storageConfig;
@@ -79,7 +78,7 @@ public class StorageManager implements IStorageManager {
@Override
public void initBefore() {
var resourceDefinitionMap = new HashMap<Class<?>, ResourceDef>();
var resourceDefinitionMap = new HashMap<Class<?>, StorageDefinition>();
// 获取需要被映射的Excel的class类文件
var clazzSet = resourceClass();
@@ -87,12 +86,12 @@ public class StorageManager implements IStorageManager {
// 通过class类文件扫描excel文件地址
for (var resourceClazz : clazzSet) {
var resourceFile = resource(resourceClazz);
ResourceDef resourceDef = new ResourceDef(resourceClazz, resourceFile);
StorageDefinition storageDefinition = new StorageDefinition(resourceClazz, resourceFile);
if (resourceDefinitionMap.containsKey(resourceClazz)) {
// 类的资源定义已经存在
throw new RuntimeException(StringUtils.format("The resource definition of the class [{}] already exists [{}]", resourceClazz, resourceDef));
throw new RuntimeException(StringUtils.format("The resource definition of the class [{}] already exists [{}]", resourceClazz, storageDefinition));
}
resourceDefinitionMap.put(resourceClazz, resourceDef);
resourceDefinitionMap.put(resourceClazz, storageDefinition);
}
// 检查class字段是否合法
@@ -126,7 +125,7 @@ public class StorageManager implements IStorageManager {
var clazz = definition.getClazz();
var resource = definition.getResource();
var fileExtName = FileUtils.fileExtName(resource.getFilename());
StorageObject<?, ?> storageObject = StorageObject.parse(resource.getInputStream(), clazz, fileExtName);
ObjectStorage<?, ?> storageObject = ObjectStorage.parse(resource.getInputStream(), clazz, fileExtName);
storageMap.putIfAbsent(clazz, storageObject);
}
} catch (Exception e) {
@@ -155,7 +154,7 @@ public class StorageManager implements IStorageManager {
Class<?> resourceClazz = (Class<?>) types[1];
StorageObject<?, ?> storageObject = storageMap.get(resourceClazz);
IStorage<?, ?> storageObject = storageMap.get(resourceClazz);
if (storageObject == null) {
throw new RuntimeException(StringUtils.format("Static class [resource:{}] does not exist", resourceClazz.getSimpleName()));
@@ -188,7 +187,7 @@ public class StorageManager implements IStorageManager {
}
@Override
public StorageObject<?, ?> getStorage(Class<?> clazz) {
public IStorage<?, ?> 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());
@@ -201,12 +200,12 @@ public class StorageManager implements IStorageManager {
}
@Override
public Map<Class<?>, StorageObject<?, ?>> storageMap() {
public Map<Class<?>, IStorage<?, ?>> storageMap() {
return storageMap;
}
@Override
public void updateStorage(Class<?> clazz, StorageObject<?, ?> storageObject) {
public void updateStorage(Class<?> clazz, ObjectStorage<?, ?> storageObject) {
storageMap.put(clazz, storageObject);
}
@@ -280,7 +279,7 @@ public class StorageManager implements IStorageManager {
var packageSearchPath = StringUtils.format("{}/**/{}.*", resourceLocation, fileName);
packageSearchPath = packageSearchPath.replaceAll("//", "/");
try {
Arrays.stream(resourcePatternResolver.getResources(packageSearchPath)).filter(it -> ResourceEnum.containsResourceEnum(FileUtils.fileExtName(it.getFilename()))).forEach(it -> resources.add(it));
Arrays.stream(resourcePatternResolver.getResources(packageSearchPath)).filter(it -> StorageEnum.containsResourceEnum(FileUtils.fileExtName(it.getFilename()))).forEach(it -> resources.add(it));
} catch (Exception e) {
// do nothing
}
@@ -289,7 +288,7 @@ public class StorageManager implements IStorageManager {
if (resources.isEmpty()) {
packageSearchPath = StringUtils.format("{}/{}.*", resourceLocation, fileName);
packageSearchPath = packageSearchPath.replaceAll("//", "/");
Arrays.stream(resourcePatternResolver.getResources(packageSearchPath)).filter(it -> ResourceEnum.containsResourceEnum(FileUtils.fileExtName(it.getFilename()))).forEach(it -> resources.add(it));
Arrays.stream(resourcePatternResolver.getResources(packageSearchPath)).filter(it -> StorageEnum.containsResourceEnum(FileUtils.fileExtName(it.getFilename()))).forEach(it -> resources.add(it));
}
resourceSet.addAll(resources);
}
@@ -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.vo;
package com.zfoo.storage.model;
import com.zfoo.protocol.exception.RunException;
import com.zfoo.protocol.util.ReflectionUtils;
@@ -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.vo;
package com.zfoo.storage.model;
import com.zfoo.protocol.collection.ArrayUtils;
import com.zfoo.protocol.exception.RunException;
@@ -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.vo;
package com.zfoo.storage.model;
import org.springframework.core.io.Resource;
@@ -19,12 +18,12 @@ import org.springframework.core.io.Resource;
* @author godotg
* @version 3.0
*/
public class ResourceDef {
public class StorageDefinition {
private final Class<?> clazz;
private final Resource resource;
public ResourceDef(Class<?> clazz, Resource resource) {
public StorageDefinition(Class<?> clazz, Resource resource) {
this.clazz = clazz;
this.resource = resource;
}
@@ -16,8 +16,8 @@ package com.zfoo.storage.schema;
import com.zfoo.protocol.util.DomUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.storage.StorageContext;
import com.zfoo.storage.config.StorageConfig;
import com.zfoo.storage.manager.StorageManager;
import com.zfoo.storage.model.config.StorageConfig;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
@@ -19,8 +19,8 @@ import com.zfoo.protocol.util.ReflectionUtils;
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.StorageObject;
import com.zfoo.storage.interpreter.data.StorageEnum;
import com.zfoo.storage.manager.IStorage;
import java.io.File;
import java.io.IOException;
@@ -82,19 +82,19 @@ public abstract class ExportUtils {
public static List<File> scanExcelFiles(String inputDir) {
return FileUtils.getAllReadableFiles(new File(inputDir))
.stream()
.filter(it -> ResourceEnum.isExcel(FileUtils.fileExtName(it.getName())))
.filter(it -> StorageEnum.isExcel(FileUtils.fileExtName(it.getName())))
.collect(Collectors.toList());
}
public static List<File> scanCsvFiles(String inputDir) {
return FileUtils.getAllReadableFiles(new File(inputDir))
.stream()
.filter(it -> ResourceEnum.getResourceEnumByType(FileUtils.fileExtName(it.getName())) == ResourceEnum.CSV)
.filter(it -> StorageEnum.getResourceEnumByType(FileUtils.fileExtName(it.getName())) == StorageEnum.CSV)
.collect(Collectors.toList());
}
// 将class里的map自动赋值storage
public static <T> T autoWrapData(Class<T> clazz, Map<Class<?>, StorageObject<?, ?>> storageMap) {
public static <T> T autoWrapData(Class<T> clazz, Map<Class<?>, IStorage<?, ?>> storageMap) {
var instance = ReflectionUtils.newInstance(clazz);
var filedList = ReflectionUtils.notStaticAndTransientFields(clazz);
@@ -14,7 +14,7 @@
package com.zfoo.storage;
import com.zfoo.storage.anno.StorageInjection;
import com.zfoo.storage.model.vo.StorageObject;
import com.zfoo.storage.manager.ObjectStorage;
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 StorageObject<Integer, StudentResource> studentResources;
public ObjectStorage<Integer, StudentResource> studentResources;
@StorageInjection
public StorageObject<Integer, StudentCsvResource> studentCsvResources;
public ObjectStorage<Integer, StudentCsvResource> studentCsvResources;
}
@@ -14,7 +14,7 @@
package com.zfoo.storage;
import com.zfoo.storage.anno.StorageInjection;
import com.zfoo.storage.model.vo.StorageObject;
import com.zfoo.storage.manager.IStorage;
import com.zfoo.storage.resource.TestResource;
import org.springframework.stereotype.Component;
@@ -26,6 +26,6 @@ import org.springframework.stereotype.Component;
public class TestManager {
@StorageInjection
public StorageObject<Integer, TestResource> testResources;
public IStorage<Integer, TestResource> testResources;
}
@@ -22,9 +22,9 @@ 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.config.StorageConfig;
import com.zfoo.storage.manager.ObjectStorage;
import com.zfoo.storage.manager.StorageManager;
import com.zfoo.storage.model.config.StorageConfig;
import com.zfoo.storage.model.vo.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 = (StorageObject<Integer, StudentResource>) storageManager.getStorage(StudentResource.class);
var storage = (ObjectStorage<Integer, StudentResource>) storageManager.getStorage(StudentResource.class);
for (StudentResource resource : storage.getAll()) {
System.out.println(JsonUtils.object2String(resource));
}