@@ -205,83 +205,194 @@ def is_indexed_username(username):
205205 return r_search .exists (f'm:u:{ username } ' )
206206
207207
208- def search_domain (s_domain = None , r_pos = False ): # TODO paginate
208+ def search_domain (s_domain = None , r_pos = False , page = 1 , nb = 500 ):
209209 if not s_domain :
210- return r_search .smembers ('m:domains' ) # TODO paginate
210+ domains = []
211+ total = r_search .scard ('m:domains' )
212+ start = nb * (page - 1 )
213+ stop = start + nb - 1
214+ cursor = 0
215+ for domain in r_search .smembers ('m:domains' ):
216+ if start <= cursor <= stop :
217+ domains .append (domain )
218+ elif cursor > stop :
219+ break
220+ cursor += 1
221+ return total , domains
211222 else :
212223 domains = {}
213224 re_search = re .compile (s_domain )
214- for domain in sscan_iterator (r_search , 'm:domains' ):
215- if s_domain in domain :
216- res = re .search (re_search , domain )
217- if res :
218- domains [domain ] = {}
219- if r_pos :
225+ total , results = get_cache_search_mail (s_domain = s_domain , page = page , nb = nb )
226+ if results is None :
227+ results = []
228+ start = nb * (page - 1 )
229+ stop = start + nb - 1
230+ cursor = 0
231+ for domain in sscan_iterator (r_search , 'm:domains' ):
232+ if s_domain in domain :
233+ results .append (domain )
234+ if start <= cursor <= stop :
235+ domains [domain ] = {}
236+ if r_pos :
237+ res = re .search (re_search , domain )
238+ if res :
239+ domains [domain ]['hl-start' ] = res .start ()
240+ domains [domain ]['hl-end' ] = res .end ()
241+ domains [domain ]['content' ] = domain
242+ cursor += 1
243+ total = len (results )
244+ if results :
245+ cache_search_mail (results , s_domain = s_domain )
246+ else :
247+ for domain in results :
248+ domains [domain ] = {}
249+ if r_pos :
250+ res = re .search (re_search , domain )
251+ if res :
220252 domains [domain ]['hl-start' ] = res .start ()
221253 domains [domain ]['hl-end' ] = res .end ()
222254 domains [domain ]['content' ] = domain
223- return domains
255+ return total , domains
224256
225- def search_domain_username (domain , s_username = None , r_pos = False ): # TODO paginate
257+ def search_domain_username (domain , s_username = None , r_pos = False , page = 1 , nb = 500 ):
226258 objs = {}
227259 if not s_username :
228- for username in r_search .smembers (f'm:d:{ domain } ' ): # TODO paginate + SSCAN ????
229- content = f'{ username } @{ domain } '
230- obj_id = get_mail_id (content )
231- objs [obj_id ] = {}
232- objs [obj_id ]['content' ] = content
260+ total = r_search .scard (f'm:d:{ domain } ' )
261+ start = nb * (page - 1 )
262+ stop = start + nb - 1
263+ cursor = 0
264+ for username in sscan_iterator (r_search , f'm:d:{ domain } ' ):
265+ if start <= cursor <= stop :
266+ content = f'{ username } @{ domain } '
267+ obj_id = get_mail_id (content )
268+ objs [obj_id ] = {}
269+ objs [obj_id ]['content' ] = content
270+ elif cursor > stop :
271+ break
272+ cursor += 1
233273 else :
234274 re_search = re .compile (s_username )
235- for username in sscan_iterator (r_search , f'm:d:{ domain } ' ):
236- if s_username in username :
237- res = re .search (re_search , username )
238- if res :
239- content = f'{ username } @{ domain } '
240- obj_id = get_mail_id (content )
241- objs [obj_id ] = {}
242- if r_pos :
275+ total , results = get_cache_search_mail (domain = domain , s_username = s_username , page = page , nb = nb )
276+ if results is None :
277+ results = []
278+ start = nb * (page - 1 )
279+ stop = start + nb - 1
280+ cursor = 0
281+ for username in sscan_iterator (r_search , f'm:d:{ domain } ' ):
282+ if s_username in username :
283+ results .append (username )
284+ if start <= cursor <= stop :
285+ content = f'{ username } @{ domain } '
286+ obj_id = get_mail_id (content )
287+ objs [obj_id ] = {}
288+ if r_pos :
289+ res = re .search (re_search , username )
290+ if res :
291+ objs [obj_id ]['hl-start' ] = res .start ()
292+ objs [obj_id ]['hl-end' ] = res .end ()
293+ objs [obj_id ]['content' ] = content
294+ cursor += 1
295+ total = len (results )
296+ if results :
297+ cache_search_mail (results , domain = domain , s_username = s_username )
298+ else :
299+ for user in results :
300+ content = f'{ user } @{ domain } '
301+ obj_id = get_mail_id (content )
302+ objs [obj_id ] = {}
303+ if r_pos :
304+ res = re .search (re_search , user )
305+ if res :
243306 objs [obj_id ]['hl-start' ] = res .start ()
244307 objs [obj_id ]['hl-end' ] = res .end ()
245308 objs [obj_id ]['content' ] = content
246- return objs
309+ return total , objs
310+
247311
248- def search_username_domain (username , s_domain = None , r_pos = False ):
312+ def search_username_domain (username , s_domain = None , r_pos = False , page = 1 , nb = 500 ):
249313 objs = {}
250314 if not s_domain :
251- for domain in r_search .smembers (f'm:u:{ username } ' ): # TODO paginate
252- obj_id = f'{ username } @{ domain } '
253- objs [obj_id ] = {}
315+ total = r_search .scard (f'm:u:{ username } ' )
316+ start = nb * (page - 1 )
317+ stop = start + nb - 1
318+ cursor = 0
319+ for domain in sscan_iterator (r_search , f'm:u:{ username } ' ):
320+ if start <= cursor <= stop :
321+ content = f'{ username } @{ domain } '
322+ obj_id = get_mail_id (content )
323+ objs [obj_id ] = {}
324+ objs [obj_id ]['content' ] = content
325+ if cursor > stop :
326+ break
327+ cursor += 1
254328 else :
329+ total , results = get_cache_search_mail (username = username , s_domain = s_domain , page = page , nb = page )
255330 re_search = re .compile (s_domain )
256- for domain in sscan_iterator (r_search , f'm:u:{ username } ' ):
257- if s_domain in domain :
258- res = re .search (re_search , domain )
259- if res :
260- content = f'{ username } @{ domain } '
261- obj_id = get_mail_id (content )
262- objs [obj_id ] = {}
263- if r_pos :
331+ if results is None : # TODO no results
332+ results = []
333+ start = nb * (page - 1 )
334+ stop = start + nb
335+ cursor = 0
336+ for domain in sscan_iterator (r_search , f'm:u:{ username } ' ):
337+ if s_domain in domain :
338+ results .append (domain )
339+ if start <= cursor <= stop :
340+ content = f'{ username } @{ domain } '
341+ obj_id = get_mail_id (content )
342+ objs [obj_id ] = {}
343+ if r_pos :
344+ res = re .search (re_search , domain )
345+ if res :
346+ objs [obj_id ]['hl-start' ] = len (username ) + 1 + res .start ()
347+ objs [obj_id ]['hl-end' ] = len (username ) + 1 + res .end ()
348+ objs [obj_id ]['content' ] = content
349+ cursor += 1
350+ total = len (results )
351+ if results :
352+ cache_search_mail (results , username = username , s_domain = s_domain )
353+ else :
354+ for dom in results :
355+ content = f'{ username } @{ dom } '
356+ obj_id = get_mail_id (content )
357+ objs [obj_id ] = {}
358+ if r_pos :
359+ res = re .search (re_search , dom )
360+ if res :
264361 objs [obj_id ]['hl-start' ] = len (username ) + 1 + res .start ()
265362 objs [obj_id ]['hl-end' ] = len (username ) + 1 + res .end ()
266363 objs [obj_id ]['content' ] = content
267- return objs
364+ return total , objs
268365
269366
270- def search_mail (mail = None , username = None , domain = None , s_username = None , s_domain = None , r_pos = False ):
271- if mail : # TODO
367+ def search_mail (mail = None , username = None , domain = None , s_username = None , s_domain = None , r_pos = False , page = 1 , nb = 500 ):
368+ if mail :
272369 m = get_mail (mail )
273370 if m .exists ():
274- return m .get_id ()
371+ return 1 , m .get_id ()
275372
276373 if domain :
277374 if is_indexed_domain (domain ):
278- return search_domain_username (domain , s_username = s_username , r_pos = r_pos )
375+ return search_domain_username (domain , s_username = s_username , r_pos = r_pos , page = page , nb = nb )
279376 elif username :
280377 if is_indexed_username (username ):
281- return search_username_domain (username , s_domain = s_domain , r_pos = r_pos )
378+ return search_username_domain (username , s_domain = s_domain , r_pos = r_pos , page = page , nb = nb )
282379 elif s_domain :
283- return search_domain (s_domain = s_domain , r_pos = r_pos )
284- return None
380+ return search_domain (s_domain = s_domain , r_pos = r_pos , page = page , nb = nb )
381+ return None , None
382+
383+ def cache_search_mail (to_cache , username = '' , domain = '' , s_username = '' , s_domain = '' ):
384+ for result in to_cache :
385+ r_cache .rpush (f'm:{ username } :{ domain } :{ s_username } :{ s_domain } ' , result )
386+ r_cache .expire (f'm:{ username } :{ domain } :{ s_username } :{ s_domain } ' , 600 )
387+
388+ def get_cache_search_mail (username = '' , domain = '' , s_username = '' , s_domain = '' , page = 1 , nb = 500 ):
389+ total = r_cache .llen (f'm:{ username } :{ domain } :{ s_username } :{ s_domain } ' )
390+ if not total :
391+ return None , None
392+ else :
393+ start = nb * (page - 1 )
394+ stop = start + nb - 1
395+ return total , r_cache .lrange (f'm:{ username } :{ domain } :{ s_username } :{ s_domain } ' , start , stop )
285396
286397
287398class Mails (AbstractDaterangeObjects ):
0 commit comments