pabloem commented on a change in pull request #11182: [BEAM-9650] Add 
PeriodicImpulse Transform and slowly changing side input documentation
URL: https://github.com/apache/beam/pull/11182#discussion_r405090794
 
 

 ##########
 File path: sdks/python/apache_beam/transforms/periodicsequence.py
 ##########
 @@ -0,0 +1,160 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from __future__ import absolute_import
+
+import time
+
+import apache_beam as beam
+import apache_beam.runners.sdf_utils as sdf_utils
+from apache_beam.io.restriction_trackers import OffsetRange
+from apache_beam.io.restriction_trackers import OffsetRestrictionTracker
+from apache_beam.transforms import core
+from apache_beam.transforms import window
+from apache_beam.transforms.ptransform import PTransform
+from apache_beam.transforms.window import TimestampedValue
+from apache_beam.utils import timestamp
+from apache_beam.utils.timestamp import MAX_TIMESTAMP
+from apache_beam.utils.timestamp import Timestamp
+
+
+class ImpulseSeqGenRestrictionProvider(core.RestrictionProvider):
+  def initial_restriction(self, element):
+    start, end, interval = element
+    return OffsetRange(start - interval, end)
+
+  def create_tracker(self, restriction):
+    return ImpulseSeqGenRestrictionTracker(restriction)
+
+  def restriction_size(self, unused_element, restriction):
+    return restriction.size()
+
+
+class ImpulseSeqGenRestrictionTracker(OffsetRestrictionTracker):
+  def try_split(self, fraction_of_remainder):
+    if not self._checkpointed:
+      if fraction_of_remainder != 0:
+        return None
+
+      if self._current_position is None:
+        cur = self._range.start
+      else:
+        cur = self._current_position
+      split_point = cur
+
+      if split_point < self._range.stop:
+        self._checkpointed = True
+        self._range, residual_range = self._range.split_at(split_point)
+        return self._range, residual_range
+
+  def cur_pos(self):
+    return self._current_position
+
+  def try_claim(self, pos):
+    if ((self._last_claim_attempt is None) or
+        (pos > self._last_claim_attempt and pos == self._range.stop)):
+      self._last_claim_attempt = pos
+      return True
+    else:
+      return super(ImpulseSeqGenRestrictionTracker, self).try_claim(pos)
+
+
+class ImpulseSeqGenDoFn(beam.DoFn):
+  def process(
+      self,
+      element,
+      restriction_tracker=beam.DoFn.RestrictionParam(
+          ImpulseSeqGenRestrictionProvider())):
+
+    _, _, interval = element
+
+    assert isinstance(restriction_tracker, sdf_utils.RestrictionTrackerView)
+
+    t = time.time()
+    cr = restriction_tracker.current_restriction()
+    current_timestamp = cr.start
+
+    restriction_tracker.try_claim(current_timestamp)
 
 Review comment:
   fair enough!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to