def remove_block(self, block): r = get_redis_instance() block_key = ExpKeys.get_block_key(block.uuid) pipe = r.pipeline(transaction=True) if block.create_new_scope: for sub_block_uuid in block.children_blocks: sub_block = self.get_block(sub_block_uuid) self.remove_block(sub_block) pipe.hdel(ExpKeys.get_scope_creating_block_uuid_keys(self.pk), block.sub_scope_name) block.on_remove(exp=self) # find all bound variables, that provided by this block for other_uuid, other_block in self.get_blocks(self.get_all_block_uuids()): if other_uuid == block.uuid: continue for f_name, bound_var in other_block.bound_inputs.items(): if bound_var.block_uuid == block.uuid: other_block.bound_inputs.pop(f_name) self.store_block(other_block) # Remove information related to block from redis pipe.lrem(ExpKeys.get_exp_blocks_list_key(self.pk), 0, block.uuid) pipe.srem(ExpKeys.get_all_exp_keys_key(self.pk), block_key) pipe.hdel(ExpKeys.get_blocks_uuid_by_alias(self.pk), block.base_name) scope = Scope(self, block.scope_name) scope.remove_vars_from_block(block) pipe.execute()
def remove_block(self, block): r = get_redis_instance() block_key = ExpKeys.get_block_key(block.uuid) pipe = r.pipeline(transaction=True) if block.create_new_scope: for sub_block_uuid in block.children_blocks: sub_block = self.get_block(sub_block_uuid) self.remove_block(sub_block) pipe.hdel(ExpKeys.get_scope_creating_block_uuid_keys(self.pk), block.sub_scope_name) block.on_remove(exp=self) # find all bound variables, that provided by this block for other_uuid, other_block in self.get_blocks( self.get_all_block_uuids()): if other_uuid == block.uuid: continue for f_name, bound_var in other_block.bound_inputs.items(): if bound_var.block_uuid == block.uuid: other_block.bound_inputs.pop(f_name) self.store_block(other_block) # Remove information related to block from redis pipe.lrem(ExpKeys.get_exp_blocks_list_key(self.pk), 0, block.uuid) pipe.srem(ExpKeys.get_all_exp_keys_key(self.pk), block_key) pipe.hdel(ExpKeys.get_blocks_uuid_by_alias(self.pk), block.base_name) scope = Scope(self, block.scope_name) scope.remove_vars_from_block(block) pipe.execute()
def change_block_alias(self, block, new_base_name): r = get_redis_instance() key = ExpKeys.get_blocks_uuid_by_alias(self.pk) pipe = r.pipeline() pipe.hdel(key, block.base_name) pipe.hset(key, new_base_name, block.uuid) pipe.execute() block.base_name = new_base_name self.store_block(block, redis_instance=r)
def change_block_alias(self, block, new_base_name): r = get_redis_instance() key = ExpKeys.get_blocks_uuid_by_alias(self.pk) pipe = r.pipeline() pipe.hdel(key, block.base_name) pipe.hset(key, new_base_name, block.uuid) pipe.execute() block.base_name = new_base_name self.store_block(block, redis_instance=r)
def get_block_aliases_map(self, redis_instance=None): """ @param redis_instance: Redis @return: Map { uuid -> alias } @rtype: dict """ if redis_instance is None: r = get_redis_instance() else: r = redis_instance orig_map = r.hgetall(ExpKeys.get_blocks_uuid_by_alias(self.pk)) return dict([(uuid, alias) for alias, uuid in orig_map.iteritems()])
def store_block(self, block, new_block=False, redis_instance=None, dont_execute_pipe=False): if redis_instance is None: r = get_redis_instance() else: r = redis_instance if not isinstance(r, StrictPipeline): pipe = r.pipeline() else: pipe = r block_key = ExpKeys.get_block_key(block.uuid) if new_block: pipe.rpush(ExpKeys.get_exp_blocks_list_key(self.pk), block.uuid) pipe.sadd(ExpKeys.get_all_exp_keys_key(self.pk), block_key) pipe.hset(ExpKeys.get_blocks_uuid_by_alias(self.pk), block.base_name, block.uuid) if block.create_new_scope: pipe.hset(ExpKeys.get_scope_creating_block_uuid_keys(self.pk), block.sub_scope_name, block.uuid) if block.scope_name != "root": # need to register in parent block parent_uuid = r.hget( ExpKeys.get_scope_creating_block_uuid_keys(self.pk), block.scope_name) parent = self.get_block(parent_uuid, r) # TODO: remove code dependency here parent.children_blocks.append(block.uuid) self.store_block(parent, new_block=False, redis_instance=pipe, dont_execute_pipe=True) pipe.set(block_key, pickle.dumps(block)) if not dont_execute_pipe: pipe.execute() log.info("block %s was stored with state: %s [uuid: %s]", block.base_name, block.state, block.uuid)
def get_block_aliases_map(self, redis_instance=None): """ @param redis_instance: Redis @return: Map { uuid -> alias } @rtype: dict """ if redis_instance is None: r = get_redis_instance() else: r = redis_instance orig_map = r.hgetall(ExpKeys.get_blocks_uuid_by_alias(self.pk)) return dict([ (uuid, alias) for alias, uuid in orig_map.iteritems() ])
def post_init(self, redis_instance=None): ## TODO: RENAME TO init experiment and invoke on first save if redis_instance is None: r = get_redis_instance() else: r = redis_instance pipe = r.pipeline() pipe.hset(ExpKeys.get_scope_creating_block_uuid_keys(self.pk), "root", None) pipe.sadd(ExpKeys.get_all_exp_keys_key(self.pk),[ ExpKeys.get_exp_blocks_list_key(self.pk), ExpKeys.get_blocks_uuid_by_alias(self.pk), ExpKeys.get_scope_creating_block_uuid_keys(self.pk), ExpKeys.get_scope_key(self.pk, "root") ]) pipe.execute()
def post_init(self, redis_instance=None): ## TODO: RENAME TO init experiment and invoke on first save if redis_instance is None: r = get_redis_instance() else: r = redis_instance pipe = r.pipeline() pipe.hset(ExpKeys.get_scope_creating_block_uuid_keys(self.pk), "root", None) pipe.sadd(ExpKeys.get_all_exp_keys_key(self.pk),[ ExpKeys.get_exp_blocks_list_key(self.pk), ExpKeys.get_blocks_uuid_by_alias(self.pk), ExpKeys.get_scope_creating_block_uuid_keys(self.pk), ExpKeys.get_scope_key(self.pk, "root") ]) pipe.execute()
def get_block_by_alias(self, alias, redis_instance=None): """ @type alias: str @param alias: Human readable block name, can be altered @type redis_instance: Redis @param redis_instance: Instance of redis client @rtype: GenericBlock @return: Block instance """ if redis_instance is None: r = get_redis_instance() else: r = redis_instance uuid = r.hget(ExpKeys.get_blocks_uuid_by_alias(self.pk), alias) return self.get_block(uuid, r)
def get_block_by_alias(self, alias, redis_instance=None): """ @type alias: str @param alias: Human readable block name, can be altered @type redis_instance: Redis @param redis_instance: Instance of redis client @rtype: GenericBlock @return: Block instance """ if redis_instance is None: r = get_redis_instance() else: r = redis_instance uuid = r.hget(ExpKeys.get_blocks_uuid_by_alias(self.pk), alias) return self.get_block(uuid, r)
def store_block(self, block, new_block=False, redis_instance=None, dont_execute_pipe=False): if redis_instance is None: r = get_redis_instance() else: r = redis_instance if not isinstance(r, StrictPipeline): pipe = r.pipeline() else: pipe = r block_key = ExpKeys.get_block_key(block.uuid) if new_block: pipe.rpush(ExpKeys.get_exp_blocks_list_key(self.pk), block.uuid) pipe.sadd(ExpKeys.get_all_exp_keys_key(self.pk), block_key) pipe.hset(ExpKeys.get_blocks_uuid_by_alias(self.pk), block.base_name, block.uuid) if block.create_new_scope: pipe.hset(ExpKeys.get_scope_creating_block_uuid_keys(self.pk), block.sub_scope_name, block.uuid) if block.scope_name != "root": # need to register in parent block parent_uuid = r.hget(ExpKeys.get_scope_creating_block_uuid_keys(self.pk), block.scope_name) parent = self.get_block(parent_uuid, r) # TODO: remove code dependency here parent.children_blocks.append(block.uuid) self.store_block(parent, new_block=False, redis_instance=pipe, dont_execute_pipe=True) pipe.set(block_key, pickle.dumps(block)) if not dont_execute_pipe: pipe.execute() log.info("block %s was stored with state: %s [uuid: %s]", block.base_name, block.state, block.uuid)