[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class DivinerAtomCache extends DivinerDiskCache { 4 5 private $fileHashMap; 6 private $atomMap; 7 private $symbolMap; 8 private $edgeSrcMap; 9 private $edgeDstMap; 10 private $graphMap; 11 12 private $atoms = array(); 13 private $writeAtoms = array(); 14 15 public function __construct($cache_directory) { 16 return parent::__construct($cache_directory, 'diviner-atom-cache'); 17 } 18 19 public function delete() { 20 parent::delete(); 21 $this->fileHashMap = null; 22 $this->atomMap = null; 23 $this->atoms = array(); 24 25 return $this; 26 } 27 28 /* -( File Hash Map )------------------------------------------------------ */ 29 30 31 public function getFileHashMap() { 32 if ($this->fileHashMap === null) { 33 $this->fileHashMap = $this->getCache()->getKey('file', array()); 34 } 35 return $this->fileHashMap; 36 } 37 38 public function addFileHash($file_hash, $atom_hash) { 39 $this->getFileHashMap(); 40 $this->fileHashMap[$file_hash] = $atom_hash; 41 return $this; 42 } 43 44 public function fileHashExists($file_hash) { 45 $map = $this->getFileHashMap(); 46 return isset($map[$file_hash]); 47 } 48 49 public function deleteFileHash($file_hash) { 50 if ($this->fileHashExists($file_hash)) { 51 $map = $this->getFileHashMap(); 52 $atom_hash = $map[$file_hash]; 53 unset($this->fileHashMap[$file_hash]); 54 55 $this->deleteAtomHash($atom_hash); 56 } 57 58 return $this; 59 } 60 61 62 /* -( Atom Map )----------------------------------------------------------- */ 63 64 65 public function getAtomMap() { 66 if ($this->atomMap === null) { 67 $this->atomMap = $this->getCache()->getKey('atom', array()); 68 } 69 return $this->atomMap; 70 } 71 72 public function getAtom($atom_hash) { 73 if (!array_key_exists($atom_hash, $this->atoms)) { 74 $key = 'atom/'.$this->getHashKey($atom_hash); 75 $this->atoms[$atom_hash] = $this->getCache()->getKey($key); 76 } 77 return $this->atoms[$atom_hash]; 78 } 79 80 public function addAtom(array $atom) { 81 $hash = $atom['hash']; 82 $this->atoms[$hash] = $atom; 83 84 $this->getAtomMap(); 85 $this->atomMap[$hash] = true; 86 87 $this->writeAtoms['atom/'.$this->getHashKey($hash)] = $atom; 88 89 return $this; 90 } 91 92 public function deleteAtomHash($atom_hash) { 93 $atom = $this->getAtom($atom_hash); 94 if ($atom) { 95 foreach ($atom['childHashes'] as $child_hash) { 96 $this->deleteAtomHash($child_hash); 97 } 98 } 99 100 $this->getAtomMap(); 101 unset($this->atomMap[$atom_hash]); 102 unset($this->writeAtoms[$atom_hash]); 103 104 $this->getCache()->deleteKey('atom/'.$this->getHashKey($atom_hash)); 105 106 return $this; 107 } 108 109 public function saveAtoms() { 110 $this->getCache()->setKeys( 111 array( 112 'file' => $this->getFileHashMap(), 113 'atom' => $this->getAtomMap(), 114 ) + $this->writeAtoms); 115 $this->writeAtoms = array(); 116 return $this; 117 } 118 119 120 /* -( Symbol Hash Map )---------------------------------------------------- */ 121 122 123 public function getSymbolMap() { 124 if ($this->symbolMap === null) { 125 $this->symbolMap = $this->getCache()->getKey('symbol', array()); 126 } 127 return $this->symbolMap; 128 } 129 130 public function addSymbol($atom_hash, $symbol_hash) { 131 $this->getSymbolMap(); 132 $this->symbolMap[$atom_hash] = $symbol_hash; 133 return $this; 134 } 135 136 public function deleteSymbol($atom_hash) { 137 $this->getSymbolMap(); 138 unset($this->symbolMap[$atom_hash]); 139 140 return $this; 141 } 142 143 public function saveSymbols() { 144 $this->getCache()->setKeys( 145 array( 146 'symbol' => $this->getSymbolMap(), 147 )); 148 return $this; 149 } 150 151 /* -( Edge Map )----------------------------------------------------------- */ 152 153 154 public function getEdgeMap() { 155 if ($this->edgeDstMap === null) { 156 $this->edgeDstMap = $this->getCache()->getKey('edge', array()); 157 $this->edgeSrcMap = array(); 158 foreach ($this->edgeDstMap as $dst => $srcs) { 159 foreach ($srcs as $src => $ignored) { 160 $this->edgeSrcMap[$src][$dst] = true; 161 } 162 } 163 } 164 return $this->edgeDstMap; 165 } 166 167 public function getEdgesWithDestination($symbol_hash) { 168 $this->getEdgeMap(); 169 return array_keys(idx($this->edgeDstMap, $symbol_hash, array())); 170 } 171 172 public function addEdges($node_hash, array $symbol_hash_list) { 173 $this->getEdgeMap(); 174 $this->edgeSrcMap[$node_hash] = array_fill_keys($symbol_hash_list, true); 175 foreach ($symbol_hash_list as $symbol_hash) { 176 $this->edgeDstMap[$symbol_hash][$node_hash] = true; 177 } 178 return $this; 179 } 180 181 public function deleteEdges($node_hash) { 182 $this->getEdgeMap(); 183 foreach (idx($this->edgeSrcMap, $node_hash, array()) as $dst => $ignored) { 184 unset($this->edgeDstMap[$dst][$node_hash]); 185 if (empty($this->edgeDstMap[$dst])) { 186 unset($this->edgeDstMap[$dst]); 187 } 188 } 189 unset($this->edgeSrcMap[$node_hash]); 190 return $this; 191 } 192 193 public function saveEdges() { 194 $this->getCache()->setKeys( 195 array( 196 'edge' => $this->getEdgeMap(), 197 )); 198 return $this; 199 } 200 201 202 /* -( Graph Map )---------------------------------------------------------- */ 203 204 205 public function getGraphMap() { 206 if ($this->graphMap === null) { 207 $this->graphMap = $this->getCache()->getKey('graph', array()); 208 } 209 return $this->graphMap; 210 } 211 212 public function deleteGraph($node_hash) { 213 $this->getGraphMap(); 214 unset($this->graphMap[$node_hash]); 215 return $this; 216 } 217 218 public function addGraph($node_hash, $graph_hash) { 219 $this->getGraphMap(); 220 $this->graphMap[$node_hash] = $graph_hash; 221 return $this; 222 } 223 224 public function saveGraph() { 225 $this->getCache()->setKeys( 226 array( 227 'graph' => $this->getGraphMap(), 228 )); 229 return $this; 230 } 231 232 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |