Main Page » XQuery » Functions » Store Functions

Store Functions

Updated: All opened stores are now kept in main memory. As a result, a newly opened store will not overwrite the default store anymore. The functions have been enriched with a parameter to supply the name of a store.

This module provides functions to organize values in a persistent main-memory key-value store.

Stores are useful if data (a system configuration, maps serving as indexes) needs to be repeatedly accessed. A store is persistent: Contents will be written to disk at shutdown time (unless WRITESTORE is disabled), and the serialized store will be retrieved from disk as soon as the store is used for the first time. The default store will be stored in a binary store.basex file in the database directory.

In addition, custom stores can be read and written, and can be addressed via an additional $name parameter. Custom stores have filenames with the pattern store-NAME.basex.

The Cache provides similar functionality, but its entries have a limited lifetime and are transient, i.e., will never be persisted.

Functions of this module are 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.

Single operations are atomic, but a sequence of them is not. If a store is accessed by several clients at the same time, a value that depends on entries that were read before may be based on outdated data.

Conventions

All functions and errors are in the http://basex.org/modules/store namespace, to which the store prefix is statically bound.

Key-value Operations

store:get

Signature
store:get(  $key   as xs:string,  $name  as xs:string?  := '') as item()*create
SummaryRetrieves an entry with the given $key from the default store, or from a custom store if $name is specified. If the addressed entry does not exist, an empty sequence is returned.

store:put

Signature
store:put(  $key    as xs:string,  $value  as item()*,  $name   as xs:string?  := '') as empty-sequence()create
SummaryStores an entry with the given $key and $value in the default store, or in a custom store if $name is specified:
  • If the addressed store does not exist in main memory, it will either be opened from disk or created automatically.
  • If the value is an empty sequence, its entry is removed.
  • Before a value is stored, it is compactified (see prof:shrink for more information).
  • Additionally, if a value refers to an opened database or is a lazy item, its contents are materialized in main memory.
  • Values with function items are rejected.

store:get-or-put

Signature
store:get-or-put(  $key   as xs:string,  $put   as fn() as item()*,  $name  as xs:string?  := '') as item()*create
SummaryRetrieves an entry with the given $key from the default store, or from a custom store if $name is specified. The $put function will be invoked if the entry does not exist, and its result will be stored and returned.

store:remove

Signature
store:remove(  $key   as xs:string,  $name  as xs:string?  := '') as empty-sequence()create
SummaryRemoves an entry with the given $key from the default store, or from a custom store if $name is specified. No error will be raised if an addressed entry does not exist.

store:keys

Signature
store:keys(  $name  as xs:string?  := '') as xs:string*create
SummaryLists the names of all keys of the default store, or of a custom store if $name is specified.

Organize Stores

store:list

Signature
store:list() as xs:string*create
SummaryLists the names of all custom stores.

store:info

Added: New function.

Signature
store:info(  $name  as xs:string?  := '') as map(*)create
SummaryReturns information on the default store, or on a custom store if $name is specified:
  • entries: number of entries. If the store is not held in main memory, the entries are counted in the store file, which is not read.
  • size: size of the store file in bytes; 0 if the store has not been written to disk yet.
  • persisted: indicates if a store file exists.
  • modified: last modification of the store file. The entry is only returned if the store is persisted.
Errors
ioThe store could not be read or written.
nameThe specified name is invalid.
Examples
store:info('sessions')?entries
Returns the number of entries of the sessions store.

store:read

Signature
store:read(  $name  as xs:string?  := '') as empty-sequence()create
SummaryRetrieves the default store from disk, or a custom store if $name is specified. This function can be called if the store file has been changed on disk by another process while a BaseX instance is running.
Errors
ioThe store could not be read or written.
nameThe specified name is invalid.

store:write

Signature
store:write(  $name  as xs:string?  := '') as empty-sequence()create
SummaryWrites the default store to disk, or to a custom store file if $name is specified. If a store is empty, its store file will be deleted. This function can be called to enforce the write operation before shutdown time.
Errors
ioThe store could not be read or written.
nameThe specified name is invalid.

store:delete

Signature
store:delete(  $name  as xs:string?  := '') as empty-sequence()create
SummaryDeletes the default store, or a custom store if $name is specified, both from main memory and from disk.
Errors
nameThe specified name is invalid.

store:close

Added: New function.

Signature
store:close(  $name  as xs:string?  := '') as empty-sequence()create
SummaryCloses a store and frees memory. Changes will be written to disk.

store:clear

Signature
store:clear() as empty-sequence()create
SummaryClears the default store and removes all other stores both from main memory and from disk.

Examples

Use Case 1: Create/update a system configuration in a running BaseX server instance
(: store an integer :)
store:put('version', 1),
(: retrieve existing or new value, store an element :)
let $license := store:get-or-put('license', fn() { 'free' })
let $type := if ($license = 'free') then 'Free' else 'Professional' 
return store:put('info', <info>{ $type } License</info>),
(: store a map :)
store:put('data', { 'year': 2022 }),
(: serialize configuration to disk :)
store:write()

The configuration can be requested by further operations, e.g. a client request:

store:get('version')

The store remains available across restarts of BaseX until it is cleared.

Use Case 2: Create index for fast lookup operations in the GUI
let $map := map:merge(
  for $country in db:get('factbook')//country
  for $religion in $country//religions
  group by $religion
  return map:entry($religion, data($country/@name))
)
return store:put('religions', $map)

A subsequent query can be used to access its contents:

store:get('religions')?Buddhism

Note that the store will eventually be written to disk unless it is deleted or cleared before closing the GUI.

Errors

CodeDescription
ioThe store could not be read or written.
nameThe specified name is invalid.

Changelog

Version 13.0Version 11.0
  • Added: New WRITESTORE option.
  • Updated: Values are compactified before being stored.
Version 10.0
  • Added: New module added.

⚡Generated with XQuery