Right, that’s the approach I would recommend.

But you can use the JSON representation to store a map as binary resource:

(: store map :)
map { 'a': 'b' }
! db:store('map', 'map.txt', json:serialize(.))
(: retrieve map :)
db:retrieve('map', 'map.txt')
=> bin:decode-string()
=> parse-json()

It can also be stored as XML resource by converting it to one of our JSON representations [1]:

(: store map :)
let $map := map { 'a': 'b' }
let $xml := json:parse(json:serialize($map))
return db:add('map', $xml, 'map.xml')

(: retrieve map :)
db:open('map', 'map.xml')
=> json:serialize()
=> json:parse(map { 'format': 'xquery' })

In both cases, the full map data will be processed and converted, so it will take some additional time. However, if you have numerous lookups in your query, it might still be faster than the initial XML proposal.

Jonathan Robie <jonathan.robie@gmail.com> schrieb am Do., 24. März 2022, 19:10:
Hi Christian,

I think the database you describe looks like this:

<words>
  <word n="1">one</word>
  <word n="2">two</word>
  <word n="3">three</word>
  <word n="4">four</word>
  <word n="5">five</word>
  <word n="6">six</word>
  <word n="7">seven</word>
  <word n="8">eight</word>
  <word n="9">nine</word>
  <word n="10">ten</word>
</words>

So I always use XML for this?  There's no persistent representation of the map data structure? 

Jonathan

On Thu, Mar 24, 2022 at 2:02 PM Christian Grün <christian.gruen@gmail.com> wrote:
Hi Jonathan,

You can create databases that contain key/value pairs:

let $words := <words>{
  for $i in 1 to 10
  return <word n='{ $i }'>{ format-integer($i, 'w') }</word>
}</words>
return db:create('words', $words, 'words.xml')

If you look up values in that database …

for $n in (1 to 10) ! string()
return db:open('words')//word[@n = $n] ! data()

… the text index will be utilized, and your query will be rewritten as follows:

(1 to 10) ! data(db:attribute("words", string(.))/
  self::attribute(n)/parent::word)

If you don’t want to rely on the rewritings of the query optimizer,
you can directly use db:attribute.

Best,
Christian