Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 352:7193845c26cb
rename package
author | one |
---|---|
date | Mon, 25 Sep 2017 19:00:06 +0900 |
parents | 9d49d6bcab95 |
children | 15061909f4da |
files | src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/DataWriteBufferTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/HashSetConvertTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/Persistent/PersistentDefaultJournalTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/Persistent/PersistentDifferentialJournalTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkAppendChildOperationTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkDeleteAttributeOprationTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkDeleteChildAtOperationTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkNodeOperationTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkPutAttributeOperationTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkTreeOperationLogTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkTreeOperationTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/datasegment/NetworkNodePathTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/logupdate/LogupdateTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/logupdate/LogupdateTestConfig.java 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 src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/DataWriteBufferTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/HashSetConvertTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/Persistent/PersistentDefaultJournalTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/Persistent/PersistentDifferentialJournalTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkAppendChildOperationTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkDeleteAttributeOprationTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkDeleteChildAtOperationTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkNodeOperationTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkPutAttributeOperationTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkTreeOperationLogTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkTreeOperationTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/datasegment/NetworkNodePathTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/LogupdateTest.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/LogupdateTestConfig.java 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 | 32 files changed, 771 insertions(+), 771 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/DataWriteBufferTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,111 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork; + +import jp.ac.u_ryukyu.ie.cr.jungle.Jungle; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.core.Children; +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListReader; +import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.treeEditor.DefaultTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser; +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.operations.NetworkNodePath; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJournal; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJungle; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction.JungleUpdater; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Iterator; + +public class DataWriteBufferTest { + ByteBuffer value = ByteBuffer.wrap("value".getBytes()); + String key = "key"; + int pos = 0; + + @Test + public void DataWriteBufferTest() throws IOException { + PersistentJournal journal1 = new PersistentJournal("commit.log"); + Jungle jungle = new PersistentJungle(journal1, "uuid",new DefaultTraverser()); + jungle.createNewTree("hoge"); + JungleTree tree1 = jungle.getTreeByName("hoge"); + tree1.setBufferSize(10); + JungleTreeEditor editor = tree1.getJungleTreeEditor(); + NetworkNodePath path = new NetworkNodePath(); + Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, pos); + NodePath childPath = path.add(pos); + Assert.assertFalse(either.isA()); + editor = either.b(); + either = editor.putAttribute(childPath, key, value); + JungleTreeEditor editor2 = either.b(); + either = editor2.success(); + Assert.assertFalse(either.isA()); + + PersistentJournal journal2 = new PersistentJournal(); + journal2.setInputFile(new File( journal1.getLogName())); + journal2.setOutputFile(new File( journal1.getLogName())); + ChangeListReader reader = journal2.getReader(); + Iterator<ChangeList> iterator = reader.iterator(); + Assert.assertFalse(iterator.hasNext()); + + for (int count = 0; count < 9; count++) { + editor = tree1.getJungleTreeEditor(); + either = editor.addNewChildAt(path, pos); + childPath = path.add(pos); + Assert.assertFalse(either.isA()); + editor = either.b(); + either = editor.putAttribute(childPath, key, value); + editor2 = either.b(); + either = editor2.success(); + Assert.assertFalse(either.isA()); + } + editor = tree1.getJungleTreeEditor(); + either = editor.flushSuccess(); + Assert.assertFalse(either.isA()); + + PersistentJournal journal3 = new PersistentJournal(); + journal3.setInputFile(new File( journal1.getLogName())); + journal3.setOutputFile(new File( journal1.getLogName())); + Jungle jungle2 = new PersistentJungle(journal1, "uuid3",new DefaultTraverser()); + ChangeListReader reader2 = journal3.getReader(); + for (ChangeList chList : reader2) { + String treeName = chList.getTreeName(); + JungleTree tree2 = jungle2.getTreeByName(treeName); + if (tree2 == null) { + tree2 = jungle2.createNewTree(treeName); + } + editor2 = tree2.getJungleTreeEditor(); + Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList); + Assert.assertFalse(either2.isA()); + editor2 = either2.b(); + editor2.success(); + } + + JungleTree tree2 = jungle2.getTreeByName("hoge"); + TreeNode node1 = tree1.getRootNode(); + TreeNode node2 = tree2.getRootNode(); + Children child1 = node1.getChildren(); + Children child2 = node2.getChildren(); + Assert.assertEquals(child1.size(), child2.size()); + Either<Error, TreeNode> either1 = child1.at(pos); + Either<Error, TreeNode> either2 = child1.at(pos); + Assert.assertFalse(either1.isA()); + Assert.assertFalse(either2.isA()); + + TreeNode nodeA = either1.b(); + TreeNode nodeB = either2.b(); + ByteBuffer bb1 = nodeA.getAttributes().get(key); + ByteBuffer bb2 = nodeB.getAttributes().get(key); + String strA = new String(bb1.array()); + String strB = new String(bb2.array()); + Assert.assertTrue(strA.equals(strB)); + journal1.close(); + journal2.close(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/HashSetConvertTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,40 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; +import org.msgpack.annotation.Message; + +import java.util.HashSet; + +public class HashSetConvertTest extends CodeSegment { + + Receiver hash = ids.create(CommandType.TAKE); + + public HashSetConvertTest() { + hash.setKey("hash"); + } + + public static void main(String[] args) { + HashSetDataSegment h = new HashSetDataSegment(); + h.hash.add("test1"); + h.hash.add("test2"); + + HashSetConvertTest cs = new HashSetConvertTest(); + cs.ods.put("hash", h); + } + + public void run() { + HashSetDataSegment h = hash.asClass(HashSetDataSegment.class); + for(String s : h.hash ) { + System.out.println("s : "+s); + } + System.exit(0); + } + + @Message + private static class HashSetDataSegment { + public HashSet<String> hash = new HashSet<String>(); + public HashSetDataSegment() {} + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/Persistent/PersistentDefaultJournalTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,84 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.Persistent; + +import jp.ac.u_ryukyu.ie.cr.jungle.Jungle; +import jp.ac.u_ryukyu.ie.cr.jungle.core.Children; +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListReader; +import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath; +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.traverser.DefaultTraverser; +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.operations.NetworkNodePath; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJournal; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJungle; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction.JungleUpdater; +import junit.framework.TestCase; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; + +public class PersistentDefaultJournalTest extends TestCase { + + public void testPersistentJounal() throws IOException { + PersistentJournal journal1 = new PersistentJournal("commit.log"); + Jungle jungle = new PersistentJungle(journal1, "uuid", new DefaultTraverser()); + jungle.createNewTree("hoge"); + JungleTree tree1 = jungle.getTreeByName("hoge"); + JungleTreeEditor editor = tree1.getJungleTreeEditor(); + NetworkNodePath path = new NetworkNodePath(); + String key = "key"; + ByteBuffer value = ByteBuffer.wrap("value".getBytes()); + int pos = 0; + Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, pos); + NodePath childPath = path.add(pos); + assertFalse(either.isA()); + editor = either.b(); + either = editor.putAttribute(childPath, key, value); + JungleTreeEditor e = either.b(); + either = e.success(); + assertFalse(either.isA()); + + PersistentJournal journal2 = new PersistentJournal(); + journal2.setInputFile(new File( journal1.getLogName())); + journal2.setOutputFile(new File(journal1.getLogName())); + Jungle jungle2 = new PersistentJungle(journal1, "uuid2", new DefaultTraverser()); + ChangeListReader reader = journal2.getReader(); + for (ChangeList chList : reader) { + String treeName = chList.getTreeName(); + JungleTree tree2 = jungle2.getTreeByName(treeName); + int treeType = chList.getTreeType(); + if (tree2 == null) { + tree2 = jungle2.createNewTree(treeName); + } + JungleTreeEditor editor2 = tree2.getJungleTreeEditor(); + Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList, treeType); + Either<Error, JungleTreeEditor> either3 = either2.b().success(); + assertFalse(either3.isA()); + } + JungleTree tree2 = jungle2.getTreeByName("hoge"); + TreeNode node1 = tree1.getRootNode(); + TreeNode node2 = tree2.getRootNode(); + Children child1 = node1.getChildren(); + Children child2 = node2.getChildren(); + assertEquals(child1.size(), child2.size()); + Either<Error, TreeNode> either1 = child1.at(pos); + Either<Error, TreeNode> either2 = child1.at(pos); + assertFalse(either1.isA()); + assertFalse(either2.isA()); + + TreeNode nodeA = either1.b(); + TreeNode nodeB = either2.b(); + ByteBuffer bb1 = nodeA.getAttributes().get(key); + ByteBuffer bb2 = nodeB.getAttributes().get(key); + String strA = new String(bb1.array()); + String strB = new String(bb2.array()); + assertTrue(strA.equals(strB)); + journal1.close(); + journal2.close(); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/Persistent/PersistentDifferentialJournalTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,111 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.Persistent; + +import jp.ac.u_ryukyu.ie.cr.jungle.Jungle; +import jp.ac.u_ryukyu.ie.cr.jungle.core.Attributes; +import jp.ac.u_ryukyu.ie.cr.jungle.core.Children; +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListReader; +import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.InterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath; +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.traverser.DefaultTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.TreeType; +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.operations.NetworkNodePath; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJournal; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJungle; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction.JungleUpdater; +import junit.framework.TestCase; +import org.junit.Assert; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Iterator; + + +public class PersistentDifferentialJournalTest extends TestCase { + + public void testPersistentJounal() throws IOException { + PersistentJournal journal1 = new PersistentJournal("commit.log"); + Jungle jungle = new PersistentJungle(journal1, "uuid", new DefaultTraverser()); + jungle.createNewDifferenceTree("hoge"); + JungleTree tree1 = jungle.getTreeByName("hoge"); + + for (int i = 0 ; i < 3 ; i++) { + JungleTreeEditor editor = tree1.getJungleTreeEditor(); + NetworkNodePath path = new NetworkNodePath(); + String key = "key"; + ByteBuffer value = ByteBuffer.wrap("value".getBytes()); + int pos = 0; + Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, pos); + NodePath childPath = path.add(pos); + assertFalse(either.isA()); + editor = either.b(); + either = editor.putAttribute(childPath, key, value); + JungleTreeEditor e = either.b(); + either = e.success(); + assertFalse(either.isA()); + } + PersistentJournal journal2 = new PersistentJournal(); + journal2.setInputFile(new File(journal1.getLogName())); + journal2.setOutputFile(new File(journal1.getLogName())); + Jungle jungle2 = new PersistentJungle(journal1, "uuid2", new DefaultTraverser()); + ChangeListReader reader = journal2.getReader(); + for (ChangeList chList : reader) { + String treeName = chList.getTreeName(); + JungleTree tree2 = jungle2.getTreeByName(treeName); + int treeType = chList.getTreeType(); + if (tree2 == null) { + Assert.assertEquals(treeType, TreeType.DIFFERENCE); + tree2 = jungle2.createNewDifferenceTree(treeName); + } + JungleTreeEditor editor2 = tree2.getJungleTreeEditor(); + Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList); + Either<Error, JungleTreeEditor> either3 = either2.b().success(); + assertFalse(either3.isA()); + } + JungleTree tree2 = jungle2.getTreeByName("hoge"); + + + InterfaceTraverser traverser1 = tree1.getTraverser(true); + Iterator<TreeNode> allNodeIterator1 = traverser1.find((TreeNode n) -> { + return true; + }); + + InterfaceTraverser traverser2 = tree2.getTraverser(true); + Iterator<TreeNode> allNodeIterator2 = traverser2.find((TreeNode n) -> { + return true; + }); + + while (allNodeIterator1.hasNext() || allNodeIterator2.hasNext()) { + TreeNode node1 = allNodeIterator1.next(); + TreeNode node2 = allNodeIterator2.next(); + Children children1 = node1.getChildren(); + Children children2 = node2.getChildren(); + int childrenCount1 = children1.size(); + int childrenCount2 = children2.size(); + Assert.assertEquals(childrenCount1,childrenCount2); + Attributes attribute1 = node1.getAttributes(); + Attributes attribute2 = node2.getAttributes(); + Iterator<String> keys1 = attribute1.getKeys(); + Iterator<String> keys2 = attribute2.getKeys(); + while (keys1.hasNext() || keys2.hasNext()) { + String key1 = keys1.next(); + String key2 = keys2.next(); + Assert.assertEquals(key1,key2); + ByteBuffer value1 = attribute1.get(key1); + ByteBuffer value2 = attribute2.get(key2); + Assert.assertEquals(value1,value2); + } + + } + journal1.close(); + journal2.close(); + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkAppendChildOperationTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,23 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.core.operations; + + +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkAppendChildAtOperation; +import junit.framework.TestCase; +import org.msgpack.MessagePack; +import org.msgpack.type.Value; + +import java.io.IOException; + +public class NetworkAppendChildOperationTest extends TestCase { + + public void testMsgpackConvert() throws IOException { + NetworkAppendChildAtOperation op = new NetworkAppendChildAtOperation(1); + MessagePack msgpack = new MessagePack(); + Value v = msgpack.unconvert(op); + NetworkAppendChildAtOperation mOp = msgpack.convert(v, NetworkAppendChildAtOperation.class); +// NodeOperation mOp = msgpack.convert(v, NodeOperation.class); + assertEquals(op.getCommand(), mOp.getCommand()); + assertEquals(op.getPosition(), 1); + assertEquals(op.getPosition(), mOp.getPosition()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkDeleteAttributeOprationTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,24 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.core.operations; + + +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkDeleteAttributeOperation; +import junit.framework.TestCase; +import org.msgpack.MessagePack; +import org.msgpack.type.Value; + +import java.io.IOException; + +public class NetworkDeleteAttributeOprationTest extends TestCase { + + public void testMsgpackConvert() throws IOException { + NetworkDeleteAttributeOperation op = new NetworkDeleteAttributeOperation("hoge"); + MessagePack msgpack = new MessagePack(); + Value v = msgpack.unconvert(op); + NetworkDeleteAttributeOperation mOp = msgpack.convert(v, NetworkDeleteAttributeOperation.class); + assertEquals(op.getCommand(), mOp.getCommand()); + assert(mOp.getKey().equals("hoge")); + + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkDeleteChildAtOperationTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,24 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.core.operations; + + +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkDeleteChildAtOperation; +import junit.framework.TestCase; +import org.msgpack.MessagePack; +import org.msgpack.type.Value; + +import java.io.IOException; + +public class NetworkDeleteChildAtOperationTest extends TestCase { + + + public void testMsgpackConvert() throws IOException { + NetworkDeleteChildAtOperation op = new NetworkDeleteChildAtOperation(1); + MessagePack msgpack = new MessagePack(); + Value v = msgpack.unconvert(op); + NetworkDeleteChildAtOperation mOp = msgpack.convert(v, NetworkDeleteChildAtOperation.class); + assertEquals(op.getCommand(), mOp.getCommand()); + assertEquals(op.getPosition(), mOp.getPosition()); + + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkNodeOperationTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,25 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.core.operations; + + +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkAppendChildAtOperation; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodeOperation; +import junit.framework.TestCase; +import org.msgpack.MessagePack; +import org.msgpack.type.Value; + +import java.io.IOException; + +public class NetworkNodeOperationTest extends TestCase { + + public void testMsgpackConvert() throws IOException { + NetworkAppendChildAtOperation op = new NetworkAppendChildAtOperation(1); + NetworkNodeOperation nOp = new NetworkNodeOperation(op); + MessagePack msgpack = new MessagePack(); + Value v = msgpack.unconvert(nOp); + NetworkNodeOperation mOp = msgpack.convert(v, NetworkNodeOperation.class); + assertEquals(op.getCommand(), mOp.getCommand()); + assertEquals(op.getPosition(), 1); + assertEquals(op.getPosition(), mOp.getPosition()); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkPutAttributeOperationTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,25 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.core.operations; + + +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkPutAttributeOperation; +import junit.framework.TestCase; +import org.msgpack.MessagePack; +import org.msgpack.type.Value; + +import java.io.IOException; +import java.nio.ByteBuffer; + +public class NetworkPutAttributeOperationTest extends TestCase { + + public void testMsgpackConvert() throws IOException { + ByteBuffer value = ByteBuffer.allocate(16); + value.put("fuga".getBytes()); + NetworkPutAttributeOperation op = new NetworkPutAttributeOperation("hoge", value); + MessagePack msgpack = new MessagePack(); + Value v = msgpack.unconvert(op); + NetworkPutAttributeOperation mOp = msgpack.convert(v, NetworkPutAttributeOperation.class); + assertEquals(op.getCommand(), mOp.getCommand()); + //assert((new String(op.getValue().array())).equals(new String(mOp.getValue().array()))); よくわからん byteBufferのunconvertがそもそも出来ない + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkTreeOperationLogTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,29 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.core.operations; + + +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkAppendChildAtOperation; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodePath; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkTreeOperationLog; +import junit.framework.TestCase; +import org.msgpack.MessagePack; +import org.msgpack.type.Value; + +import java.io.IOException; + +public class NetworkTreeOperationLogTest extends TestCase { + + public void testMsgpackConvert() throws IOException { + NetworkAppendChildAtOperation op = new NetworkAppendChildAtOperation(1); + NetworkNodePath path = new NetworkNodePath(); + path.add(1).add(2); + NetworkTreeOperationLog log = new NetworkTreeOperationLog(); + log.add(path, op); + log.add(path, op); + MessagePack msgpack = new MessagePack(); + Value v = msgpack.unconvert(log); + NetworkTreeOperationLog mLog = msgpack.convert(v, NetworkTreeOperationLog.class); + assertNotNull(mLog); + + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/operations/NetworkTreeOperationTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,27 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.core.operations; + + +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkAppendChildAtOperation; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodePath; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkTreeOperation; +import junit.framework.TestCase; +import org.msgpack.MessagePack; +import org.msgpack.type.Value; + +import java.io.IOException; + +public class NetworkTreeOperationTest extends TestCase { + + public void testMsgpackConvert() throws IOException { + NetworkAppendChildAtOperation op = new NetworkAppendChildAtOperation(1); + NetworkNodePath path = new NetworkNodePath(); + path.add(1).add(2); + NetworkTreeOperation treeOp = new NetworkTreeOperation(path, op); + MessagePack msgpack = new MessagePack(); + Value v = msgpack.unconvert(treeOp); + NetworkTreeOperation mOp = msgpack.convert(v, NetworkTreeOperation.class); + assertNotNull(mOp.getNodePath()); + assertEquals(op.getCommand(), mOp.getNodeOperation().getCommand()); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/datasegment/NetworkNodePathTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,32 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.datasegment; + + +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodePath; +import junit.framework.TestCase; +import org.msgpack.MessagePack; +import org.msgpack.type.Value; + +import java.io.IOException; +import java.util.Iterator; + +public class NetworkNodePathTest extends TestCase { + + public void testMsgpackConvert() throws IOException { + NetworkNodePath path = new NetworkNodePath(); + path = path.add(1).add(2).add(3); + MessagePack msgpack = new MessagePack(); + Value value = msgpack.unconvert(path); + NetworkNodePath mPath = msgpack.convert(value, NetworkNodePath.class); + assertEquals(path.size(), mPath.size()); + Iterator<Integer> iter1 = path.iterator(); + Iterator<Integer> iter2 = mPath.iterator(); + while(iter1.hasNext() & iter2.hasNext()) { + int i1 = iter1.next(); + int i2 = iter2.next(); + assertEquals(i1, i2); + } + } + + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/logupdate/LogupdateTest.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,22 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.logupdate; + + +import alice.topology.node.TopologyNode; +import alice.topology.node.TopologyNodeConfig; + +/** + * Created by kono on 2017/08/28. + */ +public class LogupdateTest { + public static void main(String[] args) { + LogupdateTestConfig conf = new LogupdateTestConfig(args); + LogupdateTestStartCodeSegment cs = new LogupdateTestStartCodeSegment(); + cs.ods.put("config", conf); + if (conf.getManagerHostName() !=null){ + new TopologyNode(conf, cs); + } else { + cs.ods.put("host", "local0"); + cs.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 Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,41 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.logupdate; + +import alice.daemon.Config; +import alice.topology.node.TopologyNode; +import alice.topology.node.TopologyNodeConfig; + +/** + * Created by kono on 2017/08/28. + */ +public class LogupdateTestConfig extends TopologyNodeConfig { + + public String hostname = "127.0.0.1"; + public int connectPort = 10000; + public String key = "remote"; + private boolean writeMode = false; + private String[] args1; + + public LogupdateTestConfig(String[] args) { + super(args); + args1 = 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]; + } else if ("-write".equals(args[i])) { + writeMode = true; + } + } + } + + public boolean isWriteMode() { + return writeMode; + } + + public String[] getArgs() { + return args1; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/logupdate/LogupdateTestStartCodeSegment.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,58 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.logupdate; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; +import alice.topology.node.TopologyNodeConfig; +import jp.ac.u_ryukyu.ie.cr.jungle.DefaultJungle; +import jp.ac.u_ryukyu.ie.cr.jungle.bbs.BBSTest; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.codesegment.LogUpdateCodeSegment; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.core.NetworkDefaultJungle; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.AliceJournal; + +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); + Receiver conf = ids.create(CommandType.PEEK); + private Pattern pattern = Pattern.compile("^(node|cli)([0-9]+)$"); + private String[] args; + boolean persistentFlag = false; + + public LogupdateTestStartCodeSegment(){ + conf.setKey("config"); + host.setKey("host"); + } + + @Override + public void run() { + String name = host.asString(); + LogupdateTestConfig config = conf.asClass(LogupdateTestConfig.class); + args = config.getArgs(); + + Matcher matcher = pattern.matcher(name); + matcher.find(); + // String type = matcher.group(1); + for(String arg: args) + if (arg.equals("-persistent")) { + persistentFlag = true; + } + BBSTest bbsTest = new BBSTest(new NetworkDefaultJungle(new AliceJournal(), "hoge")); + Logupdator cassaBBS = new Logupdator(name, bbsTest); + cassaBBS.init(); + ods.put("updator", cassaBBS); + + new LogUpdateCodeSegment(); + if (config.isWriteMode()){ + bbsTest.createBoard(); + bbsTest.createMessage(); + bbsTest.checkMessage(); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/logupdate/Logupdator.java Mon Sep 25 19:00:06 2017 +0900 @@ -0,0 +1,95 @@ +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.bbs.BBSTest; +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 BBSTest bbsTest; + private AtomicInteger requestCounter = new AtomicInteger(0); + + public Logupdator(String name, BBSTest bbsTest) { + this.bbsTest = bbsTest; + } + + 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.getLocalJungleTreeEditor(); + /* + * 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(); + bbsTest.checkMessage(); + 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 bbsTest.getJungle(); + } +}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/DataWriteBufferTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,111 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork; - -import jp.ac.u_ryukyu.ie.cr.jungle.Jungle; -import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor; -import jp.ac.u_ryukyu.ie.cr.jungle.core.Children; -import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; -import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListReader; -import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.treeEditor.DefaultTreeEditor; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; -import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser; -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.operations.NetworkNodePath; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJournal; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJungle; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction.JungleUpdater; -import org.junit.Assert; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.Iterator; - -public class DataWriteBufferTest { - ByteBuffer value = ByteBuffer.wrap("value".getBytes()); - String key = "key"; - int pos = 0; - - @Test - public void DataWriteBufferTest() throws IOException { - PersistentJournal journal1 = new PersistentJournal("commit.log"); - Jungle jungle = new PersistentJungle(journal1, "uuid",new DefaultTraverser()); - jungle.createNewTree("hoge"); - JungleTree tree1 = jungle.getTreeByName("hoge"); - tree1.setBufferSize(10); - JungleTreeEditor editor = tree1.getJungleTreeEditor(); - NetworkNodePath path = new NetworkNodePath(); - Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, pos); - NodePath childPath = path.add(pos); - Assert.assertFalse(either.isA()); - editor = either.b(); - either = editor.putAttribute(childPath, key, value); - JungleTreeEditor editor2 = either.b(); - either = editor2.success(); - Assert.assertFalse(either.isA()); - - PersistentJournal journal2 = new PersistentJournal(); - journal2.setInputFile(new File( journal1.getLogName())); - journal2.setOutputFile(new File( journal1.getLogName())); - ChangeListReader reader = journal2.getReader(); - Iterator<ChangeList> iterator = reader.iterator(); - Assert.assertFalse(iterator.hasNext()); - - for (int count = 0; count < 9; count++) { - editor = tree1.getJungleTreeEditor(); - either = editor.addNewChildAt(path, pos); - childPath = path.add(pos); - Assert.assertFalse(either.isA()); - editor = either.b(); - either = editor.putAttribute(childPath, key, value); - editor2 = either.b(); - either = editor2.success(); - Assert.assertFalse(either.isA()); - } - editor = tree1.getJungleTreeEditor(); - either = editor.flushSuccess(); - Assert.assertFalse(either.isA()); - - PersistentJournal journal3 = new PersistentJournal(); - journal3.setInputFile(new File( journal1.getLogName())); - journal3.setOutputFile(new File( journal1.getLogName())); - Jungle jungle2 = new PersistentJungle(journal1, "uuid3",new DefaultTraverser()); - ChangeListReader reader2 = journal3.getReader(); - for (ChangeList chList : reader2) { - String treeName = chList.getTreeName(); - JungleTree tree2 = jungle2.getTreeByName(treeName); - if (tree2 == null) { - tree2 = jungle2.createNewTree(treeName); - } - editor2 = tree2.getJungleTreeEditor(); - Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList); - Assert.assertFalse(either2.isA()); - editor2 = either2.b(); - editor2.success(); - } - - JungleTree tree2 = jungle2.getTreeByName("hoge"); - TreeNode node1 = tree1.getRootNode(); - TreeNode node2 = tree2.getRootNode(); - Children child1 = node1.getChildren(); - Children child2 = node2.getChildren(); - Assert.assertEquals(child1.size(), child2.size()); - Either<Error, TreeNode> either1 = child1.at(pos); - Either<Error, TreeNode> either2 = child1.at(pos); - Assert.assertFalse(either1.isA()); - Assert.assertFalse(either2.isA()); - - TreeNode nodeA = either1.b(); - TreeNode nodeB = either2.b(); - ByteBuffer bb1 = nodeA.getAttributes().get(key); - ByteBuffer bb2 = nodeB.getAttributes().get(key); - String strA = new String(bb1.array()); - String strB = new String(bb2.array()); - Assert.assertTrue(strA.equals(strB)); - journal1.close(); - journal2.close(); - } -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/HashSetConvertTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,40 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork; - -import alice.codesegment.CodeSegment; -import alice.datasegment.CommandType; -import alice.datasegment.Receiver; -import org.msgpack.annotation.Message; - -import java.util.HashSet; - -public class HashSetConvertTest extends CodeSegment { - - Receiver hash = ids.create(CommandType.TAKE); - - public HashSetConvertTest() { - hash.setKey("hash"); - } - - public static void main(String[] args) { - HashSetDataSegment h = new HashSetDataSegment(); - h.hash.add("test1"); - h.hash.add("test2"); - - HashSetConvertTest cs = new HashSetConvertTest(); - cs.ods.put("hash", h); - } - - public void run() { - HashSetDataSegment h = hash.asClass(HashSetDataSegment.class); - for(String s : h.hash ) { - System.out.println("s : "+s); - } - System.exit(0); - } - - @Message - private static class HashSetDataSegment { - public HashSet<String> hash = new HashSet<String>(); - public HashSetDataSegment() {} - } -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/Persistent/PersistentDefaultJournalTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,84 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.Persistent; - -import jp.ac.u_ryukyu.ie.cr.jungle.Jungle; -import jp.ac.u_ryukyu.ie.cr.jungle.core.Children; -import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; -import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListReader; -import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath; -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.traverser.DefaultTraverser; -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.operations.NetworkNodePath; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJournal; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJungle; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction.JungleUpdater; -import junit.framework.TestCase; - -import java.io.File; -import java.io.IOException; -import java.nio.ByteBuffer; - -public class PersistentDefaultJournalTest extends TestCase { - - public void testPersistentJounal() throws IOException { - PersistentJournal journal1 = new PersistentJournal("commit.log"); - Jungle jungle = new PersistentJungle(journal1, "uuid", new DefaultTraverser()); - jungle.createNewTree("hoge"); - JungleTree tree1 = jungle.getTreeByName("hoge"); - JungleTreeEditor editor = tree1.getJungleTreeEditor(); - NetworkNodePath path = new NetworkNodePath(); - String key = "key"; - ByteBuffer value = ByteBuffer.wrap("value".getBytes()); - int pos = 0; - Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, pos); - NodePath childPath = path.add(pos); - assertFalse(either.isA()); - editor = either.b(); - either = editor.putAttribute(childPath, key, value); - JungleTreeEditor e = either.b(); - either = e.success(); - assertFalse(either.isA()); - - PersistentJournal journal2 = new PersistentJournal(); - journal2.setInputFile(new File( journal1.getLogName())); - journal2.setOutputFile(new File(journal1.getLogName())); - Jungle jungle2 = new PersistentJungle(journal1, "uuid2", new DefaultTraverser()); - ChangeListReader reader = journal2.getReader(); - for (ChangeList chList : reader) { - String treeName = chList.getTreeName(); - JungleTree tree2 = jungle2.getTreeByName(treeName); - int treeType = chList.getTreeType(); - if (tree2 == null) { - tree2 = jungle2.createNewTree(treeName); - } - JungleTreeEditor editor2 = tree2.getJungleTreeEditor(); - Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList, treeType); - Either<Error, JungleTreeEditor> either3 = either2.b().success(); - assertFalse(either3.isA()); - } - JungleTree tree2 = jungle2.getTreeByName("hoge"); - TreeNode node1 = tree1.getRootNode(); - TreeNode node2 = tree2.getRootNode(); - Children child1 = node1.getChildren(); - Children child2 = node2.getChildren(); - assertEquals(child1.size(), child2.size()); - Either<Error, TreeNode> either1 = child1.at(pos); - Either<Error, TreeNode> either2 = child1.at(pos); - assertFalse(either1.isA()); - assertFalse(either2.isA()); - - TreeNode nodeA = either1.b(); - TreeNode nodeB = either2.b(); - ByteBuffer bb1 = nodeA.getAttributes().get(key); - ByteBuffer bb2 = nodeB.getAttributes().get(key); - String strA = new String(bb1.array()); - String strB = new String(bb2.array()); - assertTrue(strA.equals(strB)); - journal1.close(); - journal2.close(); - } - -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/Persistent/PersistentDifferentialJournalTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,111 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.Persistent; - -import jp.ac.u_ryukyu.ie.cr.jungle.Jungle; -import jp.ac.u_ryukyu.ie.cr.jungle.core.Attributes; -import jp.ac.u_ryukyu.ie.cr.jungle.core.Children; -import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; -import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListReader; -import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.InterfaceTraverser; -import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath; -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.traverser.DefaultTraverser; -import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree; -import jp.ac.u_ryukyu.ie.cr.jungle.tree.TreeType; -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.operations.NetworkNodePath; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJournal; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJungle; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction.JungleUpdater; -import junit.framework.TestCase; -import org.junit.Assert; - -import java.io.File; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.Iterator; - - -public class PersistentDifferentialJournalTest extends TestCase { - - public void testPersistentJounal() throws IOException { - PersistentJournal journal1 = new PersistentJournal("commit.log"); - Jungle jungle = new PersistentJungle(journal1, "uuid", new DefaultTraverser()); - jungle.createNewDifferenceTree("hoge"); - JungleTree tree1 = jungle.getTreeByName("hoge"); - - for (int i = 0 ; i < 3 ; i++) { - JungleTreeEditor editor = tree1.getJungleTreeEditor(); - NetworkNodePath path = new NetworkNodePath(); - String key = "key"; - ByteBuffer value = ByteBuffer.wrap("value".getBytes()); - int pos = 0; - Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, pos); - NodePath childPath = path.add(pos); - assertFalse(either.isA()); - editor = either.b(); - either = editor.putAttribute(childPath, key, value); - JungleTreeEditor e = either.b(); - either = e.success(); - assertFalse(either.isA()); - } - PersistentJournal journal2 = new PersistentJournal(); - journal2.setInputFile(new File(journal1.getLogName())); - journal2.setOutputFile(new File(journal1.getLogName())); - Jungle jungle2 = new PersistentJungle(journal1, "uuid2", new DefaultTraverser()); - ChangeListReader reader = journal2.getReader(); - for (ChangeList chList : reader) { - String treeName = chList.getTreeName(); - JungleTree tree2 = jungle2.getTreeByName(treeName); - int treeType = chList.getTreeType(); - if (tree2 == null) { - Assert.assertEquals(treeType, TreeType.DIFFERENCE); - tree2 = jungle2.createNewDifferenceTree(treeName); - } - JungleTreeEditor editor2 = tree2.getJungleTreeEditor(); - Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList); - Either<Error, JungleTreeEditor> either3 = either2.b().success(); - assertFalse(either3.isA()); - } - JungleTree tree2 = jungle2.getTreeByName("hoge"); - - - InterfaceTraverser traverser1 = tree1.getTraverser(true); - Iterator<TreeNode> allNodeIterator1 = traverser1.find((TreeNode n) -> { - return true; - }); - - InterfaceTraverser traverser2 = tree2.getTraverser(true); - Iterator<TreeNode> allNodeIterator2 = traverser2.find((TreeNode n) -> { - return true; - }); - - while (allNodeIterator1.hasNext() || allNodeIterator2.hasNext()) { - TreeNode node1 = allNodeIterator1.next(); - TreeNode node2 = allNodeIterator2.next(); - Children children1 = node1.getChildren(); - Children children2 = node2.getChildren(); - int childrenCount1 = children1.size(); - int childrenCount2 = children2.size(); - Assert.assertEquals(childrenCount1,childrenCount2); - Attributes attribute1 = node1.getAttributes(); - Attributes attribute2 = node2.getAttributes(); - Iterator<String> keys1 = attribute1.getKeys(); - Iterator<String> keys2 = attribute2.getKeys(); - while (keys1.hasNext() || keys2.hasNext()) { - String key1 = keys1.next(); - String key2 = keys2.next(); - Assert.assertEquals(key1,key2); - ByteBuffer value1 = attribute1.get(key1); - ByteBuffer value2 = attribute2.get(key2); - Assert.assertEquals(value1,value2); - } - - } - journal1.close(); - journal2.close(); - } - -} -
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkAppendChildOperationTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.core.operations; - - -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkAppendChildAtOperation; -import junit.framework.TestCase; -import org.msgpack.MessagePack; -import org.msgpack.type.Value; - -import java.io.IOException; - -public class NetworkAppendChildOperationTest extends TestCase { - - public void testMsgpackConvert() throws IOException { - NetworkAppendChildAtOperation op = new NetworkAppendChildAtOperation(1); - MessagePack msgpack = new MessagePack(); - Value v = msgpack.unconvert(op); - NetworkAppendChildAtOperation mOp = msgpack.convert(v, NetworkAppendChildAtOperation.class); -// NodeOperation mOp = msgpack.convert(v, NodeOperation.class); - assertEquals(op.getCommand(), mOp.getCommand()); - assertEquals(op.getPosition(), 1); - assertEquals(op.getPosition(), mOp.getPosition()); - } -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkDeleteAttributeOprationTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.core.operations; - - -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkDeleteAttributeOperation; -import junit.framework.TestCase; -import org.msgpack.MessagePack; -import org.msgpack.type.Value; - -import java.io.IOException; - -public class NetworkDeleteAttributeOprationTest extends TestCase { - - public void testMsgpackConvert() throws IOException { - NetworkDeleteAttributeOperation op = new NetworkDeleteAttributeOperation("hoge"); - MessagePack msgpack = new MessagePack(); - Value v = msgpack.unconvert(op); - NetworkDeleteAttributeOperation mOp = msgpack.convert(v, NetworkDeleteAttributeOperation.class); - assertEquals(op.getCommand(), mOp.getCommand()); - assert(mOp.getKey().equals("hoge")); - - } - - -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkDeleteChildAtOperationTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.core.operations; - - -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkDeleteChildAtOperation; -import junit.framework.TestCase; -import org.msgpack.MessagePack; -import org.msgpack.type.Value; - -import java.io.IOException; - -public class NetworkDeleteChildAtOperationTest extends TestCase { - - - public void testMsgpackConvert() throws IOException { - NetworkDeleteChildAtOperation op = new NetworkDeleteChildAtOperation(1); - MessagePack msgpack = new MessagePack(); - Value v = msgpack.unconvert(op); - NetworkDeleteChildAtOperation mOp = msgpack.convert(v, NetworkDeleteChildAtOperation.class); - assertEquals(op.getCommand(), mOp.getCommand()); - assertEquals(op.getPosition(), mOp.getPosition()); - - } - -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkNodeOperationTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.core.operations; - - -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkAppendChildAtOperation; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodeOperation; -import junit.framework.TestCase; -import org.msgpack.MessagePack; -import org.msgpack.type.Value; - -import java.io.IOException; - -public class NetworkNodeOperationTest extends TestCase { - - public void testMsgpackConvert() throws IOException { - NetworkAppendChildAtOperation op = new NetworkAppendChildAtOperation(1); - NetworkNodeOperation nOp = new NetworkNodeOperation(op); - MessagePack msgpack = new MessagePack(); - Value v = msgpack.unconvert(nOp); - NetworkNodeOperation mOp = msgpack.convert(v, NetworkNodeOperation.class); - assertEquals(op.getCommand(), mOp.getCommand()); - assertEquals(op.getPosition(), 1); - assertEquals(op.getPosition(), mOp.getPosition()); - } - -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkPutAttributeOperationTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.core.operations; - - -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkPutAttributeOperation; -import junit.framework.TestCase; -import org.msgpack.MessagePack; -import org.msgpack.type.Value; - -import java.io.IOException; -import java.nio.ByteBuffer; - -public class NetworkPutAttributeOperationTest extends TestCase { - - public void testMsgpackConvert() throws IOException { - ByteBuffer value = ByteBuffer.allocate(16); - value.put("fuga".getBytes()); - NetworkPutAttributeOperation op = new NetworkPutAttributeOperation("hoge", value); - MessagePack msgpack = new MessagePack(); - Value v = msgpack.unconvert(op); - NetworkPutAttributeOperation mOp = msgpack.convert(v, NetworkPutAttributeOperation.class); - assertEquals(op.getCommand(), mOp.getCommand()); - //assert((new String(op.getValue().array())).equals(new String(mOp.getValue().array()))); よくわからん byteBufferのunconvertがそもそも出来ない - } - -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkTreeOperationLogTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.core.operations; - - -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkAppendChildAtOperation; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodePath; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkTreeOperationLog; -import junit.framework.TestCase; -import org.msgpack.MessagePack; -import org.msgpack.type.Value; - -import java.io.IOException; - -public class NetworkTreeOperationLogTest extends TestCase { - - public void testMsgpackConvert() throws IOException { - NetworkAppendChildAtOperation op = new NetworkAppendChildAtOperation(1); - NetworkNodePath path = new NetworkNodePath(); - path.add(1).add(2); - NetworkTreeOperationLog log = new NetworkTreeOperationLog(); - log.add(path, op); - log.add(path, op); - MessagePack msgpack = new MessagePack(); - Value v = msgpack.unconvert(log); - NetworkTreeOperationLog mLog = msgpack.convert(v, NetworkTreeOperationLog.class); - assertNotNull(mLog); - - } - -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkTreeOperationTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,27 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.core.operations; - - -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkAppendChildAtOperation; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodePath; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkTreeOperation; -import junit.framework.TestCase; -import org.msgpack.MessagePack; -import org.msgpack.type.Value; - -import java.io.IOException; - -public class NetworkTreeOperationTest extends TestCase { - - public void testMsgpackConvert() throws IOException { - NetworkAppendChildAtOperation op = new NetworkAppendChildAtOperation(1); - NetworkNodePath path = new NetworkNodePath(); - path.add(1).add(2); - NetworkTreeOperation treeOp = new NetworkTreeOperation(path, op); - MessagePack msgpack = new MessagePack(); - Value v = msgpack.unconvert(treeOp); - NetworkTreeOperation mOp = msgpack.convert(v, NetworkTreeOperation.class); - assertNotNull(mOp.getNodePath()); - assertEquals(op.getCommand(), mOp.getNodeOperation().getCommand()); - } - -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/datasegment/NetworkNodePathTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.datasegment; - - -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodePath; -import junit.framework.TestCase; -import org.msgpack.MessagePack; -import org.msgpack.type.Value; - -import java.io.IOException; -import java.util.Iterator; - -public class NetworkNodePathTest extends TestCase { - - public void testMsgpackConvert() throws IOException { - NetworkNodePath path = new NetworkNodePath(); - path = path.add(1).add(2).add(3); - MessagePack msgpack = new MessagePack(); - Value value = msgpack.unconvert(path); - NetworkNodePath mPath = msgpack.convert(value, NetworkNodePath.class); - assertEquals(path.size(), mPath.size()); - Iterator<Integer> iter1 = path.iterator(); - Iterator<Integer> iter2 = mPath.iterator(); - while(iter1.hasNext() & iter2.hasNext()) { - int i1 = iter1.next(); - int i2 = iter2.next(); - assertEquals(i1, i2); - } - } - - - -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/LogupdateTest.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,22 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.logupdate; - - -import alice.topology.node.TopologyNode; -import alice.topology.node.TopologyNodeConfig; - -/** - * Created by kono on 2017/08/28. - */ -public class LogupdateTest { - public static void main(String[] args) { - LogupdateTestConfig conf = new LogupdateTestConfig(args); - LogupdateTestStartCodeSegment cs = new LogupdateTestStartCodeSegment(); - cs.ods.put("config", conf); - if (conf.getManagerHostName() !=null){ - new TopologyNode(conf, cs); - } else { - cs.ods.put("host", "local0"); - cs.execute(); - } - } -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/LogupdateTestConfig.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.logupdate; - -import alice.daemon.Config; -import alice.topology.node.TopologyNode; -import alice.topology.node.TopologyNodeConfig; - -/** - * Created by kono on 2017/08/28. - */ -public class LogupdateTestConfig extends TopologyNodeConfig { - - public String hostname = "127.0.0.1"; - public int connectPort = 10000; - public String key = "remote"; - private boolean writeMode = false; - private String[] args1; - - public LogupdateTestConfig(String[] args) { - super(args); - args1 = 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]; - } else if ("-write".equals(args[i])) { - writeMode = true; - } - } - } - - public boolean isWriteMode() { - return writeMode; - } - - public String[] getArgs() { - return args1; - } -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/LogupdateTestStartCodeSegment.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,58 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.junglenetwork.logupdate; - -import alice.codesegment.CodeSegment; -import alice.datasegment.CommandType; -import alice.datasegment.Receiver; -import alice.topology.node.TopologyNodeConfig; -import jp.ac.u_ryukyu.ie.cr.jungle.DefaultJungle; -import jp.ac.u_ryukyu.ie.cr.jungle.bbs.BBSTest; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.codesegment.LogUpdateCodeSegment; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.core.NetworkDefaultJungle; -import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.AliceJournal; - -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); - Receiver conf = ids.create(CommandType.PEEK); - private Pattern pattern = Pattern.compile("^(node|cli)([0-9]+)$"); - private String[] args; - boolean persistentFlag = false; - - public LogupdateTestStartCodeSegment(){ - conf.setKey("config"); - host.setKey("host"); - } - - @Override - public void run() { - String name = host.asString(); - LogupdateTestConfig config = conf.asClass(LogupdateTestConfig.class); - args = config.getArgs(); - - Matcher matcher = pattern.matcher(name); - matcher.find(); - // String type = matcher.group(1); - for(String arg: args) - if (arg.equals("-persistent")) { - persistentFlag = true; - } - BBSTest bbsTest = new BBSTest(new NetworkDefaultJungle(new AliceJournal(), "hoge")); - Logupdator cassaBBS = new Logupdator(name, bbsTest); - cassaBBS.init(); - ods.put("updator", cassaBBS); - - new LogUpdateCodeSegment(); - if (config.isWriteMode()){ - bbsTest.createBoard(); - bbsTest.createMessage(); - bbsTest.checkMessage(); - } - } -}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/logupdate/Logupdator.java Mon Sep 25 18:08:22 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,95 +0,0 @@ -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.bbs.BBSTest; -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 BBSTest bbsTest; - private AtomicInteger requestCounter = new AtomicInteger(0); - - public Logupdator(String name, BBSTest bbsTest) { - this.bbsTest = bbsTest; - } - - 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.getLocalJungleTreeEditor(); - /* - * 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(); - bbsTest.checkMessage(); - 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 bbsTest.getJungle(); - } -}