コード例 #1
0
    def create_shard_routing(class_, *pargs, **kargs):
        """This creates the ShardRouting object based on the kargs.
    This prunes the routing kargs so as not to interfere with the
    actual database method.

    Args:
      *pargs: Positional arguments
      **kargs: Routing key-value params. These are used to determine routing.
      There are two mutually exclusive mechanisms to indicate routing.
      1. entity_id_map {"entity_id_column": entity_id_value} where entity_id_column
      could be the sharding key or a lookup based entity column of this table. This
      helps determine the keyspace_ids for the cursor.
      2. keyrange - This helps determine the keyrange for the cursor.

    Returns:
     ShardRouting object and modified kargs
    """
        lookup_cursor_method = pargs[0]
        routing = db_object.ShardRouting(class_.keyspace)
        entity_id_map = None

        entity_id_map = kargs.get("entity_id_map", None)
        if entity_id_map is None:
            kr = None
            key_range = kargs.get("keyrange", None)
            if isinstance(key_range, keyrange.KeyRange):
                kr = key_range
            else:
                kr = keyrange.KeyRange(key_range)
            if kr is not None:
                routing.keyrange = kr
            # Both entity_id_map and keyrange have been evaluated. Return.
            return routing

        # entity_id_map is not None
        if len(entity_id_map) != 1:
            dbexceptions.ProgrammingError("Invalid entity_id_map '%s'" %
                                          entity_id_map)

        entity_id_col = entity_id_map.keys()[0]
        entity_id = entity_id_map[entity_id_col]

        #TODO: the current api means that if a table doesn't have the sharding key column name
        # then it cannot pass in sharding key for routing purposes. Will this cause
        # extra load on lookup db/cache ? This is cleaner from a design perspective.
        if entity_id_col == class_.sharding_key_column_name:
            # Routing using sharding key.
            routing.sharding_key = entity_id
            if not class_.is_sharding_key_valid(routing.sharding_key):
                raise dbexceptions.InternalError("Invalid sharding_key %s" %
                                                 routing.sharding_key)
        else:
            # Routing using lookup based entity.
            routing.entity_column_name = entity_id_col
            routing.entity_id_sharding_key_map = class_.lookup_sharding_key_from_entity_id(
                lookup_cursor_method, entity_id_col, entity_id)

        return routing
コード例 #2
0
    def create_shard_routing(class_, *pargs, **kwargs):
        routing = db_object.ShardRouting(class_.keyspace)
        routing.shard_name = kwargs.get('shard_name')
        if routing.shard_name is None:
            dbexceptions.InternalError(
                'For custom sharding, shard_name cannot be None.')

        if (_is_iterable_container(routing.shard_name) and is_dml):
            raise dbexceptions.InternalError(
                'Writes are not allowed on multiple shards.')
        return routing
コード例 #3
0
 def create_shard_routing(class_, *pargs, **kwargs):
     routing = db_object.ShardRouting(class_.keyspace)
     routing.keyrange = keyrange.KeyRange(
         keyrange_constants.NON_PARTIAL_KEYRANGE)
     return routing