Skip to content

Commit

Permalink
change default results
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Lemay committed Sep 6, 2024
1 parent a566396 commit 83a8c16
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/Client/PredisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ public function count(string $prefixKey, array $criterias = []): int
foreach ($criterias as $property => $value) {
$arguments[] = "@$property:$value";
}

if ($criterias === []) {
$arguments[] = '*';
}
Expand Down
1 change: 1 addition & 0 deletions src/Client/RedisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public function count(string $prefixKey, array $criterias = []): int
foreach ($criterias as $property => $value) {
$arguments[] = "@$property:$value";
}

if ($criterias === []) {
$arguments[] = '*';
}
Expand Down
26 changes: 21 additions & 5 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 Down Expand Up @@ -83,10 +83,12 @@ public function findByLike(array $criteria, ?array $orderBy = null, ?int $limit
private function defineLimit(?int $limit = null)
{
if ($limit === null) {
$limit = $this->count([]);
$limit = self::DEFAULT_SEARCH_LIMIT;
}

return $limit;
}

/**
* @inheritdoc
*/
Expand All @@ -107,11 +109,25 @@ public function findLike(string $search, ?int $limit = null): array
/**
* @inheritdoc
*/
public function findAll(): array
public function findAll(): iterable
{
$count = $this->count([]);
$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;

return $this->findBy([], limit: $count);
} while (true);
}

/**
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

0 comments on commit 83a8c16

Please sign in to comment.