import { type DocumentId } from '@automerge/automerge-repo'; import * as Effect from 'effect/Effect'; import type * as SqlClient from 'effect/sql/SqlClient'; import * as EffectStream from 'effect/Stream'; import { Resource } from '@dxos/context'; import { QueryAST } from '@dxos/echo-protocol'; import type * as RuntimeProvider from '@dxos/effect/RuntimeProvider'; import { type FeedStore } from '@dxos/feed'; import { type IndexEngine } from '@dxos/index-core'; import { type SpaceId } from '@dxos/keys'; import { QueryService } from '@dxos/protocols/rpc'; import { type AutomergeHost } from '../automerge/index.ts'; import { type ExecutionTrace, type QueryExecutorMode } from '../query/index.ts'; import { type InvalidationHint } from './invalidation-hint.ts'; import type { SpaceStateManager } from './space-state-manager.ts'; export type QueryServiceProps = { /** Read on each query: the host builds its engine when it opens, after this service is constructed. */ indexEngine: () => IndexEngine; runtime: RuntimeProvider.RuntimeProvider; automergeHost: AutomergeHost; spaceStateManager: SpaceStateManager; /** * Brings the index up to date and resolves once done. Awaited before a feed-scoped query's first * execution so that a query issued right after a feed append reads the just-written items instead * of racing the host's deferred indexing task. Feed reads have no client-side working-set * fallback, so the index is their only source of truth. */ updateIndexes: () => Promise; /** * True once every indexed object in scope has a snapshot (see `IndexEngine.hasCompleteSnapshots`). * The compiled executor reads that store rather than loading documents, so while it is still * filling after upgrade a query awaits indexing before its first execution. Ignored on the * in-memory path, which loads documents itself. */ hasCompleteSnapshots?: (scope: { spaceIds?: readonly SpaceId[]; feeds: boolean; }) => Promise; /** Evaluation path for every query this service creates; see {@link QueryExecutorMode}. */ executor?: QueryExecutorMode; /** Resolved lazily, like `indexEngine`: the client exists only once the host is open. */ sql: () => SqlClient.SqlClient; /** Overrides {@link DEFAULT_QUERY_DEBOUNCE}. */ debounce?: Partial; /** Whether any of a client's data subscriptions holds a document. */ holdsDocument?: (subscriptionIds: readonly string[], documentId: DocumentId) => boolean; /** Read for feeds with indexing disabled and for feed retention; see the query executor's `feedStore`. */ feedStore?: FeedStore; }; /** * How long a live query waits before re-running, in proportion to what its last run cost, so one * heavy query re-executing on every write cannot starve the cheap queries and writes sharing its worker. */ export type QueryDebounceOptions = { /** Debounce window per millisecond of the last run: at 4 a query spends at most a fifth of the time running. */ factor: number; /** Queries whose last run was cheaper than this (ms) re-run as soon as they are invalidated. */ minCost: number; /** Ceiling on a debounce window (ms), so a very slow query still refreshes. */ maxDelay: number; /** Cost (ms) charged for a run; defaults to its wall-clock time, which tests replace to be deterministic. */ cost?: (query: QueryAST.Query, measured: number) => number; }; export declare const DEFAULT_QUERY_DEBOUNCE: QueryDebounceOptions; export declare class QueryServiceImpl extends Resource implements QueryService.Handlers { #private; private readonly _params; private readonly '_queries'; private '_updateQueries'; /** Reactive queries currently registered, across every space. */ get 'activeQueryCount'(): number; 'getQueryTraces'(): ExecutionTrace[]; constructor(_params: QueryServiceProps); '_open'(): Promise; '_close'(): Promise; /** * @deprecated No longer needed with SQL-based indexing. */ ['QueryService.setConfig'](_request: QueryService.IndexConfig): Effect.Effect; /** * @deprecated No longer needed with SQL-based indexing. */ ['QueryService.reindex'](): Effect.Effect; ['QueryService.execQuery'](request: QueryService.QueryRequest): EffectStream.Stream; /** * Schedule re-execution of queries, optionally guided by a targeted hint. * When called without a hint, all queries are marked dirty (catch-all invalidation). */ 'invalidateQueries'(hint?: InvalidationHint): void; /** * Resolves once every query invalidated so far has re-run and sent its results, so a caller that * waited for the index also sees the queries it changed. Debounced queries run now rather than * when their window closes. */ 'awaitQueryUpdates'(): Promise; private '_createQuery'; private '_executeQueries'; } //# sourceMappingURL=query-service.d.ts.map