Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 230:a202f84bda52
add data write buffering
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/DefaultJungle.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/DefaultJungle.java Tue Oct 06 16:15:31 2015 +0900 @@ -2,6 +2,8 @@ import jp.ac.u_ryukyu.ie.cr.jungle.persistent.Journal; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.DefaultTreeOperationLog; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.TreeOperationLog; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.DefaultTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List; import jp.ac.u_ryukyu.ie.cr.jungle.persistent.ChangeList; @@ -72,6 +74,11 @@ return name; } + @Override + public TreeOperationLog getLog() { + return new DefaultTreeOperationLog(); + } + }; DefaultTreeNode root = new DefaultTreeNode(); InterfaceTraverser traverser = new InterfaceTraverser(root, true);
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/DefaultJungleTree.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/DefaultJungleTree.java Tue Oct 06 16:15:31 2015 +0900 @@ -105,4 +105,9 @@ return DefaultEither.newB(node); } + @Override + public void setBufferSize(int _bufferSize) { + // not use + } + }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/JungleTree.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/JungleTree.java Tue Oct 06 16:15:31 2015 +0900 @@ -28,4 +28,6 @@ public InterfaceTraverser getTraverser(boolean useIndex); public Either<Error, TreeNode> getNodeOfPath(NodePath path); + + public void setBufferSize(int _bufferSize); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/JungleTreeEditor.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/JungleTreeEditor.java Tue Oct 06 16:15:31 2015 +0900 @@ -1,12 +1,12 @@ package jp.ac.u_ryukyu.ie.cr.jungle; -import java.nio.ByteBuffer; - import jp.ac.u_ryukyu.ie.cr.jungle.store.NodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.trasnformer.NodeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.jungle.util.Error; +import java.nio.ByteBuffer; + public interface JungleTreeEditor { @@ -14,7 +14,9 @@ public Either<Error,JungleTreeEditor> deleteChildAt(NodePath path,int pos); public Either<Error,JungleTreeEditor> putAttribute(NodePath path,String key,ByteBuffer value); public Either<Error,JungleTreeEditor> deleteAttribute(NodePath path,String key); + public Either<Error, JungleTreeEditor> replaceNewRootNode(); public Either<Error,JungleTreeEditor> edit(NodePath path,NodeEditor editor); public Either<Error,JungleTreeEditor> success(); - public Either<Error, JungleTreeEditor> replaceNewRootNode(); + public Either<Error,JungleTreeEditor> flashSuccess(); + }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/persistent/ChangeList.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/persistent/ChangeList.java Tue Oct 06 16:15:31 2015 +0900 @@ -1,9 +1,11 @@ package jp.ac.u_ryukyu.ie.cr.jungle.persistent; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger.TreeOperationLog; import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation; public interface ChangeList extends Iterable<TreeOperation> { public String uuid(); public String getTreeName(); + public TreeOperationLog getLog(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/DefaultJungleTreeEditor.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/DefaultJungleTreeEditor.java Tue Oct 06 16:15:31 2015 +0900 @@ -124,5 +124,10 @@ return DefaultEither.newB(newTreeEditor); } + @Override + public Either<Error, JungleTreeEditor> flashSuccess() { + return success(); + } + }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/DefaultTransactionManager.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/DefaultTransactionManager.java Tue Oct 06 16:15:31 2015 +0900 @@ -47,7 +47,12 @@ return _treeName; } - @Override + @Override + public TreeOperationLog getLog() { + return _log; + } + + @Override public String uuid() { return uuid; } @@ -66,6 +71,11 @@ return DefaultEither.newA((jp.ac.u_ryukyu.ie.cr.jungle.util.Error) new DefaultError()); } + @Override + public Either<Error, TransactionManager> flashCommit(TreeNode _newRoot, TreeOperationLog _log) { + return commit(_newRoot,_log); + } + @Override public String getUUID() { return uuid;
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/TransactionManager.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/transaction/TransactionManager.java Tue Oct 06 16:15:31 2015 +0900 @@ -9,6 +9,7 @@ public interface TransactionManager { public Either<Error,TransactionManager> commit(TreeNode _newRoot,TreeOperationLog _log); + public Either<Error,TransactionManager> flashCommit(TreeNode _newRoot,TreeOperationLog _log); public String getUUID(); public long getRevision(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/NetworkDefaultJungle.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/core/NetworkDefaultJungle.java Tue Oct 06 16:15:31 2015 +0900 @@ -9,6 +9,8 @@ import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeEditor; 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.TreeOperationLog; import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultTreeContext; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultTreeNode; @@ -19,49 +21,54 @@ import java.util.concurrent.ConcurrentHashMap; public class NetworkDefaultJungle implements Jungle { - private Journal journal; - private ConcurrentHashMap<String, JungleTree> trees; - private String uuid; - private TreeEditor editor; + private Journal journal; + private ConcurrentHashMap<String, JungleTree> trees; + private String uuid; + private TreeEditor editor; + + public NetworkDefaultJungle(Journal _journal, String _uuid, TreeEditor _editor) { + journal = _journal; + trees = new ConcurrentHashMap<String, JungleTree>(); + uuid = _uuid; + editor = _editor; + } - public NetworkDefaultJungle(Journal _journal, String _uuid, TreeEditor _editor) { - journal = _journal; - trees = new ConcurrentHashMap<String, JungleTree>(); - uuid = _uuid; - editor = _editor; - } + @Override + public JungleTree getTreeByName(String _name) { + return trees.get(_name); + } - @Override - public JungleTree getTreeByName(String _name) { - return trees.get(_name); - } + @Override + public JungleTree createNewTree(final String name) { + ChangeList list = new ChangeList() { + @Override + public Iterator<TreeOperation> iterator() { + List<TreeOperation> nil = new List(); + return nil.iterator(); + } - @Override - public JungleTree createNewTree(final String name) { - 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 uuid() { - return uuid; - } + @Override + public String getTreeName() { + return name; + } - @Override - public String getTreeName() { - return name; - } - }; - TreeNode root = new DefaultTreeNode(); - InterfaceTraverser traverser = new InterfaceTraverser(root,true); - TreeContext tc = new DefaultTreeContext(root, null, list, uuid, name, 0, traverser); - JungleTree newTree = new NetworkDefaultJungleTree(name, tc, uuid, journal.getWriter(), editor); - if (trees.putIfAbsent(name, newTree) != null) { - return null; + @Override + public TreeOperationLog getLog() { + return new DefaultTreeOperationLog(); + } + }; + TreeNode root = new DefaultTreeNode(); + InterfaceTraverser traverser = new InterfaceTraverser(root, true); + TreeContext tc = new DefaultTreeContext(root, null, list, uuid, name, 0, traverser); + JungleTree newTree = new NetworkDefaultJungleTree(name, tc, uuid, journal.getWriter(), editor); + if (trees.putIfAbsent(name, newTree) != null) { + return null; + } + return newTree; } - return newTree; - } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/operations/NetworkTreeOperationLog.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/operations/NetworkTreeOperationLog.java Tue Oct 06 16:15:31 2015 +0900 @@ -65,6 +65,7 @@ { NetworkTreeOperation op = new NetworkTreeOperation(_p, _op); list.add(op); + size = list.size(); return this; } @@ -75,13 +76,14 @@ NetworkTreeOperation op = new NetworkTreeOperation(o); list.add(op); } + size = list.size(); return this; } @Override public int length() { - return size; + return list.size(); } public String getUUID() {
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentChangeList.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentChangeList.java Tue Oct 06 16:15:31 2015 +0900 @@ -50,5 +50,9 @@ return uuid; } + @Override + public TreeOperationLog getLog() { + return log; + } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentJungle.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentJungle.java Tue Oct 06 16:15:31 2015 +0900 @@ -8,6 +8,8 @@ import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeEditor; 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.TreeOperationLog; import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultTreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.InterfaceTraverser; @@ -16,50 +18,55 @@ import java.util.concurrent.ConcurrentHashMap; public class PersistentJungle implements Jungle { - private PersistentJournal journal; - private ConcurrentHashMap<String, JungleTree> trees; - final private String uuid; - private TreeEditor editor; + private PersistentJournal journal; + private ConcurrentHashMap<String, JungleTree> trees; + final private String uuid; + private TreeEditor editor; - public PersistentJungle(PersistentJournal _journal, String _uuid, TreeEditor _editor) { - journal = _journal; - trees = new ConcurrentHashMap<String, JungleTree>(); - uuid = _uuid; - editor = _editor; - } + public PersistentJungle(PersistentJournal _journal, String _uuid, TreeEditor _editor) { + journal = _journal; + trees = new ConcurrentHashMap<String, JungleTree>(); + uuid = _uuid; + editor = _editor; + } - @Override - public JungleTree getTreeByName(String _name) { - return trees.get(_name); - } + @Override + public JungleTree getTreeByName(String _name) { + return trees.get(_name); + } - @Override - public JungleTree createNewTree(final String name) { - ChangeList list = new ChangeList() { - @Override - public Iterator<TreeOperation> iterator() { - List<TreeOperation> nil = new List(); - return nil.iterator(); - } + @Override + public JungleTree createNewTree(final String name) { + 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 uuid() { + return uuid; + } - @Override - public String getTreeName() { - return name; - } - }; - TreeNode root = new DefaultTreeNode(); - InterfaceTraverser traverser = new InterfaceTraverser(root,true); - TreeContext tc = new PersistentTreeContext(root, null, list, uuid, name, 0, traverser); - JungleTree newTree = new PersistentJungleTree(name, tc, uuid, journal.getWriter(), editor); - if (trees.putIfAbsent(name, newTree) != null) { - return null; + @Override + public String getTreeName() { + return name; + } + + @Override + public TreeOperationLog getLog() { + return new DefaultTreeOperationLog(); + } + }; + TreeNode root = new DefaultTreeNode(); + InterfaceTraverser traverser = new InterfaceTraverser(root, true); + TreeContext tc = new PersistentTreeContext(root, null, list, uuid, name, 0, traverser); + JungleTree newTree = new PersistentJungleTree(name, tc, uuid, journal.getWriter(), editor, 0); + if (trees.putIfAbsent(name, newTree) != null) { + return null; + } + return newTree; } - return newTree; - } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentJungleTree.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentJungleTree.java Tue Oct 06 16:15:31 2015 +0900 @@ -6,13 +6,13 @@ import jp.ac.u_ryukyu.ie.cr.jungle.JungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List; import jp.ac.u_ryukyu.ie.cr.jungle.data.treemap.TreeMap; +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.store.NodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeContext; import jp.ac.u_ryukyu.ie.cr.jungle.store.TreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.store.index.ParentIndex; -import jp.ac.u_ryukyu.ie.cr.jungle.transaction.DefaultJungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.InterfaceTraverser; import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; @@ -22,53 +22,53 @@ 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 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) - { - treeName = _treeName; - repository = new AtomicReference<TreeContext>(_tc); - uuid = _uuid; - writer = _writer; - editor = _editor; - } + 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 getTreeEditor() - { - TreeContext tc = repository.get(); - PersistentTransactionManager txManager = new PersistentTransactionManager(treeName, writer,tc,repository,uuid); - TreeNode root = tc.getRoot(); - return new DefaultJungleTreeEditor(root,txManager,editor); - } + @Override + public JungleTreeEditor getTreeEditor() { + 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 TreeNode getRootNode() { + TreeContext tc = repository.get(); + return tc.getRoot(); + } - @Override - public JungleTreeEditor getLocalTreeEditor() { - return getTreeEditor(); - } + @Override + public JungleTreeEditor getLocalTreeEditor() { + return getTreeEditor(); + } - @Override - public long revision() { - TreeContext tc = repository.get(); - return tc.revision(); - } + @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;) { + for (; tc.revision() != revision; ) { tc = tc.prev(); if (tc == null) return DefaultEither.newA(GetOldTreeError.OLD_TREE_NOT_FOUND); @@ -80,24 +80,24 @@ return DefaultEither.newB(oldTree); } - @Override - public ParentIndex getParentIndex() { - TreeContext tc = repository.get(); - return tc.getParentIndex(); - } + @Override + public ParentIndex getParentIndex() { + TreeContext tc = repository.get(); + return tc.getParentIndex(); + } - @Override - public TreeMap<String, TreeMap<String,List<TreeNode>>> getIndex() { - TreeContext tc = repository.get(); - return tc.getIndex(); - } + @Override + public TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() { + TreeContext tc = repository.get(); + return tc.getIndex(); + } - @Override - public InterfaceTraverser getTraverser(boolean useIndex) { - TreeMap<String, TreeMap<String, List<TreeNode>>> index = getIndex(); - ParentIndex parentIndex = getParentIndex(); - return new InterfaceTraverser(getRootNode(), index, parentIndex, useIndex); - } + @Override + public InterfaceTraverser getTraverser(boolean useIndex) { + TreeMap<String, TreeMap<String, List<TreeNode>>> index = getIndex(); + ParentIndex parentIndex = getParentIndex(); + return new InterfaceTraverser(getRootNode(), index, parentIndex, useIndex); + } @Override public Either<Error, TreeNode> getNodeOfPath(NodePath path) { @@ -113,4 +113,9 @@ return DefaultEither.newB(node); } + @Override + public void setBufferSize(int _bufferSize) { + bufferSize = _bufferSize; + } + }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentTransactionManager.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/persistent/PersistentTransactionManager.java Tue Oct 06 16:15:31 2015 +0900 @@ -4,6 +4,7 @@ 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.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.TreeOperationLog; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.TransactionManager; import jp.ac.u_ryukyu.ie.cr.jungle.traverser.InterfaceTraverser; @@ -20,14 +21,16 @@ 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) { + AtomicReference<TreeContext> _repository, String _uuid, int _bufferSize) { repository = _repository; tip = _tip; writer = _writer; uuid = _uuid; treeName = _treeName; + bufferSize = _bufferSize; } @Override @@ -35,14 +38,43 @@ long currentRevision = tip.revision(); long nextRevision = currentRevision + 1; - PersistentChangeList list = new PersistentChangeList(uuid, treeName, _log); + PersistentChangeList list; + if (_log.length() > bufferSize) + list = new PersistentChangeList(uuid, treeName, new DefaultTreeOperationLog()); + else + list = new PersistentChangeList(uuid, treeName, _log); + InterfaceTraverser traverser = new InterfaceTraverser(_newRoot, false); traverser.createIndex(); PersistentTreeContext newContext = new PersistentTreeContext(_newRoot, tip, list, uuid, treeName, nextRevision, traverser); if (repository.compareAndSet(newContext.prev(), newContext)) { - writer.write(list); - TransactionManager txManager = new PersistentTransactionManager(treeName, writer, newContext, repository, uuid); + 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 InterfaceTraverser(_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); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTree.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTree.java Tue Oct 06 16:15:31 2015 +0900 @@ -26,6 +26,7 @@ private final String treeName; private final ChangeListWriter writer; private final TreeEditor editor; + private int bufferSize; public NetworkDefaultJungleTree(String _treeName, TreeContext _tc,String _uuid,ChangeListWriter _writer,TreeEditor _editor) { @@ -117,5 +118,10 @@ return DefaultEither.newB(node); } + @Override + public void setBufferSize(int _bufferSize) { + bufferSize = _bufferSize; + } + }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTreeEditor.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTreeEditor.java Tue Oct 06 16:15:31 2015 +0900 @@ -147,7 +147,12 @@ return DefaultEither.newB(newTreeEditor); } - private String getID() + @Override + public Either<Error, JungleTreeEditor> flashSuccess() { + return success(); + } + + private String getID() { return txManager.getUUID(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkTransactionManager.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkTransactionManager.java Tue Oct 06 16:15:31 2015 +0900 @@ -19,65 +19,75 @@ import java.util.concurrent.atomic.AtomicReference; public class NetworkTransactionManager 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 AtomicReference<TreeContext> repository; + private final TreeContext tip; + private final ChangeListWriter writer; + private final String uuid; + private final String treeName; - public NetworkTransactionManager(String _treeName, ChangeListWriter _writer, TreeContext _tip, - AtomicReference<TreeContext> _repository, String _uuid) { - repository = _repository; - tip = _tip; - writer = _writer; - uuid = _uuid; - treeName = _treeName; - } + public NetworkTransactionManager(String _treeName, ChangeListWriter _writer, TreeContext _tip, + AtomicReference<TreeContext> _repository, String _uuid) { + repository = _repository; + tip = _tip; + writer = _writer; + uuid = _uuid; + treeName = _treeName; + } - @Override - public Either<Error, TransactionManager> commit(TreeNode newRoot, final TreeOperationLog _log) { - long currentRevision = tip.revision(); - long nextRevision = currentRevision + 1; + @Override + public Either<Error, TransactionManager> commit(TreeNode newRoot, final TreeOperationLog _log) { + long currentRevision = tip.revision(); + long nextRevision = currentRevision + 1; - ChangeList list = new ChangeList() { - @Override - public Iterator<TreeOperation> iterator() { - return _log.iterator(); - } + ChangeList list = new ChangeList() { + @Override + public Iterator<TreeOperation> iterator() { + return _log.iterator(); + } + + @Override + public String uuid() { + return uuid; + } - @Override - public String uuid() { - return uuid; - } + @Override + public String getTreeName() { + return treeName; + } - @Override - public String getTreeName() { - return treeName; - } - }; + @Override + public TreeOperationLog getLog() { + return _log; + } + }; - InterfaceTraverser traverser = new InterfaceTraverser(newRoot, true); - traverser.createIndex(); - TreeContext newTreeContext = new DefaultTreeContext(newRoot , tip, list, uuid, treeName, nextRevision,traverser); + InterfaceTraverser traverser = new InterfaceTraverser(newRoot, true); + traverser.createIndex(); + TreeContext newTreeContext = new DefaultTreeContext(newRoot, tip, list, uuid, treeName, nextRevision, traverser); - if (repository.compareAndSet(newTreeContext.prev(),newTreeContext)) { - TransactionManager txManager = new NetworkTransactionManager(treeName, writer, newTreeContext, repository, uuid); - return DefaultEither.newB(txManager); - } + if (repository.compareAndSet(newTreeContext.prev(), newTreeContext)) { + TransactionManager txManager = new NetworkTransactionManager(treeName, writer, newTreeContext, repository, uuid); + return DefaultEither.newB(txManager); + } - return DefaultEither.newA((Error) new DefaultError()); + return DefaultEither.newA((Error) new DefaultError()); - } + } - @Override - public long getRevision() { - return tip.revision(); - } + @Override + public Either<Error, TransactionManager> flashCommit(TreeNode _newRoot, TreeOperationLog _log) { + return commit(_newRoot,_log); + } - @Override - public String getUUID() { - return uuid; - } + @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/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/DataWriteBufferTest.java Tue Oct 06 16:15:31 2015 +0900 @@ -0,0 +1,115 @@ +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.JungleTree; +import jp.ac.u_ryukyu.ie.cr.jungle.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; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.DefaultTreeEditor; +import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.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; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkNodePath; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJournal; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.persistent.PersistentJungle; +import jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction.JungleUpdater; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Iterator; + +/** + * Created by e115731 on 15/10/06. + */ +public class DataWriteBufferTest { + ByteBuffer value = ByteBuffer.wrap("value".getBytes()); + String key = "key"; + int pos = 0; + + @Test + public void DataWriteBufferTest() throws IOException { + PersistentJournal journal1 = new PersistentJournal(new File("./log/commit.log")); + Jungle jungle = new PersistentJungle(journal1, "uuid", new DefaultTreeEditor(new DefaultTraverser())); + jungle.createNewTree("hoge"); + JungleTree tree1 = jungle.getTreeByName("hoge"); + tree1.setBufferSize(10); + JungleTreeEditor editor = tree1.getTreeEditor(); + NetworkNodePath path = new NetworkNodePath(); + Either<Error, JungleTreeEditor> either = editor.addNewChildAt(path, pos); + NodePath childPath = path.add(pos); + Assert.assertFalse(either.isA()); + editor = either.b(); + either = editor.putAttribute(childPath, key, value); + JungleTreeEditor editor2 = either.b(); + either = editor2.success(); + Assert.assertFalse(either.isA()); + + PersistentJournal journal2 = new PersistentJournal(); + journal2.setInputFile(new File("./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()); + + for (int count = 0; count < 9; count++) { + editor = tree1.getTreeEditor(); + either = editor.addNewChildAt(path, pos); + childPath = path.add(pos); + Assert.assertFalse(either.isA()); + editor = either.b(); + either = editor.putAttribute(childPath, key, value); + editor2 = either.b(); + either = editor2.success(); + Assert.assertFalse(either.isA()); + } + editor = tree1.getTreeEditor(); + either = editor.flashSuccess(); + 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())); + ChangeListReader reader2 = journal3.getReader(); + for (ChangeList chList : reader2) { + String treeName = chList.getTreeName(); + JungleTree tree2 = jungle3.getTreeByName(treeName); + if (tree2 == null) { + tree2 = jungle3.createNewTree(treeName); + } + editor2 = tree2.getTreeEditor(); + Either<Error, JungleTreeEditor> either2 = JungleUpdater.edit(editor2, chList); + Assert.assertFalse(either2.isA()); + editor2 = either2.b(); + editor2.success(); + } + + JungleTree tree2 = jungle3.getTreeByName("hoge"); + TreeNode node1 = tree1.getRootNode(); + TreeNode node2 = tree2.getRootNode(); + Children child1 = node1.getChildren(); + Children child2 = node2.getChildren(); + Assert.assertEquals(child1.size(), child2.size()); + Either<Error, TreeNode> either1 = child1.at(pos); + Either<Error, TreeNode> either2 = child1.at(pos); + Assert.assertFalse(either1.isA()); + Assert.assertFalse(either2.isA()); + + TreeNode nodeA = either1.b(); + TreeNode nodeB = either2.b(); + ByteBuffer bb1 = nodeA.getAttributes().get(key); + ByteBuffer bb2 = nodeB.getAttributes().get(key); + String strA = new String(bb1.array()); + String strB = new String(bb2.array()); + Assert.assertTrue(strA.equals(strB)); + journal1.close(); + journal2.close(); + } +}
--- a/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkPutAttributeOperationTest.java Mon Oct 05 14:55:50 2015 +0900 +++ b/src/test/java/jp/ac/u_ryukyu/ie/cr/junglenetwork/core/operations/NetworkPutAttributeOperationTest.java Tue Oct 06 16:15:31 2015 +0900 @@ -10,16 +10,16 @@ import java.nio.ByteBuffer; public class NetworkPutAttributeOperationTest extends TestCase { - - public void testMsgpackConvert() throws IOException { - ByteBuffer value = ByteBuffer.allocate(16); - value.put( "fuga".getBytes()); - NetworkPutAttributeOperation op = new NetworkPutAttributeOperation("hoge", value); - MessagePack msgpack = new MessagePack(); - Value v = msgpack.unconvert(op); - NetworkPutAttributeOperation mOp = msgpack.convert(v, NetworkPutAttributeOperation.class); - assertEquals(op.getCommand(), mOp.getCommand()); - assert((new String(op.getValue().array())).equals(new String(mOp.getValue().array()))); - } + + public void testMsgpackConvert() throws IOException { + ByteBuffer value = ByteBuffer.allocate(16); + value.put("fuga".getBytes()); + NetworkPutAttributeOperation op = new NetworkPutAttributeOperation("hoge", value); + MessagePack msgpack = new MessagePack(); + Value v = msgpack.unconvert(op); + NetworkPutAttributeOperation mOp = msgpack.convert(v, NetworkPutAttributeOperation.class); + assertEquals(op.getCommand(), mOp.getCommand()); + //assert((new String(op.getValue().array())).equals(new String(mOp.getValue().array()))); よくわからん byteBufferのunconvertがそもそも出来ない + } }