Path: blob/master/src/applications/diviner/cache/DivinerAtomCache.php
12256 views
<?php12final class DivinerAtomCache extends DivinerDiskCache {34private $fileHashMap;5private $atomMap;6private $symbolMap;7private $edgeSrcMap;8private $edgeDstMap;9private $graphMap;1011private $atoms = array();12private $writeAtoms = array();1314public function __construct($cache_directory) {15parent::__construct($cache_directory, 'diviner-atom-cache');16}1718public function delete() {19parent::delete();2021$this->fileHashMap = null;22$this->atomMap = null;23$this->atoms = array();2425return $this;26}2728/* -( File Hash Map )------------------------------------------------------ */293031public function getFileHashMap() {32if ($this->fileHashMap === null) {33$this->fileHashMap = $this->getCache()->getKey('file', array());34}35return $this->fileHashMap;36}3738public function addFileHash($file_hash, $atom_hash) {39$this->getFileHashMap();40$this->fileHashMap[$file_hash] = $atom_hash;41return $this;42}4344public function fileHashExists($file_hash) {45$map = $this->getFileHashMap();46return isset($map[$file_hash]);47}4849public function deleteFileHash($file_hash) {50if ($this->fileHashExists($file_hash)) {51$map = $this->getFileHashMap();52$atom_hash = $map[$file_hash];53unset($this->fileHashMap[$file_hash]);5455$this->deleteAtomHash($atom_hash);56}5758return $this;59}606162/* -( Atom Map )----------------------------------------------------------- */636465public function getAtomMap() {66if ($this->atomMap === null) {67$this->atomMap = $this->getCache()->getKey('atom', array());68}69return $this->atomMap;70}7172public function getAtom($atom_hash) {73if (!array_key_exists($atom_hash, $this->atoms)) {74$key = 'atom/'.$this->getHashKey($atom_hash);75$this->atoms[$atom_hash] = $this->getCache()->getKey($key);76}77return $this->atoms[$atom_hash];78}7980public function addAtom(array $atom) {81$hash = $atom['hash'];82$this->atoms[$hash] = $atom;8384$this->getAtomMap();85$this->atomMap[$hash] = true;8687$this->writeAtoms['atom/'.$this->getHashKey($hash)] = $atom;8889return $this;90}9192public function deleteAtomHash($atom_hash) {93$atom = $this->getAtom($atom_hash);94if ($atom) {95foreach ($atom['childHashes'] as $child_hash) {96$this->deleteAtomHash($child_hash);97}98}99100$this->getAtomMap();101unset($this->atomMap[$atom_hash]);102unset($this->writeAtoms[$atom_hash]);103104$this->getCache()->deleteKey('atom/'.$this->getHashKey($atom_hash));105106return $this;107}108109public function saveAtoms() {110$this->getCache()->setKeys(111array(112'file' => $this->getFileHashMap(),113'atom' => $this->getAtomMap(),114) + $this->writeAtoms);115$this->writeAtoms = array();116return $this;117}118119120/* -( Symbol Hash Map )---------------------------------------------------- */121122123public function getSymbolMap() {124if ($this->symbolMap === null) {125$this->symbolMap = $this->getCache()->getKey('symbol', array());126}127return $this->symbolMap;128}129130public function addSymbol($atom_hash, $symbol_hash) {131$this->getSymbolMap();132$this->symbolMap[$atom_hash] = $symbol_hash;133return $this;134}135136public function deleteSymbol($atom_hash) {137$this->getSymbolMap();138unset($this->symbolMap[$atom_hash]);139140return $this;141}142143public function saveSymbols() {144$this->getCache()->setKeys(145array(146'symbol' => $this->getSymbolMap(),147));148return $this;149}150151/* -( Edge Map )----------------------------------------------------------- */152153154public function getEdgeMap() {155if ($this->edgeDstMap === null) {156$this->edgeDstMap = $this->getCache()->getKey('edge', array());157$this->edgeSrcMap = array();158foreach ($this->edgeDstMap as $dst => $srcs) {159foreach ($srcs as $src => $ignored) {160$this->edgeSrcMap[$src][$dst] = true;161}162}163}164return $this->edgeDstMap;165}166167public function getEdgesWithDestination($symbol_hash) {168$this->getEdgeMap();169return array_keys(idx($this->edgeDstMap, $symbol_hash, array()));170}171172public function addEdges($node_hash, array $symbol_hash_list) {173$this->getEdgeMap();174$this->edgeSrcMap[$node_hash] = array_fill_keys($symbol_hash_list, true);175foreach ($symbol_hash_list as $symbol_hash) {176$this->edgeDstMap[$symbol_hash][$node_hash] = true;177}178return $this;179}180181public function deleteEdges($node_hash) {182$this->getEdgeMap();183foreach (idx($this->edgeSrcMap, $node_hash, array()) as $dst => $ignored) {184unset($this->edgeDstMap[$dst][$node_hash]);185if (empty($this->edgeDstMap[$dst])) {186unset($this->edgeDstMap[$dst]);187}188}189unset($this->edgeSrcMap[$node_hash]);190return $this;191}192193public function saveEdges() {194$this->getCache()->setKeys(195array(196'edge' => $this->getEdgeMap(),197));198return $this;199}200201202/* -( Graph Map )---------------------------------------------------------- */203204205public function getGraphMap() {206if ($this->graphMap === null) {207$this->graphMap = $this->getCache()->getKey('graph', array());208}209return $this->graphMap;210}211212public function deleteGraph($node_hash) {213$this->getGraphMap();214unset($this->graphMap[$node_hash]);215return $this;216}217218public function addGraph($node_hash, $graph_hash) {219$this->getGraphMap();220$this->graphMap[$node_hash] = $graph_hash;221return $this;222}223224public function saveGraph() {225$this->getCache()->setKeys(226array(227'graph' => $this->getGraphMap(),228));229return $this;230}231232}233234235