From 2a59f577fc1869d6bc85620bac08e6978fb04fad Mon Sep 17 00:00:00 2001 From: wangxuxin Date: Sun, 25 Jun 2023 16:42:56 +0800 Subject: [PATCH] feat: sharable var integration --- .../bdware/sc/crdt/SharableVarManager.java | 10 ++++---- .../org/bdware/sc/crdt/SharableVarState.java | 3 ++- .../bdware/sc/crdt/proxy/GCounterProxy.java | 3 ++- .../sc/crdt/proxy/LWWRegisterProxy.java | 3 ++- .../bdware/sc/crdt/proxy/MVRegisterProxy.java | 3 ++- .../bdware/sc/crdt/proxy/PNCounterProxy.java | 3 ++- .../bdware/sc/crdt/proxy/RWLWWSetProxy.java | 3 ++- .../org/bdware/sc/crdt/proxy/SharableVar.java | 23 +++++++++++-------- .../org/bdware/sc/engine/DesktopEngine.java | 7 ++---- 9 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/bdware/sc/crdt/SharableVarManager.java b/src/main/java/org/bdware/sc/crdt/SharableVarManager.java index f4c17a6..789c176 100644 --- a/src/main/java/org/bdware/sc/crdt/SharableVarManager.java +++ b/src/main/java/org/bdware/sc/crdt/SharableVarManager.java @@ -107,10 +107,11 @@ public class SharableVarManager { return allVars.get(identifier); } StateInfoBase stateInfoBase = client.resolve(identifier); - if (stateInfoBase.handleValues.has("bdwType") && - stateInfoBase.handleValues.get("bdwType").getAsString().equals("SharableVar")) { + if (stateInfoBase.handleValues.has("bdwType") && stateInfoBase.handleValues + .get("bdwType").getAsString().equals("SharableVar")) { SharableVarState.SharableVarConfiguration sharableVarConf = - JsonUtil.fromJson(stateInfoBase.handleValues, SharableVarState.SharableVarConfiguration.class); + JsonUtil.fromJson(stateInfoBase.handleValues, + SharableVarState.SharableVarConfiguration.class); SharableVar sharableVar = createSharableVar(sharableVarConf, identifier, type); if (sharableVar != null) { allVars.put(identifier, sharableVar); @@ -124,7 +125,8 @@ public class SharableVarManager { } } - private SharableVar createSharableVar(SharableVarState.SharableVarConfiguration conf, String identifier, String type) { + private SharableVar createSharableVar(SharableVarState.SharableVarConfiguration conf, + String identifier, String type) { switch (type) { case Constants.TypeName.G_COUNTER: return new GCounterProxy(identifier, cpId, conf); diff --git a/src/main/java/org/bdware/sc/crdt/SharableVarState.java b/src/main/java/org/bdware/sc/crdt/SharableVarState.java index 5c290e6..64d350d 100644 --- a/src/main/java/org/bdware/sc/crdt/SharableVarState.java +++ b/src/main/java/org/bdware/sc/crdt/SharableVarState.java @@ -55,7 +55,8 @@ public class SharableVarState { LinkedHashSet result = new LinkedHashSet<>(); int[] sendToIndexes = sharableVarConfiguration.sendTo[myIndex]; for (int sendToIndex : sendToIndexes) { - if (sendToIndex >= 0 && sendToIndex < sharableVarConfiguration.nodeIds.length && sendToIndex != myIndex) { + if (sendToIndex >= 0 && sendToIndex < sharableVarConfiguration.nodeIds.length + && sendToIndex != myIndex) { result.add(sharableVarConfiguration.nodeIds[sendToIndex]); } } diff --git a/src/main/java/org/bdware/sc/crdt/proxy/GCounterProxy.java b/src/main/java/org/bdware/sc/crdt/proxy/GCounterProxy.java index 0870468..11e8132 100644 --- a/src/main/java/org/bdware/sc/crdt/proxy/GCounterProxy.java +++ b/src/main/java/org/bdware/sc/crdt/proxy/GCounterProxy.java @@ -5,7 +5,8 @@ import org.bdware.sc.crdt.SharableVarState; public class GCounterProxy extends SharableVar { - public GCounterProxy(String varId, String cpId, SharableVarState.SharableVarConfiguration conf) { + public GCounterProxy(String varId, String cpId, + SharableVarState.SharableVarConfiguration conf) { super(varId, cpId, conf); self = new GCounter(cpId, varId); } diff --git a/src/main/java/org/bdware/sc/crdt/proxy/LWWRegisterProxy.java b/src/main/java/org/bdware/sc/crdt/proxy/LWWRegisterProxy.java index 477da0a..ad73177 100644 --- a/src/main/java/org/bdware/sc/crdt/proxy/LWWRegisterProxy.java +++ b/src/main/java/org/bdware/sc/crdt/proxy/LWWRegisterProxy.java @@ -4,7 +4,8 @@ import org.bdware.crdt.register.LWWRegister; import org.bdware.sc.crdt.SharableVarState; public class LWWRegisterProxy extends SharableVar> { - public LWWRegisterProxy(String varId, String cpId, SharableVarState.SharableVarConfiguration conf) { + public LWWRegisterProxy(String varId, String cpId, + SharableVarState.SharableVarConfiguration conf) { super(varId, cpId, conf); self = new LWWRegister<>(cpId, varId); } diff --git a/src/main/java/org/bdware/sc/crdt/proxy/MVRegisterProxy.java b/src/main/java/org/bdware/sc/crdt/proxy/MVRegisterProxy.java index ac02dcd..460a7c8 100644 --- a/src/main/java/org/bdware/sc/crdt/proxy/MVRegisterProxy.java +++ b/src/main/java/org/bdware/sc/crdt/proxy/MVRegisterProxy.java @@ -5,7 +5,8 @@ import org.bdware.sc.crdt.SharableVarState; public class MVRegisterProxy extends SharableVar> { - public MVRegisterProxy(String varId, String cpId, SharableVarState.SharableVarConfiguration conf) { + public MVRegisterProxy(String varId, String cpId, + SharableVarState.SharableVarConfiguration conf) { super(varId, cpId, conf); self = new MVRegister<>(cpId, varId); } diff --git a/src/main/java/org/bdware/sc/crdt/proxy/PNCounterProxy.java b/src/main/java/org/bdware/sc/crdt/proxy/PNCounterProxy.java index eea4569..b566c99 100644 --- a/src/main/java/org/bdware/sc/crdt/proxy/PNCounterProxy.java +++ b/src/main/java/org/bdware/sc/crdt/proxy/PNCounterProxy.java @@ -4,7 +4,8 @@ import org.bdware.crdt.counter.PNCounter; import org.bdware.sc.crdt.SharableVarState; public class PNCounterProxy extends SharableVar { - public PNCounterProxy(String varId, String cpId, SharableVarState.SharableVarConfiguration conf) { + public PNCounterProxy(String varId, String cpId, + SharableVarState.SharableVarConfiguration conf) { super(varId, cpId, conf); self = new PNCounter(cpId, varId); } diff --git a/src/main/java/org/bdware/sc/crdt/proxy/RWLWWSetProxy.java b/src/main/java/org/bdware/sc/crdt/proxy/RWLWWSetProxy.java index bf07b75..4fbd074 100644 --- a/src/main/java/org/bdware/sc/crdt/proxy/RWLWWSetProxy.java +++ b/src/main/java/org/bdware/sc/crdt/proxy/RWLWWSetProxy.java @@ -6,7 +6,8 @@ import org.bdware.sc.crdt.SharableVarState; import java.util.Set; public class RWLWWSetProxy extends SharableVar> { - public RWLWWSetProxy(String varId, String cpId, SharableVarState.SharableVarConfiguration conf) { + public RWLWWSetProxy(String varId, String cpId, + SharableVarState.SharableVarConfiguration conf) { super(varId, cpId, conf); self = new RWLWWSet<>(cpId, varId); } diff --git a/src/main/java/org/bdware/sc/crdt/proxy/SharableVar.java b/src/main/java/org/bdware/sc/crdt/proxy/SharableVar.java index 96fda8b..883acab 100644 --- a/src/main/java/org/bdware/sc/crdt/proxy/SharableVar.java +++ b/src/main/java/org/bdware/sc/crdt/proxy/SharableVar.java @@ -14,12 +14,11 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public abstract class SharableVar { - public final HashedWheelTimer HASHED_WHEEL_TIMER = - new HashedWheelTimer(r -> { - Thread t = Executors.defaultThreadFactory().newThread(r); - t.setDaemon(true); - return t; - }, 5, TimeUnit.MILLISECONDS, 2); + public final HashedWheelTimer HASHED_WHEEL_TIMER = new HashedWheelTimer(r -> { + Thread t = Executors.defaultThreadFactory().newThread(r); + t.setDaemon(true); + return t; + }, 5, TimeUnit.MILLISECONDS, 2); public final Queue localDeltaQueue = new LinkedList<>(); public final Queue remoteDeltaQueue = new LinkedList<>(); @@ -28,12 +27,14 @@ public abstract class SharableVar { private SyncTimerTask syncTimerTask; private SharableVarState sharableVarState; - public SharableVar(String varId, String cpId, SharableVarState.SharableVarConfiguration resolveResult) { + public SharableVar(String varId, String cpId, + SharableVarState.SharableVarConfiguration resolveResult) { this.varId = varId; this.sharableVarState = new SharableVarState(cpId, resolveResult); this.HASHED_WHEEL_TIMER.newTimeout(timeout -> { syncTimerTask = new SyncTimerTask(); - HASHED_WHEEL_TIMER.newTimeout(syncTimerTask, sharableVarState.getInterval(), TimeUnit.MILLISECONDS); + HASHED_WHEEL_TIMER.newTimeout(syncTimerTask, sharableVarState.getInterval(), + TimeUnit.MILLISECONDS); }, 0, TimeUnit.MILLISECONDS); } @@ -49,7 +50,8 @@ public abstract class SharableVar { } } String content = JsonUtil.toJson(localDeltaQueue); - SharableVarManager.instance.broadcastSyncMessage(varId, sharableVarState.getSendTo(), content); + SharableVarManager.instance.broadcastSyncMessage(varId, sharableVarState.getSendTo(), + content); } abstract protected T newEmptyDelta(); @@ -62,7 +64,8 @@ public abstract class SharableVar { } catch (Exception e) { e.printStackTrace(); } finally { - HASHED_WHEEL_TIMER.newTimeout(syncTimerTask, sharableVarState.getInterval(), TimeUnit.MILLISECONDS); + HASHED_WHEEL_TIMER.newTimeout(syncTimerTask, sharableVarState.getInterval(), + TimeUnit.MILLISECONDS); } } } diff --git a/src/main/java/org/bdware/sc/engine/DesktopEngine.java b/src/main/java/org/bdware/sc/engine/DesktopEngine.java index 6a86463..fba5905 100644 --- a/src/main/java/org/bdware/sc/engine/DesktopEngine.java +++ b/src/main/java/org/bdware/sc/engine/DesktopEngine.java @@ -298,11 +298,8 @@ public class DesktopEngine extends JSEngine { private void compileSharable(SharableNode sharable, String variableStatement) { try { - engine.getContext() - .setAttribute( - ScriptEngine.FILENAME, - sharable.getFileName(), - ScriptContext.ENGINE_SCOPE); + engine.getContext().setAttribute(ScriptEngine.FILENAME, sharable.getFileName(), + ScriptContext.ENGINE_SCOPE); engine.eval("var " + variableStatement); LOGGER.info("load sharable: " + variableStatement); } catch (ScriptException e) {