118
|
1 package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction;
|
|
2
|
|
3 import java.nio.ByteBuffer;
|
|
4
|
|
5 import fj.data.List;
|
|
6 import fj.data.TreeMap;
|
|
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
|
|
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
|
145
|
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.IndexTreeEditor;
|
118
|
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode;
|
|
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog;
|
|
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.LoggingNode;
|
|
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.OperationLog;
|
|
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
|
|
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DefaultTreeOperation;
|
|
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation;
|
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
|
|
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.AppendChildAt;
|
|
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteAttribute;
|
|
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteChildAt;
|
|
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor;
|
|
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.PutAttribute;
|
|
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither;
|
|
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
|
|
25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
|
|
26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter;
|
146
|
27 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Triple;
|
128
|
28 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.DefaultIndexEditor;
|
|
29 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.DeleteChildIndexEditor;
|
118
|
30 import jp.ac.u_ryukyu.ie.cr.tatsuki.jungle.store.index.IndexEditor;
|
|
31
|
145
|
32 public class IndexJungleTreeEditor implements JungleTreeEditor {
|
|
33 private final TransactionManager txManager;
|
|
34 private final TreeNode root;
|
|
35 private final IndexTreeEditor editor;
|
|
36 private final TreeOperationLog log;
|
146
|
37 private TreeMap<String, TreeMap<String, List<TreeNode>>> index;
|
145
|
38 private TreeMap<TreeNode, TreeNode> parentIndex;
|
|
39
|
146
|
40 public TreeMap<String, TreeMap<String, List<TreeNode>>> getIndex() {
|
145
|
41 return index;
|
|
42 }
|
|
43
|
118
|
44
|
|
45
|
145
|
46 public IndexJungleTreeEditor(TreeNode _root, TransactionManager _txManager, IndexTreeEditor treeEditor,
|
146
|
47 TreeMap<String, TreeMap<String, List<TreeNode>>> index,TreeMap<TreeNode, TreeNode> parentIndex) {
|
145
|
48 this(_root, _txManager, treeEditor, new DefaultTreeOperationLog(), index,parentIndex);
|
|
49 }
|
118
|
50
|
145
|
51 public IndexJungleTreeEditor(TreeNode newNode, TransactionManager _txManager, IndexTreeEditor _editor,
|
146
|
52 TreeOperationLog _log, TreeMap<String, TreeMap<String, List<TreeNode>>> index,TreeMap<TreeNode, TreeNode> parentIndex) {
|
145
|
53 this.root = newNode;
|
|
54 this.txManager = _txManager;
|
|
55 this.editor = _editor;
|
|
56 this.log = _log;
|
|
57 this.index = index;
|
|
58 this.parentIndex = parentIndex;
|
|
59 }
|
118
|
60
|
145
|
61
|
|
62 public Either<Error, IndexJungleTreeEditor> _edit(final NodePath _path, NodeEditor _e, IndexEditor indexEditor) {
|
146
|
63 Either<Error,Triple<LoggingNode, TreeMap<TreeNode, TreeNode>,TreeMap<String,TreeMap<String,List<TreeNode>>>>> either = editor.edit(root, _path, _e, parentIndex, indexEditor);
|
145
|
64 if (either.isA()) {
|
|
65 return DefaultEither.newA(either.a());
|
|
66 }
|
|
67
|
146
|
68 LoggingNode newLogging = either.b().getA();
|
|
69 TreeMap<TreeNode,TreeNode> newParentIndex = either.b().getB();
|
|
70 TreeMap<String,TreeMap<String,List<TreeNode>>> newIndex = either.b().getC();
|
145
|
71 OperationLog newLog = newLogging.getOperationLog();
|
|
72 TreeNode newNode = newLogging.getWrap();
|
|
73
|
|
74 IterableConverter.Converter<TreeOperation, NodeOperation> converter = new IterableConverter.Converter<TreeOperation, NodeOperation>() {
|
|
75 @Override
|
|
76 public TreeOperation conv(NodeOperation _b) {
|
|
77 return new DefaultTreeOperation(_path, _b);
|
|
78 }
|
|
79 };
|
|
80
|
|
81 Iterable<TreeOperation> iterable = new IterableConverter<TreeOperation, NodeOperation>(newLog, converter);
|
|
82 DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog(iterable, newLog.length());
|
|
83 TreeOperationLog newTreeOpLog = log.append(treeOperationLog);
|
146
|
84 IndexJungleTreeEditor newIndexTreeEditor = new IndexJungleTreeEditor(newNode, txManager, this.editor,newTreeOpLog, newIndex, newParentIndex);
|
|
85 return DefaultEither.newB(newIndexTreeEditor);
|
145
|
86 }
|
|
87
|
|
88 @Override
|
|
89 public Either<Error, JungleTreeEditor> addNewChildAt(NodePath _path, int _pos) {
|
|
90 AppendChildAt appendChildAt = new AppendChildAt(_pos);
|
146
|
91 IndexEditor indexEditor = new DefaultIndexEditor(index);
|
145
|
92 Either<Error, IndexJungleTreeEditor> either = _edit(_path, appendChildAt, indexEditor);
|
|
93 Either<Error, JungleTreeEditor> newEither = DefaultEither.newB(either.b());
|
|
94 return newEither;
|
|
95 }
|
118
|
96
|
145
|
97 @Override
|
|
98 public Either<Error, JungleTreeEditor> deleteChildAt(NodePath _path, int _pos) {
|
|
99 DeleteChildAt deleteChildAt = new DeleteChildAt(_pos);
|
146
|
100 DeleteChildIndexEditor indexEditor = new DeleteChildIndexEditor(_pos, index);
|
145
|
101 Either<Error, IndexJungleTreeEditor> either = _edit(_path, deleteChildAt, indexEditor);
|
|
102 JungleTreeEditor editor = either.b();
|
|
103 Either<Error, JungleTreeEditor> newEither = DefaultEither.newB(editor);
|
|
104 return newEither;
|
|
105 }
|
118
|
106
|
145
|
107 @Override
|
|
108 public Either<Error, JungleTreeEditor> putAttribute(NodePath _path, String _key, ByteBuffer _value) {
|
|
109 PutAttribute putAttribute = new PutAttribute(_key, _value);
|
146
|
110 IndexEditor indexEditor = new DefaultIndexEditor(index);
|
145
|
111 Either<Error, IndexJungleTreeEditor> either = _edit(_path, putAttribute, indexEditor);
|
|
112 JungleTreeEditor editor = either.b();
|
|
113 Either<Error, JungleTreeEditor> newEither = DefaultEither.newB(editor);
|
|
114 return newEither;
|
|
115 }
|
118
|
116
|
145
|
117 @Override
|
|
118 public Either<Error, JungleTreeEditor> deleteAttribute(NodePath _path, String _key) {
|
|
119 DeleteAttribute deleteAttribute = new DeleteAttribute(_key);
|
146
|
120 IndexEditor indexEditor = new DefaultIndexEditor(index);
|
145
|
121 Either<Error, IndexJungleTreeEditor> either = _edit(_path, deleteAttribute, indexEditor);
|
|
122 Either<Error, JungleTreeEditor> newEither = DefaultEither.newB(either.b());
|
|
123 return newEither;
|
|
124 }
|
118
|
125
|
145
|
126 @Override
|
|
127 public Either<Error, JungleTreeEditor> edit(NodePath _path, NodeEditor _editor) {
|
146
|
128 IndexEditor indexEditor = new DefaultIndexEditor(index);
|
145
|
129 Either<Error, IndexJungleTreeEditor> either = _edit(_path, _editor, indexEditor);
|
|
130 JungleTreeEditor editor = either.b();
|
|
131 Either<Error, JungleTreeEditor> newEither = DefaultEither.newB(editor);
|
|
132 return newEither;
|
|
133 }
|
|
134
|
|
135 @Override
|
|
136 public Either<Error, JungleTreeEditor> success() {
|
|
137 Either<Error, TransactionManager> either = txManager.commit(root, log, index,parentIndex);
|
|
138 if (either.isA()) {
|
|
139 return DefaultEither.newA(either.a());
|
|
140 }
|
|
141
|
|
142 TransactionManager newTxManager = either.b();
|
|
143 JungleTreeEditor newTreeEditor = new IndexJungleTreeEditor(root, newTxManager, editor, index,parentIndex);
|
118
|
144
|
145
|
145 return DefaultEither.newB(newTreeEditor);
|
|
146 }
|
|
147
|
|
148 @Override
|
|
149 public String getID() {
|
|
150 return txManager.getUUID();
|
|
151 }
|
118
|
152
|
145
|
153 @Override
|
|
154 public String getRevision() {
|
|
155 return Long.toString(txManager.getRevision());
|
|
156 }
|
118
|
157
|
145
|
158 @Override
|
|
159 public TreeNode getRoot() {
|
|
160 return root;
|
|
161 }
|
118
|
162
|
|
163 } |