Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
IndexSinceLastRunService
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 runIndexing
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getResourceIterator
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
 updateLastIndexTime
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getLastIndexTime
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 getPersistence
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2018 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 */
21
22namespace oat\tao\model\search\index;
23
24use oat\tao\model\TaoOntology;
25use oat\tao\model\search\Search;
26use oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService;
27use oat\search\helper\SupportedOperatorHelper;
28use oat\tao\model\resources\ResourceIterator;
29
30/**
31 * Class IndexSinceLastRunService
32 *
33 * implementation of IndexService which do indexing of resourced updated or created since last launch of indexing.
34 *
35 * @package oat\tao\model\search\index
36 */
37class IndexSinceLastRunService extends IndexService
38{
39    public const OPTION_LASTRUN_STORE = 'lastrun_store';
40    public const OPTION_INDEX_SINCE_LAST_RUN = 'index_since_last_run';
41    public const LAST_LAUNCH_TIME_KEY = 'tao/IndexService:lastLaunchTime';
42
43    public function runIndexing(): int
44    {
45        $time = microtime(true);
46        $iterator = $this->getResourceIterator($this->getLastIndexTime(), $time);
47        $indexIterator = new IndexIterator($iterator);
48        $indexIterator->setServiceLocator($this->getServiceLocator());
49        $searchService = $this->getServiceLocator()->get(Search::SERVICE_ID);
50        $result = $searchService->index($indexIterator);
51        $this->updateLastIndexTime($time);
52        $this->logDebug($result . ' resources have been indexed by ' . static::class);
53        return $result;
54    }
55
56    /**
57     * @return \Iterator
58     * @param boolean $sinceLast load resources updated/created since last indexation
59     */
60    protected function getResourceIterator($from = null, $to = null)
61    {
62        if ($from === null || $from === 0) {
63            return parent::getResourceIterator();
64        }
65        if ($to === null) {
66            $to = microtime(true);
67        }
68        $search = $this->getServiceLocator()->get(ComplexSearchService::class);
69        $queryBuilder = $search->query();
70        $criteria = $queryBuilder->newQuery();
71        $criteria->addCriterion(
72            TaoOntology::PROPERTY_UPDATED_AT,
73            SupportedOperatorHelper::BETWEEN,
74            [$from, $to]
75        );
76        $iterator = new ResourceIterator($this->getIndexedClasses(), $criteria);
77        $iterator->setServiceLocator($this->getServiceLocator());
78        return $iterator;
79    }
80
81    /**
82     * Update time of the last indexation
83     * @throws \common_Exception
84     */
85    private function updateLastIndexTime($time)
86    {
87        $this->getPersistence()->set(self::LAST_LAUNCH_TIME_KEY, $time);
88    }
89
90    /**
91     * Get time of the last indexation. 0 if no time in the storage.
92     * @return integer
93     */
94    private function getLastIndexTime()
95    {
96        $result = $this->getPersistence()->get(self::LAST_LAUNCH_TIME_KEY);
97        return $result ? $result : 0;
98    }
99
100    /**
101     * @return \common_persistence_KeyValuePersistence
102     * @throws
103     */
104    private function getPersistence()
105    {
106        if (!$this->hasOption(self::OPTION_LASTRUN_STORE)) {
107            throw new \InvalidArgumentException('Persistence for ' . self::SERVICE_ID . ' is not configured');
108        }
109        $persistenceId = $this->getOption(self::OPTION_LASTRUN_STORE);
110        return $this
111            ->getServiceLocator()
112            ->get(\common_persistence_Manager::SERVICE_ID)
113            ->getPersistenceById($persistenceId);
114    }
115}