|
| 1 | +# Copyright (c) 2024 Airbyte, Inc., all rights reserved. |
| 2 | + |
| 3 | +from abc import ABC, abstractmethod |
| 4 | +from enum import Enum |
| 5 | +from typing import Any, Iterable, Mapping, Optional |
| 6 | + |
| 7 | + |
| 8 | +class CheckpointMode(Enum): |
| 9 | + INCREMENTAL = "incremental" |
| 10 | + RESUMABLE_FULL_REFRESH = "resumable_full_refresh" |
| 11 | + FULL_REFRESH = "full_refresh" |
| 12 | + |
| 13 | + |
| 14 | +class CheckpointReader(ABC): |
| 15 | + """ |
| 16 | + CheckpointReader manages how to iterate over a stream's partitions and serves as the bridge for interpreting the current state |
| 17 | + of the stream that should be emitted back to the platform. |
| 18 | + """ |
| 19 | + |
| 20 | + @abstractmethod |
| 21 | + def next(self) -> Optional[Mapping[str, Any]]: |
| 22 | + """ |
| 23 | + Returns the next slice that will be used to fetch the next group of records. Returning None indicates that the reader |
| 24 | + has finished iterating over all slices. |
| 25 | + """ |
| 26 | + |
| 27 | + @abstractmethod |
| 28 | + def observe(self, new_state: Mapping[str, Any]) -> None: |
| 29 | + """ |
| 30 | + Updates the internal state of the checkpoint reader based on the incoming stream state from a connector. |
| 31 | +
|
| 32 | + WARNING: This is used to retain backwards compatibility with streams using the legacy get_stream_state() method. |
| 33 | + In order to uptake Resumable Full Refresh, connectors must migrate streams to use the state setter/getter methods. |
| 34 | + """ |
| 35 | + |
| 36 | + @abstractmethod |
| 37 | + def get_checkpoint(self) -> Optional[Mapping[str, Any]]: |
| 38 | + """ |
| 39 | + Retrieves the current state value of the stream. The connector does not emit state messages if the checkpoint value is None. |
| 40 | + """ |
| 41 | + |
| 42 | + |
| 43 | +class IncrementalCheckpointReader(CheckpointReader): |
| 44 | + """ |
| 45 | + IncrementalCheckpointReader handles iterating through a stream based on partitioned windows of data that are determined |
| 46 | + before syncing data. |
| 47 | + """ |
| 48 | + |
| 49 | + def __init__(self, stream_state: Mapping[str, Any], stream_slices: Iterable[Optional[Mapping[str, Any]]]): |
| 50 | + self._state: Optional[Mapping[str, Any]] = stream_state |
| 51 | + self._stream_slices = iter(stream_slices) |
| 52 | + self._has_slices = False |
| 53 | + |
| 54 | + def next(self) -> Optional[Mapping[str, Any]]: |
| 55 | + try: |
| 56 | + next_slice = next(self._stream_slices) |
| 57 | + self._has_slices = True |
| 58 | + return next_slice |
| 59 | + except StopIteration: |
| 60 | + # This is used to avoid sending a duplicate state message at the end of a sync since the stream has already |
| 61 | + # emitted state at the end of each slice. If we want to avoid this extra complexity, we can also just accept |
| 62 | + # that every sync emits a final duplicate state |
| 63 | + if self._has_slices: |
| 64 | + self._state = None |
| 65 | + return None |
| 66 | + |
| 67 | + def observe(self, new_state: Mapping[str, Any]) -> None: |
| 68 | + self._state = new_state |
| 69 | + |
| 70 | + def get_checkpoint(self) -> Optional[Mapping[str, Any]]: |
| 71 | + return self._state |
| 72 | + |
| 73 | + |
| 74 | +class ResumableFullRefreshCheckpointReader(CheckpointReader): |
| 75 | + """ |
| 76 | + ResumableFullRefreshCheckpointReader allows for iteration over an unbounded set of records based on the pagination strategy |
| 77 | + of the stream. Because the number of pages is unknown, the stream's current state is used to determine whether to continue |
| 78 | + fetching more pages or stopping the sync. |
| 79 | + """ |
| 80 | + |
| 81 | + def __init__(self, stream_state: Mapping[str, Any]): |
| 82 | + # The first attempt of an RFR stream has an empty {} incoming state, but should still make a first attempt to read records |
| 83 | + # from the first page in next(). |
| 84 | + self._first_page = bool(stream_state == {}) |
| 85 | + self._state: Mapping[str, Any] = stream_state |
| 86 | + |
| 87 | + def next(self) -> Optional[Mapping[str, Any]]: |
| 88 | + if self._first_page: |
| 89 | + self._first_page = False |
| 90 | + return self._state |
| 91 | + elif self._state == {}: |
| 92 | + return None |
| 93 | + else: |
| 94 | + return self._state |
| 95 | + |
| 96 | + def observe(self, new_state: Mapping[str, Any]) -> None: |
| 97 | + self._state = new_state |
| 98 | + |
| 99 | + def get_checkpoint(self) -> Optional[Mapping[str, Any]]: |
| 100 | + return self._state or {} |
| 101 | + |
| 102 | + |
| 103 | +class FullRefreshCheckpointReader(CheckpointReader): |
| 104 | + """ |
| 105 | + FullRefreshCheckpointReader iterates over data that cannot be checkpointed incrementally during the sync because the stream |
| 106 | + is not capable of managing state. At the end of a sync, a final state message is emitted to signal completion. |
| 107 | + """ |
| 108 | + |
| 109 | + def __init__(self, stream_slices: Iterable[Optional[Mapping[str, Any]]]): |
| 110 | + self._stream_slices = iter(stream_slices) |
| 111 | + self._final_checkpoint = False |
| 112 | + |
| 113 | + def next(self) -> Optional[Mapping[str, Any]]: |
| 114 | + try: |
| 115 | + return next(self._stream_slices) |
| 116 | + except StopIteration: |
| 117 | + self._final_checkpoint = True |
| 118 | + return None |
| 119 | + |
| 120 | + def observe(self, new_state: Mapping[str, Any]) -> None: |
| 121 | + pass |
| 122 | + |
| 123 | + def get_checkpoint(self) -> Optional[Mapping[str, Any]]: |
| 124 | + if self._final_checkpoint: |
| 125 | + return {"__ab_no_cursor_state_message": True} |
| 126 | + return None |
0 commit comments