Skip to content

Commit 1c7a6b4

Browse files
authored
[configdb]: add mod_entry, rename set_config to mod_config (#25)
* [configdb]: add mod_entry, rename set_config to mod_config mod_entry will not delete extra data in the db. change the set_config to mod_config so that mod_config does not delete extra data in the db
1 parent b7861cc commit 1c7a6b4

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/swsssdk/configdb.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Write to config DB
66
config_db = ConfigDBConnector()
77
config_db.connect()
8-
config_db.set_entry('BGP_NEIGHBOR', '10.0.0.1', {
8+
config_db.mod_entry('BGP_NEIGHBOR', '10.0.0.1', {
99
'admin_status': state
1010
})
1111
@@ -144,6 +144,7 @@ def deserialize_key(key):
144144

145145
def set_entry(self, table, key, data):
146146
"""Write a table entry to config db.
147+
Remove extra fields in the db which are not in the data.
147148
Args:
148149
table: Table name.
149150
key: Key of table entry, or a tuple of keys if it is a multi-key table.
@@ -164,6 +165,23 @@ def set_entry(self, table, key, data):
164165
k = k + '@'
165166
client.hdel(_hash, self.serialize_key(k))
166167

168+
def mod_entry(self, table, key, data):
169+
"""Modify a table entry to config db.
170+
Args:
171+
table: Table name.
172+
key: Key of table entry, or a tuple of keys if it is a multi-key table.
173+
data: Table row data in a form of dictionary {'column_key': 'value', ...}.
174+
Pass {} as data will create an entry with no column if not already existed.
175+
Pass None as data will delete the entry.
176+
"""
177+
key = self.serialize_key(key)
178+
client = self.redis_clients[self.CONFIG_DB]
179+
_hash = '{}{}{}'.format(table.upper(), self.TABLE_NAME_SEPARATOR, key)
180+
if data == None:
181+
client.delete(_hash)
182+
else:
183+
client.hmset(_hash, self.__typed_to_raw(data))
184+
167185
def get_entry(self, table, key):
168186
"""Read a table entry from config db.
169187
Args:
@@ -202,8 +220,9 @@ def get_table(self, table):
202220
pass #Ignore non table-formated redis entries
203221
return data
204222

205-
def set_config(self, data):
206-
"""Write multiple tables into config db.
223+
def mod_config(self, data):
224+
"""Write multiple tables into config db.
225+
Extra entries/fields in the db which are not in the data are kept.
207226
Args:
208227
data: config data in a dictionary form
209228
{
@@ -215,7 +234,7 @@ def set_config(self, data):
215234
for table_name in data:
216235
table_data = data[table_name]
217236
for key in table_data:
218-
self.set_entry(table_name, key, table_data[key])
237+
self.mod_entry(table_name, key, table_data[key])
219238

220239
def get_config(self):
221240
"""Read all config data.

0 commit comments

Comments
 (0)