-
Notifications
You must be signed in to change notification settings - Fork 5
/
engine.py
64 lines (57 loc) · 2.34 KB
/
engine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# TODO: consider storing multiple versions of each entity
import pprint
import redis
from errors import Error
import logging
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)
class Engine(object):
def __init__(self, **kwargs):
try:
self.verbose = kwargs.pop("verbose")
except KeyError:
pass
self.conn = redis.StrictRedis(**kwargs)
def redis(self, func, *args, **kwargs):
retval = func(*args, **kwargs)
if self.verbose:
log.debug("REDIS: %s(%s, %s) -> %s" % (func.__name__, repr(args)[1:-1], kwargs or "", repr(retval)))
return retval
def execute(self, ast):
if self.verbose:
log.debug("AST: %s" % pprint.pformat(ast))
retval = [getattr(self, item[0])(item[1:]) for item in ast]
if len(retval) == 1:
return retval[0]
def iterselect(self, params):
conn = self.conn
attnames, tablename, whereclause, orderbyatt = params
idvalues = self.redis(conn.smembers, "%s_id" % tablename)
if attnames == "*":
for idvalue in idvalues:
mapping = self.redis(conn.hgetall, "%s:%s" % (tablename, idvalue))
yield mapping
else:
for idvalue in idvalues:
data = self.redis(conn.hmget, "%s:%s" % (tablename, idvalue), attnames)
yield dict(zip(attnames, data))
def select(self, params):
attnames, tablename, whereclause, orderbyatt = params
rowset = list(self.iterselect(params))
if orderbyatt:
rowset.sort(lambda lhs, rhs : cmp(lhs.get(orderbyatt), rhs.get(orderbyatt)))
return rowset
def insert(self, params):
tablename, attnames, attvalues = params
#tablename = tablename.lower()
#attnames = [attname.lower() for attname in attnames]
if not len(attnames) == len(attvalues):
raise Error("number of columns does not match number of values")
mapping = dict(zip(attnames, attvalues))
try:
idvalue = mapping["id"]
except KeyError:
raise Error("you must specify the 'id' column and value")
self.conn.sadd("%s_id" % tablename, idvalue)
self.conn.hmset("%s:%s" % (tablename, idvalue), mapping)