def list_insert_after(self, key, value, value_to_insert, limit=0): try: new_value = utils.to36(value) new_value_to_insert = utils.to36(value_to_insert) ret = self.client.linsert(key, 'after', new_value, new_value_to_insert) # deal pipeline if self.batch_size: self.queued_size+=1 if self.queued_size>self.batch_size: self.client.execute() self.queued_size=0 return ret except: return False
def list_insert_after(self, key, value, value_to_insert, limit=0): try: new_value = utils.to36(value) new_value_to_insert = utils.to36(value_to_insert) ret = self.client.linsert(key, 'after', new_value, new_value_to_insert) # deal pipeline if self.batch_size: self.queued_size += 1 if self.queued_size > self.batch_size: self.client.execute() self.queued_size = 0 return ret except: return False
def slist_add(self, key, value, score, limit=0, order_by=None): if not isinstance(score, (int, long, float)): return False value_36 = utils.to36(value) ret = self.client.zadd(key, value_36, score) # remove elements that exceeds the limit if limit>0: overflow = self.slist_len(key) - limit if overflow>0: if order_by==ORDER_ASC: self.client.zremrangebyrank(key, -1*overflow, -1) else: self.client.zremrangebyrank(key, 0, overflow) #endif #endif #endif #deal pipeline if self.batch_size: self.queued_size+=1 if self.queued_size>self.batch_size: self.client.execute() self.queued_size=0 return ret
def slist_add(self, key, value, score, limit=0, order_by=None): if not isinstance(score, (int, long, float)): return False value_36 = utils.to36(value) ret = self.client.zadd(key, value_36, score) # remove elements that exceeds the limit if limit > 0: overflow = self.slist_len(key) - limit if overflow > 0: if order_by == ORDER_ASC: self.client.zremrangebyrank(key, -1 * overflow, -1) else: self.client.zremrangebyrank(key, 0, overflow) #endif #endif #endif #deal pipeline if self.batch_size: self.queued_size += 1 if self.queued_size > self.batch_size: self.client.execute() self.queued_size = 0 return ret
def slist_merge_range(self, keys, order_by=ORDER_ASC, count=25, start_index=-1, start_value=None): fn_score_dict = {} sub_count = 1000/len(keys) if sub_count<500: sub_count=50 fn_score_list = None for key in keys: if order_by==ORDER_ASC: fn_score_list = self.client.zrange(key, 0, sub_count-1, withscores=True) else: fn_score_list = self.client.zrevrange(key, 0, sub_count-1, withscores=True) #endif if fn_score_list is None or len(fn_score_list)<=0: continue #print fn_score_list for fn_score in fn_score_list: fn, score = fn_score fn_score_dict[fn] = fn_score #endfor #endfor fn_score_list = [fn_score for fn_score in fn_score_dict.itervalues()] reverse = False if order_by==ORDER_ASC else True fn_score_list.sort(key=lambda x:x[1], reverse=reverse) fn_list = [x[0] for x in fn_score_list] result_list = [] start_value_36 = utils.to36(start_value) if start_index>=0: if start_value is None: result_list = fn_list[start_index:start_index+count] else: try: idx = fn_list.index(start_value_36) except: idx = start_index result_list = fn_list[idx:idx+count] #endif else: if start_value is None: result_list = fn_list[:count] else: try: idx = fn_list.index(start_value_36) except: return [] result_list = fn_list[idx:idx+count] #endif #endif new_list = [int(x,36) for x in result_list] return new_list
def list_del_val(self, key, value): try: new_value = utils.to36(value) ret = self.client.lrem(key, new_value, 0) # deal pipeline if self.batch_size: self.queued_size += 1 if self.queued_size > self.batch_size: self.client.execute() self.queued_size = 0 return ret except: return False
def list_del_val(self, key, value): try: new_value = utils.to36(value) ret = self.client.lrem(key, new_value, 0) # deal pipeline if self.batch_size: self.queued_size+=1 if self.queued_size>self.batch_size: self.client.execute() self.queued_size=0 return ret except: return False
def list_rpush(self, key, value, limit=0): new_value = utils.to36(value) ret = self.client.rpush(key, new_value) # remove elements that exceeds the limit if limit > 0 and self.list_len(key) - limit > 0: self.client.ltrim(key, -1 * limit, -1) #endif # deal pipeline if self.batch_size: self.queued_size += 1 if self.queued_size > self.batch_size: self.client.execute() self.queued_size = 0 return ret
def list_rpush(self, key, value, limit=0): new_value = utils.to36(value) ret = self.client.rpush(key, new_value) # remove elements that exceeds the limit if limit>0 and self.list_len(key)-limit>0: self.client.ltrim(key, -1*limit, -1) #endif # deal pipeline if self.batch_size: self.queued_size+=1 if self.queued_size>self.batch_size: self.client.execute() self.queued_size=0 return ret
def list_set(self, key, index, value): index = utils.force_int(index, None) if index is None: return False try: new_value = utils.to36(value) ret = self.client.lset(key, index, new_value) # deal pipeline if self.batch_size: self.queued_size += 1 if self.queued_size > self.batch_size: self.client.execute() self.queued_size = 0 return ret except: return False
def list_set(self, key, index, value): index = utils.force_int(index, None) if index is None: return False try: new_value = utils.to36(value) ret = self.client.lset(key, index, new_value) # deal pipeline if self.batch_size: self.queued_size+=1 if self.queued_size>self.batch_size: self.client.execute() self.queued_size=0 return ret except: return False
def list_rpush_multi(self, key, values, limit=0): if limit > 0 and len(values) > limit: values = values[:limit] #endif new_values = [utils.to36(x) for x in values] ret = self.client.rpush(key, *new_values) # remove elements that exceeds the limit if limit > 0 and self.list_len(key) - limit > 0: self.client.ltrim(key, -1 * limit, -1) #endif # deal pipeline if self.batch_size: self.queued_size += 1 if self.queued_size > self.batch_size: self.client.execute() self.queued_size = 0 return ret
def list_rpush_multi(self, key, values, limit=0): if limit>0 and len(values)>limit: values = values[:limit] #endif new_values = [utils.to36(x) for x in values] ret = self.client.rpush(key, *new_values) # remove elements that exceeds the limit if limit>0 and self.list_len(key)-limit>0: self.client.ltrim(key, -1*limit, -1) #endif # deal pipeline if self.batch_size: self.queued_size+=1 if self.queued_size>self.batch_size: self.client.execute() self.queued_size=0 return ret
def list_insert(self, key, index, value_to_insert, limit=0): index = utils.force_int(index, 0) value_36 = self.client.lindex(key, index) if value_36: try: new_value_36_to_insert = utils.to36(value_to_insert) ret = self.client.linsert(key, 'before', value_36, new_value_36_to_insert) # deal pipeline if self.batch_size: self.queued_size+=1 if self.queued_size>self.batch_size: self.client.execute() self.queued_size=0 return ret except: return False else: return False
def list_insert(self, key, index, value_to_insert, limit=0): index = utils.force_int(index, 0) value_36 = self.client.lindex(key, index) if value_36: try: new_value_36_to_insert = utils.to36(value_to_insert) ret = self.client.linsert(key, 'before', value_36, new_value_36_to_insert) # deal pipeline if self.batch_size: self.queued_size += 1 if self.queued_size > self.batch_size: self.client.execute() self.queued_size = 0 return ret except: return False else: return False
def slist_merge_range(self, keys, order_by=ORDER_ASC, count=25, start_index=-1, start_value=None): fn_score_dict = {} sub_count = 1000 / len(keys) if sub_count < 500: sub_count = 50 fn_score_list = None for key in keys: if order_by == ORDER_ASC: fn_score_list = self.client.zrange(key, 0, sub_count - 1, withscores=True) else: fn_score_list = self.client.zrevrange(key, 0, sub_count - 1, withscores=True) #endif if fn_score_list is None or len(fn_score_list) <= 0: continue #print fn_score_list for fn_score in fn_score_list: fn, score = fn_score fn_score_dict[fn] = fn_score #endfor #endfor fn_score_list = [fn_score for fn_score in fn_score_dict.itervalues()] reverse = False if order_by == ORDER_ASC else True fn_score_list.sort(key=lambda x: x[1], reverse=reverse) fn_list = [x[0] for x in fn_score_list] result_list = [] start_value_36 = utils.to36(start_value) if start_index >= 0: if start_value is None: result_list = fn_list[start_index:start_index + count] else: try: idx = fn_list.index(start_value_36) except: idx = start_index result_list = fn_list[idx:idx + count] #endif else: if start_value is None: result_list = fn_list[:count] else: try: idx = fn_list.index(start_value_36) except: return [] result_list = fn_list[idx:idx + count] #endif #endif new_list = [int(x, 36) for x in result_list] return new_list
def list_range(self, key, count=25, start_index=-1, start_value=None): if not isinstance(key, (str, unicode, list)): return [] count = utils.force_int(count, 25) if count<=0: return [] start_index = utils.force_int(start_index, -1) if isinstance(key, list): key_count = len(key) if key_count<=0: return [] elif key_count>1: return self.list_merge_range(key, count, start_index, start_value) else: key = key[0] #endif #endif fn_list = None if start_index>=0: if start_value is None: fn_list = self.client.lrange(key, start_index, start_index+count-1) elif start_value is not None: offset = 10 offset_start_index = start_index-offset if start_index-offset>0 else 0 start_candidate_list = self.client.lrange(key, offset_start_index, offset_start_index + offset + count + offset) if start_candidate_list and len(start_candidate_list)>0: i = None try: start_value_36 = utils.to36(start_value) i = start_candidate_list.index(start_value_36) except: i = None #endtry if i is not None: if i+count<=len(start_candidate_list): fn_list = start_candidate_list[i:i+count] else: new_start_index = offset_start_index + i fn_list = self.client.lrange(key, new_start_index, new_start_index + count-1) #endif else: fn_list = self.client.lrange(key, start_index, start_index + count-1) #endif #endif #endif elif start_index<0: if start_value is None: fn_list = self.client.lrange(key, 0, count-1) elif start_value is not None: #/////////////////////////////////////////////////////////////////////////////////////// # Find from start to end new_start_index = None i = 0 step_count = 500 start_value_36 = utils.to36(start_value) while True: start_candidate_list = self.client.lrange(key, i, i + step_count-1) if start_candidate_list and len(start_candidate_list)>0: try: new_start_index = start_candidate_list.index(start_value_36) new_start_index = i + new_start_index except: new_start_index = None if new_start_index is not None: break #endif else: break #endif i = i + step_count #endwhile if new_start_index is not None: fn_list = self.client.lrange(key, new_start_index, new_start_index + count-1) #endif #/////////////////////////////////////////////////////////////////////////////////////// #/////////////////////////////////////////////////////////////////////////////////////// # Smart Find ''' new_start_index = None i = 0 step_count = count/2 + count%2 if step_count>8: step_count = 8 #endif start_value_36 = utils.to36(start_value) while True: i = i-step_count if i-step_count>0 else 0 start_candidate_list = self.client.lrange(key, i, i + step_count*2 - 1) if start_candidate_list and len(start_candidate_list)>0: try: new_start_index = start_candidate_list.index(start_value_36) new_start_index = i + new_start_index except: new_start_index = None if new_start_index is not None: break #endif else: break #endif i = i + count #endwhile if new_start_index is not None: fn_list = self.client.lrange(key, new_start_index, new_start_index + count-1) #endif ''' #/////////////////////////////////////////////////////////////////////////////////////// #endif #endif if fn_list is not None: new_list = [int(x,36) for x in fn_list] #print "\n", "*"*100 #print "start_index:", start_index, "start_value:", start_value, "count:", count #print new_list #print "$"*100 return new_list else: return []
def slist_range(self, key, order_by=ORDER_ASC, count=25, start_index=-1, start_value=None): if not isinstance(key, (str, unicode, list)): return [] count = utils.force_int(count, 25) if count <= 0: return [] start_index = utils.force_int(start_index, -1) if isinstance(key, list): key_count = len(key) if key_count <= 0: return [] elif key_count > 1: return self.slist_merge_range(key, order_by, count, start_index, start_value) else: key = key[0] #endif #endif zrange_func = None if order_by == ORDER_ASC: zrange_func = self.client.zrange else: zrange_func = self.client.zrevrange #endif fn_list = None if start_index >= 0: if start_value is None: fn_list = zrange_func(key, start_index, start_index + count - 1) elif start_value is not None: offset = 10 offset_start_index = start_index - offset if start_index - offset > 0 else 0 start_candidate_list = zrange_func( key, offset_start_index, offset_start_index + offset + count + offset) #endif if start_candidate_list and len(start_candidate_list) > 0: i = None try: start_value_36 = utils.to36(start_value) i = start_candidate_list.index(start_value_36) except: i = None #endtry if i is not None: if i + count <= len(start_candidate_list): fn_list = start_candidate_list[i:i + count] else: new_start_index = offset_start_index + i fn_list = zrange_func(key, new_start_index, new_start_index + count - 1) #endif else: fn_list = zrange_func(key, start_index, start_index + count - 1) #endif #endif #endif elif start_index < 0: if start_value is None: fn_list = zrange_func(key, 0, count - 1) elif start_value is not None: #/////////////////////////////////////////////////////////////////////////////////////// # Find from start to end fn_list = None new_start_index = None i = 0 step_count = 500 start_value_36 = utils.to36(start_value) while True: start_candidate_list = zrange_func(key, i, i + step_count - 1) if isinstance(start_candidate_list, list): list_len = len(start_candidate_list) if list_len > 0: try: idx = start_candidate_list.index( start_value_36) if count <= list_len - idx: fn_list = start_candidate_list[idx:idx + count] break #endif new_start_index = i + idx except: new_start_index = None if new_start_index is not None: break #endif else: #empty list break #endif else: #not a list break #endif i = i + step_count #endwhile if fn_list is None and new_start_index is not None: fn_list = zrange_func(key, new_start_index, new_start_index + count - 1) #endif #/////////////////////////////////////////////////////////////////////////////////////// #endif #endif if fn_list is not None: new_list = [int(x, 36) for x in fn_list] #print "\n", "*"*100 #print "start_index:", start_index, "start_value:", start_value, "count:", count #print new_list #print "$"*100 return new_list else: return []
def list_range(self, key, count=25, start_index=-1, start_value=None): if not isinstance(key, (str, unicode, list)): return [] count = utils.force_int(count, 25) if count <= 0: return [] start_index = utils.force_int(start_index, -1) if isinstance(key, list): key_count = len(key) if key_count <= 0: return [] elif key_count > 1: return self.list_merge_range(key, count, start_index, start_value) else: key = key[0] #endif #endif fn_list = None if start_index >= 0: if start_value is None: fn_list = self.client.lrange(key, start_index, start_index + count - 1) elif start_value is not None: offset = 10 offset_start_index = start_index - offset if start_index - offset > 0 else 0 start_candidate_list = self.client.lrange( key, offset_start_index, offset_start_index + offset + count + offset) if start_candidate_list and len(start_candidate_list) > 0: i = None try: start_value_36 = utils.to36(start_value) i = start_candidate_list.index(start_value_36) except: i = None #endtry if i is not None: if i + count <= len(start_candidate_list): fn_list = start_candidate_list[i:i + count] else: new_start_index = offset_start_index + i fn_list = self.client.lrange( key, new_start_index, new_start_index + count - 1) #endif else: fn_list = self.client.lrange(key, start_index, start_index + count - 1) #endif #endif #endif elif start_index < 0: if start_value is None: fn_list = self.client.lrange(key, 0, count - 1) elif start_value is not None: #/////////////////////////////////////////////////////////////////////////////////////// # Find from start to end new_start_index = None i = 0 step_count = 500 start_value_36 = utils.to36(start_value) while True: start_candidate_list = self.client.lrange( key, i, i + step_count - 1) if start_candidate_list and len(start_candidate_list) > 0: try: new_start_index = start_candidate_list.index( start_value_36) new_start_index = i + new_start_index except: new_start_index = None if new_start_index is not None: break #endif else: break #endif i = i + step_count #endwhile if new_start_index is not None: fn_list = self.client.lrange(key, new_start_index, new_start_index + count - 1) #endif #/////////////////////////////////////////////////////////////////////////////////////// #/////////////////////////////////////////////////////////////////////////////////////// # Smart Find ''' new_start_index = None i = 0 step_count = count/2 + count%2 if step_count>8: step_count = 8 #endif start_value_36 = utils.to36(start_value) while True: i = i-step_count if i-step_count>0 else 0 start_candidate_list = self.client.lrange(key, i, i + step_count*2 - 1) if start_candidate_list and len(start_candidate_list)>0: try: new_start_index = start_candidate_list.index(start_value_36) new_start_index = i + new_start_index except: new_start_index = None if new_start_index is not None: break #endif else: break #endif i = i + count #endwhile if new_start_index is not None: fn_list = self.client.lrange(key, new_start_index, new_start_index + count-1) #endif ''' #/////////////////////////////////////////////////////////////////////////////////////// #endif #endif if fn_list is not None: new_list = [int(x, 36) for x in fn_list] #print "\n", "*"*100 #print "start_index:", start_index, "start_value:", start_value, "count:", count #print new_list #print "$"*100 return new_list else: return []
def slist_range(self, key, order_by=ORDER_ASC, count=25, start_index=-1, start_value=None): if not isinstance(key, (str, unicode, list)): return [] count = utils.force_int(count, 25) if count<=0: return [] start_index = utils.force_int(start_index, -1) if isinstance(key, list): key_count = len(key) if key_count<=0: return [] elif key_count>1: return self.slist_merge_range(key, order_by, count, start_index, start_value) else: key = key[0] #endif #endif zrange_func = None if order_by==ORDER_ASC: zrange_func = self.client.zrange else: zrange_func = self.client.zrevrange #endif fn_list = None if start_index>=0: if start_value is None: fn_list = zrange_func(key, start_index, start_index+count-1) elif start_value is not None: offset = 10 offset_start_index = start_index-offset if start_index-offset>0 else 0 start_candidate_list = zrange_func(key, offset_start_index, offset_start_index + offset + count + offset) #endif if start_candidate_list and len(start_candidate_list)>0: i = None try: start_value_36 = utils.to36(start_value) i = start_candidate_list.index(start_value_36) except: i = None #endtry if i is not None: if i+count<=len(start_candidate_list): fn_list = start_candidate_list[i:i+count] else: new_start_index = offset_start_index + i fn_list = zrange_func(key, new_start_index, new_start_index + count-1) #endif else: fn_list = zrange_func(key, start_index, start_index + count-1) #endif #endif #endif elif start_index<0: if start_value is None: fn_list = zrange_func(key, 0, count-1) elif start_value is not None: #/////////////////////////////////////////////////////////////////////////////////////// # Find from start to end fn_list = None new_start_index = None i = 0 step_count = 500 start_value_36 = utils.to36(start_value) while True: start_candidate_list = zrange_func(key, i, i + step_count-1) if isinstance(start_candidate_list, list): list_len = len(start_candidate_list) if list_len>0: try: idx = start_candidate_list.index(start_value_36) if count <= list_len-idx: fn_list = start_candidate_list[idx:idx+count] break #endif new_start_index = i + idx except: new_start_index = None if new_start_index is not None: break #endif else: #empty list break #endif else: #not a list break #endif i = i + step_count #endwhile if fn_list is None and new_start_index is not None: fn_list = zrange_func(key, new_start_index, new_start_index + count-1) #endif #/////////////////////////////////////////////////////////////////////////////////////// #endif #endif if fn_list is not None: new_list = [int(x,36) for x in fn_list] #print "\n", "*"*100 #print "start_index:", start_index, "start_value:", start_value, "count:", count #print new_list #print "$"*100 return new_list else: return []