|
| 1 | +"""Command mixin for emulating `redis-py`'s BF functionality.""" |
| 2 | +import io |
| 3 | + |
| 4 | +import pybloom_live |
| 5 | + |
| 6 | +from fakeredis import _msgs as msgs |
| 7 | +from fakeredis._command_args_parsing import extract_args |
| 8 | +from fakeredis._commands import command, Key, CommandItem, Float, Int |
| 9 | +from fakeredis._helpers import SimpleError, OK, casematch |
| 10 | + |
| 11 | + |
| 12 | +class ScalableBloomFilter(pybloom_live.ScalableBloomFilter): |
| 13 | + NO_GROWTH = 0 |
| 14 | + |
| 15 | + def __init__(self, *args, **kwargs): |
| 16 | + super().__init__(*args, **kwargs) |
| 17 | + self.filters.append( |
| 18 | + pybloom_live.BloomFilter( |
| 19 | + capacity=self.initial_capacity, |
| 20 | + error_rate=self.error_rate * self.ratio)) |
| 21 | + |
| 22 | + def add(self, key): |
| 23 | + if key in self: |
| 24 | + return True |
| 25 | + if self.scale == self.NO_GROWTH and self.filters and self.filters[-1].count >= self.filters[-1].capacity: |
| 26 | + raise SimpleError(msgs.FILTER_FULL_MSG) |
| 27 | + return super(ScalableBloomFilter, self).add(key) |
| 28 | + |
| 29 | + |
| 30 | +class BFCommandsMixin: |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def _bf_add(key: CommandItem, item: bytes) -> int: |
| 34 | + res = key.value.add(item) |
| 35 | + key.updated() |
| 36 | + return 0 if res else 1 |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def _bf_exist(key: CommandItem, item: bytes) -> int: |
| 40 | + return 1 if (item in key.value) else 0 |
| 41 | + |
| 42 | + @command( |
| 43 | + name="BF.ADD", |
| 44 | + fixed=(Key(ScalableBloomFilter), bytes), |
| 45 | + repeat=(), |
| 46 | + ) |
| 47 | + def bf_add(self, key, value: bytes): |
| 48 | + return BFCommandsMixin._bf_add(key, value) |
| 49 | + |
| 50 | + @command( |
| 51 | + name="BF.MADD", |
| 52 | + fixed=(Key(ScalableBloomFilter), bytes), |
| 53 | + repeat=(bytes,), |
| 54 | + ) |
| 55 | + def bf_madd(self, key, *values): |
| 56 | + res = list() |
| 57 | + for value in values: |
| 58 | + res.append(BFCommandsMixin._bf_add(key, value)) |
| 59 | + return res |
| 60 | + |
| 61 | + @command( |
| 62 | + name="BF.CARD", |
| 63 | + fixed=(Key(ScalableBloomFilter),), |
| 64 | + repeat=(), |
| 65 | + ) |
| 66 | + def bf_card(self, key): |
| 67 | + return len(key.value) |
| 68 | + |
| 69 | + @command( |
| 70 | + name="BF.EXISTS", |
| 71 | + fixed=(Key(ScalableBloomFilter), bytes), |
| 72 | + repeat=(), |
| 73 | + ) |
| 74 | + def bf_exist(self, key, value: bytes): |
| 75 | + return BFCommandsMixin._bf_exist(key, value) |
| 76 | + |
| 77 | + @command( |
| 78 | + name="BF.MEXISTS", |
| 79 | + fixed=(Key(ScalableBloomFilter), bytes), |
| 80 | + repeat=(bytes,), |
| 81 | + ) |
| 82 | + def bf_mexists(self, key, *values: bytes): |
| 83 | + res = list() |
| 84 | + for value in values: |
| 85 | + res.append(BFCommandsMixin._bf_exist(key, value)) |
| 86 | + return res |
| 87 | + |
| 88 | + @command( |
| 89 | + name="BF.RESERVE", |
| 90 | + fixed=(Key(), Float, Int,), |
| 91 | + repeat=(bytes,), |
| 92 | + flags=msgs.FLAG_LEAVE_EMPTY_VAL, |
| 93 | + ) |
| 94 | + def bf_reserve(self, key: CommandItem, error_rate, capacity, *args: bytes): |
| 95 | + if key.value is not None: |
| 96 | + raise SimpleError(msgs.ITEM_EXISTS_MSG) |
| 97 | + (expansion, non_scaling), _ = extract_args(args, ("+expansion", "nonscaling")) |
| 98 | + if expansion is not None and non_scaling: |
| 99 | + raise SimpleError(msgs.NONSCALING_FILTERS_CANNOT_EXPAND_MSG) |
| 100 | + if expansion is None: |
| 101 | + expansion = 2 |
| 102 | + scale = ScalableBloomFilter.NO_GROWTH if non_scaling else expansion |
| 103 | + key.update(ScalableBloomFilter(capacity, error_rate, scale)) |
| 104 | + return OK |
| 105 | + |
| 106 | + @command( |
| 107 | + name="BF.INSERT", |
| 108 | + fixed=(Key(),), |
| 109 | + repeat=(bytes,), |
| 110 | + ) |
| 111 | + def bf_insert(self, key: CommandItem, *args: bytes): |
| 112 | + (capacity, error_rate, expansion, non_scaling, no_create), left_args = extract_args( |
| 113 | + args, ("+capacity", ".error", "+expansion", "nonscaling", "nocreate"), |
| 114 | + error_on_unexpected=False, left_from_first_unexpected=True) |
| 115 | + # if no_create and (capacity is not None or error_rate is not None): |
| 116 | + # raise SimpleError("...") |
| 117 | + if len(left_args) < 2 or not casematch(left_args[0], b'items'): |
| 118 | + raise SimpleError("...") |
| 119 | + items = left_args[1:] |
| 120 | + |
| 121 | + error_rate = error_rate or 0.001 |
| 122 | + capacity = capacity or 100 |
| 123 | + if key.value is None and no_create: |
| 124 | + raise SimpleError(msgs.NOT_FOUND_MSG) |
| 125 | + if expansion is not None and non_scaling: |
| 126 | + raise SimpleError(msgs.NONSCALING_FILTERS_CANNOT_EXPAND_MSG) |
| 127 | + if expansion is None: |
| 128 | + expansion = 2 |
| 129 | + scale = ScalableBloomFilter.NO_GROWTH if non_scaling else expansion |
| 130 | + if key.value is None: |
| 131 | + key.value = ScalableBloomFilter(capacity, error_rate, scale) |
| 132 | + res = list() |
| 133 | + for item in items: |
| 134 | + res.append(self._bf_add(key, item)) |
| 135 | + key.updated() |
| 136 | + return res |
| 137 | + |
| 138 | + @command( |
| 139 | + name="BF.INFO", |
| 140 | + fixed=(Key(),), |
| 141 | + repeat=(bytes,), |
| 142 | + ) |
| 143 | + def bf_info(self, key: CommandItem, *args: bytes): |
| 144 | + if key.value is None or type(key.value) != ScalableBloomFilter: |
| 145 | + raise SimpleError('...') |
| 146 | + if len(args) > 1: |
| 147 | + raise SimpleError(msgs.SYNTAX_ERROR_MSG) |
| 148 | + if len(args) == 0: |
| 149 | + return [ |
| 150 | + b'Capacity', key.value.capacity, |
| 151 | + b'Size', key.value.capacity, |
| 152 | + b'Number of filters', len(key.value.filters), |
| 153 | + b'Number of items inserted', key.value.count, |
| 154 | + b'Expansion rate', key.value.scale if key.value.scale > 0 else None, |
| 155 | + ] |
| 156 | + if casematch(args[0], b'CAPACITY'): |
| 157 | + return key.value.capacity |
| 158 | + elif casematch(args[0], b'SIZE'): |
| 159 | + return key.value.capacity |
| 160 | + elif casematch(args[0], b'FILTERS'): |
| 161 | + return len(key.value.filters) |
| 162 | + elif casematch(args[0], b'ITEMS'): |
| 163 | + return key.value.count |
| 164 | + elif casematch(args[0], b'EXPANSION'): |
| 165 | + return key.value.scale if key.value.scale > 0 else None |
| 166 | + else: |
| 167 | + raise SimpleError(msgs.SYNTAX_ERROR_MSG) |
| 168 | + |
| 169 | + @command( |
| 170 | + name="BF.SCANDUMP", |
| 171 | + fixed=(Key(), Int,), |
| 172 | + repeat=(), |
| 173 | + flags=msgs.FLAG_LEAVE_EMPTY_VAL, |
| 174 | + ) |
| 175 | + def bf_scandump(self, key: CommandItem, iterator: int): |
| 176 | + if key.value is None: |
| 177 | + raise SimpleError(msgs.NOT_FOUND_MSG) |
| 178 | + f = io.BytesIO() |
| 179 | + |
| 180 | + if iterator == 0: |
| 181 | + key.value.tofile(f) |
| 182 | + f.seek(0) |
| 183 | + s = f.read() |
| 184 | + f.close() |
| 185 | + return [1, s] |
| 186 | + else: |
| 187 | + return [0, None] |
| 188 | + |
| 189 | + @command( |
| 190 | + name="BF.LOADCHUNK", |
| 191 | + fixed=(Key(), Int, bytes), |
| 192 | + repeat=(), |
| 193 | + flags=msgs.FLAG_LEAVE_EMPTY_VAL, |
| 194 | + ) |
| 195 | + def bf_loadchunk(self, key: CommandItem, iterator: int, data: bytes): |
| 196 | + if key.value is not None and type(key.value) != ScalableBloomFilter: |
| 197 | + raise SimpleError(msgs.NOT_FOUND_MSG) |
| 198 | + f = io.BytesIO(data) |
| 199 | + key.value = ScalableBloomFilter.fromfile(f) |
| 200 | + f.close() |
| 201 | + key.updated() |
| 202 | + return OK |
0 commit comments