• 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

95.05
/exist-core/src/main/java/org/exist/storage/FluentBrokerAPI.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.storage;
25

26
import com.evolvedbinary.j8fu.tuple.Tuple2;
27
import com.evolvedbinary.j8fu.tuple.Tuple3;
28
import org.exist.EXistException;
29
import org.exist.collections.Collection;
30
import org.exist.dom.persistent.DocumentImpl;
31
import org.exist.dom.persistent.LockedDocument;
32
import org.exist.security.PermissionDeniedException;
33
import org.exist.storage.lock.Lock.LockMode;
34
import org.exist.util.LockException;
35
import org.exist.xmldb.XmldbURI;
36

37
import javax.annotation.Nullable;
38
import java.util.Optional;
39
import java.util.function.BiFunction;
40
import java.util.function.Function;
41

42
/**
43
 * A fluent lambda API for working
44
 * with Documents and Collections.
45
 *
46
 * @author <a href="mailto:adam@evolvedbinary.com">Adam Retter</a>
47
 */
48
public class FluentBrokerAPI {
×
49

50
    /**
51
     * Convenience function for constructing an {@link XmldbURI}.
52
     *
53
     * @param uri a string expressing a URI
54
     *
55
     * @return an XmldbURI
56
     */
57
    public static XmldbURI uri(final String uri) {
58
        return XmldbURI.create(uri);
1✔
59
    }
60

61
    /**
62
     * Creates a builder which can be used
63
     * for describing a series of operations
64
     * to be carried out with a broker
65
     *
66
     * @param brokerPool The broker pool to use brokers from
67
     *
68
     * @return a builder
69
     */
70
    public static FluentBrokerAPIBuilder builder(final BrokerPool brokerPool) {
71
        return new FluentBrokerAPIBuilder(brokerPool);
1✔
72
    }
73

74
    public static class FluentBrokerAPIBuilder {
75
        private final BrokerPool brokerPool;
76

77
        private FluentBrokerAPIBuilder(final BrokerPool brokerPool) {
1✔
78
            this.brokerPool = brokerPool;
1✔
79
        }
1✔
80

81
        /**
82
         * A Collection on which to perform operations.
83
         *
84
         * @param collectionUri The URI of the Collection.
85
         * @param collectionLockMode The mode under which the Collection should be locked.
86
         *
87
         * @return a builder
88
         */
89
        public FluentBrokerAPIBuilder_Col1 withCollection(final XmldbURI collectionUri, final LockMode collectionLockMode) {
90
            return new FluentBrokerAPIBuilder_Col1(collectionUri, collectionLockMode);
1✔
91
        }
92

93
//        public FluentBrokerAPIBuilder_Col1 withCollections(final Tuple2<XmldbURI, LockMode>... collectionsAndLockModes) {
94
//            return new FluentBrokerAPIBuilder_ColN(collectionsAndLockModes);
95
//        }
96

97
        public class FluentBrokerAPIBuilder_Col1 {
98
            private final XmldbURI collectionUri;
99
            private final LockMode collectionLockMode;
100

101
            private FluentBrokerAPIBuilder_Col1(final XmldbURI collectionUri, final LockMode collectionLockMode) {
1✔
102
                this.collectionUri = collectionUri;
1✔
103
                this.collectionLockMode = collectionLockMode;
1✔
104
            }
1✔
105

106
            /**
107
             * An operation to perform on a Collection.
108
             *
109
             * The operation will be executed after the Collection is retrieved and locked.
110
             *
111
             * @param <CR> The return type of the {@code collectionOp}
112
             * @param collectionOp The function to execute against the Collection
113
             *
114
             * @return a builder.
115
             */
116
            public <CR> FluentBrokerAPIBuilder_Col1_Exec<CR> execute(final Function<Collection, CR> collectionOp) {
117
                return new FluentBrokerAPIBuilder_Col1_Exec<>(collectionOp);
1✔
118
            }
119

120
            /**
121
             * A Document within the Collection on which to perform an operation.
122
             *
123
             * @param documentLookupFun A function which given a Collection, returns a tuple of Document Name and Lock Mode
124
             *
125
             * @return a builder.
126
             */
127
            public FluentBrokerAPIBuilder_Col1_NoExec_Doc1 withDocument(final Function<Collection, Tuple2<XmldbURI, LockMode>> documentLookupFun) {
128
                return new FluentBrokerAPIBuilder_Col1_NoExec_Doc1(documentLookupFun);
1✔
129
            }
130

131
            public class FluentBrokerAPIBuilder_Col1_Exec<CR> {
132
                private Function<Collection, CR> collectionOp;
133

134
                private FluentBrokerAPIBuilder_Col1_Exec(final Function<Collection, CR> collectionOp) {
1✔
135
                    this.collectionOp = collectionOp;
1✔
136
                }
1✔
137

138
                /**
139
                 * A Document within the Collection on which to perform an operation.
140
                 *
141
                 * @param documentLookupFun A function which given a Collection, returns a tuple of Document Name and Lock Mode
142
                 *
143
                 * @return a builder.
144
                 */
145
                public FluentBrokerAPIBuilder_Col1_Exec_Doc1 withDocument(final Function<Collection, Tuple2<XmldbURI, LockMode>> documentLookupFun) {
146
                    return new FluentBrokerAPIBuilder_Col1_Exec_Doc1(documentLookupFun);
1✔
147
                }
148

149
                /**
150
                 * Executes the Collection operation and returns the result.
151
                 *
152
                 * @throws PermissionDeniedException in case user does not have sufficent rights
153
                 * @throws LockException in case the database is locked
154
                 * @throws EXistException in case of a database error
155
                 * @return The result of the Collection operation.
156
                 */
157
                public CR doAll() throws PermissionDeniedException, LockException, EXistException {
158
                    final Tuple3<Optional<CR>, Optional<Void>, Optional<Void>> result = FluentBrokerAPIBuilder.this.doAll(collectionUri, collectionLockMode, Optional.of(collectionOp), null, Optional.empty(), Optional.empty());
1✔
159
                    return result._1.get();
1✔
160
                }
161

162
                public class FluentBrokerAPIBuilder_Col1_Exec_Doc1 {
163
                    private final Function<Collection, Tuple2<XmldbURI, LockMode>> documentLookupFun;
164

165
                    private FluentBrokerAPIBuilder_Col1_Exec_Doc1(final Function<Collection, Tuple2<XmldbURI, LockMode>> documentLookupFun) {
1✔
166
                        this.documentLookupFun = documentLookupFun;
1✔
167
                    }
1✔
168

169
                    /**
170
                     * An operation to perform on a Collection and Document.
171
                     *
172
                     * The operation will be executed after both the Collection and Document are retrieved and locked.
173
                     *
174
                     * @param <CDR> The return type of the {@code collectionDocumentOp}
175
                     * @param collectionDocumentOp The function to execute against the Collection and Document
176
                     *
177
                     * @return a builder.
178
                     */
179
                    public <CDR> FluentBrokerAPIBuilder_Col1_Exec_Doc1_Exec<CDR> execute(final BiFunction<Collection, DocumentImpl, CDR> collectionDocumentOp) {
180
                        return new FluentBrokerAPIBuilder_Col1_Exec_Doc1_Exec<>(collectionDocumentOp);
1✔
181
                    }
182

183
                    /**
184
                     * Releases the Collection.
185
                     *
186
                     * @return a builder.
187
                     */
188
                    public FluentBrokerAPIBuilder_Col1_Exec_Doc1_NoExec withoutCollection() {
189
                        return new FluentBrokerAPIBuilder_Col1_Exec_Doc1_NoExec();
1✔
190
                    }
191

192
                    public class FluentBrokerAPIBuilder_Col1_Exec_Doc1_Exec<CDR> {
193
                        private final BiFunction<Collection, DocumentImpl, CDR> collectionDocumentOp;
194

195
                        private FluentBrokerAPIBuilder_Col1_Exec_Doc1_Exec(final BiFunction<Collection, DocumentImpl, CDR> collectionDocumentOp) {
1✔
196
                            this.collectionDocumentOp = collectionDocumentOp;
1✔
197
                        }
1✔
198

199
                        /**
200
                         * Releases the Collection.
201
                         *
202
                         * @return a builder.
203
                         */
204
                        public FluentBrokerAPIBuilder_Col1_Exec_Doc1_Exec_Doc1 withoutCollection() {
205
                            return new FluentBrokerAPIBuilder_Col1_Exec_Doc1_Exec_Doc1();
1✔
206
                        }
207

208
                        /**
209
                         * Executes the Collection operation, the Collection and Document operation, and returns the results.
210
                         *
211
                         * @return A tuple, where the first entry is the result of the Collection operation,
212
                         *     and the second entry is the result of the Collection and Document operation.
213
                         *
214
                         * @throws PermissionDeniedException in case user does not have sufficent rights
215
                         * @throws LockException in case the database is locked
216
                         * @throws EXistException in case of a database error
217

218
                         */
219
                        public Tuple2<CR, CDR> doAll() throws PermissionDeniedException, LockException, EXistException {
220
                            final Tuple3<Optional<CR>, Optional<CDR>, Optional<Void>> result = FluentBrokerAPIBuilder.this.doAll(collectionUri, collectionLockMode, Optional.of(collectionOp), documentLookupFun, Optional.of(collectionDocumentOp), Optional.empty());
1✔
221
                            return new Tuple2<>(result._1.get(), result._2.get());
1✔
222
                        }
223

224
                        public class FluentBrokerAPIBuilder_Col1_Exec_Doc1_Exec_Doc1 {
225
                            private FluentBrokerAPIBuilder_Col1_Exec_Doc1_Exec_Doc1() {}
1✔
226

227
                            /**
228
                             * An operation to perform on a Document from the Collection.
229
                             *
230
                             * The operation will be executed after the Document is retrieved and locked, and after the Collection was released.
231
                             *
232
                             * @param <DR> The return type of the {@code documentOp}
233
                             * @param documentOp The function to execute against the Document
234
                             *
235
                             * @return a builder.
236
                             */
237
                            public <DR> FluentBrokerAPIBuilder_Col1_Exec_Doc1_Exec_Doc1_Exec<DR> execute(final Function<DocumentImpl, DR> documentOp) {
238
                                return new FluentBrokerAPIBuilder_Col1_Exec_Doc1_Exec_Doc1_Exec<>(documentOp);
1✔
239
                            }
240
                        }
241

242
                        public class FluentBrokerAPIBuilder_Col1_Exec_Doc1_Exec_Doc1_Exec<DR> {
243
                            private final Function<DocumentImpl, DR> documentOp;
244

245
                            private FluentBrokerAPIBuilder_Col1_Exec_Doc1_Exec_Doc1_Exec(final Function<DocumentImpl, DR> documentOp) {
1✔
246
                                this.documentOp = documentOp;
1✔
247
                            }
1✔
248

249
                            /**
250
                             * Executes the Collection operation, the Collection and Document operation, the Document Operation, and returns the results.
251
                             *
252
                             * @return A triple, where the first entry is the result of the Collection operation,
253
                             *     the second entry is the result of the Collection and Document operation,
254
                             *     and the third entry is the result of the Document operation.
255
                             * @throws PermissionDeniedException in case user does not have sufficent rights
256
                             * @throws LockException in case the database is locked
257
                             * @throws EXistException in case of a database error
258

259
                             */
260
                            public Tuple3<CR, CDR, DR> doAll() throws PermissionDeniedException, LockException, EXistException {
261
                                final Tuple3<Optional<CR>, Optional<CDR>, Optional<DR>> result = FluentBrokerAPIBuilder.this.doAll(collectionUri, collectionLockMode, Optional.of(collectionOp), documentLookupFun, Optional.of(collectionDocumentOp), Optional.of(documentOp));
1✔
262
                                return new Tuple3<>(result._1.get(), result._2.get(), result._3.get());
1✔
263
                            }
264
                        }
265
                    }
266

267
                    public class FluentBrokerAPIBuilder_Col1_Exec_Doc1_NoExec {
268
                        private FluentBrokerAPIBuilder_Col1_Exec_Doc1_NoExec() {}
1✔
269

270
                        /**
271
                         * An operation to perform on a Document from the Collection.
272
                         *
273
                         * The operation will be executed after the Document is retrieved and locked, and after the Collection was released.
274
                         *
275
                         * @param <DR> The return type of the {@code documentOp}
276
                         * @param documentOp The function to execute against the Document
277
                         *
278
                         * @return a builder.
279
                         */
280
                        public <DR> FluentBrokerAPIBuilder_Col1_Exec_Doc1_NoExec_Exec<DR> execute(final Function<DocumentImpl, DR> documentOp) {
281
                            return new FluentBrokerAPIBuilder_Col1_Exec_Doc1_NoExec_Exec<>(documentOp);
1✔
282
                        }
283

284
                        /**
285
                         * Executes the Collection operation and returns the result.
286
                         *
287
                         * @return The result of the Collection operation.
288
                         * @throws PermissionDeniedException if user has not sufficient rights
289
                         * @throws LockException in response to an lock error
290
                         * @throws EXistException generic exception
291
                         */
292
                        public CR doAll() throws PermissionDeniedException, LockException, EXistException {
293
                            final Tuple3<Optional<CR>, Optional<Void>, Optional<Void>> result = FluentBrokerAPIBuilder.this.doAll(collectionUri, collectionLockMode, Optional.of(collectionOp), documentLookupFun, Optional.empty(), Optional.empty());
×
294
                            return result._1.get();
×
295
                        }
296

297
                        public class FluentBrokerAPIBuilder_Col1_Exec_Doc1_NoExec_Exec<DR> {
298
                            private final Function<DocumentImpl, DR> documentOp;
299

300
                            private FluentBrokerAPIBuilder_Col1_Exec_Doc1_NoExec_Exec(final Function<DocumentImpl, DR> documentOp) {
1✔
301
                                this.documentOp = documentOp;
1✔
302
                            }
1✔
303

304
                            /**
305
                             * Executes the Collection operation, the Document operation, and returns the results.
306
                             *
307
                             * @return A tuple, where the first entry is the result of the Collection operation,
308
                             *     and the second entry is the result of the Document operation.
309
                             * @throws PermissionDeniedException if user has not sufficient rights
310
                             * @throws LockException in response to an lock error
311
                             * @throws EXistException generic exception
312
                             */
313
                            public Tuple2<CR, DR> doAll() throws PermissionDeniedException, LockException, EXistException {
314
                                final Tuple3<Optional<CR>, Optional<Void>, Optional<DR>> result = FluentBrokerAPIBuilder.this.doAll(collectionUri, collectionLockMode, Optional.of(collectionOp), documentLookupFun, Optional.empty(), Optional.of(documentOp));
1✔
315
                                return new Tuple2<>(result._1.get(), result._3.get());
1✔
316
                            }
317
                        }
318
                    }
319
                }
320
            }
321

322
            public class FluentBrokerAPIBuilder_Col1_NoExec_Doc1 {
323
                private final Function<Collection, Tuple2<XmldbURI, LockMode>> documentLookupFun;
324

325
                private FluentBrokerAPIBuilder_Col1_NoExec_Doc1(final Function<Collection, Tuple2<XmldbURI, LockMode>> documentLookupFun) {
1✔
326
                    this.documentLookupFun = documentLookupFun;
1✔
327
                }
1✔
328

329
                /**
330
                 * An operation to perform on a Collection and Document.
331
                 *
332
                 * The operation will be executed after both the Collection and Document are retrieved and locked.
333
                 *
334
                 * @param <CDR> The return type of the {@code collectionDocumentOp}
335
                 * @param collectionDocumentOp The function to execute against the Collection and Document
336
                 *
337
                 * @return a builder.
338
                 */
339
                public <CDR> FluentBrokerAPIBuilder_Col1_NoExec_Doc1_Exec<CDR> execute(final BiFunction<Collection, DocumentImpl, CDR> collectionDocumentOp) {
340
                    return new FluentBrokerAPIBuilder_Col1_NoExec_Doc1_Exec<>(collectionDocumentOp);
1✔
341
                }
342

343
                /**
344
                 * Releases the Collection.
345
                 *
346
                 * @return a builder.
347
                 */
348
                public FluentBrokerAPIBuilder_Col1_NoExec_Doc1_NoExec withoutCollection() {
349
                    return new FluentBrokerAPIBuilder_Col1_NoExec_Doc1_NoExec();
1✔
350
                }
351

352
                public class FluentBrokerAPIBuilder_Col1_NoExec_Doc1_Exec<CDR> {
353
                    private final BiFunction<Collection, DocumentImpl, CDR> collectionDocumentOp;
354

355
                    private FluentBrokerAPIBuilder_Col1_NoExec_Doc1_Exec(final BiFunction<Collection, DocumentImpl, CDR> collectionDocumentOp) {
1✔
356
                        this.collectionDocumentOp = collectionDocumentOp;
1✔
357
                    }
1✔
358

359
                    /**
360
                     * Releases the Collection.
361
                     *
362
                     * @return a builder.
363
                     */
364
                    public FluentBrokerAPIBuilder_Col1_NoExec_Doc1_Exec_NoExec withoutCollection() {
365
                        return new FluentBrokerAPIBuilder_Col1_NoExec_Doc1_Exec_NoExec();
1✔
366
                    }
367

368
                    /**
369
                     * Executes the Collection and Document operation and returns the result.
370
                     *
371
                     * @return The result of the Collection and Document operation.
372
                     * @throws PermissionDeniedException if user has not sufficient rights
373
                     * @throws LockException in response to an lock error
374
                     * @throws EXistException generic exception
375
                     */
376
                    public CDR doAll() throws PermissionDeniedException, LockException, EXistException {
377
                        final Tuple3<Optional<Void>, Optional<CDR>, Optional<Void>> result = FluentBrokerAPIBuilder.this.doAll(collectionUri, collectionLockMode, Optional.empty(), documentLookupFun, Optional.of(collectionDocumentOp), Optional.empty());
1✔
378
                        return result._2.get();
1✔
379
                    }
380

381
                    public class FluentBrokerAPIBuilder_Col1_NoExec_Doc1_Exec_NoExec {
382
                        private FluentBrokerAPIBuilder_Col1_NoExec_Doc1_Exec_NoExec() {}
1✔
383

384
                        /**
385
                         * An operation to perform on a Document from the Collection.
386
                         *
387
                         * The operation will be executed after the Document is retrieved and locked, and after the Collection was released.
388
                         *
389
                         * @param <DR> The return type of the {@code documentOp}
390
                         * @param documentOp The function to execute against the Document
391
                         *
392
                         * @return a builder.
393
                         */
394
                        public <DR> FluentBrokerAPIBuilder_Col1_NoExec_Doc1_Exec_NoExec_Exec<DR> execute(final Function<DocumentImpl, DR> documentOp) {
395
                            return new FluentBrokerAPIBuilder_Col1_NoExec_Doc1_Exec_NoExec_Exec<>(documentOp);
1✔
396
                        }
397
                    }
398

399
                    public class FluentBrokerAPIBuilder_Col1_NoExec_Doc1_Exec_NoExec_Exec<DR> {
400
                        private final Function<DocumentImpl, DR> documentOp;
401

402
                        private FluentBrokerAPIBuilder_Col1_NoExec_Doc1_Exec_NoExec_Exec(final Function<DocumentImpl, DR> documentOp) {
1✔
403
                            this.documentOp = documentOp;
1✔
404
                        }
1✔
405

406

407
                        /**
408
                         * Executes the Collection and Document operation, the Document operation, and returns the results.
409
                         *
410
                         * @return A tuple, where the first entry is the result of the Collection and Document operation,
411
                         *     and the second entry is the result of the Document operation.
412
                         * @throws PermissionDeniedException if user has not sufficient rights
413
                         * @throws LockException in response to an lock error
414
                         * @throws EXistException generic exception
415
                         */
416
                        public Tuple2<CDR, DR> doAll() throws PermissionDeniedException, LockException, EXistException {
417
                            final Tuple3<Optional<Void>, Optional<CDR>, Optional<DR>> result = FluentBrokerAPIBuilder.this.doAll(collectionUri, collectionLockMode, Optional.empty(), documentLookupFun, Optional.of(collectionDocumentOp), Optional.of(documentOp));
1✔
418
                            return new Tuple2<>(result._2.get(), result._3.get());
1✔
419
                        }
420
                    }
421
                }
422

423
                public class FluentBrokerAPIBuilder_Col1_NoExec_Doc1_NoExec {
424
                    private FluentBrokerAPIBuilder_Col1_NoExec_Doc1_NoExec() {}
1✔
425

426
                    /**
427
                     * An operation to perform on a Document from the Collection.
428
                     *
429
                     * The operation will be executed after the Document is retrieved and locked, and after the Collection was released.
430
                     *
431
                     * @param <DR> The return type of the {@code documentOp}
432
                     * @param documentOp The function to execute against the Document
433
                     *
434
                     * @return a builder.
435
                     */
436
                    public <DR> FluentBrokerAPIBuilder_Col1_NoExec_Doc1_NoExec_Exec<DR> execute(final Function<DocumentImpl, DR> documentOp) {
437
                        return new FluentBrokerAPIBuilder_Col1_NoExec_Doc1_NoExec_Exec<>(documentOp);
1✔
438
                    }
439

440
                    public class FluentBrokerAPIBuilder_Col1_NoExec_Doc1_NoExec_Exec<DR> {
441
                        private final Function<DocumentImpl, DR> documentOp;
442

443
                        private FluentBrokerAPIBuilder_Col1_NoExec_Doc1_NoExec_Exec(final Function<DocumentImpl, DR> documentOp) {
1✔
444
                            this.documentOp = documentOp;
1✔
445
                        }
1✔
446

447
                        /**
448
                         * Executes the Document operation and returns the result.
449
                         *
450
                         * @return The result of the Document operation.
451
                         * @throws PermissionDeniedException if user has not sufficient rights
452
                         * @throws LockException in response to an lock error
453
                         * @throws EXistException generic exception
454
                         */
455
                        public DR doAll() throws PermissionDeniedException, LockException, EXistException {
456
                            final Tuple3<Optional<Void>, Optional<Void>, Optional<DR>> result = FluentBrokerAPIBuilder.this.doAll(collectionUri, collectionLockMode, Optional.empty(), documentLookupFun, Optional.empty(), Optional.of(documentOp));
1✔
457
                            return result._3.get();
1✔
458
                        }
459
                    }
460
                }
461
            }
462
        }
463

464
        private <CR, CDR, DR> Tuple3<Optional<CR>, Optional<CDR>, Optional<DR>> doAll(
465
                final XmldbURI collectionUri, final LockMode collectionLockMode,
466
                final Optional<Function<Collection, CR>> collectionFun,
467
                @Nullable final Function<Collection, Tuple2<XmldbURI, LockMode>> documentLookupFun,
468
                final Optional<BiFunction<Collection, DocumentImpl, CDR>> collectionDocumentFun,
469
                final Optional<Function<DocumentImpl, DR>> documentFun) throws EXistException, PermissionDeniedException, LockException {
470

471
            final Optional<CR> collectionFunResult;
472
            final Optional<CDR> collectionDocumentFunResult;
473
            final Optional<DR> documentFunResult;
474

475
            try(final DBBroker broker = brokerPool.getBroker()) {
1✔
476

477
                Collection collection = null;
1✔
478
                try {
479
                    collection = broker.openCollection(collectionUri, collectionLockMode);
1✔
480

481
                    if(collection == null) {
1!
482
                        throw new EXistException("No such Collection: " + collectionUri);
×
483
                    }
484

485
                    final Collection c = collection;    // needed final for closures
1✔
486
                    collectionFunResult = collectionFun.map(cf -> cf.apply(c));
1✔
487

488
                    if(collectionDocumentFun.isPresent() || documentFun.isPresent()) {
1✔
489

490
                        final Tuple2<XmldbURI, LockMode> docAccess = documentLookupFun.apply(collection);
1✔
491

492
                        try(final LockedDocument lockedDocument = collection.getDocumentWithLock(broker, docAccess._1, docAccess._2)) {
1✔
493
                            final DocumentImpl document = lockedDocument.getDocument();
1✔
494

495
                            collectionDocumentFunResult = collectionDocumentFun.map(cdf -> cdf.apply(c, document));
1✔
496

497
                            // release the Collection lock early
498
                            collection.close();
1✔
499
                            collection = null;  // signal closed
1✔
500

501
                            documentFunResult = documentFun.map(df -> df.apply(document));
1✔
502
                        }
503
                    } else {
504
                        collectionDocumentFunResult = Optional.empty();
1✔
505
                        documentFunResult = Optional.empty();
1✔
506
                    }
507
                } finally {
1✔
508
                    // catch-all to close the collection in case of an exception and it hasn't been closed
509
                    if(collection != null) {
1✔
510
                        collection.close();
1✔
511
                        collection = null;
1✔
512
                    }
513
                }
514
            }
515

516
            return new Tuple3<>(collectionFunResult, collectionDocumentFunResult, documentFunResult);
1✔
517
        }
518

519

520

521
//        public class FluentBrokerAPIBuilder_ColN {
522
//            private final Tuple2<XmldbURI, LockMode> collectionsAndLockModes[];
523
//
524
//            private FluentBrokerAPIBuilder_ColN(final Tuple2<XmldbURI, LockMode>... collectionsAndLockModes) {
525
//                this.collectionsAndLockModes = collectionsAndLockModes;
526
//            }
527
//
528
//            public Object[] execute(final Function<Collection, Object>... collectionOps) {
529
//                if(collectionsAndLockModes.length != collectionOps.length) {
530
//                    throw new IllegalStateException();
531
//                }
532
//            }
533
//        }
534
    }
535
}
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