Skip to content

Commit

Permalink
Add support to remove the key of table entry (#15)
Browse files Browse the repository at this point in the history
* Add support to remove the key of table entry

* [configdb]: Fix set_entry error exception
  • Loading branch information
Liuqu authored and lguohan committed Dec 5, 2017
1 parent 0af5d73 commit 993d961
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/swsssdk/configdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,20 @@ def __raw_to_typed(self, raw_data):

def __typed_to_raw(self, typed_data):
if typed_data == None:
return None
return None, None
elif typed_data == {}:
return { "NULL": "NULL" }
return { "NULL": "NULL" }, None
raw_data = {}
del_data = []
for key in typed_data:
value = typed_data[key]
if type(value) is list:
raw_data[key+'@'] = ','.join(value)
elif value is None:
del_data.append(key)
else:
raw_data[key] = value
return raw_data
return raw_data, del_data

@staticmethod
def serialize_key(key):
Expand All @@ -150,14 +153,19 @@ def set_entry(self, table, key, data):
data: Table row data in a form of dictionary {'column_key': 'value', ...}.
Pass {} as data will create an entry with no column if not already existed.
Pass None as data will delete the entry.
Pass {'column_key': None, ...} as data will delete the key which value is None.
"""
key = self.serialize_key(key)
client = self.redis_clients[self.CONFIG_DB]
_hash = '{}{}{}'.format(table.upper(), self.TABLE_NAME_SEPARATOR, key)
if data == None:
client.delete(_hash)
else:
client.hmset(_hash, self.__typed_to_raw(data))
raw_data, del_data = self.__typed_to_raw(data)
if del_data:
client.hdel(_hash, *del_data)
if raw_data:
client.hmset(_hash, raw_data)

def get_entry(self, table, key):
"""Read a table entry from config db.
Expand Down

0 comments on commit 993d961

Please sign in to comment.