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