/** * Invalidates according graph Uri entries, the result and all triple pattern. * * @param Query $queryObject All data according to this query will be invalidated. */ public function invalidateByQuery(Query $queryObject) { // log it $this->addToLog(array('method' => 'invalidateByQuery', 'parameter' => array('queryObject' => $queryObject))); $query = $queryObject->getQuery(); // load query cache container by given query $queryCacheContainer = $this->cache->load($query); /** * remove according query from the query list which belongs to one of the graph URI's in the query * cache container. */ if (true === is_array($queryCacheContainer['graph_uris'])) { foreach ($queryCacheContainer['graph_uris'] as $graphUri) { $queryList = $this->cache->load($graphUri); unset($queryList[$query]); // if graphUri entry is empty after the operation, remove it from the cache if (0 == count($queryList)) { $this->cache->remove($graphUri); // otherwise save updated entry } else { $this->cache->save($graphUri, $queryList); } } } // check for according triple pattern if (true === is_array($queryCacheContainer['triple_pattern'])) { foreach ($queryCacheContainer['triple_pattern'] as $patternKey) { $queryList = $this->cache->load($patternKey); unset($queryList[$query]); // if patternKey entry is empty after the operation, remove it from the cache if (0 == count($queryList)) { $this->cache->remove($patternKey); // otherwise save updated entry } else { $this->cache->save($patternKey, $queryList); } } } /** * Remove query cache container */ $this->cache->remove($query); }
/** * Create statements from query. * * @param Query $queryObject Query object which represents a SPARQL query. * @return StatementIterator StatementIterator object */ protected function getStatements(Query $queryObject) { $queryParts = $queryObject->getQueryParts(); $statementArray = array(); // if only triples, but no quads if (true === isset($queryParts['triple_pattern']) && false === isset($queryParts['quad_pattern'])) { foreach ($queryParts['triple_pattern'] as $pattern) { /** * Create Node instances for S, P and O to build a Statement instance later on */ $s = $this->createNodeByValueAndType($pattern['s'], $pattern['s_type']); $p = $this->createNodeByValueAndType($pattern['p'], $pattern['p_type']); $o = $this->createNodeByValueAndType($pattern['o'], $pattern['o_type']); $g = null; $statementArray[] = $this->statementFactory->createStatement($s, $p, $o, $g); } // if only quads, but not triples } elseif (false === isset($queryParts['triple_pattern']) && true === isset($queryParts['quad_pattern'])) { foreach ($queryParts['quad_pattern'] as $pattern) { /** * Create Node instances for S, P and O to build a Statement instance later on */ $s = $this->createNodeByValueAndType($pattern['s'], $pattern['s_type']); $p = $this->createNodeByValueAndType($pattern['p'], $pattern['p_type']); $o = $this->createNodeByValueAndType($pattern['o'], $pattern['o_type']); $g = $this->createNodeByValueAndType($pattern['g'], $pattern['g_type']); $statementArray[] = $this->statementFactory->createStatement($s, $p, $o, $g); } // found quads and triples } elseif (true === isset($queryParts['triple_pattern']) && true === isset($queryParts['quad_pattern'])) { throw new \Exception('Query contains quads and triples. That is not supported yet.'); // neither quads nor triples } else { throw new \Exception('Query contains neither quads nor triples.'); } return $this->statementIteratorFactory->createIteratorFromArray($statementArray); }