Skip to content

Commit

Permalink
Move sorting to RedisList / RedisSet / RedisSortedSet
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Sep 29, 2019
1 parent 48eaff4 commit 494a32c
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 45 deletions.
38 changes: 1 addition & 37 deletions src/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,42 +305,6 @@ public function scan(?string $pattern = null, ?int $count = null): Iterator
});
}

/**
* @param string $key
* @param SortOptions $sort
*
* @return Promise<array>
*
* @link https://redis.io/commands/sort
*/
public function getSorted(
string $key,
SortOptions $sort
): Promise {
$payload = ['SORT', $key];

if ($sort->hasPattern()) {
$payload[] = 'BY';
$payload[] = $sort->getPattern();
}

if ($sort->hasLimit()) {
$payload[] = 'LIMIT';
$payload[] = $sort->getOffset();
$payload[] = $sort->getCount();
}

if ($sort->isDescending()) {
$payload[] = 'DESC';
}

if ($sort->isLexicographicSorting()) {
$payload[] = 'ALPHA';
}

return $this->queryExecutor->execute($payload);
}

/**
* @param string $key
*
Expand Down Expand Up @@ -671,7 +635,7 @@ public function set(string $key, string $value, SetOptions $options = null): Pro
$query = ['set', $key, $value];

if ($options !== null) {
$query = \array_merge($query, $options->toSetQuery());
$query = \array_merge($query, $options->toQuery());
}

return $this->queryExecutor->execute($query, toBool);
Expand Down
12 changes: 12 additions & 0 deletions src/RedisList.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,16 @@ public function trim(int $start = 0, int $stop = -1): Promise
{
return $this->queryExecutor->execute(['ltrim', $this->key, $start, $stop], toNull);
}

/**
* @param SortOptions $sort
*
* @return Promise<array>
*
* @link https://redis.io/commands/sort
*/
public function sort(?SortOptions $sort = null): Promise
{
return $this->queryExecutor->execute(\array_merge(['SORT', $this->key], ($sort ?? new SortOptions)->toQuery()));
}
}
12 changes: 12 additions & 0 deletions src/RedisSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,16 @@ public function scan(?string $pattern = null, ?int $count = null): Iterator
} while ($cursor !== '0');
});
}

/**
* @param SortOptions $sort
*
* @return Promise<array>
*
* @link https://redis.io/commands/sort
*/
public function sort(?SortOptions $sort = null): Promise
{
return $this->queryExecutor->execute(\array_merge(['SORT', $this->key], ($sort ?? new SortOptions)->toQuery()));
}
}
12 changes: 12 additions & 0 deletions src/RedisSortedSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,16 @@ public function storeUnion(array $keys, string $aggregate = 'sum'): Promise

return $this->queryExecutor->execute($payload);
}

/**
* @param SortOptions $sort
*
* @return Promise<array>
*
* @link https://redis.io/commands/sort
*/
public function sort(?SortOptions $sort = null): Promise
{
return $this->queryExecutor->execute(\array_merge(['SORT', $this->key], ($sort ?? new SortOptions)->toQuery()));
}
}
2 changes: 1 addition & 1 deletion src/SetOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function withoutCreation(): self
return $clone;
}

public function toSetQuery(): array
public function toQuery(): array
{
$query = [];

Expand Down
26 changes: 26 additions & 0 deletions src/SortOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,30 @@ public function withNumericSorting(): self

return $clone;
}

public function toQuery(): array
{
$payload = [];

if ($this->hasPattern()) {
$payload[] = 'BY';
$payload[] = $this->getPattern();
}

if ($this->hasLimit()) {
$payload[] = 'LIMIT';
$payload[] = $this->getOffset();
$payload[] = $this->getCount();
}

if ($this->isDescending()) {
$payload[] = 'DESC';
}

if ($this->isLexicographicSorting()) {
$payload[] = 'ALPHA';
}

return $payload;
}
}
2 changes: 2 additions & 0 deletions test/RedisListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function test(): \Generator
$this->assertSame(1, yield $list->remove('a'));
$this->assertSame(['y', 'x', 'b'], yield $list->getRange());

$this->assertSame(['b', 'x', 'y'], yield $list->sort((new SortOptions)->withLexicographicSorting()));

$this->assertSame('y', yield $list->popHeadBlocking());
$this->assertSame('b', yield $list->popTailBlocking());
}
Expand Down
2 changes: 1 addition & 1 deletion test/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class RedisTest extends IntegrationTest
{
public function test()
public function test(): \Generator
{
yield $this->redis->flushAll();

Expand Down
12 changes: 6 additions & 6 deletions test/SetOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public function test(): void
{
$options = new SetOptions;

$this->assertSame([], $options->toSetQuery());
$this->assertSame(['EX', 3], $options->withTtl(3)->toSetQuery());
$this->assertSame(['PX', 3], $options->withTtlInMillis(3)->toSetQuery());
$this->assertSame(['PX', 3, 'XX'], $options->withTtlInMillis(3)->withoutCreation()->toSetQuery());
$this->assertSame(['PX', 3, 'NX'], $options->withTtlInMillis(3)->withoutOverwrite()->toSetQuery());
$this->assertSame([], $options->toQuery());
$this->assertSame(['EX', 3], $options->withTtl(3)->toQuery());
$this->assertSame(['PX', 3], $options->withTtlInMillis(3)->toQuery());
$this->assertSame(['PX', 3, 'XX'], $options->withTtlInMillis(3)->withoutCreation()->toQuery());
$this->assertSame(['PX', 3, 'NX'], $options->withTtlInMillis(3)->withoutOverwrite()->toQuery());
$this->assertSame(
['PX', 3, 'NX'],
$options->withTtlInMillis(3)->withoutCreation()->withoutOverwrite()->toSetQuery()
$options->withTtlInMillis(3)->withoutCreation()->withoutOverwrite()->toQuery()
);
}
}

0 comments on commit 494a32c

Please sign in to comment.