aboutsummaryrefslogtreecommitdiff
path: root/src/phorkie/Database/Adapter
diff options
context:
space:
mode:
authorChristian Weiske <cweiske@cweiske.de>2012-05-07 20:47:01 +0200
committerChristian Weiske <cweiske@cweiske.de>2012-05-07 20:47:01 +0200
commit7c5ea8dded583601f29c7ea9bcb507ecbdd854d6 (patch)
tree53426dba320005d5f058250e20e239d454080d76 /src/phorkie/Database/Adapter
parent3c83676016bce1727f0046f4aad7865be8a71fd4 (diff)
downloadphorkie-7c5ea8dded583601f29c7ea9bcb507ecbdd854d6.tar.gz
phorkie-7c5ea8dded583601f29c7ea9bcb507ecbdd854d6.zip
move elasticsearch code in one folder
Diffstat (limited to 'src/phorkie/Database/Adapter')
-rw-r--r--src/phorkie/Database/Adapter/Elasticsearch/Indexer.php121
-rw-r--r--src/phorkie/Database/Adapter/Elasticsearch/Search.php82
-rw-r--r--src/phorkie/Database/Adapter/Elasticsearch/Setup.php91
3 files changed, 294 insertions, 0 deletions
diff --git a/src/phorkie/Database/Adapter/Elasticsearch/Indexer.php b/src/phorkie/Database/Adapter/Elasticsearch/Indexer.php
new file mode 100644
index 0000000..d9d1032
--- /dev/null
+++ b/src/phorkie/Database/Adapter/Elasticsearch/Indexer.php
@@ -0,0 +1,121 @@
+<?php
+namespace phorkie;
+
+class Database_Adapter_Elasticsearch_Indexer
+{
+ public function __construct()
+ {
+ $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
+ }
+
+
+ public function addRepo(Repository $repo, $crdate = null)
+ {
+ if ($crdate == null) {
+ $crdate = time();
+ }
+ $this->updateRepo($repo, $crdate);
+ }
+
+ public function updateRepo(Repository $repo, $crdate = null)
+ {
+ //add repository
+ $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+ $this->searchInstance . 'repo/' . $repo->id,
+ \HTTP_Request2::METHOD_PUT
+ );
+ $repoData = array(
+ 'id' => $repo->id,
+ 'description' => $repo->getDescription(),
+ 'tstamp' => gmdate('c', time()),
+ );
+ if ($crdate !== null) {
+ $repoData['crdate'] = gmdate('c', $crdate);
+ }
+ $r->setBody(json_encode((object)$repoData));
+ $r->send();
+
+ //add files
+ //clean up before adding files; files might have been deleted
+ $this->deleteRepoFiles($repo);
+
+ foreach ($repo->getFiles() as $file) {
+ $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+ $this->searchInstance . 'file/?parent=' . $repo->id,
+ \HTTP_Request2::METHOD_POST
+ );
+ $r->setBody(
+ json_encode(
+ (object)array(
+ 'name' => $file->getFilename(),
+ 'extension' => $file->getExt(),
+ 'content' => $file->isText() ? $file->getContent() : '',
+ )
+ )
+ );
+ $r->send();
+ }
+ }
+
+ public function deleteAllRepos()
+ {
+ $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+ $this->searchInstance . 'repo/_query',
+ \HTTP_Request2::METHOD_DELETE
+ );
+ $r->setBody(
+ json_encode(
+ (object)array(
+ 'match_all' => (object)array()
+ )
+ )
+ );
+ $r->send();
+ $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+ $this->searchInstance . 'file/_query',
+ \HTTP_Request2::METHOD_DELETE
+ );
+ $r->setBody(
+ json_encode(
+ (object)array(
+ 'match_all' => (object)array()
+ )
+ )
+ );
+ $r->send();
+ }
+
+ public function deleteRepo(Repository $repo)
+ {
+ //delete repository from index
+ $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+ $this->searchInstance . 'repo/' . $repo->id,
+ \HTTP_Request2::METHOD_DELETE
+ );
+ $r->send();
+
+ $this->deleteRepoFiles($repo);
+ }
+
+ protected function deleteRepoFiles(Repository $repo)
+ {
+ //delete files of that repository
+ $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+ $this->searchInstance . 'file/_query',
+ \HTTP_Request2::METHOD_DELETE
+ );
+ $r->setBody(
+ json_encode(
+ (object)array(
+ 'field' => (object)array(
+ '_parent' => $repo->id
+ )
+ )
+ )
+ );
+ $r->send();
+ }
+
+}
+
+?>
diff --git a/src/phorkie/Database/Adapter/Elasticsearch/Search.php b/src/phorkie/Database/Adapter/Elasticsearch/Search.php
new file mode 100644
index 0000000..88ad4ac
--- /dev/null
+++ b/src/phorkie/Database/Adapter/Elasticsearch/Search.php
@@ -0,0 +1,82 @@
+<?php
+namespace phorkie;
+
+class Database_Adapter_Elasticsearch_Search
+{
+ public function __construct()
+ {
+ $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
+ }
+
+ /**
+ * Search for a given term and return repositories that contain it
+ * in their description, file names or file content
+ *
+ * @param string $term Search term
+ * @param integer $page Page of search results, starting with 0
+ * @param integer $perPage Number of results per page
+ *
+ * @return Search_Result Search result object
+ */
+ public function search($term, $page = 0, $perPage = 10)
+ {
+ $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+ $this->searchInstance . 'repo/_search',
+ \HTTP_Request2::METHOD_GET
+ );
+ $r->setBody(
+ json_encode(
+ (object)array(
+ 'from' => $page * $perPage,
+ 'size' => $perPage,
+ 'query' => (object)array(
+ 'bool' => (object)array(
+ 'should' => array(
+ (object)array(
+ 'query_string' => (object)array(
+ 'query' => $term,
+ 'default_operator' => 'AND'
+ ),
+ ),
+ (object)array(
+ 'has_child' => (object)array(
+ 'type' => 'file',
+ 'query' => (object)array(
+ 'query_string' => (object)array(
+ 'query' => $term,
+ 'default_operator' => 'AND'
+ )
+ )
+ )
+ )
+ )
+ )
+ )
+ )
+ )
+ );
+ $httpRes = $r->send();
+ $jRes = json_decode($httpRes->getBody());
+ if (isset($jRes->error)) {
+ throw new Exception(
+ 'Search exception: ' . $jRes->error, $jRes->status
+ );
+ }
+
+ $sres = new Search_Result();
+ $sres->results = $jRes->hits->total;
+ $sres->page = $page;
+ $sres->perPage = $perPage;
+
+ foreach ($jRes->hits->hits as $hit) {
+ $r = new Repository();
+ //FIXME: error handling. what about deleted repos?
+ $r->loadById($hit->_source->id);
+ $sres->repos[] = $r;
+ }
+
+ return $sres;
+ }
+}
+
+?>
diff --git a/src/phorkie/Database/Adapter/Elasticsearch/Setup.php b/src/phorkie/Database/Adapter/Elasticsearch/Setup.php
new file mode 100644
index 0000000..acbe77c
--- /dev/null
+++ b/src/phorkie/Database/Adapter/Elasticsearch/Setup.php
@@ -0,0 +1,91 @@
+<?php
+namespace phorkie;
+
+class Database_Adapter_Elasticsearch_Setup
+{
+ public function __construct()
+ {
+ $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
+ }
+
+ public function setup()
+ {
+ $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+ $this->searchInstance . 'repo/_mapping',
+ \HTTP_Request2::METHOD_DELETE
+ );
+ $r->send();
+
+ //create mapping
+ //mapping for repositories
+ $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+ $this->searchInstance . 'repo/_mapping',
+ \HTTP_Request2::METHOD_PUT
+ );
+ $r->setBody(
+ json_encode(
+ (object)array(
+ 'repo' => (object)array(
+ '_timestamp' => (object)array(
+ 'enabled' => true,
+ 'path' => 'tstamp',
+ ),
+ 'properties' => (object)array(
+ 'id' => (object)array(
+ 'type' => 'long'
+ ),
+ 'description' => (object)array(
+ 'type' => 'string',
+ 'boost' => 2.0
+ ),
+ 'crdate' => (object)array(
+ //creation date
+ 'type' => 'date',
+ ),
+ 'tstamp' => (object)array(
+ //modification date
+ 'type' => 'date',
+ )
+ )
+ )
+ )
+ )
+ );
+ $r->send();
+
+ //mapping for files
+ $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+ $this->searchInstance . 'file/_mapping',
+ \HTTP_Request2::METHOD_PUT
+ );
+ $r->setBody(
+ json_encode(
+ (object)array(
+ 'file' => (object)array(
+ '_parent' => (object)array(
+ 'type' => 'repo'
+ ),
+ 'properties' => (object)array(
+ 'name' => (object)array(
+ 'type' => 'string',
+ 'boost' => 1.5
+ ),
+ 'extension' => (object)array(
+ 'type' => 'string',
+ 'boost' => 1.0
+ ),
+ 'content' => (object)array(
+ 'type' => 'string',
+ 'boost' => 0.8
+ )
+ )
+ )
+ )
+ )
+ );
+ $r->send();
+ }
+
+}
+
+?>