# ========= Copyright 2026 @ Strukto.AI All Rights Reserved. ========= # Licensed 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. # ========= Copyright 2026 @ Strukto.AI All Rights Reserved. ========= from collections.abc import AsyncIterator from functools import partial from mirage.accessor.postgres import PostgresAccessor from mirage.cache.index import IndexCacheStore from mirage.commands.builtin.generic.head import head as generic_head from mirage.commands.builtin.generic.head import head_multi from mirage.commands.builtin.postgres._provision import head_tail_provision from mirage.commands.builtin.utils.stream import _read_stdin_async from mirage.commands.registry import command from mirage.commands.spec import SPECS from mirage.core.postgres.glob import resolve_glob from mirage.core.postgres.read import read as postgres_read from mirage.io.types import ByteSource, IOResult from mirage.types import PathSpec @command("head", resource="postgres", spec=SPECS["head"], provision=head_tail_provision) async def head( accessor: PostgresAccessor, paths: list[PathSpec], *texts: str, stdin: AsyncIterator[bytes] | bytes | None = None, n: str | None = None, c: str | None = None, q: bool = False, v: bool = False, index: IndexCacheStore = None, **_extra: object, ) -> tuple[ByteSource | None, IOResult]: n_int = int(n) if n is not None else None c_int = int(c) if c is not None else None if paths: paths = await resolve_glob(accessor, paths, index) # Row reads push LIMIT into the query instead of fetching the whole # relation; non-row scopes ignore the limit kwarg. n_eff = n_int if n_int is not None else 10 read_fn = postgres_read if c_int is None and n_eff > 0: read_fn = partial(postgres_read, limit=min(n_eff, accessor.config.default_row_limit)) return head_multi(paths, read=read_fn, accessor=accessor, index=index, n=n_int, c=c_int, show_headers=(v or len(paths) > 1) and not q), IOResult() raw = await _read_stdin_async(stdin) if raw is None: raise ValueError("head: missing operand") return generic_head(raw, n=n_int, c=c_int), IOResult()