def create_vtgate_cursor(class_, vtgate_conn, tablet_type, is_dml, **cursor_kargs): cursor_method = functools.partial(db_object.create_cursor_from_params, vtgate_conn, tablet_type, False) routing = class_.create_shard_routing(cursor_method, **cursor_kargs) if is_dml: if routing.sharding_key is None or db_object._is_iterable_container(routing.sharding_key): dbexceptions.InternalError( "Writes require unique sharding_key") keyspace_ids = None keyranges = None if routing.sharding_key is not None: keyspace_ids = [] if db_object._is_iterable_container(routing.sharding_key): for sk in routing.sharding_key: kid = class_.sharding_key_to_keyspace_id(sk) keyspace_ids.append(pack_keyspace_id(kid)) else: kid = class_.sharding_key_to_keyspace_id(routing.sharding_key) keyspace_ids = [pack_keyspace_id(kid),] elif routing.entity_id_sharding_key_map is not None: keyspace_ids = [] for sharding_key in routing.entity_id_sharding_key_map.values(): keyspace_ids.append(pack_keyspace_id(class_.sharding_key_to_keyspace_id(sharding_key))) elif routing.keyrange: keyranges = [routing.keyrange,] cursor = vtgate_cursor.VTGateCursor(vtgate_conn, class_.keyspace, tablet_type, keyspace_ids=keyspace_ids, keyranges=keyranges, writable=is_dml) cursor.routing = routing return cursor
def create_vtgate_cursor(class_, vtgate_conn, tablet_type, is_dml, **cursor_kargs): cursor_method = functools.partial(db_object.create_cursor_from_params, vtgate_conn, tablet_type, False) routing = class_.create_shard_routing(cursor_method, **cursor_kargs) if is_dml: if routing.sharding_key is None or db_object._is_iterable_container( routing.sharding_key): dbexceptions.InternalError( "Writes require unique sharding_key") keyspace_ids = None keyranges = None if routing.sharding_key is not None: keyspace_ids = [] if db_object._is_iterable_container(routing.sharding_key): for sk in routing.sharding_key: kid = class_.sharding_key_to_keyspace_id(sk) keyspace_ids.append(pack_keyspace_id(kid)) else: kid = class_.sharding_key_to_keyspace_id(routing.sharding_key) keyspace_ids = [ pack_keyspace_id(kid), ] elif routing.entity_id_sharding_key_map is not None: keyspace_ids = [] for sharding_key in routing.entity_id_sharding_key_map.values(): keyspace_ids.append( pack_keyspace_id( class_.sharding_key_to_keyspace_id(sharding_key))) elif routing.keyrange: keyranges = [ routing.keyrange, ] cursor = vtgate_cursor.VTGateCursor(vtgate_conn, class_.keyspace, tablet_type, keyspace_ids=keyspace_ids, keyranges=keyranges, writable=is_dml) cursor.routing = routing return cursor
def select_by_ids(class_, cursor, where_column_value_pairs, columns_list=None, order_by=None, group_by=None, limit=None, **kwargs): """This method is used to perform in-clause queries. Such queries can cause vtgate to scatter over multiple shards. This uses execute_entity_ids method of vtgate cursor and the entity column and the associated entity_keyspace_id_map is computed based on the routing used - sharding_key or entity_id_map. """ if columns_list is None: columns_list = class_.columns_list query, bind_vars = class_.create_select_query( where_column_value_pairs, columns_list=columns_list, order_by=order_by, group_by=group_by, limit=limit, **kwargs) entity_col_name = None entity_id_keyspace_id_map = {} if cursor.routing.sharding_key is not None: # If the in-clause is based on sharding key entity_col_name = class_.sharding_key_column_name if db_object._is_iterable_container(cursor.routing.sharding_key): for sk in list(cursor.routing.sharding_key): entity_id_keyspace_id_map[sk] = pack_keyspace_id( class_.sharding_key_to_keyspace_id(sk)) else: sk = cursor.routing.sharding_key entity_id_keyspace_id_map[sk] = pack_keyspace_id( class_.sharding_key_to_keyspace_id(sk)) elif cursor.routing.entity_id_sharding_key_map is not None: # If the in-clause is based on entity column entity_col_name = cursor.routing.entity_column_name for en_id, sk in cursor.routing.entity_id_sharding_key_map.iteritems( ): entity_id_keyspace_id_map[en_id] = pack_keyspace_id( class_.sharding_key_to_keyspace_id(sk)) else: dbexceptions.ProgrammingError("Invalid routing method used.") # cursor.routing.entity_column_name is set while creating shard routing. rowcount = cursor.execute_entity_ids(query, bind_vars, entity_id_keyspace_id_map, entity_col_name) rows = cursor.fetchall() return [sql_builder.DBRow(columns_list, row) for row in rows]
def select_by_ids(class_, cursor, where_column_value_pairs, columns_list = None,order_by=None, group_by=None, limit=None, **kwargs): """This method is used to perform in-clause queries. Such queries can cause vtgate to scatter over multiple shards. This uses execute_entity_ids method of vtgate cursor and the entity column and the associated entity_keyspace_id_map is computed based on the routing used - sharding_key or entity_id_map. """ if class_.columns_list is None: raise dbexceptions.ProgrammingError("DB class should define columns_list") if columns_list is None: columns_list = class_.columns_list query, bind_vars = sql_builder.select_by_columns_query(columns_list, class_.table_name, where_column_value_pairs, order_by=order_by, group_by=group_by, limit=limit, **kwargs) entity_col_name = None entity_id_keyspace_id_map = {} if cursor.routing.sharding_key is not None: # If the in-clause is based on sharding key entity_col_name = class_.sharding_key_column_name if db_object._is_iterable_container(cursor.routing.sharding_key): for sk in list(cursor.routing.sharding_key): entity_id_keyspace_id_map[sk] = pack_keyspace_id(class_.sharding_key_to_keyspace_id(sk)) else: sk = cursor.routing.sharding_key entity_id_keyspace_id_map[sk] = pack_keyspace_id(class_.sharding_key_to_keyspace_id(sk)) elif cursor.routing.entity_id_sharding_key_map is not None: # If the in-clause is based on entity column entity_col_name = cursor.routing.entity_column_name for en_id, sk in cursor.routing.entity_id_sharding_key_map.iteritems(): entity_id_keyspace_id_map[en_id] = pack_keyspace_id(class_.sharding_key_to_keyspace_id(sk)) else: dbexceptions.ProgrammingError("Invalid routing method used.") # cursor.routing.entity_column_name is set while creating shard routing. rowcount = cursor.execute_entity_ids(query, bind_vars, entity_id_keyspace_id_map, entity_col_name) rows = cursor.fetchall() return [sql_builder.DBRow(columns_list, row) for row in rows]