Skip to content

Commit 031b208

Browse files
authored
Add BITFIELD_RO (#2340)
1 parent e95b05a commit 031b208

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

redis/commands/core.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,29 @@ def bitfield(
15041504
"""
15051505
return BitFieldOperation(self, key, default_overflow=default_overflow)
15061506

1507+
def bitfield_ro(
1508+
self: Union["Redis", "AsyncRedis"],
1509+
key: KeyT,
1510+
encoding: str,
1511+
offset: BitfieldOffsetT,
1512+
items: Optional[list] = None,
1513+
) -> ResponseT:
1514+
"""
1515+
Return an array of the specified bitfield values
1516+
where the first value is found using ``encoding`` and ``offset``
1517+
parameters and remaining values are result of corresponding
1518+
encoding/offset pairs in optional list ``items``
1519+
Read-only variant of the BITFIELD command.
1520+
1521+
For more information see https://redis.io/commands/bitfield_ro
1522+
"""
1523+
params = [key, "GET", encoding, offset]
1524+
1525+
items = items or []
1526+
for encoding, offset in items:
1527+
params.extend(["GET", encoding, offset])
1528+
return self.execute_command("BITFIELD_RO", *params)
1529+
15071530
def bitop(self, operation: str, dest: KeyT, *keys: KeyT) -> ResponseT:
15081531
"""
15091532
Perform a bitwise operation using ``operation`` between ``keys`` and

tests/test_asyncio/test_commands.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,6 +2953,19 @@ async def test_bitfield_operations(self, r: redis.Redis):
29532953
)
29542954
assert resp == [0, None, 255]
29552955

2956+
@skip_if_server_version_lt("6.0.0")
2957+
async def test_bitfield_ro(self, r: redis.Redis):
2958+
bf = r.bitfield("a")
2959+
resp = await bf.set("u8", 8, 255).execute()
2960+
assert resp == [0]
2961+
2962+
resp = await r.bitfield_ro("a", "u8", 0)
2963+
assert resp == [0]
2964+
2965+
items = [("u4", 8), ("u4", 12), ("u4", 13)]
2966+
resp = await r.bitfield_ro("a", "u8", 0, items)
2967+
assert resp == [0, 15, 15, 14]
2968+
29562969
@skip_if_server_version_lt("4.0.0")
29572970
async def test_memory_stats(self, r: redis.Redis):
29582971
# put a key into the current db to make sure that "db.<current-db>"

tests/test_commands.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4438,6 +4438,19 @@ def test_bitfield_operations(self, r):
44384438
)
44394439
assert resp == [0, None, 255]
44404440

4441+
@skip_if_server_version_lt("6.0.0")
4442+
def test_bitfield_ro(self, r: redis.Redis):
4443+
bf = r.bitfield("a")
4444+
resp = bf.set("u8", 8, 255).execute()
4445+
assert resp == [0]
4446+
4447+
resp = r.bitfield_ro("a", "u8", 0)
4448+
assert resp == [0]
4449+
4450+
items = [("u4", 8), ("u4", 12), ("u4", 13)]
4451+
resp = r.bitfield_ro("a", "u8", 0, items)
4452+
assert resp == [0, 15, 15, 14]
4453+
44414454
@skip_if_server_version_lt("4.0.0")
44424455
def test_memory_help(self, r):
44434456
with pytest.raises(NotImplementedError):

0 commit comments

Comments
 (0)