class QueryWaiter(threading.Thread): def __init__(self, cb, cursor, search_id): self.__cursor = cursor self.__cb = cb self.__waitCondition = threading.Condition() self.__results = [] self.__resultNotifier = ResultNotifier() self.__search_id = search_id super(QueryWaiter, self).__init__() def run(self): self.__resultNotifier.waitForSearchResult(self.__search_id, self.cbOnQueryResults) self.__waitCondition.acquire() while True: if len(self.__results) <= self.__cursor : self.__waitCondition.wait() else: break self.__waitCondition.release() self.__resultNotifier.removeSearchResult(self.__search_id) try: self.__cb(CursorHandler.getPageForSearchResults(self.__results, self.__cursor), self.__cursor) except Exception as e : pass def cbOnQueryResults(self, results): self.__results = results self.__waitCondition.acquire() self.__waitCondition.notify() self.__waitCondition.release()
def __init__(self, cb, cursor, search_id): self.__cursor = cursor self.__cb = cb self.__waitCondition = threading.Condition() self.__results = [] self.__resultNotifier = ResultNotifier() self.__search_id = search_id super(QueryWaiter, self).__init__()
def __init__(self,**kwargs): self.__curr_cursor = kwargs.get('cursor') self.__searchId = kwargs.get('searchid') self.__cb = kwargs.get('callback') #self.__threshold = kwargs.get('threshold') self.__rdal = ReadDAL() self.__wdal = WriteDAL() self.__counter = 0 self.__done = False self.__notificationBus = ResultNotifier()
class CursorHandler(object): @staticmethod def orderByIndexTime(docs): sorted(docs, key=lambda doc:doc.get('indexed_at')) print docs return docs def __init__(self,**kwargs): self.__curr_cursor = kwargs.get('cursor') self.__searchId = kwargs.get('searchid') self.__cb = kwargs.get('callback') #self.__threshold = kwargs.get('threshold') self.__rdal = ReadDAL() self.__wdal = WriteDAL() self.__counter = 0 self.__done = False self.__notificationBus = ResultNotifier() def addRawDocuments(self, docs): #CursorHandler.orderByIndexTime(docs) #Fetch from memcache c = [] for doc in docs: item = json.loads(doc.get("full").encode('utf-8')) type = doc.get("type") c.append({"type" : type, "item" : item}) #store the cached search self.__wdal.cacheSearchResult(self.__searchId, c) if len(c) > self.__curr_cursor : #notify of result self.__notificationBus.notifyConditionWithResults(self.__searchId, c) # def addRawDocument(self, doc): # #Fetch from memcache # c = self.__rdal.getSearchID(self.__searchId) # # item = doc.get("full").encode('utf-8') # type = doc.get("type") # # #add to the cached result # if c == None: # c = [item] # else : # c.append({"type" : type, "item" : item}) # # #store the cached search # self.__wdal.cacheSearchResult(self.__searchId, c) # # #if we're still looking to hit that threshold to return, then keep incrementing the counter # if not self.__done : # self.__counter += 1 # if self.__counter >= self.__threshold : # self.__done = True # self.__cb(c) def isDone(self): return self.__done @staticmethod def getPageForSearchResults(result, cursor): page_size = boobox.config.PAGE_SIZE end = max([(cursor+1)*(page_size-1), len(result)]) return result[cursor:end]