def __init__(self, doc=''): self.user_key = doc + USERS self.users = hot_redis.Dict(key=self.user_key, client=client) self.id = hot_redis.Int(key=self.user_key + "ID", client=client) self.changes = hot_redis.Dict(key=self.user_key + "changes", client=client) self.user_changes = hot_redis.Dict(key=self.user_key + "userChangesMap", client=client)
def merge(self, client_a, client_b): # Getting Client A and Client B's state client_a_state = hot_redis.Dict(key=client_a, client=connection) client_b_state = hot_redis.Dict(key=client_b, client=connection) # Merging Client A's state with Client B's state and storing in Client A's State for client in self.client_list: client_a_state[client] = repr( eval(client_a_state[client]).union(eval( client_b_state[client])))
def merge(self, client_a, client_b): # Getting Client A and Client B's state client_a_state = hot_redis.Dict(key=client_a, client=connection) client_b_state = hot_redis.Dict(key=client_b, client=connection) # Merging Client A's state with Client B's state and storing in Client A's State print self.client_list for client in self.client_list: print client print client_a_state client_a_state[client] = max(int(client_a_state[client]), int(client_b_state[client]))
def merge(self, client_a, client_b): # Getting Client A and Client B's state client_a_state = hot_redis.Dict(key=client_a, client=connection) client_b_state = hot_redis.Dict(key=client_b, client=connection) # Merging Client A's state with Client B's state and storing in Client A's State for client in self.client_list: client_a_value = eval(client_a_state[client]) client_b_value = eval(client_b_state[client]) if client_a_value['timestamp'] > client_b_value['timestamp']: client_a_state[client] = str({'value': client_a_value['value'], 'timestamp': client_a_value['timestamp']}) else: client_a_state[client] = str({'value': client_b_value['value'], 'timestamp': client_b_value['timestamp']})
def add_client(self, client_id): # Generating a client list key for this key new_client = get_client_key(self.key, client_id) with hot_redis.transaction(): # Adding a new state to all the existing clients for client in self.client_list: hot_redis.Dict(key=client, client=connection)[new_client] = 0 # Adding client to GCounter instance's client list self.client_list.add(new_client) # Adding a new state dictionary for this GCounter client new_client_state = hot_redis.Dict(key=new_client, client=connection) for client in self.client_list: new_client_state[client] = 0
def set(self, client_id, val): # Generating the current client's key for GCounter current_client_key = get_client_key(self.key, client_id) # Updating the client's state with the value to be set hot_redis.Dict(key=current_client_key, client=connection)[current_client_key] = val
def new_g_counter(): key = request.args.get('key') client_id = request.args.get('client_id') result_dict = dict() # Getting all clients and data types of all CRDTs all_clients = hot_redis.Set(key=ALL_CLIENTS, client=connection) data_types = hot_redis.Dict(key=DATA_TYPES, client=connection) # Checking if the client ID is valid if client_id not in all_clients: print 'Missing Client ID' result_dict['status'] = status_codes['client_id_not_found'] return jsonify(result_dict) # Checking if an empty or null key has been given, and generating key if key is None or len(key) is 0: key = generate_random_crdt_key() while key in data_types.keys(): key = generate_random_crdt_key() print 'Generated new random CRDT key' # Checking if the key has already been taken elif key in data_types.keys() and data_types[key] != G_COUNTER: result_dict['status'] = status_codes['data_type_mismatch'] return jsonify(result_dict) # All conditions met for key and client ID new_g_counter = GCounter(key=key) new_g_counter.add_client(client_id) result_dict['status'] = status_codes['success'] result_dict['key'] = key result_dict['data_type'] = data_types[key] result_dict['client_id'] = client_id return jsonify(result_dict)
def __init__(self, key): # Setting the key of the LWWERegister Instance self.key = key # Getting/Setting the client list and type of the LWWERegister instance self.client_list = hot_redis.Set(key=get_client_list_key(key), client=connection) hot_redis.Dict(key=DATA_TYPES, client=connection)[key] = LWW_REGISTER
def add_client(self, client_id): # Generating a client list key for this key new_client = get_client_key(self.key, client_id) with hot_redis.transaction(): # Adding a new state to all the existing clients for client in self.client_list: hot_redis.Dict(key=client, client=connection)[new_client] = str({'timestamp': -1, 'value': None}) # Adding client to LWWERegister instance's client list self.client_list.add(new_client) # Adding a new state dictionary for this LWWERegister client new_client_state = hot_redis.Dict(key=new_client, client=connection) for client in self.client_list: new_client_state[client] = str({'timestamp': -1, 'value': None})
def test_update(self): a = {"wagwaan": "popcaan", "flute": "don"} b = {"wagwaan": "hotskull", "nba": "hangtime"} c = hot_redis.Dict(a) a.update(b) c.update(b) self.assertEquals(a, c)
def __init__(self, var, val): if type(val) == dict: # print('dict') instance = hot_redis.Dict(val, key=var) elif type(val) == list: # print('list') instance = hot_redis.List(val, key=var) elif type(val) == set: # print('set') instance = hot_redis.Set(val, key=var) elif type(val) == str: # print('str') instance = hot_redis.String(val, key=var) elif type(val) == int: # print('int') instance = hot_redis.Int(val, key=var) elif type(val) == None: # Not handled # print('None Not handled') return None elif callable(val): # print('function') return None else: # print("else, None:", type(val)) return None # instance = hot_redis(instance) # print("debug: ", var, val) self.instance = instance
def set(self, client_id, val, timestamp): # Generating the current client's key for LWWERegister current_client_key = get_client_key(self.key, client_id) # Updating the client's state with the value to be set hot_redis.Dict(key=current_client_key, client=connection)[current_client_key] = str({'value': val, 'timestamp': timestamp})
def __init__(self, key): # Setting the key of the GSet Instance self.key = key # Getting/Setting the client list and type of the GSet instance self.client_list = hot_redis.Set(key=get_client_list_key(key), client=connection) hot_redis.Dict(key=DATA_TYPES, client=connection)[key] = G_SET
def peek(self, client_id): # Generating the current client's key for GCounter current_client_key = get_client_key(self.key, client_id) # Peek at the current value return int( hot_redis.Dict(key=current_client_key, client=connection)[current_client_key])
def test_get(self): a = {"wagwaan": "popcaan", "flute": "don"} b = hot_redis.Dict(a) self.assertEquals(a["wagwaan"], b["wagwaan"]) self.assertEquals(a.get("wagwaan"), b.get("wagwaan")) self.assertRaises(KeyError, lambda: b["hotskull"]) self.assertEquals(a.get("hotskull"), b.get("hotskull")) self.assertEquals(a.get("hotskull", "don"), b.get("hotskull", "don")) self.assertNotEquals(a.get("hotskull", "don"), b.get("hotskull", "x"))
def __init__(self, key): # Setting the key of the PN Counter Instance self.key = key self.pcounter = GCounter(get_pcounter_key(key)) self.ncounter = GCounter(get_ncounter_key(key)) # Getting/Setting the client list and type of the GCounter instance self.client_list = hot_redis.Set(key=get_client_list_key(key), client=connection) hot_redis.Dict(key=DATA_TYPES, client=connection)[key] = PN_COUNTER
def __init__(self, key): # Setting the key of the PN Counter Instance self.key = key self.add_set = GSet(get_add_set_key(key)) self.delete_set = GSet(get_delete_set_key(key)) # Getting/Setting the client list and type of the GCounter instance self.client_list = hot_redis.Set(key=get_client_list_key(key), client=connection) hot_redis.Dict(key=DATA_TYPES, client=connection)[key] = TWO_P_SET
def test_del(self): a = hot_redis.Dict({"wagwaan": "popcaan", "flute": "don"}) del a["wagwaan"] self.assertRaises(KeyError, lambda: a["wagwaan"]) def del_missing(): del a["hotskull"] self.assertRaises(KeyError, del_missing)
def __init__(self, key): # Setting the key of the TwoPTwoPGraph Instance self.key = key self.nodes = TwoPSet(get_nodes_set_key(key)) self.edges = TwoPSet(get_edges_set_key(key)) # Getting/Setting the client list and type of the TwoPTwoPGraph instance self.client_list = hot_redis.Set(key=get_client_list_key(key), client=connection) hot_redis.Dict(key=DATA_TYPES, client=connection)[key] = TWO_P_TWO_P_GRAPH
def __init__(self, user='', doc=''): self.key = md5(str(MAIN_PPS) + doc + PPS_STRUCT).hexdigest() self.key_pos_tags = md5(str(MAIN_PPS) + doc + POS_TAGS).hexdigest() self.key_map_persistent = md5(str(MAIN_PPS) + doc + PERSIST).hexdigest() self.pps = hot_redis.Dict(key=self.key, client=client) self.tags = hot_redis.List(key=self.key_pos_tags, client=client) self.persist = hot_redis.Dict(key=self.key_map_persistent, client=client) self.user_list = UserOrder(doc=doc) self.user = user if hot_redis.List(key=KEYS, client=client).count(self.key) == 0: self.pps['0'] = PHI # unicode for null self.pps['1'] = PHI # unicode for null self.persist['0'] = YES self.persist['1'] = YES self.tags.append('0') self.tags.append('1') hot_redis.List(key=KEYS, client=client).append(self.key)
def test_setdefault(self): a = {"wagwaan": "popcaan", "flute": "don"} b = hot_redis.Dict(a) c = "nba" d = "hangtime" e = b.setdefault(c, d) self.assertEquals(e, d) self.assertEquals(b[c], d) self.assertEquals(a.setdefault(c, d), e) e = b.setdefault(c, c) self.assertEquals(e, d) self.assertEquals(a.setdefault(c, c), e)
def test_pop(self): a = hot_redis.Dict({"wagwaan": "popcaan", "flute": "don"}) v = a.pop("wagwaan") self.assertEqual(v, "popcaan") self.assertRaises(KeyError, lambda: a["wagwaan"]) def pop_missing(): a.pop("hotskull") self.assertRaises(KeyError, pop_missing) marker = object() self.assertEqual(a.pop("hotskull", marker), marker)
def check_input_fault(key, client_id, value1, value2, data_type): result_dict = dict() # Checking for valid key if key is None or len(key) == 0: result_dict['status'] = status_codes['missing_key_or_state'] return result_dict # Checking for valid client ID if client_id is None or len(client_id) == 0: result_dict['status'] = status_codes['client_id_not_found'] return result_dict # Checking for valid value1 if (value1 is None or len(value1) == 0) and value1 != "default": result_dict['status'] = status_codes['value_not_found'] return result_dict # Checking for valid value2 if (value2 is None or len(value2) == 0) and value2 != "default": result_dict['status'] = status_codes['value_not_found'] return result_dict data_types = hot_redis.Dict(key=DATA_TYPES, client=connection) all_clients = hot_redis.Set(key=ALL_CLIENTS, client=connection) client_list = hot_redis.Set(key=get_client_list_key(key), client=connection) # Checking if client ID is valid if client_id not in all_clients: result_dict['status'] = status_codes['client_id_not_found'] return result_dict # Checking if key is present if key not in data_types.keys(): result_dict['status'] = status_codes['missing_key_or_state'] return result_dict # Checking if the key is the right type if data_types[key] != data_type: result_dict['status'] = status_codes['data_type_mismatch'] return result_dict # Checking if client is in the GCounter's client list if get_client_key(key=key, client_id=client_id) not in client_list: result_dict['status'] = status_codes['client_id_not_in_crdt'] return result_dict return False
def repository(namespace, name, branch='master'): '''Returns a repository''' with TemporaryDirectory() as download_path: old_directory = str(pwd()).strip() try: git.clone('https://github.com/{0}/{1}.git'.format(namespace, name), download_path) cd(download_path) git.fetch('origin', branch) git.checkout(branch) yield (download_path, git('rev-parse', 'HEAD'), redis.Dict(key="{0}.{1}".format(namespace, name))) except ErrorReturnCode_128: mkdir(download_path) yield (None, None, None) cd(old_directory)
def check_input_fault(key, client_id, timestamp, data_type): result_dict = dict() # Checking for valid key if key is None or len(key) == 0: result_dict['status'] = status_codes['missing_key_or_state'] return result_dict # Checking for valid client ID if client_id is None or len(client_id) == 0: result_dict['status'] = status_codes['client_id_not_found'] return result_dict # Checking validity of timestamp if timestamp != -1.0: try: timestamp = float(timestamp) except ValueError: result_dict['status'] = status_codes['timestamp_not_valid'] return result_dict data_types = hot_redis.Dict(key=DATA_TYPES, client=connection) all_clients = hot_redis.Set(key=ALL_CLIENTS, client=connection) client_list = hot_redis.Set(key=get_client_list_key(key), client=connection) # Checking if client ID is valid if client_id not in all_clients: result_dict['status'] = status_codes['client_id_not_found'] return result_dict # Checking if key is present if key not in data_types.keys(): result_dict['status'] = status_codes['missing_key_or_state'] return result_dict # Checking if the key is the right type if data_types[key] != data_type: result_dict['status'] = status_codes['data_type_mismatch'] return result_dict # Checking if client is in the GCounter's client list if get_client_key(key=key, client_id=client_id) not in client_list: result_dict['status'] = status_codes['client_id_not_in_crdt'] return result_dict return False
def get(self, client_id=None): # Getting the client's key for this GCounter if client_id is None: current_client_key = random.choice(list(self.client_list.value)) else: current_client_key = get_client_key(self.key, client_id) # Merging state from every other client for this GCounter for client in self.client_list: self.merge(current_client_key, client) # Updating the states from all other clients as the client's latest state current_client_state = hot_redis.Dict(key=current_client_key, client=connection) # Getting the final merged value count = 0 for val in current_client_state.values(): count += int(val) return count
def get(self, client_id): # Getting the client's key for this GSet current_client_key = get_client_key(self.key, client_id) # Merging state from every other client for this GSet for client in self.client_list: self.merge(current_client_key, client) # Updating the states from all other clients as the client's latest state current_client_state = hot_redis.Dict(key=current_client_key, client=connection) # Getting the final merged value count = set() for val in current_client_state.values(): count = count.union(eval(val)) current_client_state[current_client_key] = repr(count) print repr(count) print current_client_state[current_client_key] return str(list(count))
def get(self, client_id): # Getting the client's key for this LWWERegister current_client_key = get_client_key(self.key, client_id) # Merging state from every other client for this LWWERegister for client in self.client_list: self.merge(current_client_key, client) # Updating the states from all other clients as the client's latest state current_client_state = hot_redis.Dict(key=current_client_key, client=connection) register = dict() register['value'] = eval(current_client_state[current_client_key])['value'] register['timestamp'] = eval(current_client_state[current_client_key])['timestamp'] for value in current_client_state.values(): value = eval(value) if value['timestamp'] > register['timestamp']: register['value'] = value['value'] register['timestamp'] = value['timestamp'] current_client_state[current_client_key] = str(register) return str(register)
def hot_redis_test(clear=False): """ 测试 hot redis 常用功能 :return: """ # clear = True dat = { 'base': 10, 'crx': 2, 'jobbole': 1, } # 初始化一个字典 _dict = hot_redis.Dict(client=rdb_out, key='ig.dict') # 更新, 自动写入 redis _dict['fns'] = dat # 删除字典 if clear: _dict.clear() # 测试 list _list = hot_redis.List(client=rdb_out, key='ig.list') _list += list(dat.keys()) # pop list if clear: for i in range(len(_list)): _list.pop() # 字符串 _string = hot_redis.String(client=rdb_out, key='ig.string', initial=','.join([str(_) for _ in dat.keys()])) # 没有找到 hot_redis 如何删除, 只好使用 redis 默认的 if clear: rd.delete('ig.string')
def test_clear(self): a = hot_redis.Dict({"wagwaan": "popcaan", "flute": "don"}) a.clear() self.assertEquals(len(a), 0)