xref: /dokuwiki/inc/Search/Collection/DirectCollection.php (revision 0b1bbbbb7d4e3c531cd255dbf878ce27d5967a0c)
1<?php
2
3namespace dokuwiki\Search\Collection;
4
5use dokuwiki\Search\Exception\IndexAccessException;
6use dokuwiki\Search\Exception\IndexLockException;
7use dokuwiki\Search\Exception\IndexWriteException;
8
9/**
10 * Abstract collection for direct 1:1 entity-token mappings
11 *
12 * In a direct collection each entity has exactly one token stored at the entity's position
13 * in the token index (entity.RID === token.RID). No frequency or reverse indexes are used.
14 *
15 * Example: each page has exactly one title.
16 *
17 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
18 * @author Andreas Gohr <andi@splitbrain.org>
19 */
20abstract class DirectCollection extends AbstractCollection
21{
22    /**
23     * Store a single token for the given entity
24     *
25     * Takes the first token from the list and writes it directly at the entity's position
26     * in the token index. An empty list stores an empty string.
27     *
28     * @param string $entity The name of the entity
29     * @param string[] $tokens The list of tokens (only the first is used)
30     * @return static
31     * @throws IndexLockException
32     * @throws IndexAccessException
33     * @throws IndexWriteException
34     */
35    public function addEntity(string $entity, array $tokens): static
36    {
37        if (!$this->isWritable) {
38            throw new IndexLockException('Indexes not locked. Forgot to call lock()?');
39        }
40
41        $entityIndex = $this->getEntityIndex();
42        $entityId = $entityIndex->accessCachedValue($entity);
43
44        $token = $tokens[0] ?? '';
45        $tokenIndex = $this->getTokenIndex('');
46        $tokenIndex->changeRow($entityId, $token);
47        $tokenIndex->save();
48
49        return $this;
50    }
51
52    /**
53     * Get the token stored for the given entity
54     *
55     * @param string $entity The name of the entity
56     * @return string The stored token, or empty string if none
57     * @throws IndexAccessException
58     * @throws IndexLockException
59     * @throws IndexWriteException
60     */
61    public function getToken(string $entity): string
62    {
63        $entityIndex = $this->getEntityIndex();
64        $entityId = $entityIndex->accessCachedValue($entity);
65
66        $tokenIndex = $this->getTokenIndex('');
67        return $tokenIndex->retrieveRow($entityId);
68    }
69
70    /**
71     * Not actually used, because we override addEntity() to directly write the token.
72     * @inheritdoc
73     */
74    protected function countTokens(array $tokens): array
75    {
76        $token = $tokens[0] ?? '';
77        return [$token => 1];
78    }
79}
80