def find_one(cls, query={}): """Returns one node as a Node object or None.""" from_cache = whiskeycache.find_one(cls, query) if from_cache is not None: return from_cache else: data = cls.COLLECTION.find_one(query, sort=[("_id", -1)]) if data is not None: return whiskeycache.from_cache(cls, data, dirty=False) else: return None
def find_one(cls, query={}): '''Returns one node as a Node object or None.''' from_cache = whiskeycache.find_one(cls, query) if from_cache is not None: return from_cache else: data = cls.COLLECTION.find_one(query, sort=[('_id',-1)]) if data is not None: return whiskeycache.from_cache(cls, data, dirty=False) else: return None
def next(self): """ this will return the items in cache and the db""" if self.__limit == 0 or self.__retrieved < self.__limit: self.__retrieved = self.__retrieved + 1 if len(self.existing) > 0: if self.__d is None: try: self.__d = self.cursor.next() except StopIteration: return self.existing.popleft() d = self.__d attr_existing = getattr(self.existing[0], sort[0][0]) attr_d = d.get(sort[0][0]) if sort[0][1] == -1: if attr_existing > attr_d: return self.existing.popleft() else: if attr_existing < attr_d: return self.existing.popleft() if self.existing[0]._id == d["_id"]: self.__d = None return self.existing.popleft() else: self.__d = None rv = whiskeycache.from_cache(cls, d, dirty=False) try: self.existing.remove( rv ) # todo test to see if "rv in self.existing" is faster than try excepting except ValueError: pass return rv else: if self.__d: d = self.__d self.__d = None return whiskeycache.from_cache(cls, d, dirty=False) else: return whiskeycache.from_cache(cls, self.cursor.next(), dirty=False) raise StopIteration()
def next(self): ''' this will return the items in cache and the db''' if self.__limit == 0 or self.__retrieved < self.__limit: self.__retrieved = self.__retrieved + 1 if len(self.existing) > 0: if self.__d is None: try: self.__d = self.cursor.next() except StopIteration: return self.existing.popleft() d = self.__d attr_existing = getattr(self.existing[0], sort[0][0]) attr_d = d.get(sort[0][0]) if sort[0][1] == -1: if attr_existing > attr_d: return self.existing.popleft() else: if attr_existing < attr_d: return self.existing.popleft() if self.existing[0]._id == d['_id']: self.__d = None return self.existing.popleft() else: self.__d = None rv = whiskeycache.from_cache(cls, d, dirty=False) try: self.existing.remove(rv) #todo test to see if "rv in self.existing" is faster than try excepting except ValueError: pass return rv else: if self.__d: d = self.__d self.__d = None return whiskeycache.from_cache(cls, d, dirty=False) else: return whiskeycache.from_cache(cls, self.cursor.next(), dirty=False) raise StopIteration()
def from_ids(cls, ids): if len(ids) == 0: return [] if not isinstance(ids[0], ObjectId): ids = [ObjectId(x) for x in ids] to_query = [] to_return = [] for _id in ids: if _id in whiskeycache.RAM: to_return.append(whiskeycache.RAM[_id]) else: to_query.append(_id) if len(to_query) > 0: cursor = cls.COLLECTION.find({"_id": {"$in": to_query}}) to_return.extend([whiskeycache.from_cache(cls, data, dirty=False) for data in cursor]) return to_return
def from_ids(cls, ids): if len(ids) == 0: return [] if not isinstance(ids[0], ObjectId): ids = [ObjectId(x) for x in ids] to_query = [] to_return = [] for _id in ids: if _id in whiskeycache.RAM: to_return.append(whiskeycache.RAM[_id]) else: to_query.append(_id) if len(to_query) > 0: cursor = cls.COLLECTION.find({'_id':{'$in':to_query}}) to_return.extend([whiskeycache.from_cache(cls, data, dirty=False) for data in cursor]) return to_return
def from_dict(cls, data, dirty=False): if data is None: return None return whiskeycache.from_cache(cls, data, dirty)
def from_dict(cls, data, dirty=False): if data is None: return None return whiskeycache.from_cache(cls, data, dirty)