Skip to content
Open
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
30 changes: 14 additions & 16 deletions src/etcetra/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,7 @@ def get_prefix(
encoding='utf-8',
):
encoded_key = key.encode(encoding)
Copy link

Choose a reason for hiding this comment

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

we don't need this encode here we already do it inside new function. Same for other cases.

Copy link

Choose a reason for hiding this comment

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

or new function shouldn't worry about conversion and should consume only bytes

if key[-1] == '/' and len(key) >= 2:
range_end = encoded_key[:-2] + bytes([encoded_key[-2] + 1]) + b'/'
else:
range_end = encoded_key[:-1] + bytes([encoded_key[-1] + 1])
range_end = increment_last_byte(encoded_key)
return rpc_pb2.RangeRequest(
key=encoded_key,
range_end=range_end,
Expand Down Expand Up @@ -408,10 +405,7 @@ def delete_prefix(
):
# TODO: Implement prev_kv response
encoded_key = key.encode(encoding)
if key[-1] == '/' and len(key) >= 2:
range_end = encoded_key[:-2] + bytes([encoded_key[-2] + 1]) + b'/'
else:
range_end = encoded_key[:-1] + bytes([encoded_key[-1] + 1])
range_end = increment_last_byte(encoded_key)
return rpc_pb2.DeleteRangeRequest(
key=encoded_key,
range_end=range_end,
Expand Down Expand Up @@ -461,10 +455,7 @@ def keys_prefix(
encoding='utf-8',
):
encoded_key = key.encode(encoding)
if key[-1] == '/' and len(key) >= 2:
range_end = encoded_key[:-2] + bytes([encoded_key[-2] + 1]) + b'/'
else:
range_end = encoded_key[:-1] + bytes([encoded_key[-1] + 1])
range_end = increment_last_byte(encoded_key)
return rpc_pb2.RangeRequest(
key=encoded_key,
range_end=range_end,
Expand Down Expand Up @@ -1309,10 +1300,7 @@ def watch_prefix(
encoding = self.encoding

encoded_key = key.encode(encoding)
if key[-1] == '/' and len(key) >= 2:
range_end = encoded_key[:-2] + bytes([encoded_key[-2] + 1]) + b'/'
else:
range_end = encoded_key[:-1] + bytes([encoded_key[-1] + 1])
range_end = increment_last_byte(encoded_key)
return self._watch_impl(
key.encode(encoding), encoding,
ready_event=ready_event, filters=filters, prev_kv=prev_kv,
Expand Down Expand Up @@ -1671,3 +1659,13 @@ async def __aexit__(self, exc_type, exc, tb) -> Optional[bool]:
self._lock_id = None
self._lease_id = None
return False


def increment_last_byte(encoded_key):
s = bytearray(encoded_key)
for i in range(len(s) - 1, -1, -1):
if s[i] < 0xff:
s[i] += 1
return bytes(s[:i + 1])
else:
return b'\x00'