Skip to content

Commit

Permalink
Merge pull request #705 from cosmocode/lazy-search
Browse files Browse the repository at this point in the history
Lazy search
  • Loading branch information
splitbrain committed Feb 14, 2024
2 parents fa365d7 + 1c9d444 commit cb58845
Show file tree
Hide file tree
Showing 18 changed files with 259 additions and 196 deletions.
4 changes: 2 additions & 2 deletions _test/AccessTableDataReplacementTest.php
Expand Up @@ -93,7 +93,7 @@ public function test_simple()

$search = new meta\SearchConfig($actual_config);
list(, $opts) = $search->getSQL();
$result = $search->execute();
$result = $search->getRows();

$this->assertEquals(['page1', 'page2'], $opts, '$STRUCT.table.col$ should not require table to be selected');
$this->assertEquals('data of page1', $result[0][1]->getValue());
Expand All @@ -114,7 +114,7 @@ public function test_emptyfield()
$actual_config = $configParser->getConfig();

$search = new meta\SearchConfig($actual_config);
$result = $search->execute();
$result = $search->getRows();

$this->assertEquals(0, count($result), 'if no pages a given, then none should be shown');
}
Expand Down
8 changes: 2 additions & 6 deletions _test/AggregationResultsTest.php
Expand Up @@ -6,8 +6,6 @@
use dokuwiki\plugin\struct\meta\ConfigParser;
use dokuwiki\plugin\struct\meta\PageMeta;
use dokuwiki\plugin\struct\test\mock\AccessTable as MockAccessTableAlias;
use dokuwiki\plugin\struct\test\mock\AggregationEditorTable as MockAggregationEditorTableAlias;
use dokuwiki\plugin\struct\test\mock\AggregationTable as MockAggregationTableAlias;
use dokuwiki\plugin\struct\test\mock\SearchConfig as MockSearchConfigAlias;

/**
Expand Down Expand Up @@ -181,8 +179,7 @@ protected function fetchAllResults($schema, $id = '', $filters = [])
if ($filters) $config['filter'][] = $filters;
$search = new MockSearchConfigAlias($config);

$table = new MockAggregationTableAlias($id, 'xhtml', new \Doku_Renderer_xhtml(), $search);
return $table->getResult();
return $search->getRows();
}

/**
Expand Down Expand Up @@ -211,8 +208,7 @@ protected function fetchNonPageResults($schema, $id = '', $filters = [])
if ($filters) $config['filter'][] = $filters;
$search = new MockSearchConfigAlias($config);

$table = new MockAggregationEditorTableAlias($id, 'xhtml', new \Doku_Renderer_xhtml(), $search);
return $table->getResult();
return $search->getRows();
}

protected function prepareLookup()
Expand Down
61 changes: 34 additions & 27 deletions _test/SearchTest.php
Expand Up @@ -16,6 +16,9 @@ class SearchTest extends StructTest

public function setUp(): void
{
// workaround for recent GitHub disk I/O errors
parent::setUpBeforeClass();

parent::setUp();

$this->loadSchemaJSON('schema1');
Expand Down Expand Up @@ -103,7 +106,7 @@ public function test_simple()
$search->addColumn('second');

/** @var meta\Value[][] $result */
$result = $search->execute();
$result = $search->getRows();

$this->assertCount(2, $result, 'result rows');
$this->assertCount(3, $result[0], 'result columns');
Expand All @@ -122,7 +125,7 @@ public function test_simple_title()
$search->addColumn('second');

/** @var meta\Value[][] $result */
$result = $search->execute();
$result = $search->getRows();

$this->assertCount(2, $result, 'result rows');
$this->assertCount(3, $result[0], 'result columns');
Expand All @@ -142,7 +145,7 @@ public function test_search_published()
$search->addColumn('second');

/** @var meta\Value[][] $result */
$result = $search->execute();
$result = $search->getRows();

$this->assertCount(0, $result, 'result rows');
}
Expand All @@ -158,7 +161,7 @@ public function test_search_lasteditor()
$search->addColumn('second');

/** @var meta\Value[][] $result */
$result = $search->execute();
$result = $search->getRows();

$this->assertCount(2, $result, 'result rows');
$this->assertCount(4, $result[0], 'result columns');
Expand All @@ -185,7 +188,7 @@ public function test_search_lastupdate()
$search->addColumn('second');

/** @var meta\Value[][] $result */
$result = $search->execute();
$result = $search->getRows();

$expected_time = dformat(filemtime(wikiFN('page01')), '%Y-%m-%d %H:%M:%S');

Expand Down Expand Up @@ -214,7 +217,7 @@ public function test_search_lastsummary()
$search->addColumn('second');

/** @var meta\Value[][] $result */
$result = $search->execute();
$result = $search->getRows();

$this->assertCount(2, $result, 'result rows');
$this->assertCount(4, $result[0], 'result columns');
Expand Down Expand Up @@ -270,7 +273,7 @@ public function test_search()
$search->addFilter('second', '%sec%', '~', 'AND');
$search->addFilter('first', '%rst%', '~', 'AND');

$result = $search->execute();
$result = $search->getRows();
$count = $search->getCount();

$this->assertEquals(1, $count, 'result count');
Expand All @@ -280,40 +283,25 @@ public function test_search()
// sort by multi-column
$search->addSort('second');
$this->assertCount(2, $search->sortby);
$result = $search->execute();
$result = $search->getRows();
$count = $search->getCount();
$this->assertEquals(1, $count, 'result count');
$this->assertCount(1, $result, 'result rows');
$this->assertCount(6, $result[0], 'result columns');

/*
{#debugging
list($sql, $opts) = $search->getSQL();
print "\n";
print_r($sql);
print "\n";
print_r($opts);
print "\n";
#print_r($result);
}
*/
}

public function test_ranges()
{
$search = new mock\Search();
$search->addSchema('schema2');

$search->addColumn('%pageid%');
$search->addColumn('afirst');
$search->addColumn('asecond');

$search->addFilter('%pageid%', '%ag%', '~', 'AND');

$search->addSort('%pageid%', false);

/** @var meta\Value[][] $result */
$result = $search->execute();
$result = $search->getRows();
$count = $search->getCount();

// check result dimensions
Expand All @@ -326,9 +314,19 @@ public function test_ranges()
$this->assertEquals('page19', $result[1][0]->getValue());
$this->assertEquals('page18', $result[2][0]->getValue());

// now add limit
// now with limit
// new search object because result is fetched only once
$search = new mock\Search();
$search->addSchema('schema2');
$search->addColumn('%pageid%');
$search->addColumn('afirst');
$search->addColumn('asecond');
$search->addFilter('%pageid%', '%ag%', '~', 'AND');
$search->addSort('%pageid%', false);
$search->setLimit(5);
$result = $search->execute();

/** @var meta\Value[][] $result */
$result = $search->getRows();
$count = $search->getCount();

// check result dimensions
Expand All @@ -340,8 +338,17 @@ public function test_ranges()
$this->assertEquals('page16', $result[4][0]->getValue());

// now add offset
// again a new object
$search = new mock\Search();
$search->addSchema('schema2');
$search->addColumn('%pageid%');
$search->addColumn('afirst');
$search->addColumn('asecond');
$search->addFilter('%pageid%', '%ag%', '~', 'AND');
$search->addSort('%pageid%', false);
$search->setLimit(5);
$search->setOffset(5);
$result = $search->execute();
$result = $search->getRows();
$count = $search->getCount();

// check result dimensions
Expand Down
13 changes: 0 additions & 13 deletions _test/mock/AggregationTable.php

This file was deleted.

4 changes: 2 additions & 2 deletions _test/types/DecimalTest.php
Expand Up @@ -191,7 +191,7 @@ public function test_sort()
$search->addColumn('field');
$search->addSort('field', true);
/** @var Value[][] $result */
$result = $search->execute();
$result = $search->getRows();

$this->assertEquals(4, count($result));
$this->assertEquals('page4', $result[0][0]->getValue());
Expand All @@ -216,7 +216,7 @@ public function test_filter()
$search->addFilter('field', '800', '>', 'AND');
$search->addSort('field', true);
/** @var Value[][] $result */
$result = $search->execute();
$result = $search->getRows();

$this->assertEquals(3, count($result));
$this->assertEquals('page3', $result[0][0]->getValue());
Expand Down
12 changes: 6 additions & 6 deletions _test/types/PageTest.php
Expand Up @@ -58,7 +58,7 @@ public function test_sort()
$search->addColumn('singletitle');
$search->addSort('singletitle', true);
/** @var Value[][] $result */
$result = $search->execute();
$result = $search->getRows();

$this->assertEquals(3, count($result));
$this->assertEquals('test3', $result[0][0]->getValue());
Expand Down Expand Up @@ -98,7 +98,7 @@ public function test_search()
$search->addColumn('multititle');

/** @var Value[][] $result */
$result = $search->execute();
$result = $search->getRows();

// no titles:
$this->assertEquals('wiki:dokuwiki', $result[0][0]->getValue());
Expand Down Expand Up @@ -127,28 +127,28 @@ public function test_search()
// search single with title
$single = clone $search;
$single->addFilter('singletitle', 'Overview', '*~', 'AND');
$result = $single->execute();
$result = $single->getRows();
$this->assertTrue(is_array($result));
$this->assertEquals(1, count($result));

// search multi with title
$multi = clone $search;
$multi->addFilter('multititle', 'Foobar', '*~', 'AND');
$result = $multi->execute();
$result = $multi->getRows();
$this->assertTrue(is_array($result));
$this->assertEquals(1, count($result));

// search single with page
$single = clone $search;
$single->addFilter('singletitle', 'wiki:dokuwiki', '*~', 'AND');
$result = $single->execute();
$result = $single->getRows();
$this->assertTrue(is_array($result));
$this->assertEquals(1, count($result));

// search multi with page
$multi = clone $search;
$multi->addFilter('multititle', 'welcome', '*~', 'AND');
$result = $multi->execute();
$result = $multi->getRows();
$this->assertTrue(is_array($result));
$this->assertEquals(1, count($result));
}
Expand Down
2 changes: 1 addition & 1 deletion action/bureaucracy.php
Expand Up @@ -103,7 +103,7 @@ public function handleLookupFields(Event $event, $param)
$search = new Search();
$search->addSchema($config['schema']);
$search->addColumn($config['field']);
$result = $search->execute();
$result = $search->getRows();
$pids = $search->getPids();
$rids = $search->getRids();

Expand Down
8 changes: 0 additions & 8 deletions meta/Aggregation.php
Expand Up @@ -24,12 +24,6 @@ abstract class Aggregation
/** @var Column[] the list of columns to be displayed */
protected $columns;

/** @var Value[][] the search result */
protected $result;

/** @var int number of all results */
protected $resultCount;

/** @var string usually a div, but AggregationValue needs to be wrapped in a span */
protected $tagName = 'div';

Expand Down Expand Up @@ -62,8 +56,6 @@ public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $
$this->searchConfig = $searchConfig;
$this->data = $searchConfig->getConf();
$this->columns = $searchConfig->getColumns();
$this->result = $this->searchConfig->execute();
$this->resultCount = $this->searchConfig->getCount();
$this->helper = plugin_load('helper', 'struct_config');
}

Expand Down
33 changes: 11 additions & 22 deletions meta/AggregationCloud.php
Expand Up @@ -10,30 +10,19 @@ class AggregationCloud extends Aggregation
/** @var int */
protected $min;

/**
* Initialize the Aggregation renderer and executes the search
*
* You need to call @param string $id
* @param string $mode
* @param \Doku_Renderer $renderer
* @param SearchConfig $searchConfig
* @see render() on the resulting object.
*
*/
public function __construct($id, $mode, \Doku_Renderer $renderer, SearchCloud $searchConfig)
{
parent::__construct($id, $mode, $renderer, $searchConfig);

$this->max = $this->result[0]['count'];
$this->min = end($this->result)['count'];
}

/** @inheritdoc */
public function render($showNotFound = false)
{
$this->sortResults();

if ($this->mode !== 'xhtml') return;

$rows = $this->searchConfig->getRows();
$this->max = $rows[0]['count'];
$this->min = end($rows)['count'];

$this->sortResults($rows);
$this->startList();
foreach ($this->result as $result) {
foreach ($rows as $result) {
$this->renderTag($result);
}
$this->finishList();
Expand Down Expand Up @@ -110,9 +99,9 @@ protected function getWeight($current, $min, $max)
/**
* Sort the list of results
*/
protected function sortResults()
protected function sortResults(&$rows)
{
usort($this->result, function ($a, $b) {
usort($rows, function ($a, $b) {
$asort = $a['tag']->getColumn()->getType()->getSortString($a['tag']);
$bsort = $b['tag']->getColumn()->getType()->getSortString($b['tag']);
if ($asort < $bsort) {
Expand Down
2 changes: 1 addition & 1 deletion meta/AggregationEditorTable.php
Expand Up @@ -70,7 +70,7 @@ public function getFirstRow()
$this->renderer->table_open();
$this->renderer->doc = '';

$this->renderResultRow(0, $this->result[0]);
$this->renderResultRow(0, $this->searchConfig->getRows()[0]);
return $this->renderer->doc;
}
}
4 changes: 2 additions & 2 deletions meta/AggregationList.php
Expand Up @@ -22,8 +22,8 @@ public function __construct($id, $mode, \Doku_Renderer $renderer, SearchConfig $
/** @inheritdoc */
public function render($showNotFound = false)
{
if ($this->result) {
$nestedResult = new NestedResult($this->result);
if ($this->searchConfig->getResult()) {
$nestedResult = new NestedResult($this->searchConfig->getRows());
$root = $nestedResult->getRoot($this->data['nesting'], $this->data['index']);
$this->renderNode($root);
} elseif ($showNotFound) {
Expand Down

0 comments on commit cb58845

Please sign in to comment.