basex-lsp/webapp/lsp/storex.xqm
2026-02-22 12:17:47 +00:00

68 lines
No EOL
1.9 KiB
Text

(:~
Manage a store for websocket info.It is named as the websocket $sid
:)
module namespace storex = 'storex';
(: cache has index :)
declare variable $storex:rules:={
"doc":{
"description":"A text document"
},
"parse":{
"description":"XML parse tree" ,
"depends":"text",
"calc": "xqparse"
},
"symbols":{
"description":"LSP symbols",
"depends":"parse",
"calc": "symbols"
}
};
(:~ in store $sid, and key $uri get value of $property :)
declare function storex:get($uri as xs:string,$sid as xs:string,$property as xs:string)
{
let $id:=storex:id($uri,$sid )
let $property:=storex:property($property)
return store:get($property || $id,$sid)
otherwise error(xs:QName("storex:get"),`property unknown: '{$property}'`)
};
(:~ get value of $property :)
declare function storex:put($uri as xs:string,
$sid as xs:string,
$property as xs:string,
$value as item())
{
let $id:=storex:id($uri,$sid ,true() )
let $property:=storex:property($property)
return store:put($property || $id,$value,$sid)
};
(:~ key for $sid and $uri, error if not found unless $add.
if $add then add entry for uri
:)
declare function storex:id($uri as xs:string,$sid as xs:string,$add as xs:boolean:=false())
{
let $index:=store:get-or-put("index",fn(){{}},$sid)
return if(map:contains($index,$uri))
then $index?uri
else if($add)
then let $id:=random:uuid()
return ($id,store:put("index",map:put($index,$uri,$id),$sid))
else error(xs:QName("storex:get"),`Uri not found: '{$uri}'`)
};
(:~ key for $sid and $uri, error if not found unless $add.
if $add then add entry for uri
:)
declare function storex:property($property as xs:string)
as xs:string
{
if(map:contains($storex:rules,$property))
then $property
else error(xs:QName("storex:get"),`property not in rules: '{$property}'`)
};