@@ -602,6 +602,48 @@ public boolean isDocumentLockedForRead(final XmldbURI documentPath) {
602
602
return existingLock .getReadLockCount () > 0 ;
603
603
}
604
604
605
+ /**
606
+ * Returns the LockMode that should be used for accessing
607
+ * a Collection when that access is just for the purposes
608
+ * or accessing a document(s) within the Collection.
609
+ *
610
+ * When Path Locks are enabled for Documents, both Collections and
611
+ * Documents share the same lock domain, a path based tree hierarchy.
612
+ * With a shared lock-hierarchy, if we were to READ_LOCK a Collection
613
+ * and then request a WRITE_LOCK for a Document in that Collection we
614
+ * would reach a dead-lock situation. To avoid this, if this method is
615
+ * called like
616
+ * {@code relativeCollectionLockMode(LockMode.READ_LOCK, LockMode.WRITE_LOCK)}
617
+ * it will return a WRITE_LOCK. That is to say that to aid dealock-avoidance,
618
+ * this function may return a stricter locking mode than the {@code desiredCollectionLockMode}.
619
+ *
620
+ * When Path Locks are disabled (the default) for Documents, Collection and Documents
621
+ * have independent locking domains. In this case this function will always return
622
+ * the {@code desiredCollectionLockMode}.
623
+ *
624
+ * @param desiredCollectionLockMode The desired lock mode for the Collection.
625
+ * @param documentLockMode The lock mode that will be used for subsequent document operations in the Collection.
626
+ *
627
+ * @return The lock mode that should be used for accessing the Collection.
628
+ */
629
+ public Lock .LockMode relativeCollectionLockMode (final Lock .LockMode desiredCollectionLockMode ,
630
+ final Lock .LockMode documentLockMode ) {
631
+ if (!usePathLocksForDocuments ) {
632
+ return desiredCollectionLockMode ;
633
+
634
+ } else {
635
+ switch (documentLockMode ) {
636
+ case NO_LOCK :
637
+ case INTENTION_READ :
638
+ case READ_LOCK :
639
+ return Lock .LockMode .READ_LOCK ;
640
+
641
+ default :
642
+ return Lock .LockMode .WRITE_LOCK ;
643
+ }
644
+ }
645
+ }
646
+
605
647
/**
606
648
* Retrieves a lock for a {@link org.exist.storage.dom.DOMFile}
607
649
*
0 commit comments