Reading

Range queries, row iterators, column reads and multi-queries.

QueryRequest

QueryRequest is a range query over [StartTime, EndTime) nanoseconds. Set Columns to read only some of each candle’s values; see ColumnSet.

type QueryRequest struct {
	StreamID StreamID
	// StartTime (inclusive) and EndTime (exclusive) are Unix nanoseconds.
	StartTime int64
	EndTime   int64
	// MaxResults caps returned records; 0 means "as many as fit in one
	// response frame". The server may return fewer either way and signals
	// continuation via the response's HasMore byte.
	MaxResults uint32
	// IncludeGaps requests gap-marker records alongside candles. Ignored for
	// tick queries: a tick is an event, not a scheduled slot, so there is no
	// gap to mark.
	IncludeGaps bool
	// Ticks requests raw ticks (24-byte records) instead of candles. The
	// response is a TickResponse payload.
	Ticks bool
	// Descending returns the range newest-first. Combined with MaxResults it
	// selects the LAST N records in the range rather than the first N
	// reversed, which is what makes "the 3 most recent candles" expressible
	// in one request (TestServer_DescendingWithMaxResultsReturnsTheNewest).
	Descending bool
	// AllowStale lets an IN-SYNC COPY answer when this node is not the
	// stream's primary (§7.7.7). The answer then says so (QueryResponse.Stale)
	// and how current it is. Candles only: a copy holds no flushed tick, so a
	// stale tick read is refused by the codec rather than answered short
	// (TestQueryPayload_AStaleTickReadIsRefused). A copy answers only while it
	// is within the node's cluster.copy_read_max_staleness of its primary
	// (default 5s); past it the copy redirects, as one outside the in-sync set
	// does (TestStaleRead_ACopyBeyondItsStalenessBoundRedirects).
	AllowStale bool
	// Rollup, when positive, asks for bars of this length derived at query
	// time from the stream's candles — or, with Ticks, from its trades — by
	// the rules a declared lineage uses (§8.1.12); nothing is stored. The
	// answer is a candle QueryResponse either way, holding only buckets whose
	// stamp lies in [StartTime, EndTime) and that are closed: a bucket still
	// open answers nothing, as the ingest path sends closed candles only.
	// Not with AllowStale (TestQueryPayload_ARollupRoundTripsAndItsMisuseIsRefused).
	Rollup time.Duration
	// Columns, when not zero, asks for only these value columns of each
	// candle (the stamp is always sent). The answer is QueryResponse.Columns,
	// not Points, so a node decodes, sends and a client holds only what was
	// asked for: one column of a long stream is a third of the bytes of the
	// whole record. Candles only, from one node's own data, plain bars: not
	// with Ticks, IncludeGaps, AllowStale or Rollup
	// (TestQueryPayload_AColumnQueryMisuseIsRefusedOnBothSides). A sub-query of
	// a QUERY_MULTI may ask for columns too, each its own set; its result then
	// carries MultiQueryResult.Columns.
	Columns ColumnSet
}

QueryRequest is the decoded payload of a QUERY_STREAM frame.

func ParseQueryPayload(payload []byte) (QueryRequest, error)

ParseQueryPayload decodes a QUERY_STREAM payload. The tick, include-gaps and descending flags are all honored; unknown flag bits are rejected explicitly rather than ignored, so a future flag can never be misread as absent by an older server.

func (q QueryRequest) AnswersTicks() bool

AnswersTicks reports whether q is answered with raw ticks (a TickResponse, or tick records in a multi-query result): a tick query that is not a rollup. A rollup from trades is answered with candles.

QueryResponse

QueryResponse carries candle results. When HasMore is set, resume by advancing StartTime past the last returned record’s timestamp.

type QueryResponse struct {
	Status QueryStatus
	// HasMore signals that the range holds more records than this frame
	// carries; the client resumes by advancing StartTime past the last
	// returned record's timestamp.
	HasMore bool
	// Points carries the records; IsGap marks gap-marker records (only
	// present when the request set IncludeGaps).
	Points []CandlePoint
	// Stale marks an answer served by an in-sync copy rather than the
	// stream's primary (§7.7.7): everything the primary had made durable up
	// to LevelAt is in it, and anything after may be missing. Never set on an
	// answer to a request without AllowStale.
	Stale bool
	// LevelAt is when the copy was last level with its primary's durable
	// watermark, Unix nanoseconds. Carried only when Stale.
	LevelAt int64
	// Columns holds the answer to a column query (QueryRequest.Columns) in
	// place of Points, which is then nil: the rows, but only the columns asked
	// for. Nil for every other answer.
	Columns *ColumnBatch
}

QueryResponse is the decoded payload of a QUERY_RESPONSE frame.

func ParseQueryResponsePayload(payload []byte) (QueryResponse, error)

ParseQueryResponsePayload decodes and validates a QUERY_RESPONSE payload.

TickResponse

TickResponse carries tick results; pagination as QueryResponse.

type TickResponse struct {
	Status  QueryStatus
	HasMore bool
	Ticks   []Tick
}

TickResponse is the decoded payload of a QUERY_RESPONSE carrying ticks.

func ParseTickResponsePayload(payload []byte) (TickResponse, error)

ParseTickResponsePayload decodes a tick-carrying QUERY_RESPONSE payload.

QueryStatus

QueryStatus is the in-band query outcome (distinct from transport errors).

type QueryStatus uint8

QueryStatus is the status byte of a QUERY_RESPONSE, using the spec’s §3.10 codes.

const (
	// QueryStatusOK means the query executed; records follow.
	QueryStatusOK QueryStatus = 0x00
	// QueryStatusStreamNotFound means the stream has never been ingested on
	// this node.
	QueryStatusStreamNotFound QueryStatus = 0x02
	// QueryStatusInvalidRange means the request's time range is malformed
	// (end before or equal to start).
	QueryStatusInvalidRange QueryStatus = 0x03
	// QueryStatusElsewhere means another node serves the sub-query's stream:
	// the result carries the redirect a single query would have received, and
	// no records. It appears only in a QUERY_MULTI_RESPONSE — a single query
	// for a foreign stream is answered with a REDIRECT frame — and lets one
	// batch name streams on several nodes, so the client can split what it is
	// sent back rather than the server refusing the whole batch (§7.7.4).
	QueryStatusElsewhere QueryStatus = 0x04
)

CandleRows

CandleRows iterates a candle range, fetching pages as the caller loops — see Client.QueryCandlesRows. Always check Err after the loop.

type CandleRows struct {
	// Has unexported fields.
}

CandleRows iterates a candle range, fetching pages as needed. Create with Client.QueryCandlesRows; iterate with Next; always finish by checking Err. Not safe for concurrent use — one goroutine drives one CandleRows.

func (r *CandleRows) Close() error

Close releases nothing today (pages are plain memory) but is part of the contract so resource-carrying pages can exist later without breaking callers. Safe to call at any point, more than once.

func (r *CandleRows) Err() error

Err reports the first failure. A nil Err after Next returns false means the range is genuinely exhausted; ALWAYS check it — a transport error mid-range otherwise reads as a short but plausible result.

func (r *CandleRows) Next() bool

Next advances to the next point, fetching the next page when the current one is exhausted. It returns false at the end of the range, on error, or once the MaxResults budget is spent; Err distinguishes failure from exhaustion.

func (r *CandleRows) Point() CandlePoint

Point returns the current point. Valid only after a true Next.

TickRows

TickRows is CandleRows for ticks; see Client.QueryTicksRows.

type TickRows struct {
	// Has unexported fields.
}

TickRows is CandleRows for ticks; same contract, same caveats.

func (r *TickRows) Close() error

Close ends the iteration; see CandleRows.Close.

func (r *TickRows) Err() error

Err reports the first failure; see CandleRows.Err.

func (r *TickRows) Next() bool

Next advances to the next tick; see CandleRows.Next.

func (r *TickRows) Tick() Tick

Tick returns the current tick. Valid only after a true Next.

QueryStatusError

QueryStatusError is an in-band query refusal (stream not found, invalid range) surfaced through the row iterators.

type QueryStatusError struct {
	Status   QueryStatus
	StreamID StreamID
}

QueryStatusError is an in-band query refusal (stream not found, invalid range) surfaced through the iterator, where there is no response struct to carry a status field.

func (e *QueryStatusError) Error() string

MultiResult

MultiResult is one sub-query’s complete answer from Client.QueryMultiAll. Results are positional — result i answers the i’th request — which is what permits two different windows of one stream in a single batch. A missing stream or invalid range marks only its own result’s Status; the rest of the batch is unaffected.

type MultiResult struct {
	// Status is the sub-query's own outcome. A missing stream or an invalid
	// range marks only this result; the rest of the batch is unaffected.
	Status QueryStatus
	// Ticks reports which slice carries the records, mirroring the request.
	Ticks    bool
	Points   []CandlePoint
	TickData []Tick
	// Columns holds a column sub-query's rows (QueryRequest.Columns) in place
	// of Points: the stamps and only the columns asked for. Non-nil for such a
	// sub-query whose Status is OK, even when it holds no rows; nil otherwise.
	Columns *ColumnBatch
	// Elsewhere names the node serving this sub-query's stream when Status is
	// wire.QueryStatusElsewhere: the node asked does not serve it, and the
	// sub-query was not run. RoutingClient.QueryMultiAll follows it; a plain
	// Client hands it back rather than answering with an empty series.
	Elsewhere wire.Redirect
	// Err is set, with Status StatusUnanswered, when RoutingClient.QueryMultiAll
	// could not get this sub-query answered: the node serving it was down,
	// timed out, or could not be routed to. The call's error is then a
	// *PartialResultError naming the node.
	Err error
	// Stale marks a result an in-sync copy answered, for a sub-query that set
	// AllowStale (§7.7.7), and LevelAt is when that copy was last level with
	// its primary, Unix ns. Across pages it is the EARLIEST page's: a series is
	// as current as its oldest part.
	Stale   bool
	LevelAt int64
}

MultiResult is one sub-query’s COMPLETE answer, assembled by QueryMultiAll across as many round trips as it took. Results are positional: result i answers the i’th request, which is what permits two different windows of one stream in a single batch.

MultiQueryResponse

MultiQueryResponse is the raw single-round-trip answer from Client.QueryMulti, possibly partial: results the server’s frame budget did not reach carry HasMore and a resume point. Most callers want QueryMultiAll, which owns the re-asking.

type MultiQueryResponse struct {
	Results []MultiQueryResult
}

MultiQueryResponse is the decoded payload of a QUERY_MULTI_RESPONSE frame. Results are positional: Results[i] answers Queries[i].

func ParseMultiQueryResponsePayload(payload []byte) (MultiQueryResponse, error)

ParseMultiQueryResponsePayload decodes and validates a QUERY_MULTI_RESPONSE payload.

MultiQueryResult

MultiQueryResult is one sub-query’s answer within a MultiQueryResponse.

type MultiQueryResult struct {
	Status QueryStatus
	// HasMore reports that this sub-query's range holds records this response
	// does not carry — either because it was truncated, or because the frame
	// filled before the server reached it at all.
	HasMore bool
	// ResumeFrom is where to restart this sub-query, and is meaningful only
	// when HasMore is set. For a sub-query the server never reached it is the
	// untouched start of the original window, which is precisely why it cannot
	// be derived from the returned records.
	ResumeFrom int64
	// Ticks reports which of the two record slices carries this result. A
	// batch may mix candle and tick sub-queries, and decoding 24-byte tick
	// records as 49-byte candle records yields plausible garbage rather than
	// an error, so the kind travels with the result.
	Ticks    bool
	Points   []CandlePoint
	TickData []Tick
	// Columns carries a column sub-query's answer (QueryRequest.Columns) in
	// place of Points, which is then nil.
	Columns *ColumnBatch
	// Elsewhere names the node that serves this sub-query's stream, and is set
	// exactly when Status is QueryStatusElsewhere. Such a result carries no
	// records, no resume point and no HasMore: the sub-query was not run here.
	// On the wire it follows the result header as a 2-byte length and the
	// REDIRECT payload, the one encoding of a redirect.
	Elsewhere Redirect
	// Stale marks a result an in-sync copy answered, for a sub-query that set
	// AllowStale (§7.7.7), and LevelAt is when that copy was last level with
	// its primary, Unix ns — QueryResponse's pair, per result, because one
	// batch mixes a primary's answers and a copy's. Candle results only.
	Stale   bool
	LevelAt int64
}

MultiQueryResult is one sub-query’s answer.

ColumnSet

ColumnSet names the value columns of a candle a column query asks for: QueryRequest.Columns. The stamp is always sent. A node decodes, sends and the client holds only the columns asked for, so one column of a long stream costs a third of what reading it whole does. Read the range with Client.QueryColumns; a single page of one is QueryResponse.Columns, in place of Points, and a batched sub-query’s is MultiResult.Columns. Not for ticks, gap markers, stale reads or rollups.

type ColumnSet uint8

ColumnSet names value columns of a candle. The stamp is not in the set: a column answer always has it.

const (
	ColumnOpen ColumnSet = 1 << iota
	ColumnHigh
	ColumnLow
	ColumnClose
	ColumnVolume
	// ColumnsAll is every value column.
	ColumnsAll = ColumnOpen | ColumnHigh | ColumnLow | ColumnClose | ColumnVolume
)

The value columns a column query can ask for.

func (s ColumnSet) Count() int

Count is how many columns s holds.

func (s ColumnSet) Has(c ColumnSet) bool

Has reports whether s holds every column in c.

ColumnBatch

ColumnBatch is the answer to a column query: the stamps and, for each column asked for, a slice of values, row for row. A column not asked for is nil. Incomplete reports a row that is a derived bar whose source had a hole.

type ColumnBatch struct {
	Set                            ColumnSet
	Timestamp                      []int64
	Open, High, Low, Close, Volume []float64
	Marks                          []uint8
}

ColumnBatch is a column answer: rows in column form, ascending. Only the columns in Set are non-nil. Marks is the wire’s flags byte per row and is nil when no row is marked, so a batch of plain bars carries none; a marked row is an INCOMPLETE derived bar (Incomplete).

func (b *ColumnBatch) Append(more *ColumnBatch)

Append adds the rows of more, which holds the same columns, after b’s. A page with marks gives b a mark slice covering every row before it.

func (b *ColumnBatch) Incomplete(i int) bool

Incomplete reports whether row i is a derived bar whose source had a hole under its window (CandlePoint.Incomplete).

func (b *ColumnBatch) Len() int

Len is the number of rows.