Changes

Jump to navigation Jump to search
104 bytes added ,  18:33, 1 December 2023
m
Text replacement - "</syntaxhighlight>" to "</pre>"
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 contents content has not been reqested requested yet.
The following BaseX functions return lazy items:
* Lazy Base64 binaries:
** <code>[[Database Module#db:retrieve{{Function|db:retrieve]]</code>** <code>[[Fetch Module#fetch:binary|fetch:binary]]</code>}}** <code>[[{{Function|File Module#|file:read-binary}}** {{Function|Database|filedb:readget-binary]]</code>}}
* Lazy strings:
** <code>[[{{Function|Fetch Module#fetch:text|fetch:text]]</code>}}** <code>[[{{Function|File Module#file:read-text|file:read-text]]</code>}}
Some functions are capable of consuming the contents of lazy items in a ''streamable'' fashion: data will not be cached, but instead passed on to another target (file, the calling expression, etc.). The following streaming functions are currently available:
* [[Archive Module]] (most functions)
* Conversion Module: <code>[[Conversion Module#convert:binary-to-bytes{{Function|convert:binary-to-bytes]]</code>, <code>[[Conversion Module#convert:binary-to-string|convert:binary-to-string]]</code>}}* Database Module: <code>[[Database File Module#db:store{{Function|db:store]]</code>* File Module: <code>[[File Module#|file:write-binary|file:write-binary]]</code>text}}, <code>[[{{Function|File Module#file:write-text|file:write-text]] </code> }} (if no encoding is specified)* Database Module: {{Function|Database|db:put-binary}}
* [[Hashing Module]] (all functions)
The XQuery expression below serves as an example on how large files can be downloaded and written to a file with constant memory consumption:
<pre classlang="brush:'xquery"'>
file:write-binary('output.data', fetch:binary('http://files.basex.org/xml/xmark111mb.zip'))
</pre>
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>lazy:cache|( $input as item ()*, $lazy as itemxs:boolean? := false()|) as item()}}*</pre>|-valign="top"
| '''Summary'''
|Caches the contents data of a lazy {{Code|$iteminput}}items:<br />* contents data of lazy items will be are retrieved and cached in inside the item.* non-lazy items , or lazy items with cached contents will data, are simply be passed through.* If {{Code|$lazy}} is set to {{Code|true()}}, caching is deferred until the data is eventually requested. Streaming will be disabled: Data will be cached before a stream is returned.Caching of a lazy items is advisable if an item is processed more than once, or if the the targeted contents will 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:<pre classlang="brush:'xquery"'>
let $file := 'data.txt'
let $text := lazy:cache(file:read-text($file))
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|stream<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|stream<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.
=Changelog=
 
;Version 9.1
 
* Updated: {{Function||lazy:cache}}: {{Code|$lazy}} argument added; support for sequences.
;Version 9.0
* Updated: Renamed from Streaming Module to Lazy Module.
* Added: [[#lazy:is-cached{{Function||lazy:is-cached]]}}
;Version 8.0
* Updated: [[#stream:materialize{{Function||stream:materialize]] }} extended to sequences.
This module was introduced with Version 7.7.
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu