1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from __future__ import print_function
6 from __future__ import absolute_import
7 from future.utils import listvalues
8 from builtins import object
17 from ._ranges import locators_and_ranges, Range
18 from .arvfile import StreamFileReader
19 from arvados.retry import retry_method
20 from arvados.keep import *
23 from ._normalize_stream import normalize_stream
25 class StreamReader(object):
26 def __init__(self, tokens, keep=None, debug=False, _empty=False,
28 self._stream_name = None
29 self._data_locators = []
30 self._files = collections.OrderedDict()
32 self.num_retries = num_retries
38 if debug: print('tok', tok)
39 if self._stream_name is None:
40 self._stream_name = tok.replace('\\040', ' ')
43 s = re.match(r'^[0-9a-f]{32}\+(\d+)(\+\S+)*$', tok)
45 blocksize = int(s.group(1))
46 self._data_locators.append(Range(tok, streamoffset, blocksize, 0))
47 streamoffset += blocksize
50 s = re.search(r'^(\d+):(\d+):(\S+)', tok)
53 size = int(s.group(2))
54 name = s.group(3).replace('\\040', ' ')
55 if name not in self._files:
56 self._files[name] = StreamFileReader(self, [Range(pos, 0, size, 0)], name)
58 filereader = self._files[name]
59 filereader.segments.append(Range(pos, filereader.size(), size))
62 raise errors.SyntaxError("Invalid manifest format")
65 return self._stream_name
71 return listvalues(self._files)
74 n = self._data_locators[-1]
75 return n.range_start + n.range_size
77 def locators_and_ranges(self, range_start, range_size):
78 return locators_and_ranges(self._data_locators, range_start, range_size)
81 def _keepget(self, locator, num_retries=None):
82 return self._keep.get(locator, num_retries=num_retries)
85 def readfrom(self, start, size, num_retries=None):
86 """Read up to 'size' bytes from the stream, starting at 'start'"""
89 if self._keep is None:
90 self._keep = KeepClient(num_retries=self.num_retries)
92 for lr in locators_and_ranges(self._data_locators, start, size):
93 data.append(self._keepget(lr.locator, num_retries=num_retries)[lr.segment_offset:lr.segment_offset+lr.segment_size])
96 def manifest_text(self, strip=False):
97 manifest_text = [self.name().replace(' ', '\\040')]
99 for d in self._data_locators:
100 m = re.match(r'^[0-9a-f]{32}\+\d+', d.locator)
101 manifest_text.append(m.group(0))
103 manifest_text.extend([d.locator for d in self._data_locators])
104 manifest_text.extend([' '.join(["{}:{}:{}".format(seg.locator, seg.range_size, f.name.replace(' ', '\\040'))
105 for seg in f.segments])
106 for f in listvalues(self._files)])
107 return ' '.join(manifest_text) + '\n'