-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib-io.xqy
513 lines (474 loc) · 13.6 KB
/
lib-io.xqy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
xquery version "1.0-ml";
(:
: cq
:
: Copyright (c) 2002-2011 MarkLogic Corporation. All Rights Reserved.
:
: Licensed under the Apache License, Version 2.0 (the "License");
: you may not use this file except in compliance with the License.
: You may obtain a copy of the License at
:
: http://www.apache.org/licenses/LICENSE-2.0
:
: Unless required by applicable law or agreed to in writing, software
: distributed under the License is distributed on an "AS IS" BASIS,
: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
: See the License for the specific language governing permissions and
: limitations under the License.
:
: The use of the Apache License does not indicate that this project is
: affiliated with the Apache Software Foundation.
:)
(:
: This library provides routines for reading and writing server modules,
: whether stored on the filesystem or in a database.
:)
module namespace io = "com.marklogic.developer.cq.io";
declare default function namespace "http://www.w3.org/2005/xpath-functions";
import module namespace admin = "http://marklogic.com/xdmp/admin"
at "/MarkLogic/admin.xqy";
import module namespace su = "com.marklogic.developer.cq.security"
at "lib-security-utils.xqy";
import module namespace x = "com.marklogic.developer.cq.xquery"
at "lib-xquery.xqy";
declare namespace dir = "http://marklogic.com/xdmp/directory";
declare option xdmp:mapping "false";
(:~ @private :)
declare variable $io:MODULES-DB as xs:unsignedLong :=
xdmp:modules-database()
;
(:~ @private :)
declare variable $io:MODULES-ROOT as xs:string :=
(: life is easier if the root does not end in "/" :)
let $root := xdmp:modules-root()
return
if (ends-with($root, "/"))
then substring($root, 1, string-length($root) - 1)
else $root
;
(:~ @private :)
declare variable $io:EVAL-OPTIONS as element() :=
<options xmlns="xdmp:eval">{
element database { $io:MODULES-DB }
}</options>
;
(:~ read the contents of a path :)
declare function io:read($path as xs:string)
as document-node()?
{
let $path := io:canonicalize($path)
return
if ($io:MODULES-DB eq 0)
then io:read-fs($path)
else io:read-db($path)
};
(:~ write a document-node to a path :)
declare function io:write($path as xs:string, $new as document-node())
as empty-sequence()
{
let $path := io:canonicalize($path)
return
if ($io:MODULES-DB eq 0)
then io:write-fs($path, $new)
else io:write-db($path, $new)
};
(:~ list contents of a directory path :)
declare function io:list($path as xs:string)
as document-node()*
{
(: TODO pagination :)
let $path := io:canonicalize($path)
return
if ($io:MODULES-DB eq 0)
then io:list-fs($path)
else io:list-db($path)
};
(:~ delete a path :)
declare function io:delete($path as xs:string)
as empty-sequence()
{
let $path := io:canonicalize($path)
return
if ($io:MODULES-DB eq 0)
then io:delete-fs($path)
else io:delete-db($path)
};
(:~ return true if path exists :)
declare function io:exists($path as xs:string)
as xs:boolean
{
let $path := io:canonicalize($path)
return
if ($io:MODULES-DB eq 0)
then io:exists-fs($path)
else io:exists-db($path)
};
(:~ release a lock :)
declare function io:lock-release($path as xs:string)
as empty-sequence()
{
let $path := io:canonicalize($path)
return
if ($io:MODULES-DB eq 0)
then io:lock-release-fs($path)
else io:lock-release-db($path)
};
(:~ acquire a lock :)
declare function io:lock-acquire($path as xs:string, $owner as xs:string)
as empty-sequence()
{
io:lock-acquire($path, (), (), $owner, xs:unsignedLong(300))
};
(:~ acquire a lock :)
declare function io:lock-acquire(
$path as xs:string, $scope as xs:string?,
$depth as xs:string?, $owner as item()?,
$timeout as xs:unsignedLong?)
as empty-sequence()
{
if ($path eq '' or ends-with($path, '/'))
then error((), 'IO-BADPATH', text { $path })
else (),
let $path := io:canonicalize($path)
(: a pox on varargs - anyway, we can spec our own defaults :)
let $scope := ($scope[. = ("exclusive", "shared")], "exclusive")[1]
let $depth := ($depth[. = ("0", "infinity")], "0")[1]
let $owner := ($owner, xdmp:get-current-user())[1]
(: NB empty timeout is considered infinite :)
return
if ($io:MODULES-DB eq 0)
then io:lock-acquire-fs($path, $scope, $depth, $owner, $timeout)
else io:lock-acquire-db($path, $scope, $depth, $owner, $timeout)
};
(:~ list locks :)
declare function io:document-locks($paths as xs:string*)
as document-node()*
{
let $paths := for $path in $paths return io:canonicalize($path)
return
if ($io:MODULES-DB eq 0)
then io:document-locks-fs($paths)
else io:document-locks-db($paths)
};
(:~ @private :)
declare function io:exists-db($uri as xs:string)
as xs:boolean
{
xdmp:eval(
'xquery version "1.0-ml";
declare variable $URI as xs:string external;
xdmp:exists(doc($URI))',
(xs:QName('URI'), $uri),
$io:EVAL-OPTIONS
)
};
(:~ @private :)
declare function io:exists-fs($path as xs:string)
as xs:boolean
{
try {
exists(xdmp:document-get($path))
} catch ($ex) {
false(),
if ($ex/error:code eq 'SVC-FILOPN') then ()
else xdmp:log(text {
"io:exists-fs:", normalize-space(xdmp:quote($ex)) })
}
};
(:~ @private :)
declare function io:delete-db($uri as xs:string)
as empty-sequence()
{
xdmp:eval(
'xquery version "1.0-ml";
declare variable $URI as xs:string external;
xdmp:document-delete($URI)',
(xs:QName('URI'), $uri),
$io:EVAL-OPTIONS
)
};
(:~ @private :)
declare function io:delete-fs($path as xs:string)
as empty-sequence()
{
(: TODO we cannot delete the document, so save an empty text node. :)
xdmp:save($path, text { '' })
};
(:~ @private :)
declare function io:list-db($uri as xs:string)
as document-node()*
{
xdmp:eval(
'xquery version "1.0-ml";
declare variable $URI as xs:string external;
xdmp:directory($URI, "1")',
(xs:QName('URI'), $uri),
$io:EVAL-OPTIONS
)
};
(:~ @private :)
declare function io:list-fs($path as xs:string)
as document-node()*
{
(: ignore any files that are not xml :)
for $p as xs:string in xdmp:filesystem-directory($path)/dir:entry
[ dir:type eq "file" ]/dir:pathname
where ends-with($p, '.xml')
return try {
xdmp:document-get(
$p,
<options xmlns="xdmp:document-get">{
element format { 'xml' } }</options>
) }
catch ($ex) {
(: log and ignore :)
xdmp:log(
text {
'file is not XML:', $p, normalize-space(xdmp:quote($ex)) },
'debug')
}
};
(:~ @private :)
declare function io:canonicalize($path as xs:string)
as xs:string
{
concat(
$io:MODULES-ROOT,
"/"[not(starts-with($path, "/"))],
$path
)
};
(:~ @private :)
declare function io:lock-path-fs($path as xs:string)
as xs:string
{
(: primitive, yet messy :)
concat($path, ".lock")
};
(:~ @private :)
declare function io:read-db($uri as xs:string)
as document-node()?
{
xdmp:eval(
'xquery version "1.0-ml";
declare variable $URI as xs:string external;
doc($URI)',
(xs:QName('URI'), $uri),
$io:EVAL-OPTIONS
)
};
(:~ @private :)
declare function io:read-fs($path as xs:string)
as document-node()?
{
if (ends-with($path, "/")) then () else try {
(: hack - possibly a problem with ntfs streams? :)
xdmp:document-get(
$path,
<options xmlns="xdmp:document-get">
<format>xml</format>
</options>
)[1]
} catch ($ex) {
if ($ex/error:code eq 'SVC-FILOPN') then ()
else xdmp:log(text {
"io:read-fs:", normalize-space(xdmp:quote($ex)) })
}
};
(:~ @private :)
declare function io:write-db($uri as xs:string, $new as document-node())
as empty-sequence()
{
xdmp:eval(
'xquery version "1.0-ml";
declare variable $URI as xs:string external;
declare variable $NEW as document-node() external;
declare variable $EXISTS as xs:boolean := xdmp:exists(doc($URI));
xdmp:document-insert(
$URI, $NEW,
if ($EXISTS) then xdmp:document-get-permissions($URI)
else xdmp:default-permissions(),
if ($EXISTS) then xdmp:document-get-collections($URI)
else xdmp:default-collections(),
if ($EXISTS) then xdmp:document-get-quality($URI)
else 0
)',
(xs:QName('URI'), $uri, xs:QName('NEW'), $new),
$io:EVAL-OPTIONS
)
};
(:~ @private :)
declare function io:write-fs($path as xs:string, $new as document-node())
as empty-sequence()
{
xdmp:save($path, $new)
};
(:~ @private :)
declare function io:lock-release-db($uri as xs:string)
as empty-sequence()
{
xdmp:eval(
'xquery version "1.0-ml";
declare variable $URI as xs:string external;
xdmp:lock-release($URI)',
(xs:QName('URI'), $uri),
$io:EVAL-OPTIONS
)
};
(:~ @private :)
declare function io:lock-release-fs($path as xs:string)
as empty-sequence()
{
(: filesystem lock-release - check and release.
: This might do ok with multiple locks.
:)
let $old := io:document-locks($path)/lock:lock
let $locks := $old/lock:active-locks/lock:active-lock
let $check :=
if (exists($locks)) then ()
else error((), "IO-NOTLOCKED", text { $path, "is not locked" })
let $check :=
if ($su:USER-IS-ADMIN or $locks[sec:user-id eq $su:USER-ID]) then ()
else error(
(), "IO-NOUSER", text {
$path, "is not locked by", $su:USER, $su:USER-ID,
"existing locks are held by", data($locks/sec:user-id)
}
)
let $path := io:canonicalize($path)
let $lock-path := io:lock-path-fs($path)
return
if ($su:USER-IS-ADMIN or empty($locks[ sec:user-id ne $su:USER-ID ]))
then io:delete-fs($lock-path)
else io:write-fs($lock-path, document {
element {node-name($old)} {
$old/@*,
$old/node()[ node-name(.) ne xs:QName("lock:active-locks") ],
element lock:active-locks {
$old/lock:active-locks/@*,
$old/lock:active-locks/node()
[ node-name(.) ne xs:QName("lock:active-locks") ],
$locks[ sec:user-id ne $su:USER-ID ]
}
}
} )
};
(:~ @private :)
declare function io:lock-acquire-db(
$uri as xs:string, $scope as xs:string,
$depth as xs:string, $owner as item(),
$timeout as xs:unsignedLong)
as empty-sequence()
{
xdmp:eval(
'xquery version "1.0-ml";
declare variable $URI as xs:string external;
declare variable $SCOPE as xs:string external;
declare variable $DEPTH as xs:string external;
declare variable $OWNER as xs:string external;
declare variable $TIMEOUT as xs:unsignedLong external;
xdmp:lock-acquire($URI, $SCOPE, $DEPTH, $OWNER, $TIMEOUT)',
(xs:QName('URI'), $uri, xs:QName('SCOPE'), $scope,
xs:QName('DEPTH'), $depth, xs:QName('OWNER'), $owner,
xs:QName('TIMEOUT'), $timeout),
$io:EVAL-OPTIONS
)
};
declare function io:get-conflicting-locks(
$uri as xs:string, $limit as xs:integer?, $owner as xs:string
)
as element(lock:active-lock)*
{
(: a lock is conflicting if...
: 1. it is a write-lock
: 2. the owner is not $owner
: 3. it has an active-lock which has not expired
:)
let $now := x:get-epoch-seconds()
let $locks :=
for $c in io:document-locks($uri)
/lock:lock[lock:lock-type eq 'write']
/lock:active-locks/lock:active-lock
[ lock:owner ne $owner ]
let $timeout := data($c/lock:timeout)
let $expires :=
if (empty($timeout)) then (1 + $now)
else ($c/lock:timestamp + xs:unsignedLong($timeout))
where $expires ge $now
(: we only care about the lock(s) that expires last.
: an empty timeout is considered infinite.
:)
order by empty($timeout) descending, $expires descending
return $c
return
if (empty($limit)) then $locks else subsequence($locks, 1, $limit)
};
(:~ @private :)
declare function io:lock-acquire-fs(
$path as xs:string, $scope as xs:string,
$depth as xs:string, $owner as item(),
$timeout as xs:unsignedLong?)
as empty-sequence()
{
(: NB: the caller is responsible for checking our arguments! :)
(: TODO does not handle multiple locks, shared vs exclusive :)
(: first... can we lock this path?
: admin always can... others can only break their own locks.
:)
let $conflict :=
if ($su:USER-IS-ADMIN) then ()
else io:document-locks($path)/lock:lock/lock:active-locks
/lock:active-lock[ sec:user-id ne $su:USER-ID ]
let $check :=
if (not($conflict)) then ()
else error(
(), "IO-LOCKED",
text { $path, "is locked by", $conflict/lock:owner }
)
let $lock := document {
element lock:lock {
element lock:lock-type { "write" },
element lock:lock-scope { $scope },
element lock:active-locks {
element lock:active-lock {
element lock:depth { $depth },
element lock:owner { $owner },
element lock:timeout { $timeout },
element lock:lock-token {
concat(
'http://marklogic.com/xdmp/locks/',
xdmp:integer-to-hex(xdmp:random())
)
},
element lock:timestamp { x:get-epoch-seconds() },
element sec:user-id { $su:USER-ID }
}
}
}
}
let $path := io:lock-path-fs($path)
return io:write-fs($path, $lock)
};
(:~ @private :)
declare function io:document-locks-db($uris as xs:string*)
as document-node()*
{
xdmp:eval(
'xquery version "1.0-ml";
declare variable $URIS-SSV as xs:string external;
declare variable $URIS as xs:string+ := tokenize($URIS-SSV, "\s+");
xdmp:document-locks($URIS)',
(xs:QName('URIS-SSV'), string-join($uris, ' ')),
$io:EVAL-OPTIONS
)
};
(:~ @private :)
declare function io:document-locks-fs($paths as xs:string*)
as document-node()*
{
for $path in $paths
let $lock-path := io:lock-path-fs($path)
let $fs := io:read-fs($lock-path)
return $fs
};
(: lib-io.xqy :)