917
917
key_a = path_prefix_key(path_a)
918
918
key_b = path_prefix_key(path_b)
919
919
return cmp(key_a, key_b)
922
def offsets_to_http_ranges(offsets, fudge_factor=0):
923
"""Turn a list of offsets and sizes into a list of byte ranges.
925
:param offsets: A list of tuples of (start, size). An empty list
927
:param fudge_factor: Fudge together ranges that are fudge_factor
928
bytes from eachother together.
930
:return: a list of byte ranges (start, end) and the amount of data
931
to fetch from the tail of the file.. Adjacent ranges will be
932
combined in the result.
934
# We need a copy of the offsets, as the caller might expect it to
935
# remain unsorted. This doesn't seem expensive for memory at least.
936
offsets = sorted(offsets)
942
for start, size in offsets:
944
max_negative = min(start, max_negative)
946
end = start + size - 1
948
combined.append([start, end])
949
elif start <= prev_end + 1 + fudge_factor:
950
combined[-1][1] = end
952
combined.append([start, end])
955
mutter("combined %d offsets into %d ranges; tail access %d", len(offsets),
956
len(combined), -max_negative)
958
return combined, -max_negative