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");
67 define("QUERY_SELECT_ALL_Node", "select hex(nodeid), hex(typeid), identifier from Node");
68 define("QUERY_SELECT_ALL_Node_With_modtimestamp", "select hex(nodeid), hex(typeid), identifier, modtimestamp from Node");
69 define("QUERY_SELECT_ALL_Edge", "select hex(edgeid), hex(typeid), hex(nodeid0), hex(nodeid1) from Edge");
71 define("QUERY_SELECT_modified_Node", "select hex(nodeid), hex(typeid), identifier from Node WHERE modtimestamp>?");
72 define("QUERY_SELECT_modified_Node_TYPES", "i");
75 mysqli_report(MYSQLI_REPORT_ERROR);
80 if(isset($_GET['action'])){
81 $action = $_GET['action'];
82 if(strcmp($action, 'rebuild') == 0){
84 exitWithResponseCode("CEA95615-649C-4837-9E24-0C968FA57647", "OK");
85 } else if(strcmp($action, 'getallnode') == 0){
86 $stmt = $db->prepare(QUERY_SELECT_ALL_Node);
89 $stmt->store_result();
90 if($stmt->errno != 0){
91 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
94 $stmt->bind_result($uuid, $typeid, $ident);
95 while($stmt->fetch()){
97 getFormedUUIDString($uuid),
98 getFormedUUIDString($typeid),
105 echoMemoryDBNetworkTimestamp();
107 } else if(strcmp($action, 'getnodemod') == 0){
108 if(isset($_GET['t'])){
113 $stmt = $db->prepare(QUERY_SELECT_modified_Node);
114 $stmt->bind_param(QUERY_SELECT_modified_Node_TYPES, $ts);
117 $stmt->store_result();
118 if($stmt->errno != 0){
119 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
122 $stmt->bind_result($uuid, $typeid, $ident);
123 while($stmt->fetch()){
125 getFormedUUIDString($uuid),
126 getFormedUUIDString($typeid),
133 echoMemoryDBNetworkTimestamp();
135 } else if(strcmp($action, 'viewallnode') == 0){
136 $stmt = $db->prepare(QUERY_SELECT_ALL_Node_With_modtimestamp);
139 $stmt->store_result();
140 if($stmt->errno != 0){
141 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B");
144 $stmt->bind_result($uuid, $typeid, $ident, $mts);
145 while($stmt->fetch()){
147 getFormedUUIDString($uuid),
148 getFormedUUIDString($typeid),
154 echo($stmt->num_rows);
156 exit(" OK " . getTimeStampMs());
157 } else if(strcmp($action, 'addnode') == 0){
158 if(isset($_GET['nid'])){
159 $nodeid = $_GET['nid'];
161 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid needed.");
163 if(isset($_GET['tid'])){
164 $typeid = $_GET['tid'];
166 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed.");
168 if(isset($_GET['ident'])){
169 $ident = $_GET['ident'];
171 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "ident needed.");
174 $stmt = $db->prepare(QUERY_ADD_Node);
175 $mts = getTimeStampMs();
176 $stmt->bind_param(QUERY_ADD_Node_TYPES, $nodeid, $typeid, $ident, $mts);
179 $stmt->store_result();
180 if($stmt->errno != 0){
181 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db));
184 exitWithResponseCode("cea95615-649c-4837-9e24-0c968fa57647", "OK");
185 } else if(strcmp($action, 'updatenode') == 0){
186 if(isset($_GET['nid'])){
187 $nodeid = $_GET['nid'];
189 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid needed.");
191 if(isset($_GET['tid'])){
192 $typeid = $_GET['tid'];
194 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed.");
196 if(isset($_GET['ident'])){
197 $ident = $_GET['ident'];
199 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "ident needed.");
202 $stmt = $db->prepare(QUERY_UPDATE_Node);
203 $mts = getTimeStampMs();
204 $stmt->bind_param(QUERY_UPDATE_Node_TYPES, $typeid, $ident, $mts, $nodeid);
207 $stmt->store_result();
208 if($stmt->errno != 0){
209 exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db));
212 exitWithResponseCode("cea95615-649c-4837-9e24-0c968fa57647", "OK");
217 exitWithResponseCode("b539657c-0fa6-49c2-afb0-13af5c7866ed");
219 function exitWithResponseCode($errid, $description = "")
221 echoNode("1eeb6d3d-751f-444f-91c8-ed940e65f8bd", $errid, $description);
225 function echoMemoryDBNetworkTimestamp()
228 "a2560a9c-dcf7-4746-ac14-347188518cf2",
229 "e3346fd4-ac17-41c3-b3c7-e04972e5c014",
234 function echoNode($nid, $tid, $ident)
236 echo('["' . $nid .'","' . $tid .'","' . $ident . '"]');
241 $db = new mysqli('localhost', DATABASE_USER, DATABASE_PWD, DATABASE_NAME);
243 if (mysqli_connect_error()) {
245 exitWithResponseCode("3A8CF3C8-E6B6-4A99-9134-343CA341B591", mysqli_connect_error());
249 $db->set_charset("utf8");
252 $stmt = $db->prepare("show tables");
255 $stmt->store_result();
256 if($stmt->errno != 0){
257 exitWithResponseCode("80FA2D65-9473-40B0-A3CE-159AE8E67017");
260 $stmt->bind_result($tablename);
262 while($stmt->fetch()){
263 if($tablename == "Node"){
266 if($tablename == "Edge"){
270 if(($found & 3) != 3){
278 function rebuildDB($db)
285 $stmt = $db->query("drop table if exists Node");
288 $stmt = $db->query("drop table if exists Edge");
295 $stmt = $db->query(QUERY_CREATE_TABLE_Node);
298 $stmt = $db->query(QUERY_CREATE_TABLE_Edge);
301 function getFormedUUIDString($str)
303 $str = strtolower($str);
305 substr($str, 0, 8) . "-" .
306 substr($str, 8, 4) . "-" .
307 substr($str, 12, 4) . "-" .
308 substr($str, 16, 4) . "-" .
313 function getTimeStampMs()
315 return ceil(microtime(true)*1000);