|
22 | 22 | from __future__ import absolute_import |
23 | 23 |
|
24 | 24 | import copy |
| 25 | +import functools |
25 | 26 | import inspect |
26 | 27 | import logging |
27 | 28 | import random |
@@ -918,7 +919,9 @@ def add_input(self, mutable_accumulator, element, *args, **kwargs): |
918 | 919 | raise NotImplementedError(str(self)) |
919 | 920 |
|
920 | 921 | def add_inputs(self, mutable_accumulator, elements, *args, **kwargs): |
921 | | - """Returns the result of folding each element in elements into accumulator. |
| 922 | + """DEPRECATED and unused. |
| 923 | +
|
| 924 | + Returns the result of folding each element in elements into accumulator. |
922 | 925 |
|
923 | 926 | This is provided in case the implementation affords more efficient |
924 | 927 | bulk addition of elements. The default implementation simply loops |
@@ -988,10 +991,14 @@ def apply(self, elements, *args, **kwargs): |
988 | 991 | *args: Additional arguments and side inputs. |
989 | 992 | **kwargs: Additional arguments and side inputs. |
990 | 993 | """ |
| 994 | + if args or kwargs: |
| 995 | + add_input = lambda accumulator, element: self.add_input( |
| 996 | + accumulator, element, *args, **kwargs) |
| 997 | + else: |
| 998 | + add_input = self.add_input |
991 | 999 | return self.extract_output( |
992 | | - self.add_inputs( |
993 | | - self.create_accumulator(*args, **kwargs), elements, *args, |
994 | | - **kwargs), |
| 1000 | + functools.reduce( |
| 1001 | + add_input, elements, self.create_accumulator(*args, **kwargs)), |
995 | 1002 | *args, |
996 | 1003 | **kwargs) |
997 | 1004 |
|
@@ -1088,12 +1095,6 @@ def add_input(self, accumulator, element, *args, **kwargs): |
1088 | 1095 | accumulator = [self._fn(accumulator, *args, **kwargs)] |
1089 | 1096 | return accumulator |
1090 | 1097 |
|
1091 | | - def add_inputs(self, accumulator, elements, *args, **kwargs): |
1092 | | - accumulator.extend(elements) |
1093 | | - if len(accumulator) > self._buffer_size: |
1094 | | - accumulator = [self._fn(accumulator, *args, **kwargs)] |
1095 | | - return accumulator |
1096 | | - |
1097 | 1098 | def merge_accumulators(self, accumulators, *args, **kwargs): |
1098 | 1099 | return [self._fn(_ReiterableChain(accumulators), *args, **kwargs)] |
1099 | 1100 |
|
@@ -1165,12 +1166,6 @@ def add_input(self, accumulator, element): |
1165 | 1166 | accumulator = [self._fn(accumulator)] |
1166 | 1167 | return accumulator |
1167 | 1168 |
|
1168 | | - def add_inputs(self, accumulator, elements): |
1169 | | - accumulator.extend(elements) |
1170 | | - if len(accumulator) > self._buffer_size: |
1171 | | - accumulator = [self._fn(accumulator)] |
1172 | | - return accumulator |
1173 | | - |
1174 | 1169 | def merge_accumulators(self, accumulators): |
1175 | 1170 | return [self._fn(_ReiterableChain(accumulators))] |
1176 | 1171 |
|
@@ -2182,31 +2177,7 @@ def process(self, element, *args, **kwargs): |
2182 | 2177 | # Expected elements input to this DoFn are 2-tuples of the form |
2183 | 2178 | # (key, iter), with iter an iterable of all the values associated with key |
2184 | 2179 | # in the input PCollection. |
2185 | | - if self.runtime_type_check: |
2186 | | - # Apply the combiner in a single operation rather than artificially |
2187 | | - # breaking it up so that output type violations manifest as TypeCheck |
2188 | | - # errors rather than type errors. |
2189 | | - return [(element[0], self.combinefn.apply(element[1], *args, **kwargs))] |
2190 | | - |
2191 | | - # Add the elements into three accumulators (for testing of merge). |
2192 | | - elements = list(element[1]) |
2193 | | - accumulators = [] |
2194 | | - for k in range(3): |
2195 | | - if len(elements) <= k: |
2196 | | - break |
2197 | | - accumulators.append( |
2198 | | - self.combinefn.add_inputs( |
2199 | | - self.combinefn.create_accumulator(*args, **kwargs), |
2200 | | - elements[k::3], |
2201 | | - *args, |
2202 | | - **kwargs)) |
2203 | | - # Merge the accumulators. |
2204 | | - accumulator = self.combinefn.merge_accumulators( |
2205 | | - accumulators, *args, **kwargs) |
2206 | | - # Convert accumulator to the final result. |
2207 | | - return [( |
2208 | | - element[0], self.combinefn.extract_output(accumulator, *args, |
2209 | | - **kwargs))] |
| 2180 | + yield element[0], self.combinefn.apply(element[1], *args, **kwargs) |
2210 | 2181 |
|
2211 | 2182 | def default_type_hints(self): |
2212 | 2183 | hints = self.combinefn.get_type_hints() |
|
0 commit comments