~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/osutils.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-06 16:29:12 UTC
  • mto: This revision was merged to the branch mainline in revision 1869.
  • Revision ID: john@arbash-meinel.com-20060706162912-1680b115cdb24071
[merge] Johan Rydberg test updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
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)
 
920
 
 
921
 
 
922
def offsets_to_http_ranges(offsets, fudge_factor=0):
 
923
    """Turn a list of offsets and sizes into a list of byte ranges.
 
924
 
 
925
    :param offsets: A list of tuples of (start, size).  An empty list
 
926
    is not accepted.
 
927
    :param fudge_factor: Fudge together ranges that are fudge_factor
 
928
    bytes from eachother together.
 
929
 
 
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.
 
933
    """
 
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)
 
937
 
 
938
    max_negative = 0
 
939
    prev_end = None
 
940
    combined = []
 
941
 
 
942
    for start, size in offsets:
 
943
        if start < 0:
 
944
            max_negative = min(start, max_negative)
 
945
        else:
 
946
            end = start + size - 1
 
947
            if prev_end is None:
 
948
                combined.append([start, end])
 
949
            elif start <= prev_end + 1 + fudge_factor:
 
950
                combined[-1][1] = end
 
951
            else:
 
952
                combined.append([start, end])
 
953
            prev_end = end 
 
954
 
 
955
    mutter("combined %d offsets into %d ranges; tail access %d", len(offsets),
 
956
           len(combined), -max_negative)
 
957
 
 
958
    return combined, -max_negative