def checksite(): if R.exists(POSTINCR): print "Redis up and ready to go. \ Site implemented with incr: %i" % R.get(POSTINCR) return True else: R.incr(POSTINCR) print "Initialized to: %i" % R.get(POSTINCR) return True
def getall(self): """ return kwargs this is where things get tricky... for now refer to str. We're just getting off the ground query is the biggest step. """ #self.pid, self.uid = key(id) got = [] for pid, kind in R.lrange("global:docs", 0, R.llen("global:docs")): got.append(R.get("%s:%s" % (kind, pid))) got.append(R.get("%s:%s:publishtime" % (kind, pid))) return str(got)
def __str__(self): """ simply dumps the server's contents """ got = {} self.uids = R.smembers(DEXREF) # all the uids self.kinds = R.smembers(TYPELST) # all types of docs for uid in self.uids: got[uid] = '' for kind in self.kinds: got[uid] = [R.get("%s:%s" % (kind, uid))] pid = pid_from_uid(uid, kind) got['%s:%s' % (kind, uid)] = { 'kind' : kind, 'datetime' : R.get("%s:%s:publishtime" %\ (kind, pid))} pprint.pprint(got)
def post(self, container=None): """ Metadata """ self.uid = uuid.uuid1() assert unique(self.uid) self.atrs = {'uid': uuid.uuid1(), self.incr: R.get(POSTINCR), 'kind': self.kind, 'publishtime': datetime.datetime.now()} for key in self.atrs.keys(): R.setnx("%s:%s:%s" % (self.kind, self.incr, key), self.atrs[key]) # stuff forgetting, errr, for getting. R.sadd(DEXREF, self.uid) # Global reference R.sadd(TYPELST, self.kind) # add to container if not isinstance(container, str) and not container==None: container = "_meta" R.rpush(container, "%s:%s" % (self.kind, self.incr)) R.rpush(RANKREF, "%s:%s" % (self.kind, self.incr)) R.zadd("%s:%s" % (RANKREF, self.kind), "%s:%s" % (self.kind,\ self.incr), self.rank) R.rpush("global:docs", "%s:%s" % (self.incr, self.uid)) return True
def key(pid=None, uid=None, kind='global'): """ A general look-up method for stuff. Actually, it's more of a graph between the keys. """ if pid == None and uid == None: raise ValueError("Need a uuid or pid") elif isinstance(uid, uuid.UUID): pid = R.get("%s:%s" % (kind, uid)) uid = R.get("%s:%i:uid" % (kind, self.pid)) elif isinstance(uid, str): pid = R.get("%s:%s" % (kind, uid)) uid = R.get("%s:%s:uid" % (kind, self.pid)) elif isinstance(pid, int): uid = R.get("%s:%s:uid" % (kind, pid)) pid = R.get("%s:%s" % (kind, self.uid)) else: raise ValueError("Ok, something went wrong...") return (pid, uid)
def pid_from_uid(uid, kind): """ Returns the pid from the uid """ return R.get("%s:%s" % (kind, uid))