Changes

Jump to navigation Jump to search
477 bytes added ,  17:50, 21 November 2017
no edit summary
This [[Module Library|XQuery Module]] contains functions for handling ''streamablelazy'' items.
In contrast to standard XQuery items, a streamable lazy item contains only a reference to the actual data. The , and the data itself will only be retrieved if it is requested by an expression, or if the item is to be serialized. Hence, a streamable item only uses a few bytespossible errors will be postponed, and no additional memory is will be occupied during serializationby a lazy item as long as its contents has not been reqested yet.
The following BaseX functions return streamable lazy items:
* Streamable Lazy Base64 binaries:
** <code>[[Database Module#db:retrieve|db:retrieve]]</code>
** <code>[[Fetch Module#fetch:binary|fetch:binary]]</code>
** <code>[[File Module#file:read-binary|file:read-binary]]</code>
* Streamable Lazy strings:
** <code>[[Fetch Module#fetch:text|fetch:text]]</code>
** <code>[[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 never 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|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>[[Fetch File Module#file:write-text|file: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:
file:write-binary('output.data', fetch:binary('http://files.basex.org/xml/xmark111mb.zip'))
</pre>
 
If lazy items are serialized, they will be streamed as well.
=Conventions=
All functions and errors in this module are assigned to the <code><nowiki>http://basex.org/modules/streamlazy</nowiki></code> namespace, which is statically bound to the {{Code|streamlazy}} prefix.<br/>All errors are assigned to the <code><nowiki>http://basex.org/errors</nowiki></code> namespace, which is statically bound to the {{Code|bxerr}} prefix.
=Functions=
==streamlazy:materializecache==
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|streamlazy:materializecache|$value item as item()*|item()*}}
|-
| '''Summary'''
|Returns Caches the contents of a materialized instance of the specified lazy {{Code|$valueitem}}:<br />* if an item is streamable, its value contents of lazy items will be retrieved and cachedin the item.* other, non-streamable lazy items or lazy items with cached contents will simply be passed through.Materialization Caching of a streamable value lazy items is advisable if it may the the targeted contents will not be available anymore when it will eventually be required (see the example below)at a later stage.
|-
| '''Example'''
|In the following example, a file will be deleted before its content is returned. To avoid a “file not found” errorwhen serializing the result, the content will must be materializedcached:
<pre class="brush:xquery">
let $file := 'data.txt'
let $data text := streamlazy:materializecache(file:read-text($file))return (file:delete($file), $datatext)
</pre>
|}
==streamlazy:is-streamablelazy== 
{| width='100%'
|-
| width='120' | '''Signatures'''
|{{Func|stream:is-streamablelazy|$item as item()|xs:boolean}}
|-
| '''Summary'''
|Checks whether the specified {{Code|$item}} is streamablelazy.|} ==lazy:is-cached== {| width='100%'|-| width='120' | '''Signatures'''|{{Func|stream: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.0
 
* Updated: Renamed from Streaming Module to Lazy Module.
* Added: [[#lazy:is-cached|lazy:is-cached]]
;Version 8.0
 * UpdateUpdated: [[#stream:materialize|stream:materialize]] extended to sequences.
This module was introduced with Version 7.7.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu