This [[Module Library|XQuery Module]] contains functions for handling ''lazy'' items.
In contrast to standard XQuery items, a lazy item contains a reference to the actual data, and the data itself will only be retrieved if it is requestedprocessed. Hence, possible errors will be postponed, and no memory will be occupied by a lazy item as long as its content has not been requested yet.
The following BaseX functions return lazy items:
The XQuery expression below serves as an example on how large files can be downloaded and written to a file with constant memory consumption:
<syntaxhighlight pre lang="'xquery"'>
file:write-binary('output.data', fetch:binary('http://files.basex.org/xml/xmark111mb.zip'))
</syntaxhighlightpre>
If lazy items are serialized, they will be streamed as well.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|lazy:cache|$items as item()*|item()*}}<br/pre>{{Func|lazy:cache|( $items input as item()*, $lazy as xs:boolean|? := false()) as item()*}}</pre>|-valign="top"
| '''Summary'''
|Caches the data of lazy {{Code|$itemsinput}} in a sequenceitems:<br />* data of lazy items will be are retrieved and cached inside the item.* non-lazy items, or lazy items with cached data, will are simply be passed through.* If {{Code|$lazy}} is set to {{Code|true()}}, caching will be is deferred until the data is eventually requested. Streaming will be disabled: Data will always be cached before a stream is returned.Caching is advisable if an item will be is processed more than once, or if the data may not be available anymore at a later stage.|-valign="top"
| '''Example'''
|In the following example, a file will be is deleted before its content is returned. To avoid a “file not found” error when serializing the result, the content must be cached:<syntaxhighlight pre lang="'xquery"'>
let $file := 'data.txt'
let $text := lazy:cache(file:read-text($file))
return (file:delete($file), $text)
</syntaxhighlightpre>
|}
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>lazy:is-lazy|( $item as item()|) as xs:boolean}}</pre>|-valign="top"
| '''Summary'''
|Checks whether the specified {{Code|$item}} is lazy.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>lazy:is-cached|( $item as item()|) as xs:boolean}}</pre>|-valign="top"
| '''Summary'''
|Checks whether the contents of the specified {{Code|$item}} are cached. The function will always return {{Code|true}} for non-lazy items.