comparison src/alice/jungle/persistence/JungleUpdater.java @ 85:e641f559559c

Added some files for persistent
author one
date Mon, 28 Oct 2013 17:21:56 +0900
parents
children
comparison
equal deleted inserted replaced
84:82d1d3dac7bc 85:e641f559559c
1 package alice.jungle.persistence;
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.store.Command;
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation;
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
13
14 public class JungleUpdater {
15
16 public JungleUpdater() {
17
18 }
19
20 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log) {
21 JungleTreeEditor editor = _editor;
22 Either<Error, JungleTreeEditor> either = null;
23 for (TreeOperation op : _log) {
24 either = _edit(editor, op);
25 if(either.isA()) {
26 return either;
27 }
28 editor = either.b();
29 }
30 return either;
31 }
32
33 private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor,
34 TreeOperation op) {
35 DefaultNodePath path = new DefaultNodePath();
36 NodeOperation nodeOp = op.getNodeOperation();
37 int pos = nodeOp.getPosition();
38 Command c = nodeOp.getCommand();
39 String key = "";
40 switch (c) {
41 case PUT_ATTRIBUTE:
42 key = nodeOp.getKey();
43 ByteBuffer value = nodeOp.getValue();
44 return editor.putAttribute(path, key, value);
45 case DELETE_ATTRIBUTE:
46 key = nodeOp.getKey();
47 return editor.deleteAttribute(path, key);
48 case APPEND_CHILD:
49 return editor.addNewChildAt(path, pos);
50 case DELETE_CHILD:
51 return editor.deleteChildAt(path, 0);
52 }
53 return null;
54 }
55
56
57
58 }