|
12 | 12 | Protocol, |
13 | 13 | dataclass_transform, |
14 | 14 | Annotated, |
| 15 | + TypeVar, |
| 16 | + Generic, |
| 17 | + Literal, |
15 | 18 | get_args, |
16 | 19 | ) |
17 | 20 |
|
@@ -421,6 +424,53 @@ def _inner(fn: Callable[..., Any]) -> Callable[..., Any]: |
421 | 424 | return _inner |
422 | 425 |
|
423 | 426 |
|
| 427 | +######################################################## |
| 428 | +# Custom source connector |
| 429 | +######################################################## |
| 430 | + |
| 431 | + |
| 432 | +@dataclasses.dataclass |
| 433 | +class SourceReadOptions: |
| 434 | + include_ordinal: bool = False |
| 435 | + include_content_version_fp: bool = False |
| 436 | + include_value: bool = False |
| 437 | + |
| 438 | + |
| 439 | +T = TypeVar("T") |
| 440 | + |
| 441 | +NON_EXISTENCE = "NON_EXISTENCE" |
| 442 | + |
| 443 | + |
| 444 | +@dataclasses.dataclass |
| 445 | +class SourceRowData(Generic[T]): |
| 446 | + """ |
| 447 | + The data of a source row. |
| 448 | +
|
| 449 | + - value: The value of the source row. |
| 450 | + - ordinal: The ordinal of the source row. |
| 451 | + - content_version_fp: The content version fingerprint of the source row. |
| 452 | + """ |
| 453 | + |
| 454 | + value: T | Literal["NON_EXISTENCE"] | None |
| 455 | + ordinal: int | None = None |
| 456 | + content_version_fp: bytes | None = None |
| 457 | + |
| 458 | + |
| 459 | +# Users will define a source spec class first (e.g. CustomSource), then provide a source connector like this: |
| 460 | +# |
| 461 | +# @cocoindex.op.source_connector(spec_cls=CustomSource, key_type=KeyType, value_type=ValueType) |
| 462 | +# class CustomSourceConnector: |
| 463 | +# @staticmethod |
| 464 | +# async def create(spec: CustomSource) -> Self: |
| 465 | +# ... |
| 466 | +# |
| 467 | +# async def list(self, options: SourceReadOptions) -> AsyncIterator[tuple[KeyType, SourceRowData[ValueType]]]: |
| 468 | +# ... |
| 469 | +# |
| 470 | +# async def get_value(self, key: KeyType, options: SourceReadOptions) -> SourceRowData[ValueType]: |
| 471 | +# ... |
| 472 | + |
| 473 | + |
424 | 474 | ######################################################## |
425 | 475 | # Custom target connector |
426 | 476 | ######################################################## |
|
0 commit comments