Use commit timestamps for ElasticSearch and sort on last commit time.
[phorkie.git] / src / phorkie / Database / Adapter / Elasticsearch / Indexer.php
index dbe769e56a647e21fef33ebd0ce3d707649bc536..e93cfa7c4b93d13b209d6b09fbbbef462cf99cb3 100644 (file)
@@ -9,15 +9,18 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
     }
 
 
-    public function addRepo(Repository $repo, $crdate = null)
+    public function addRepo(Repository $repo, $crdate = null, $modate = null)
     {
         if ($crdate == null) {
             $crdate = time();
         }
-        $this->updateRepo($repo, $crdate);
+        if ($modate == null) {
+            $modate = time();
+        }
+        $this->updateRepo($repo, $crdate, $modate);
     }
 
-    public function updateRepo(Repository $repo, $crdate = null)
+    public function updateRepo(Repository $repo, $crdate = null, $modate = null)
     {
         //add repository
         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
@@ -35,6 +38,12 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
         if ($crdate !== null) {
             $repoData['crdate'] = gmdate('c', $crdate);
         }
+        if ($modate == null) {
+            $modate = $this->getMoDate($repo);
+        }
+        if ($modate !== null) {
+            $repoData['modate'] = gmdate('c', $modate);
+        }
 
         $r->setBody(json_encode((object)$repoData));
         $r->send();
@@ -83,12 +92,35 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
         return strtotime($json->_source->crdate);
     }
 
+    /**
+     * When updating the repository, we don't have a modification date.
+     * We need to keep it, but elasticsearch does not have a simple way
+     * to update some fields only (without using a custom script).
+     *
+     * @return integer Unix timestamp
+     */
+    protected function getMoDate(Repository $repo)
+    {
+        $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+            $this->searchInstance . 'repo/' . $repo->id,
+            \HTTP_Request2::METHOD_GET
+        );
+        $json = json_decode($r->send()->getBody());
+
+        if (!isset($json->_source->modate)) {
+            return null;
+        }
+
+        return strtotime($json->_source->modate);
+    }
+
     public function deleteAllRepos()
     {
         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
             $this->searchInstance . 'repo/_query',
             \HTTP_Request2::METHOD_DELETE
         );
+        $r->allow404 = true;
         $r->setBody(
             json_encode(
                 (object)array(
@@ -101,6 +133,7 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
             $this->searchInstance . 'file/_query',
             \HTTP_Request2::METHOD_DELETE
         );
+        $r->allow404 = true;
         $r->setBody(
             json_encode(
                 (object)array(
@@ -118,6 +151,7 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
             $this->searchInstance . 'repo/' . $repo->id,
             \HTTP_Request2::METHOD_DELETE
         );
+        $r->allow404 = true;
         $r->send();
 
         $this->deleteRepoFiles($repo);
@@ -130,6 +164,7 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
             $this->searchInstance . 'file/_query',
             \HTTP_Request2::METHOD_DELETE
         );
+        $r->allow404 = true;
         $r->setBody(
             json_encode(
                 (object)array(