Mercurial > hg > Database > jungle-network
comparison src/alice/jungle/transaction/NetworkDefaultJungleTreeEditor.java @ 31:190f6a3bdab2
rename some packages
author | one |
---|---|
date | Mon, 01 Jul 2013 20:34:03 +0900 |
parents | src/jungle/test/transaction/NetworkDefaultJungleTreeEditor.java@e968224ad0ce |
children | 2f1879e0acec |
comparison
equal
deleted
inserted
replaced
30:8593c2525aa7 | 31:190f6a3bdab2 |
---|---|
1 package alice.jungle.transaction; | |
2 | |
3 import java.nio.ByteBuffer; | |
4 | |
5 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor; | |
6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; | |
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath; | |
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.TreeEditor; | |
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; | |
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.DefaultTreeOperationLog; | |
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.LoggingNodeHook; | |
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.OperationLog; | |
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog; | |
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; | |
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.AppendChildAt; | |
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; | |
20 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditor; | |
21 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.PutAttribute; | |
22 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.DefaultJungleTreeEditor; | |
23 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction.TransactionManager; | |
24 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; | |
25 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; | |
26 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
27 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; | |
28 | |
29 public class NetworkDefaultJungleTreeEditor<T extends TreeNode<T>> implements JungleTreeEditor { | |
30 | |
31 private final TransactionManager<T> txManager; | |
32 private final T root; | |
33 private final TreeEditor editor; | |
34 private final TreeOperationLog log; | |
35 | |
36 public NetworkDefaultJungleTreeEditor(T _root,TransactionManager<T> _txManager,TreeEditor _editor) | |
37 { | |
38 this(_root,_txManager,_editor,new DefaultTreeOperationLog()); | |
39 } | |
40 | |
41 public NetworkDefaultJungleTreeEditor(T _root,TransactionManager<T> _txManager,TreeEditor _editor,TreeOperationLog _log) | |
42 { | |
43 root = _root; | |
44 txManager = _txManager; | |
45 editor = _editor; | |
46 log = _log; | |
47 } | |
48 | |
49 private Either<Error,JungleTreeEditor> _edit(final NodePath _path,NodeEditor _e) | |
50 { | |
51 LoggingNodeHook hook = new LoggingNodeHook(_e); | |
52 Either<Error,T> either = editor.edit(root,_path,hook); | |
53 if(either.isA()){ | |
54 return DefaultEither.newA(either.a()); | |
55 } | |
56 | |
57 T newNode = either.b(); | |
58 OperationLog newLog = hook.getLog(); | |
59 | |
60 IterableConverter.Converter<TreeOperation,NodeOperation> converter = new IterableConverter.Converter<TreeOperation,NodeOperation>(){ | |
61 @Override | |
62 public TreeOperation conv(NodeOperation _b){ | |
63 return new DefaultTreeOperation(_path,_b); | |
64 } | |
65 }; | |
66 | |
67 Iterable<TreeOperation> iterable = new IterableConverter<TreeOperation,NodeOperation>(newLog,converter); | |
68 DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog(iterable,newLog.length()); | |
69 TreeOperationLog newTreeOpLog = log.append(treeOperationLog); | |
70 | |
71 JungleTreeEditor newEditor = new NetworkDefaultJungleTreeEditor<T>(newNode,txManager,editor,newTreeOpLog); | |
72 return DefaultEither.newB(newEditor); | |
73 } | |
74 | |
75 @Override | |
76 public Either<Error,JungleTreeEditor> addNewChildAt(NodePath _path, int _pos) | |
77 { | |
78 AppendChildAt appendChildAt = new AppendChildAt(_pos); | |
79 return _edit(_path,appendChildAt); | |
80 } | |
81 | |
82 @Override | |
83 public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path, int _pos) | |
84 { | |
85 DeleteChildAt deleteChildAt = new DeleteChildAt(_pos); | |
86 return _edit(_path,deleteChildAt); | |
87 } | |
88 | |
89 @Override | |
90 public Either<Error,JungleTreeEditor> putAttribute(NodePath _path,String _key,ByteBuffer _value) | |
91 { | |
92 PutAttribute putAttribute = new PutAttribute(_key,_value); | |
93 return _edit(_path,putAttribute); | |
94 } | |
95 | |
96 @Override | |
97 public Either<Error,JungleTreeEditor> deleteAttribute(NodePath _path, String _key) | |
98 { | |
99 DeleteAttribute deleteAttribute = new DeleteAttribute(_key); | |
100 return _edit(_path,deleteAttribute); | |
101 } | |
102 | |
103 @Override | |
104 public Either<Error,JungleTreeEditor> edit(NodePath _path,NodeEditor _editor) | |
105 { | |
106 return _edit(_path,_editor); | |
107 } | |
108 | |
109 @Override | |
110 public Either<Error,JungleTreeEditor> success() | |
111 { | |
112 Either<Error,TransactionManager<T>> either = txManager.commit(root,log); | |
113 if(either.isA()){ | |
114 return DefaultEither.newA(either.a()); | |
115 } | |
116 | |
117 TransactionManager<T> newTxManager = either.b(); | |
118 JungleTreeEditor newTreeEditor = new NetworkDefaultJungleTreeEditor<T>(root,newTxManager,editor); | |
119 | |
120 return DefaultEither.newB(newTreeEditor); | |
121 } | |
122 | |
123 @Override | |
124 public String getID() | |
125 { | |
126 return txManager.getUUID(); | |
127 } | |
128 | |
129 @Override | |
130 public String getRevision() | |
131 { | |
132 return Long.toString(txManager.getRevision()); | |
133 } | |
134 | |
135 @Override | |
136 public Node getRoot() | |
137 { | |
138 return null; | |
139 } | |
140 | |
141 public JungleTreeEditor setNewLogAndGetEditor(TreeOperationLog _log) { | |
142 JungleTreeEditor newEditor = new NetworkDefaultJungleTreeEditor<T>(root,txManager,editor, _log); | |
143 return newEditor; | |
144 } | |
145 | |
146 public TreeOperationLog getTreeOperationLog() { | |
147 return log; | |
148 } | |
149 | |
150 | |
151 } |