This [[Module Library|XQuery Module]] provides functions to organize values in a persistent main-memory key-value store.
The store is useful if data (a system configuration, maps serving as indexes) needs to be repeatedly accessed. The store is persistent: Contents will be written to disk at shutdown time, and the serialized store will be retrieved from disk as soon as the store is used for the first time. The store will be stored in a binary {{Code|store.basex}} file in the database directory.
In addition, custom stores can be read and written. Custom stores have filenames with the pattern {{Code|store-NAME.basex}}. The implicit write of the standard store at shutdown time will be disabled if a custom store is used.
Functions of this module are non-deterministic nondeterministic and side-effecting: Updates will immediately be visible, and a repeated call of the same function may yield different results if the contents of the store have changed.
=Conventions=
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>store:get|( $key as xs:string|) as item()*}}<br/ pre>|-valign="top"
|'''Summary'''
|Retrieves an entry from the store with the given {{Code|$key}}. If the addressed entry does not exist, an empty sequence is returned.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>store:put|( $key as xs:string, $value as item()*|) as empty-sequence()}}<br/ pre>|-valign="top"
|'''Summary'''
|Stores an entry with the given {{Code|$key}} and {{Code|$value}} in the store:
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>store:get-or-put|( $key as xs:string, $put as function(*)) as item()*|item()*}}<br/ pre>|-valign="top"
|'''Summary'''
|Retrieves an entry from the store with the given {{Code|$key}}. The {{Code|$put}} function will only be invoked if the entry does not exist, and its result will be stored and returned instead.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>store:remove|( $key as xs:string|) as empty-sequence()}}<br/ pre>|-valign="top"
|'''Summary'''
|Removes an entry with the given {{Code|$key}} from the store. No error will be raised if an addressed entry does not exist.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>store:keys||() as xs:string*}}</pre>|-valign="top"
|'''Summary'''
|Lists the names of all keys.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>store:clear||() as empty-sequence()}}<br/ pre>|-valign="top"
|'''Summary'''
|Resets the store by removing all its entries.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|store:read||empty-sequence()}}<br/ pre>{{Func|store:read|( $name as xs:string|? := ()) as empty-sequence()}}</pre>|-valign="top"
|'''Summary'''
|Retrieves the standard store from disk, or a custom store if a {{Code|$name}} is supplied.
|-valign="top"
|'''Errors'''
|{{Error|io|#Errors}} The store could not be read.<br/>{{Error|name|#Errors}} The specified name is invalid.<br/>{{Error|not-found|#Errors}} A store with the specified name does not exist.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|store:write||empty-sequence()}}<br/ pre>{{Func|store:write|( $name as xs:string|? := ()) as empty-sequence()}}</pre>|-valign="top"
|'''Summary'''
|Writes the standard store to disk, or to a custom store file if a {{Code|$name}} is supplied. If the standard store is empty, the store file will be deleted.
|-valign="top"
|'''Errors'''
|{{Error|io|#Errors}} The store could not be written.<br/>{{Error|name|#Errors}} The specified name is invalid.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>store:list||() as xs:string*}}</pre>|-valign="top"
|'''Summary'''
|Lists the names of all custom stores.
{| width='100%'
|-valign="top"| width='120' | '''SignaturesSignature'''|{{Func|<pre>store:delete|( $name as xs:string|) as empty-sequence()}}</pre>|-valign="top"
|'''Summary'''
|Deletes a custom store from disk.
|-valign="top"
|'''Errors'''
|{{Error|name|#Errors}} The specified name is invalid.<br/>{{Error|not-found|#Errors}} A store with the specified name does not exist.
'''Use Case 1: Create/update a system configuration in a running BaseX server instance:
<syntaxhighlight pre lang="'xquery"'>
(: store an integer :)
store:put('version', 1),
(: serialize configuration to disk :)
store:write()
</syntaxhighlightpre>
The configuration can be requested by further operations, e.g. a client request:
<syntaxhighlight pre lang="'xquery"'>
store:get('version')
</syntaxhighlightpre>
The store will still be available if BaseX is restarted until it is cleared.
'''Use Case 2: Create index for fast lookup operations in the GUI:
<syntaxhighlight pre lang="'xquery"'>
let $map := map:merge(
for $country in db:openget('factbook')//country
for $religion in $country//religions
group by $religion
)
return store:put('religions', $map)
</syntaxhighlightpre>
A subsequent query can be used to access its contents:
<syntaxhighlight pre lang="'xquery"'>
store:get('religions')?Buddhism
</syntaxhighlightpre>
Note that the store will eventually be written to disk unless it is invalidated before closing the GUI.
! width="110"|Code
|Description
|-valign="top"
|{{Code|io}}
| The store could not be read or written.
|-valign="top"
|{{Code|name}}
| The specified name is invalid.
|-valign="top"
|{{Code|not-found}}
| A store with the specified name does not exist.