Documentation
¶
Overview ¶
Package xdmstream is a pull cursor over a document as it is parsed: the events of xdm's one parse loop, with every well-formedness, DOCTYPE, entity and limit rule xdm.Parse applies, and MaxBytes, MaxNodes and MaxDepth counted over the whole document. Only the open elements are held; each element the consumer moves past is dropped.
The parse runs in steps on the reader's goroutine (xdmburst.Steps): each call that needs the next event parses on to it, and the parse pauses there.
What a consumer may read at each event, through Event.Node:
- StartElement: the element's name, attributes, in-scope namespaces and base URI, and its ancestors' (all open). Not its content, which is not read yet.
- EndElement: the same element; its content has been dropped.
- Text, Comment, PI: the node, complete: kind, name, string value.
- EndDocument: nothing (Node is nil).
Event.Node is a node in the parse's own tree, whose records are reused once it is dropped: it is valid until the next call on the cursor and must not be kept (a kept one would read another node), nor compared with the nodes below. Strings read from it stay valid. View gives a start tag's element as a node that may be kept, without its content; Ground and GroundLater copy an element out whole. Those are the nodes of the document to identity and order: copies of one node are that node.
Index ¶
- type Cursor
- func (c *Cursor) At() Event
- func (c *Cursor) Cancel(cancel func() error)
- func (c *Cursor) Close()
- func (c *Cursor) Flush() error
- func (c *Cursor) Fresh() bool
- func (c *Cursor) Ground() (*xdm.Node, error)
- func (c *Cursor) GroundLater(fn func(*xdm.Node) error) error
- func (c *Cursor) Handoff(f func() func())
- func (c *Cursor) Leaf() (*xdm.Node, error)
- func (c *Cursor) Next() (Event, error)
- func (c *Cursor) NextTag(f *Filter) (Event, error)
- func (c *Cursor) Observe(o Observer)
- func (c *Cursor) Ordinal(n *xdm.Node) (uint64, bool)
- func (c *Cursor) Peek() (Event, error)
- func (c *Cursor) Skip() error
- func (c *Cursor) SkipTag(f *Filter) (Event, error)
- func (c *Cursor) StripWhitespace(strip func(el *xdm.Node) bool)
- func (c *Cursor) Tee(n int, run func(k int, sub *Cursor) error, swap func(k int, in bool)) []error
- func (c *Cursor) Text() (string, error)
- func (c *Cursor) View() (*xdm.Node, error)
- type Event
- type Filter
- type Kind
- type Observer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cursor ¶
type Cursor struct {
// contains filtered or unexported fields
}
Cursor reads one document. It is not safe for concurrent use.
func Open ¶
func Open(r io.Reader, opts xdm.ParseOptions) *Cursor
Open returns a cursor over r, parsed with opts. Nothing is read until the first call, but the document's place among other trees is fixed now, as a tree-mode parse made now would have it: copies out of it order before a document loaded after Open and before the first read. Close releases it.
func (*Cursor) At ¶
At is the current event: the last one Next, NextTag, SkipTag, Skip, Ground, GroundLater or Text returned (a Peek does not move it).
func (*Cursor) Cancel ¶
Cancel has the parse stop once cancel returns an error (a context's Err): it is asked at each read of the input, so a parse that reads on inside one step -- through a skipped element or events a filter passes over -- stops too, and the error the cursor then returns is cancel's. A Tee's cursors have c's. It must be set before the first call.
func (*Cursor) Flush ¶
Flush calls now the functions GroundLater was given whose copies are still held, rather than when the document element closes. It may be called only at an end tag: when the last event Next, Skip, Ground, GroundLater or Text returned is an EndElement, and nothing is peeked. A consumer whose reading ends inside the document -- at the close of the element it was handed -- calls it there, so that every copy it asked for has arrived. The error is the first those functions returned.
func (*Cursor) Fresh ¶
Fresh reports whether c can still be teed: nothing has been read from it and it has no observers.
func (*Cursor) Ground ¶
Ground moves past the current element, as Skip, and returns a copy of it that may be kept: the element, whole, under shallow copies of its ancestors (name, namespaces, attributes, base URI) and a document node with the document's URIs, as xdm.ParseBursts delivers one. A small element's copy shares its tree and its ancestors' copies with the small elements grounded before and after it (xdm's ground batch), so a Ground under a deep spine costs the element, not the spine; nothing the copy answers tells that tree from one of its own: it has no siblings there, and its order, identity and generate-id are its original's. A large element's copy is in a tree of its own.
func (*Cursor) GroundLater ¶
GroundLater moves past the current element, as Skip, and has fn called with a copy of it, as Ground returns one, perhaps after more of the document has been read. Small elements grounded so share one tree and the copies of their ancestors (xdm's burst batches), as Ground's do, and the tree is handed over complete, with no later growth. Such a tree holds those elements under their common ancestors' copies, so the ancestors' content is not one element's alone. fn is called in document order, before any later Ground returns, and at the latest when the document element closes. A copy is not handed over when the parse fails first.
fn's error does not stop the parse: it is returned by the call that met it (the one during which the copy was handed over, or Flush), and the cursor reads on from where that call left it, so the rest of the document can still be read and checked (a consumer that failed settles: is the rest well-formed, valid?). Once a function has failed, those after it are not called: their consumer has failed.
func (*Cursor) Handoff ¶
func (c *Cursor) Handoff(f func() func())
Handoff has f called where the cursor's parse waits for another goroutine: a Tee's cursors, which take turns, call it as a turn ends (what f returns is called when the turn goes on), so that state the reader's goroutine holds (xpath's EvalLock, once a host call has shared it) can be let go before the next reader needs it. The parse itself runs on the reader's goroutine, so a cursor read alone hands nothing off. A Tee's cursors have c's. It must be set before the first call.
func (*Cursor) Leaf ¶
Leaf returns the current event's text, comment or processing instruction as a node that may be kept: a copy, complete, under copies of its ancestors as a view has them (names, attributes, namespaces, base URI; no content), which is the original to Is, Identity, Compare and generate-id, as views and grounded copies are. It may be asked again until the cursor moves on, but not after Peek.
func (*Cursor) Next ¶
Next returns the next event, descending into the current element when it is a start tag. After EndDocument it returns io.EOF; after a parse error, that error again.
func (*Cursor) NextTag ¶
NextTag is Next for a reader that needs only some of the document's events, those f reports (see Filter), and EndDocument. Passing over events costs no switch to the reader: it is what makes a reader that wants few of them cheap. Event.Closed says how far up the elements passed over closed.
func (*Cursor) Ordinal ¶
Ordinal returns the ordinal of n when n is an element copied out of this cursor's document (a view, or a node of a copy Ground or GroundLater made): the index of its original in a tree-mode parse, which an Observer is told with it. For any other node it returns false.
func (*Cursor) Peek ¶
Peek returns the event Next will return, without moving to it. At a start tag it decides to descend: Skip, Ground and Text are no longer possible there, and the view is made first, since it can be made only there. The current event's node stays valid only if it is a start tag.
func (*Cursor) Skip ¶
Skip moves past the current element, a start tag Next just returned, to its end. Its content is still parsed and checked, and an error in it is Skip's, but it is dropped as it is parsed: the parse holds no more of it than the path to the node being read.
func (*Cursor) SkipTag ¶
SkipTag is Skip, then NextTag: the end tag of the element skipped is not returned, and the skip and the next event cost one switch to the reader.
func (*Cursor) StripWhitespace ¶
StripWhitespace has the parse drop whitespace-only text directly inside each element el for which strip reports true, as xsl:strip-space does: el is open in the parse's tree, with its ancestors. Such text is not an event, nor in a copy, nor in Text's value. It must be set before the first call.
func (*Cursor) Tee ¶
Tee is how several consumers read one document at once (xsl:fork, an xsl:map whose entries each read it: full streaming C10). Each consumer gets a cursor of its own over the bytes c would have parsed, which are read from c's reader once, a block at a time, as the consumers ask for them. Every cursor parses those bytes with c's options, whitespace stripping and Handoff, so each sees exactly the events, nodes, identities, errors and limits c would have: the consumers need nothing a single cursor does not give them. The cost is a parse per consumer; the memory, one block of bytes beside what each parse holds.
The consumers take turns, one at a time, in the order k = 0..n-1 for each block: run(k, its cursor) runs until its parse wants bytes that have not been read, then the next consumer goes on. swap is called as consumer k stops (in false) and goes on (in true), so that state the consumers share can be set aside between turns (state a goroutine holds, such as a lock, is c's Handoff's: a turn ends as a parse waits for bytes). A consumer runs on a goroutine of its own, but never at the same time as another or as the caller: each turn starts and ends with a channel exchange.
Tee returns each consumer's error. Once consumer k has failed, those after it are not needed (an error is reported in consumer order): their cursors fail with errTeeStopped at their next read. A panic in run is raised again in the caller, the first in consumer order. Tee may be called only before anything has been read from c; c is spent afterwards.
A consumer may tee its own cursor (an xsl:fork in a fork's prong): its consumers are then live beside every consumer of the tees it is under, so the bound is on the consumers live at once across such nested tees, maxTeeReaders, not on each tee. A tee that would pass it starts nothing and fails every consumer with errTeeReaders, leaving c unread.
func (*Cursor) Text ¶
Text moves past the current element, as Skip, and returns its string value, read from the parse's tree before the element is dropped: nothing is copied out, but the parse holds the element whole until its end.
func (*Cursor) View ¶
View returns the view of the current element, a start tag Next just returned: a node that may be kept, in a small tree of its own, with the element's name, attributes, namespaces, base URI, ancestors (copies of them, with theirs) and identity, as a full parse has them, and no content. Asking it for content -- children, descendants, siblings, its string or typed value -- panics with a *streamtrace.PostureError (counted by streamtrace.Guard) instead of answering. Views of one element, and the copies Ground and GroundLater make, are one node to Is, Identity, Compare and generate-id, and order as a full parse's nodes do.
It may be asked again, and after Peek, until the cursor moves on; then it fails as Skip would.
type Event ¶
type Event struct {
Kind Kind
Depth int
Node *xdm.Node
// Closed is the least depth at which an element NextTag passed over
// closed since the event before, 0 when none did: what was open there
// is not the element open there now.
Closed int
}
Event is one step of the document. Depth is the node's depth: 1 for the document element and the comments and processing instructions beside it.
type Filter ¶
type Filter struct {
// Accept reports whether the start tag of el is reported; every one is
// when Accept is nil. It is called during the parse, with el open in
// the parse's tree, and must not call the cursor.
Accept func(el *xdm.Node) bool
// Leaves says text, comments and processing instructions are reported.
Leaves bool
// The end tag of an element descended into is reported at Depth or
// above, and deeper only with Ends.
Depth int
Ends bool
}
A Filter says which events NextTag and SkipTag report. What they pass over is still parsed and checked, and an element whose start tag they pass over is descended into, so that what is inside it is met; its end tag is passed over too.
type Observer ¶
An Observer is told of every element's start and its end (end true), and of every text, comment and processing instruction (end false), in document order, those inside an element skipped, grounded or read by Text too, before the cursor reports it; last, before EndDocument, of the document's end (n nil, depth 0, end true). The node is valid only during the call, and must not be kept; its ancestors are open, its name, attributes and (for a leaf) value are complete. ord is the node's ordinal (Ordinal).