-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphDB.php
executable file
·733 lines (561 loc) · 16.3 KB
/
graphDB.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
<?php
/*
###### Graph Database Abstraction Layer ######
This module will allow keeping Graph data in a MySQL database
*/
require __DIR__ . '/vendor/autoload.php';
use Hashids\Hashids;
date_default_timezone_set('Asia/Kolkata');
$GLOBALS['HASHID_SALT'] = "MaceHub is the best";
/**
* A class that represents a node
*/
class Node
{
// For reloading all the properties and labels from the
// grpah incase the contents of the database is updated
public function reload()
{
$conn = $this->conn;
// Clearing out data from the array so that it doesn't messes up with data
$this->labels= [];
$this->properties = array();
if($this->internalID)
{
$id = mysqli_real_escape_string($conn,$this->internalID);
$sql1 = 'SELECT name FROM '.$this->gdb->gdb_table_nodes.' WHERE id='.$this->internalID.';';
$result = $conn->query($sql1);
$this->ID = $result->fetch_assoc()['name'];
}
else
{
// Finding internal ID of the node
$id = mysqli_real_escape_string($conn,$this->ID);
$sql1 = 'SELECT id FROM '.$this->gdb->gdb_table_nodes.' WHERE name="'.$this->ID.'";';
$result = $conn->query($sql1);
$this->internalID = $result->fetch_assoc()['id'];
}
// Loading labels
$sql2 = 'SELECT prop_value from '.$this->gdb->gdb_table_properties.' WHERE prop_type="l" AND e_id="'.$this->internalID.'";';
$result = $conn->query($sql2);
$this->labels = [];
while ($data = $result->fetch_assoc()) {
array_push($this->labels, $data['prop_value']);
}
// Loading properties
$sql3 = 'SELECT prop_name,prop_value FROM '.$this->gdb->gdb_table_properties.' WHERE prop_type="p" AND e_id="'.$this->internalID.'";';
$result = $conn->query($sql3);
$this->properties = array();
while ($data = $result->fetch_assoc()) {
$this->properties[$data['prop_name']] = $data['prop_value'];
}
// Loading outgoing relations
$sql4 = 'SELECT DISTINCT e1_id,e2_id FROM '.$this->gdb->gdb_table_relations.' WHERE e1_id='.$this->internalID.';';
$result = $conn->query($sql4);
$this->outgoingRelations = [];
while ($data = $result->fetch_assoc()) {
$a = new Relationship($this->gdb,$data['e1_id'],$data['e2_id'],$load=False,$ids=True);
array_push($this->outgoingRelations, $a);
}
// Loading incoming relations
$sql4 = 'SELECT DISTINCT e1_id,e2_id FROM '.$this->gdb->gdb_table_relations.' WHERE e2_id='.$this->internalID.';';
$result = $conn->query($sql4);
$this->incomingRelations = [];
while ($data = $result->fetch_assoc()) {
$a = new Relationship($this->gdb,$data['e1_id'],$data['e2_id'],$load=False,$ids=True);
array_push($this->incomingRelations, $a);
}
}
function __construct($gdb,$id,$load=True)
{
$this->gdb = $gdb;
$this->conn = $gdb->conn;
$this->internalID = False;
$this->labels = [];
$this->properties = array();
$this->outgoingRelations = [];
$this->incomingRelations = [];
$this->modifiedOn = "";
if(gettype($id)=="string") {
$this->ID = $id;
}
elseif (gettype($id)=="integer") {
$this->internalID = $id;
}
else {
$this->ID = $id;
}
// Load the properties of the Node from db
if ($load==True) {
$this->reload();
}
}
public function setProperty($key,$value)
{
$conn = $this->conn;
// Updating instance variable
$this->properties[$key] = $value;
// Updating database
$key = mysqli_real_escape_string($conn,$key);
$value = mysqli_real_escape_string($conn,$value);
$time = date('Y-m-d H:i:s', time());
$sql = 'INSERT INTO '.$this->gdb->gdb_table_properties.'(e_id,prop_name,prop_value,prop_type,modified_on) VALUES('.$this->internalID.',"'.$key.'","'.$value.'","p","'.$time.'");';
if($conn->query($sql)===True) {
return "ok";
}
else {
return "Error in setting the entity for property : ".$conn->error;
}
}
public function setLabel($label)
{
$conn = $this->conn;
// Updating instance variable
array_push($this->labels, $label);
// Updating database
$label = mysqli_real_escape_string($conn,$label);
$time = date('Y-m-d H:i:s', time());
$sql = 'INSERT INTO '.$this->gdb->gdb_table_properties.'(e_id,prop_name,prop_value,prop_type,modified_on) VALUES('.$this->internalID.',"","'.$label.'","l","'.$time.'");';
if($conn->query($sql)===True) {
return "ok";
}
else {
return "Error in setting label for entity : ".$conn->error;
}
}
public function getProperty($key)
{
return $this->properties[$key];
}
public function getLabel()
{
return $this->labels;
}
public function connectTo($node)
{
$a = new Relationship($this->gdb,$this,$node);
array_push($this->outgoingRelations, $a);
$this->gdb->numOfRelations += 1;
return $a;
}
}
/**
* Relationship class
*/
class Relationship
{
function reload()
{
$conn = $this->conn;
// Re setting all values of instance variables
$this->labels = [];
$this->properties = array();
// Loading properties
$e1_id = (int)$this->nodeID1;
$e2_id = (int)$this->nodeID2;
$sql = 'SELECT rel_name,rel_value FROM '.$this->gdb->gdb_table_relations.' WHERE rel_type="p" AND e1_id='.$e1_id.' AND e2_id='.$e2_id.';';
$result = $conn->query($sql);
while ($data = $result->fetch_assoc())
{
$this->properties[$data['rel_name']] = $data['rel_value'];
}
// Loading labels
$sql2 = 'SELECT rel_value from '.$this->gdb->gdb_table_relations.' WHERE rel_type="l" AND e1_id='.$e1_id.' AND e2_id='.$e2_id.';';
$result = $conn->query($sql2);
$this->labels = [];
while ($data = $result->fetch_assoc()) {
array_push($this->labels, $data['rel_value']);
}
}
function __construct($gdb,$node1,$node2,$load=True,$ids=False)
{
// All the instance variables
$this->gdb = $gdb;
$this->conn = $gdb->conn;
$this->nodeID1 = "";
$this->nodeID2 = "";
$this->labels = [];
$this->properties = array();
$this->modifiedOn = "";
if($ids==False) {
$this->nodeID1 = $node1->internalID;
$this->nodeID2 = $node2->internalID;
}
else {
$this->nodeID1 = $node1;
$this->nodeID2 = $node2;
}
if($load==True) {
$this->reload();
}
}
function setProperty($key,$value)
{
$conn = $this->conn;
$key = mysqli_real_escape_string($conn,$key);
$value = mysqli_real_escape_string($conn,$value);
$time = date('Y-m-d H:i:s', time());
$sql = 'INSERT INTO '.$this->gdb->gdb_table_relations.'(e1_id,e2_id,rel_name,rel_value,rel_type,modified_on) VALUES('.$this->nodeID1.','.$this->nodeID2.',"'.$key.'","'.$value.'","p","'.$time.'");';
if($conn->query($sql))
{
$this->properties[$key] = $value;
return "ok";
}
else {
return "Error in setting entity relation label : ".$conn->error;
}
}
function setLabel($label)
{
$conn = $this->conn;
$label = mysqli_real_escape_string($conn,$label);
$time = date('Y-m-d H:i:s', time());
$sql = 'INSERT INTO '.$this->gdb->gdb_table_relations.'(e1_id,e2_id,rel_name,rel_value,rel_type,modified_on) VALUES('.$this->nodeID1.','.$this->nodeID2.',"","'.$label.'","l","'.$time.'");';
if($conn->query($sql)) {
array_push($this->labels, $label);
return "ok";
}
else {
return "Error in setting entity relation label : ".$conn->error;
}
}
function getProperty($key)
{
return $this->properties[$key];
}
function getLabels()
{
return $this->labels;
}
}
/**
* Main class
*/
class GraphDB
{
/*
* Private method that deals with the creation of tables
* in a mysql database
*/
private function setupTables()
{
$conn = $this->conn;
$gdb_nodes = false;
$gdb_relations = false;
$gdb_properties = false;
$sql1 = 'CREATE TABLE IF NOT EXISTS '.$this->gdb_table_nodes.'(id INT(6) AUTO_INCREMENT PRIMARY KEY, name TEXT NOT NULL);';
$sql2 = 'CREATE TABLE IF NOT EXISTS '.$this->gdb_table_relations.'(id INT(6) AUTO_INCREMENT PRIMARY KEY, e1_id INT NOT NULL,e2_id INT NOT NULL,rel_name TEXT,rel_value TEXT,rel_type VARCHAR(255), modified_on TIMESTAMP);';
$sql3 = 'CREATE TABLE IF NOT EXISTS '.$this->gdb_table_properties.'(id INT(6) AUTO_INCREMENT PRIMARY KEY,e_id INT NOT NULL,prop_name TEXT,prop_value TEXT,prop_type VARCHAR(255) NOT NULL,modified_on TIMESTAMP);';
$errors = array(
'gdb_nodes' => "",
'gdb_relations' => "",
'gdb_properties' => ""
);
if ($conn->query($sql1)===True) {
$gdb_nodes = true;
}
else {
$errors['gdb_nodes'] = $conn->error;
}
if ($conn->query($sql2)===True) {
$gdb_relations = true;
}
else {
$errors['gdb_relations'] = $conn->error;
}
if ($conn->query($sql3)===True) {
$gdb_properties = true;
return "ok";
}
else {
$errors['gdb_properties'] = $conn->error;
}
if($gdb_nodes && $gdb_relations && $gdb_properties === True) {
return "ok";
}
else {
return $errors;
}
}
/**
* Creates the tables for storing the search results
*/
private function setupTempTables()
{
$conn = $this->conn;
$gdb_temp_nodes = false;
$gdb_temp_relations = false;
$gdb_temp_properties = false;
$sql1 = "CREATE TABLE IF NOT EXISTS gdb_temp_nodes(id INT(6) AUTO_INCREMENT PRIMARY KEY,group_id INT NOT NULL, name TEXT NOT NULL);";
$sql2 = "CREATE TABLE IF NOT EXISTS gdb_temp_relations(id INT(6) AUTO_INCREMENT PRIMARY KEY, group_id INT NOT NULL, e1_id INT NOT NULL,e2_id INT NOT NULL,rel_name TEXT,rel_value TEXT,rel_type VARCHAR(255), modified_on TIMESTAMP);";
$sql3 = "CREATE TABLE IF NOT EXISTS gdb_temp_properties(id INT(6) AUTO_INCREMENT PRIMARY KEY,group_id INT NOT NULL, e_id INT NOT NULL,prop_name TEXT,prop_value TEXT,prop_type VARCHAR(255) NOT NULL,modified_on TIMESTAMP);";
$errors = array(
'gdb_temp_nodes' => "",
'gdb_temp_relations' => "",
'gdb_temp_properties' => ""
);
if ($conn->query($sql1)===True) {
$gdb_temp_nodes = true;
}
else {
$errors['gdb_temp_nodes'] = $conn->error;
}
if ($conn->query($sql2)===True) {
$gdb_temp_relations = true;
}
else {
$errors['gdb_temp_relations'] = $conn->error;
}
if ($conn->query($sql3)===True) {
$gdb_temp_properties = true;
return "ok";
}
else {
$errors['gdb_temp_properties'] = $conn->error;
}
if($gdb_temp_nodes && $gdb_temp_relations && $gdb_temp_properties === True) {
return "ok";
}
else {
return $errors;
}
}
/*
* Constructor method
*/
function __construct($conn,$gdbname='a')
{
$this->gdbname = $gdbname;
//Table names
$this->gdb_table_nodes = "gdb_".$gdbname."_nodes";
$this->gdb_table_relations = "gdb_".$gdbname."_relations";
$this->gdb_table_properties = "gdb_".$gdbname."_properties";
$this->conn = $conn;
$this->numOfNodes = 0;
$this->numOfRelations = 0;
// Creating the MySQL tables for storing graphs
$this->setupTables();
$this->setupTempTables();
// Counting no. of nodes
$sql = 'SELECT * FROM '.$this->gdb_table_nodes.';';
$result = $conn->query($sql);
$this->numOfNodes = intval($result->num_rows);
// Counting no. of relations
$sql2 = 'SELECT DISTINCT e1_id,e2_id FROM '.$this->gdb_table_relations.';';
$result = $conn->query($sql2);
$this->numOfRelations = intval($result->num_rows);
}
/*
Creates new entity with the given name
*/
public function createNode()
{
$conn = $this->conn;
$this->numOfNodes = $this->numOfNodes + 1;
// Generating the node's hashid
$hashids = new Hashids($GLOBALS['HASHID_SALT'], 7); // 7 characters give us the power to express 3521.61 billion entities uniquely.
$hashid = $hashids->encode($this->numOfNodes);
$hashid = mysqli_real_escape_string($conn,$hashid);
$sql = 'INSERT INTO '.$this->gdb_table_nodes.'(name) VALUES("'.$hashid.'");';
if($conn->query($sql)===True){
$a = new Node($this, $hashid);
return $a;
}
else {
return "Error in creating new entity : " . $conn->error;
}
}
public function GQL()
{
$a = new GQL($this);
return $a;
}
}
/**
* For searching the graph database
*/
class GQL
{
function __construct($gdb)
{
$this->conn = $gdb->conn;
$this->gdb = $gdb;
$conn = $this->conn;
$this->nodeList = [];
}
function createNode()
{
$conn = $this->conn;
$a = new GQLNode($this->gdb);
array_push($this->nodeList, $a);
return $this->nodeList[sizeof($this->nodeList)-1];
}
function search()
{
// Exploring all nodes in GQL
$filteredIDs = [];
$searchResult = [];
foreach ($this->nodeList as $node)
{
$filPropIDs = [];
$filLabelIDs = [];
$filRelPropIDs = [];
$filRelLabelIDs = [];
$possibleNodeIDs = [];
$g = [];
// Checking for properties
if(sizeof($node->properties)>0)
{
foreach ($node->properties as $key => $value)
{
$sql = 'SELECT e_id FROM '.$this->gdb->gdb_table_properties.' WHERE prop_name="'.$key.'" AND prop_value="'.$value.'";';
$result = $this->conn->query($sql);
while ($data = $result->fetch_assoc()) {
array_push($filPropIDs, intval($data["e_id"]));
}
$filPropIDs = array_unique($filPropIDs);
}
array_push($possibleNodeIDs, $filPropIDs);
}
// Checking for labels
if(sizeof($node->labels)>0)
{
foreach ($node->labels as $label)
{
$sql2 = 'SELECT e_id FROM '.$this->gdb->gdb_table_properties.' WHERE prop_type="l" AND prop_value="'.$label.'";';
$result = $this->conn->query($sql2);
while ($data = $result->fetch_assoc()) {
array_push($filLabelIDs, intval($data["e_id"]));
}
$filLabelIDs = array_unique($filLabelIDs);
}
array_push($possibleNodeIDs, $filLabelIDs);
}
// Checking for relationships
foreach ($node->outgoingRelations as $rel)
{
// Checking realtionship properties
if(sizeof($rel->properties)>0)
{
foreach ($rel->properties as $key => $value)
{
$sql1 = 'SELECT e1_id FROM '.$this->gdb->gdb_table_relations.' WHERE rel_name="'.$key.'" AND rel_value="'.$value.'";';
$result = $this->conn->query($sql1);
while ($data = $result->fetch_assoc()) {
array_push($filRelPropIDs, intval($data['e1_id']));
}
$filRelPropIDs = array_unique($filRelPropIDs);
}
array_push($possibleNodeIDs, $filRelPropIDs);
}
// Checking relationship labels
if(sizeof($rel->labels)>0)
{
foreach ($rel->labels as $rellabel)
{
$sql2 = 'SELECT e1_id FROM '.$this->gdb->gdb_table_relations.' WHERE rel_type="l" AND rel_value="'.$rellabel.'";';
$result = $this->conn->query($sql2);
while ($data = $result->fetch_assoc()) {
array_push($filRelLabelIDs, intval($data['e1_id']));
}
$filRelLabelIDs = array_unique($filRelLabelIDs);
}
array_push($possibleNodeIDs, $filRelLabelIDs);
}
}
// Doing intersection of IDs
if(sizeof($possibleNodeIDs)==1)
{
$g = $possibleNodeIDs[0];
}
elseif (sizeof($possibleNodeIDs)==2)
{
$g = array_intersect($possibleNodeIDs[0], $possibleNodeIDs[1]);
}
elseif (sizeof($possibleNodeIDs)==3)
{
$g = array_intersect($possibleNodeIDs[0], $possibleNodeIDs[1],$possibleNodeIDs[2]);
}
elseif (sizeof($possibleNodeIDs)==4)
{
$g = array_intersect($possibleNodeIDs[0], $possibleNodeIDs[1], $possibleNodeIDs[2], $possibleNodeIDs[3]);
}
array_push($filteredIDs, $g);
spit();
spit("Node properties : ");
var_dump($filPropIDs);
spit("Node Labels : ");
var_dump($filLabelIDs);
spit("Relationship properties : ");
var_dump($filRelPropIDs);
spit("Relationship Labels");
var_dump($filRelLabelIDs);
spit("Value of g is : ");
var_dump($g);
spit();
}
spit();
spit("Value of filteredIDs : ");
var_dump($filteredIDs);
// Generating real nodes using the filteredIDs array
foreach ($filteredIDs as $node) {
foreach ($node as $x) {
$g = new Node($this->gdb,$x);
array_push($searchResult, $g);
}
}
return $searchResult;
}
}
class GQLNode
{
function __construct()
{
$this->properties = array();
$this->outgoingRelations = [];
$this->labels = [];
}
function setLabel($label)
{
array_push($this->labels, $label);
return $this;
}
function setProperty($key,$value)
{
$this->properties[$key] = $value;
return $this;
}
function getLabel()
{
return $this->labels;
}
function getProperty($key)
{
return $this->properties[$key];
}
function connectTo($node)
{
$a = new GQLRelationship($this,$node);
array_push($this->outgoingRelations, $a);
return $this->outgoingRelations[sizeof($this->outgoingRelations)-1];
}
}
class GQLRelationship
{
function __construct($node1,$node2)
{
$this->properties = array();
$this->labels = [];
}
function setLabel($label)
{
array_push($this->labels, $label);
return $this;
}
function setProperty($key,$value)
{
$this->properties[$key] = $value;
return $this;
}
}
?>