changeset 342:a8794548f5c9

add logtest method
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 29 Aug 2017 11:33:54 +0900
parents 7388d8606528
children 3fc39bff7a67
files src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/LogupdateTestStartCodeSegment.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/Logupdator.java
diffstat 2 files changed, 118 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/LogupdateTestStartCodeSegment.java	Mon Aug 28 21:55:44 2017 +0900
+++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/LogupdateTestStartCodeSegment.java	Tue Aug 29 11:33:54 2017 +0900
@@ -1,13 +1,38 @@
 package jp.ac.u_ryukyu.ie.cr.junglenetwork.logupdate;
 
 import alice.codesegment.CodeSegment;
+import alice.datasegment.CommandType;
+import alice.datasegment.Receiver;
+import jp.ac.u_ryukyu.ie.cr.jungleNetwork.codesegment.LogUpdateCodeSegment;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * Created by kono on 2017/08/28.
  */
 public class LogupdateTestStartCodeSegment extends CodeSegment {
+
+    int bbsPort = 8080;
+    Receiver host = ids.create(CommandType.PEEK);
+    private Pattern pattern = Pattern.compile("^(node|cli)([0-9]+)$");
+    private String[] args;
+    boolean persistentFlag = false;
+
     @Override
     public void run() {
-        
+        String name = host.asString();
+        Matcher matcher = pattern.matcher(name);
+        matcher.find();
+        //		String type = matcher.group(1);
+        for(String arg: args)
+            if (arg.equals("-persistent")) {
+                persistentFlag = true;
+            }
+        Logupdator cassaBBS = new Logupdator(name);
+        cassaBBS.init();
+        ods.put("updator", cassaBBS);
+
+        new LogUpdateCodeSegment();
     }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/Logupdator.java	Tue Aug 29 11:33:54 2017 +0900
@@ -0,0 +1,92 @@
+package jp.ac.u_ryukyu.ie.cr.junglenetwork.logupdate;
+
+import jp.ac.u_ryukyu.ie.cr.jungle.Jungle;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.DefaultNodePath;
+import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor;
+import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode;
+import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree;
+import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
+import jp.ac.u_ryukyu.ie.cr.jungle.util.jungleError.Error;
+import jp.ac.u_ryukyu.ie.cr.jungleNetwork.codesegment.JungleDistributedUpdator;
+import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkTreeOperationLog;
+import jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction.JungleUpdater;
+
+import java.nio.ByteBuffer;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Created by kono on 2017/08/29.
+ */
+public class Logupdator implements JungleDistributedUpdator {
+
+    private Jungle jungle;
+    private AtomicInteger requestCounter = new AtomicInteger(0);
+
+    public Logupdator(String name) {
+    }
+
+    public void init() {
+    }
+
+    public int requestIncrementAndGet() {
+        return requestCounter.incrementAndGet();
+    }
+    @Override
+    public Either<Error, JungleTreeEditor> update(NetworkTreeOperationLog netLog) {
+        String treeName = netLog.getTreeName();
+        Jungle jungle = getJungle();
+        if (jungle.getTreeByName(treeName) == null) {
+            if(null == jungle.createNewTree(treeName)){
+                throw new IllegalStateException();
+            }
+        }
+        Either<Error, JungleTreeEditor> either = null;
+        JungleTree tree = jungle.getTreeByName(treeName);
+
+        long timestamp = System.currentTimeMillis();
+        ByteBuffer tBuffer = ByteBuffer.allocate(16);
+        DefaultNodePath root = new DefaultNodePath();
+        tBuffer.putLong(timestamp);
+        do {
+            JungleTreeEditor editor = tree.getJungleTreeEditor();
+			/*
+			 * Merge.
+			 */
+            int pos = calculatePosition(tree.getRootNode(), netLog.getTimeStamp());
+            either = JungleUpdater.edit(editor, netLog, pos);
+            if(either.isA()) {
+                throw new IllegalStateException();
+            }
+            editor = either.b();
+            either = editor.putAttribute(root, "renewtime", tBuffer);
+            if(either.isA()) {
+                throw new IllegalStateException();
+            }
+            editor = either.b();
+            either = editor.success();
+        }while(either.isA());
+        requestIncrementAndGet();
+        return either;
+    }
+
+    private int calculatePosition(TreeNode node, long newNodeTimeStamp) {
+        int count = 0;
+        long childTimeStamp = 0;
+        for(TreeNode n : node.getChildren()) {
+            ByteBuffer timestamp = n.getAttributes().get("timestamp");
+            if(timestamp == null) {
+                return count;
+            }
+            childTimeStamp = timestamp.getLong(0);
+            if (newNodeTimeStamp < childTimeStamp) {
+                break;
+            }
+            count++;
+        }
+        return count;
+    }
+
+    public Jungle getJungle() {
+        return jungle;
+    }
+}