xref: /dokuwiki/inc/Search/Collection/AbstractCollection.php (revision 9369b4a991666bc911474806b106d8958e79f4c1)
1f2bbffb5SAndreas Gohr<?php
2f2bbffb5SAndreas Gohr
3f2bbffb5SAndreas Gohrnamespace dokuwiki\Search\Collection;
4f2bbffb5SAndreas Gohr
5f2bbffb5SAndreas Gohruse dokuwiki\Search\Exception\IndexAccessException;
621fbd01bSAndreas Gohruse dokuwiki\Search\Exception\IndexIntegrityException;
7f2bbffb5SAndreas Gohruse dokuwiki\Search\Exception\IndexLockException;
895b16223SAndreas Gohruse dokuwiki\Search\Exception\IndexUsageException;
9f2bbffb5SAndreas Gohruse dokuwiki\Search\Exception\IndexWriteException;
1095b16223SAndreas Gohruse dokuwiki\Search\Index\AbstractIndex;
11f2bbffb5SAndreas Gohruse dokuwiki\Search\Index\FileIndex;
12f2bbffb5SAndreas Gohruse dokuwiki\Search\Index\Lock;
13f2bbffb5SAndreas Gohruse dokuwiki\Search\Index\MemoryIndex;
140a9fafedSAndreas Gohruse dokuwiki\Search\Index\TupleOps;
150a9fafedSAndreas Gohruse dokuwiki\Search\Tokenizer;
16f2bbffb5SAndreas Gohr
17f2bbffb5SAndreas Gohr/**
18f2bbffb5SAndreas Gohr * Abstract base class for index collections
19f2bbffb5SAndreas Gohr *
20f2bbffb5SAndreas Gohr * A collection manages a group of related indexes that together provide a specific search use case.
21f2bbffb5SAndreas Gohr * Every collection works with four index types: entity, token, frequency, and reverse.
22f2bbffb5SAndreas Gohr *
23f2bbffb5SAndreas Gohr * entity - the list of the main entities (eg. pages)
24f2bbffb5SAndreas Gohr * token - the list of tokens (eg. words) assigned to entities (can be split into multiple files)
25f2bbffb5SAndreas Gohr * frequency - how often a token appears on a entity (can be split into multiple files)
26f2bbffb5SAndreas Gohr * reverse - the list of tokens assigned to each entity
27f2bbffb5SAndreas Gohr *
28f2bbffb5SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
29f2bbffb5SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org>
30f2bbffb5SAndreas Gohr * @author Tom N Harris <tnharris@whoopdedo.org>
31f2bbffb5SAndreas Gohr */
32f2bbffb5SAndreas Gohrabstract class AbstractCollection
33f2bbffb5SAndreas Gohr{
3495b16223SAndreas Gohr    /** @var array<string|AbstractIndex> Index names or objects that have been successfully locked */
350a9fafedSAndreas Gohr    protected array $lockedIndexes = [];
360a9fafedSAndreas Gohr
37f2bbffb5SAndreas Gohr    /** @var bool Has a lock been acquired for all used indexes? */
38f2bbffb5SAndreas Gohr    protected bool $isWritable = false;
39f2bbffb5SAndreas Gohr
40f2bbffb5SAndreas Gohr    /**
41f2bbffb5SAndreas Gohr     * Initialize the collection with the names of the indexes it manages
42f2bbffb5SAndreas Gohr     *
4395b16223SAndreas Gohr     * Entity and token indexes can be passed as already instantiated AbstractIndex objects
4495b16223SAndreas Gohr     * for sharing between collections. When $idxToken is an object, $splitByLength must be false.
4595b16223SAndreas Gohr     *
4695b16223SAndreas Gohr     * @param string|AbstractIndex $idxEntity Name or instance of the primary entity index, eg. 'page'
4795b16223SAndreas Gohr     * @param string|AbstractIndex $idxToken Name or instance of the secondary entity index, eg. 'w' for words
48f2bbffb5SAndreas Gohr     * @param string $idxFrequency Base name of the frequency index, eg. 'i' for word frequencies
49f2bbffb5SAndreas Gohr     * @param string $idxReverse Name of the reverse index, eg. 'pageword'
50f2bbffb5SAndreas Gohr     * @param bool $splitByLength Whether to split token/frequency indexes by token length
5195b16223SAndreas Gohr     * @throws IndexUsageException
52f2bbffb5SAndreas Gohr     */
53f2bbffb5SAndreas Gohr    public function __construct(
5495b16223SAndreas Gohr        protected string|AbstractIndex $idxEntity,
5595b16223SAndreas Gohr        protected string|AbstractIndex $idxToken,
56d92c078cSAndreas Gohr        protected string $idxFrequency = '',
57d92c078cSAndreas Gohr        protected string $idxReverse = '',
58f2bbffb5SAndreas Gohr        protected bool $splitByLength = false
59*9369b4a9SAndreas Gohr    ) {
6095b16223SAndreas Gohr        if ($idxToken instanceof AbstractIndex && $splitByLength) {
6195b16223SAndreas Gohr            throw new IndexUsageException('Cannot split by length when using a pre-instantiated token index');
6295b16223SAndreas Gohr        }
63f2bbffb5SAndreas Gohr    }
64f2bbffb5SAndreas Gohr
65f2bbffb5SAndreas Gohr    /**
66f2bbffb5SAndreas Gohr     * Destructor
67f2bbffb5SAndreas Gohr     *
68f2bbffb5SAndreas Gohr     * Ensures locks are released when the class is destroyed
69f2bbffb5SAndreas Gohr     */
70f2bbffb5SAndreas Gohr    public function __destruct()
71f2bbffb5SAndreas Gohr    {
72f2bbffb5SAndreas Gohr        $this->unlock();
73f2bbffb5SAndreas Gohr    }
74f2bbffb5SAndreas Gohr
75f2bbffb5SAndreas Gohr    /**
76f2bbffb5SAndreas Gohr     * Lock all indexes for writing
77f2bbffb5SAndreas Gohr     *
78f2bbffb5SAndreas Gohr     * @return $this can be used for chaining
79f2bbffb5SAndreas Gohr     * @throws IndexLockException
80f2bbffb5SAndreas Gohr     */
81f2bbffb5SAndreas Gohr    public function lock(): static
82f2bbffb5SAndreas Gohr    {
83*9369b4a9SAndreas Gohr        foreach (
84*9369b4a9SAndreas Gohr            [
850a9fafedSAndreas Gohr            $this->idxEntity,
860a9fafedSAndreas Gohr            $this->idxToken,
870a9fafedSAndreas Gohr            $this->idxFrequency,
880a9fafedSAndreas Gohr            $this->idxReverse
89*9369b4a9SAndreas Gohr            ] as $idx
90*9369b4a9SAndreas Gohr        ) {
9195b16223SAndreas Gohr            if ($idx === '') continue;
92c66b5ec6SAndreas Gohr            try {
9395b16223SAndreas Gohr                if ($idx instanceof AbstractIndex) {
9495b16223SAndreas Gohr                    $idx->lock();
9595b16223SAndreas Gohr                } else {
9695b16223SAndreas Gohr                    Lock::acquire($idx);
9795b16223SAndreas Gohr                }
98*9369b4a9SAndreas Gohr                $this->lockedIndexes[] = $idx;
99c66b5ec6SAndreas Gohr            } catch (IndexLockException $e) {
100c66b5ec6SAndreas Gohr                $this->unlock();
101c66b5ec6SAndreas Gohr                throw $e;
102c66b5ec6SAndreas Gohr            }
103f2bbffb5SAndreas Gohr        }
104f2bbffb5SAndreas Gohr        $this->isWritable = true;
105f2bbffb5SAndreas Gohr        return $this;
106f2bbffb5SAndreas Gohr    }
107f2bbffb5SAndreas Gohr
108f2bbffb5SAndreas Gohr    /**
1090a9fafedSAndreas Gohr     * Unlock all indexes that were successfully locked
110f2bbffb5SAndreas Gohr     *
11183b3acccSAndreas Gohr     * @return static
112f2bbffb5SAndreas Gohr     */
11383b3acccSAndreas Gohr    public function unlock(): static
114f2bbffb5SAndreas Gohr    {
11595b16223SAndreas Gohr        foreach ($this->lockedIndexes as $idx) {
11695b16223SAndreas Gohr            if ($idx instanceof AbstractIndex) {
11795b16223SAndreas Gohr                $idx->unlock();
11895b16223SAndreas Gohr            } else {
11995b16223SAndreas Gohr                Lock::release($idx);
12095b16223SAndreas Gohr            }
121f2bbffb5SAndreas Gohr        }
1220a9fafedSAndreas Gohr        $this->lockedIndexes = [];
123f2bbffb5SAndreas Gohr        $this->isWritable = false;
12483b3acccSAndreas Gohr        return $this;
125f2bbffb5SAndreas Gohr    }
126f2bbffb5SAndreas Gohr
127f2bbffb5SAndreas Gohr    /**
12895b16223SAndreas Gohr     * @return AbstractIndex
129c66b5ec6SAndreas Gohr     * @throws IndexLockException
130f2bbffb5SAndreas Gohr     */
13195b16223SAndreas Gohr    public function getEntityIndex(): AbstractIndex
132f2bbffb5SAndreas Gohr    {
13395b16223SAndreas Gohr        if ($this->idxEntity instanceof AbstractIndex) {
13495b16223SAndreas Gohr            return $this->idxEntity;
13595b16223SAndreas Gohr        }
136f2bbffb5SAndreas Gohr        return new FileIndex($this->idxEntity, '', $this->isWritable);
137f2bbffb5SAndreas Gohr    }
138f2bbffb5SAndreas Gohr
139f2bbffb5SAndreas Gohr    /**
1406734bb8cSAndreas Gohr     * @param int $group Index group (0 for non-split, token length for split)
14195b16223SAndreas Gohr     * @return AbstractIndex
142c66b5ec6SAndreas Gohr     * @throws IndexLockException
143f2bbffb5SAndreas Gohr     */
1446734bb8cSAndreas Gohr    public function getTokenIndex(int $group = 0): AbstractIndex
145f2bbffb5SAndreas Gohr    {
14695b16223SAndreas Gohr        if ($this->idxToken instanceof AbstractIndex) {
14795b16223SAndreas Gohr            return $this->idxToken;
14895b16223SAndreas Gohr        }
1496734bb8cSAndreas Gohr        return new MemoryIndex($this->idxToken, $this->groupToSuffix($group), $this->isWritable);
150f2bbffb5SAndreas Gohr    }
151f2bbffb5SAndreas Gohr
152f2bbffb5SAndreas Gohr    /**
1536734bb8cSAndreas Gohr     * @param int $group Index group (0 for non-split, token length for split)
15495b16223SAndreas Gohr     * @return AbstractIndex
155c66b5ec6SAndreas Gohr     * @throws IndexLockException
156f2bbffb5SAndreas Gohr     */
1576734bb8cSAndreas Gohr    public function getFrequencyIndex(int $group = 0): AbstractIndex
158f2bbffb5SAndreas Gohr    {
1596734bb8cSAndreas Gohr        return new MemoryIndex($this->idxFrequency, $this->groupToSuffix($group), $this->isWritable);
160f2bbffb5SAndreas Gohr    }
161f2bbffb5SAndreas Gohr
162f2bbffb5SAndreas Gohr    /**
16395b16223SAndreas Gohr     * @return AbstractIndex
164c66b5ec6SAndreas Gohr     * @throws IndexLockException
165f2bbffb5SAndreas Gohr     */
16695b16223SAndreas Gohr    public function getReverseIndex(): AbstractIndex
167f2bbffb5SAndreas Gohr    {
168f2bbffb5SAndreas Gohr        return new FileIndex($this->idxReverse, '', $this->isWritable);
169f2bbffb5SAndreas Gohr    }
170f2bbffb5SAndreas Gohr
171f2bbffb5SAndreas Gohr    /**
1726734bb8cSAndreas Gohr     * Whether this collection splits token/frequency indexes by token length
1736734bb8cSAndreas Gohr     *
1746734bb8cSAndreas Gohr     * @return bool
1756734bb8cSAndreas Gohr     */
1766734bb8cSAndreas Gohr    public function isSplitByLength(): bool
1776734bb8cSAndreas Gohr    {
1786734bb8cSAndreas Gohr        return $this->splitByLength;
1796734bb8cSAndreas Gohr    }
1806734bb8cSAndreas Gohr
1816734bb8cSAndreas Gohr    /**
1826734bb8cSAndreas Gohr     * Convert a logical group number to the index file suffix
1836734bb8cSAndreas Gohr     *
1846734bb8cSAndreas Gohr     * Group 0 represents non-split indexes (suffix '') while positive integers
1856734bb8cSAndreas Gohr     * represent split-by-length indexes (suffix = the length).
1866734bb8cSAndreas Gohr     *
1876734bb8cSAndreas Gohr     * @param int $group
1886734bb8cSAndreas Gohr     * @return string The file suffix ('' for group 0, the group number as string otherwise)
1896734bb8cSAndreas Gohr     * @throws IndexUsageException when group does not match the collection's split mode
1906734bb8cSAndreas Gohr     */
1916734bb8cSAndreas Gohr    protected function groupToSuffix(int $group): string
1926734bb8cSAndreas Gohr    {
1936734bb8cSAndreas Gohr        if ($group === 0 && $this->splitByLength) {
1946734bb8cSAndreas Gohr            throw new IndexUsageException('Group 0 is not valid for split-by-length collections');
1956734bb8cSAndreas Gohr        }
1966734bb8cSAndreas Gohr        if ($group !== 0 && !$this->splitByLength) {
1976734bb8cSAndreas Gohr            throw new IndexUsageException("Group $group is not valid for non-split collections");
1986734bb8cSAndreas Gohr        }
1996734bb8cSAndreas Gohr        return $group === 0 ? '' : (string)$group;
2006734bb8cSAndreas Gohr    }
2016734bb8cSAndreas Gohr
2026734bb8cSAndreas Gohr    /**
2036734bb8cSAndreas Gohr     * Resolve token IDs to entity frequencies
2046734bb8cSAndreas Gohr     *
2056734bb8cSAndreas Gohr     * Given a set of token IDs from a specific index group, returns the entities
2066734bb8cSAndreas Gohr     * that have those tokens and their frequencies. This encapsulates the frequency
2076734bb8cSAndreas Gohr     * index access so that subclasses (e.g. DirectCollection) can provide alternative
2086734bb8cSAndreas Gohr     * mappings.
2096734bb8cSAndreas Gohr     *
2106734bb8cSAndreas Gohr     * @param int $group Index group (0 for non-split, token length for split)
2116734bb8cSAndreas Gohr     * @param int[] $tokenIds The token IDs to resolve
2126734bb8cSAndreas Gohr     * @return array [tokenId => [entityId => frequency, ...], ...]
2136734bb8cSAndreas Gohr     */
2146734bb8cSAndreas Gohr    public function resolveTokenFrequencies(int $group, array $tokenIds): array
2156734bb8cSAndreas Gohr    {
2166734bb8cSAndreas Gohr        $freqIndex = $this->getFrequencyIndex($group);
2176734bb8cSAndreas Gohr        if (!$freqIndex->exists()) return [];
218*9369b4a9SAndreas Gohr        return array_map(TupleOps::parseTuples(...), $freqIndex->retrieveRows($tokenIds));
2196734bb8cSAndreas Gohr    }
2206734bb8cSAndreas Gohr
2216734bb8cSAndreas Gohr    /**
2226734bb8cSAndreas Gohr     * Return all entity names that have data in this collection
2236734bb8cSAndreas Gohr     *
2246734bb8cSAndreas Gohr     * @return string[] entity names
2256734bb8cSAndreas Gohr     */
2266734bb8cSAndreas Gohr    public function getEntitiesWithData(): array
2276734bb8cSAndreas Gohr    {
2286734bb8cSAndreas Gohr        $entityIndex = $this->getEntityIndex();
2296734bb8cSAndreas Gohr
2306734bb8cSAndreas Gohr        // collect entity IDs from all frequency index groups
23121fbd01bSAndreas Gohr        $max = $this->splitByLength ? $this->getTokenIndexMaximum() : 0;
23221fbd01bSAndreas Gohr        $groups = $this->splitByLength ? ($max > 0 ? range(1, $max) : []) : [0];
2336734bb8cSAndreas Gohr
2346734bb8cSAndreas Gohr        $entityIds = [];
2356734bb8cSAndreas Gohr        foreach ($groups as $group) {
2366734bb8cSAndreas Gohr            $freqIndex = $this->getFrequencyIndex($group);
2376734bb8cSAndreas Gohr            if (!$freqIndex->exists()) continue;
2386734bb8cSAndreas Gohr            foreach ($freqIndex as $line) {
239*9369b4a9SAndreas Gohr                foreach (array_keys(TupleOps::parseTuples($line)) as $entityId) {
2406734bb8cSAndreas Gohr                    $entityIds[$entityId] = true;
2416734bb8cSAndreas Gohr                }
2426734bb8cSAndreas Gohr            }
2436734bb8cSAndreas Gohr        }
2446734bb8cSAndreas Gohr
2456734bb8cSAndreas Gohr        $names = $entityIndex->retrieveRows(array_keys($entityIds));
2466734bb8cSAndreas Gohr        return array_values(array_filter($names, static fn($v) => $v !== ''));
2476734bb8cSAndreas Gohr    }
2486734bb8cSAndreas Gohr
2496734bb8cSAndreas Gohr    /**
250f2bbffb5SAndreas Gohr     * Maximum suffix for the token indexes (eg. max word length currently stored)
251f2bbffb5SAndreas Gohr     *
252f2bbffb5SAndreas Gohr     * @return int
253c66b5ec6SAndreas Gohr     * @throws IndexLockException
254f2bbffb5SAndreas Gohr     */
255f2bbffb5SAndreas Gohr    public function getTokenIndexMaximum(): int
256f2bbffb5SAndreas Gohr    {
2576734bb8cSAndreas Gohr        if ($this->idxToken instanceof AbstractIndex) {
2586734bb8cSAndreas Gohr            return $this->idxToken->max();
2596734bb8cSAndreas Gohr        }
2606734bb8cSAndreas Gohr        return (new MemoryIndex($this->idxToken, ''))->max();
261f2bbffb5SAndreas Gohr    }
262f2bbffb5SAndreas Gohr
263f2bbffb5SAndreas Gohr    /**
26421fbd01bSAndreas Gohr     * Check the structural integrity of this collection's indexes
26521fbd01bSAndreas Gohr     *
26621fbd01bSAndreas Gohr     * Verifies that paired indexes have matching line counts:
26721fbd01bSAndreas Gohr     * - token == frequency (per group, both keyed by token RID)
26821fbd01bSAndreas Gohr     * - entity == reverse (both keyed by entity RID)
26921fbd01bSAndreas Gohr     *
27021fbd01bSAndreas Gohr     * @throws IndexIntegrityException when a structural inconsistency is found
27121fbd01bSAndreas Gohr     */
27221fbd01bSAndreas Gohr    public function checkIntegrity(): void
27321fbd01bSAndreas Gohr    {
27421fbd01bSAndreas Gohr        // Check token/frequency pairs
27521fbd01bSAndreas Gohr        $max = $this->splitByLength ? $this->getTokenIndexMaximum() : 0;
27621fbd01bSAndreas Gohr        $groups = $this->splitByLength ? ($max > 0 ? range(1, $max) : []) : [0];
27721fbd01bSAndreas Gohr
27821fbd01bSAndreas Gohr        foreach ($groups as $group) {
27921fbd01bSAndreas Gohr            $tokenIndex = $this->getTokenIndex($group);
28021fbd01bSAndreas Gohr            $freqIndex = $this->getFrequencyIndex($group);
28121fbd01bSAndreas Gohr
28221fbd01bSAndreas Gohr            if (!$tokenIndex->exists() && !$freqIndex->exists()) continue;
28321fbd01bSAndreas Gohr
28421fbd01bSAndreas Gohr            if ($tokenIndex->exists() !== $freqIndex->exists()) {
28521fbd01bSAndreas Gohr                throw new IndexIntegrityException(
28621fbd01bSAndreas Gohr                    "Group $group: missing " .
28721fbd01bSAndreas Gohr                    ($tokenIndex->exists() ? 'frequency' : 'token') . ' index'
28821fbd01bSAndreas Gohr                );
28921fbd01bSAndreas Gohr            }
29021fbd01bSAndreas Gohr
29121fbd01bSAndreas Gohr            $tc = count($tokenIndex);
29221fbd01bSAndreas Gohr            $fc = count($freqIndex);
29321fbd01bSAndreas Gohr            if ($tc !== $fc) {
29421fbd01bSAndreas Gohr                throw new IndexIntegrityException(
29521fbd01bSAndreas Gohr                    "Group $group: token count ($tc) != frequency count ($fc)"
29621fbd01bSAndreas Gohr                );
29721fbd01bSAndreas Gohr            }
29821fbd01bSAndreas Gohr        }
29921fbd01bSAndreas Gohr
30021fbd01bSAndreas Gohr        // Check entity/reverse pair
30121fbd01bSAndreas Gohr        $entityIndex = $this->getEntityIndex();
30221fbd01bSAndreas Gohr        $reverseIndex = $this->getReverseIndex();
30321fbd01bSAndreas Gohr        if ($entityIndex->exists() && $reverseIndex->exists()) {
30421fbd01bSAndreas Gohr            $ec = count($entityIndex);
30521fbd01bSAndreas Gohr            $rc = count($reverseIndex);
30621fbd01bSAndreas Gohr            if ($ec !== $rc) {
30721fbd01bSAndreas Gohr                throw new IndexIntegrityException(
30821fbd01bSAndreas Gohr                    "Entity count ($ec) != reverse count ($rc)"
30921fbd01bSAndreas Gohr                );
31021fbd01bSAndreas Gohr            }
31121fbd01bSAndreas Gohr        }
31221fbd01bSAndreas Gohr    }
31321fbd01bSAndreas Gohr
31421fbd01bSAndreas Gohr    /**
315f2bbffb5SAndreas Gohr     * Add or update the tokens for a given entity
316f2bbffb5SAndreas Gohr     *
317f2bbffb5SAndreas Gohr     * The given list of tokens replaces the previously stored list for that entity. An empty list removes the
318f2bbffb5SAndreas Gohr     * entity from the index.
319f2bbffb5SAndreas Gohr     *
320f2bbffb5SAndreas Gohr     * The update merges old and new token data. getReverseAssignments() returns all previously stored token IDs
321f2bbffb5SAndreas Gohr     * with a value of 0 (see parseReverseRecord). resolveTokens() returns the new token IDs with their values.
322f2bbffb5SAndreas Gohr     * After array_replace_recursive, tokens only in the old map keep value 0 — causing updateIndexes to delete
323f2bbffb5SAndreas Gohr     * them from the frequency index via TupleOps::updateTuple. Tokens in the new map overwrite with their value.
324f2bbffb5SAndreas Gohr     *
325f2bbffb5SAndreas Gohr     * @param string $entity The name of the entity
326f2bbffb5SAndreas Gohr     * @param string[] $tokens The list of tokens for this entity
32783b3acccSAndreas Gohr     * @return static
328f2bbffb5SAndreas Gohr     * @throws IndexAccessException
329f2bbffb5SAndreas Gohr     * @throws IndexWriteException
330f2bbffb5SAndreas Gohr     * @throws IndexLockException
331f2bbffb5SAndreas Gohr     */
33283b3acccSAndreas Gohr    public function addEntity(string $entity, array $tokens): static
333f2bbffb5SAndreas Gohr    {
334f2bbffb5SAndreas Gohr        if (!$this->isWritable) {
335f2bbffb5SAndreas Gohr            throw new IndexLockException('Indexes not locked. Forgot to call lock()?');
336f2bbffb5SAndreas Gohr        }
337f2bbffb5SAndreas Gohr
338f2bbffb5SAndreas Gohr        $entityIndex = $this->getEntityIndex();
339f2bbffb5SAndreas Gohr        $entityId = $entityIndex->accessCachedValue($entity);
340f2bbffb5SAndreas Gohr
341f2bbffb5SAndreas Gohr        $old = $this->getReverseAssignments($entity);
342f2bbffb5SAndreas Gohr        $new = $this->resolveTokens($tokens);
343f2bbffb5SAndreas Gohr
344f2bbffb5SAndreas Gohr        $merged = array_replace_recursive($old, $new);
345f2bbffb5SAndreas Gohr
346f2bbffb5SAndreas Gohr        $this->updateIndexes($merged, $entityId);
347f2bbffb5SAndreas Gohr        $this->saveReverseAssignments($entity, $merged);
34883b3acccSAndreas Gohr
34983b3acccSAndreas Gohr        return $this;
350f2bbffb5SAndreas Gohr    }
351f2bbffb5SAndreas Gohr
352f2bbffb5SAndreas Gohr    /**
353f2bbffb5SAndreas Gohr     * Resolve raw tokens into the two-level structure [group => [tokenId => frequency]]
354f2bbffb5SAndreas Gohr     *
355f2bbffb5SAndreas Gohr     * Calls countTokens() to get token frequencies (subclass responsibility), then groups
356f2bbffb5SAndreas Gohr     * by token length if splitByLength is enabled, or under '' if not. Finally resolves
357f2bbffb5SAndreas Gohr     * token strings to IDs via the appropriate token index.
358f2bbffb5SAndreas Gohr     *
359f2bbffb5SAndreas Gohr     * @param string[] $tokens The raw token list
360f2bbffb5SAndreas Gohr     * @return array [group => [tokenId => frequency, ...], ...]
361f2bbffb5SAndreas Gohr     * @throws IndexLockException
362f2bbffb5SAndreas Gohr     */
363f2bbffb5SAndreas Gohr    protected function resolveTokens(array $tokens): array
364f2bbffb5SAndreas Gohr    {
365f2bbffb5SAndreas Gohr        $counted = $this->countTokens($tokens);
366f2bbffb5SAndreas Gohr
367f2bbffb5SAndreas Gohr        // group tokens by their index suffix
368f2bbffb5SAndreas Gohr        $groups = [];
369f2bbffb5SAndreas Gohr        foreach ($counted as $token => $freq) {
3706734bb8cSAndreas Gohr            $group = $this->splitByLength ? Tokenizer::tokenLength($token) : 0;
371f2bbffb5SAndreas Gohr            $groups[$group][$token] = $freq;
372f2bbffb5SAndreas Gohr        }
373f2bbffb5SAndreas Gohr
374f2bbffb5SAndreas Gohr        // resolve token strings to IDs
375f2bbffb5SAndreas Gohr        $result = [];
376f2bbffb5SAndreas Gohr        foreach ($groups as $group => $tokenFreqs) {
377f2bbffb5SAndreas Gohr            $tokenIndex = $this->getTokenIndex($group);
378f2bbffb5SAndreas Gohr            $result[$group] = [];
379f2bbffb5SAndreas Gohr            foreach ($tokenFreqs as $token => $freq) {
380f2bbffb5SAndreas Gohr                $tokenId = $tokenIndex->getRowID((string)$token);
381f2bbffb5SAndreas Gohr                $result[$group][$tokenId] = $freq;
382f2bbffb5SAndreas Gohr            }
383f2bbffb5SAndreas Gohr            $tokenIndex->save();
384f2bbffb5SAndreas Gohr        }
385f2bbffb5SAndreas Gohr
386f2bbffb5SAndreas Gohr        return $result;
387f2bbffb5SAndreas Gohr    }
388f2bbffb5SAndreas Gohr
389f2bbffb5SAndreas Gohr    /**
390f2bbffb5SAndreas Gohr     * Count or deduplicate tokens and return their frequencies
391f2bbffb5SAndreas Gohr     *
392f2bbffb5SAndreas Gohr     * FrequencyCollections return actual occurrence counts.
393f2bbffb5SAndreas Gohr     * LookupCollections deduplicate and return 1 for each token.
394f2bbffb5SAndreas Gohr     *
395f2bbffb5SAndreas Gohr     * @param string[] $tokens The raw token list
396f2bbffb5SAndreas Gohr     * @return array [token => frequency, ...]
397f2bbffb5SAndreas Gohr     */
398f2bbffb5SAndreas Gohr    abstract protected function countTokens(array $tokens): array;
399f2bbffb5SAndreas Gohr
400f2bbffb5SAndreas Gohr    /**
401f2bbffb5SAndreas Gohr     * Get the token assignments for a given entity from the reverse index
402f2bbffb5SAndreas Gohr     *
403f2bbffb5SAndreas Gohr     * Returns the parsed reverse index record. The exact structure depends on the collection type.
404f2bbffb5SAndreas Gohr     *
405f2bbffb5SAndreas Gohr     * @param string $entity
406f2bbffb5SAndreas Gohr     * @return array
407f2bbffb5SAndreas Gohr     * @throws IndexAccessException
408f2bbffb5SAndreas Gohr     * @throws IndexWriteException
409c66b5ec6SAndreas Gohr     * @throws IndexLockException
410f2bbffb5SAndreas Gohr     */
411f2bbffb5SAndreas Gohr    public function getReverseAssignments(string $entity): array
412f2bbffb5SAndreas Gohr    {
413f2bbffb5SAndreas Gohr        $entityIndex = $this->getEntityIndex();
414f2bbffb5SAndreas Gohr        $entityId = $entityIndex->accessCachedValue($entity);
415f2bbffb5SAndreas Gohr
416f2bbffb5SAndreas Gohr        $reverseIndex = $this->getReverseIndex();
417f2bbffb5SAndreas Gohr        $record = $reverseIndex->retrieveRow($entityId);
418f2bbffb5SAndreas Gohr
419f2bbffb5SAndreas Gohr        if ($record === '') {
420f2bbffb5SAndreas Gohr            return [];
421f2bbffb5SAndreas Gohr        }
422f2bbffb5SAndreas Gohr
423f2bbffb5SAndreas Gohr        return $this->parseReverseRecord($record);
424f2bbffb5SAndreas Gohr    }
425f2bbffb5SAndreas Gohr
426f2bbffb5SAndreas Gohr    /**
427f2bbffb5SAndreas Gohr     * Store the reverse index info about what tokens are assigned to the entity
428f2bbffb5SAndreas Gohr     *
429f2bbffb5SAndreas Gohr     * @param string $entity
430f2bbffb5SAndreas Gohr     * @param array $data The assignment data to store
431f2bbffb5SAndreas Gohr     * @return void
432f2bbffb5SAndreas Gohr     * @throws IndexAccessException
433f2bbffb5SAndreas Gohr     * @throws IndexWriteException
434f2bbffb5SAndreas Gohr     * @throws IndexLockException
435f2bbffb5SAndreas Gohr     */
436f2bbffb5SAndreas Gohr    protected function saveReverseAssignments(string $entity, array $data): void
437f2bbffb5SAndreas Gohr    {
438f2bbffb5SAndreas Gohr        // remove tokens with frequency 0 (no longer assigned), then remove empty groups
439*9369b4a9SAndreas Gohr        $data = array_map(array_filter(...), $data);
440f2bbffb5SAndreas Gohr        $data = array_filter($data);
441f2bbffb5SAndreas Gohr
442f2bbffb5SAndreas Gohr        $record = $this->formatReverseRecord($data);
443f2bbffb5SAndreas Gohr
444f2bbffb5SAndreas Gohr        $entityIndex = $this->getEntityIndex();
445f2bbffb5SAndreas Gohr        $entityId = $entityIndex->accessCachedValue($entity);
446f2bbffb5SAndreas Gohr
447f2bbffb5SAndreas Gohr        $reverseIndex = $this->getReverseIndex();
448f2bbffb5SAndreas Gohr        $reverseIndex->changeRow($entityId, $record);
449f2bbffb5SAndreas Gohr    }
450f2bbffb5SAndreas Gohr
451f2bbffb5SAndreas Gohr    /**
452f2bbffb5SAndreas Gohr     * Parse a reverse index record into a two-level array
453f2bbffb5SAndreas Gohr     *
454f2bbffb5SAndreas Gohr     * The reverse index only stores which token IDs belong to an entity, not their frequencies. All values
455f2bbffb5SAndreas Gohr     * in the returned array are set to 0. This is intentional: when merged with new data in addEntity(),
456f2bbffb5SAndreas Gohr     * tokens absent from the new data retain 0, signaling deletion from the frequency index.
457f2bbffb5SAndreas Gohr     *
458f2bbffb5SAndreas Gohr     * For split collections the format is "group*tokenId:group*tokenId:..." where group is the token length.
459f2bbffb5SAndreas Gohr     * For non-split collections the group prefix is omitted: "tokenId:tokenId:..."
460f2bbffb5SAndreas Gohr     * This mirrors how TupleOps omits *1 for frequency 1.
461f2bbffb5SAndreas Gohr     *
462f2bbffb5SAndreas Gohr     * @param string $record The raw reverse index record
463f2bbffb5SAndreas Gohr     * @return array [group => [tokenId => 0, ...], ...]
464f2bbffb5SAndreas Gohr     */
465f2bbffb5SAndreas Gohr    protected function parseReverseRecord(string $record): array
466f2bbffb5SAndreas Gohr    {
467f2bbffb5SAndreas Gohr        $result = [];
468f2bbffb5SAndreas Gohr        foreach (explode(':', $record) as $entry) {
469f2bbffb5SAndreas Gohr            $parts = explode('*', $entry, 2);
470f2bbffb5SAndreas Gohr            $tokenId = array_pop($parts);
4716734bb8cSAndreas Gohr            $group = (int)(array_pop($parts) ?? 0);
472f2bbffb5SAndreas Gohr            $result[$group][$tokenId] = 0;
473f2bbffb5SAndreas Gohr        }
474f2bbffb5SAndreas Gohr        return $result;
475f2bbffb5SAndreas Gohr    }
476f2bbffb5SAndreas Gohr
477f2bbffb5SAndreas Gohr    /**
478f2bbffb5SAndreas Gohr     * Format a two-level array into a reverse index record string
479f2bbffb5SAndreas Gohr     *
480f2bbffb5SAndreas Gohr     * @param array $data [group => [tokenId => freq, ...], ...]
481f2bbffb5SAndreas Gohr     * @return string The formatted record
482f2bbffb5SAndreas Gohr     */
483f2bbffb5SAndreas Gohr    protected function formatReverseRecord(array $data): string
484f2bbffb5SAndreas Gohr    {
485f2bbffb5SAndreas Gohr        $parts = [];
486f2bbffb5SAndreas Gohr        foreach ($data as $group => $tokens) {
4876734bb8cSAndreas Gohr            $prefix = $group === 0 ? '' : "$group*";
488f2bbffb5SAndreas Gohr            foreach (array_keys($tokens) as $tokenId) {
489f2bbffb5SAndreas Gohr                $parts[] = $prefix . $tokenId;
490f2bbffb5SAndreas Gohr            }
491f2bbffb5SAndreas Gohr        }
492f2bbffb5SAndreas Gohr        return implode(':', $parts);
493f2bbffb5SAndreas Gohr    }
494f2bbffb5SAndreas Gohr
495f2bbffb5SAndreas Gohr    /**
496f2bbffb5SAndreas Gohr     * Update frequency indexes with the given data
497f2bbffb5SAndreas Gohr     *
498f2bbffb5SAndreas Gohr     * Iterates over the two-level structure [group => [tokenId => freq]] and updates the
499f2bbffb5SAndreas Gohr     * corresponding frequency index for each group. A frequency of 0 removes the entity
500f2bbffb5SAndreas Gohr     * from that token's frequency record.
501f2bbffb5SAndreas Gohr     *
502f2bbffb5SAndreas Gohr     * @param array $data [group => [tokenId => frequency, ...], ...]
503f2bbffb5SAndreas Gohr     * @param int $entityId The entity ID
504f2bbffb5SAndreas Gohr     * @throws IndexLockException
505f2bbffb5SAndreas Gohr     */
506f2bbffb5SAndreas Gohr    protected function updateIndexes(array $data, int $entityId): void
507f2bbffb5SAndreas Gohr    {
508f2bbffb5SAndreas Gohr        foreach ($data as $group => $tokens) {
509f2bbffb5SAndreas Gohr            $freqIndex = $this->getFrequencyIndex($group);
510f2bbffb5SAndreas Gohr            foreach ($tokens as $tokenId => $freq) {
511f2bbffb5SAndreas Gohr                $record = $freqIndex->retrieveRow($tokenId);
512f2bbffb5SAndreas Gohr                $record = TupleOps::updateTuple($record, $entityId, $freq);
513f2bbffb5SAndreas Gohr                $freqIndex->changeRow($tokenId, $record);
514f2bbffb5SAndreas Gohr            }
515f2bbffb5SAndreas Gohr            $freqIndex->save();
516f2bbffb5SAndreas Gohr        }
517f2bbffb5SAndreas Gohr    }
518f2bbffb5SAndreas Gohr}
519