1- from typing import Union
1+ from typing import List , Optional , Union
22
3- from dataclasses import asdict
43from txredisapi import BaseRedisProtocol , ConnectionHandler
54from twisted .internet .defer import inlineCallbacks
65
76from RSO .base import BaseModel
8- from .index import ListIndex
97
108
119class Model (BaseModel ):
@@ -33,7 +31,7 @@ def save(self, redis: Union[BaseRedisProtocol, ConnectionHandler]):
3331
3432 @classmethod
3533 @inlineCallbacks
36- def search (cls , redis : ConnectionHandler , value ):
34+ def search (cls , redis : ConnectionHandler , value ) -> Optional [ 'Model' ] :
3735 redis_key = cls .redis_key_from_value (value )
3836 result = yield redis .exists (redis_key )
3937 if bool (result ):
@@ -43,6 +41,33 @@ def search(cls, redis: ConnectionHandler, value):
4341 return cls (** dict_data )
4442 return
4543
44+ @classmethod
45+ @inlineCallbacks
46+ def all (cls , redis : ConnectionHandler ) -> List ['Model' ]:
47+ redis_key = cls .redis_key_from_value ('*' )
48+ members = yield redis .keys (redis_key )
49+ indexes = []
50+ for index_class in cls .__indexes__ :
51+ indexes .append (
52+ f'::{ index_class .__index_name__ } ::{ index_class .__key__ } '
53+ )
54+
55+ pipe = yield redis .multi ()
56+ for member in members :
57+ is_index = False
58+ for index in indexes :
59+ if index in member :
60+ is_index = True
61+ break
62+ if not is_index :
63+ yield pipe .hgetall (member )
64+ result_data = yield pipe .commit ()
65+
66+ result = []
67+ for data in result_data :
68+ result .append (cls (** data ))
69+ return result
70+
4671 @inlineCallbacks
4772 def delete (self , redis : Union [BaseRedisProtocol , ConnectionHandler ]):
4873 if isinstance (redis , ConnectionHandler ):
@@ -56,48 +81,6 @@ def delete(self, redis: Union[BaseRedisProtocol, ConnectionHandler]):
5681 if getattr (self , index_class .__key__ ) is None :
5782 continue
5883 index_class .remove (pipe , self )
59- pipe .delete (self .redis_key )
84+ yield pipe .delete (self .redis_key )
6085 if do_commit :
6186 yield pipe .commit ()
62-
63- # @inlineCallbacks
64- # def extended_save(
65- # self, redis: Union[BaseRedisProtocol, ConnectionHandler]
66- # ):
67- # """Extended save function to avoid multiple push on list index
68- #
69- # Note: Use this method if you use List Index
70- # """
71- # list_index_map = {}
72- # for index_class in self.__indexes__ or []:
73- # if not issubclass(index_class, ListIndex):
74- # continue
75- # index = index_class.create_from_model_class(self)
76- # model_key_value = getattr(self, self.__key__, None)
77- # if model_key_value is None:
78- # exist_on_index = False
79- # else:
80- # exist_on_index = yield index.is_exist_on_list(
81- # redis, model_key_value
82- # )
83- # list_index_map[index] = exist_on_index
84- #
85- # if isinstance(redis, ConnectionHandler):
86- # pipe = yield redis.multi()
87- # else:
88- # raise NotImplementedError
89- #
90- # pipe.hmset(self.redis_key, self.to_redis())
91- # for index_class in self.__indexes__ or []:
92- # if getattr(self, index_class.__key__, None) is None:
93- # continue
94- # index = index_class.create_from_model_class(self)
95- # yield index.save_index(pipe)
96- #
97- # # remove duplicate on index queue list
98- # for index, exist_on_index in list_index_map.items():
99- # if exist_on_index is True:
100- # model_key_value = getattr(self, self.__key__)
101- # yield index.remove_from_list(pipe, model_key_value)
102- #
103- # yield pipe.commit()
0 commit comments