Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 317:d6b81870216b
Persisitent Differential Tree implement
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/index/CreateIndexBenchMark.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/index/CreateIndexBenchMark.java Wed Feb 01 04:04:17 2017 +0900 @@ -5,6 +5,7 @@ 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.store.nodepath.NodePath; +import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.RedBlackTreeNodePath; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree; @@ -19,26 +20,26 @@ } if (args[0].equals("default")) { System.out.println("create default tree"); - } - else { + } else { System.out.println("create red black tree"); } String key = "key"; String indexKey = "indexKey"; - Jungle jungle = new DefaultJungle(null, "hogehoge"); - for (int i = 100; i <= 100000;) { + for (int i = 0; i <= 3000; ) { + Jungle jungle = new DefaultJungle(null, "hogehoge"); Long t1 = System.currentTimeMillis(); JungleTree tree; + NodePath path; if (args[0].equals("default")) { tree = jungle.createNewTree("Tree" + i); - } - else { + path = new DefaultNodePath(); + } else { tree = jungle.createNewRedBlackTree("Tree" + i, key); + path = new RedBlackTreeNodePath(); } - JungleTreeEditor editor = tree.getJungleTreeEditor(); - NodePath path = new DefaultNodePath(); + JungleTreeEditor editor = tree.getJungleTreeEditor(); int maxNodeCount = i; JungleTreeCreater creater = new JungleTreeCreater(maxNodeCount); editor = creater.createTree(editor, key, indexKey, path);
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/persistent/DataReadBenchMark.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/persistent/DataReadBenchMark.java Wed Feb 01 04:04:17 2017 +0900 @@ -1,13 +1,12 @@ package jp.ac.u_ryukyu.ie.cr.benchMark.persistent; 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.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.editor.jungleTreeEditor.JungleTreeEditor; 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.Error.Error; import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodePath; @@ -28,8 +27,8 @@ public static void main(String[] args) throws IOException { PersistentJournal journal1 = new PersistentJournal(new File("./log/commit.log")); - Jungle jungle = new PersistentJungle(journal1, "uuid", new DefaultTreeEditor(new DefaultTraverser())); - Jungle jungle2 = new PersistentJungle(journal1, "uuid3", new DefaultTreeEditor(new DefaultTraverser())); + Jungle jungle = new PersistentJungle(journal1, "uuid", new DefaultTraverser()); + Jungle jungle2 = new PersistentJungle(journal1, "uuid3", new DefaultTraverser()); Long t1; Long t2; PrintWriter readTimeWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File("./time/readTime")))); @@ -64,12 +63,13 @@ t1 = System.currentTimeMillis(); for (ChangeList chList : reader) { String treeName = chList.getTreeName(); + int treeType = chList.getTreeType(); JungleTree tree2 = jungle2.getTreeByName(treeName); if (tree2 == null) { tree2 = jungle2.createNewTree(treeName); } JungleTreeEditor editor2 = tree2.getJungleTreeEditor(); - Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList); + Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList,treeType); Assert.assertFalse(either2.isA()); editor2 = either2.b(); editor2.success();
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/persistent/DataWriteBenchMark.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/persistent/DataWriteBenchMark.java Wed Feb 01 04:04:17 2017 +0900 @@ -1,11 +1,10 @@ package jp.ac.u_ryukyu.ie.cr.benchMark.persistent; 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.store.nodepath.DefaultNodePath; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor; -import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.DefaultNodePath; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.treeEditor.DefaultTreeEditor; 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.jungleNetwork.persistent.PersistentJournal; import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJungle; @@ -17,7 +16,7 @@ public static void main(String[] args) throws FileNotFoundException { PersistentJournal journal1 = new PersistentJournal(new File("./log/CommitTest.log")); - Jungle jungle = new PersistentJungle(journal1, "uuid",new DefaultTreeEditor(new DefaultTraverser())); + Jungle jungle = new PersistentJungle(journal1, "uuid",new DefaultTraverser()); jungle.createNewTree("testTree"); JungleTree tree = jungle.getTreeByName("testTree"); JungleTreeEditor editor = tree.getJungleTreeEditor();
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/persistent/JungleLogCopyBenchMark.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/persistent/JungleLogCopyBenchMark.java Wed Feb 01 04:04:17 2017 +0900 @@ -1,11 +1,10 @@ package jp.ac.u_ryukyu.ie.cr.benchMark.persistent; 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.store.nodepath.NodePath; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor; -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.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.Error.Error; import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodePath; @@ -27,7 +26,7 @@ public static void main(String[] args) throws IOException { PersistentJournal journal1 = new PersistentJournal(new File("./log/commit.log")); - Jungle jungle = new PersistentJungle(journal1, "uuid", new DefaultTreeEditor(new DefaultTraverser())); + Jungle jungle = new PersistentJungle(journal1, "uuid", new DefaultTraverser()); Long t1; Long t2; PrintWriter readTimeWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File("./time/copyTime"))));
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/tree/CreateListTreeBenchMark.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/tree/CreateListTreeBenchMark.java Wed Feb 01 04:04:17 2017 +0900 @@ -14,30 +14,30 @@ public class CreateListTreeBenchMark { public static void main(String args[]) { - Jungle jungle = new DefaultJungle(null, "hogehoge"); if (args.length == 0) { System.out.println("args default or difference"); System.exit(0); } if (args[0].equals("default")) - defaultJungleTreeBenchMark(jungle); + defaultJungleTreeBenchMark(); else if (args[0].equals("difference")) - DifferenceJungleTreeBenchMark(jungle); + DifferenceJungleTreeBenchMark(); else { System.out.println("args default or difference"); System.exit(0); } } - private static void DifferenceJungleTreeBenchMark(Jungle jungle) { + private static void DifferenceJungleTreeBenchMark() { System.out.println("differencialTree"); - for (int i = 1; i <= 100; i++) { + for (int i = 1; i <= 150; i++) { + Jungle jungle = new DefaultJungle(null, "hogehoge"); JungleTree tree = jungle.createNewDifferenceTree("Tree" + i); Long t1 = System.currentTimeMillis(); NodePath path = new DefaultNodePath(); - for (int j = 0; j < (20 * i); j++) { + for (int j = 0; j < (1 * i); j++) { JungleTreeEditor editor = tree.getJungleTreeEditor(); Either<Error, JungleTreeEditor> either = editor.putAttribute(path, "key", ByteBuffer.wrap("value".getBytes())); if (either.isA()) @@ -47,19 +47,20 @@ if (either.isA()) return; } + Long t2 = System.currentTimeMillis(); + System.out.println((i * 1) + " " + (t2 - t1)); System.gc(); - Long t2 = System.currentTimeMillis(); - System.out.println((i * 20) + " " + (t2 - t1)); } } - private static void defaultJungleTreeBenchMark(Jungle jungle) { + private static void defaultJungleTreeBenchMark() { System.out.println("defaultTree"); - for (int i = 1; i <= 10; i++) { + for (int i = 1; i <= 150; i++) { + Jungle jungle = new DefaultJungle(null, "hogehoge"); JungleTree tree = jungle.createNewTree("Tree" + i); Long t1 = System.currentTimeMillis(); NodePath path = new DefaultNodePath(); - for (int j = 0; j < (200 * i); j++) { + for (int j = 0; j < (1 * i); j++) { JungleTreeEditor editor = tree.getJungleTreeEditor(); Either<Error, JungleTreeEditor> either = editor.addNewChildAndPutAttribute(path, 0,"key", ByteBuffer.wrap("value".getBytes())); if (either.isA()) @@ -72,7 +73,8 @@ } Long t2 = System.currentTimeMillis(); - System.out.println((i * 200) + " " + (t2 - t1)); + System.out.println((i * 1) + " " + (t2 - t1)); + System.gc(); } } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/DefaultJungle.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/DefaultJungle.java Wed Feb 01 04:04:17 2017 +0900 @@ -28,10 +28,7 @@ import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DefaultInterfaceTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.RedBlackTreeTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.Traverser; -import jp.ac.u_ryukyu.ie.cr.jungle.tree.DefaultJungleTree; -import jp.ac.u_ryukyu.ie.cr.jungle.tree.DifferenceListJungleTree; -import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree; -import jp.ac.u_ryukyu.ie.cr.jungle.tree.RedBlackJungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.*; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; @@ -104,6 +101,11 @@ } @Override + public int getTreeType() { + return TreeType.DEFAULT; + } + + @Override public TreeOperationLog getLog() { return new DefaultTreeOperationLog(); } @@ -144,6 +146,11 @@ } @Override + public int getTreeType() { + return TreeType.DIFFERENCE; + } + + @Override public TreeOperationLog getLog() { return new DefaultTreeOperationLog(); } @@ -178,6 +185,11 @@ } @Override + public int getTreeType() { + return TreeType.REDBLACK; + } + + @Override public TreeOperationLog getLog() { return new DefaultTreeOperationLog(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/treemap/TreeMap.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/data/treemap/TreeMap.java Wed Feb 01 04:04:17 2017 +0900 @@ -10,8 +10,8 @@ public class TreeMap<K, V> { - final Node<K, V> root; - final Comparator comparator; + private final Node<K, V> root; + private final Comparator comparator; public TreeMap() { this.root = new EmptyNode<>();
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/persistent/ChangeList.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/persistent/ChangeList.java Wed Feb 01 04:04:17 2017 +0900 @@ -7,5 +7,6 @@ { public String uuid(); public String getTreeName(); + public int getTreeType(); public TreeOperationLog getLog(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/Command.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/Command.java Wed Feb 01 04:04:17 2017 +0900 @@ -2,7 +2,6 @@ public enum Command { - COMMIT, APPEND_CHILD, DELETE_CHILD, PUT_ATTRIBUTE,
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/index/IndexCreater.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/index/IndexCreater.java Wed Feb 01 04:04:17 2017 +0900 @@ -25,14 +25,14 @@ String key = keys.next(); String value = targetNode.getAttributes().getString(key); if (value != null) - index.set(key, value, targetNode); + index = index.set(key, value, targetNode); } if (node.getChildren().size() > 0) { nodeStack.push(node); TreeNode parent = node; children = node.getChildren(); node = children.at(0).b(); - parentIndex.set(parent, node); + parentIndex = parentIndex.set(parent, node); childNumber = 1; searchStack.push(childNumber); } else if (node == rootNode) { @@ -44,7 +44,7 @@ TreeNode parent = nodeStack.pop(); nodeStack.push(parent); node = children.at(childNumber).b(); - parentIndex.set(parent, node); + parentIndex = parentIndex.set(parent, node); searchStack.push(++childNumber); } else { node = nodeStack.pop(); @@ -64,7 +64,7 @@ nodeStack.push(node); TreeNode parent = node; node = children.at(childNumber).b(); - parentIndex.set(parent, node); + parentIndex = parentIndex.set(parent, node); searchStack.push(++childNumber); } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/nodepath/RedBlackTreeNodePath.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/nodepath/RedBlackTreeNodePath.java Wed Feb 01 04:04:17 2017 +0900 @@ -64,7 +64,7 @@ @Override public NodePath add(int pos) { - return null; + return this; } @Override
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/operations/AppendChildAtOperation.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/operations/AppendChildAtOperation.java Wed Feb 01 04:04:17 2017 +0900 @@ -1,12 +1,12 @@ package jp.ac.u_ryukyu.ie.cr.jungle.store.operations; +import jp.ac.u_ryukyu.ie.cr.jungle.store.Command; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; + import java.nio.ByteBuffer; -import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; -import jp.ac.u_ryukyu.ie.cr.jungle.store.Command; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; -import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; - public final class AppendChildAtOperation implements NodeOperation {
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/operations/Commit.java Fri Jan 27 01:09:43 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.jungle.store.operations; - -import jp.ac.u_ryukyu.ie.cr.jungle.store.Command; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; -import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; -import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; -import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; - -import java.nio.ByteBuffer; - -import static jp.ac.u_ryukyu.ie.cr.jungle.util.Error.TreeEditorError.NOT_USE_METHOD; - -/** - * Created by e115731 on 2016/12/16. - */ -public class Commit implements NodeOperation { - - @Override - public Command getCommand() { - return Command.COMMIT; - } - - @Override - public Either<Error, TreeNode> invoke(TreeNode _target) { - return DefaultEither.newA(NOT_USE_METHOD); - } - - @Override - public int getPosition() { - return 0; - } - - @Override - public String getKey() { - return null; - } - - @Override - public ByteBuffer getValue() { - return null; - } -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/manager/DefaultTransactionManager.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/manager/DefaultTransactionManager.java Wed Feb 01 04:04:17 2017 +0900 @@ -13,6 +13,7 @@ import jp.ac.u_ryukyu.ie.cr.jungle.transaction.context.DefaultTreeContext; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DefaultInterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.TreeType; import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.DefaultError; @@ -53,6 +54,11 @@ } @Override + public int getTreeType() { + return TreeType.DEFAULT; + } + + @Override public TreeOperationLog getLog() { return _log; } @@ -67,8 +73,8 @@ Index index = tip.getIndex(); ParentIndex parentIndex = tip.getParentIndex(); InterfaceTraverser traverser = new DefaultInterfaceTraverser(newRoot, index, parentIndex, true); - traverser.updateIndex(editNodeList); - //traverser.createIndex(); + //traverser.updateIndex(editNodeList); + traverser.createIndex(); TreeContext newTreeContext = new DefaultTreeContext(newRoot, tip, list, uuid, _treeName, nextRevision, traverser); if (repository.compareAndSet(newTreeContext.prev(), newTreeContext)) {
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/manager/DifferenceTransactionManager.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/manager/DifferenceTransactionManager.java Wed Feb 01 04:04:17 2017 +0900 @@ -9,13 +9,11 @@ import jp.ac.u_ryukyu.ie.cr.jungle.store.index.Index; import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.TreeOperationLog; -import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.DefaultNodePath; -import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.Commit; -import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.NodeOperation; import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.context.DifferenceTreeContext; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNodeChildren; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.TreeType; import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; @@ -54,14 +52,12 @@ public Either<Error, TransactionManager> commit(TreeNode subTreeRoot, TreeOperationLog log,List<TreeNode> editNodeList) { long currentRevision = tip.revision(); long nextRevision = currentRevision + 1; - NodeOperation commitOperation = new Commit(); - TreeOperationLog newLog = log.add(new DefaultNodePath(), commitOperation); final String _treeName = tip.getTreeName(); ChangeList list = new ChangeList() { @Override public Iterator<TreeOperation> iterator() { - return newLog.iterator(); + return log.iterator(); } @Override @@ -70,8 +66,13 @@ } @Override + public int getTreeType() { + return TreeType.DIFFERENCE; + } + + @Override public TreeOperationLog getLog() { - return newLog; + return log; } @Override
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/manager/RedBlackTreeTransactionManager.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/manager/RedBlackTreeTransactionManager.java Wed Feb 01 04:04:17 2017 +0900 @@ -10,6 +10,7 @@ import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.context.DefaultTreeContext; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.TreeType; import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.DefaultError; @@ -50,6 +51,11 @@ } @Override + public int getTreeType() { + return TreeType.REDBLACK; + } + + @Override public TreeOperationLog getLog() { return _log; }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/tree/DefaultJungleTree.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/tree/DefaultJungleTree.java Wed Feb 01 04:04:17 2017 +0900 @@ -23,10 +23,10 @@ public class DefaultJungleTree implements JungleTree { - protected final AtomicReference<TreeContext> repository; - protected final String uuid; - protected final ChangeListWriter writer; - protected final TreeEditor treeEditor; + private final AtomicReference<TreeContext> repository; + private final String uuid; + private final ChangeListWriter writer; + private final TreeEditor treeEditor; public DefaultJungleTree(TreeContext tc, String uuid, ChangeListWriter writer, TreeEditor editor) { this.repository = new AtomicReference<TreeContext>(tc); @@ -43,6 +43,23 @@ return new DefaultJungleTreeEditor(root, txManager, treeEditor); } + protected AtomicReference<TreeContext> getRepository(){ + return repository; + } + + protected ChangeListWriter getWriter(){ + return writer; + } + + protected String getUuid(){ + return uuid; + } + + protected TreeEditor getTreeEditor(){ + return treeEditor; + } + + @Override public JungleTreeEditor getLocalJungleTreeEditor() { return getJungleTreeEditor(); @@ -75,22 +92,6 @@ return path.addHead(-1); } - AtomicReference<TreeContext> getRepository(){ - return repository; - } - - ChangeListWriter getWriter(){ - return writer; - } - - String getUuid(){ - return uuid; - } - - TreeEditor getTreeEditor(){ - return treeEditor; - } - @Override public InterfaceTraverser getTraverser(boolean useIndex) { TreeContext tc = repository.get();
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/tree/DifferenceListJungleTree.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/tree/DifferenceListJungleTree.java Wed Feb 01 04:04:17 2017 +0900 @@ -36,6 +36,7 @@ @Override public Either<Error, JungleTree> getOldTree(long revision) { + AtomicReference<TreeContext> repository = super.getRepository(); TreeContext tc = repository.get(); for (; tc.revision() != revision; ) { @@ -43,14 +44,16 @@ if (tc == null) return DefaultEither.newA(GetOldTreeError.OLD_TREE_NOT_FOUND); } - + String uuid = super.getUuid(); String oldTreeUuid = uuid + revision; + ChangeListWriter writer = super.getWriter(); + TreeEditor treeEditor = super.getTreeEditor(); JungleTree oldTree = new DifferenceListJungleTree(tc, oldTreeUuid, writer, treeEditor); return DefaultEither.newB(oldTree); } //test用 method キャストして使ってね - public TreeNode getEndNode(){ + public TreeNode getEndNode() { AtomicReference<TreeContext> repository = super.getRepository(); TreeContext tc = repository.get(); return tc.getEndNode();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/tree/TreeType.java Wed Feb 01 04:04:17 2017 +0900 @@ -0,0 +1,11 @@ +package jp.ac.u_ryukyu.ie.cr.jungle.tree; + +/** + * Created by e115731 on 2017/01/31. + */ +public class TreeType { + public static int DEFAULT = 1; + public static int DIFFERENCE = 2; + public static int REDBLACK = 3; + public static int UNKNOWN = 4; +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/NetworkDefaultJungle.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/NetworkDefaultJungle.java Wed Feb 01 04:04:17 2017 +0900 @@ -16,6 +16,7 @@ import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DefaultInterfaceTraverser; 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.jungleNetwork.transaction.NetworkDefaultJungleTree; import java.util.Enumeration; @@ -65,6 +66,11 @@ } @Override + public int getTreeType() { + return TreeType.DEFAULT; + } + + @Override public TreeOperationLog getLog() { return new DefaultTreeOperationLog(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/operations/NetworkNodeOperation.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/operations/NetworkNodeOperation.java Wed Feb 01 04:04:17 2017 +0900 @@ -13,93 +13,92 @@ @Message public class NetworkNodeOperation implements NodeOperation { - public int pos; - public String key; - public ByteBuffer value; - public int commandType; + public int pos; + public String key; + public ByteBuffer value; + public int commandType; + + private final static int NUM_PUT_ATTRIBUTE = 1; + private final static int NUM_APPEND_CHILD = 2; + private final static int NUM_DELETE_CHILD = 3; + private final static int NUM_DELETE_ATTRIBUTE = 4; + + public NetworkNodeOperation() { + pos = -2; + key = null; + value = null; + commandType = 0; + } + + public NetworkNodeOperation(NodeOperation _op) { + pos = _op.getPosition(); + key = _op.getKey(); + value = _op.getValue(); + commandType = getCommandType(_op.getCommand()); + } + + private static int getCommandType(Command c) { + switch (c) { + case PUT_ATTRIBUTE: + return NUM_PUT_ATTRIBUTE; + case APPEND_CHILD: + return NUM_APPEND_CHILD; + case DELETE_CHILD: + return NUM_DELETE_CHILD; + case DELETE_ATTRIBUTE: + return NUM_DELETE_ATTRIBUTE; + default: + break; + } return 0; + } - public final static int NUM_PUT_ATTRIBUTE = 1; - public final static int NUM_APPEND_CHILD = 2; - public final static int NUM_DELETE_CHILD = 3; - public final static int NUM_DELETE_ATTRIBUTE = 4; - - public NetworkNodeOperation() { - pos = -2; - key = null; - value = null; - commandType = 0; - } - - public NetworkNodeOperation(NodeOperation _op) { - pos = _op.getPosition(); - key = _op.getKey(); - value = _op.getValue(); - commandType = getCommandType(_op.getCommand()); - } - - public static int getCommandType(Command c) { - switch(c) { - case PUT_ATTRIBUTE: - return NUM_PUT_ATTRIBUTE; - case APPEND_CHILD: - return NUM_APPEND_CHILD; - case DELETE_CHILD: - return NUM_DELETE_CHILD; - case DELETE_ATTRIBUTE: - return NUM_DELETE_ATTRIBUTE; - default: - break; - } - return 0; - } - - public static Command getCommand(int num) { - switch(num) { - case NUM_PUT_ATTRIBUTE: - return Command.PUT_ATTRIBUTE; - case NUM_APPEND_CHILD: - return Command.APPEND_CHILD; - case NUM_DELETE_CHILD: - return Command.DELETE_CHILD; - case NUM_DELETE_ATTRIBUTE: - return Command.DELETE_ATTRIBUTE; - default: - break; - } - return null; - } - - public Command getCommand() { - return getCommand(commandType); - } + public static Command getCommand(int num) { + switch (num) { + case NUM_PUT_ATTRIBUTE: + return Command.PUT_ATTRIBUTE; + case NUM_APPEND_CHILD: + return Command.APPEND_CHILD; + case NUM_DELETE_CHILD: + return Command.DELETE_CHILD; + case NUM_DELETE_ATTRIBUTE: + return Command.DELETE_ATTRIBUTE; + default: + break; + } + return null; + } + + public Command getCommand() { + return getCommand(commandType); + } + + public int getPosition() { + return pos; + } - public int getPosition() { - return pos; - } - - public String getKey() { - return key; - } - - public ByteBuffer getValue() { - return value; - } + public String getKey() { + return key; + } + + public ByteBuffer getValue() { + return value; + } - @Override - public Either<Error, TreeNode> invoke(TreeNode _target) { - switch(getCommand(commandType)) { - case PUT_ATTRIBUTE: - return _target.getAttributes().put(key, value); - case APPEND_CHILD: - return _target.getChildren().addNewChildAt(pos); - case DELETE_CHILD: - return _target.getChildren().deleteChildAt(pos); - case DELETE_ATTRIBUTE: - return _target.getAttributes().delete(key); - default: - break; - } - return null; - } + @Override + public Either<Error, TreeNode> invoke(TreeNode _target) { + switch (getCommand(commandType)) { + case PUT_ATTRIBUTE: + return _target.getAttributes().put(key, value); + case APPEND_CHILD: + return _target.getChildren().addNewChildAt(pos); + case DELETE_CHILD: + return _target.getChildren().deleteChildAt(pos); + case DELETE_ATTRIBUTE: + return _target.getAttributes().delete(key); + default: + break; + } + return null; + } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/operations/NetworkTreeOperation.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/operations/NetworkTreeOperation.java Wed Feb 01 04:04:17 2017 +0900 @@ -9,60 +9,60 @@ @Message public class NetworkTreeOperation implements TreeOperation { - public NetworkNodePath path; - public NetworkNodeOperation operation; - - public NetworkTreeOperation() { - path = null; - operation = null; - } - - public NetworkTreeOperation(TreeOperation treeOp) { - path = new NetworkNodePath(treeOp.getNodePath()); - operation = new NetworkNodeOperation(treeOp.getNodeOperation()); - } - - public NetworkTreeOperation(NodePath _p, NodeOperation _op) { - path = new NetworkNodePath(_p); - operation = new NetworkNodeOperation(_op); - } + public NetworkNodePath path; + public NetworkNodeOperation operation; + + public NetworkTreeOperation() { + path = null; + operation = null; + } + + public NetworkTreeOperation(TreeOperation treeOp) { + path = new NetworkNodePath(treeOp.getNodePath()); + operation = new NetworkNodeOperation(treeOp.getNodeOperation()); + } - public NetworkTreeOperation(NetworkNodePath _p, NodeOperation _op) { - path = _p; - operation = new NetworkNodeOperation(_op); - } + public NetworkTreeOperation(NodePath _p, NodeOperation _op) { + path = new NetworkNodePath(_p); + operation = new NetworkNodeOperation(_op); + } - public NetworkTreeOperation(NodePath _p, NetworkNodeOperation _op) { - path = new NetworkNodePath(_p); - operation = _op; - } + public NetworkTreeOperation(NetworkNodePath _p, NodeOperation _op) { + path = _p; + operation = new NetworkNodeOperation(_op); + } + + public NetworkTreeOperation(NodePath _p, NetworkNodeOperation _op) { + path = new NetworkNodePath(_p); + operation = _op; + } - public NetworkTreeOperation(NetworkNodePath _p, NetworkNodeOperation _op) { - path = _p; - operation = _op; - } - - @Override - public NodePath getNodePath() { - return path; - } - - @Override - public NodeOperation getNodeOperation() { - Command c = operation.getCommand(); - switch(c) { - case PUT_ATTRIBUTE: - return new PutAttributeOperation(operation.getKey(), operation.getValue()); - case APPEND_CHILD: - return new AppendChildAtOperation(operation.getPosition()); - case DELETE_CHILD: - return new DeleteChildAtOperation(operation.getPosition()); - case DELETE_ATTRIBUTE: - return new DeleteAttributeOperation(operation.getKey()); - default: - break; - } - return null; - } - + public NetworkTreeOperation(NetworkNodePath _p, NetworkNodeOperation _op) { + path = _p; + operation = _op; + } + + @Override + public NodePath getNodePath() { + return path; + } + + @Override + public NodeOperation getNodeOperation() { + Command c = operation.getCommand(); + switch (c) { + case PUT_ATTRIBUTE: + return new PutAttributeOperation(operation.getKey(), operation.getValue()); + case APPEND_CHILD: + return new AppendChildAtOperation(operation.getPosition()); + case DELETE_CHILD: + return new DeleteChildAtOperation(operation.getPosition()); + case DELETE_ATTRIBUTE: + return new DeleteAttributeOperation(operation.getKey()); + default: + break; + } + return null; + } + }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/operations/NetworkTreeOperationLog.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/operations/NetworkTreeOperationLog.java Wed Feb 01 04:04:17 2017 +0900 @@ -1,10 +1,11 @@ package jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations; +import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.TreeOperationLog; import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath; -import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.TreeOperationLog; import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.NodeOperation; import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.TreeType; import org.msgpack.annotation.Message; import java.util.Date; @@ -12,94 +13,94 @@ import java.util.LinkedList; @Message -public class NetworkTreeOperationLog implements TreeOperationLog -{ - public LinkedList<NetworkTreeOperation> list; - public int size; - String uuid; - String treeName; - long timestamp; - - public NetworkTreeOperationLog() { - list = new LinkedList<NetworkTreeOperation>(); - size = 0; - treeName = ""; - timestamp = new Date().getTime(); - } +public class NetworkTreeOperationLog implements TreeOperationLog { + public LinkedList<NetworkTreeOperation> list; + public int size; + String uuid; + String treeName; + long timestamp; + int type; + + public NetworkTreeOperationLog() { + list = new LinkedList<NetworkTreeOperation>(); + size = 0; + treeName = ""; + type = TreeType.DEFAULT; + timestamp = new Date().getTime(); + } - public NetworkTreeOperationLog(String _uuid, String _treeName, Iterable<TreeOperation> _list) - { - this(_uuid, _treeName, _list, new Date().getTime()); - } - - public NetworkTreeOperationLog(String _uuid, String _treeName, Iterable<TreeOperation> _list, long _timestamp) - { - uuid = _uuid; - treeName = _treeName; - list = new LinkedList<NetworkTreeOperation>(); - timestamp = _timestamp; - for(TreeOperation op: _list) { - NetworkTreeOperation nOp = new NetworkTreeOperation(op); - list.add(nOp); - } - size = list.size(); - } + public NetworkTreeOperationLog(String _uuid, String _treeName, int type, Iterable<TreeOperation> _list) { + this(_uuid, _treeName, type, _list, new Date().getTime()); + } - public NetworkTreeOperationLog(Iterable<TreeOperation> _list) - { - this("", "", _list); - } + public NetworkTreeOperationLog(String _uuid, String _treeName, int type, Iterable<TreeOperation> _list, long _timestamp) { + this.uuid = _uuid; + this.treeName = _treeName; + this.type = type; + this.list = new LinkedList<NetworkTreeOperation>(); + this.timestamp = _timestamp; + for (TreeOperation op : _list) { + NetworkTreeOperation nOp = new NetworkTreeOperation(op); + list.add(nOp); + } + this.size = list.size(); + } + + public NetworkTreeOperationLog(Iterable<TreeOperation> _list) { + this("", "",TreeType.UNKNOWN , _list); + } + - - @Override - public Iterator<TreeOperation> iterator() { - LinkedList<TreeOperation> opList = new LinkedList<TreeOperation>(); - for(NetworkTreeOperation op : list) { - opList.add(op); - } - return opList.iterator(); - } + @Override + public Iterator<TreeOperation> iterator() { + LinkedList<TreeOperation> opList = new LinkedList<TreeOperation>(); + for (NetworkTreeOperation op : list) { + opList.add(op); + } + return opList.iterator(); + } - @Override - public NetworkTreeOperationLog add(NodePath _p, NodeOperation _op) - { - NetworkTreeOperation op = new NetworkTreeOperation(_p, _op); - list.add(op); + @Override + public NetworkTreeOperationLog add(NodePath _p, NodeOperation _op) { + NetworkTreeOperation op = new NetworkTreeOperation(_p, _op); + list.add(op); size = list.size(); - return this; - } + return this; + } - @Override - public NetworkTreeOperationLog append(TreeOperationLog _log) - { - for (TreeOperation o : _log) { - NetworkTreeOperation op = new NetworkTreeOperation(o); - list.add(op); - } + @Override + public NetworkTreeOperationLog append(TreeOperationLog _log) { + for (TreeOperation o : _log) { + NetworkTreeOperation op = new NetworkTreeOperation(o); + list.add(op); + } size = list.size(); - return this; - } + return this; + } + + @Override + public int length() { + return list.size(); + } - @Override - public int length() - { - return list.size(); - } - - public String getUUID() { - return uuid; - } - - public String getTreeName() { - return treeName; - } - - public long getTimeStamp() { - return timestamp; - } - - public void setTimeStamp(long _timestamp) { - timestamp = _timestamp; - } + public String getUUID() { + return uuid; + } + + public String getTreeName() { + return treeName; + } + + public int getTreeType() { + return type; + } + + public long getTimeStamp() { + return timestamp; + } + + public void setTimeStamp(long _timestamp) { + timestamp = _timestamp; + } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentChangeList.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentChangeList.java Wed Feb 01 04:04:17 2017 +0900 @@ -11,48 +11,56 @@ public class PersistentChangeList implements ChangeList { - public NetworkTreeOperationLog log; - public String treeName; - public String uuid; + public NetworkTreeOperationLog log; + public int treeType = -1; + public String treeName; + public String uuid; + + public PersistentChangeList(String uuid, String treeName, int treeType, TreeOperationLog log) { + this.uuid = uuid; + this.treeName = treeName; + this.treeType = treeType; + this.log = new NetworkTreeOperationLog(log); + } + + public PersistentChangeList(NetworkTreeOperationLog _log) { + log = _log; + treeType = log.getTreeType(); + treeName = _log.getTreeName(); + uuid = _log.getUUID(); + } - public PersistentChangeList(String _uuid, String _treeName, TreeOperationLog _log) { - uuid = _uuid; - treeName = _treeName; - log = new NetworkTreeOperationLog(_log); - } - - public PersistentChangeList(NetworkTreeOperationLog _log) { - log = _log; - treeName = _log.getTreeName(); - uuid = _log.getUUID(); - } - - public PersistentChangeList(TreeContext cs) { - treeName = cs.getTreeName(); - uuid = cs.uuid(); - log = new NetworkTreeOperationLog(cs.getChangeList()); - } - - @Override - public Iterator<TreeOperation> iterator() { - return log.iterator(); - } + public PersistentChangeList(TreeContext cs) { + treeName = cs.getTreeName(); + uuid = cs.uuid(); + log = new NetworkTreeOperationLog(cs.getChangeList()); + } + + @Override + public Iterator<TreeOperation> iterator() { + return log.iterator(); + } - public NetworkTreeOperationLog getTreeOperationLog() { - return log; - } - - public String getTreeName() { - return treeName; - } - - public String uuid() { - return uuid; - } + public NetworkTreeOperationLog getTreeOperationLog() { + return log; + } + + public String getTreeName() { + return treeName; + } + + @Override + public int getTreeType() { + return treeType; + } + + public String uuid() { + return uuid; + } @Override public TreeOperationLog getLog() { return log; } - + }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentChangeListWriter.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentChangeListWriter.java Wed Feb 01 04:04:17 2017 +0900 @@ -11,25 +11,24 @@ import java.io.OutputStream; public class PersistentChangeListWriter implements ChangeListWriter { - - MessagePack msgpack = SingletonMessageFromAlice.getInstance(); - OutputStream out; - - public PersistentChangeListWriter(OutputStream _out) { - out = _out; - } - - @Override - public Result write(ChangeList cs) - { - NetworkTreeOperationLog log = new NetworkTreeOperationLog(cs.uuid(), cs.getTreeName(), cs); - try { - msgpack.write(out, log); - out.flush(); - return Result.SUCCESS; - } catch (IOException e) { - - } - return null; - } + + MessagePack msgpack = SingletonMessageFromAlice.getInstance(); + OutputStream out; + + public PersistentChangeListWriter(OutputStream _out) { + out = _out; + } + + @Override + public Result write(ChangeList cs) { + NetworkTreeOperationLog log = new NetworkTreeOperationLog(cs.uuid(), cs.getTreeName(), cs.getTreeType(), cs); + try { + msgpack.write(out, log); + out.flush(); + return Result.SUCCESS; + } catch (IOException e) { + + } + return null; + } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentJungle.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentJungle.java Wed Feb 01 04:04:17 2017 +0900 @@ -4,16 +4,26 @@ import jp.ac.u_ryukyu.ie.cr.jungle.Jungle; import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List; import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; +import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DefaultInterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DifferentialInterfaceTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.InterfaceTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.DefaultTreeOperationLog; import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.TreeOperationLog; import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.treeEditor.DefaultTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.treeEditor.DifferenceTreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.treeEditor.TreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.Default.DefaultTreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.Differencial.DifferencialTreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; -import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DefaultInterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.traverser.Traverser; 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.jungleNetwork.persistent.tree.PersistentDifferentialJungleTree; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.tree.PersistentJungleTree; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.treeContext.PersistentTreeContext; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.treeContext.PersistentdifferentialTreeContext; import java.util.Enumeration; import java.util.Iterator; @@ -24,12 +34,14 @@ private ConcurrentHashMap<String, JungleTree> trees; final private String uuid; private TreeEditor editor; + private TreeEditor differenceEditor; - public PersistentJungle(PersistentJournal _journal, String _uuid, TreeEditor _editor) { + public PersistentJungle(PersistentJournal _journal, String _uuid, Traverser traverser) { journal = _journal; trees = new ConcurrentHashMap<String, JungleTree>(); uuid = _uuid; - editor = _editor; + editor = new DefaultTreeEditor(traverser); + differenceEditor = new DifferenceTreeEditor(traverser); } @Override @@ -40,7 +52,7 @@ @Override public JungleTree createNewTree(final String name) { - return createNewTree(name,new DefaultTreeNode()); + return createNewTree(name, new DefaultTreeNode()); } @Override @@ -48,7 +60,7 @@ ChangeList list = new ChangeList() { @Override public Iterator<TreeOperation> iterator() { - List<TreeOperation> nil = new List(); + List<TreeOperation> nil = new List<>(); return nil.iterator(); } @@ -63,6 +75,11 @@ } @Override + public int getTreeType() { + return TreeType.DEFAULT; + } + + @Override public TreeOperationLog getLog() { return new DefaultTreeOperationLog(); } @@ -77,14 +94,50 @@ return newTree; } + @Override - public JungleTree createNewDifferenceTree(String name) { - return null; //未実装 + public JungleTree createNewDifferenceTree(final String name) { + TreeNode rootNode = new DifferencialTreeNode(); + return createNewDifferenceTree(name, rootNode); } @Override - public JungleTree createNewDifferenceTree(String name, TreeNode rootNode) { - return null;//未実装 + public JungleTree createNewDifferenceTree(final String name, TreeNode rootNode) { + ChangeList list = new ChangeList() { + @Override + public Iterator<TreeOperation> iterator() { + List<TreeOperation> nil = new List<>(); + return nil.iterator(); + } + + @Override + public String uuid() { + return uuid; + } + + @Override + public String getTreeName() { + return name; + } + + @Override + public int getTreeType() { + return TreeType.DIFFERENCE; + } + + @Override + public TreeOperationLog getLog() { + return new DefaultTreeOperationLog(); + } + + }; + InterfaceTraverser traverser = new DifferentialInterfaceTraverser(rootNode, rootNode, true); + TreeContext tc = new PersistentdifferentialTreeContext(rootNode, rootNode, null, list, uuid, name, 0, traverser); + JungleTree newTree = new PersistentDifferentialJungleTree(name,tc, uuid, journal.getWriter(), differenceEditor,0); + if (trees.putIfAbsent(name, newTree) != null) { + return null; + } + return newTree; } @Override
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentJungleTree.java Fri Jan 27 01:09:43 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,144 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent; - - -import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; -import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListWriter; -import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.InterfaceTraverser; -import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; -import jp.ac.u_ryukyu.ie.cr.jungle.store.index.Index; -import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; -import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.DefaultNodePath; -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.editor.treeEditor.TreeEditor; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNodeChildren; -import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DefaultInterfaceTraverser; -import jp.ac.u_ryukyu.ie.cr.jungle.tree.DefaultJungleTree; -import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree; -import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; -import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; -import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; -import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.GetOldTreeError; - -import java.util.Optional; -import java.util.concurrent.atomic.AtomicReference; - -public class PersistentJungleTree implements JungleTree { - private final AtomicReference<TreeContext> repository; - private final String uuid; - private final String treeName; - private final ChangeListWriter writer; - private final TreeEditor editor; - private int bufferSize; - - public PersistentJungleTree(String _treeName, TreeContext _tc, String _uuid, ChangeListWriter _writer, TreeEditor _editor, int _bufferSize) { - treeName = _treeName; - repository = new AtomicReference<TreeContext>(_tc); - uuid = _uuid; - writer = _writer; - editor = _editor; - bufferSize = _bufferSize; - } - - @Override - public JungleTreeEditor getJungleTreeEditor() { - TreeContext tc = repository.get(); - PersistentTransactionManager txManager = new PersistentTransactionManager(treeName, writer, tc, repository, uuid, bufferSize); - TreeNode root = tc.getRoot(); - ChangeList cl = tc.getChangeList(); - return new PersistentJungleTreeEditor(root, txManager, editor, cl.getLog()); - } - - @Override - public TreeNode getRootNode() { - TreeContext tc = repository.get(); - return tc.getRoot(); - } - - @Override - public JungleTreeEditor getLocalJungleTreeEditor() { - return getJungleTreeEditor(); - } - - @Override - public long revision() { - TreeContext tc = repository.get(); - return tc.revision(); - } - - @Override - public Either<Error, JungleTree> getOldTree(long revision) { - TreeContext tc = repository.get(); - - for (; tc.revision() != revision; ) { - tc = tc.prev(); - if (tc == null) - return DefaultEither.newA(GetOldTreeError.OLD_TREE_NOT_FOUND); - } - - - String oldTreeUuid = uuid + revision; - JungleTree oldTree = new DefaultJungleTree(tc, oldTreeUuid, writer, editor); - return DefaultEither.newB(oldTree); - } - - @Override - public ParentIndex getParentIndex() { - TreeContext tc = repository.get(); - return tc.getParentIndex(); - } - - @Override - public Index getIndex() { - TreeContext tc = repository.get(); - return tc.getIndex(); - } - - @Override - public InterfaceTraverser getTraverser(boolean useIndex) { - Index index = getIndex(); - ParentIndex parentIndex = getParentIndex(); - return new DefaultInterfaceTraverser(getRootNode(), index, parentIndex, useIndex); - } - - @Override - public Either<Error, TreeNode> getNodeOfPath(NodePath path) { - TreeNode node = repository.get().getRoot(); - for (int num : path) { - if (num == -1) - continue; - Either<Error, TreeNode> either = node.getChildren().at(num); - if (either.isA()) - return either; - node = either.b(); - } - return DefaultEither.newB(node); - } - - @Override - public void setBufferSize(int _bufferSize) { - bufferSize = _bufferSize; - } - - @Override - public NodePath getNodePath(TreeNode node) { - ParentIndex parentIndex = getParentIndex(); - DefaultNodePath path = new DefaultNodePath(); - Optional<TreeNode> parentOp = parentIndex.get(node); - while(parentOp.isPresent()) { - TreeNode parent = parentOp.get(); - TreeNodeChildren children = parent.getChildren(); - int num = 0; - for (TreeNode child : children) { - if (child == node) - break; - num++; - } - path = path.add(num); - node = parent; - parentOp = parentIndex.get(node); - } - return path; - } -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentTransactionManager.java Fri Jan 27 01:09:43 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent; - - -import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List; -import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListWriter; -import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.InterfaceTraverser; -import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; -import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.DefaultTreeOperationLog; -import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.TreeOperationLog; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.manager.TransactionManager; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; -import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DefaultInterfaceTraverser; -import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; -import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; -import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.DefaultError; -import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; - -import java.util.concurrent.atomic.AtomicReference; - -public class PersistentTransactionManager implements TransactionManager { - private final AtomicReference<TreeContext> repository; - private final TreeContext tip; - private final ChangeListWriter writer; - private final String uuid; - private final String treeName; - private final int bufferSize; - - public PersistentTransactionManager(String _treeName, ChangeListWriter _writer, TreeContext _tip, AtomicReference<TreeContext> _repository, String _uuid, int _bufferSize) { - repository = _repository; - tip = _tip; - writer = _writer; - uuid = _uuid; - treeName = _treeName; - bufferSize = _bufferSize; - } - - @Override - public Either<Error, TransactionManager> commit(TreeNode _newRoot, final TreeOperationLog _log,List<TreeNode> editNodeList) { - long currentRevision = tip.revision(); - long nextRevision = currentRevision + 1; - - PersistentChangeList list; - if (_log.length() > bufferSize) - list = new PersistentChangeList(uuid, treeName, new DefaultTreeOperationLog()); - else - list = new PersistentChangeList(uuid, treeName, _log); - - InterfaceTraverser traverser = new DefaultInterfaceTraverser(_newRoot, false); - traverser.createIndex(); - PersistentTreeContext newContext = new PersistentTreeContext(_newRoot, tip, list, uuid, treeName, nextRevision, traverser); - - if (repository.compareAndSet(newContext.prev(), newContext)) { - if (_log.length() > bufferSize) { - PersistentChangeList writeList = new PersistentChangeList(uuid, treeName, _log); - writer.write(writeList); - } - TransactionManager txManager = new PersistentTransactionManager(treeName, writer, newContext, repository, uuid, bufferSize); - return DefaultEither.newB(txManager); - } - - return DefaultEither.newA((Error) new DefaultError()); - - } - - @Override - public Either<Error, TransactionManager> flashCommit(TreeNode _newRoot, final TreeOperationLog _log) { - long currentRevision = tip.revision(); - long nextRevision = currentRevision + 1; - - PersistentChangeList list; - list = new PersistentChangeList(uuid, treeName, new DefaultTreeOperationLog()); - InterfaceTraverser traverser = new DefaultInterfaceTraverser(_newRoot, false); - traverser.createIndex(); - PersistentTreeContext newContext = new PersistentTreeContext(_newRoot, tip, list, uuid, treeName, nextRevision, traverser); - if (repository.compareAndSet(newContext.prev(), newContext)) { - PersistentChangeList writeList = new PersistentChangeList(uuid, treeName, _log); - writer.write(writeList); - TransactionManager txManager = new PersistentTransactionManager(treeName, writer, newContext, repository, uuid, bufferSize); - return DefaultEither.newB(txManager); - } - - return DefaultEither.newA((Error) new DefaultError()); - - } - - @Override - public long getRevision() { - return tip.revision(); - } - - @Override - public String getUUID() { - return uuid; - } - - -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentTreeContext.java Fri Jan 27 01:09:43 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,86 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent; - - -import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; -import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.InterfaceTraverser; -import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; -import jp.ac.u_ryukyu.ie.cr.jungle.store.index.Index; -import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; -import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; - -public class PersistentTreeContext implements TreeContext { - private final TreeNode root; - private final TreeContext previous; - private final ChangeList changeList; - private final String uuid; - private final long revision; - private final String treeName; - private InterfaceTraverser traverser; - - public PersistentTreeContext(TreeNode _node, TreeContext _prev, ChangeList _log, String _uuid, String _treeName, - long _revision, InterfaceTraverser traverser) { - this.root = _node; - this.previous = _prev; - this.changeList = _log; - this.uuid = _uuid; - this.revision = _revision; - this.treeName = _treeName; - this.traverser = traverser; - } - - @Override - public TreeNode getRoot() { - return root; - } - - @Override - public TreeNode getEndNode() { - return null; // not use - } - - @Override - public TreeContext prev() { - return previous; - } - - @Override - public ChangeList getChangeList() { - return changeList; - } - - @Override - public String uuid() { - return uuid; - } - - @Override - public long revision() { - return revision; - } - - @Override - public Iterable<TreeOperation> getOperations() { - return changeList; - } - - public String getTreeName() { - return treeName; - } - - @Override - public Index getIndex() { - return traverser.getIndex(); - } - - @Override - public ParentIndex getParentIndex() { - return traverser.getParentIndex(); - } - - @Override - public InterfaceTraverser getTraverser() { - return traverser; - } - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/TransactionManager/PersistentDifferenceTransactionManager.java Wed Feb 01 04:04:17 2017 +0900 @@ -0,0 +1,116 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.TransactionManager; + +import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List; +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListWriter; +import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DifferentialInterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.InterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; +import jp.ac.u_ryukyu.ie.cr.jungle.store.index.Index; +import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; +import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.DefaultTreeOperationLog; +import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.TreeOperationLog; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.context.DifferenceTreeContext; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.manager.TransactionManager; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNodeChildren; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.TreeType; +import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentChangeList; + +import java.util.concurrent.atomic.AtomicReference; + +import static jp.ac.u_ryukyu.ie.cr.jungle.util.Error.TransactionError.APPEND_FAILD; +import static jp.ac.u_ryukyu.ie.cr.jungle.util.Error.TreeEditorError.CAS_MISS; + + +public class PersistentDifferenceTransactionManager implements TransactionManager { + private final AtomicReference<TreeContext> repository; + private final TreeContext tip; + private final ChangeListWriter writer; + private final String uuid; + private final int bufferSize; + + public PersistentDifferenceTransactionManager(ChangeListWriter writer, TreeContext tip, AtomicReference<TreeContext> repository, String uuid, int bufferSize) { + this.repository = repository; + this.tip = tip; + this.writer = writer; + this.uuid = uuid; + this.bufferSize = bufferSize; + } + + /** + * DifferentialTreeではIndexの更新時 + * delete処理を行う必要が無いので + * editNodeListは使用しない + * commitの第三引数のeditNodeListは使用しない + * しかし、末尾のノード情報をrepositoryに入れる必要があるため + * editNodeListを使ってここに持って来ている + * 昔はJungleTreeEditorで後からsetしていたが、 + * DifferentialInterfaceTraverserに末尾ノードを入れる必要が出てきたので + * この実装になった + */ + @Override + public Either<Error, TransactionManager> commit(TreeNode subTreeRoot, TreeOperationLog log, List<TreeNode> editNodeList) { + long currentRevision = tip.revision(); + long nextRevision = currentRevision + 1; + //NodeOperation commitOperation = new Commit(); + //TreeOperationLog newLog = log.add(new DefaultNodePath(), commitOperation); + final String treeName = tip.getTreeName(); + + PersistentChangeList list; + if (log.length() > bufferSize) + list = new PersistentChangeList(uuid, treeName, TreeType.DIFFERENCE, new DefaultTreeOperationLog()); + else + list = new PersistentChangeList(uuid, treeName, TreeType.DIFFERENCE, log); + + TreeNode root = tip.getRoot(); + Index index = tip.getIndex(); + ParentIndex parentIndex = tip.getParentIndex(); + TreeNode endNode = editNodeList.get(0); + InterfaceTraverser traverser = new DifferentialInterfaceTraverser(root, endNode, index, parentIndex, true); + TreeContext newTreeContext = new DifferenceTreeContext(root, endNode, tip, list, uuid, treeName, nextRevision, traverser); + if (repository.compareAndSet(newTreeContext.prev(), newTreeContext)) { + Either<Error, TreeNode> either = appendSubTree(subTreeRoot); + if (either.isA()) + return DefaultEither.newA(either.a()); + traverser.updateIndex(tip.getEndNode()); + if (log.length() > bufferSize) { + PersistentChangeList writeList = new PersistentChangeList(uuid, treeName, TreeType.DIFFERENCE, log); + writer.write(writeList); + } + TransactionManager txManager = new PersistentDifferenceTransactionManager(writer, newTreeContext, repository, uuid, bufferSize); + return DefaultEither.newB(txManager); + } + + return DefaultEither.newA(CAS_MISS); + } + + private Either<Error, TreeNode> appendSubTree(TreeNode subTreeRoot) { + TreeNode appendedNode = tip.getEndNode(); + TreeNodeChildren children = appendedNode.getChildren(); + if (children.size() != 0) + return DefaultEither.newA(APPEND_FAILD); //append Nodeが1つ以上子を持つ場合過去の木であるため、変更させない。そうすることで整合性を保証する + return children.addNewChildAt(0, subTreeRoot); + } + + + @Override + public Either<Error, TransactionManager> flashCommit(TreeNode _newRoot, TreeOperationLog _log) { + return null;//commit(_newRoot, _log); + } + + @Override + public String getUUID() { + return uuid; + } + + @Override + public long getRevision() { + return tip.revision(); + } + +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/TransactionManager/PersistentTransactionManager.java Wed Feb 01 04:04:17 2017 +0900 @@ -0,0 +1,100 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.TransactionManager; + + +import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List; +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListWriter; +import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.InterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; +import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.DefaultTreeOperationLog; +import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.TreeOperationLog; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.manager.TransactionManager; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DefaultInterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.TreeType; +import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.DefaultError; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentChangeList; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.treeContext.PersistentTreeContext; + +import java.util.concurrent.atomic.AtomicReference; + +public class PersistentTransactionManager implements TransactionManager { + private final AtomicReference<TreeContext> repository; + private final TreeContext tip; + private final ChangeListWriter writer; + private final String uuid; + private final String treeName; + private final int bufferSize; + + public PersistentTransactionManager(String _treeName, ChangeListWriter _writer, TreeContext _tip, AtomicReference<TreeContext> _repository, String _uuid, int _bufferSize) { + repository = _repository; + tip = _tip; + writer = _writer; + uuid = _uuid; + treeName = _treeName; + bufferSize = _bufferSize; + } + + @Override + public Either<Error, TransactionManager> commit(TreeNode _newRoot, final TreeOperationLog _log,List<TreeNode> editNodeList) { + long currentRevision = tip.revision(); + long nextRevision = currentRevision + 1; + + PersistentChangeList list; + if (_log.length() > bufferSize) + list = new PersistentChangeList(uuid, treeName, TreeType.DEFAULT,new DefaultTreeOperationLog()); + else + list = new PersistentChangeList(uuid, treeName,TreeType.DEFAULT, _log); + + InterfaceTraverser traverser = new DefaultInterfaceTraverser(_newRoot, false); + traverser.createIndex(); + PersistentTreeContext newContext = new PersistentTreeContext(_newRoot, tip, list, uuid, treeName, nextRevision, traverser); + + if (repository.compareAndSet(newContext.prev(), newContext)) { + if (_log.length() > bufferSize) { + PersistentChangeList writeList = new PersistentChangeList(uuid, treeName,TreeType.DEFAULT, _log); + writer.write(writeList); + } + TransactionManager txManager = new PersistentTransactionManager(treeName, writer, newContext, repository, uuid, bufferSize); + return DefaultEither.newB(txManager); + } + + return DefaultEither.newA((Error) new DefaultError()); + + } + + @Override + public Either<Error, TransactionManager> flashCommit(TreeNode _newRoot, final TreeOperationLog _log) { + long currentRevision = tip.revision(); + long nextRevision = currentRevision + 1; + + PersistentChangeList list; + list = new PersistentChangeList(uuid, treeName,TreeType.DEFAULT ,new DefaultTreeOperationLog()); + InterfaceTraverser traverser = new DefaultInterfaceTraverser(_newRoot, false); + traverser.createIndex(); + PersistentTreeContext newContext = new PersistentTreeContext(_newRoot, tip, list, uuid, treeName, nextRevision, traverser); + if (repository.compareAndSet(newContext.prev(), newContext)) { + PersistentChangeList writeList = new PersistentChangeList(uuid, treeName,TreeType.DEFAULT, _log); + writer.write(writeList); + TransactionManager txManager = new PersistentTransactionManager(treeName, writer, newContext, repository, uuid, bufferSize); + return DefaultEither.newB(txManager); + } + + return DefaultEither.newA((Error) new DefaultError()); + + } + + @Override + public long getRevision() { + return tip.revision(); + } + + @Override + public String getUUID() { + return uuid; + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/tree/PersistentDifferentialJungleTree.java Wed Feb 01 04:04:17 2017 +0900 @@ -0,0 +1,65 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.tree; + +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListWriter; +import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.DifferenceJungleTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.treeEditor.TreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.manager.TransactionManager; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.Differencial.DifferencialTreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.DifferenceListJungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.GetOldTreeError; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.TransactionManager.PersistentDifferenceTransactionManager; + +import java.util.concurrent.atomic.AtomicReference; + + +public class PersistentDifferentialJungleTree extends PersistentJungleTree { + + public PersistentDifferentialJungleTree(String treeName, TreeContext tc, String uuid, ChangeListWriter writer, TreeEditor editor, int bufferSize) { + super(treeName, tc, uuid, writer,editor,bufferSize); + } + + @Override + public JungleTreeEditor getJungleTreeEditor() { + AtomicReference<TreeContext> repository = super.getRepository(); + TreeContext tc = repository.get(); + ChangeListWriter writer = super.getWriter(); + String uuid = super.getUuid(); + TreeEditor treeEditor = super.getTreeEditor(); + int bufferSize = super.getBufferSize(); + TransactionManager txManager = new PersistentDifferenceTransactionManager(writer, tc, repository, uuid,bufferSize); + TreeNode subTreeRoot = new DifferencialTreeNode(); + return new DifferenceJungleTreeEditor(subTreeRoot, txManager, treeEditor); + } + + @Override + public Either<Error, JungleTree> getOldTree(long revision) { + TreeContext tc = super.getRepository().get(); + + for (; tc.revision() != revision; ) { + tc = tc.prev(); + if (tc == null) + return DefaultEither.newA(GetOldTreeError.OLD_TREE_NOT_FOUND); + } + + String uuid = super.getUuid(); + String oldTreeUuid = uuid + revision; + ChangeListWriter writer = super.getWriter(); + TreeEditor treeEditor = super.getTreeEditor(); + JungleTree oldTree = new DifferenceListJungleTree(tc, oldTreeUuid, writer, treeEditor); + return DefaultEither.newB(oldTree); + } + + //test用 method キャストして使ってね + public TreeNode getEndNode(){ + AtomicReference<TreeContext> repository = super.getRepository(); + TreeContext tc = repository.get(); + return tc.getEndNode(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/tree/PersistentJungleTree.java Wed Feb 01 04:04:17 2017 +0900 @@ -0,0 +1,166 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.tree; + + +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeListWriter; +import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.InterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; +import jp.ac.u_ryukyu.ie.cr.jungle.store.index.Index; +import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; +import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.DefaultNodePath; +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.editor.treeEditor.TreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNodeChildren; +import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DefaultInterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.DefaultJungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.JungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; +import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.GetOldTreeError; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJungleTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.TransactionManager.PersistentTransactionManager; + +import java.util.Optional; +import java.util.concurrent.atomic.AtomicReference; + +public class PersistentJungleTree implements JungleTree { + private final AtomicReference<TreeContext> repository; + private final ChangeListWriter writer; + private final String uuid; + private final String treeName; + private final TreeEditor editor; + private int bufferSize; + + public PersistentJungleTree(String _treeName, TreeContext _tc, String _uuid, ChangeListWriter _writer, TreeEditor _editor, int _bufferSize) { + treeName = _treeName; + repository = new AtomicReference<TreeContext>(_tc); + uuid = _uuid; + writer = _writer; + editor = _editor; + bufferSize = _bufferSize; + } + + protected AtomicReference<TreeContext> getRepository(){ + return repository; + } + + protected ChangeListWriter getWriter(){ + return writer; + } + + protected String getUuid(){ + return uuid; + } + + protected TreeEditor getTreeEditor(){ + return editor; + } + + protected int getBufferSize(){ + return bufferSize; + } + + @Override + public JungleTreeEditor getJungleTreeEditor() { + TreeContext tc = repository.get(); + PersistentTransactionManager txManager = new PersistentTransactionManager(treeName, writer, tc, repository, uuid, bufferSize); + TreeNode root = tc.getRoot(); + ChangeList cl = tc.getChangeList(); + return new PersistentJungleTreeEditor(root, txManager, editor, cl.getLog()); + } + + @Override + public TreeNode getRootNode() { + TreeContext tc = repository.get(); + return tc.getRoot(); + } + + @Override + public JungleTreeEditor getLocalJungleTreeEditor() { + return getJungleTreeEditor(); + } + + @Override + public long revision() { + TreeContext tc = repository.get(); + return tc.revision(); + } + + @Override + public Either<Error, JungleTree> getOldTree(long revision) { + TreeContext tc = repository.get(); + + for (; tc.revision() != revision; ) { + tc = tc.prev(); + if (tc == null) + return DefaultEither.newA(GetOldTreeError.OLD_TREE_NOT_FOUND); + } + + + String oldTreeUuid = uuid + revision; + JungleTree oldTree = new DefaultJungleTree(tc, oldTreeUuid, writer, editor); + return DefaultEither.newB(oldTree); + } + + @Override + public ParentIndex getParentIndex() { + TreeContext tc = repository.get(); + return tc.getParentIndex(); + } + + @Override + public Index getIndex() { + TreeContext tc = repository.get(); + return tc.getIndex(); + } + + @Override + public InterfaceTraverser getTraverser(boolean useIndex) { + Index index = getIndex(); + ParentIndex parentIndex = getParentIndex(); + return new DefaultInterfaceTraverser(getRootNode(), index, parentIndex, useIndex); + } + + @Override + public Either<Error, TreeNode> getNodeOfPath(NodePath path) { + TreeNode node = repository.get().getRoot(); + for (int num : path) { + if (num == -1) + continue; + Either<Error, TreeNode> either = node.getChildren().at(num); + if (either.isA()) + return either; + node = either.b(); + } + return DefaultEither.newB(node); + } + + @Override + public void setBufferSize(int _bufferSize) { + bufferSize = _bufferSize; + } + + @Override + public NodePath getNodePath(TreeNode node) { + ParentIndex parentIndex = getParentIndex(); + DefaultNodePath path = new DefaultNodePath(); + Optional<TreeNode> parentOp = parentIndex.get(node); + while(parentOp.isPresent()) { + TreeNode parent = parentOp.get(); + TreeNodeChildren children = parent.getChildren(); + int num = 0; + for (TreeNode child : children) { + if (child == node) + break; + num++; + } + path = path.add(num); + node = parent; + parentOp = parentIndex.get(node); + } + return path; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/treeContext/PersistentTreeContext.java Wed Feb 01 04:04:17 2017 +0900 @@ -0,0 +1,86 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.treeContext; + + +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; +import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.InterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; +import jp.ac.u_ryukyu.ie.cr.jungle.store.index.Index; +import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; +import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; + +public class PersistentTreeContext implements TreeContext { + private final TreeNode root; + private final TreeContext previous; + private final ChangeList changeList; + private final String uuid; + private final String treeName; + private final long revision; + private InterfaceTraverser traverser; + + public PersistentTreeContext(TreeNode node, TreeContext prev, ChangeList log, String uuid, String treeName, + long revision, InterfaceTraverser traverser) { + this.root = node; + this.previous = prev; + this.changeList = log; + this.uuid = uuid; + this.revision = revision; + this.treeName = treeName; + this.traverser = traverser; + } + + @Override + public TreeNode getRoot() { + return root; + } + + @Override + public TreeNode getEndNode() { + return null; // not use + } + + @Override + public TreeContext prev() { + return previous; + } + + @Override + public ChangeList getChangeList() { + return changeList; + } + + @Override + public String uuid() { + return uuid; + } + + @Override + public long revision() { + return revision; + } + + @Override + public Iterable<TreeOperation> getOperations() { + return changeList; + } + + public String getTreeName() { + return treeName; + } + + @Override + public Index getIndex() { + return traverser.getIndex(); + } + + @Override + public ParentIndex getParentIndex() { + return traverser.getParentIndex(); + } + + @Override + public InterfaceTraverser getTraverser() { + return traverser; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/treeContext/PersistentdifferentialTreeContext.java Wed Feb 01 04:04:17 2017 +0900 @@ -0,0 +1,90 @@ +package jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.treeContext; + +import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; +import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.InterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; +import jp.ac.u_ryukyu.ie.cr.jungle.store.index.Index; +import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; +import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; + +/** + * Created by e115731 on 2017/01/31. + */ +public class PersistentdifferentialTreeContext implements TreeContext { + private final TreeNode root; + private final TreeNode endNode; + private final TreeContext previous; + private final ChangeList changeList; + private final String uuid; + private final String treeName; + private final long revision; + private InterfaceTraverser traverser; + + public PersistentdifferentialTreeContext(TreeNode node, TreeNode endNode, TreeContext prev, ChangeList log, String uuid, String treeName, + long revision, InterfaceTraverser traverser) { + this.root = node; + this.endNode = endNode; + this.previous = prev; + this.changeList = log; + this.uuid = uuid; + this.revision = revision; + this.treeName = treeName; + this.traverser = traverser; + } + + @Override + public TreeNode getRoot() { + return root; + } + + @Override + public TreeNode getEndNode() { + return endNode; + } + + @Override + public TreeContext prev() { + return previous; + } + + @Override + public ChangeList getChangeList() { + return changeList; + } + + @Override + public String uuid() { + return uuid; + } + + @Override + public long revision() { + return revision; + } + + @Override + public Iterable<TreeOperation> getOperations() { + return changeList; + } + + public String getTreeName() { + return treeName; + } + + @Override + public Index getIndex() { + return traverser.getIndex(); + } + + @Override + public ParentIndex getParentIndex() { + return traverser.getParentIndex(); + } + + @Override + public InterfaceTraverser getTraverser() { + return traverser; + } + +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/JungleUpdater.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/JungleUpdater.java Wed Feb 01 04:04:17 2017 +0900 @@ -1,11 +1,11 @@ package jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.store.Command; import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.NodeOperation; import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation; +import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; @@ -29,7 +29,7 @@ } editor = either.b(); } - return either; + return editor.success(); } /* @@ -47,7 +47,7 @@ } editor = either.b(); } - return either; + return editor.success(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTree.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTree.java Wed Feb 01 04:04:17 2017 +0900 @@ -15,6 +15,7 @@ import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DefaultInterfaceTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.tree.DefaultJungleTree; 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.DefaultEither; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.Error; @@ -29,10 +30,12 @@ private final String treeName; private final ChangeListWriter writer; private final TreeEditor editor; + private final int type = TreeType.DEFAULT;; private int bufferSize; public NetworkDefaultJungleTree(String _treeName, TreeContext _tc,String _uuid,ChangeListWriter _writer,TreeEditor _editor) { + treeName = _treeName; repository = new AtomicReference<TreeContext>(_tc); uuid = _uuid; @@ -46,7 +49,7 @@ TreeContext tc = repository.get(); NetworkTransactionManager txManager = new NetworkTransactionManager(treeName, writer,tc,repository,uuid); TreeNode root = tc.getRoot(); - return new NetworkDefaultJungleTreeEditor(treeName, root,txManager,editor); + return new NetworkDefaultJungleTreeEditor(treeName,type, root,txManager,editor); } @Override @@ -55,7 +58,7 @@ TreeContext tc = repository.get(); NetworkTransactionManager txManager = new NetworkTransactionManager(treeName, writer,tc,repository,uuid); TreeNode root = tc.getRoot(); - return NetworkDefaultJungleTreeEditor.NewLocalJungleTreeEditor(treeName,root,txManager, editor); + return NetworkDefaultJungleTreeEditor.NewLocalJungleTreeEditor(treeName,type,root,txManager, editor); } @Override
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTreeEditor.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTreeEditor.java Wed Feb 01 04:04:17 2017 +0900 @@ -28,179 +28,175 @@ public class NetworkDefaultJungleTreeEditor implements JungleTreeEditor { - private final TransactionManager txManager; - private final TreeNode root; - private final TreeEditor editor; - private final String treeName; - private final TreeOperationLog log; - private boolean exportLog; - private final List<TreeNode> editedNodeList = new List<>(); + private final TransactionManager txManager; + private final TreeNode root; + private final TreeEditor editor; + private final String treeName; + private final int type; + private final TreeOperationLog log; + private boolean exportLog; + private final List<TreeNode> editedNodeList = new List<>(); + + public NetworkDefaultJungleTreeEditor(String _treeName, int type, TreeNode _root, TransactionManager _txManager, TreeEditor _editor) { + this(_treeName, type, _root, _txManager, _editor, new DefaultTreeOperationLog()); + } - public NetworkDefaultJungleTreeEditor(String _treeName, TreeNode _root,TransactionManager _txManager,TreeEditor _editor) - { - this(_treeName, _root,_txManager,_editor,new DefaultTreeOperationLog()); - } - - public NetworkDefaultJungleTreeEditor(String _treeName, TreeNode _root,TransactionManager _txManager,TreeEditor _editor, TreeOperationLog _log) - { - treeName = _treeName; - root = _root; - txManager = _txManager; - editor = _editor; - log = _log; - exportLog = true; - } - - public static NetworkDefaultJungleTreeEditor NewLocalJungleTreeEditor(String _treeName, TreeNode _root,TransactionManager _txManager,TreeEditor _editor) { - NetworkDefaultJungleTreeEditor treeEditor = new NetworkDefaultJungleTreeEditor(_treeName, _root,_txManager,_editor,new DefaultTreeOperationLog()); - treeEditor.exportLog = false; - return treeEditor; - } + public NetworkDefaultJungleTreeEditor(String _treeName, int type, TreeNode _root, TransactionManager _txManager, TreeEditor _editor, TreeOperationLog _log) { + this.treeName = _treeName; + this.root = _root; + this.txManager = _txManager; + this.editor = _editor; + this.log = _log; + this.exportLog = true; + this.type = type; + } + + public static NetworkDefaultJungleTreeEditor NewLocalJungleTreeEditor(String _treeName, int type, TreeNode _root, TransactionManager _txManager, TreeEditor _editor) { + NetworkDefaultJungleTreeEditor treeEditor = new NetworkDefaultJungleTreeEditor(_treeName, type, _root, _txManager, _editor, new DefaultTreeOperationLog()); + treeEditor.exportLog = false; + return treeEditor; + } + + public static NetworkDefaultJungleTreeEditor NewLocalJungleTreeEditor(String _treeName, int type, TreeNode _root, TransactionManager _txManager, TreeEditor _editor, TreeOperationLog _log) { + NetworkDefaultJungleTreeEditor treeEditor = new NetworkDefaultJungleTreeEditor(_treeName, type, _root, _txManager, _editor, _log); + treeEditor.exportLog = false; + return treeEditor; + } + + private Either<Error, JungleTreeEditor> _edit(final NodePath _path, NodeEditor _e) { + Either<Error, LoggingNode> editEither = editor.edit(root, _path, _e); + if (editEither.isA()) { + return DefaultEither.newA(editEither.a()); + } - public static NetworkDefaultJungleTreeEditor NewLocalJungleTreeEditor(String _treeName, TreeNode _root,TransactionManager _txManager,TreeEditor _editor, TreeOperationLog _log) { - NetworkDefaultJungleTreeEditor treeEditor = new NetworkDefaultJungleTreeEditor(_treeName, _root,_txManager,_editor,_log); - treeEditor.exportLog = false; - return treeEditor; - } - - private Either<Error,JungleTreeEditor> _edit(final NodePath _path,NodeEditor _e) - { - Either<Error, LoggingNode> editEither = editor.edit(root, _path, _e); - if(editEither.isA()){ - return DefaultEither.newA(editEither.a()); - } - - TreeNode newNode = editEither.b().getWrap(); - OperationLog newLog = editEither.b().getOperationLog(); - - IterableConverter.Converter<TreeOperation,NodeOperation> converter = new IterableConverter.Converter<TreeOperation,NodeOperation>(){ - @Override - public TreeOperation conv(NodeOperation _b){ - return new DefaultTreeOperation(_path,_b); - } - }; - - Iterable<TreeOperation> iterable = new IterableConverter<TreeOperation,NodeOperation>(newLog,converter); - DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog(iterable,newLog.length()); - TreeOperationLog newTreeOpLog = log.append(treeOperationLog); - - JungleTreeEditor newEditor; - if(exportLog) { - newEditor = new NetworkDefaultJungleTreeEditor(treeName, newNode,txManager,editor,newTreeOpLog); - } else { - newEditor = NetworkDefaultJungleTreeEditor.NewLocalJungleTreeEditor(treeName, newNode, txManager, editor, newTreeOpLog); - } - return DefaultEither.newB(newEditor); - } - - @Override - public Either<Error,JungleTreeEditor> addNewChildAt(NodePath _path, int _pos) - { - AppendChildAt appendChildAt = new AppendChildAt(_pos); - return _edit(_path,appendChildAt); - } + TreeNode newNode = editEither.b().getWrap(); + OperationLog newLog = editEither.b().getOperationLog(); + + IterableConverter.Converter<TreeOperation, NodeOperation> converter = new IterableConverter.Converter<TreeOperation, NodeOperation>() { + @Override + public TreeOperation conv(NodeOperation _b) { + return new DefaultTreeOperation(_path, _b); + } + }; + + Iterable<TreeOperation> iterable = new IterableConverter<TreeOperation, NodeOperation>(newLog, converter); + DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog(iterable, newLog.length()); + TreeOperationLog newTreeOpLog = log.append(treeOperationLog); - @Override - public Either<Error, JungleTreeEditor> addNewChildAndPutAttribute(NodePath path, int pos, String key, ByteBuffer value) { - AppendChildAndPutAttribute appendChildAndPutAttribute = new AppendChildAndPutAttribute(key,value,pos); - return _edit(path,appendChildAndPutAttribute); - } + JungleTreeEditor newEditor; + if (exportLog) { + newEditor = new NetworkDefaultJungleTreeEditor(treeName, type, newNode, txManager, editor, newTreeOpLog); + } else { + newEditor = NetworkDefaultJungleTreeEditor.NewLocalJungleTreeEditor(treeName, type,newNode, txManager, editor, newTreeOpLog); + } + return DefaultEither.newB(newEditor); + } - @Override - public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path, int _pos) - { - DeleteChildAt deleteChildAt = new DeleteChildAt(_pos); - return _edit(_path,deleteChildAt); - } + @Override + public Either<Error, JungleTreeEditor> addNewChildAt(NodePath _path, int _pos) { + AppendChildAt appendChildAt = new AppendChildAt(_pos); + return _edit(_path, appendChildAt); + } + + @Override + public Either<Error, JungleTreeEditor> addNewChildAndPutAttribute(NodePath path, int pos, String key, ByteBuffer value) { + AppendChildAndPutAttribute appendChildAndPutAttribute = new AppendChildAndPutAttribute(key, value, pos); + return _edit(path, appendChildAndPutAttribute); + } + + @Override + public Either<Error, JungleTreeEditor> deleteChildAt(NodePath _path, int _pos) { + DeleteChildAt deleteChildAt = new DeleteChildAt(_pos); + return _edit(_path, deleteChildAt); + } - @Override - public Either<Error,JungleTreeEditor> putAttribute(NodePath _path,String _key,ByteBuffer _value) - { - PutAttribute putAttribute = new PutAttribute(_key,_value); - return _edit(_path,putAttribute); - } + @Override + public Either<Error, JungleTreeEditor> putAttribute(NodePath _path, String _key, ByteBuffer _value) { + PutAttribute putAttribute = new PutAttribute(_key, _value); + return _edit(_path, putAttribute); + } - @Override - public Either<Error,JungleTreeEditor> deleteAttribute(NodePath _path, String _key) - { - DeleteAttribute deleteAttribute = new DeleteAttribute(_key); - return _edit(_path,deleteAttribute); - } + @Override + public Either<Error, JungleTreeEditor> deleteAttribute(NodePath _path, String _key) { + DeleteAttribute deleteAttribute = new DeleteAttribute(_key); + return _edit(_path, deleteAttribute); + } - @Override - public Either<Error, JungleTreeEditor> moveChild(NodePath path, int childNum, String move) { - MoveChild movechild = new MoveChild(move, childNum); - return _edit(path,movechild); - } + @Override + public Either<Error, JungleTreeEditor> moveChild(NodePath path, int childNum, String move) { + MoveChild movechild = new MoveChild(move, childNum); + return _edit(path, movechild); + } - @Override - public Either<Error,JungleTreeEditor> edit(NodePath _path,NodeEditor _editor) - { - return _edit(_path,_editor); - } + @Override + public Either<Error, JungleTreeEditor> edit(NodePath _path, NodeEditor _editor) { + return _edit(_path, _editor); + } - @Override - public Either<Error,JungleTreeEditor> success() - { - Either<Error,TransactionManager> either = txManager.commit(root,log,editedNodeList); - if(either.isA()){ - return DefaultEither.newA(either.a()); - } - if(exportLog) { - try { - putTreeOperationLog(log); - } catch (IOException e) { - return DefaultEither.newA(either.a()); - } - } + @Override + public Either<Error, JungleTreeEditor> success() { + Either<Error, TransactionManager> either = txManager.commit(root, log, editedNodeList); + if (either.isA()) { + return DefaultEither.newA(either.a()); + } + if (exportLog) { + try { + putTreeOperationLog(log); + } catch (IOException e) { + return DefaultEither.newA(either.a()); + } + } - TransactionManager newTxManager = either.b(); - JungleTreeEditor newTreeEditor = new NetworkDefaultJungleTreeEditor(treeName, root,newTxManager,editor); - - return DefaultEither.newB(newTreeEditor); - } + TransactionManager newTxManager = either.b(); + JungleTreeEditor newTreeEditor = new NetworkDefaultJungleTreeEditor(treeName,type, root, newTxManager, editor); + + return DefaultEither.newB(newTreeEditor); + } @Override public Either<Error, JungleTreeEditor> flushSuccess() { return success(); } - private String getID() - { - return txManager.getUUID(); - } + private String getID() { + return txManager.getUUID(); + } + + private String getRevision() { + return Long.toString(txManager.getRevision()); + } - private String getRevision() - { - return Long.toString(txManager.getRevision()); - } + public String getTreeName() { + return treeName; + } - public String getTreeName() { - return treeName; - } - - public TreeOperationLog getTreeOperationLog() { - return log; - } + public TreeOperationLog getTreeOperationLog() { + return log; + } + + private int getType(){ + return type; + } - public void putTreeOperationLog(Iterable<TreeOperation> newLog) throws IOException { - String uuid = getID(); - String treeName = getTreeName(); - String updaterName = getID(); - String revision = getRevision(); - putDataSegment(uuid, treeName, updaterName, newLog, revision); - } - - public void putDataSegment(String _uuid, String _treeName, String _updaterName, Iterable<TreeOperation> newLog, String nextRevision) throws IOException { - NetworkTreeOperationLog netLog = new NetworkTreeOperationLog(_uuid, _treeName,newLog); - CodeSegment cs = new LogPutCodeSegment(netLog); - cs.execute(); - } + public void putTreeOperationLog(Iterable<TreeOperation> newLog) throws IOException { + String uuid = getID(); + String treeName = getTreeName(); + String updaterName = getID(); + String revision = getRevision(); + int type = getType(); + putDataSegment(uuid, treeName,type, updaterName, newLog, revision); + } - @Override - public Either<Error, JungleTreeEditor> replaceNewRootNode() { - // TODO Auto-generated method stub - return null; - } + public void putDataSegment(String _uuid, String _treeName, int type,String _updaterName, Iterable<TreeOperation> newLog, String nextRevision) throws IOException { + NetworkTreeOperationLog netLog = new NetworkTreeOperationLog(_uuid, _treeName,type, newLog); + CodeSegment cs = new LogPutCodeSegment(netLog); + cs.execute(); + } + + @Override + public Either<Error, JungleTreeEditor> replaceNewRootNode() { + // TODO Auto-generated method stub + return null; + } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkTransactionManager.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkTransactionManager.java Wed Feb 01 04:04:17 2017 +0900 @@ -12,6 +12,7 @@ import jp.ac.u_ryukyu.ie.cr.jungle.transaction.manager.TransactionManager; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.query.traverser.DefaultInterfaceTraverser; +import jp.ac.u_ryukyu.ie.cr.jungle.tree.TreeType; import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.jungle.util.Error.DefaultError; @@ -57,6 +58,11 @@ } @Override + public int getTreeType() { + return TreeType.DEFAULT; + } + + @Override public TreeOperationLog getLog() { return _log; }
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/impl/defaultnode/GetNodePath.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/jungle/impl/defaultnode/GetNodePath.java Wed Feb 01 04:04:17 2017 +0900 @@ -42,6 +42,7 @@ Assert.assertTrue(iterator.hasNext()); TreeNode node = iterator.next(); + String aaa = node.getAttributes().getString(key); NodePath path = tree.getNodePath(node); String pathtring = path.toString(); Assert.assertEquals("<-1,2,0>",pathtring);
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/DataWriteBufferTest.java Fri Jan 27 01:09:43 2017 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/DataWriteBufferTest.java Wed Feb 01 04:04:17 2017 +0900 @@ -1,15 +1,14 @@ 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.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.Error.Error; import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodePath; @@ -32,7 +31,7 @@ @Test public void DataWriteBufferTest() throws IOException { PersistentJournal journal1 = new PersistentJournal(new File("./log/commit.log")); - Jungle jungle = new PersistentJungle(journal1, "uuid", new DefaultTreeEditor(new DefaultTraverser())); + Jungle jungle = new PersistentJungle(journal1, "uuid", new DefaultTraverser()); jungle.createNewTree("hoge"); JungleTree tree1 = jungle.getTreeByName("hoge"); tree1.setBufferSize(10); @@ -72,16 +71,17 @@ PersistentJournal journal3 = new PersistentJournal(); journal3.setInputFile(new File("./log/" + journal1.getLogName())); journal3.setOutputFile(new File("./log/" + journal1.getLogName())); - Jungle jungle2 = new PersistentJungle(journal1, "uuid3", new DefaultTreeEditor(new DefaultTraverser())); + Jungle jungle2 = new PersistentJungle(journal1, "uuid3", new DefaultTraverser()); ChangeListReader reader2 = journal3.getReader(); for (ChangeList chList : reader2) { String treeName = chList.getTreeName(); + int treeType = chList.getTreeType(); JungleTree tree2 = jungle2.getTreeByName(treeName); if (tree2 == null) { tree2 = jungle2.createNewTree(treeName); } editor2 = tree2.getJungleTreeEditor(); - Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList); + Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList,treeType); Assert.assertFalse(either2.isA()); editor2 = either2.b(); editor2.success();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/Persistent/PersistentDefaultJournalTest.java Wed Feb 01 04:04:17 2017 +0900 @@ -0,0 +1,83 @@ +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.Error.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(new File("./log/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("./log/" + journal1.getLogName())); + journal2.setOutputFile(new File("./log/" + 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); + assertFalse(either2.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 Wed Feb 01 04:04:17 2017 +0900 @@ -0,0 +1,110 @@ +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.Error.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(new File("./log/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("./log/" + journal1.getLogName())); + journal2.setOutputFile(new File("./log/" + 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); + assertFalse(either2.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/PersistentJournalTest.java Fri Jan 27 01:09:43 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +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.Error.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 PersistentJournalTest extends TestCase { - - public void testPersistentJounal() throws IOException { - PersistentJournal journal1 = new PersistentJournal(new File("./log/commit.log")); - Jungle jungle = new PersistentJungle(journal1, "uuid",new DefaultTreeEditor(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("./log/"+journal1.getLogName())); - journal2.setOutputFile(new File("./log/"+journal1.getLogName())); - Jungle jungle2 = new PersistentJungle(journal1, "uuid2", new DefaultTreeEditor(new DefaultTraverser())); - ChangeListReader reader = journal2.getReader(); - for (ChangeList chList : reader) { - String treeName = chList.getTreeName(); - JungleTree tree2 = jungle2.getTreeByName(treeName); - if(tree2 == null) { - tree2 = jungle2.createNewTree(treeName); - } - JungleTreeEditor editor2 = tree2.getJungleTreeEditor(); - Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList); - 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(); - 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(); - } - -}