Mercurial > hg > Members > nobuyasu > jungle-network
view src/alice/jungle/transaction/NetworkDefaultJungleTreeEditor.java @ 66:29127ac788a6
move some files
author | one |
---|---|
date | Tue, 20 Aug 2013 17:38:09 +0900 |
parents | ebf42371454b |
children | 2c7b3f2b2ee1 |
line wrap: on
line source
package alice.jungle.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.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 String treeName; private final TreeEditor editor; private final TreeOperationLog log; private final String updaterName; public NetworkDefaultJungleTreeEditor(String _updaterName, String _treeName, T _root,TransactionManager<T> _txManager,TreeEditor _editor) { this(_updaterName, _treeName, _root,_txManager,_editor,new DefaultTreeOperationLog()); } public NetworkDefaultJungleTreeEditor(String _updaterName, String _treeName, T _root,TransactionManager<T> _txManager,TreeEditor _editor, TreeOperationLog _log) { updaterName = _updaterName; treeName = _treeName; 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>(updaterName, 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); } @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>(updaterName, treeName, 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 root.getAsNode(); } public TreeOperationLog getTreeOperationLog() { return log; } public String getTreeName() { return treeName; } public String getUpdaterName() { return updaterName; } }