Adds methods to store the locator map in the db

In order to use the db for system monitoring we need a way of mapping the locator the the uuids the appointments/jobs are identified in the db. First steps towards #15
This commit is contained in:
Sergi Delgado Segura
2019-11-06 17:08:05 +00:00
parent 6a476751fa
commit 810f6ec525
2 changed files with 73 additions and 3 deletions

View File

@@ -2,7 +2,8 @@ import json
import plyvel
from pisa.logger import Logger
from pisa.conf import WATCHER_PREFIX, RESPONDER_PREFIX, WATCHER_LAST_BLOCK_KEY, RESPONDER_LAST_BLOCK_KEY
from pisa.conf import WATCHER_PREFIX, RESPONDER_PREFIX, WATCHER_LAST_BLOCK_KEY, RESPONDER_LAST_BLOCK_KEY, \
LOCATOR_MAP_PREFIX
logger = Logger("DBManager")
@@ -73,6 +74,40 @@ class DBManager:
self.create_entry(uuid, job, prefix=RESPONDER_PREFIX)
logger.info("Adding appointment to Responder's db", uuid=uuid)
def load_locator_map(self, locator):
key = (LOCATOR_MAP_PREFIX+locator).encode('utf-8')
locator_map = self.db.get(key)
if locator_map is not None:
locator_map = json.loads(locator_map.decode('utf-8'))
else:
logger.info("Locator not found in the db", locator=locator)
return locator_map
def store_update_locator_map(self, locator, uuid):
locator_map = self.load_locator_map(locator)
if locator_map is not None:
if uuid not in locator_map:
locator_map.append(uuid)
logger.info("Updating locator map", locator=locator, uuid=uuid)
else:
logger.info("UUID already in the map", locator=locator, uuid=uuid)
else:
locator_map = [uuid]
logger.info("Creating new locator map", locator=locator, uuid=uuid)
key = (LOCATOR_MAP_PREFIX + locator).encode('utf-8')
self.db.put(key, json.dumps(locator_map).encode('utf-8'))
def delete_locator_map(self, locator):
self.delete_entry(locator, prefix=LOCATOR_MAP_PREFIX)
logger.info("Deleting locator map from db", uuid=locator)
def delete_watcher_appointment(self, uuid):
self.delete_entry(uuid, prefix=WATCHER_PREFIX)
logger.info("Deleting appointment from Watcher's db", uuid=uuid)