1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
import functools
from typing import Callable, List, Optional, Union, Any
from fakeredis import _msgs as msgs
from fakeredis._command_args_parsing import extract_args
from fakeredis._commands import Key, command, Int, CommandItem, Timeout, fix_range
from fakeredis._helpers import OK, SimpleError, SimpleString, casematch, Database
def _list_pop_count(get_slice, key, count):
if not key:
return None
elif type(key.value) is not list:
raise SimpleError(msgs.WRONGTYPE_MSG)
slc = get_slice(count)
ret = key.value[slc]
del key.value[slc]
key.updated()
return ret
def _list_pop(get_slice, key, *args):
"""Implements lpop and rpop.
`get_slice` must take a count and return a slice expression for the range to pop.
"""
# This implementation is somewhat contorted to match the odd
# behaviours described in https://github.com/redis/redis/issues/9680.
count = 1
if len(args) > 1:
raise SimpleError(msgs.SYNTAX_ERROR_MSG)
elif len(args) == 1:
count = Int.decode(args[0], msgs.INDEX_NEGATIVE_ERROR_MSG)
if count < 0:
raise SimpleError(msgs.INDEX_NEGATIVE_ERROR_MSG)
ret = _list_pop_count(get_slice, key, count)
if ret and not args:
ret = ret[0]
return ret
class ListCommandsMixin:
_blocking: Callable[[Optional[Union[float, int]], Callable[[bool], Any]], Any]
def __init__(self, *args, **kwargs):
super(ListCommandsMixin, self).__init__(*args, **kwargs)
self._db: Database
def _bpop_pass(self, keys, op, first_pass):
for key in keys:
item = CommandItem(key, self._db, item=self._db.get(key), default=[])
if not isinstance(item.value, list):
if first_pass:
raise SimpleError(msgs.WRONGTYPE_MSG)
else:
continue
if item.value:
ret = op(item.value)
item.updated()
item.writeback()
return [key, ret]
return None
def _bpop(self, args, op):
keys = args[:-1]
timeout = Timeout.decode(args[-1])
return self._blocking(timeout, functools.partial(self._bpop_pass, keys, op))
@command((bytes, bytes), (bytes,), flags=msgs.FLAG_NO_SCRIPT)
def blpop(self, *args):
return self._bpop(args, lambda lst: lst.pop(0))
@command((bytes, bytes), (bytes,), flags=msgs.FLAG_NO_SCRIPT)
def brpop(self, *args):
return self._bpop(args, lambda lst: lst.pop())
def _brpoplpush_pass(self, source, destination, first_pass):
src = CommandItem(source, self._db, item=self._db.get(source), default=[])
if not isinstance(src.value, list):
if first_pass:
raise SimpleError(msgs.WRONGTYPE_MSG)
else:
return None
if not src.value:
return None # Empty list
dst = CommandItem(destination, self._db, item=self._db.get(destination), default=[])
if not isinstance(dst.value, list):
raise SimpleError(msgs.WRONGTYPE_MSG)
el = src.value.pop()
dst.value.insert(0, el)
src.updated()
src.writeback()
if destination != source:
# Ensure writeback only happens once
dst.updated()
dst.writeback()
return el
@command(name="BRPOPLPUSH", fixed=(bytes, bytes, Timeout), flags=msgs.FLAG_NO_SCRIPT)
def brpoplpush(self, source, destination, timeout):
return self._blocking(timeout, functools.partial(self._brpoplpush_pass, source, destination))
@command((Key(list, None), Int))
def lindex(self, key, index):
try:
return key.value[index]
except IndexError:
return None
@command((Key(list), bytes, bytes, bytes))
def linsert(self, key, where, pivot, value):
if not casematch(where, b"before") and not casematch(where, b"after"):
raise SimpleError(msgs.SYNTAX_ERROR_MSG)
if not key:
return 0
else:
try:
index = key.value.index(pivot)
except ValueError:
return -1
if casematch(where, b"after"):
index += 1
key.value.insert(index, value)
key.updated()
return len(key.value)
@command((Key(list),))
def llen(self, key):
return len(key.value)
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 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)
def _lmpop(self, keys, count, direction_left, first_pass):
if direction_left:
op = lambda count: slice(None, count) # noqa:E731
else:
op = lambda count: slice(None, -count - 1, -1) # noqa:E731
for key in keys:
item = CommandItem(key, self._db, item=self._db.get(key), default=[])
res = _list_pop_count(op, item, count)
if res:
return [key, res]
return None
@command(fixed=(Int,), repeat=(bytes,))
def lmpop(self, numkeys, *args):
if numkeys <= 0:
raise SimpleError(msgs.NUMKEYS_GREATER_THAN_ZERO_MSG)
if casematch(args[-2], b"count"):
count = Int.decode(args[-1])
args = args[:-2]
else:
count = 1
if len(args) != numkeys + 1 or (not casematch(args[-1], b"left") and not casematch(args[-1], b"right")):
raise SimpleError(msgs.SYNTAX_ERROR_MSG)
return self._lmpop(args[:-1], count, casematch(args[-1], b"left"), False)
@command(fixed=(Timeout, Int), repeat=(bytes,))
def blmpop(self, timeout, numkeys, *args):
if numkeys <= 0:
raise SimpleError(msgs.NUMKEYS_GREATER_THAN_ZERO_MSG)
if casematch(args[-2], b"count"):
count = Int.decode(args[-1])
args = args[:-2]
else:
count = 1
if len(args) != numkeys + 1 or (not casematch(args[-1], b"left") and not casematch(args[-1], b"right")):
raise SimpleError(msgs.SYNTAX_ERROR_MSG)
return self._blocking(
timeout,
functools.partial(self._lmpop, args[:-1], count, casematch(args[-1], b"left")),
)
@command((Key(list), bytes), (bytes,))
def lpush(self, key, *values):
for value in values:
key.value.insert(0, value)
key.updated()
return len(key.value)
@command((Key(list), bytes), (bytes,))
def lpushx(self, key, *values):
if not key:
return 0
return self.lpush(key, *values)
@command((Key(list), Int, Int))
def lrange(self, key, start, stop):
start, stop = fix_range(start, stop, len(key.value))
return key.value[start:stop]
@command((Key(list), Int, bytes))
def lrem(self, key, count, value):
a_list = key.value
found = []
for i, el in enumerate(a_list):
if el == value:
found.append(i)
if count > 0:
indices_to_remove = found[:count]
elif count < 0:
indices_to_remove = found[count:]
else:
indices_to_remove = found
# Iterating in reverse order to ensure the indices
# remain valid during deletion.
for index in reversed(indices_to_remove):
del a_list[index]
if indices_to_remove:
key.updated()
return len(indices_to_remove)
@command((Key(list), bytes, bytes))
def lset(self, key, index, value):
if not key:
raise SimpleError(msgs.NO_KEY_MSG)
index = Int.decode(index)
try:
key.value[index] = value
key.updated()
except IndexError:
raise SimpleError(msgs.INDEX_ERROR_MSG)
return OK
@command((Key(list), Int, Int))
def ltrim(self, key, start, stop):
if key:
if stop == -1:
stop = None
else:
stop += 1
new_value = key.value[start:stop]
# TODO: check if this should actually be conditional
if len(new_value) != len(key.value):
key.update(new_value)
return OK
@command(fixed=(Key(),), repeat=(bytes,))
def rpop(self, key, *args):
return _list_pop(lambda count: slice(None, -count - 1, -1), key, *args)
@command((Key(list, None), Key(list)))
def rpoplpush(self, src, dst):
el = self.rpop(src)
self.lpush(dst, el)
return el
@command((Key(list), bytes), (bytes,))
def rpush(self, key, *values):
for value in values:
key.value.append(value)
key.updated()
return len(key.value)
@command((Key(list), bytes), (bytes,))
def rpushx(self, key, *values):
if not key:
return 0
return self.rpush(key, *values)
@command(fixed=(Key(list), bytes), repeat=(bytes,))
def lpos(self, key, elem, *args):
(rank, count, maxlen), _ = extract_args(
args,
(
"+rank",
"+count",
"+maxlen",
),
)
if rank == 0:
raise SimpleError(msgs.LPOS_RANK_CAN_NOT_BE_ZERO)
rank = rank or 1
ind, direction = (0, 1) if rank > 0 else (len(key.value) - 1, -1)
rank = abs(rank)
parse_count = len(key.value) if count == 0 else (count or 1)
maxlen = maxlen or len(key.value)
res: List[int] = []
comparisons = 0
while 0 <= ind <= len(key.value) - 1 and len(res) < parse_count and comparisons < maxlen:
comparisons += 1
if key.value[ind] == elem:
if rank > 1:
rank -= 1
else:
res.append(ind)
ind += direction
if len(res) == 0 and count is None:
return None
if len(res) == 1 and count is None:
return res[0]
return res
|