Skip to content

Add BITFIELD_RO #2340

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

Merged
merged 1 commit into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,29 @@ def bitfield(
"""
return BitFieldOperation(self, key, default_overflow=default_overflow)

def bitfield_ro(
self: Union["Redis", "AsyncRedis"],
key: KeyT,
encoding: str,
offset: BitfieldOffsetT,
items: Optional[list] = None,
) -> ResponseT:
"""
Return an array of the specified bitfield values
where the first value is found using ``encoding`` and ``offset``
parameters and remaining values are result of corresponding
encoding/offset pairs in optional list ``items``
Read-only variant of the BITFIELD command.

For more information see https://redis.io/commands/bitfield_ro
"""
params = [key, "GET", encoding, offset]

items = items or []
for encoding, offset in items:
params.extend(["GET", encoding, offset])
return self.execute_command("BITFIELD_RO", *params)

def bitop(self, operation: str, dest: KeyT, *keys: KeyT) -> ResponseT:
"""
Perform a bitwise operation using ``operation`` between ``keys`` and
Expand Down
13 changes: 13 additions & 0 deletions tests/test_asyncio/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2953,6 +2953,19 @@ async def test_bitfield_operations(self, r: redis.Redis):
)
assert resp == [0, None, 255]

@skip_if_server_version_lt("6.0.0")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realize the documentation is confusing here. But it appears the command is available since 6.0.0, but the help text indicates it's from Redis 6.2

@itamarhaber can you weigh in on which is correct, so that we get this tagged appropriately?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BITFIELD_RO was added in Redis v6.0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool thanks! Seems like a doc needs changing in redis-docs In that case, stamped.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing the error in the docs
image

async def test_bitfield_ro(self, r: redis.Redis):
bf = r.bitfield("a")
resp = await bf.set("u8", 8, 255).execute()
assert resp == [0]

resp = await r.bitfield_ro("a", "u8", 0)
assert resp == [0]

items = [("u4", 8), ("u4", 12), ("u4", 13)]
resp = await r.bitfield_ro("a", "u8", 0, items)
assert resp == [0, 15, 15, 14]

@skip_if_server_version_lt("4.0.0")
async def test_memory_stats(self, r: redis.Redis):
# put a key into the current db to make sure that "db.<current-db>"
Expand Down
13 changes: 13 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4438,6 +4438,19 @@ def test_bitfield_operations(self, r):
)
assert resp == [0, None, 255]

@skip_if_server_version_lt("6.0.0")
def test_bitfield_ro(self, r: redis.Redis):
bf = r.bitfield("a")
resp = bf.set("u8", 8, 255).execute()
assert resp == [0]

resp = r.bitfield_ro("a", "u8", 0)
assert resp == [0]

items = [("u4", 8), ("u4", 12), ("u4", 13)]
resp = r.bitfield_ro("a", "u8", 0, items)
assert resp == [0, 15, 15, 14]

@skip_if_server_version_lt("4.0.0")
def test_memory_help(self, r):
with pytest.raises(NotImplementedError):
Expand Down