From 75fa72e80d4a9358f7ed729514070eda360dce57 Mon Sep 17 00:00:00 2001 From: auxten Date: Mon, 22 Apr 2024 16:54:55 +0800 Subject: [PATCH] Add PyReader and PyWriter ABC --- chdb/rwabc.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 chdb/rwabc.py diff --git a/chdb/rwabc.py b/chdb/rwabc.py new file mode 100644 index 00000000000..30a79ec9576 --- /dev/null +++ b/chdb/rwabc.py @@ -0,0 +1,64 @@ +from abc import ABC, abstractmethod +from typing import List, Any + + +class PyReader(ABC): + def __init__(self, data: Any): + """ + Initialize the reader with data. The exact type and structure of `data` can vary. + + Args: + data (Any): The data with which to initialize the reader, format and type are not strictly defined. + """ + self.data = data + + @abstractmethod + def read(self, col_names: List[str], count: int) -> bytes: + """ + Read a specified number of rows from the given columns. Must be implemented by subclasses. + + Args: + col_names (List[str]): List of column names to read. + count (int): Maximum number of rows to read. + + Returns: + bytes: Serialized data representing the requested rows. + """ + pass + + +class PyWriter(ABC): + def __init__(self, col_names: List[str], types: List[type], data: Any): + """ + Initialize the writer with column names, their types, and initial data. + + Args: + col_names (List[str]): List of column names. + types (List[type]): List of types corresponding to each column. + data (Any): Initial data to setup the writer, format and type are not strictly defined. + """ + self.col_names = col_names + self.types = types + self.data = data + self.blocks = [] + + @abstractmethod + def write(self, col_names: List[str], columns: List[List[Any]]) -> None: + """ + Save columns of data to blocks. Must be implemented by subclasses. + + Args: + col_names (List[str]): List of column names that are being written. + columns (List[List[Any]]): List of columns data, each column is represented by a list. + """ + pass + + @abstractmethod + def finalize(self) -> bytes: + """ + Assemble and return the final data from blocks. Must be implemented by subclasses. + + Returns: + bytes: The final serialized data. + """ + pass