Mercurial > hg > Members > nobuyasu > jungle-network
changeset 12:80680cfcfd3b
add jungle/test/transaction, jungle/test/core/NetworkDefaultJungle
author | one |
---|---|
date | Wed, 26 Jun 2013 19:03:19 +0900 |
parents | c9ff68c4d82f |
children | f3c0a65c3f12 |
files | src/jungle/test/core/NetworkDefaultJungle.java src/jungle/test/transaction/NetworkDefaultJungleTree.java src/jungle/test/transaction/NetworkDefaultJungleTreeEditor.java |
diffstat | 3 files changed, 252 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/core/NetworkDefaultJungle.java Wed Jun 26 19:03:19 2013 +0900 @@ -0,0 +1,62 @@ +package jungle.test.core; + +import java.util.Iterator; +import java.util.concurrent.ConcurrentHashMap; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeList; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.Journal; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.NullJournal; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.ChangeSet; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultChangeSet; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultTreeContext; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultTreeNode; +import jungle.test.transaction.NetworkDefaultJungleTree; +import fj.data.List; + +public class NetworkDefaultJungle implements Jungle { + private Journal journal; + private ConcurrentHashMap<String,JungleTree> trees; + private String uuid; + private TreeEditor editor; + + public NetworkDefaultJungle(Journal _journal,String _uuid,TreeEditor _editor) + { + journal = new NullJournal(); + trees = new ConcurrentHashMap<String,JungleTree>(); + uuid = _uuid; + editor = _editor; + } + + @Override + public JungleTree getTreeByName(String _name) + { + return trees.get(_name); + } + + @Override + public JungleTree createNewTree(String _name) + { + ChangeList list = new ChangeList(){ + @Override + public Iterator<TreeOperation> iterator() { + List<TreeOperation> nil = List.nil(); + return nil.iterator(); + } + }; + + DefaultTreeNode root = new DefaultTreeNode(); + ChangeSet set = new DefaultChangeSet(root.getAsNode(),null,list,uuid,0); + DefaultTreeContext<DefaultTreeNode> tc = new DefaultTreeContext<DefaultTreeNode>(root,set); + JungleTree newTree = new NetworkDefaultJungleTree<DefaultTreeNode>(tc,uuid,journal.getWriter(),editor); + if(trees.putIfAbsent(_name,newTree) != null){ + return null; + } + return newTree; + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/transaction/NetworkDefaultJungleTree.java Wed Jun 26 19:03:19 2013 +0900 @@ -0,0 +1,44 @@ +package jungle.test.transaction; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.persistent.ChangeListWriter; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.ChangeSet; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.AtomicReservableReference; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultTransactionManager; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TreeContext; + +public class NetworkDefaultJungleTree<T extends TreeNode<T>> implements JungleTree { + private final AtomicReservableReference<TreeContext<T>> repository; + private final String uuid; + private final ChangeListWriter writer; + private final TreeEditor editor; + + public NetworkDefaultJungleTree(TreeContext<T> _tc,String _uuid,ChangeListWriter _writer,TreeEditor _editor) + { + repository = new AtomicReservableReference<TreeContext<T>>(_tc); + uuid = _uuid; + writer = _writer; + editor = _editor; + } + + @Override + public JungleTreeEditor getTreeEditor() + { + TreeContext<T> tc = repository.get(); + DefaultTransactionManager<T> txManager = new DefaultTransactionManager<T>(writer,tc,repository,uuid); + T root = tc.getTreeNode(); + return new NetworkDefaultJungleTreeEditor<T>(root,txManager,editor); + } + + @Override + public Node getRootNode() + { + TreeContext<T> tc = repository.get(); + ChangeSet cs = tc.getChangeSet(); + return cs.getRoot(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/jungle/test/transaction/NetworkDefaultJungleTreeEditor.java Wed Jun 26 19:03:19 2013 +0900 @@ -0,0 +1,146 @@ +package jungle.test.transaction; + +import java.nio.ByteBuffer; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.LoggingNodeHook; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.OperationLog; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DefaultTreeOperation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.AppendChildAt; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteAttribute; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteChildAt; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.PutAttribute; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultJungleTreeEditor; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TransactionManager; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; + +public class NetworkDefaultJungleTreeEditor<T extends TreeNode<T>> implements JungleTreeEditor { + + private final TransactionManager<T> txManager; + private final T root; + private final TreeEditor editor; + private final TreeOperationLog log; + + public NetworkDefaultJungleTreeEditor(T _root,TransactionManager<T> _txManager,TreeEditor _editor) + { + this(_root,_txManager,_editor,new DefaultTreeOperationLog()); + } + + public NetworkDefaultJungleTreeEditor(T _root,TransactionManager<T> _txManager,TreeEditor _editor,TreeOperationLog _log) + { + root = _root; + txManager = _txManager; + editor = _editor; + log = _log; + } + + private Either<Error,JungleTreeEditor> _edit(final NodePath _path,NodeEditor _e) + { + LoggingNodeHook hook = new LoggingNodeHook(_e); + Either<Error,T> either = editor.edit(root,_path,hook); + if(either.isA()){ + return DefaultEither.newA(either.a()); + } + + T newNode = either.b(); + OperationLog newLog = hook.getLog(); + + 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 = new NetworkDefaultJungleTreeEditor<T>(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); + } + + @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<T>> either = txManager.commit(root,log); + if(either.isA()){ + return DefaultEither.newA(either.a()); + } + + TransactionManager<T> newTxManager = either.b(); + JungleTreeEditor newTreeEditor = new NetworkDefaultJungleTreeEditor<T>(root,newTxManager,editor); + + return DefaultEither.newB(newTreeEditor); + } + + @Override + public String getID() + { + return txManager.getUUID(); + } + + @Override + public String getRevision() + { + return Long.toString(txManager.getRevision()); + } + + @Override + public Node getRoot() + { + return null; + } + + public JungleTreeEditor setNewLogAndGetEditor(TreeOperationLog _log) { + JungleTreeEditor newEditor = new NetworkDefaultJungleTreeEditor<T>(root,txManager,editor, _log); + return newEditor; + } + +}