3 _logger = logging.getLogger('arvados.ranges')
6 def __init__(self, locator, range_start, range_size, segment_offset=0):
8 self.range_start = range_start
9 self.range_size = range_size
10 self.segment_offset = segment_offset
13 return "Range(%r, %r, %r, %r)" % (self.locator, self.range_start, self.range_size, self.segment_offset)
15 def __eq__(self, other):
16 return (self.locator == other.locator and
17 self.range_start == other.range_start and
18 self.range_size == other.range_size and
19 self.segment_offset == other.segment_offset)
21 def first_block(data_locators, range_start, range_size):
24 # range_start/block_start is the inclusive lower bound
25 # range_end/block_end is the exclusive upper bound
27 hi = len(data_locators)
29 i = int((hi + lo) / 2)
30 block_size = data_locators[i].range_size
31 block_start = data_locators[i].range_start
32 block_end = block_start + block_size
34 # perform a binary search for the first block
35 # assumes that all of the blocks are contiguous, so range_start is guaranteed
36 # to either fall into the range of a block or be outside the block range entirely
37 while not (range_start >= block_start and range_start < block_end):
39 # must be out of range, fail
41 if range_start > block_start:
45 i = int((hi + lo) / 2)
46 block_size = data_locators[i].range_size
47 block_start = data_locators[i].range_start
48 block_end = block_start + block_size
52 class LocatorAndRange(object):
53 def __init__(self, locator, block_size, segment_offset, segment_size):
54 self.locator = locator
55 self.block_size = block_size
56 self.segment_offset = segment_offset
57 self.segment_size = segment_size
59 def __eq__(self, other):
60 return (self.locator == other.locator and
61 self.block_size == other.block_size and
62 self.segment_offset == other.segment_offset and
63 self.segment_size == other.segment_size)
66 return "LocatorAndRange(%r, %r, %r, %r)" % (self.locator, self.block_size, self.segment_offset, self.segment_size)
68 def locators_and_ranges(data_locators, range_start, range_size):
69 """Get blocks that are covered by a range.
71 Returns a list of LocatorAndRange objects.
74 list of Range objects, assumes that blocks are in order and contiguous
86 range_end = range_start + range_size
88 i = first_block(data_locators, range_start, range_size)
92 # We should always start at the first segment due to the binary
94 while i < len(data_locators):
96 block_start = dl.range_start
97 block_size = dl.range_size
98 block_end = block_start + block_size
100 "%s range_start %s block_start %s range_end %s block_end %s",
101 dl.locator, range_start, block_start, range_end, block_end)
102 if range_end <= block_start:
103 # range ends before this block starts, so don't look at any more locators
106 if range_start >= block_start and range_end <= block_end:
107 # range starts and ends in this block
108 resp.append(LocatorAndRange(dl.locator, block_size, dl.segment_offset + (range_start - block_start), range_size))
109 elif range_start >= block_start and range_end > block_end:
110 # range starts in this block
111 resp.append(LocatorAndRange(dl.locator, block_size, dl.segment_offset + (range_start - block_start), block_end - range_start))
112 elif range_start < block_start and range_end > block_end:
113 # range starts in a previous block and extends to further blocks
114 resp.append(LocatorAndRange(dl.locator, block_size, dl.segment_offset, block_size))
115 elif range_start < block_start and range_end <= block_end:
116 # range starts in a previous block and ends in this block
117 resp.append(LocatorAndRange(dl.locator, block_size, dl.segment_offset, range_end - block_start))
118 block_start = block_end
122 def replace_range(data_locators, new_range_start, new_range_size, new_locator, new_segment_offset):
124 Replace a file segment range with a new segment.
127 data_locators will be updated in place
130 list of Range objects, assumes that segments are in order and contiguous
133 start of range to replace in data_locators
136 size of range to replace in data_locators
139 locator for new segment to be inserted
142 segment offset within the locator
145 if new_range_size == 0:
148 new_range_end = new_range_start + new_range_size
150 if len(data_locators) == 0:
151 data_locators.append(Range(new_locator, new_range_start, new_range_size, new_segment_offset))
154 last = data_locators[-1]
155 if (last.range_start+last.range_size) == new_range_start:
156 if last.locator == new_locator:
157 # extend last segment
158 last.range_size += new_range_size
160 data_locators.append(Range(new_locator, new_range_start, new_range_size, new_segment_offset))
163 i = first_block(data_locators, new_range_start, new_range_size)
167 # We should always start at the first segment due to the binary
169 while i < len(data_locators):
170 dl = data_locators[i]
171 old_segment_start = dl.range_start
172 old_segment_end = old_segment_start + dl.range_size
174 "%s range_start %s segment_start %s range_end %s segment_end %s",
175 dl, new_range_start, old_segment_start, new_range_end,
177 if new_range_end <= old_segment_start:
178 # range ends before this segment starts, so don't look at any more locators
181 if old_segment_start <= new_range_start and new_range_end <= old_segment_end:
182 # new range starts and ends in old segment
183 # split segment into up to 3 pieces
184 if (new_range_start-old_segment_start) > 0:
185 data_locators[i] = Range(dl.locator, old_segment_start, (new_range_start-old_segment_start), dl.segment_offset)
186 data_locators.insert(i+1, Range(new_locator, new_range_start, new_range_size, new_segment_offset))
188 data_locators[i] = Range(new_locator, new_range_start, new_range_size, new_segment_offset)
190 if (old_segment_end-new_range_end) > 0:
191 data_locators.insert(i+2, Range(dl.locator, new_range_end, (old_segment_end-new_range_end), dl.segment_offset + (new_range_start-old_segment_start) + new_range_size))
193 elif old_segment_start <= new_range_start and new_range_end > old_segment_end:
194 # range starts in this segment
195 # split segment into 2 pieces
196 data_locators[i] = Range(dl.locator, old_segment_start, (new_range_start-old_segment_start), dl.segment_offset)
197 data_locators.insert(i+1, Range(new_locator, new_range_start, new_range_size, new_segment_offset))
199 elif new_range_start < old_segment_start and new_range_end >= old_segment_end:
200 # range starts in a previous segment and extends to further segments
201 # delete this segment
204 elif new_range_start < old_segment_start and new_range_end < old_segment_end:
205 # range starts in a previous segment and ends in this segment
206 # move the starting point of this segment up, and shrink it.
207 data_locators[i] = Range(dl.locator, new_range_end, (old_segment_end-new_range_end), dl.segment_offset + (new_range_end-old_segment_start))