Changes

Jump to navigation Jump to search
137 bytes added ,  18:36, 1 December 2023
m
Text replacement - "<syntaxhighlight lang="xquery">" to "<pre lang='xquery'>"
===XQuery===
With {{Announce|Version Since BaseX 10}}, the [[BaseX 10#Compilation|lock detection was has been fundamentally improved]] , by splitting compilation into multiple steps.
Local locks can be applied if it is possible after compile time to associate all database operations with static databases names:
{| class="wikitable"
|-valign="top"
! Query
! Description
|-valign="top"
| <code>//item</code>
| Read lock of the currently opened database
|-valign="top"
| <code>doc('factbook')</code>
| Read lock of the {{Code|factbook}} database
|-valign="top"
| <code>collection('documents/path/to/docs')</code>
| Read lock of the {{Code|documents}} database
|-valign="top"
| <code>delete nodes db:get('test')//*[@type = 'misc']</code>
| Write lock of the {{Code|test}} database
|-valign="top"
| <code>declare variables $db external;<br/>db:get($db)</code>
| Read lock of the database externally bound to {{Code|$db}}.
|-valign="top"
| <code>for $db in ('db1', 'db2')<br/>return db:get($db)</code>
| Read lock of {{Code|db1}} and {{Code|db2}}, as the query is [[XQuery OptimizationOptimizations#Loop Unrolling|unrolled at compile time]].|-valign="top"
| <code>let $db := 'test'<br/>return insert nodes <test/> into db:get($db)</code>
| Read lock of {{Code|test}}, as the [[XQuery Optimizations#Variable_Inlining|variable is inlined]] at compile time.
|-valign="top"
| <code>sum(1 to 100)</code>
| No lock required
|-valign="top"
| <code>declare variable $SIMULATE := true();<br/>if($SIMULATE) then <doc/> else db:get('doc')</code>
| No lock required, as the query is simplified to {{Code|<doc/>}} at compile time.
{| class="wikitable"
|-valign="top"
! Query
! Description
|-valign="top"
| <code>db:get(doc('test')/reference/text())</code>
| The name of the database to be opened will only be known at evaluation time.
|-valign="top"| <code>(1 to 100) ! db:get(concat($'db', .))</code>
| The {{Option|UNROLLLIMIT}} can be increased to generate 100 {{Code|db:get}} function calls and corresponding locks.
|}
In the following module, lock annotations are used to prevent concurrent write operations on the same file:
<syntaxhighlight pre lang="'xquery"'>
module namespace config = 'config';
file:write-text('config.txt', $data)
};
</syntaxhighlightpre>
Some explanations:
* If a query calls the a reading <code>configbasex:readlock</code> function, a read lock will be acquired for the user-defined {{Code|CONFIG}} lock string before query evaluation.* If an updating <code>configbasex:writelock</code> is called by a query, a write lock will be applied.* If another query calls a <code>configbasex:writelock</code>function, it will be queued until the first query is evaluated.
==Pragmas==
Locks can also be declared via pragmas:
<syntaxhighlight pre lang="'xquery"'>
update:output((# basex:lock CONFIG #) {
file:write('config.xml', <config/>)
})
</syntaxhighlightpre>
The write locks is enforced via the {{Code|Update|update:output}}.
Locks for the functions of a module can also be assigned via option declarations:
<syntaxhighlight pre lang="'xquery"'>
declare option basex:lock 'CONFIG';
update:output(file:write('config.xml', <config/>))
</syntaxhighlightpre>
Once again, a write lock is enforced.
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu