Redis: state.py db fixes
This commit is contained in:
parent
9af20e8328
commit
c83c390ae8
@ -9,7 +9,7 @@ from solar import errors
|
||||
class RedisDB(object):
|
||||
COLLECTIONS = Enum(
|
||||
'Collections',
|
||||
'connection resource'
|
||||
'connection resource state_data state_log'
|
||||
)
|
||||
DB = {
|
||||
'host': 'localhost',
|
||||
@ -21,9 +21,12 @@ class RedisDB(object):
|
||||
self.entities = {}
|
||||
|
||||
def read(self, uid, collection=COLLECTIONS.resource):
|
||||
return json.loads(
|
||||
self._r.get(self._make_key(collection, uid))
|
||||
)
|
||||
try:
|
||||
return json.loads(
|
||||
self._r.get(self._make_key(collection, uid))
|
||||
)
|
||||
except TypeError:
|
||||
return None
|
||||
|
||||
def save(self, uid, data, collection=COLLECTIONS.resource):
|
||||
return self._r.set(
|
||||
|
@ -76,8 +76,9 @@ class Log(object):
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
items = []
|
||||
if path in db:
|
||||
items = db[path] or items
|
||||
r = db.read(path, collection=db.COLLECTIONS.state_log)
|
||||
if r:
|
||||
items = r or items
|
||||
|
||||
self.items = deque([LogItem(
|
||||
l['uid'], l['res'],
|
||||
@ -85,7 +86,12 @@ class Log(object):
|
||||
getattr(STATES, l['state'])) for l in items])
|
||||
|
||||
def sync(self):
|
||||
db[self.path] = [i.to_dict() for i in self.items]
|
||||
#db[self.path] = [i.to_dict() for i in self.items]
|
||||
db.save(
|
||||
self.path,
|
||||
[i.to_dict() for i in self.items],
|
||||
collection=db.COLLECTIONS.state_log
|
||||
)
|
||||
|
||||
|
||||
def add(self, logitem):
|
||||
@ -121,19 +127,22 @@ class Data(collections.MutableMapping):
|
||||
def __init__(self, path):
|
||||
self.path = path
|
||||
self.store = {}
|
||||
if path in db:
|
||||
self.store = db[path] or self.store
|
||||
r = db.read(path, collection=db.COLLECTIONS.state_data)
|
||||
if r:
|
||||
self.store = r or self.store
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.store[key]
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self.store[key] = value
|
||||
db[self.path] = self.store
|
||||
db.save(self.path, self.store, collection=db.COLLECTIONS.state_data)
|
||||
#db[self.path] = self.store
|
||||
|
||||
def __delitem__(self, key):
|
||||
self.store.pop(key)
|
||||
db[self.path] = self.store
|
||||
db.save(self.path, self.store, collection=db.COLLECTIONS.state_data)
|
||||
#db[self.path] = self.store
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.store)
|
||||
|
Loading…
x
Reference in New Issue
Block a user