Mercurial > hg > Members > shoshi > jungle > jungle-core
view src/main/java/jp/ac/u_ryukyu/ie/cr/jungleNetwork/transaction/NetworkDefaultJungleTreeEditor.java @ 329:2a0cb1f0ba4e
rename Error package
author | kono |
---|---|
date | Sat, 08 Jul 2017 21:05:55 +0900 |
parents | b606b727dc2b |
children | 7a7d5d175c0c |
line wrap: on
line source
package jp.ac.u_ryukyu.ie.cr.jungleNetwork.transaction; import alice.codesegment.CodeSegment; import jp.ac.u_ryukyu.ie.cr.jungle.data.list.List; import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.DefaultTreeOperationLog; import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.LoggingNode; import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.OperationLog; import jp.ac.u_ryukyu.ie.cr.jungle.store.logger.TreeOperationLog; import jp.ac.u_ryukyu.ie.cr.jungle.store.nodepath.NodePath; import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.DefaultTreeOperation; import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.NodeOperation; import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.TreeOperation; import jp.ac.u_ryukyu.ie.cr.jungle.store.trasnformer.*; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.jungleTreeEditor.JungleTreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.editor.treeEditor.TreeEditor; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.manager.TransactionManager; import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode; import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.jungle.util.jungleError.Error; import jp.ac.u_ryukyu.ie.cr.jungle.util.IterableConverter; import jp.ac.u_ryukyu.ie.cr.jungleNetwork.codesegment.LogPutCodeSegment; import jp.ac.u_ryukyu.ie.cr.jungleNetwork.operations.NetworkTreeOperationLog; import java.io.IOException; import java.nio.ByteBuffer; public class NetworkDefaultJungleTreeEditor implements JungleTreeEditor { private final TransactionManager txManager; private final TreeNode root; private final TreeEditor editor; private final String treeName; private final int type; private final TreeOperationLog log; private boolean exportLog; private final List<TreeNode> editedNodeList = new List<>(); public NetworkDefaultJungleTreeEditor(String _treeName, int type, TreeNode _root, TransactionManager _txManager, TreeEditor _editor) { this(_treeName, type, _root, _txManager, _editor, new DefaultTreeOperationLog()); } public NetworkDefaultJungleTreeEditor(String _treeName, int type, TreeNode _root, TransactionManager _txManager, TreeEditor _editor, TreeOperationLog _log) { this.treeName = _treeName; this.root = _root; this.txManager = _txManager; this.editor = _editor; this.log = _log; this.exportLog = true; this.type = type; } public static NetworkDefaultJungleTreeEditor NewLocalJungleTreeEditor(String _treeName, int type, TreeNode _root, TransactionManager _txManager, TreeEditor _editor) { NetworkDefaultJungleTreeEditor treeEditor = new NetworkDefaultJungleTreeEditor(_treeName, type, _root, _txManager, _editor, new DefaultTreeOperationLog()); treeEditor.exportLog = false; return treeEditor; } public static NetworkDefaultJungleTreeEditor NewLocalJungleTreeEditor(String _treeName, int type, TreeNode _root, TransactionManager _txManager, TreeEditor _editor, TreeOperationLog _log) { NetworkDefaultJungleTreeEditor treeEditor = new NetworkDefaultJungleTreeEditor(_treeName, type, _root, _txManager, _editor, _log); treeEditor.exportLog = false; return treeEditor; } private Either<Error, JungleTreeEditor> _edit(final NodePath _path, NodeEditor _e) { Either<Error, LoggingNode> editEither = editor.edit(root, _path, _e); if (editEither.isA()) { return DefaultEither.newA(editEither.a()); } TreeNode newNode = editEither.b().getWrap(); OperationLog newLog = editEither.b().getOperationLog(); 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(treeName, type, newNode, txManager, editor, newTreeOpLog); } else { newEditor = NetworkDefaultJungleTreeEditor.NewLocalJungleTreeEditor(treeName, type, 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> addNewChildAndPutAttribute(NodePath path, int pos, String key, ByteBuffer value) { AppendChildAndPutAttribute appendChildAndPutAttribute = new AppendChildAndPutAttribute(key, value, pos); return _edit(path, appendChildAndPutAttribute); } @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> moveChild(NodePath path, int childNum, String move) { MoveChild movechild = new MoveChild(move, childNum); return _edit(path, movechild); } @Override public Either<Error, JungleTreeEditor> edit(NodePath _path, NodeEditor _editor) { return _edit(_path, _editor); } @Override public Either<Error, JungleTreeEditor> success() { Either<Error, TransactionManager> either = txManager.commit(root, log, editedNodeList); if (either.isA()) { return DefaultEither.newA(either.a()); } TransactionManager newTxManager = either.b(); JungleTreeEditor newTreeEditor = null; if (exportLog) { try { newTreeEditor = new NetworkDefaultJungleTreeEditor(treeName, type, root, newTxManager, editor); putTreeOperationLog(log); } catch (IOException e) { return DefaultEither.newA(either.a()); } } else { newTreeEditor = NewLocalJungleTreeEditor(treeName, type, root, newTxManager, editor); } return DefaultEither.newB(newTreeEditor); } @Override public Either<Error, JungleTreeEditor> flushSuccess() { return success(); } private String getID() { return txManager.getUUID(); } private String getRevision() { return Long.toString(txManager.getRevision()); } public String getTreeName() { return treeName; } public TreeOperationLog getTreeOperationLog() { return log; } private int getType() { return type; } public void putTreeOperationLog(Iterable<TreeOperation> newLog) throws IOException { String uuid = getID(); String treeName = getTreeName(); String updaterName = getID(); String revision = getRevision(); int type = getType(); putDataSegment(uuid, treeName, type, updaterName, newLog, revision); } public void putDataSegment(String _uuid, String _treeName, int type, String _updaterName, Iterable<TreeOperation> newLog, String nextRevision) throws IOException { NetworkTreeOperationLog netLog = new NetworkTreeOperationLog(_uuid, _treeName, type, newLog); CodeSegment cs = new LogPutCodeSegment(netLog); cs.execute(); } @Override public Either<Error, JungleTreeEditor> replaceNewRootNode() { // TODO Auto-generated method stub return null; } }