Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

Commit

Permalink
⚡ improved indexing performance
Browse files Browse the repository at this point in the history
✨ timeout for indexing, continue indexing on multiple pageloads if needed

Signed-off-by: Bruno Meilick <[email protected]>
  • Loading branch information
bnomei committed Apr 18, 2020
1 parent 702bf68 commit 6d541f9
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 210 deletions.
40 changes: 22 additions & 18 deletions classes/AutoID.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,33 @@ public static function generate(): ?string
return null;
}

private static $didIndexOnce;
public static function index(bool $force = false, ?Page $root = null): int
{
if (static::$didIndexOnce) {
return static::$didIndexOnce;
}
$timeout = intval(\option('bnomei.autoid.index.timeout'));
$indexed = 0;
if (AutoIDDatabase::singleton()->count() === 0 || $force) {
$break = time() + $timeout;

// site
if (self::push(site())) {
$indexed++;
}
if (! $root) {
$root = site();
}
if ($root->hasChildren()) {
foreach ($root->children()->index() as $page) {
if (self::push($page)) {
$indexed++;
}
if (!$force && time() >= $break) {
break;
}
$indexKey = $root ? $root->id() : 'site';
$indexing = kirby()->cache('bnomei.autoid')->get($indexKey, false);
if ($force || $indexing || AutoIDDatabase::singleton()->count() === 0) {
kirby()->cache('bnomei.autoid')->set($indexKey, true);
$break = time() + 2; //$timeout;
$indexer = new AutoIDIndexer($root);
foreach ($indexer->next() as $page) {
if (self::push($page)) {
$indexed++;
}
if (!$force && time() >= $break) {
$break = true;
break;
}
}
// if break then is still indexing
kirby()->cache('bnomei.autoid')->set($indexKey, $break === true);
}
static::$didIndexOnce = $indexed;
return $indexed;
}

Expand Down Expand Up @@ -91,6 +93,8 @@ public static function remove($object, bool $recursive = false): void

public static function flush(): void
{
static::$didIndexOnce = null;
kirby()->cache('bnomei.autoid')->set('site', false);
AutoIDDatabase::singleton()->flush();
}

Expand Down
8 changes: 8 additions & 0 deletions classes/AutoIDDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public function findByID($objectid): ?AutoIDItem
return null;
}

public function findByDiruri(string $objectid): ?AutoIDItem
{
foreach ($this->database->query("SELECT * FROM AUTOID WHERE diruri = '${objectid}'") as $obj) {
return new AutoIDItem($obj);
}
return null;
}

public function findByTemplate(string $template, string $rootId = ''): Collection
{
$rootId = ltrim($rootId, '/');
Expand Down
38 changes: 38 additions & 0 deletions classes/AutoIDIndexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Bnomei;

use Kirby\Cms\Page;

final class AutoIDIndexer
{
/**
* @var \Kirby\Cms\Site
*/
private $root;

public function __construct(?Page $root = null)
{
if (! $root) {
$root = kirby()->site();
}
$this->root = $root;
}

public function next(): \Generator
{
$next = $this->root;
while($next) {
yield $next;
foreach($next->children() as $child) {
$recursive = new self($child);
foreach ($recursive->next() as $item) {
yield $item;
}
}
$next = null;
}
}
}
22 changes: 19 additions & 3 deletions classes/AutoIDProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,25 @@ public function __construct($object, bool $overwrite = false)
$this->update = [];
$this->autoids = [];

$this->indexPageOrFile();
$this->indexStructures();
$this->update();
if (! $this->overwrite) {
// skip without reading the autoid from content file
$diruri = null;
if(is_a($object, Page::class)) {
$diruri = $this->object->diruri();
} elseif(is_a($object, File::class)) {
$diruri = $this->object->parent()->diruri() . '@' . $this->object->filename();
} elseif(is_a($object, Site::class)) {
$diruri = '$';
}

$this->indexed = AutoIDDatabase::singleton()->findByDiruri($diruri) !== null;
}

if (! $this->indexed) {
$this->indexPageOrFile();
$this->indexStructures();
$this->update();
}
}

public function isIndexed(): bool
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bnomei/kirby3-autoid",
"type": "kirby-plugin",
"version": "2.6.0",
"version": "2.6.1",
"description": "Automatic unique ID for Pages, Files and nested Structures including performant helpers to retrieve them. Bonus: Tiny-URL.",
"license": "MIT",
"authors": [
Expand Down
Loading

0 comments on commit 6d541f9

Please sign in to comment.