20
|
1 using UnityEngine;
|
|
2 using System.Collections;
|
|
3
|
|
4 namespace JungleDB {
|
|
5 public class NetworkNodeOperation : NodeOperation {
|
|
6
|
|
7 public int Position;
|
|
8 public string Key;
|
|
9 public byte[] Value;
|
|
10 public int commandType;
|
|
11
|
|
12 // when switch use static readonly so, use const.
|
|
13 // when A code compalie, case const use is fast.
|
|
14 // case readonly use is bit slow.
|
|
15 public const int NUM_PUT_ATTRIBUTE = 1;
|
|
16 public const int NUM_APPEND_CHILD = 2;
|
|
17 public const int NUM_DELETE_CHILD = 3;
|
|
18 public const int NUM_DELETE_ATTRIBUTE = 4;
|
|
19
|
|
20 public NetworkNodeOperation () {
|
|
21 this.Position = -2;
|
|
22 this.Key = null;
|
|
23 this.Value = null;
|
|
24 this.commandType = 0;
|
|
25 }
|
|
26
|
|
27 public NetworkNodeOperation(NodeOperation op) {
|
|
28 this.Position = op.getPosition();
|
|
29 this.Key = op.getKey();
|
|
30 this.Value = op.getValue();
|
|
31 this.commandType = getCommandType(op.getCommand());
|
|
32 }
|
|
33
|
|
34 public static int getCommandType (Command c) {
|
|
35 switch(c) {
|
|
36 case Command.PUT_ATTRIBUTE:
|
|
37 return NUM_PUT_ATTRIBUTE;
|
|
38 case Command.APPEND_CHILD:
|
|
39 return NUM_APPEND_CHILD;
|
|
40 case Command.DELETE_CHILD:
|
|
41 return NUM_DELETE_CHILD;
|
|
42 case Command.DELETE_ATTRIBUTE:
|
|
43 return NUM_DELETE_ATTRIBUTE;
|
|
44 default:
|
|
45 break;
|
|
46 }
|
|
47 return 0;
|
|
48 }
|
|
49
|
|
50 public static Command getCommand (int num) {
|
|
51 switch(num) {
|
|
52 case NUM_PUT_ATTRIBUTE:
|
|
53 return Command.PUT_ATTRIBUTE;
|
|
54 case NUM_APPEND_CHILD:
|
|
55 return Command.APPEND_CHILD;
|
|
56 case NUM_DELETE_CHILD:
|
|
57 return Command.DELETE_CHILD;
|
|
58 case NUM_DELETE_ATTRIBUTE:
|
|
59 return Command.DELETE_ATTRIBUTE;
|
|
60 default:
|
|
61 break;
|
|
62 }
|
|
63 return Command.DEFAULT;
|
|
64 }
|
|
65
|
|
66 public Command getCommand() {
|
|
67 return getCommand(commandType);
|
|
68 }
|
|
69
|
|
70 public int getPosition () {
|
|
71 return this.Position;
|
|
72 }
|
|
73
|
|
74 public string getKey () {
|
|
75 return this.Key;
|
|
76 }
|
|
77
|
|
78 public byte[] getValue() {
|
|
79 return this.Value;
|
|
80 }
|
|
81
|
|
82 public Either<Error, TreeNode> invoke (TreeNode target) {
|
|
83 switch(getCommand(commandType)) {
|
|
84 case Command.PUT_ATTRIBUTE:
|
|
85 return target.getAttributes().put(this.Key, this.Value);
|
|
86 case Command.APPEND_CHILD:
|
|
87 return target.getChildren().addNewChildAt(this.Position);
|
|
88 case Command.DELETE_CHILD:
|
|
89 return target.getChildren().deleteChildAt(this.Position);
|
|
90 case Command.DELETE_ATTRIBUTE:
|
|
91 return target.getAttributes().delete(this.Key);
|
|
92 }
|
|
93 return null;
|
|
94 }
|
|
95 }
|
|
96 }
|