xref: /dokuwiki/inc/Search/Index/MemoryIndex.php (revision d6396b6d927e3ff373c0aed3927ae61eaca8d537)
19bd7d62fSAndreas Gohr<?php
29bd7d62fSAndreas Gohr
39bd7d62fSAndreas Gohrnamespace dokuwiki\Search\Index;
49bd7d62fSAndreas Gohr
59bd7d62fSAndreas Gohruse dokuwiki\Search\Exception\IndexWriteException;
69bd7d62fSAndreas Gohr
79bd7d62fSAndreas Gohr/**
89bd7d62fSAndreas Gohr * Access to a single index file
99bd7d62fSAndreas Gohr *
109bd7d62fSAndreas Gohr * Access using this class always happens by loading the full index into memory.
119bd7d62fSAndreas Gohr * All modifications need to be explicitly made permanent using the save() method.
129bd7d62fSAndreas Gohr * Should be used for small indexes that receive many changes at once.
139bd7d62fSAndreas Gohr */
149bd7d62fSAndreas Gohrclass MemoryIndex extends AbstractIndex
159bd7d62fSAndreas Gohr{
169bd7d62fSAndreas Gohr
179bd7d62fSAndreas Gohr    /** @var string the raw data lines of the index, no newlines */
189bd7d62fSAndreas Gohr    protected $data;
199bd7d62fSAndreas Gohr
209bd7d62fSAndreas Gohr    /**
219bd7d62fSAndreas Gohr     * Loads the full contents of the index into memory
229bd7d62fSAndreas Gohr     *
239bd7d62fSAndreas Gohr     * @inheritdoc
249bd7d62fSAndreas Gohr     */
259bd7d62fSAndreas Gohr    public function __construct($idx, $suffix = '')
269bd7d62fSAndreas Gohr    {
279bd7d62fSAndreas Gohr        parent::__construct($idx, $suffix);
289bd7d62fSAndreas Gohr
299bd7d62fSAndreas Gohr        $this->data = [];
309bd7d62fSAndreas Gohr        if (!file_exists($this->filename)) return;
319bd7d62fSAndreas Gohr        $this->data = file($this->filename, FILE_IGNORE_NEW_LINES);
329bd7d62fSAndreas Gohr
339bd7d62fSAndreas Gohr    }
349bd7d62fSAndreas Gohr
359bd7d62fSAndreas Gohr    /** @inheritdoc */
369bd7d62fSAndreas Gohr    public function changeRow($rid, $value)
379bd7d62fSAndreas Gohr    {
389bd7d62fSAndreas Gohr        if ($rid > count($this->data)) {
399bd7d62fSAndreas Gohr            $this->data = array_pad($this->data, $rid, '');
409bd7d62fSAndreas Gohr        }
419bd7d62fSAndreas Gohr        $this->data[$rid] = $value;
429bd7d62fSAndreas Gohr    }
439bd7d62fSAndreas Gohr
449bd7d62fSAndreas Gohr    /** @inheritdoc */
459bd7d62fSAndreas Gohr    public function retrieveRow($rid)
469bd7d62fSAndreas Gohr    {
479bd7d62fSAndreas Gohr        if (isset($this->data[$rid])) return $this->data[$rid];
489bd7d62fSAndreas Gohr        return '';
499bd7d62fSAndreas Gohr    }
509bd7d62fSAndreas Gohr
51*d6396b6dSAndreas Gohr    /** @inheritdoc */
52*d6396b6dSAndreas Gohr    public function accessValues($values)
53*d6396b6dSAndreas Gohr    {
54*d6396b6dSAndreas Gohr        $values = array_map('trim', $values);
55*d6396b6dSAndreas Gohr        $values = array_fill_keys($values, 1); // easier access as associative array
56*d6396b6dSAndreas Gohr
57*d6396b6dSAndreas Gohr        $result = [];
58*d6396b6dSAndreas Gohr        $count = count($this->data);
59*d6396b6dSAndreas Gohr        for ($ln = 0; $ln < $count; $ln++) {
60*d6396b6dSAndreas Gohr            $line = $this->data[$ln];
61*d6396b6dSAndreas Gohr            if (isset($values[$line])) {
62*d6396b6dSAndreas Gohr                $result[$line] = $ln;
63*d6396b6dSAndreas Gohr                unset($values[$line]);
64*d6396b6dSAndreas Gohr            }
65*d6396b6dSAndreas Gohr        }
66*d6396b6dSAndreas Gohr
67*d6396b6dSAndreas Gohr        // if there are still values, they have not been found and will be appended
68*d6396b6dSAndreas Gohr        foreach (array_keys($values) as $value) {
69*d6396b6dSAndreas Gohr            $this->data[] = $value;
70*d6396b6dSAndreas Gohr            $result[$value] = $ln++;
71*d6396b6dSAndreas Gohr        }
72*d6396b6dSAndreas Gohr
73*d6396b6dSAndreas Gohr        return $result;
74*d6396b6dSAndreas Gohr    }
75*d6396b6dSAndreas Gohr
769bd7d62fSAndreas Gohr    /**
779bd7d62fSAndreas Gohr     * Save the changed index back to its file
789bd7d62fSAndreas Gohr     *
799bd7d62fSAndreas Gohr     * @throws IndexWriteException
809bd7d62fSAndreas Gohr     */
819bd7d62fSAndreas Gohr    public function save()
829bd7d62fSAndreas Gohr    {
839bd7d62fSAndreas Gohr        global $conf;
849bd7d62fSAndreas Gohr
859bd7d62fSAndreas Gohr        $tempname = $this->filename . '.tmp';
869bd7d62fSAndreas Gohr
879bd7d62fSAndreas Gohr        $fh = @fopen($tempname, 'w');
889bd7d62fSAndreas Gohr        if (!$fh) {
899bd7d62fSAndreas Gohr            throw new IndexWriteException("Failed to write $tempname");
909bd7d62fSAndreas Gohr        }
919bd7d62fSAndreas Gohr        fwrite($fh, implode("\n", $this->data));
929bd7d62fSAndreas Gohr        if (!empty($lines)) {
939bd7d62fSAndreas Gohr            fwrite($fh, "\n");
949bd7d62fSAndreas Gohr        }
959bd7d62fSAndreas Gohr        fclose($fh);
969bd7d62fSAndreas Gohr
979bd7d62fSAndreas Gohr        if ($conf['fperm']) {
989bd7d62fSAndreas Gohr            chmod($tempname, $conf['fperm']);
999bd7d62fSAndreas Gohr        }
1009bd7d62fSAndreas Gohr
1019bd7d62fSAndreas Gohr        if (!io_rename($tempname, $this->filename)) {
1029bd7d62fSAndreas Gohr            throw new IndexWriteException("Failed to write {$this->filename}");
1039bd7d62fSAndreas Gohr        }
1049bd7d62fSAndreas Gohr    }
1059bd7d62fSAndreas Gohr
1069bd7d62fSAndreas Gohr}
107