Skip to content

Commit

Permalink
Added missing engine methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
olsgreen committed May 23, 2021
1 parent 9bd7783 commit 52e2eb7
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/DatabaseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;

Expand Down Expand Up @@ -155,6 +156,58 @@ public function map(Builder $builder, $results, $model)
})->filter();
}

/**
* Map the given results to instances of the given model via a lazy collection.
*
* @param \Laravel\Scout\Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Support\LazyCollection
*/
public function lazyMap(Builder $builder, $results, $model)
{
return LazyCollection::make(function() use ($results, $model) {
$chunks = collect($this->mapIds($results))->chunk(1000);

foreach ($chunks as $chunk) {
foreach ($chunk as $item) {
$query = $model->query();

if ($this->usesSoftDelete($model)) {
$query = $query->withTrashed();
}

if ($result = $query->find($item)) {
yield $result;
}
};
};
});
}

/**
* Create a search index.
*
* @param string $name
* @param array $options
* @return mixed
*/
public function createIndex($name, array $options = [])
{
throw new Exception('Database indexes are created automatically upon adding objects.');
}

/**
* Delete a search index.
*
* @param string $name
* @return mixed
*/
public function deleteIndex($name)
{
$this->query()->where('index', $name)->delete();
}

/**
* Get the total count from a raw result returned by the engine.
*
Expand Down
41 changes: 41 additions & 0 deletions tests/DatabaseEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,47 @@ public function test_map_correctly_maps_empty()
$this->assertCount(0, $results);
}

public function test_lazy_map_correctly_maps_results_to_models()
{
$manager = Mockery::mock('Illuminate\Database\DatabaseManager');
$queryBuilder = Mockery::mock('\Illuminate\Database\Query\Builder');
$manager->shouldReceive('table')->with('scout_index')->andReturn($queryBuilder);

$model = Mockery::mock('StdClass');
$model->shouldReceive('query')->andReturn($model);
$model->shouldReceive('find')->with(1)->andReturn($model);

$builder = Mockery::mock(Builder::class);

$record = new \StdClass();
$record->objectID = 1;

$engine = new DatabaseEngine($manager);
$results = $engine->lazyMap($builder, ['results' => collect([
$record,
]), 'total' => 1], $model);

$this->assertEquals(1, $results->count());
}

public function test_lazy_map_correctly_maps_empty()
{
$manager = Mockery::mock('Illuminate\Database\DatabaseManager');
$queryBuilder = Mockery::mock('\Illuminate\Database\Query\Builder');
$manager->shouldReceive('table')->with('scout_index')->andReturn($queryBuilder);

$model = Mockery::mock('StdClass');
$model->shouldReceive('query')->andReturn($model);
$model->shouldReceive('find')->with(1)->andReturn($model);

$builder = Mockery::mock(Builder::class);

$engine = new DatabaseEngine($manager);
$results = $engine->lazyMap($builder, ['results' => collect([]), 'total' => 0], $model);

$this->assertEquals(0, $results->count());
}

public function test_paginate_returns_correctly()
{
$queryBuilder = Mockery::mock('\Illuminate\Database\Query\Builder');
Expand Down

0 comments on commit 52e2eb7

Please sign in to comment.