|
29 | 29 | import apache_beam as beam |
30 | 30 | from apache_beam import coders |
31 | 31 | from apache_beam.io import unbounded_source as _unbounded_source_module |
| 32 | +from apache_beam.io.unbounded_source import _EVENT_TIME_BASE |
32 | 33 | from apache_beam.io.unbounded_source import _NO_DATA |
33 | 34 | from apache_beam.io.unbounded_source import CheckpointMark |
34 | 35 | from apache_beam.io.unbounded_source import ReadFromUnboundedSource |
| 36 | +from apache_beam.io.unbounded_source import UnboundedCountingSource |
35 | 37 | from apache_beam.io.unbounded_source import UnboundedReader |
36 | 38 | from apache_beam.io.unbounded_source import UnboundedSource |
| 39 | +from apache_beam.io.unbounded_source import _CountingCheckpointMark |
| 40 | +from apache_beam.io.unbounded_source import _CountingReader |
37 | 41 | from apache_beam.io.unbounded_source import _FinalizeCheckpointOnce |
38 | 42 | from apache_beam.io.unbounded_source import _ReaderCache |
39 | 43 | from apache_beam.io.unbounded_source import _ReadFromUnboundedSourceDoFn |
|
55 | 59 |
|
56 | 60 | # pylint: disable=expression-not-assigned |
57 | 61 |
|
58 | | -# Realistic event-time base away from the Unix epoch. |
59 | | -_EVENT_TIME_BASE = Timestamp(1729987200) # 2024-10-27T00:00:00Z |
60 | | - |
61 | | -# ------------------------------------------------------------------------------ |
62 | | -# In-memory demo source emitting integers 0..count-1 with event time |
63 | | -# ``_EVENT_TIME_BASE + index``. It self-terminates at EOF, resumes from |
64 | | -# ``last_index + 1``, and splits into even/odd sub-sources when configured. |
65 | | -# ------------------------------------------------------------------------------ |
66 | | - |
67 | | - |
68 | | -class _CountingCheckpointMark(CheckpointMark): |
69 | | - def __init__(self, last_index, finalize_log=None): |
70 | | - self.last_index = last_index |
71 | | - self._finalize_log = finalize_log |
72 | | - |
73 | | - @override |
74 | | - def finalize_checkpoint(self): |
75 | | - if self._finalize_log is not None: |
76 | | - self._finalize_log.append(self.last_index) |
77 | | - |
78 | | - def __eq__(self, other): |
79 | | - return ( |
80 | | - isinstance(other, _CountingCheckpointMark) and |
81 | | - other.last_index == self.last_index) |
82 | | - |
83 | | - def __hash__(self): |
84 | | - return hash(self.last_index) |
85 | | - |
86 | | - def __repr__(self): |
87 | | - return '_CountingCheckpointMark(last_index=%r)' % (self.last_index, ) |
88 | | - |
89 | | - |
90 | | -class _CountingReader(UnboundedReader): |
91 | | - def __init__( |
92 | | - self, count, start_index, finalize_log=None, modulus=1, residue=0): |
93 | | - self._count = count |
94 | | - self._next = start_index |
95 | | - self._modulus = modulus |
96 | | - self._residue = residue |
97 | | - self._current = None |
98 | | - self._exhausted = False |
99 | | - self._finalize_log = finalize_log |
100 | | - self.closed = False |
101 | | - |
102 | | - def _read_next(self): |
103 | | - while self._next < self._count: |
104 | | - index = self._next |
105 | | - self._next += 1 |
106 | | - if index % self._modulus == self._residue: |
107 | | - self._current = index |
108 | | - return True |
109 | | - self._exhausted = True |
110 | | - return False |
111 | | - |
112 | | - @override |
113 | | - def start(self): |
114 | | - return self._read_next() |
115 | | - |
116 | | - @override |
117 | | - def advance(self): |
118 | | - return self._read_next() |
119 | | - |
120 | | - @override |
121 | | - def get_current(self): |
122 | | - return self._current |
123 | | - |
124 | | - @override |
125 | | - def get_current_timestamp(self): |
126 | | - return _EVENT_TIME_BASE + self._current |
127 | | - |
128 | | - @override |
129 | | - def get_watermark(self): |
130 | | - if self._exhausted: |
131 | | - return MAX_TIMESTAMP |
132 | | - if self._current is None: |
133 | | - return MIN_TIMESTAMP |
134 | | - return _EVENT_TIME_BASE + self._current |
135 | | - |
136 | | - @override |
137 | | - def get_checkpoint_mark(self): |
138 | | - last = self._current if self._current is not None else self._next - 1 |
139 | | - return _CountingCheckpointMark(last, finalize_log=self._finalize_log) |
140 | | - |
141 | | - @override |
142 | | - def close(self): |
143 | | - self.closed = True |
144 | | - |
145 | | - |
146 | | -class UnboundedCountingSource(UnboundedSource): |
147 | | - def __init__( |
148 | | - self, |
149 | | - count, |
150 | | - finalize_log=None, |
151 | | - is_splittable=False, |
152 | | - modulus=1, |
153 | | - residue=0): |
154 | | - self._count = count |
155 | | - self._finalize_log = finalize_log |
156 | | - self._is_splittable = is_splittable |
157 | | - self._modulus = modulus |
158 | | - self._residue = residue |
159 | | - self.last_reader = None |
160 | | - |
161 | | - @override |
162 | | - def split(self, desired_num_splits, options=None): |
163 | | - if not self._is_splittable or desired_num_splits < 2: |
164 | | - return [self] |
165 | | - # Split into independent even/odd sub-sources (each non-splittable). |
166 | | - return [ |
167 | | - UnboundedCountingSource( |
168 | | - self._count, |
169 | | - finalize_log=self._finalize_log, |
170 | | - modulus=2, |
171 | | - residue=residue) for residue in (0, 1) |
172 | | - ] |
173 | | - |
174 | | - @override |
175 | | - def create_reader(self, options, checkpoint_mark): |
176 | | - start_index = ( |
177 | | - 0 if checkpoint_mark is None else checkpoint_mark.last_index + 1) |
178 | | - self.last_reader = _CountingReader( |
179 | | - self._count, |
180 | | - start_index, |
181 | | - finalize_log=self._finalize_log, |
182 | | - modulus=self._modulus, |
183 | | - residue=self._residue) |
184 | | - return self.last_reader |
185 | | - |
186 | | - @override |
187 | | - def get_checkpoint_mark_coder(self): |
188 | | - return coders.PickleCoder() |
189 | | - |
190 | 62 |
|
191 | 63 | class _StringCountingReader(_CountingReader): |
192 | 64 | @override |
|
0 commit comments