19bd7d62fSAndreas Gohr<?php 29bd7d62fSAndreas Gohr 39bd7d62fSAndreas Gohrnamespace dokuwiki\Search\Index; 49bd7d62fSAndreas Gohr 5db8be586SAndreas Gohruse dokuwiki\Logger; 67fcedc39SAndreas Gohruse dokuwiki\Search\Exception\IndexLockException; 79bd7d62fSAndreas Gohruse dokuwiki\Search\Exception\IndexWriteException; 89bd7d62fSAndreas Gohr 99bd7d62fSAndreas Gohr/** 109bd7d62fSAndreas Gohr * Access to a single index file 119bd7d62fSAndreas Gohr * 129bd7d62fSAndreas Gohr * Access using this class always happens by loading the full index into memory. 13db8be586SAndreas Gohr * Changes can be made permanent explicitly via save(), but will also be 14db8be586SAndreas Gohr * auto-saved on destruction to prevent data loss when indexes are used in tandem 15db8be586SAndreas Gohr * (a new RID in one index may already be referenced by another). 169bd7d62fSAndreas Gohr * Should be used for small indexes that receive many changes at once. 179bd7d62fSAndreas Gohr */ 189bd7d62fSAndreas Gohrclass MemoryIndex extends AbstractIndex 199bd7d62fSAndreas Gohr{ 209369b4a9SAndreas Gohr /** @var string[] the raw data lines of the index, no newlines */ 219369b4a9SAndreas Gohr protected array $data = []; 229bd7d62fSAndreas Gohr 23b3cb0bc3SAndreas Gohr /** @var bool has the index been modified? */ 249369b4a9SAndreas Gohr protected bool $dirty = false; 25b3cb0bc3SAndreas Gohr 269bd7d62fSAndreas Gohr /** 279bd7d62fSAndreas Gohr * Loads the full contents of the index into memory 289bd7d62fSAndreas Gohr * 299bd7d62fSAndreas Gohr * @inheritdoc 309bd7d62fSAndreas Gohr */ 317fcedc39SAndreas Gohr public function __construct($idx, $suffix = '', $isWritable = false) 329bd7d62fSAndreas Gohr { 337fcedc39SAndreas Gohr parent::__construct($idx, $suffix, $isWritable); 34b3cb0bc3SAndreas Gohr if (!file_exists($this->filename)) { 35b3cb0bc3SAndreas Gohr return; 36b3cb0bc3SAndreas Gohr } 379bd7d62fSAndreas Gohr $this->data = file($this->filename, FILE_IGNORE_NEW_LINES); 389bd7d62fSAndreas Gohr } 399bd7d62fSAndreas Gohr 407fcedc39SAndreas Gohr /** 41db8be586SAndreas Gohr * Auto-save dirty data before releasing the lock 42db8be586SAndreas Gohr * 43db8be586SAndreas Gohr * When indexes are used in tandem, a new RID written to one index may already 44db8be586SAndreas Gohr * be referenced by other indexes that were saved. Losing unsaved data here 45db8be586SAndreas Gohr * would leave dangling references, causing silent index corruption. 46db8be586SAndreas Gohr * 47db8be586SAndreas Gohr * The try/catch is necessary because unlock() is called from __destruct() 48db8be586SAndreas Gohr * (in the parent class), and PHP destructors must not throw — a throw 49db8be586SAndreas Gohr * during exception unwinding causes a fatal error. 50c66b5ec6SAndreas Gohr * 51c66b5ec6SAndreas Gohr * @inheritdoc 527fcedc39SAndreas Gohr */ 539369b4a9SAndreas Gohr public function unlock(): void 547fcedc39SAndreas Gohr { 557fcedc39SAndreas Gohr if ($this->isDirty()) { 56db8be586SAndreas Gohr try { 57db8be586SAndreas Gohr $this->save(); 58db8be586SAndreas Gohr } catch (\Exception $e) { 59db8be586SAndreas Gohr Logger::error('MemoryIndex failed to save on unlock: ' . $e->getMessage()); 60db8be586SAndreas Gohr } 617fcedc39SAndreas Gohr } 62c66b5ec6SAndreas Gohr parent::unlock(); 637fcedc39SAndreas Gohr } 647fcedc39SAndreas Gohr 657fcedc39SAndreas Gohr /** 667fcedc39SAndreas Gohr * @inheritdoc 677fcedc39SAndreas Gohr * @throws IndexLockException 687fcedc39SAndreas Gohr */ 699369b4a9SAndreas Gohr public function changeRow(int $rid, string $value): void 709bd7d62fSAndreas Gohr { 717fcedc39SAndreas Gohr if (!$this->isWritable) throw new IndexLockException(); 727fcedc39SAndreas Gohr 739bd7d62fSAndreas Gohr if ($rid > count($this->data)) { 749bd7d62fSAndreas Gohr $this->data = array_pad($this->data, $rid, ''); 759bd7d62fSAndreas Gohr } 769bd7d62fSAndreas Gohr $this->data[$rid] = $value; 77b3cb0bc3SAndreas Gohr $this->dirty = true; 789bd7d62fSAndreas Gohr } 799bd7d62fSAndreas Gohr 80*06053dcaSAndreas Gohr /** @inheritdoc */ 819369b4a9SAndreas Gohr public function retrieveRow(int $rid): string 829bd7d62fSAndreas Gohr { 83*06053dcaSAndreas Gohr return $this->data[$rid] ?? ''; 849bd7d62fSAndreas Gohr } 859bd7d62fSAndreas Gohr 86d6396b6dSAndreas Gohr /** @inheritdoc */ 879369b4a9SAndreas Gohr public function retrieveRows(array $rids): array 889f63f003SAndreas Gohr { 899f63f003SAndreas Gohr $result = []; 909f63f003SAndreas Gohr foreach ($rids as $rid) { 919f63f003SAndreas Gohr if (isset($this->data[$rid])) $result[$rid] = $this->data[$rid]; 929f63f003SAndreas Gohr } 939f63f003SAndreas Gohr 949f63f003SAndreas Gohr return $result; 959f63f003SAndreas Gohr } 969f63f003SAndreas Gohr 979f63f003SAndreas Gohr /** @inheritdoc */ 989369b4a9SAndreas Gohr public function getRowIDs(array $values): array 99d6396b6dSAndreas Gohr { 1009369b4a9SAndreas Gohr $values = array_map(trim(...), $values); 101d6396b6dSAndreas Gohr $values = array_fill_keys($values, 1); // easier access as associative array 102d6396b6dSAndreas Gohr 103d6396b6dSAndreas Gohr $result = []; 104d6396b6dSAndreas Gohr $count = count($this->data); 105d6396b6dSAndreas Gohr for ($ln = 0; $ln < $count; $ln++) { 106d6396b6dSAndreas Gohr $line = $this->data[$ln]; 107d6396b6dSAndreas Gohr if (isset($values[$line])) { 108d6396b6dSAndreas Gohr $result[$line] = $ln; 109d6396b6dSAndreas Gohr unset($values[$line]); 110d6396b6dSAndreas Gohr } 111d6396b6dSAndreas Gohr } 112d6396b6dSAndreas Gohr 1137fcedc39SAndreas Gohr if (!$this->isWritable) return $result; 1147fcedc39SAndreas Gohr 115d6396b6dSAndreas Gohr // if there are still values, they have not been found and will be appended 116d6396b6dSAndreas Gohr foreach (array_keys($values) as $value) { 117d6396b6dSAndreas Gohr $this->data[] = $value; 118d6396b6dSAndreas Gohr $result[$value] = $ln++; 119b3cb0bc3SAndreas Gohr $this->dirty = true; 120d6396b6dSAndreas Gohr } 121d6396b6dSAndreas Gohr 122d6396b6dSAndreas Gohr return $result; 123d6396b6dSAndreas Gohr } 124d6396b6dSAndreas Gohr 12503a35633SAndreas Gohr /** @inheritdoc */ 1269369b4a9SAndreas Gohr public function search(string $re): array 12703a35633SAndreas Gohr { 12803a35633SAndreas Gohr return preg_grep($re, $this->data); 12903a35633SAndreas Gohr } 13003a35633SAndreas Gohr 1319bd7d62fSAndreas Gohr /** 1329bd7d62fSAndreas Gohr * Save the changed index back to its file 1339bd7d62fSAndreas Gohr * 134b3cb0bc3SAndreas Gohr * The method will check the internal dirty state and will only write when the index has actually been changed 135b3cb0bc3SAndreas Gohr * 1369bd7d62fSAndreas Gohr * @throws IndexWriteException 1377fcedc39SAndreas Gohr * @throws IndexLockException 1389bd7d62fSAndreas Gohr */ 1399369b4a9SAndreas Gohr public function save(): void 1409bd7d62fSAndreas Gohr { 1419bd7d62fSAndreas Gohr global $conf; 1429bd7d62fSAndreas Gohr 143b3cb0bc3SAndreas Gohr if (!$this->isDirty()) { 144b3cb0bc3SAndreas Gohr return; 145b3cb0bc3SAndreas Gohr } 146b3cb0bc3SAndreas Gohr 1477fcedc39SAndreas Gohr if (!$this->isWritable) throw new IndexLockException(); 1487fcedc39SAndreas Gohr 1499bd7d62fSAndreas Gohr $tempname = $this->filename . '.tmp'; 1509bd7d62fSAndreas Gohr 1519bd7d62fSAndreas Gohr $fh = @fopen($tempname, 'w'); 1529bd7d62fSAndreas Gohr if (!$fh) { 1539bd7d62fSAndreas Gohr throw new IndexWriteException("Failed to write $tempname"); 1549bd7d62fSAndreas Gohr } 1559bd7d62fSAndreas Gohr fwrite($fh, implode("\n", $this->data)); 1569369b4a9SAndreas Gohr if ($this->data !== []) { 1579bd7d62fSAndreas Gohr fwrite($fh, "\n"); 1589bd7d62fSAndreas Gohr } 1599bd7d62fSAndreas Gohr fclose($fh); 1609bd7d62fSAndreas Gohr 1619bd7d62fSAndreas Gohr if ($conf['fperm']) { 1629bd7d62fSAndreas Gohr chmod($tempname, $conf['fperm']); 1639bd7d62fSAndreas Gohr } 1649bd7d62fSAndreas Gohr 1659bd7d62fSAndreas Gohr if (!io_rename($tempname, $this->filename)) { 1669369b4a9SAndreas Gohr throw new IndexWriteException("Failed to write $this->filename"); 1679bd7d62fSAndreas Gohr } 168b3cb0bc3SAndreas Gohr 169b3cb0bc3SAndreas Gohr $this->dirty = false; 1709bd7d62fSAndreas Gohr } 1719bd7d62fSAndreas Gohr 172b3cb0bc3SAndreas Gohr /** 173b3cb0bc3SAndreas Gohr * Check if the index has been modified and needs to be saved 174b3cb0bc3SAndreas Gohr * @return bool 175b3cb0bc3SAndreas Gohr */ 1769369b4a9SAndreas Gohr public function isDirty(): bool 177b3cb0bc3SAndreas Gohr { 178b3cb0bc3SAndreas Gohr return $this->dirty; 179b3cb0bc3SAndreas Gohr } 18083b3acccSAndreas Gohr 18183b3acccSAndreas Gohr /** @inheritdoc */ 18221fbd01bSAndreas Gohr public function count(): int 18321fbd01bSAndreas Gohr { 18421fbd01bSAndreas Gohr return count($this->data); 18521fbd01bSAndreas Gohr } 18621fbd01bSAndreas Gohr 18721fbd01bSAndreas Gohr /** @inheritdoc */ 18883b3acccSAndreas Gohr public function getIterator(): \ArrayIterator 18983b3acccSAndreas Gohr { 19083b3acccSAndreas Gohr return new \ArrayIterator($this->data); 19183b3acccSAndreas Gohr } 1929bd7d62fSAndreas Gohr} 193