Skip to content

Commit 8d53195

Browse files
authored
feat(query-util): provide util functions to get default table name (#514)
1 parent 95c7ece commit 8d53195

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

python/cocoindex/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Cocoindex is a framework for building and running indexing pipelines.
33
"""
4-
from . import functions, query, sources, storages, cli
4+
from . import functions, query, sources, storages, cli, utils
55
from .flow import FlowBuilder, DataScope, DataSlice, Flow, flow_def, transform_flow
66
from .flow import EvaluateAndDumpOptions, GeneratedField
77
from .flow import update_all_flows_async, FlowLiveUpdater, FlowLiveUpdaterOptions

python/cocoindex/flow.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,8 @@ class _FlowBuilderState:
310310
engine_flow_builder: _engine.FlowBuilder
311311
field_name_builder: _NameBuilder
312312

313-
def __init__(self, /, name: str | None = None):
314-
flow_name = _flow_name_builder.build_name(name, prefix="_flow_")
315-
self.engine_flow_builder = _engine.FlowBuilder(get_full_flow_name(flow_name))
313+
def __init__(self, full_name: str):
314+
self.engine_flow_builder = _engine.FlowBuilder(full_name)
316315
self.field_name_builder = _NameBuilder()
317316

318317
def get_data_slice(self, v: Any) -> _engine.DataSlice:
@@ -464,9 +463,13 @@ class Flow:
464463
"""
465464
A flow describes an indexing pipeline.
466465
"""
466+
_name: str
467+
_full_name: str
467468
_lazy_engine_flow: Callable[[], _engine.Flow]
468469

469-
def __init__(self, engine_flow_creator: Callable[[], _engine.Flow]):
470+
def __init__(self, name: str, full_name: str, engine_flow_creator: Callable[[], _engine.Flow]):
471+
self._name = name
472+
self._full_name = full_name
470473
engine_flow = None
471474
lock = Lock()
472475
def _lazy_engine_flow() -> _engine.Flow:
@@ -497,7 +500,7 @@ def build_tree(label: str, lines: list):
497500
tree.children.append(section_node)
498501
return tree
499502

500-
def _get_spec(self, verbose: bool = False) -> list[tuple[str, str, int]]:
503+
def _get_spec(self, verbose: bool = False) -> _engine.RenderedSpec:
501504
return self._lazy_engine_flow().get_spec(output_mode="verbose" if verbose else "concise")
502505

503506
def _get_schema(self) -> list[tuple[str, str, str]]:
@@ -509,12 +512,19 @@ def __str__(self):
509512
def __repr__(self):
510513
return repr(self._lazy_engine_flow())
511514

515+
@property
516+
def name(self) -> str:
517+
"""
518+
Get the name of the flow.
519+
"""
520+
return self._name
521+
512522
@property
513523
def full_name(self) -> str:
514524
"""
515525
Get the full name of the flow.
516526
"""
517-
return self._lazy_engine_flow().name()
527+
return self._full_name
518528

519529
def update(self) -> _engine.IndexUpdateInfo:
520530
"""
@@ -555,14 +565,16 @@ def _create_lazy_flow(name: str | None, fl_def: Callable[[FlowBuilder, DataScope
555565
Create a flow without really building it yet.
556566
The flow will be built the first time when it's really needed.
557567
"""
568+
flow_name = _flow_name_builder.build_name(name, prefix="_flow_")
569+
flow_full_name = get_full_flow_name(flow_name)
558570
def _create_engine_flow() -> _engine.Flow:
559-
flow_builder_state = _FlowBuilderState(name=name)
571+
flow_builder_state = _FlowBuilderState(flow_full_name)
560572
root_scope = DataScope(
561573
flow_builder_state, flow_builder_state.engine_flow_builder.root_scope())
562574
fl_def(FlowBuilder(flow_builder_state), root_scope)
563575
return flow_builder_state.engine_flow_builder.build_flow(execution_context.event_loop)
564576

565-
return Flow(_create_engine_flow)
577+
return Flow(flow_name, flow_full_name, _create_engine_flow)
566578

567579

568580
_flows_lock = Lock()
@@ -695,7 +707,7 @@ async def _flow_info_async(self) -> TransformFlowInfo:
695707
return self._lazy_flow_info
696708

697709
async def _build_flow_info_async(self) -> TransformFlowInfo:
698-
flow_builder_state = _FlowBuilderState(name=self._flow_name)
710+
flow_builder_state = _FlowBuilderState(self._flow_name)
699711
sig = inspect.signature(self._flow_fn)
700712
if len(sig.parameters) != len(self._flow_arg_types):
701713
raise ValueError(

python/cocoindex/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from .flow import Flow
2+
from .setting import get_app_namespace
3+
4+
def get_target_storage_default_name(flow: Flow, target_name: str, delimiter: str = "__") -> str:
5+
"""
6+
Get the default name for a target.
7+
It's used as the underlying storage name (e.g. a table, a collection, etc.) followed by most storage backends, if not explicitly specified.
8+
"""
9+
return get_app_namespace(trailing_delimiter=delimiter) + flow.name + delimiter + target_name

0 commit comments

Comments
 (0)