Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
Writer
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
8 / 8
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 commit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 deleteAll
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 deleteByQuery
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 deleteRecords
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 optimize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 save
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 write
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Solr Writer service
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Demian Katz 2013.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Solr
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\Solr;
31
32use VuFind\Db\Service\ChangeTrackerServiceInterface;
33use VuFindSearch\Backend\Solr\Command\WriteDocumentCommand;
34use VuFindSearch\Backend\Solr\Document\CommitDocument;
35use VuFindSearch\Backend\Solr\Document\DeleteDocument;
36use VuFindSearch\Backend\Solr\Document\DocumentInterface;
37use VuFindSearch\Backend\Solr\Document\OptimizeDocument;
38use VuFindSearch\ParamBag;
39use VuFindSearch\Service;
40
41use function func_get_args;
42
43/**
44 * Solr Writer service
45 *
46 * @category VuFind
47 * @package  Solr
48 * @author   Demian Katz <demian.katz@villanova.edu>
49 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
50 * @link     https://vufind.org/wiki/development Wiki
51 */
52class Writer
53{
54    /**
55     * Constructor
56     *
57     * @param Service                       $searchService Search service
58     * @param ChangeTrackerServiceInterface $changeTracker Change tracker database service
59     */
60    public function __construct(
61        protected Service $searchService,
62        protected ChangeTrackerServiceInterface $changeTracker
63    ) {
64    }
65
66    /**
67     * Commit the index.
68     *
69     * @param string $backend Backend ID
70     *
71     * @return void
72     */
73    public function commit($backend)
74    {
75        // Commit can take a long time -- use a custom timeout:
76        $this->write($backend, new CommitDocument(), 60 * 60);
77    }
78
79    /**
80     * Delete all records in the index.
81     *
82     * Note: This does not update the change tracker!
83     *
84     * @param string $backend Backend ID
85     *
86     * @return void
87     */
88    public function deleteAll($backend)
89    {
90        $this->deleteByQuery($backend, '*:*');
91    }
92
93    /**
94     * Delete records based on a Solr query.
95     *
96     * Note: This does not update the change tracker!
97     *
98     * @param string $backend Backend ID
99     * @param string $query   Delete query
100     *
101     * @return void
102     */
103    public function deleteByQuery($backend, $query)
104    {
105        $deleteDoc = new DeleteDocument();
106        $deleteDoc->addQuery($query);
107        $this->write($backend, $deleteDoc);
108    }
109
110    /**
111     * Delete an array of IDs from the specified search backend
112     *
113     * @param string $backend Backend ID
114     * @param array  $idList  Record IDs to delete
115     *
116     * @return void
117     */
118    public function deleteRecords($backend, $idList)
119    {
120        // Delete IDs:
121        $deleteDoc = new DeleteDocument();
122        $deleteDoc->addKeys($idList);
123        $result = $this->write($backend, $deleteDoc);
124
125        // Update change tracker:
126        foreach ($idList as $id) {
127            $this->changeTracker->markDeleted($result['core'], $id);
128        }
129    }
130
131    /**
132     * Optimize the index.
133     *
134     * @param string $backend Backend ID
135     *
136     * @return void
137     */
138    public function optimize($backend)
139    {
140        // Optimize can take a long time -- use a custom timeout:
141        $this->write($backend, new OptimizeDocument(), 60 * 60 * 24);
142    }
143
144    /**
145     * Save new record(s) to the index.
146     *
147     * @param string            $backend Backend ID
148     * @param DocumentInterface $doc     Document(s) to save
149     * @param string            $handler Update handler
150     * @param ParamBag          $params  Update handler parameters
151     *
152     * @return void
153     */
154    public function save(
155        $backend,
156        DocumentInterface $doc,
157        $handler = 'update',
158        ParamBag $params = null
159    ) {
160        $this->write($backend, $doc, null, $handler, $params);
161    }
162
163    /**
164     * Write a document to the search service. Return the result array from
165     * the command.
166     *
167     * @param string            $backend Backend ID
168     * @param DocumentInterface $doc     Document(s) to write
169     * @param ?int              $timeout Timeout value (null for default)
170     * @param string            $handler Handler to use
171     * @param ?ParamBag         $params  Additional backend params (optional)
172     *
173     * @return array
174     */
175    protected function write(
176        $backend,
177        DocumentInterface $doc,
178        $timeout = null,
179        $handler = 'update',
180        $params = null
181    ) {
182        $command = new WriteDocumentCommand(...func_get_args());
183        return $this->searchService->invoke($command)->getResult();
184    }
185}