Skip to content

Commit

Permalink
feat[list-commands]:implement BLMOVE
Browse files Browse the repository at this point in the history
  • Loading branch information
cunla committed Jun 18, 2023
1 parent 7bf6064 commit ed92273
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
15 changes: 9 additions & 6 deletions docs/about/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ description: Change log of all fakeredis releases
### 🚀 Features

- Implemented support for various stream groups commands:
- `XGROUP CREATE` #161, `XGROUP DESTROY` #164, `XGROUP SETID` #165, `XGROUP DELCONSUMER` #162,
`XGROUP CREATECONSUMER` #163, `XINFO GROUPS` #168, `XINFO CONSUMERS` #168, `XINFO STREAM` #169, `XREADGROUP` #171,
`XACK` #157, `XPENDING` #170
- Implemented sorted set commands:
- `ZRANDMEMBER` #192, `ZDIFF` #187, `ZINTER` #189, `ZUNION` #194, `ZDIFFSTORE` #188,
`ZINTERCARD` #190, `ZRANGESTORE` #193
- `XGROUP CREATE` #161, `XGROUP DESTROY` #164, `XGROUP SETID` #165, `XGROUP DELCONSUMER` #162,
`XGROUP CREATECONSUMER` #163, `XINFO GROUPS` #168, `XINFO CONSUMERS` #168, `XINFO STREAM` #169, `XREADGROUP` #171,
`XACK` #157, `XPENDING` #170
- Implemented sorted set commands:
- `ZRANDMEMBER` #192, `ZDIFF` #187, `ZINTER` #189, `ZUNION` #194, `ZDIFFSTORE` #188,
`ZINTERCARD` #190, `ZRANGESTORE` #193

- Implemented list commands:
- `BLMOVE` #182,

### 🧰 Maintenance

Expand Down
8 changes: 4 additions & 4 deletions docs/redis-commands/Redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,10 @@ Performs arbitrary read-only bitfield integer operations on strings.

## list commands

### [BLMOVE](https://redis.io/commands/blmove/)

Pops an element from a list, pushes it to another list and returns it. Blocks until an element is available otherwise. Deletes the list if the last element was moved.

### [BLPOP](https://redis.io/commands/blpop/)

Removes and returns the first element in a list. Blocks until an element is available otherwise. Deletes the list if the last element was popped.
Expand Down Expand Up @@ -732,10 +736,6 @@ Appends an element to a list only when the list exists.
### Unsupported list commands
> To implement support for a command, see [here](../../guides/implement-command/)
#### [BLMOVE](https://redis.io/commands/blmove/) <small>(not implemented)</small>

Pops an element from a list, pushes it to another list and returns it. Blocks until an element is available otherwise. Deletes the list if the last element was moved.

#### [BLMPOP](https://redis.io/commands/blmpop/) <small>(not implemented)</small>

Pops the first element from one of multiple lists. Blocks until an element is available otherwise. Deletes the list if the last element was popped.
Expand Down
16 changes: 12 additions & 4 deletions fakeredis/commands_mixins/list_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,24 @@ def linsert(self, key, where, pivot, value):
def llen(self, key):
return len(key.value)

@command((Key(list, None), Key(list), SimpleString, SimpleString))
def lmove(self, first_list, second_list, src, dst):
def _lmove(self, first_list, second_list, src, dst, first_pass):
if ((not casematch(src, b'left') and not casematch(src, b'right'))
or (not casematch(dst, b'left') and not casematch(dst, b'right'))):
raise SimpleError(msgs.SYNTAX_ERROR_MSG)

el = self.rpop(first_list) if src == b'RIGHT' else self.lpop(first_list)
self.lpush(second_list, el) if dst == b'LEFT' else self.rpush(second_list, el)
el = self.rpop(first_list) if casematch(src, b'RIGHT') else self.lpop(first_list)
self.lpush(second_list, el) if casematch(dst, b'LEFT') else self.rpush(second_list, el)
return el

@command((Key(list, None), Key(list), SimpleString, SimpleString))
def lmove(self, first_list, second_list, src, dst):
return self._lmove(first_list, second_list, src, dst, False)

@command((Key(list, None), Key(list), SimpleString, SimpleString, Timeout))
def blmove(self, first_list, second_list, src, dst, timeout):
return self._blocking(
timeout, functools.partial(self._lmove, first_list, second_list, src, dst))

@command(fixed=(Key(),), repeat=(bytes,))
def lpop(self, key, *args):
return _list_pop(lambda count: slice(None, count), key, *args)
Expand Down
6 changes: 6 additions & 0 deletions test/test_mixins/test_list_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,12 @@ def test_lmove(r: redis.Redis):
assert r.lrem('bar', -1, 'two') == 1


def test_blmove(r: redis.Redis):
r.rpush("a", "one", "two", "three", "four")
assert r.blmove("a", "b", 5)
assert r.blmove("a", "b", 1, "RIGHT", "LEFT")


@pytest.mark.disconnected
@testtools.fake_only
def test_lmove_disconnected_raises_connection_error(r: redis.Redis):
Expand Down

0 comments on commit ed92273

Please sign in to comment.