-
-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement options for ZRANGE, ZRANGEBYSCORE, ZREVRANGE, ZREVRANGEBYSCORE #78
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Amp\Redis; | ||
|
||
final class RangeByScoreOptions | ||
{ | ||
/** @var bool */ | ||
private $withScores = false; | ||
/** @var int|null */ | ||
private $offset; | ||
/** @var int|null */ | ||
private $count; | ||
|
||
public function withScores(): self | ||
{ | ||
$clone = clone $this; | ||
$clone->withScores = true; | ||
|
||
return $clone; | ||
} | ||
|
||
public function isWithScores(): bool | ||
{ | ||
return $this->withScores; | ||
} | ||
|
||
public function withOffset(int $offset, int $count): self | ||
{ | ||
$clone = clone $this; | ||
$clone->offset = $offset; | ||
$clone->count = $count; | ||
|
||
return $clone; | ||
} | ||
|
||
public function toQuery(): array | ||
{ | ||
$query = []; | ||
|
||
if ($this->withScores() !== false) { | ||
$query[] = "WITHSCORES"; | ||
} | ||
|
||
if ($this->offset !== null) { | ||
$query[] = "LIMIT"; | ||
$query[] = $this->offset; | ||
$query[] = $this->count; | ||
} | ||
|
||
return $query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Amp\Redis; | ||
|
||
final class RangeOptions | ||
{ | ||
/** @var bool */ | ||
private $withScores = false; | ||
|
||
public function isWithScores(): bool | ||
{ | ||
return $this->withScores; | ||
} | ||
|
||
public function withScores(): self | ||
{ | ||
$clone = clone $this; | ||
$clone->withScores = true; | ||
|
||
return $clone; | ||
} | ||
|
||
public function toQuery(): array | ||
{ | ||
$query = []; | ||
|
||
if ($this->withScores() !== false) { | ||
$query[] = "WITHSCORES"; | ||
} | ||
|
||
return $query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -42,9 +42,14 @@ public function add(array $data): Promise | |||||
* | ||||||
* @return Promise | ||||||
*/ | ||||||
public function getRange(int $start, int $end): Promise | ||||||
public function getRange(int $start, int $end, ?RangeByScoreOptions $options = null): Promise | ||||||
{ | ||||||
return $this->queryExecutor->execute(['zrange', $this->key, $start, $end]); | ||||||
$query = ['zrange', $this->key, $start, $end]; | ||||||
if ($options !== null) { | ||||||
$query = \array_merge($query, $options->toQuery()); | ||||||
} | ||||||
|
||||||
return $this->queryExecutor->execute($query, $options !== null && $options->isWithScores() ? toFloatMap : null); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -53,9 +58,14 @@ public function getRange(int $start, int $end): Promise | |||||
* | ||||||
* @return Promise | ||||||
*/ | ||||||
public function getReverseRange(int $start, int $end): Promise | ||||||
public function getReverseRange(int $start, int $end, ?RangeByScoreOptions $options = null): Promise | ||||||
{ | ||||||
return $this->queryExecutor->execute(['zrevrange', $this->key, $start, $end]); | ||||||
$query = ['zrevrange', $this->key, $start, $end]; | ||||||
if ($options !== null) { | ||||||
$query = \array_merge($query, $options->toQuery()); | ||||||
} | ||||||
|
||||||
return $this->queryExecutor->execute($query, $options !== null && $options->isWithScores() ? toFloatMap : null); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -64,9 +74,14 @@ public function getReverseRange(int $start, int $end): Promise | |||||
* | ||||||
* @return Promise | ||||||
*/ | ||||||
public function getRangeByScore(float $min, float $max): Promise | ||||||
public function getRangeByScore(float $min, float $max, RangeByScoreOptions $options = null): Promise | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
return $this->queryExecutor->execute(['zrangebyscore', $this->key, $min, $max]); | ||||||
$query = ['zrangebyscore', $this->key, $min, $max]; | ||||||
if ($options !== null) { | ||||||
$query = \array_merge($query, $options->toQuery()); | ||||||
} | ||||||
|
||||||
return $this->queryExecutor->execute($query, $options !== null && $options->isWithScores() ? toFloatMap : null); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -75,9 +90,14 @@ public function getRangeByScore(float $min, float $max): Promise | |||||
* | ||||||
* @return Promise | ||||||
*/ | ||||||
public function getReverseRangeByScore(float $min, float $max): Promise | ||||||
public function getReverseRangeByScore(float $min, float $max, RangeByScoreOptions $options = null): Promise | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
return $this->queryExecutor->execute(['ZREVRANGEBYSCORE', $this->key, $min, $max]); | ||||||
$query = ['zrevrangebyscore', $this->key, $min, $max]; | ||||||
if ($options !== null) { | ||||||
$query = \array_merge($query, $options->toQuery()); | ||||||
} | ||||||
|
||||||
return $this->queryExecutor->execute($query, $options !== null && $options->isWithScores() ? toFloatMap : null); | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -89,12 +109,12 @@ public function getSize(): Promise | |||||
} | ||||||
|
||||||
/** | ||||||
* @param int $min | ||||||
* @param int $max | ||||||
* @param float $min | ||||||
* @param float $max | ||||||
* | ||||||
* @return Promise<int> | ||||||
*/ | ||||||
public function count(int $min, int $max): Promise | ||||||
public function count(float $min, float $max): Promise | ||||||
{ | ||||||
return $this->queryExecutor->execute(['zcount', $this->key, $min, $max]); | ||||||
} | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be called
withLimit
instead?