Mercurial > hg > Database > jungle-network
view src/jungle/test/transaction/NetworkDefaultJungleTreeEditor.java @ 12:80680cfcfd3b
add jungle/test/transaction, jungle/test/core/NetworkDefaultJungle
author | one |
---|---|
date | Wed, 26 Jun 2013 19:03:19 +0900 |
parents | |
children | 1d7f52c3b3d9 |
line wrap: on
line source
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; } }