1010from fakeredis ._commands import Key , Int , Float , command , CommandItem
1111from fakeredis ._helpers import OK , SimpleError , SimpleString
1212
13- random .seed (time .time ())
14-
1513
1614class Bucket (object ):
17-
1815 def __init__ (self , counter : int , fingerprint : int ):
1916 self .counter = counter
2017 self .fingerprint = fingerprint
@@ -36,7 +33,7 @@ def count(self, fingerprint: int) -> int:
3633
3734 def _decay (self , decay : float ) -> bool :
3835 if self .counter > 0 :
39- probability = decay ** self .counter
36+ probability = decay ** self .counter
4037 if probability >= 1 or random .random () < probability :
4138 self .counter -= 1
4239 return self .counter == 0
@@ -64,7 +61,11 @@ def _hash(self, item: bytes) -> int:
6461
6562
6663class HeavyKeeper (object ):
64+ is_topk_initialized = False
65+
6766 def __init__ (self , k : int , width : int = 1024 , depth : int = 5 , decay : float = 0.9 ) -> None :
67+ if not HeavyKeeper .is_topk_initialized :
68+ random .seed (time .time ())
6869 self .k = k
6970 self .width = width
7071 self .depth = depth
@@ -115,15 +116,7 @@ class TopkCommandsMixin:
115116 def __init__ (self , * args : Any , ** kwargs : Any ) -> None :
116117 super ().__init__ (* args , ** kwargs )
117118
118- @command (
119- name = "TOPK.ADD" ,
120- fixed = (
121- Key (HeavyKeeper ),
122- bytes ,
123- ),
124- repeat = (bytes ,),
125- flags = msgs .FLAG_DO_NOT_CREATE ,
126- )
119+ @command (name = "TOPK.ADD" , fixed = (Key (HeavyKeeper ), bytes ), repeat = (bytes ,), flags = msgs .FLAG_DO_NOT_CREATE )
127120 def topk_add (self , key : CommandItem , * args : bytes ) -> List [Optional [bytes ]]:
128121 if key .value is None :
129122 raise SimpleError ("TOPK: key does not exist" )
@@ -133,15 +126,7 @@ def topk_add(self, key: CommandItem, *args: bytes) -> List[Optional[bytes]]:
133126 key .updated ()
134127 return res
135128
136- @command (
137- name = "TOPK.COUNT" ,
138- fixed = (
139- Key (HeavyKeeper ),
140- bytes ,
141- ),
142- repeat = (bytes ,),
143- flags = msgs .FLAG_DO_NOT_CREATE ,
144- )
129+ @command (name = "TOPK.COUNT" , fixed = (Key (HeavyKeeper ), bytes ), repeat = (bytes ,), flags = msgs .FLAG_DO_NOT_CREATE )
145130 def topk_count (self , key : CommandItem , * args : bytes ) -> List [int ]:
146131 if key .value is None :
147132 raise SimpleError ("TOPK: key does not exist" )
@@ -150,15 +135,7 @@ def topk_count(self, key: CommandItem, *args: bytes) -> List[int]:
150135 res : List [int ] = [key .value .count (_item ) for _item in args ]
151136 return res
152137
153- @command (
154- name = "TOPK.QUERY" ,
155- fixed = (
156- Key (HeavyKeeper ),
157- bytes ,
158- ),
159- repeat = (bytes ,),
160- flags = msgs .FLAG_DO_NOT_CREATE ,
161- )
138+ @command (name = "TOPK.QUERY" , fixed = (Key (HeavyKeeper ), bytes ), repeat = (bytes ,), flags = msgs .FLAG_DO_NOT_CREATE )
162139 def topk_query (self , key : CommandItem , * args : bytes ) -> List [int ]:
163140 if key .value is None :
164141 raise SimpleError ("TOPK: key does not exist" )
@@ -168,16 +145,7 @@ def topk_query(self, key: CommandItem, *args: bytes) -> List[int]:
168145 res : List [int ] = [1 if _item in topk else 0 for _item in args ]
169146 return res
170147
171- @command (
172- name = "TOPK.INCRBY" ,
173- fixed = (
174- Key (),
175- bytes ,
176- Int ,
177- ),
178- repeat = (bytes , Int ),
179- flags = msgs .FLAG_DO_NOT_CREATE ,
180- )
148+ @command (name = "TOPK.INCRBY" , fixed = (Key (), bytes , Int ), repeat = (bytes , Int ), flags = msgs .FLAG_DO_NOT_CREATE )
181149 def topk_incrby (self , key : CommandItem , * args : Any ) -> List [Optional [bytes ]]:
182150 if key .value is None :
183151 raise SimpleError ("TOPK: key does not exist" )
@@ -192,12 +160,7 @@ def topk_incrby(self, key: CommandItem, *args: Any) -> List[Optional[bytes]]:
192160 key .updated ()
193161 return res
194162
195- @command (
196- name = "TOPK.INFO" ,
197- fixed = (Key (),),
198- repeat = (),
199- flags = msgs .FLAG_DO_NOT_CREATE ,
200- )
163+ @command (name = "TOPK.INFO" , fixed = (Key (),), repeat = (), flags = msgs .FLAG_DO_NOT_CREATE )
201164 def topk_info (self , key : CommandItem ) -> List [Any ]:
202165 if key .value is None :
203166 raise SimpleError ("TOPK: key does not exist" )
@@ -214,12 +177,7 @@ def topk_info(self, key: CommandItem) -> List[Any]:
214177 key .value .decay ,
215178 ]
216179
217- @command (
218- name = "TOPK.LIST" ,
219- fixed = (Key (),),
220- repeat = (bytes ,),
221- flags = msgs .FLAG_DO_NOT_CREATE ,
222- )
180+ @command (name = "TOPK.LIST" , fixed = (Key (),), repeat = (bytes ,), flags = msgs .FLAG_DO_NOT_CREATE )
223181 def topk_list (self , key : CommandItem , * args : Any ) -> List [Any ]:
224182 (withcount ,), _ = extract_args (args , ("withcount" ,))
225183 if key .value is None :
@@ -233,19 +191,7 @@ def topk_list(self, key: CommandItem, *args: Any) -> List[Any]:
233191 temp = [[item [1 ], item [0 ]] for item in value_list ]
234192 return [item for sublist in temp for item in sublist ]
235193
236- @command (
237- name = "TOPK.RESERVE" ,
238- fixed = (
239- Key (),
240- Int ,
241- ),
242- repeat = (
243- Int ,
244- Int ,
245- Float ,
246- ),
247- flags = msgs .FLAG_DO_NOT_CREATE ,
248- )
194+ @command (name = "TOPK.RESERVE" , fixed = (Key (), Int ), repeat = (Int , Int , Float ), flags = msgs .FLAG_DO_NOT_CREATE )
249195 def topk_reserve (self , key : CommandItem , topk : int , * args : Any ) -> SimpleString :
250196 if len (args ) == 3 :
251197 width , depth , decay = args
0 commit comments