Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 344:3fc39bff7a67
merge
author | nozomi |
---|---|
date | Wed, 13 Sep 2017 16:32:31 +0900 |
parents | a8794548f5c9 (diff) 441c9edcb6b0 (current diff) |
children | ec382195c477 |
files | |
diffstat | 4 files changed, 173 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/LogupdateTest.java Wed Sep 13 16:32:31 2017 +0900 @@ -0,0 +1,17 @@ +package jp.ac.u_ryukyu.ie.cr.junglenetwork.logupdate; + +import alice.daemon.AliceDaemon; +import alice.datasegment.DataSegment; + +/** + * Created by kono on 2017/08/28. + */ +public class LogupdateTest { + public static void main(String[] args) { + LogupdateTestConfig conf = new LogupdateTestConfig(args);//トポロジー設定をコマンドライン引数からとって設定? + + new AliceDaemon(conf).listen();//構成開始?TopMはない + DataSegment.connect(conf.key, "", conf.hostname, conf.connectPort); + new LogupdateTestStartCodeSegment().execute(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/LogupdateTestConfig.java Wed Sep 13 16:32:31 2017 +0900 @@ -0,0 +1,26 @@ +package jp.ac.u_ryukyu.ie.cr.junglenetwork.logupdate; + +import alice.daemon.Config; + +/** + * Created by kono on 2017/08/28. + */ +public class LogupdateTestConfig extends Config { + + public String hostname = "127.0.0.1"; + public int connectPort = 10000; + public String key = "remote"; + + public LogupdateTestConfig(String[] args) { + super(args); + for (int i = 0; i< args.length; i++) { + if ("-h".equals(args[i])) { + hostname = args[++i]; + } else if ("-cp".equals(args[i])) { + connectPort = Integer.parseInt(args[++i]); + } else if ("-key".equals(args[i])) { + key = args[++i]; + } + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/LogupdateTestStartCodeSegment.java Wed Sep 13 16:32:31 2017 +0900 @@ -0,0 +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 Wed Sep 13 16:32:31 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; + } +}