fix read s table
parent
098024c896
commit
b1e9d4040d
|
@ -9,6 +9,7 @@ import org.slf4j.LoggerFactory;
|
|||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
@ -81,43 +82,48 @@ public class STableManager {
|
|||
String tableName = clazz.getAnnotation(Table.class).name();
|
||||
String path = SysUtil.getPath("conf", "server", tableName + ".txt");
|
||||
File file = new File(path);
|
||||
String line;
|
||||
List<String> key = new ArrayList<>();
|
||||
List<String> type = new ArrayList<>();
|
||||
int lineNum = 0;
|
||||
|
||||
if (!file.exists()) {
|
||||
LOGGER.error("file not find {}, do not find sheet with {} you need rebuild all sheet by gen sheet tool!", path, tableName);
|
||||
// return null;
|
||||
}
|
||||
try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){
|
||||
LOGGER.info("initMap:{}", clazz.getSimpleName());
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
if (line.isEmpty()){
|
||||
continue;
|
||||
}
|
||||
T obj = clazz.newInstance();
|
||||
String[] prarms = line.split("\\t");
|
||||
switch (lineNum) {
|
||||
case 0:
|
||||
prarms = StringUtil.fieldHandle(prarms);
|
||||
key.addAll(Arrays.asList(prarms));
|
||||
break;
|
||||
case 1:
|
||||
type.addAll(Arrays.asList(prarms));
|
||||
break;
|
||||
default:
|
||||
dealParams(clazz, map, key, type, obj, prarms);
|
||||
break;
|
||||
}
|
||||
lineNum++;
|
||||
}
|
||||
}
|
||||
LOGGER.info("initMap:{}", clazz.getSimpleName());
|
||||
readFileToCache(clazz, map, file);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
private static <T> void readFileToCache(Class<T> clazz, Map<Integer, T> map, File file) throws IOException, InstantiationException, IllegalAccessException, NoSuchFieldException {
|
||||
String line;
|
||||
List<String> key = new ArrayList<>();
|
||||
List<String> type = new ArrayList<>();
|
||||
int lineNum = 0;
|
||||
try(BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
if (line.isEmpty()){
|
||||
continue;
|
||||
}
|
||||
T obj = clazz.newInstance();
|
||||
String[] prarms = line.split("\\t");
|
||||
switch (lineNum) {
|
||||
case 0:
|
||||
prarms = StringUtil.fieldHandle(prarms);
|
||||
key.addAll(Arrays.asList(prarms));
|
||||
break;
|
||||
case 1:
|
||||
type.addAll(Arrays.asList(prarms));
|
||||
break;
|
||||
default:
|
||||
dealParams(clazz, map, key, type, obj, prarms);
|
||||
break;
|
||||
}
|
||||
lineNum++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 读取配置表到内存
|
||||
|
@ -125,44 +131,20 @@ public class STableManager {
|
|||
public static <T> Map<Integer, Map<Integer, T>> getMapConfig(Class<T> clazz) throws Exception {
|
||||
Map<Integer, Map<Integer, T>> map = new HashMap<>();
|
||||
String tableName = clazz.getAnnotation(Table.class).name();
|
||||
int mapType = 100;
|
||||
for (int i = 100; i < 1000; i++) {
|
||||
Map<Integer, T> mapConf = new HashMap<>();
|
||||
tableName = tableName + "_"+ i;
|
||||
String path = SysUtil.getPath("conf", "server", tableName + ".txt");
|
||||
File file = new File(path);
|
||||
if (!file.exists()) {
|
||||
tableName = tableName.substring(0, 3);
|
||||
mapType += 100;
|
||||
i = mapType;
|
||||
String pathDir = SysUtil.getPath("conf", "server");
|
||||
File mapDir = new File(pathDir);
|
||||
String prefix = tableName + "_";
|
||||
int preLength = prefix.length();
|
||||
for(File mapFile : mapDir.listFiles()){
|
||||
tableName = mapFile.getName();
|
||||
if(!mapFile.getName().startsWith(prefix)){
|
||||
continue;
|
||||
}
|
||||
String line;
|
||||
List<String> key = new ArrayList<>();
|
||||
List<String> type = new ArrayList<>();
|
||||
int lineNum = 0;
|
||||
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){
|
||||
LOGGER.info("initMap:{}", tableName);
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
T obj = clazz.newInstance();
|
||||
String[] prarms = line.split("\\t");
|
||||
switch (lineNum) {
|
||||
case 0:
|
||||
prarms = StringUtil.fieldHandle(prarms);
|
||||
key.addAll(Arrays.asList(prarms));
|
||||
break;
|
||||
case 1:
|
||||
type.addAll(Arrays.asList(prarms));
|
||||
break;
|
||||
default:
|
||||
dealParams(clazz, mapConf, key, type, obj, prarms);
|
||||
break;
|
||||
}
|
||||
lineNum++;
|
||||
}
|
||||
map.put(i, mapConf);
|
||||
tableName = tableName.substring(0, 3);
|
||||
}
|
||||
int mapId = Integer.parseInt(tableName.substring(preLength).split("\\.")[0]);
|
||||
LOGGER.info("initMap:{}", tableName);
|
||||
Map<Integer, T> mapConf = new HashMap<>();
|
||||
readFileToCache(clazz,mapConf,mapFile);
|
||||
map.put(mapId, mapConf);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue