Skip to content

Commit

Permalink
Merge pull request #85 from flug/fix/minimum-results
Browse files Browse the repository at this point in the history
fix max results for for all data
  • Loading branch information
clementtalleu authored Sep 10, 2024
2 parents 651e5a9 + 370561b commit 1baad20
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/Client/PredisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@ public function count(string $prefixKey, array $criterias = []): int
$arguments[] = "@$property:$value";
}

$rawResult = call_user_func_array([$this->redis, 'executeRaw'], $arguments);
if ($criterias === []) {
$arguments[] = '*';
}

$rawResult = call_user_func_array([$this->redis, 'executeRaw'], [$arguments]);

if (!$rawResult) {
$this->handleError(__METHOD__, $this->getLastError());
Expand Down
4 changes: 4 additions & 0 deletions src/Client/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ public function count(string $prefixKey, array $criterias = []): int
$arguments[] = "@$property:$value";
}

if ($criterias === []) {
$arguments[] = '*';
}

$rawResult = call_user_func_array([$this->redis, 'rawCommand'], $arguments);

if (!$rawResult) {
Expand Down
45 changes: 37 additions & 8 deletions src/Om/Repository/AbstractObjectRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class AbstractObjectRepository implements RepositoryInterface
public ?string $className = null;
protected ?RedisClientInterface $redisClient = null;
protected ?ConverterInterface $converter = null;

private const DEFAULT_SEARCH_LIMIT = 10000;
public function __construct(public ?string $format = null)
{
}
Expand All @@ -37,6 +37,7 @@ abstract public function getPropertyValue($identifier, string $property): mixed;
*/
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = 0): array
{
$limit = $this->defineLimit($limit);
$this->convertDates($criteria);
$this->convertSpecial($criteria);
$data = $this->redisClient->search(
Expand All @@ -61,14 +62,15 @@ public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = nu
*/
public function findByLike(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = 0): array
{
$limit = $this->defineLimit($limit);
$this->convertDates($criteria);
$this->convertSpecial($criteria);
foreach ($criteria as $property => $value) {
$criteria[$property.'_text'] = "*$value*";
$criteria[$property . '_text'] = "*$value*";
unset($criteria[$property]);
}

$data = $this->redisClient->search(prefixKey: $this->prefix, search: $criteria, orderBy: $orderBy ?? [], format: $this->format, numberOfResults: $limit, offset: $offset, searchType: Property::INDEX_TEXT);
$data = $this->redisClient->search(prefixKey: $this->prefix, search: $criteria, orderBy: $orderBy ?? [], format: $this->format, numberOfResults: $limit, offset: $offset, searchType: Property::INDEX_TEXT);

$collection = [];
foreach ($data as $item) {
Expand All @@ -78,11 +80,22 @@ public function findByLike(array $criteria, ?array $orderBy = null, ?int $limit
return $collection;
}

private function defineLimit(?int $limit = null)
{
if ($limit === null) {
$limit = self::DEFAULT_SEARCH_LIMIT;
}

return $limit;
}

/**
* @inheritdoc
*/
public function findLike(string $search, ?int $limit = null): array
{
$limit = $this->defineLimit($limit);

$data = $this->redisClient->searchLike($this->prefix, $search, $this->format, $limit);

$collection = [];
Expand All @@ -96,9 +109,25 @@ public function findLike(string $search, ?int $limit = null): array
/**
* @inheritdoc
*/
public function findAll(): array
public function findAll(): iterable
{
return $this->findBy([]);
$limit = self::DEFAULT_SEARCH_LIMIT;
$offset = 0;

do {
$results = $this->findBy([], offset: $offset, limit: $limit);

if (empty($results)) {
break;
}

foreach ($results as $result) {
yield $result;
}

$offset += $limit;

} while (true);
}

/**
Expand All @@ -125,11 +154,11 @@ public function findOneByLike(array $criteria, ?array $orderBy = null): ?object
$this->convertDates($criteria);
$this->convertSpecial($criteria);
foreach ($criteria as $property => $value) {
$criteria[$property.'_text'] = "*$value*";
$criteria[$property . '_text'] = "*$value*";
unset($criteria[$property]);
}

$data = $this->redisClient->search(prefixKey: $this->prefix, search: $criteria, orderBy: $orderBy ?? [], format: $this->format, numberOfResults: 1, searchType: Property::INDEX_TEXT);
$data = $this->redisClient->search(prefixKey: $this->prefix, search: $criteria, orderBy: $orderBy ?? [], format: $this->format, numberOfResults: 1, searchType: Property::INDEX_TEXT);

if ($data === []) {
return null;
Expand All @@ -152,7 +181,7 @@ public function count(array $criteria = []): int
public function createQueryBuilder(): QueryBuilder
{
return new QueryBuilder(
redisClient: $this->redisClient,
redisClient: $this->redisClient,
converter: $this->converter,
className: $this->className,
redisKey: $this->prefix,
Expand Down
2 changes: 1 addition & 1 deletion src/Om/Repository/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function findLike(string $search, ?int $limit = null): array;
/**
* Find all objects from specific class.
*/
public function findAll(): array;
public function findAll(): iterable;

/**
* Find one object by a set of criteria.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ public function testFindAll()
$repository = $this->objectManager->getRepository(DummyJson::class);

$collection = $repository->findAll();
$this->assertCount(3, $collection);
foreach ($collection as $dummy) {
$results = iterator_to_array($collection);
$this->assertCount(3, $results);
foreach ($results as $dummy) {
$this->assertInstanceOf(DummyJson::class, $dummy);
}
}
Expand Down

0 comments on commit 1baad20

Please sign in to comment.