-
Notifications
You must be signed in to change notification settings - Fork 0
/
LokiService.php
141 lines (118 loc) · 3.67 KB
/
LokiService.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
<?php
require_once 'DBService.php';
class LokiService {
private $lokiName;
private $stringData;
private $changes;
public function __construct($lokiName, $stringData = null, $changes = null) {
$this->lokiName = $lokiName;
$this->stringData = $stringData;
$this->changes = $changes;
}
public function getLokiAsString() {
$result = $this->stringData;
if ($this->changes != null) {
$this->stringData = $this->loadLoki();
$result = applyChanges();
}
return $result;
}
private function applyChanges() {
if ($this->stringData != null) {
$lokiAsObj = json_decode($this->stringData);
$collections = $lokiAsObj->collections;
$changesObj = json_decode($this->changes);
$this->updateCollections($lokiAsObj, $collections, $changesObj);
return json_encode($lokiAsObj);
}
return null;
}
public function saveLoki($str = null) {
$str = $str == null ? $this->getLokiAsString() : $str;
if ($str != null) {
$db = new DBService();
$conn = $db->createPDOConnection();
$sql = $sql = "SELECT name FROM lokidbs WHERE name=?";
$query = $conn->prepare($sql);
$query->execute(array($this->lokiName));
if ($query->rowCount() > 0) {
// update
$sql = "UPDATE lokidbs SET data = ? WHERE name = ?";
$query = $conn->prepare($sql);
$query->execute(array($str,$this->lokiName));
} else {
// insert
$sql = "INSERT INTO lokidbs (data, name) VALUES (?, ?)";
$query = $conn->prepare($sql);
$query->execute(array($str,$this->lokiName));
}
}
}
public function loadLoki() {
$db = new DBService();
$conn = $db->createPDOConnection();
$sql = "SELECT data FROM lokidbs where name = ?";
$query = $conn->prepare($sql);
$query->execute(array($this->lokiName));
if ($query->rowCount() > 0) {
$row = $query->fetch(PDO::FETCH_ASSOC);
$str = $row["data"];
return $str;
} else {
return null;
}
}
private function findCollection($arr, $name) {
foreach ($arr as $coll) {
if ($coll->name == $name) {
return $coll;
}
}
return null;
}
private function updateDocument(&$data, $obj) {
for ($i = 0; $i < count($data); $i ++) {
$docLokiId = $data[$i]->{'$loki'};
$objLokiId = $obj->{'$loki'};
if ($docLokiId == $objLokiId) {
$data[$i] = $obj;
return;
}
}
}
private function removeDocument(&$data, $objLokiId) {
for ($i = 0; $i < count($data); $i ++) {
$docLokiId = $data[$i]->{'$loki'};
if ($docLokiId == $objLokiId) {
array_splice($data, $i, 1);
return;
}
}
}
private function updateCollections(&$db, &$collections, $changes) {
foreach ($changes as $change) {
echo "<br>";
$coll = $this->findCollection($collections, $change->name);
if ($coll != null) {
if ($change->operation == "I") {
array_push($coll->data, $change->obj);
$lokiId = $change->obj->{'$loki'};
$coll->maxId = $lokiId;
array_push($coll->idIndex, $lokiId);
} elseif ($change->operation == "U") {
$lokiId = $change->obj->{'$loki'};
$this->updateDocument($coll->data, $change->obj);
} elseif ($change->operation == "R") {
$lokiId = $change->obj;
$this->removeDocument($coll->data, $lokiId);
for ($i = 0; $i < count($coll->idIndex); $i ++) {
if ($coll->idIndex[$i] == $lokiId) {
array_splice($coll->idIndex, $i, 1);
break;
}
}
}
}
}
}
}