|
|
@@ -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();
|
|
|
+ }
|
|
|
+}
|