Răsfoiți Sursa

内存读取结构

skyfffire 3 ani în urmă
părinte
comite
d15c5007d8

+ 10 - 0
pom.xml

@@ -93,5 +93,15 @@
                 </plugin>
             </plugins>
         </pluginManagement>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>8</source>
+                    <target>8</target>
+                </configuration>
+            </plugin>
+        </plugins>
     </build>
 </project>

+ 5 - 0
src/main/java/config/WebConfig.java

@@ -7,6 +7,7 @@ import com.jfinal.template.Engine;
 import controller.EthMevController;
 import controller.HelloController;
 import controller.SwapPathController;
+import memory.SwapPathMemoryDb;
 import model.EthMev;
 import model.SwapPath;
 
@@ -19,6 +20,7 @@ public class WebConfig extends JFinalConfig {
     private static String password = "Qwe410410.";
 
     static {
+        // 识别运行环境
         File file = new File(".debug");
         IsDev = file.exists();
     }
@@ -54,6 +56,9 @@ public class WebConfig extends JFinalConfig {
 
         arp.addMapping("t_ethereum_mev_v1", "hash", EthMev.class);
         arp.addMapping("t_swap_path", "sum_value_and_level", SwapPath.class);
+
+        // 初始化内存数据库线程
+        SwapPathMemoryDb.startBuildSwapPathGroupByLp();
     }
 
     @Override

+ 0 - 3
src/main/java/controller/SwapPathController.java

@@ -62,7 +62,6 @@ public class SwapPathController extends Controller {
 
     @Before(AuthInterceptor.class)
     public void findLevel2PathByLpAddress() {
-        System.out.println("零、" + System.currentTimeMillis());
         if (StrKit.isBlank(getPara("lp_address"))) {
             renderJson(MyRet.create().setFail().setMsg("缺少参数,请检查参数[lp_address]。"));
             return;
@@ -70,13 +69,11 @@ public class SwapPathController extends Controller {
 
         String lpAddress = getPara("lp_address");
         JSONArray pathList = service.generatePathListByLpAddress(lpAddress);
-        System.out.println("五、" + System.currentTimeMillis());
 
         if (pathList.size() == 0) {
             renderJson(MyRet.create().setOk().setMsg("没有找到LP:" + lpAddress + "相关路径。").setData(pathList));
         } else {
             renderJson(MyRet.create().setOk().setMsg("查询成功,LP:" + lpAddress + "。").setData(pathList));
         }
-        System.out.println("六、" + System.currentTimeMillis());
     }
 }

+ 0 - 1
src/main/java/interceptor/AuthInterceptor.java

@@ -10,7 +10,6 @@ import util.MyRet;
 public class AuthInterceptor implements Interceptor {
     @Override
     public void intercept(Invocation invocation) {
-        System.out.println("负、" + System.currentTimeMillis());
         Controller controller = invocation.getController();
 
         String timestamp = controller.getPara("timestamp");

+ 58 - 0
src/main/java/memory/SwapPathMemoryDb.java

@@ -0,0 +1,58 @@
+package memory;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import model.SwapPath;
+
+import java.util.List;
+
+public class SwapPathMemoryDb {
+    public static JSONObject GROUP_BY_LP = new JSONObject();
+
+    public static JSONArray getLevel2PathByLpAddress(String lpAddress) {
+        if (GROUP_BY_LP.get(lpAddress) == null) {
+            return JSONArray.parseArray("[]");
+        } else {
+            return GROUP_BY_LP.getJSONArray(lpAddress);
+        }
+    }
+
+    public static void handleAPath(SwapPath path, JSONObject tempGroupByLp) {
+        JSONArray swapPathListSameSum = JSONArray.parseArray(path.getStr("data"));
+
+        for (JSONArray swapPath: swapPathListSameSum.toJavaList(JSONArray.class)) {
+            String aLpAddress = swapPath.getJSONObject(0).getString("LP").toLowerCase();
+            String bLpAddress = swapPath.getJSONObject(1).getString("LP").toLowerCase();
+
+            if (tempGroupByLp.get(aLpAddress) == null) { tempGroupByLp.put(aLpAddress, JSONArray.parseArray("[]")); }
+            if (tempGroupByLp.get(bLpAddress) == null) { tempGroupByLp.put(bLpAddress, JSONArray.parseArray("[]")); }
+
+            tempGroupByLp.getJSONArray(aLpAddress).add(JSONArray.parseArray(swapPath.toJSONString()));
+            tempGroupByLp.getJSONArray(bLpAddress).add(JSONArray.parseArray(swapPath.toJSONString()));
+        }
+    }
+
+    public static void startBuildSwapPathGroupByLp() {
+        Runnable build = () -> {
+            while (true) {
+                try {
+                    Thread.sleep(1000);
+
+                    List<SwapPath> swapPathDbAssembly = SwapPath.dao.findAll();
+                    JSONObject tempGroupByLp = new JSONObject();
+
+                    for (SwapPath pathSameSum : swapPathDbAssembly) {
+                        handleAPath(pathSameSum, tempGroupByLp);
+                    }
+
+                    GROUP_BY_LP = tempGroupByLp;
+                } catch (InterruptedException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        };
+
+        Thread thread = new Thread(build);
+        thread.start();
+    }
+}

+ 2 - 23
src/main/java/service/SwapPathService.java

@@ -2,33 +2,12 @@ package service;
 
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
+import memory.SwapPathMemoryDb;
 import model.SwapPath;
 
 public class SwapPathService {
     public JSONArray generatePathListByLpAddress(String lpAddress) {
         lpAddress = lpAddress.toLowerCase();
-        JSONArray swapPathList = JSONArray.parseArray("[]");
-
-        String sql = "select * from t_swap_path where instr(lower(data), ?)";
-        System.out.println("二、" + System.currentTimeMillis());
-        SwapPath pathObj = SwapPath.dao.findFirst(sql, lpAddress);
-        System.out.println("三、" + System.currentTimeMillis());
-
-        if (pathObj != null) {
-            JSONArray unFilterSwapPathList = JSONArray.parseArray(pathObj.getStr("data"));
-
-            for (JSONArray swapPath : unFilterSwapPathList.toJavaList(JSONArray.class)) {
-                JSONObject aLp = swapPath.getJSONObject(0);
-                JSONObject bLp = swapPath.getJSONObject(1);
-
-                if (aLp.getString("LP").toLowerCase().equals(lpAddress)
-                || bLp.getString("LP").toLowerCase().equals(lpAddress)) {
-                    swapPathList.add(swapPath);
-                }
-            }
-        }
-        System.out.println("四、" + System.currentTimeMillis());
-
-        return swapPathList;
+        return SwapPathMemoryDb.getLevel2PathByLpAddress(lpAddress);
     }
 }