Changes

Jump to navigation Jump to search
1,537 bytes added ,  16:20, 27 February 2020
no edit summary
This [[Module Library|XQuery Module]] contains functions for handling ''streamablelazy'' items.
In contrast to standard XQuery items, streamable items may take up much less space, because they only contain a lazy item contains a reference to the actual data. The , and the data itself will only be retrieved if it is required requested. Hence, possible errors will be postponed, and no memory will be occupied by another expression, or if an a lazy item is serialized. Serialization of streamable items takes constant spaceas long as its content has not been requested yet.
The following BaseX functions return streamable lazy items:
* Lazy Base64 binaries:** <code>[[Database Module#db:retrieve|db:retrieve]]</code>** <code>[[Fetch Module#fetch:binary|fetch:binary]]</code>* <code>[[Fetch Module#fetch:text|fetch:text]]</code>* <code>[[File Module#file:read-binary|file:read-binary]]</code>* <code>[[File Module#file:read-text|file:read-text]]</code>
Some functions are capable of consuming items in a ''streamable'' fashion* Lazy strings: data will never be cached, but instead passed on to another target (** <code>[[Fetch Module#fetch:text|fetch:text]]</code>** <code>[[File Module#file:read-text|file, the calling expression, etc.). The following streaming functions are currently available:read-text]]</code>
* <code>[[Conversion Module#convertSome functions are capable of consuming the contents of lazy items in a ''streamable'' fashion:binary-data will not be cached, but instead passed on to-bytes|convert:binary-to-bytes]]</code>* <code>[[Database Module#db:store|db:store]]</code>* <code>[[File Module#file:write-binary|file:write-binary]]</code>* <code>[[Fetch Module#file:write-text|another target (file, the calling expression, etc.). The following streaming functions are currently available:write-text]]</code>
The XQuery expression serves as an example on how large files can be downloaded and written * [[Archive Module]] (most functions)* Conversion Module: <code>[[Conversion Module#convert:binary-to a -bytes|convert:binary-to-bytes]]</code>, <code>[[Conversion Module#convert:binary-to-string|convert:binary-to-string]]</code>* Database Module: <code>[[Database Module#db:store|db:store]]</code>* File Module: <code>[[File Module#file:write-binary|file:write-binary]]</code>, <code>[[File Module#file:write-text|file with constant memory consumption:write-text]] </code> (if no encoding is specified)* [[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 classsyntaxhighlight lang="brush:xquery">
file:write-binary('output.data', fetch:binary('http://files.basex.org/xml/xmark111mb.zip'))
</presyntaxhighlightIf lazy items are serialized, they will be streamed as well.
=Conventions=
All functions and errors in this module are assigned to the {{Code|<code><nowiki>http://basex.org/modules/stream}} lazy</nowiki></code> namespace, which is statically bound to the {{Code|streamlazy}} prefix.<br/>All errors are assigned to the {{Code|http://basex.org/errors}} namespace, which is statically bound to the {{Code|bxerr}} prefix.
=Functions=
==streamlazy:materializecache== 
{| width='100%'
|-
| width='90120' | '''Signatures'''|{{Func|streamlazy:materializecache|$items as item ()*|item()*}}<br/>{{Func|lazy:cache|$items as item()*, $lazy as xs:boolean|item()*}}
|-
| '''Summary'''
|Returns a materialized instance Caches the data of the specified lazy {{Code|$itemitems}}.in a sequence:<br />* data of lazy items will be retrieved and cached inside the item.* non-lazy items, or lazy items with cached data, will simply be passed through.* If an item {{Code|$lazy}} is streamableset to {{Code|true()}}, its content caching will be deferred until the data is eventually requested. Streaming will be disabled: Data will always be retrieved, and cached before a new stream is returned.Caching is advisable if an item containing its will be processed more than once, or if the data may not be available anymore at a later stage.|-| '''Example'''|In the following example, a file will be deleted before its content is returned. OtherTo avoid a “file not found” error when serializing the result, non-streamable items will simply the content must be passed throughcached:<syntaxhighlight lang="xquery">let $file := 'data.txt'let $text := lazy:cache(file:read-text($file))return (file:delete($file), $text)</syntaxhighlight>
|}
==streamlazy:is-streamablelazy== 
{| width='100%'
|-
| width='90120' | '''Signatures'''|{{Func|streamlazy:is-streamablelazy|$item as item()|item()xs:boolean}}
|-
| '''Summary'''
|Checks if whether the specified {{Code|$item}} is streamablelazy.|} ==lazy:is-cached== {| width='100%'|-| width='120' | '''Signatures'''|{{Func|lazy:is-cached|$item as item()|xs:boolean}}|-| '''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: [[#lazy:cache|lazy:cache]]: {{Code|$lazy}} argument added; support for sequences.
 
;Version 9.0
 
* Updated: Renamed from Streaming Module to Lazy Module.
* Added: [[#lazy:is-cached|lazy:is-cached]]
 
;Version 8.0
 
* Updated: [[#stream:materialize|stream:materialize]] extended to sequences.
This module was introduced with Version 7.7.
 
[[Category:XQuery]]
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu