26
26
class ConfigDBConnector (SonicV2Connector ):
27
27
28
28
INIT_INDICATOR = 'CONFIG_DB_INITIALIZED'
29
- TABLE_NAME_SEPARATOR = '|'
30
- KEY_SEPARATOR = '|'
31
29
32
30
def __init__ (self , decode_responses = True , ** kwargs ):
33
31
# By default, connect to Redis through TCP, which does not requires root.
@@ -49,19 +47,26 @@ def __init__(self, decode_responses=True, **kwargs):
49
47
'namespace' is implicitly passed to the parent SonicV2Connector class.
50
48
"""
51
49
super (ConfigDBConnector , self ).__init__ (** kwargs )
50
+ # Trick: to achieve static/instance method "overload", we must use initize the function in ctor
51
+ # ref: https://stackoverflow.com/a/28766809/2514803
52
+ self .serialize_key = self ._serialize_key
53
+ self .deserialize_key = self ._deserialize_key
54
+
55
+ self .TABLE_NAME_SEPARATOR = '|'
56
+ self .KEY_SEPARATOR = '|'
52
57
self .handlers = {}
53
58
54
59
def __wait_for_db_init (self ):
55
60
client = self .get_redis_client (self .db_name )
56
61
pubsub = client .pubsub ()
57
- initialized = client .get (self .INIT_INDICATOR )
62
+ initialized = client .get (ConfigDBConnector .INIT_INDICATOR )
58
63
if not initialized :
59
- pattern = "__keyspace@{}__:{}" .format (self .get_dbid (self .db_name ), self .INIT_INDICATOR )
64
+ pattern = "__keyspace@{}__:{}" .format (self .get_dbid (self .db_name ), ConfigDBConnector .INIT_INDICATOR )
60
65
pubsub .psubscribe (pattern )
61
66
for item in pubsub .listen ():
62
67
if item ['type' ] == 'pmessage' :
63
68
key = item ['channel' ].split (':' , 1 )[1 ]
64
- if key == self .INIT_INDICATOR :
69
+ if key == ConfigDBConnector .INIT_INDICATOR :
65
70
initialized = client .get (self .INIT_INDICATOR )
66
71
if initialized :
67
72
break
@@ -151,21 +156,30 @@ def typed_to_raw(self, typed_data):
151
156
raw_data [key ] = str (value )
152
157
return raw_data
153
158
159
+ # Note: we could not use a class variable for KEY_SEPARATOR, but original dependent code is using
160
+ # these static functions. So we implement both static and instance functions with the same name.
161
+ # The static function will behave according to ConfigDB separators.
154
162
@staticmethod
155
- def serialize_key (key ):
163
+ def serialize_key (key , separator = '|' ):
156
164
if type (key ) is tuple :
157
- return ConfigDBConnector . KEY_SEPARATOR .join (key )
165
+ return separator .join (key )
158
166
else :
159
167
return str (key )
160
168
169
+ def _serialize_key (self , key ):
170
+ return ConfigDBConnector .serialize_key (key , self .KEY_SEPARATOR )
171
+
161
172
@staticmethod
162
- def deserialize_key (key ):
163
- tokens = key .split (ConfigDBConnector . KEY_SEPARATOR )
173
+ def deserialize_key (key , separator = '|' ):
174
+ tokens = key .split (separator )
164
175
if len (tokens ) > 1 :
165
176
return tuple (tokens )
166
177
else :
167
178
return key
168
179
180
+ def _deserialize_key (self , key ):
181
+ return ConfigDBConnector .deserialize_key (key , self .KEY_SEPARATOR )
182
+
169
183
def set_entry (self , table , key , data ):
170
184
"""Write a table entry to config db.
171
185
Remove extra fields in the db which are not in the data.
@@ -276,7 +290,6 @@ def delete_table(self, table):
276
290
client = self .get_redis_client (self .db_name )
277
291
pattern = '{}{}*' .format (table .upper (), self .TABLE_NAME_SEPARATOR )
278
292
keys = client .keys (pattern )
279
- data = {}
280
293
for key in keys :
281
294
client .delete (key )
282
295
0 commit comments