Skip to content

Commit

Permalink
配置表缓存实现
Browse files Browse the repository at this point in the history
  • Loading branch information
omoomoo committed Sep 19, 2016
1 parent cbc4008 commit 03f2de4
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 98 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/cpcb/gs/io/ProtocolDeploy.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public String toString() {
return "InnerObject [id=" + id + ", name=" + name + "]";
}
}

public static ProtocolDeploy getDeploy(int key) {
return (ProtocolDeploy) TableLoader.Load("protocol", ProtocolDeploy.class).get(key);
}
}
101 changes: 55 additions & 46 deletions src/main/java/com/cpcb/gs/io/TableLoader.java
Original file line number Diff line number Diff line change
@@ -1,75 +1,84 @@
package com.cpcb.gs.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cpcb.gs.io.ProtocolDeploy.TestEnum;

public class TableLoader {
private static HashMap<String, TableT> cacheMap;
private static Map<String, Map<Integer, ? extends BaseDeploy>> cacheMap = new HashMap<String, Map<Integer, ? extends BaseDeploy>>(); // 配置表缓存

public static TableT<? extends BaseDeploy> Load(String path, Class<? extends BaseDeploy> clazz) {
Iterable<CSVRecord> records = getCSVRecords(path);
public static Map<Integer, ?> Load(String path, Class<? extends BaseDeploy> clazz) {
if (cacheMap.containsKey(path)) {
return cacheMap.get(path);
}

try {
for (CSVRecord record : records) {
BaseDeploy deploy = clazz.newInstance();
deploy.setId(Integer.parseInt(record.get("id")));
Iterable<CSVRecord> records = getCSVRecords(path);
if (records != null) {
try {
Map<Integer, BaseDeploy> deployMap = new HashMap<Integer, BaseDeploy>();
for (CSVRecord record : records) {
BaseDeploy deploy = ResolveRecord(record, clazz);
deployMap.put(deploy.id, deploy);
}
cacheMap.put(path, deployMap);
return deployMap;
} catch (Exception e) {
System.err.println(e.getMessage());
}
}

for (Field field : clazz.getDeclaredFields()) {
if (!record.isSet(field.getName())) {
continue;
}
return null;
}

Class<?> fieldClass = field.getType();
String fieldValue = record.get(field.getName());
if (int.class.equals(fieldClass)) {
field.setInt(deploy, Integer.parseInt(fieldValue));
} else if (long.class.equals(fieldClass)) {
field.setLong(deploy, Long.parseLong(fieldValue));
} else if (float.class.equals(fieldClass)) {
field.setFloat(deploy, Float.parseFloat(fieldValue));
} else if (double.class.equals(fieldClass)) {
field.setDouble(deploy, Double.parseDouble(fieldValue));
} else if (byte.class.equals(fieldClass)) {
field.setByte(deploy, Byte.parseByte(fieldValue));
} else if (String.class.equals(fieldClass)) {
field.set(deploy, fieldValue);
} else if (boolean.class.equals(fieldClass)) {
field.setBoolean(deploy, Integer.parseInt(fieldValue) != new Integer(0));
} else if (fieldClass.isEnum()) {
field.set(deploy, TestEnum.valueOf(fieldValue));
} else if (List.class.equals(fieldClass)) {
field.set(deploy, JSON.parseArray(fieldValue));
} else if (JSONObject.class.equals(fieldClass)) {
field.set(deploy, JSON.parseObject(fieldValue));
} else {
field.set(deploy, JSON.parseObject(fieldValue, fieldClass));
}
}
private static BaseDeploy ResolveRecord(CSVRecord record, Class<? extends BaseDeploy> clazz)
throws InstantiationException, IllegalAccessException {
BaseDeploy deploy = clazz.newInstance();
deploy.setId(Integer.parseInt(record.get("id")));

System.out.println(deploy.toString());
for (Field field : clazz.getDeclaredFields()) {
if (!record.isSet(field.getName())) {
continue;
}

return null;
} catch (Exception e) {
System.err.println(e.getMessage());
Class<?> fieldClass = field.getType();
String fieldValue = record.get(field.getName());
if (int.class.equals(fieldClass)) {
field.setInt(deploy, Integer.parseInt(fieldValue));
} else if (long.class.equals(fieldClass)) {
field.setLong(deploy, Long.parseLong(fieldValue));
} else if (float.class.equals(fieldClass)) {
field.setFloat(deploy, Float.parseFloat(fieldValue));
} else if (double.class.equals(fieldClass)) {
field.setDouble(deploy, Double.parseDouble(fieldValue));
} else if (byte.class.equals(fieldClass)) {
field.setByte(deploy, Byte.parseByte(fieldValue));
} else if (String.class.equals(fieldClass)) {
field.set(deploy, fieldValue);
} else if (boolean.class.equals(fieldClass)) {
field.setBoolean(deploy, Integer.parseInt(fieldValue) != new Integer(0));
} else if (fieldClass.isEnum()) {
field.set(deploy, TestEnum.valueOf(fieldValue));
} else if (List.class.equals(fieldClass)) {
field.set(deploy, JSON.parseArray(fieldValue));
} else if (JSONObject.class.equals(fieldClass)) {
field.set(deploy, JSON.parseObject(fieldValue));
} else {
field.set(deploy, JSON.parseObject(fieldValue, fieldClass));
}
}

return null;
return deploy;
}

private static Iterable<CSVRecord> getCSVRecords(String relativePath) {
Expand Down
30 changes: 0 additions & 30 deletions src/main/java/com/cpcb/gs/io/TableT.java

This file was deleted.

77 changes: 55 additions & 22 deletions src/test/java/com/cpcb/gs/utils/TestCommonsCSV.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,74 @@
package com.cpcb.gs.utils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.junit.Test;

import com.cpcb.gs.io.ProtocolDeploy;
import com.cpcb.gs.io.TableLoader;

public class TestCommonsCSV {

@Test
public void read_01() throws IOException {
File file = new File("test");
file.createNewFile();
System.out.println(file.getAbsolutePath());
System.out.println(getClass().getResource("/").toString());
System.out.println(System.getProperty("user.dir"));
Reader in = new FileReader("path/to/file.csv");
Iterable<CSVRecord> records = CSVFormat.TDF.parse(in);
for (CSVRecord record : records) {
String lastName = record.get("LastName");
String firstName = record.get("FirstName");

System.out.println(lastName + "->" + firstName);
}
public void test_tableLoader() throws IOException {
long start = System.currentTimeMillis();

ProtocolDeploy deploy = ProtocolDeploy.getDeploy(101);
System.out.println(deploy);
long end = System.currentTimeMillis();
System.err.println("Spend Time : " + (end - start));
System.err.println("Per Spend Time : " + (end - start) * 1f / 500f);
}

@Test
public void test_tableLoader() throws IOException {
public void test_tableLoader2() throws IOException {
long start = System.currentTimeMillis();

ProtocolDeploy deploy = ProtocolDeploy.getDeploy(1);
System.out.println(deploy);
long end = System.currentTimeMillis();
System.err.println("Spend Time : " + (end - start));
System.err.println("Per Spend Time : " + (end - start) * 1f / 500f);
}

@Test
public void test_tableLoader3() throws IOException {
long start = System.currentTimeMillis();
TableLoader.Load("protocol", ProtocolDeploy.class);

ProtocolDeploy deploy = ProtocolDeploy.getDeploy(11);
System.out.println(deploy);
long end = System.currentTimeMillis();
System.err.println("Spend Time : " + (end - start));
System.err.println("Per Spend Time : " + (end - start) * 1f / 500f);
}

@Test
public void test_tableLoader4() throws IOException {
long start = System.currentTimeMillis();

ProtocolDeploy deploy = ProtocolDeploy.getDeploy(100);
System.out.println(deploy);
long end = System.currentTimeMillis();
System.err.println("Spend Time : " + (end - start));
System.err.println("Per Spend Time : " + (end - start) * 1f / 500f);
}

@Test
public void test_tableLoader5() throws IOException {
long start = System.currentTimeMillis();

ProtocolDeploy deploy = ProtocolDeploy.getDeploy(201);
System.out.println(deploy);
long end = System.currentTimeMillis();
System.err.println("Spend Time : " + (end - start));
System.err.println("Per Spend Time : " + (end - start) * 1f / 500f);
}

@Test
public void test_tableLoader6() throws IOException {
long start = System.currentTimeMillis();

ProtocolDeploy deploy = ProtocolDeploy.getDeploy(401);
System.out.println(deploy);
long end = System.currentTimeMillis();
System.err.println("Spend Time : " + (end - start));
System.err.println("Per Spend Time : " + (end - start) * 1f / 500f);
Expand Down

0 comments on commit 03f2de4

Please sign in to comment.