Mercurial > hg > Members > tatsuki > bench > jungle-core
annotate src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultJungleTreeEditor.java @ 79:b4f145174970
change log
author | one |
---|---|
date | Sun, 31 Aug 2014 02:10:14 +0900 |
parents | 540a27dde42f |
children | 715a9fbf02fc |
rev | line source |
---|---|
37 | 1 package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction; |
2 | |
3 import java.nio.ByteBuffer; | |
78 | 4 |
37 | 5 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; |
6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; | |
53 | 7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; |
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; | |
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog; | |
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.LoggingNodeHook; | |
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.OperationLog; | |
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog; | |
78 | 13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.AppendChildAtOperation; |
53 | 14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DefaultTreeOperation; |
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation; | |
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation; | |
49
e3a507fd57a4
modified TreeEditor and JungleTreeEditor (not done yet)
Shoshi TAMAKI
parents:
46
diff
changeset
|
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.AppendChildAt; |
53 | 18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteAttribute; |
19 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.DeleteChildAt; | |
37 | 20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; |
53 | 21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.PutAttribute; |
38
cb9fabaedfd4
added tests and changed testing provider to junit47
Shoshi TAMAKI
parents:
37
diff
changeset
|
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; |
37 | 23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; |
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
53 | 25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; |
37 | 26 |
53 | 27 public class DefaultJungleTreeEditor<T extends TreeNode<T>> implements JungleTreeEditor |
37 | 28 { |
54 | 29 private final TransactionManager<T> txManager; |
49
e3a507fd57a4
modified TreeEditor and JungleTreeEditor (not done yet)
Shoshi TAMAKI
parents:
46
diff
changeset
|
30 private final T root; |
53 | 31 private final TreeEditor editor; |
32 private final TreeOperationLog log; | |
37 | 33 |
54 | 34 public DefaultJungleTreeEditor(T _root,TransactionManager<T> _txManager,TreeEditor _editor) |
53 | 35 { |
54 | 36 this(_root,_txManager,_editor,new DefaultTreeOperationLog()); |
53 | 37 } |
38 | |
54 | 39 public DefaultJungleTreeEditor(T _root,TransactionManager<T> _txManager,TreeEditor _editor,TreeOperationLog _log) |
37 | 40 { |
41 root = _root; | |
42 txManager = _txManager; | |
53 | 43 editor = _editor; |
44 log = _log; | |
37 | 45 } |
46 | |
53 | 47 private Either<Error,JungleTreeEditor> _edit(final NodePath _path,NodeEditor _e) |
46 | 48 { |
53 | 49 LoggingNodeHook hook = new LoggingNodeHook(_e); |
54 | 50 Either<Error,T> either = editor.edit(root,_path,hook); |
38
cb9fabaedfd4
added tests and changed testing provider to junit47
Shoshi TAMAKI
parents:
37
diff
changeset
|
51 if(either.isA()){ |
cb9fabaedfd4
added tests and changed testing provider to junit47
Shoshi TAMAKI
parents:
37
diff
changeset
|
52 return DefaultEither.newA(either.a()); |
cb9fabaedfd4
added tests and changed testing provider to junit47
Shoshi TAMAKI
parents:
37
diff
changeset
|
53 } |
37 | 54 |
53 | 55 T newNode = either.b(); |
56 OperationLog newLog = hook.getLog(); | |
37 | 57 |
53 | 58 IterableConverter.Converter<TreeOperation,NodeOperation> converter = new IterableConverter.Converter<TreeOperation,NodeOperation>(){ |
59 @Override | |
60 public TreeOperation conv(NodeOperation _b){ | |
61 return new DefaultTreeOperation(_path,_b); | |
62 } | |
63 }; | |
38
cb9fabaedfd4
added tests and changed testing provider to junit47
Shoshi TAMAKI
parents:
37
diff
changeset
|
64 |
79 | 65 Iterable<TreeOperation> iterable = new IterableConverter<TreeOperation,NodeOperation>(newLog,converter); |
66 DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog(iterable,newLog.length()); | |
67 TreeOperationLog newTreeOpLog = log.append(treeOperationLog); | |
38
cb9fabaedfd4
added tests and changed testing provider to junit47
Shoshi TAMAKI
parents:
37
diff
changeset
|
68 |
78 | 69 JungleTreeEditor newEditor = new DefaultJungleTreeEditor<T>(newNode,txManager,editor,new DefaultTreeOperationLog()); |
53 | 70 return DefaultEither.newB(newEditor); |
37 | 71 } |
53 | 72 |
37 | 73 @Override |
53 | 74 public Either<Error,JungleTreeEditor> addNewChildAt(NodePath _path, int _pos) |
37 | 75 { |
53 | 76 AppendChildAt appendChildAt = new AppendChildAt(_pos); |
77 return _edit(_path,appendChildAt); | |
37 | 78 } |
79 | |
80 @Override | |
53 | 81 public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path, int _pos) |
37 | 82 { |
53 | 83 DeleteChildAt deleteChildAt = new DeleteChildAt(_pos); |
84 return _edit(_path,deleteChildAt); | |
85 } | |
86 | |
87 @Override | |
88 public Either<Error,JungleTreeEditor> putAttribute(NodePath _path,String _key,ByteBuffer _value) | |
89 { | |
90 PutAttribute putAttribute = new PutAttribute(_key,_value); | |
91 return _edit(_path,putAttribute); | |
37 | 92 } |
93 | |
94 @Override | |
53 | 95 public Either<Error,JungleTreeEditor> deleteAttribute(NodePath _path, String _key) |
37 | 96 { |
53 | 97 DeleteAttribute deleteAttribute = new DeleteAttribute(_key); |
98 return _edit(_path,deleteAttribute); | |
37 | 99 } |
100 | |
101 @Override | |
53 | 102 public Either<Error,JungleTreeEditor> edit(NodePath _path,NodeEditor _editor) |
37 | 103 { |
53 | 104 return _edit(_path,_editor); |
105 } | |
106 | |
107 @Override | |
108 public Either<Error,JungleTreeEditor> success() | |
109 { | |
54 | 110 Either<Error,TransactionManager<T>> either = txManager.commit(root,log); |
38
cb9fabaedfd4
added tests and changed testing provider to junit47
Shoshi TAMAKI
parents:
37
diff
changeset
|
111 if(either.isA()){ |
cb9fabaedfd4
added tests and changed testing provider to junit47
Shoshi TAMAKI
parents:
37
diff
changeset
|
112 return DefaultEither.newA(either.a()); |
cb9fabaedfd4
added tests and changed testing provider to junit47
Shoshi TAMAKI
parents:
37
diff
changeset
|
113 } |
cb9fabaedfd4
added tests and changed testing provider to junit47
Shoshi TAMAKI
parents:
37
diff
changeset
|
114 |
54 | 115 TransactionManager<T> newTxManager = either.b(); |
116 JungleTreeEditor newTreeEditor = new DefaultJungleTreeEditor<T>(root,newTxManager,editor); | |
45 | 117 |
118 return DefaultEither.newB(newTreeEditor); | |
37 | 119 } |
120 | |
45 | 121 @Override |
122 public String getID() | |
123 { | |
124 return txManager.getUUID(); | |
125 } | |
126 | |
127 @Override | |
128 public String getRevision() | |
129 { | |
130 return Long.toString(txManager.getRevision()); | |
131 } | |
53 | 132 |
133 @Override | |
74 | 134 public TreeNode<T> getRoot() |
53 | 135 { |
136 return null; | |
137 } | |
37 | 138 } |