• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.55
/exist-core/src/main/java/org/exist/collections/LockedCollection.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 */
24
package org.exist.collections;
25

26
import org.exist.EXistException;
27
import org.exist.collections.triggers.TriggerException;
28
import org.exist.dom.QName;
29
import org.exist.dom.persistent.*;
30
import org.exist.security.Permission;
31
import org.exist.security.PermissionDeniedException;
32
import org.exist.security.Subject;
33
import org.exist.storage.*;
34
import org.exist.storage.io.VariableByteOutputStream;
35
import org.exist.storage.lock.Lock;
36
import org.exist.storage.lock.LockedDocumentMap;
37
import org.exist.storage.lock.ManagedCollectionLock;
38
import org.exist.storage.txn.Txn;
39
import org.exist.util.LockException;
40
import org.exist.util.MimeType;
41
import org.exist.xmldb.XmldbURI;
42
import org.w3c.dom.DocumentType;
43
import org.w3c.dom.Node;
44
import org.xml.sax.InputSource;
45
import org.xml.sax.SAXException;
46
import org.xml.sax.XMLReader;
47

48
import javax.annotation.Nullable;
49
import java.io.IOException;
50
import java.io.InputStream;
51
import java.util.Date;
52
import java.util.Iterator;
53
import java.util.List;
54

55
/**
56
 * Just a Delegate to a {@link Collection} which allows us to also hold a lock
57
 * lease which is released when {@link Collection#close()} is called. This
58
 * allows us to use ARM (Automatic Resource Management) e.g. try-with-resources
59
 * with eXist Collection objects
60
 *
61
 * @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a>
62
 */
63
public class LockedCollection implements Collection {
64
    private final ManagedCollectionLock managedCollectionLock;
65
    private final Collection collection;
66

67
    public LockedCollection(final ManagedCollectionLock managedCollectionLock, final Collection collection) {
1✔
68
        this.managedCollectionLock = managedCollectionLock;
1✔
69
        this.collection = collection;
1✔
70
    }
1✔
71

72
    //TODO(AR) if we decide that LockedCollection shouldn't implement Collection (but instead become a Tuple2<ManagedCollectionLock, Collection>) then drop this method
73
    /**
74
     * Given a Collection implementation returns the non-lock wrapped
75
     * implementation.
76
     *
77
     * @param collection a Collection object.
78
     * @return the actual non-lock wrapped Collection object.
79
     */
80
    public static Collection unwrapLocked(final Collection collection) {
81
        //TODO(AR) do we want to stay with LockedCollection implements Collection design {@link LockedCollection#getCollection()}
82
        if(collection instanceof LockedCollection) {
1✔
83
            // unwrap the locked collection
84
            return ((LockedCollection)collection).collection;
1✔
85
        } else {
86
            return collection;
1✔
87
        }
88
    }
89

90
    /**
91
     * Unlocks and Closes the Collection
92
     */
93
    @Override
94
    public void close() {
95
        collection.close();
1✔
96
        managedCollectionLock.close();
1✔
97
    }
1✔
98

99
    @Override
100
    public int getId() {
101
        return collection.getId();
1✔
102
    }
103

104
    @Override
105
    public XmldbURI getURI() {
106
        return collection.getURI();
1✔
107
    }
108

109
    @Override
110
    public void setPath(final XmldbURI path) {
111
        collection.setPath(path);
×
112
    }
×
113

114
    @Override
115
    public void setPath(final XmldbURI path, final boolean updateChildren) {
116
        collection.setPath(path, updateChildren);
1✔
117
    }
1✔
118

119
    @Deprecated
120
    @Override
121
    public CollectionMetadata getMetadata() {
122
        return collection.getMetadata();
×
123
    }
124

125
    @Override
126
    public Permission getPermissions() {
127
        return collection.getPermissions();
1✔
128
    }
129

130
    @Override
131
    public Permission getPermissionsNoLock() {
132
        return collection.getPermissionsNoLock();
1✔
133
    }
134

135
    @Override
136
    public void setPermissions(final DBBroker broker, final int mode) throws LockException, PermissionDeniedException {
137
        collection.setPermissions(broker, mode);
×
138
    }
×
139

140
    @Override
141
    public long getCreated() {
142
        return collection.getCreated();
1✔
143
    }
144

145
    @Override
146
    public void setCreated(final long timestamp) {
147
        collection.setCreated(timestamp);
×
148
    }
×
149

150
    @Override
151
    public CollectionConfiguration getConfiguration(final DBBroker broker) {
152
        return collection.getConfiguration(broker);
1✔
153
    }
154

155
    @Override
156
    public IndexSpec getIndexConfiguration(final DBBroker broker) {
157
        return collection.getIndexConfiguration(broker);
1✔
158
    }
159

160
    @Override
161
    public GeneralRangeIndexSpec getIndexByPathConfiguration(final DBBroker broker, final NodePath nodePath) {
162
        return collection.getIndexByPathConfiguration(broker, nodePath);
×
163
    }
164

165
    @Override
166
    public QNameRangeIndexSpec getIndexByQNameConfiguration(final DBBroker broker, final QName nodeName) {
167
        return collection.getIndexByQNameConfiguration(broker, nodeName);
×
168
    }
169

170
    @Override
171
    public boolean isTempCollection() {
172
        return collection.isTempCollection();
×
173
    }
174

175
    @Override
176
    public int getMemorySize() {
177
        return collection.getMemorySize();
×
178
    }
179

180
    @Override
181
    public int getMemorySizeNoLock() {
182
        return collection.getMemorySizeNoLock();
×
183
    }
184

185
    @Override
186
    public XmldbURI getParentURI() {
187
        return collection.getParentURI();
1✔
188
    }
189

190
    @Override
191
    public boolean isEmpty(final DBBroker broker) throws PermissionDeniedException {
192
        return collection.isEmpty(broker);
1✔
193
    }
194

195
    @Override
196
    public int getDocumentCount(final DBBroker broker) throws PermissionDeniedException {
197
        return collection.getDocumentCount(broker);
1✔
198
    }
199

200
    @Override
201
    @Deprecated
202
    public int getDocumentCountNoLock(final DBBroker broker) throws PermissionDeniedException {
203
        return collection.getDocumentCountNoLock(broker);
×
204
    }
205

206
    @Override
207
    public int getChildCollectionCount(final DBBroker broker) throws PermissionDeniedException {
208
        return collection.getChildCollectionCount(broker);
1✔
209
    }
210

211
    @Override
212
    public boolean hasDocument(final DBBroker broker, final XmldbURI name) throws PermissionDeniedException {
213
        return collection.hasDocument(broker, name);
1✔
214
    }
215

216
    @Override
217
    public boolean hasChildCollection(final DBBroker broker, final XmldbURI name) throws PermissionDeniedException, LockException {
218
        return collection.hasChildCollection(broker, name);
1✔
219
    }
220

221
    @Override
222
    @Deprecated
223
    public boolean hasChildCollectionNoLock(final DBBroker broker, final XmldbURI name) throws PermissionDeniedException {
224
        return collection.hasChildCollectionNoLock(broker, name);
×
225
    }
226

227
    @Override
228
    public void addCollection(final DBBroker broker, final Collection child) throws PermissionDeniedException, LockException {
229
        collection.addCollection(broker, child);
1✔
230
    }
1✔
231

232
    @Override
233
    public List<CollectionEntry> getEntries(final DBBroker broker) throws PermissionDeniedException, LockException, IOException {
234
        return collection.getEntries(broker);
×
235
    }
236

237
    @Override
238
    public CollectionEntry getChildCollectionEntry(final DBBroker broker, final String name) throws PermissionDeniedException, LockException, IOException {
239
        return collection.getChildCollectionEntry(broker, name);
1✔
240
    }
241

242
    @Override
243
    public CollectionEntry getResourceEntry(final DBBroker broker, final String name) throws PermissionDeniedException, LockException, IOException {
244
        return collection.getResourceEntry(broker, name);
1✔
245
    }
246

247
    @Override
248
    public void addDocument(final Txn transaction, final DBBroker broker, final DocumentImpl doc) throws PermissionDeniedException, LockException {
249
        collection.addDocument(transaction, broker, doc);
1✔
250
    }
1✔
251

252
    @Override
253
    public void unlinkDocument(final DBBroker broker, final DocumentImpl doc) throws PermissionDeniedException, LockException {
254
        collection.unlinkDocument(broker, doc);
×
255
    }
×
256

257
    @Override
258
    public Iterator<XmldbURI> collectionIterator(final DBBroker broker) throws PermissionDeniedException, LockException {
259
        return collection.collectionIterator(broker);
1✔
260
    }
261

262
    @Override
263
    @Deprecated
264
    public Iterator<XmldbURI> collectionIteratorNoLock(final DBBroker broker) throws PermissionDeniedException {
265
        return collection.collectionIteratorNoLock(broker);
1✔
266
    }
267

268
    @Override
269
    public Iterator<DocumentImpl> iterator(final DBBroker broker) throws PermissionDeniedException, LockException {
270
        return collection.iterator(broker);
1✔
271
    }
272

273
    @Override
274
    @Deprecated
275
    public Iterator<DocumentImpl> iteratorNoLock(final DBBroker broker) throws PermissionDeniedException {
276
        return collection.iteratorNoLock(broker);
1✔
277
    }
278

279
    @Override
280
    public List<Collection> getDescendants(final DBBroker broker, final Subject user) throws PermissionDeniedException {
281
        return collection.getDescendants(broker, user);
×
282
    }
283

284
    @Override
285
    public MutableDocumentSet allDocs(final DBBroker broker, final MutableDocumentSet docs, final boolean recursive) throws PermissionDeniedException, LockException {
286
        return collection.allDocs(broker, docs, recursive);
1✔
287
    }
288

289
    @Override
290
    public MutableDocumentSet allDocs(final DBBroker broker, final MutableDocumentSet docs, final boolean recursive, final LockedDocumentMap lockMap) throws PermissionDeniedException, LockException {
291
        return collection.allDocs(broker, docs, recursive, lockMap);
1✔
292
    }
293

294
    @Override
295
    public DocumentSet allDocs(final DBBroker broker, final MutableDocumentSet docs, final boolean recursive, final LockedDocumentMap lockMap, final Lock.LockMode lockType) throws LockException, PermissionDeniedException {
296
        return collection.allDocs(broker, docs, recursive, lockMap, lockType);
1✔
297
    }
298

299
    @Override
300
    public DocumentSet getDocuments(final DBBroker broker, final MutableDocumentSet docs) throws PermissionDeniedException, LockException {
301
        return collection.getDocuments(broker, docs);
1✔
302
    }
303

304
    @Override
305
    @Deprecated
306
    public DocumentSet getDocumentsNoLock(final DBBroker broker, final MutableDocumentSet docs) {
307
        return collection.getDocumentsNoLock(broker, docs);
×
308
    }
309

310
    @Override
311
    public DocumentSet getDocuments(final DBBroker broker, final MutableDocumentSet docs, final LockedDocumentMap lockMap, final Lock.LockMode lockType) throws LockException, PermissionDeniedException {
312
        return collection.getDocuments(broker, docs, lockMap, lockType);
×
313
    }
314

315
    @Override
316
    public DocumentImpl getDocument(final DBBroker broker, final XmldbURI name) throws PermissionDeniedException {
317
        return collection.getDocument(broker, name);
1✔
318
    }
319

320
    @Override
321
    @Deprecated
322
    public LockedDocument getDocumentWithLock(final DBBroker broker, final XmldbURI name) throws LockException, PermissionDeniedException {
323
        return collection.getDocumentWithLock(broker, name);
×
324
    }
325

326
    @Override
327
    public LockedDocument getDocumentWithLock(final DBBroker broker, final XmldbURI name, final Lock.LockMode lockMode) throws LockException, PermissionDeniedException {
328
        return collection.getDocumentWithLock(broker, name, lockMode);
1✔
329
    }
330

331
    @Override
332
    @Deprecated
333
    public DocumentImpl getDocumentNoLock(final DBBroker broker, final String rawPath) throws PermissionDeniedException {
334
        return collection.getDocumentNoLock(broker, rawPath);
×
335
    }
336

337
    @Override
338
    public void removeCollection(final DBBroker broker, final XmldbURI name) throws LockException, PermissionDeniedException {
339
        collection.removeCollection(broker, name);
1✔
340
    }
1✔
341

342
    @Override
343
    public void removeResource(final Txn transaction, final DBBroker broker, final DocumentImpl doc) throws PermissionDeniedException, LockException, IOException, TriggerException {
344
        collection.removeResource(transaction, broker, doc);
1✔
345
    }
1✔
346

347
    @Override
348
    public void removeXMLResource(final Txn transaction, final DBBroker broker, final XmldbURI name) throws PermissionDeniedException, TriggerException, LockException, IOException {
349
        collection.removeXMLResource(transaction, broker, name);
1✔
350
    }
1✔
351

352
    @Override
353
    public void removeBinaryResource(final Txn transaction, final DBBroker broker, final XmldbURI name) throws PermissionDeniedException, LockException, TriggerException {
354
        collection.removeBinaryResource(transaction, broker, name);
1✔
355
    }
1✔
356

357
    @Override
358
    public void removeBinaryResource(final Txn transaction, final DBBroker broker, final DocumentImpl doc) throws PermissionDeniedException, LockException, TriggerException {
359
        collection.removeBinaryResource(transaction, broker, doc);
1✔
360
    }
1✔
361

362
    @Override
363
    public void storeDocument(final Txn transaction, final DBBroker broker, final XmldbURI name, final InputSource source, @Nullable final MimeType mimeType) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException, IOException {
364
        broker.storeDocument(transaction, name, source, mimeType, collection);
1✔
365
    }
1✔
366

367
    @Override
368
    public void storeDocument(final Txn transaction, final DBBroker broker, final XmldbURI name, final InputSource source, @Nullable final MimeType mimeType, @Nullable final Date createdDate, @Nullable final Date lastModifiedDate, @Nullable final Permission permission, @Nullable final DocumentType documentType, @Nullable final XMLReader xmlReader) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException, IOException {
369
        collection.storeDocument(transaction, broker, name, source, mimeType, createdDate, lastModifiedDate, permission, documentType, xmlReader);
1✔
370
    }
1✔
371

372
    @Override
373
    public void storeDocument(final Txn transaction, final DBBroker broker, final XmldbURI name, final Node node, @Nullable final MimeType mimeType) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException, IOException {
374
        broker.storeDocument(transaction, name, node, mimeType, collection);
×
375
    }
×
376

377
    @Override
378
    public void storeDocument(final Txn transaction, final DBBroker broker, final XmldbURI name, final Node node, @Nullable final MimeType mimeType, @Nullable final Date createdDate, @Nullable final Date lastModifiedDate, @Nullable final Permission permission, @Nullable final DocumentType documentType, @Nullable final XMLReader xmlReader) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException, IOException {
379
        collection.storeDocument(transaction, broker, name, node, mimeType, createdDate, lastModifiedDate, permission, documentType, xmlReader);
1✔
380
    }
1✔
381

382
    @Deprecated
383
    @Override
384
    public IndexInfo validateXMLResource(final Txn transaction, final DBBroker broker, final XmldbURI name, final InputSource source) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException, IOException {
385
        return collection.validateXMLResource(transaction, broker, name, source);
×
386
    }
387

388
    @Deprecated
389
    @Override
390
    public IndexInfo validateXMLResource(final Txn transaction, final DBBroker broker, final XmldbURI name, final InputSource source, final XMLReader reader) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException, IOException {
391
        return collection.validateXMLResource(transaction, broker, name, source, reader);
×
392
    }
393

394
    @Deprecated
395
    @Override
396
    public IndexInfo validateXMLResource(final Txn transaction, final DBBroker broker, final XmldbURI name, final String data) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException, IOException {
397
        return collection.validateXMLResource(transaction, broker, name, data);
×
398
    }
399

400
    @Deprecated
401
    @Override
402
    public IndexInfo validateXMLResource(final Txn transaction, final DBBroker broker, final XmldbURI name, final Node node) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException, IOException {
403
        return collection.validateXMLResource(transaction, broker, name, node);
×
404
    }
405

406
    @Deprecated
407
    @Override
408
    public void store(final Txn transaction, final DBBroker broker, final IndexInfo info, final InputSource source) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException {
409
        collection.store(transaction, broker, info, source);
×
410
    }
×
411

412
    @Deprecated
413
    @Override
414
    public void store(final Txn transaction, final DBBroker broker, final IndexInfo info, final InputSource source, final XMLReader reader) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException {
415
        collection.store(transaction, broker, info, source, reader);
×
416
    }
×
417

418
    @Deprecated
419
    @Override
420
    public void store(final Txn transaction, final DBBroker broker, final IndexInfo info, final String data) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException {
421
        collection.store(transaction, broker, info, data);
×
422
    }
×
423

424
    @Deprecated
425
    @Override
426
    public void store(final Txn transaction, final DBBroker broker, final IndexInfo info, final Node node) throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException {
427
        collection.store(transaction, broker, info, node);
×
428
    }
×
429

430
    @Deprecated
431
    @Override
432
    public BinaryDocument validateBinaryResource(final Txn transaction, final DBBroker broker, final XmldbURI name) throws PermissionDeniedException, LockException, TriggerException, IOException {
433
        return collection.validateBinaryResource(transaction, broker, name);
×
434
    }
435

436
    @Deprecated
437
    @Override
438
    public BinaryDocument addBinaryResource(final Txn transaction, final DBBroker broker, final XmldbURI name, final InputStream is, final String mimeType, final long size, final Date created, final Date modified) throws EXistException, PermissionDeniedException, LockException, TriggerException, IOException {
439
        return collection.addBinaryResource(transaction, broker, name, is, mimeType, size, created, modified);
×
440
    }
441

442
    @Override
443
    public BinaryDocument addBinaryResource(final Txn transaction, final DBBroker broker, final XmldbURI name, final InputStream is, final String mimeType, final long size, final Date created, final Date modified, @Nullable final Permission permission) throws EXistException, PermissionDeniedException, LockException, TriggerException, IOException {
444
        return collection.addBinaryResource(transaction, broker, name, is, mimeType, size, created, modified, permission);
×
445
    }
446

447
    @Override
448
    @Deprecated
449
    public BinaryDocument addBinaryResource(final Txn transaction, final DBBroker broker, final XmldbURI name, final byte[] data, final String mimeType) throws EXistException, PermissionDeniedException, LockException, TriggerException, IOException {
450
        return collection.addBinaryResource(transaction, broker, name, data, mimeType);
×
451
    }
452

453
    @Override
454
    @Deprecated
455
    public BinaryDocument addBinaryResource(final Txn transaction, final DBBroker broker, final XmldbURI name, final byte[] data, final String mimeType, final Date created, final Date modified) throws EXistException, PermissionDeniedException, LockException, TriggerException, IOException {
456
        return collection.addBinaryResource(transaction, broker, name, data, mimeType, created, modified);
×
457
    }
458

459
    @Deprecated
460
    @Override
461
    public BinaryDocument addBinaryResource(final Txn transaction, final DBBroker broker, final XmldbURI name, final InputStream is, final String mimeType, final long size) throws EXistException, PermissionDeniedException, LockException, TriggerException, IOException {
462
        return collection.addBinaryResource(transaction, broker, name, is, mimeType, size);
×
463
    }
464

465
    @Deprecated
466
    @Override
467
    public BinaryDocument addBinaryResource(final Txn transaction, final DBBroker broker, final BinaryDocument blob, final InputStream is, final String mimeType, final long size, final Date created, final Date modified) throws EXistException, PermissionDeniedException, LockException, TriggerException, IOException {
468
        return collection.addBinaryResource(transaction, broker, blob, is, mimeType, size, created, modified);
×
469
    }
470

471
    @Deprecated
472
    @Override
473
    public BinaryDocument addBinaryResource(final Txn transaction, final DBBroker broker, final BinaryDocument blob, final InputStream is, final String mimeType, final long size, final Date created, final Date modified, final DBBroker.PreserveType preserve) throws EXistException, PermissionDeniedException, LockException, TriggerException, IOException {
474
        return collection.addBinaryResource(transaction, broker, blob, is, mimeType, size, created, modified, preserve);
×
475
    }
476

477
    @Override
478
    public void serialize(final VariableByteOutputStream outputStream) throws IOException, LockException {
479
        collection.serialize(outputStream);
1✔
480
    }
1✔
481

482
    @Override
483
    public int compareTo(final Collection o) {
484
        return collection.compareTo(o);
×
485
    }
486
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc