forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDivinerAtomCache.php
232 lines (181 loc) · 5.54 KB
/
DivinerAtomCache.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
<?php
final class DivinerAtomCache extends DivinerDiskCache {
private $fileHashMap;
private $atomMap;
private $symbolMap;
private $edgeSrcMap;
private $edgeDstMap;
private $graphMap;
private $atoms = array();
private $writeAtoms = array();
public function __construct($cache_directory) {
return parent::__construct($cache_directory, 'diviner-atom-cache');
}
public function delete() {
parent::delete();
$this->fileHashMap = null;
$this->atomMap = null;
$this->atoms = array();
return $this;
}
/* -( File Hash Map )------------------------------------------------------ */
public function getFileHashMap() {
if ($this->fileHashMap === null) {
$this->fileHashMap = $this->getCache()->getKey('file', array());
}
return $this->fileHashMap;
}
public function addFileHash($file_hash, $atom_hash) {
$this->getFileHashMap();
$this->fileHashMap[$file_hash] = $atom_hash;
return $this;
}
public function fileHashExists($file_hash) {
$map = $this->getFileHashMap();
return isset($map[$file_hash]);
}
public function deleteFileHash($file_hash) {
if ($this->fileHashExists($file_hash)) {
$map = $this->getFileHashMap();
$atom_hash = $map[$file_hash];
unset($this->fileHashMap[$file_hash]);
$this->deleteAtomHash($atom_hash);
}
return $this;
}
/* -( Atom Map )----------------------------------------------------------- */
public function getAtomMap() {
if ($this->atomMap === null) {
$this->atomMap = $this->getCache()->getKey('atom', array());
}
return $this->atomMap;
}
public function getAtom($atom_hash) {
if (!array_key_exists($atom_hash, $this->atoms)) {
$key = 'atom/'.$this->getHashKey($atom_hash);
$this->atoms[$atom_hash] = $this->getCache()->getKey($key);
}
return $this->atoms[$atom_hash];
}
public function addAtom(array $atom) {
$hash = $atom['hash'];
$this->atoms[$hash] = $atom;
$this->getAtomMap();
$this->atomMap[$hash] = true;
$this->writeAtoms['atom/'.$this->getHashKey($hash)] = $atom;
return $this;
}
public function deleteAtomHash($atom_hash) {
$atom = $this->getAtom($atom_hash);
if ($atom) {
foreach ($atom['childHashes'] as $child_hash) {
$this->deleteAtomHash($child_hash);
}
}
$this->getAtomMap();
unset($this->atomMap[$atom_hash]);
unset($this->writeAtoms[$atom_hash]);
$this->getCache()->deleteKey('atom/'.$this->getHashKey($atom_hash));
return $this;
}
public function saveAtoms() {
$this->getCache()->setKeys(
array(
'file' => $this->getFileHashMap(),
'atom' => $this->getAtomMap(),
) + $this->writeAtoms);
$this->writeAtoms = array();
return $this;
}
/* -( Symbol Hash Map )---------------------------------------------------- */
public function getSymbolMap() {
if ($this->symbolMap === null) {
$this->symbolMap = $this->getCache()->getKey('symbol', array());
}
return $this->symbolMap;
}
public function addSymbol($atom_hash, $symbol_hash) {
$this->getSymbolMap();
$this->symbolMap[$atom_hash] = $symbol_hash;
return $this;
}
public function deleteSymbol($atom_hash) {
$this->getSymbolMap();
unset($this->symbolMap[$atom_hash]);
return $this;
}
public function saveSymbols() {
$this->getCache()->setKeys(
array(
'symbol' => $this->getSymbolMap(),
));
return $this;
}
/* -( Edge Map )----------------------------------------------------------- */
public function getEdgeMap() {
if ($this->edgeDstMap === null) {
$this->edgeDstMap = $this->getCache()->getKey('edge', array());
$this->edgeSrcMap = array();
foreach ($this->edgeDstMap as $dst => $srcs) {
foreach ($srcs as $src => $ignored) {
$this->edgeSrcMap[$src][$dst] = true;
}
}
}
return $this->edgeDstMap;
}
public function getEdgesWithDestination($symbol_hash) {
$this->getEdgeMap();
return array_keys(idx($this->edgeDstMap, $symbol_hash, array()));
}
public function addEdges($node_hash, array $symbol_hash_list) {
$this->getEdgeMap();
$this->edgeSrcMap[$node_hash] = array_fill_keys($symbol_hash_list, true);
foreach ($symbol_hash_list as $symbol_hash) {
$this->edgeDstMap[$symbol_hash][$node_hash] = true;
}
return $this;
}
public function deleteEdges($node_hash) {
$this->getEdgeMap();
foreach (idx($this->edgeSrcMap, $node_hash, array()) as $dst => $ignored) {
unset($this->edgeDstMap[$dst][$node_hash]);
if (empty($this->edgeDstMap[$dst])) {
unset($this->edgeDstMap[$dst]);
}
}
unset($this->edgeSrcMap[$node_hash]);
return $this;
}
public function saveEdges() {
$this->getCache()->setKeys(
array(
'edge' => $this->getEdgeMap(),
));
return $this;
}
/* -( Graph Map )---------------------------------------------------------- */
public function getGraphMap() {
if ($this->graphMap === null) {
$this->graphMap = $this->getCache()->getKey('graph', array());
}
return $this->graphMap;
}
public function deleteGraph($node_hash) {
$this->getGraphMap();
unset($this->graphMap[$node_hash]);
return $this;
}
public function addGraph($node_hash, $graph_hash) {
$this->getGraphMap();
$this->graphMap[$node_hash] = $graph_hash;
return $this;
}
public function saveGraph() {
$this->getCache()->setKeys(
array(
'graph' => $this->getGraphMap(),
));
return $this;
}
}