changeset 231:48907bf43043

add PersistentJungleTreeEditor.java
author tatsuki
date Tue, 12 Jan 2016 22:46:09 +0900
parents a202f84bda52
children e2f7a8698c67
files src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/persistent/DataReadBenchmark.java src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/persistent/JungleLogCopyBenchMark.java src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/JungleTreeEditor.java src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/bbs/JungleBulletinBoard.java src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/DefaultJungleTreeEditor.java src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentJungleTreeEditor.java src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/JungleUpdater.java src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTreeEditor.java src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/DataWriteBufferTest.java
diffstat 9 files changed, 291 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/persistent/DataReadBenchmark.java	Tue Jan 12 22:46:09 2016 +0900
@@ -0,0 +1,87 @@
+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.JungleTree;
+import jp.ac.u_ryukyu.ie.cr.jungle.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;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultTreeEditor;
+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;
+import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodePath;
+import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJournal;
+import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJungle;
+import jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction.JungleUpdater;
+import org.junit.Assert;
+
+import java.io.*;
+import java.nio.ByteBuffer;
+
+public class DataReadBenchMark {
+
+    static ByteBuffer value = ByteBuffer.wrap("value".getBytes());
+    static String key = "key";
+    static int pos = 0;
+
+    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()));
+        Long t1;
+        Long t2;
+        PrintWriter readTimeWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File("./time/readTime"))));
+        PrintWriter writeTimeWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File("./time/writeTime"))));
+        for (int i = 1 ; i <= 20 ; i++) {
+            jungle.createNewTree("hoge" + i);
+            JungleTree tree1 = jungle.getTreeByName("hoge" + i);
+            JungleTreeEditor editor;
+            NetworkNodePath path = new NetworkNodePath();
+            t1 = System.currentTimeMillis();
+            for (int j = 0; j < 100 * i; j++) {
+                editor = tree1.getTreeEditor();
+                Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, pos);
+                NodePath childPath = path.add(pos);
+                Assert.assertFalse(either.isA());
+                editor = either.b();
+                either = editor.putAttribute(childPath, key, value);
+                JungleTreeEditor editor2 = either.b();
+                either = editor2.success();
+                Assert.assertFalse(either.isA());
+            }
+            t2 = System.currentTimeMillis();
+
+            writeTimeWriter.println((100 * i) + " " + (t2 - t1));
+            System.out.println("write time = " + (t2 - t1));
+
+            PersistentJournal journal2 = new PersistentJournal();
+            journal2.setInputFile(new File("./log/" + journal1.getLogName()));
+            journal2.setOutputFile(new File("./log/" + journal1.getLogName()));
+            ChangeListReader reader = journal2.getReader();
+
+            t1 = System.currentTimeMillis();
+            for (ChangeList chList : reader) {
+                String treeName = chList.getTreeName();
+                JungleTree tree2 = jungle2.getTreeByName(treeName);
+                if (tree2 == null) {
+                    tree2 = jungle2.createNewTree(treeName);
+                }
+                JungleTreeEditor editor2 = tree2.getTreeEditor();
+                Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList);
+                Assert.assertFalse(either2.isA());
+                editor2 = either2.b();
+                editor2.success();
+            }
+
+            t2 = System.currentTimeMillis();
+            readTimeWriter.println((100 * i) + " " + (t2 - t1));
+            System.out.println("read time = " + (t2 - t1));
+
+        }
+        writeTimeWriter.close();
+        readTimeWriter.close();
+        journal1.close();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/benchMark/persistent/JungleLogCopyBenchMark.java	Tue Jan 12 22:46:09 2016 +0900
@@ -0,0 +1,60 @@
+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.JungleTree;
+import jp.ac.u_ryukyu.ie.cr.jungle.JungleTreeEditor;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultTreeEditor;
+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.jungleNetwork.operations.NetworkNodePath;
+import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJournal;
+import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJungle;
+import org.junit.Assert;
+
+import java.io.*;
+import java.nio.ByteBuffer;
+
+/**
+ * Created by e115731 on 15/11/16.
+ */
+public class JungleLogCopyBenchMark {
+
+    static ByteBuffer value = ByteBuffer.wrap("value".getBytes());
+    static String key = "key";
+    static int pos = 0;
+
+    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()));
+        Long t1;
+        Long t2;
+        PrintWriter readTimeWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File("./time/copyTime"))));
+        jungle.createNewTree("hoge");
+        JungleTree tree1 = jungle.getTreeByName("hoge");
+        tree1.setBufferSize(2000);
+        JungleTreeEditor editor;
+        NetworkNodePath path = new NetworkNodePath();
+        int count = 0;
+        for (int j = 0; j < 2000; j++) {
+            count++;
+            editor = tree1.getTreeEditor();
+            Either<jp.ac.u_ryukyu.ie.cr.jungle.util.Error, JungleTreeEditor> either = editor.addNewChildAt(path, pos);
+            NodePath childPath = path.add(pos);
+            Assert.assertFalse(either.isA());
+            editor = either.b();
+            either = editor.putAttribute(childPath, key, value);
+            JungleTreeEditor editor2 = either.b();
+            t1 = System.currentTimeMillis();
+            editor2.success();
+            t2 = System.currentTimeMillis();
+            readTimeWriter.println(count + " " + (t2 - t1));
+            System.out.println(count + " = " + (t2 - t1));
+            Assert.assertFalse(either.isA());
+        }
+        readTimeWriter.close();
+        journal1.close();
+    }
+}
+
+
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/JungleTreeEditor.java	Tue Oct 06 16:15:31 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/JungleTreeEditor.java	Tue Jan 12 22:46:09 2016 +0900
@@ -17,6 +17,7 @@
     public Either<Error, JungleTreeEditor> replaceNewRootNode();
 	public Either<Error,JungleTreeEditor> edit(NodePath path,NodeEditor editor);
 	public Either<Error,JungleTreeEditor> success();
-    public Either<Error,JungleTreeEditor> flashSuccess();
+    public Either<Error,JungleTreeEditor> flushSuccess();
+
 
 }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/bbs/JungleBulletinBoard.java	Tue Oct 06 16:15:31 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/bbs/JungleBulletinBoard.java	Tue Jan 12 22:46:09 2016 +0900
@@ -101,9 +101,6 @@
 				throw new IllegalStateException();
 			}
 			editor = either.b();
-
-
-
 			editor = editor.putAttribute(path.add(size),"author", ByteBuffer.wrap(_author.getBytes())).b();
 			editor = editor.putAttribute(path.add(size),"key", ByteBuffer.wrap(_editKey.getBytes())).b();
 			either = editor.putAttribute(path.add(size),"mes", ByteBuffer.wrap(_message.getBytes()));
@@ -145,10 +142,8 @@
 		IterableConverter.Converter<BoardMessage, TreeNode> converter = new IterableConverter.Converter<BoardMessage, TreeNode>() {
 			public BoardMessage conv(TreeNode _b) {
 				String uuid = Integer.toString(counter.get());
-				String author = new String(_b.getAttributes().get("author")
-						.array());
-				String message = new String(_b.getAttributes().get("mes")
-						.array());
+				String author = new String(_b.getAttributes().get("author").array());
+				String message = new String(_b.getAttributes().get("mes").array());
 				counter.incrementAndGet();
 				return new BoardMessageImpl(author, message, uuid);
 			}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/DefaultJungleTreeEditor.java	Tue Oct 06 16:15:31 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/DefaultJungleTreeEditor.java	Tue Jan 12 22:46:09 2016 +0900
@@ -125,7 +125,7 @@
 	}
 
     @Override
-    public Either<Error, JungleTreeEditor> flashSuccess() {
+    public Either<Error, JungleTreeEditor> flushSuccess() {
         return success();
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentJungleTreeEditor.java	Tue Jan 12 22:46:09 2016 +0900
@@ -0,0 +1,133 @@
+package jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent;
+
+import jp.ac.u_ryukyu.ie.cr.jungle.JungleTreeEditor;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeEditor;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultNodePath;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.DefaultTreeOperationLog;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.LoggingNode;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.OperationLog;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.TreeOperationLog;
+import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.DefaultTreeOperation;
+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.store.trasnformer.*;
+import jp.ac.u_ryukyu.ie.cr.jungle.transaction.TransactionManager;
+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;
+import jp.ac.u_ryukyu.ie.cr.jungle.util.IterableConverter;
+
+import java.nio.ByteBuffer;
+
+public class PersistentJungleTreeEditor implements JungleTreeEditor {
+    private final TransactionManager txManager;
+    private final TreeNode root;
+    private final TreeEditor editor;
+    private final TreeOperationLog log;
+
+
+    public PersistentJungleTreeEditor(TreeNode _root, TransactionManager _txManager, TreeEditor _editor) {
+        this(_root, _txManager, _editor, new DefaultTreeOperationLog());
+    }
+
+
+    public PersistentJungleTreeEditor(TreeNode newNode, TransactionManager _txManager, TreeEditor _editor, TreeOperationLog _log) {
+        this.root = newNode;
+        this.txManager = _txManager;
+        this.editor = _editor;
+        this.log = _log;
+    }
+
+    private Either<jp.ac.u_ryukyu.ie.cr.jungle.util.Error, JungleTreeEditor> _edit(final NodePath _path, NodeEditor _e) {
+        Either<jp.ac.u_ryukyu.ie.cr.jungle.util.Error, LoggingNode> either = editor.edit(root, _path, _e);
+        if (either.isA()) {
+            return DefaultEither.newA(either.a());
+        }
+
+        LoggingNode newLogging = either.b();
+        OperationLog newLog = newLogging.getOperationLog();
+        TreeNode newNode = newLogging.getWrap();
+
+        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<>(newLog, converter);
+        DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog(iterable, newLog.length());
+        TreeOperationLog newTreeOpLog = log.append(treeOperationLog);
+
+        JungleTreeEditor newEditor = new PersistentJungleTreeEditor(newNode, txManager, editor, newTreeOpLog);
+        return DefaultEither.newB(newEditor);
+    }
+
+    @Override
+    public Either<Error, JungleTreeEditor> replaceNewRootNode() {
+        replaceRootNodeAt appendChildAt = new replaceRootNodeAt();
+        return _edit(new DefaultNodePath(), appendChildAt);
+    }
+
+    @Override
+    public Either<Error, JungleTreeEditor> addNewChildAt(NodePath _path, int _pos) {
+        AppendChildAt appendChildAt = new AppendChildAt(_pos);
+        return _edit(_path, appendChildAt);
+    }
+
+    @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> deleteAttribute(NodePath _path, String _key) {
+        DeleteAttribute deleteAttribute = new DeleteAttribute(_key);
+        return _edit(_path, deleteAttribute);
+    }
+
+    @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);
+        if (either.isA()) {
+            return DefaultEither.newA(either.a());
+        }
+
+        TransactionManager newTxManager = either.b();
+
+        JungleTreeEditor newTreeEditor;
+        newTreeEditor = new PersistentJungleTreeEditor(root, newTxManager, editor, log);
+        return DefaultEither.newB(newTreeEditor);
+    }
+
+    @Override
+    public Either<Error, JungleTreeEditor> flushSuccess() {
+        Either<Error, TransactionManager> either = txManager.flashCommit(root, log);
+        if (either.isA()) {
+            return DefaultEither.newA(either.a());
+        }
+
+        TransactionManager newTxManager = either.b();
+
+        JungleTreeEditor newTreeEditor;
+        newTreeEditor = new PersistentJungleTreeEditor(root, newTxManager, editor, log);
+        return DefaultEither.newB(newTreeEditor);
+    }
+
+
+}
+
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/JungleUpdater.java	Tue Oct 06 16:15:31 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/JungleUpdater.java	Tue Jan 12 22:46:09 2016 +0900
@@ -55,12 +55,10 @@
 				NodeOperation nodeOp, int pos) {
 			String key = "";
 			Command c = nodeOp.getCommand();
-			System.out.println("path = " + path.toString());
 		switch (c) {
 		case PUT_ATTRIBUTE:
 			key = nodeOp.getKey();
 			ByteBuffer value = nodeOp.getValue();
-			System.out.println("key = " + key + " : " + "value = " + new String(value.array()));
 			return editor.putAttribute(path, key, value);
 		case DELETE_ATTRIBUTE:
 			key = nodeOp.getKey();
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTreeEditor.java	Tue Oct 06 16:15:31 2015 +0900
+++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTreeEditor.java	Tue Jan 12 22:46:09 2016 +0900
@@ -148,7 +148,7 @@
 	}
 
     @Override
-    public Either<Error, JungleTreeEditor> flashSuccess() {
+    public Either<Error, JungleTreeEditor> flushSuccess() {
         return success();
     }
 
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/DataWriteBufferTest.java	Tue Oct 06 16:15:31 2015 +0900
+++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/DataWriteBufferTest.java	Tue Jan 12 22:46:09 2016 +0900
@@ -24,9 +24,6 @@
 import java.nio.ByteBuffer;
 import java.util.Iterator;
 
-/**
- * Created by e115731 on 15/10/06.
- */
 public class DataWriteBufferTest {
     ByteBuffer value = ByteBuffer.wrap("value".getBytes());
     String key = "key";
@@ -53,7 +50,6 @@
         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();
         Iterator<ChangeList> iterator = reader.iterator();
         Assert.assertFalse(iterator.hasNext());
@@ -70,19 +66,19 @@
             Assert.assertFalse(either.isA());
         }
         editor = tree1.getTreeEditor();
-        either = editor.flashSuccess();
+        either = editor.flushSuccess();
         Assert.assertFalse(either.isA());
 
         PersistentJournal journal3 = new PersistentJournal();
         journal3.setInputFile(new File("./log/" + journal1.getLogName()));
         journal3.setOutputFile(new File("./log/" + journal1.getLogName()));
-        Jungle jungle3 = new PersistentJungle(journal1, "uuid2", new DefaultTreeEditor(new DefaultTraverser()));
+        Jungle jungle2 = new PersistentJungle(journal1, "uuid3", new DefaultTreeEditor(new DefaultTraverser()));
         ChangeListReader reader2 = journal3.getReader();
         for (ChangeList chList : reader2) {
             String treeName = chList.getTreeName();
-            JungleTree tree2 = jungle3.getTreeByName(treeName);
+            JungleTree tree2 = jungle2.getTreeByName(treeName);
             if (tree2 == null) {
-                tree2 = jungle3.createNewTree(treeName);
+                tree2 = jungle2.createNewTree(treeName);
             }
             editor2 = tree2.getTreeEditor();
             Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList);
@@ -91,7 +87,7 @@
             editor2.success();
         }
 
-        JungleTree tree2 = jungle3.getTreeByName("hoge");
+        JungleTree tree2 = jungle2.getTreeByName("hoge");
         TreeNode node1 = tree1.getRootNode();
         TreeNode node2 = tree2.getRootNode();
         Children child1 = node1.getChildren();