Mercurial > hg > Members > nobuyasu > jungle-network
diff src/main/java/alice/jungle/transaction/NetworkDefaultJungleTreeEditor.java @ 105:f9e29a52efd3
Move some files
author | one |
---|---|
date | Tue, 26 Nov 2013 06:43:10 +0900 |
parents | src/alice/jungle/transaction/NetworkDefaultJungleTreeEditor.java@6e2cec364e5a |
children | f42f2e6b31de |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/jungle/transaction/NetworkDefaultJungleTreeEditor.java Tue Nov 26 06:43:10 2013 +0900 @@ -0,0 +1,194 @@ +package alice.jungle.transaction; + + +import java.io.IOException; +import java.nio.ByteBuffer; + +import alice.jungle.operations.NetworkTreeOperationLog; +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; +import jungle.app.bbs.codesegment.NullCodeSegment; + +public class NetworkDefaultJungleTreeEditor<T extends TreeNode<T>> implements JungleTreeEditor { + + private final TransactionManager<T> txManager; + private final T root; + private final TreeEditor editor; + private final String treeName; + private final TreeOperationLog log; + private boolean exportLog; + + public NetworkDefaultJungleTreeEditor(String _treeName, T _root,TransactionManager<T> _txManager,TreeEditor _editor) + { + this(_treeName, _root,_txManager,_editor,new DefaultTreeOperationLog()); + } + + public NetworkDefaultJungleTreeEditor(String _treeName, T _root,TransactionManager<T> _txManager,TreeEditor _editor, TreeOperationLog _log) + { + treeName = _treeName; + root = _root; + txManager = _txManager; + editor = _editor; + log = _log; + exportLog = true; + } + + public static <T1 extends TreeNode<T1>> NetworkDefaultJungleTreeEditor<T1> NewLocalJungleTreeEditor(String _treeName, T1 _root,TransactionManager<T1> _txManager,TreeEditor _editor) { + NetworkDefaultJungleTreeEditor<T1> treeEditor = new NetworkDefaultJungleTreeEditor<T1>(_treeName, _root,_txManager,_editor,new DefaultTreeOperationLog()); + treeEditor.exportLog = false; + return treeEditor; + } + + public static <T1 extends TreeNode<T1>> NetworkDefaultJungleTreeEditor<T1> NewLocalJungleTreeEditor(String _treeName, T1 _root,TransactionManager<T1> _txManager,TreeEditor _editor, TreeOperationLog _log) { + NetworkDefaultJungleTreeEditor<T1> treeEditor = new NetworkDefaultJungleTreeEditor<T1>(_treeName, _root,_txManager,_editor,_log); + treeEditor.exportLog = false; + return treeEditor; + } + + 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; + if(exportLog) { + newEditor = new NetworkDefaultJungleTreeEditor<T>(treeName, newNode,txManager,editor,newTreeOpLog); + } else { + newEditor = NetworkDefaultJungleTreeEditor.NewLocalJungleTreeEditor(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()); + } + if(exportLog) { + try { + putTreeOperationLog(log); + } catch (IOException e) { + return DefaultEither.newA(either.a()); + } + } + + TransactionManager<T> newTxManager = either.b(); + JungleTreeEditor newTreeEditor = new NetworkDefaultJungleTreeEditor<T>(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 String getTreeName() { + return treeName; + } + + public TreeOperationLog getTreeOperationLog() { + return log; + } + + public void putTreeOperationLog(Iterable<TreeOperation> newLog) throws IOException { + String uuid = getID(); + String treeName = getTreeName(); + String updaterName = getID(); + String revision = getRevision(); + putDataSegment(uuid, treeName, updaterName, newLog, revision); + } + + public void putDataSegment(String _uuid, String _treeName, String _updaterName, Iterable<TreeOperation> newLog, String nextRevision) throws IOException { + NetworkTreeOperationLog netLog = new NetworkTreeOperationLog(_uuid, _treeName,newLog); + NullCodeSegment cs = new NullCodeSegment(); + cs.ods.put("log", netLog); + cs.execute(); /* Do nothing CodeSegment */ + } +}