7 // grant 権限内容(all privileges) on 権限対象(dbname.tablename) to ユーザー@ホスト名 [ identified by "パスワード"];
10 define("DATABASE_USER", "aiclient");
12 define("DATABASE_PWD", "WhoAmI?");
14 define("DATABASE_NAME", "MemoryDB");
20 define("QUERY_CREATE_TABLE_Node", "
22 nodeid binary(16) primary key,
23 typeid binary(16) not null,
24 identifier text character set utf8,
29 define("QUERY_CREATE_TABLE_Edge", "
31 edgeid binary(16) primary key,
32 typeid binary(16) not null,
33 nodeid0 binary(16) not null,
34 nodeid1 binary(16) not null,
40 define("QUERY_ADD_Node", "
42 nodeid, typeid, identifier, modtimestamp
44 unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), ?, ?
47 define("QUERY_ADD_Node_TYPES", "sssi");
49 define("QUERY_ADD_Edge", "
51 edgeid, typeid, nodeid0, nodeid1, modtimestamp
53 unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), unhex(replace(?, '-', '')), ?
56 define("QUERY_ADD_Edge_TYPES", "ssssi");
58 define("QUERY_UPDATE_Node", "
60 typeid=unhex(replace(?, '-', '')), identifier=?, modtimestamp=?
62 nodeid=unhex(replace(?, '-', ''))
64 define("QUERY_UPDATE_Node_TYPES", "ssis");
66 define("QUERY_UPDATE_Edge", "
68 typeid=unhex(replace(?, '-', '')), nodeid0=unhex(replace(?, '-', '')), nodeid1=unhex(replace(?, '-', '')), modtimestamp=?
70 edgeid=unhex(replace(?, '-', ''))
72 define("QUERY_UPDATE_Edge_TYPES", "sssis");
74 define("QUERY_SELECT_ALL_Node", "select hex(nodeid), hex(typeid), identifier from Node");
75 define("QUERY_SELECT_ALL_Node_With_modtimestamp", "select hex(nodeid), hex(typeid), identifier, modtimestamp from Node");
76 define("QUERY_SELECT_ALL_Edge", "select hex(edgeid), hex(typeid), hex(nodeid0), hex(nodeid1) from Edge");
77 define("QUERY_SELECT_ALL_Edge_With_modtimestamp", "select hex(edgeid), hex(typeid), hex(nodeid0), hex(nodeid1), modtimestamp from Edge");
79 define("QUERY_SELECT_modified_Node", "select hex(nodeid), hex(typeid), identifier from Node WHERE modtimestamp>?");
80 define("QUERY_SELECT_modified_Node_TYPES", "i");
83 mysqli_report(MYSQLI_REPORT_ERROR);
88 if(isset($_GET['action'])){
89 $action = $_GET['action'];
90 if(strcmp($action, 'rebuild') == 0){
92 exitWithResponseCode("CEA95615-649C-4837-9E24-0C968FA57647", "OK");
93 } else if(strcmp($action, 'getallnode') == 0){
94 $stmt = $db->prepare(QUERY_SELECT_ALL_Node);
97 $stmt->store_result();
98 if($stmt->errno != 0){
99 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
102 $stmt->bind_result($uuid, $typeid, $ident);
103 while($stmt->fetch()){
105 getFormedUUIDString($uuid),
106 getFormedUUIDString($typeid),
113 echoMemoryDBNetworkTimestamp();
115 } else if(strcmp($action, 'getalledge') == 0){
116 $stmt = $db->prepare(QUERY_SELECT_ALL_Edge);
119 $stmt->store_result();
120 if($stmt->errno != 0){
121 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
124 $stmt->bind_result($uuid, $typeid, $nid0, $nid1);
125 while($stmt->fetch()){
127 getFormedUUIDString($uuid),
128 getFormedUUIDString($typeid),
129 getFormedUUIDString($nid0),
130 getFormedUUIDString($nid1)
136 echoMemoryDBNetworkTimestamp();
138 } else if(strcmp($action, 'getallnodemod') == 0){
139 if(isset($_GET['t'])){
144 $stmt = $db->prepare(QUERY_SELECT_modified_Node);
145 $stmt->bind_param(QUERY_SELECT_modified_Node_TYPES, $ts);
148 $stmt->store_result();
149 if($stmt->errno != 0){
150 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
153 $stmt->bind_result($uuid, $typeid, $ident);
154 while($stmt->fetch()){
156 getFormedUUIDString($uuid),
157 getFormedUUIDString($typeid),
164 echoMemoryDBNetworkTimestamp();
166 } else if(strcmp($action, 'viewallnode') == 0){
167 $stmt = $db->prepare(QUERY_SELECT_ALL_Node_With_modtimestamp);
170 $stmt->store_result();
171 if($stmt->errno != 0){
172 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
175 $stmt->bind_result($uuid, $typeid, $ident, $mts);
176 while($stmt->fetch()){
178 getFormedUUIDString($uuid),
179 getFormedUUIDString($typeid),
185 echo($stmt->num_rows);
187 exit(" OK " . getTimeStampMs());
188 } else if(strcmp($action, 'viewalledge') == 0){
189 $stmt = $db->prepare(QUERY_SELECT_ALL_Edge_With_modtimestamp);
192 $stmt->store_result();
193 if($stmt->errno != 0){
194 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
197 $stmt->bind_result($uuid, $typeid, $nid0, $nid1, $mts);
198 while($stmt->fetch()){
200 getFormedUUIDString($uuid),
201 getFormedUUIDString($typeid),
202 getFormedUUIDString($nid0),
203 getFormedUUIDString($nid1)
208 echo($stmt->num_rows);
210 exit(" OK " . getTimeStampMs());
211 } else if(strcmp($action, 'addnode') == 0){
212 if(isset($_GET['nid'])){
213 $nodeid = $_GET['nid'];
215 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid needed.");
217 if(isset($_GET['tid'])){
218 $typeid = $_GET['tid'];
220 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed.");
222 if(isset($_GET['ident'])){
223 $ident = $_GET['ident'];
225 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "ident needed.");
228 $stmt = $db->prepare(QUERY_ADD_Node);
229 $mts = getTimeStampMs();
230 $stmt->bind_param(QUERY_ADD_Node_TYPES, $nodeid, $typeid, $ident, $mts);
233 $stmt->store_result();
234 if($stmt->errno != 0){
235 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db));
238 exitWithResponseCode("cea95615-649c-4837-9e24-0c968fa57647", "OK");
239 } else if(strcmp($action, 'updatenode') == 0){
240 if(isset($_GET['nid'])){
241 $nodeid = $_GET['nid'];
243 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid needed.");
245 if(isset($_GET['tid'])){
246 $typeid = $_GET['tid'];
248 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed.");
250 if(isset($_GET['ident'])){
251 $ident = $_GET['ident'];
253 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "ident needed.");
256 $stmt = $db->prepare(QUERY_UPDATE_Node);
257 $mts = getTimeStampMs();
258 $stmt->bind_param(QUERY_UPDATE_Node_TYPES, $typeid, $ident, $mts, $nodeid);
261 $stmt->store_result();
262 if($stmt->errno != 0){
263 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db));
266 exitWithResponseCode("cea95615-649c-4837-9e24-0c968fa57647", "OK");
267 } else if(strcmp($action, 'addedge') == 0){
268 if(isset($_GET['eid'])){
269 $edgeid = $_GET['eid'];
271 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "edgeid needed.");
273 if(isset($_GET['tid'])){
274 $typeid = $_GET['tid'];
276 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed.");
278 if(isset($_GET['nid0'])){
279 $nid0 = $_GET['nid0'];
281 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid0 needed.");
283 if(isset($_GET['nid1'])){
284 $nid1 = $_GET['nid1'];
286 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid1 needed.");
289 $stmt = $db->prepare(QUERY_ADD_Edge);
290 $mts = getTimeStampMs();
291 $stmt->bind_param(QUERY_ADD_Edge_TYPES, $edgeid, $typeid, $nid0, $nid1, $mts);
294 $stmt->store_result();
295 if($stmt->errno != 0){
296 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db));
299 exitWithResponseCode("cea95615-649c-4837-9e24-0c968fa57647", "OK");
300 } else if(strcmp($action, 'updateedge') == 0){
301 if(isset($_GET['eid'])){
302 $edgeid = $_GET['eid'];
304 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "edgeid needed.");
306 if(isset($_GET['tid'])){
307 $typeid = $_GET['tid'];
309 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed.");
311 if(isset($_GET['nid0'])){
312 $nid0 = $_GET['nid0'];
314 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid0 needed.");
316 if(isset($_GET['nid1'])){
317 $nid1 = $_GET['nid1'];
319 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid1 needed.");
322 $stmt = $db->prepare(QUERY_UPDATE_Edge);
323 $mts = getTimeStampMs();
324 $stmt->bind_param(QUERY_UPDATE_Edge_TYPES, $typeid, $nid0, $nid1, $mts, $edgeid);
327 $stmt->store_result();
328 if($stmt->errno != 0){
329 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db));
332 exitWithResponseCode("cea95615-649c-4837-9e24-0c968fa57647", "OK");
337 exitWithResponseCode("b539657c-0fa6-49c2-afb0-13af5c7866ed");
339 function exitWithResponseCode($errid, $description = "")
341 echoNode("1eeb6d3d-751f-444f-91c8-ed940e65f8bd", $errid, $description);
345 function echoMemoryDBNetworkTimestamp()
348 "a2560a9c-dcf7-4746-ac14-347188518cf2",
349 "e3346fd4-ac17-41c3-b3c7-e04972e5c014",
354 function echoNode($nid, $tid, $ident)
356 echo('["' . $nid .'","' . $tid .'","' . $ident . '"]');
359 function echoEdge($eid, $tid, $nid0, $nid1)
361 echo('["' . $eid .'","' . $tid .'","' . $nid0 .'","' . $nid1 . '"]');
366 $db = new mysqli('localhost', DATABASE_USER, DATABASE_PWD, DATABASE_NAME);
368 if (mysqli_connect_error()) {
370 exitWithResponseCode("3A8CF3C8-E6B6-4A99-9134-343CA341B591", mysqli_connect_error());
374 $db->set_charset("utf8");
377 $stmt = $db->prepare("show tables");
380 $stmt->store_result();
381 if($stmt->errno != 0){
382 exitWithResponseCode("80FA2D65-9473-40B0-A3CE-159AE8E67017");
385 $stmt->bind_result($tablename);
387 while($stmt->fetch()){
388 if($tablename == "Node"){
391 if($tablename == "Edge"){
395 if(($found & 3) != 3){
403 function rebuildDB($db)
410 $stmt = $db->query("drop table if exists Node");
413 $stmt = $db->query("drop table if exists Edge");
420 $stmt = $db->query(QUERY_CREATE_TABLE_Node);
423 $stmt = $db->query(QUERY_CREATE_TABLE_Edge);
426 function getFormedUUIDString($str)
428 $str = strtolower($str);
430 substr($str, 0, 8) . "-" .
431 substr($str, 8, 4) . "-" .
432 substr($str, 12, 4) . "-" .
433 substr($str, 16, 4) . "-" .
438 function getTimeStampMs()
440 return ceil(microtime(true)*1000);