Closed webmozart closed 9 years ago
Here are what I have in mind:
ArrayStore
is obvisouly easily sortable ;NullStore
has the method and it should do nothing ;JsonFileStore
is sortable and the data is written in the file after the sort ;CachedStore
is sortable and just call the delegated store sort()
method ;I'm not sure about the others (PhpRedisStore
, PredisStore
, RiakStore
), do you have an idea?
Sounds good! I don't know about the others, did you research whether they support anything like that?
I never worked with Riak but it seems not possible (http://stackoverflow.com/questions/7601997/how-to-sort-order-data-in-riak).
In Redis (PhpRedis and Preedis) it seems possible (http://stackoverflow.com/questions/5780365/redis-how-can-i-sort-my-hash-by-keys) but need a bit of changes in the store, like probably creating a single key containing all the store data.
I'll think about it a bit more.
Actually it seems possible to list all the keys in Riak: http://www.paperplanes.de/2011/12/13/list-all-of-the-riak-keys.html. We could store these keys in an array in the store and sort them on sort()
call. What do you think?
Probably that's better, yes. What about changing the interface to:
interface SortedStore extends KeyValueStore
{
}
This interface could be implemented directly by the stores that support sorting easily. For the others, we could implement a SortingAdapter
that takes another store and sorts the keys returned from getMultiple()
and keys()
. What do you think?
However, as a user of the SortedStore
, you lose the power to control the direction of the sorting, which is maybe not optimal. Hm.
I think what could be great is SortableStore
: just an interface for stores supporting sort. We could also introduce your idea of SortingAdapter
as a way to sort not sortable stores. We could have something like:
class ArrayStore implements SortableStore, SortingAdapter
{}
class RiakStore implements SortableStore
{
private $sortableAdapter;
public function sort() {
// We inject all the data in the adapter, sort it, and return
}
}
What do you think?
Sounds good. However, I think SortingAdapter
should be something like this:
class SortableAdapter implements SortableStore
{
private $innerStore;
private $flags = SORT_REGULAR;
public function sort($flags)
{
$this->flags = $flags;
}
public function keys()
{
$keys = $this->innerStore->keys();
sort($keys, $this->flags);
return $keys;
}
}
Hum. I'm not a fan of giving the possibility to use it externaly easily.
Perhaps externalizing the system of SortableAdapter of the stores would be better, don't you think?
I don't understand?
I mean implementing SortableStore
make me think it's a store usable from a developer point of view. I think it's more an internal system, and shouldn't implement an API interface. But it's not that much a problem :) .
The problem is that sorting stores that don't support sorting natively might be quite expensive. So sorting their keys in memory might be a better alternative. Let's talk about this on IRC.
For summary:
CachedStore
in CachingDecorator
;SortableStore
interface for sortable stores ;SortableDecorator
with a sort()
method that either call the internal store sort()
if it implements SortableStore
or use an array for an in-memory sort for others ;SortableDecorator::$flags
should be null by default (no sorting) and be reset to null on set()
so that it behaves the same as any other sorted store, which is only sorted on a call to sort()
.
Is it fine for you?
Almost. I would not check whether the inner store implements SortableStore
in SortableDecorator
, that's too complicated. Simply treat the inner store as non-sortable store.
CachedStore
in CachingDecorator
;SortableStore
interface for sortable stores ;SortableDecorator
with a sort()
method that use an array for an in-memory sort for others ;SortableDecorator::$flags
should be null by default (no sorting) and be reset to null on set()
so that it behaves the same as any other sorted store, which is only sorted on a call to sort()
.
I suppose we can close that now :) .
Yes, thanks :)
An interface
Sortable
should be added to the API with the following method:This method sorts the store by its keys.
The supported sort flags are the standard PHP sort flags constants (http://de.php.net/sort).
Research needs to be done about which stores support sorting.