*/ abstract class DirectCollection extends AbstractCollection { /** @inheritdoc */ public function checkIntegrity(): void { $entityIndex = $this->getEntityIndex(); $tokenIndex = $this->getTokenIndex(0); if ($entityIndex->exists() && $tokenIndex->exists()) { $ec = count($entityIndex); $tc = count($tokenIndex); if ($ec !== $tc) { throw new IndexIntegrityException( "Entity count ($ec) != token count ($tc)" ); } } } /** * Store a single token for the given entity * * Takes the first token from the list and writes it directly at the entity's position * in the token index. An empty list stores an empty string. * * @param string $entity The name of the entity * @param string[] $tokens The list of tokens (only the first is used) * @return static * @throws IndexLockException * @throws IndexAccessException * @throws IndexWriteException */ public function addEntity(string $entity, array $tokens): static { if (!$this->isWritable) { throw new IndexLockException('Indexes not locked. Forgot to call lock()?'); } $entityIndex = $this->getEntityIndex(); $entityId = $entityIndex->accessCachedValue($entity); $token = $tokens[0] ?? ''; $tokenIndex = $this->getTokenIndex(0); $tokenIndex->changeRow($entityId, $token); $tokenIndex->save(); return $this; } /** * Get the token stored for the given entity * * @param string $entity The name of the entity * @return string The stored token, or empty string if none * @throws IndexAccessException * @throws IndexLockException * @throws IndexWriteException */ public function getToken(string $entity): string { $entityIndex = $this->getEntityIndex(); $entityId = $entityIndex->accessCachedValue($entity); $tokenIndex = $this->getTokenIndex(0); return $tokenIndex->retrieveRow($entityId); } /** @inheritdoc */ public function resolveTokenFrequencies(int $group, array $tokenIds): array { // In a DirectCollection, token RID = entity RID, frequency is always 1 $result = []; foreach ($tokenIds as $tokenId) { $result[$tokenId] = [$tokenId => 1]; } return $result; } /** @inheritdoc */ public function getEntitiesWithData(): array { $entityIndex = $this->getEntityIndex(); $tokenIndex = $this->getTokenIndex(0); $entities = []; foreach ($tokenIndex as $entityId => $token) { if ($token === '') continue; $name = $entityIndex->retrieveRow($entityId); if ($name !== '') { $entities[] = $name; } } return $entities; } /** * Not actually used, because we override addEntity() to directly write the token. * @inheritdoc */ protected function countTokens(array $tokens): array { $token = $tokens[0] ?? ''; return [$token => 1]; } }