Module rugged.tuf.hashed_bins

Functions

def find_hash_bin(path: str) ‑> str
Expand source code
def find_hash_bin(path: str) -> str:
    """ Returns the name of the bin for a target file based on its path hash. """
    # Generate hash digest of passed target path and take its prefix, given the
    # global prefix length for the given number of bins.
    hasher = sha256()
    hasher.update(path.encode("utf-8"))
    target_name_hash = hasher.hexdigest()
    log.debug(f"Hash of '{path}': {target_name_hash}")
    hash_prefix_length = _get_hash_prefix_length()
    prefix = int(target_name_hash[:hash_prefix_length], 16)
    # Find lower and upper bounds for hash prefix given its numerical value and
    # the the general bin size for the given number of bins.
    bin_size = _get_bin_size()
    low = prefix - (prefix % bin_size)
    high = low + bin_size - 1
    return _bin_name(low, high)

Returns the name of the bin for a target file based on its path hash.

def generate_hash_bins() ‑> Iterator[Tuple[str, List[str]]]
Expand source code
def generate_hash_bins() -> Iterator[Tuple[str, List[str]]]:
    """
    Returns generator for bin names and hash prefixes per bin.

    Iterates over the total number of hash prefixes in 'bin size'-steps to
    generate bin names and a list of hash prefixes served by each bin.

    It yields an ordered list of incremental hash bin names (ranges), plus
    the hash prefixes each bin is responsible for, e.g.:

    bin_n_name:  00-07  bin_n_hash_prefixes: 00 01 02 03 04 05 06 07
                 08-0f                       08 09 0a 0b 0c 0d 0e 0f
                 10-17                       10 11 12 13 14 15 16 17
                 ...                         ...
                 f8-ff                       f8 f9 fa fb fc fd fe ff
    """
    number_of_prefixes = _get_number_of_prefixes()
    bin_size = _get_bin_size()
    for low in range(0, number_of_prefixes, bin_size):
        hash_prefixes = _get_hash_prefixes_for_bin(low)
        # If the configured number of bins is not a power of 2, the final
        # bin will contain fewer hash prefixes.
        actual_bin_size = len(hash_prefixes)
        high = low + actual_bin_size - 1
        bin_name = _bin_name(low, high)
        yield bin_name, hash_prefixes

Returns generator for bin names and hash prefixes per bin.

Iterates over the total number of hash prefixes in 'bin size'-steps to generate bin names and a list of hash prefixes served by each bin.

It yields an ordered list of incremental hash bin names (ranges), plus the hash prefixes each bin is responsible for, e.g.:

bin_n_name: 00-07 bin_n_hash_prefixes: 00 01 02 03 04 05 06 07 08-0f 08 09 0a 0b 0c 0d 0e 0f 10-17 10 11 12 13 14 15 16 17 … … f8-ff f8 f9 fa fb fc fd fe ff

def get_bins_for_targets(targets: List[str]) ‑> List[str]
Expand source code
def get_bins_for_targets(targets: List[str]) -> List[str]:
    """ Return a list of bin_n role names for a list of targets. """
    bin_n_names = []
    for target in targets:
        bin_n_names.append(find_hash_bin(target))
    return bin_n_names

Return a list of bin_n role names for a list of targets.

def get_number_of_bins() ‑> int
Expand source code
@lru_cache
def get_number_of_bins() -> int:
    """ Return the configured number of hashed bins to distribute targets between. """
    number_of_bins = config['number_of_bins'].get(int)
    log.debug(f"Hashed bins is configured to use {number_of_bins} bins.")
    return number_of_bins

Return the configured number of hashed bins to distribute targets between.

def hashed_bins_is_enabled() ‑> bool
Expand source code
def hashed_bins_is_enabled() -> bool:
    """ Return whether the repository is configured to use hashed bins. """
    return config['use_hashed_bins'].get()

Return whether the repository is configured to use hashed bins.