1 module neton.store.Event; 2 3 import neton.store.NodeExtern; 4 import std.experimental.allocator; 5 import std.json; 6 import hunt.logging; 7 import neton.store.Util; 8 9 enum ServiceState 10 { 11 Passing = "passing", 12 Warning = "warning", 13 Critical = "critical", 14 Maintenance = "maintenance", 15 } 16 17 enum EventAction 18 { 19 Get = "get", 20 Create = "create", 21 Set = "set", 22 Update = "update", 23 Delete = "delete", 24 Register = "register", 25 Deregister = "deregister", 26 CompareAndSwap = "compareAndSwap", 27 CompareAndDelete = "compareAndDelete", 28 Expire = "expire", 29 } 30 31 class Event 32 { 33 34 this() 35 { 36 37 } 38 39 this(EventAction action, string key, ulong modifiedIndex, bool recursive = false) 40 { 41 42 // logWarning("begin load value :",key ); 43 _node = new NodeExtern(key, modifiedIndex, recursive); 44 // logWarning("end load value :",key ); 45 _action = action; 46 _refresh = false; 47 } 48 49 this(EventAction action, string reqKey,string key, ulong modifiedIndex, bool recursive = false) 50 { 51 52 // logWarning("begin load value :",key ); 53 _node = new NodeExtern(reqKey,key, modifiedIndex, recursive); 54 // logWarning("end load value :",key ); 55 _action = action; 56 _refresh = false; 57 } 58 59 void opAssign(S)(auto ref S e) if (is(S == Unqual!(typeof(this)))) 60 { 61 _action = e._action; 62 _netonIndex = e._netonIndex; 63 _refresh = e._refresh; 64 _node = e._node.Clone(); 65 _prevNode = e._prevNode.Clone(); 66 _errorMsg = e._errorMsg; 67 } 68 69 bool IsCreated() 70 { 71 if (this._action == EventAction.Create) 72 { 73 return true; 74 } 75 return _action == EventAction.Set && _prevNode is null; 76 } 77 78 @property ulong Index() 79 { 80 return /*_node.modifiedIndex*/ _netonIndex; 81 } 82 83 @property EventAction action() 84 { 85 return _action; 86 } 87 88 @property NodeExtern prevNode() 89 { 90 return _prevNode; 91 } 92 93 @property NodeExtern node() 94 { 95 return _node; 96 } 97 98 string nodeKey() 99 { 100 return _node.key; 101 } 102 103 string nodeOriginKey() 104 { 105 return _node.originKey; 106 } 107 108 JSONValue getNodeValue() 109 { 110 return _node.value; 111 } 112 113 @property ulong netonIndex() 114 { 115 return _netonIndex; 116 } 117 118 void setNetonIndex(ulong idx) 119 { 120 _netonIndex = idx; 121 } 122 123 Event Clone() 124 { 125 auto e = theAllocator.make!Event(); 126 e = this; 127 return e; 128 } 129 130 @property bool dir() 131 { 132 return _node.dir; 133 } 134 135 @property bool refresh() 136 { 137 return _refresh; 138 } 139 140 void SetRefresh() 141 { 142 _refresh = true; 143 } 144 145 @property bool isOk() 146 { 147 return _errorMsg.length == 0; 148 } 149 150 @property string error() 151 { 152 return _errorMsg; 153 } 154 155 void setErrorMsg(string msg) 156 { 157 _errorMsg = msg; 158 } 159 160 string rpcValue() 161 { 162 if (this.error.length > 0) 163 return this.error; 164 JSONValue nodeValue = this.getNodeValue(); 165 if (nodeValue.type == JSONType.object) 166 { 167 if ("value" in nodeValue) 168 { 169 if (nodeValue["value"].type == JSONType..string) 170 return nodeValue["value"].str; 171 else 172 return nodeValue["value"].toString; 173 } 174 } 175 return nodeValue.toString; 176 } 177 178 import neton.protocol.neton; 179 180 KeyValue[] getKeyValues() 181 { 182 KeyValue[] kvs; 183 if (this.error.length > 0) 184 return null; 185 JSONValue nodeValue = this.getNodeValue(); 186 if(nodeValue.type == JSONType.null_) 187 return null; 188 if (isDir(nodeValue)) 189 { 190 nodeValue = nodeValue["nodes"]; 191 if (nodeValue.type == JSONType.object) 192 { 193 if (!isDir(nodeValue)) 194 { 195 auto kv = new KeyValue(); 196 kv.key = cast(ubyte[])(nodeValue["key"].str); 197 if ("value" in nodeValue) 198 { 199 if (nodeValue["value"].type == JSONType..string) 200 kv.value = cast(ubyte[])(nodeValue["value"].str); 201 else 202 kv.value = cast(ubyte[])(nodeValue["value"].toString); 203 } 204 kvs ~= kv; 205 } 206 } 207 else if (nodeValue.type == JSONType.array) 208 { 209 foreach (item; nodeValue.array) 210 { 211 if (!isDir(item)) 212 { 213 auto kv = new KeyValue(); 214 kv.key = cast(ubyte[])(item["key"].str); 215 if ("value" in item) 216 { 217 if (item["value"].type == JSONType..string) 218 kv.value = cast(ubyte[])(item["value"].str); 219 else 220 kv.value = cast(ubyte[])(item["value"].toString); 221 } 222 kvs ~= kv; 223 } 224 } 225 } 226 } 227 else 228 { 229 auto kv = new KeyValue(); 230 kv.key = cast(ubyte[])(nodeValue["key"].str); 231 if ("value" in nodeValue) 232 { 233 if (nodeValue["value"].type == JSONType..string) 234 kv.value = cast(ubyte[])(nodeValue["value"].str); 235 else 236 kv.value = cast(ubyte[])(nodeValue["value"].toString); 237 } 238 kvs ~= kv; 239 } 240 241 return kvs; 242 } 243 244 bool isDir(JSONValue data) 245 { 246 if (data.type != JSONType.object) 247 return false; 248 if ("dir" in data && data["dir"].str == "true") 249 return true; 250 return false; 251 } 252 253 private: 254 EventAction _action; 255 NodeExtern _node; 256 NodeExtern _prevNode; 257 ulong _netonIndex; 258 bool _refresh; 259 string _errorMsg; 260 }