180 likes | 202 Views
Learn about the implementation of Concurrent R-trees in Java for indexing and representing multidimensional objects efficiently. Understand the structure of R-trees, insertion complexities, and different implementation strategies such as Monitor Solution and Readers-Writers Solution.
E N D
Implementing Concurrent R-trees in Java Mehdi Kargar Department of Computer Science and Engineering
R-trees • R-tree is useful for indexing and representing multidimensional objects. • An R-tree is a depth balanced tree with a dynamic index structure • Leaf nodes point to actual keys • The number of entries in a node is between m and N (1 < m ≤ N) • Root might have between 1 and N entries. • All leaf nodes areat the same level • The key for each internal node is the minimum bounding rectangle of its child nodes • keys at all levels might have overlap with each other
R-trees • During the search for a key, it might be necessary to descend multiple sub-trees • Insertion is more complex than search • After inserting a new key, the new bounding rectangle should be propagated up to the tree. • If a node overflows, it should be split. The split should also be propagated up to the tree. • During the Insertion, only one sub-tree is traversed at each level. We should not descend multiple sub-trees.
Three Implementations • The Monitor Solution • The Readers-Writers Solution • Locking Nodes (RLink-Tree)
The Monitor Solution public class MonitorRTree { public synchronized void add(Rectangle rect) { . . . } public synchronized boolean search(Rectangle rect) { . . . } }
The Readers Writers Solution import java.util.concurrent.locks.ReentrantReadWriteLock; public class ReadWriteLockRTree { private ReentrantReadWriteLocklock; . . . . public void add(Rectangle rect) { this.lock.writeLock().lock(); . . this.lock.writeLock().unlock(); } public boolean search(Rectangle rect) { this.lock.readLock().lock(); . . this.lock.readLock().unlock(); } }
RLink-Tree • Basic idea : • Logical Serial Number (LSN) is used to capture unfinished splits. • Right Links is used to follow the split nodes.
RLink-Tree (Locking Problem) • RLink-Tree uses the lock couplingstrategy for inserting new entries. • The normal implementation of lock coupling leads to deadlocks. Write-Lock Read-Lock 2 1 Process 2 After Inserting new entry, the split and new bounding rectangle should be propagated Write-Lockthe nodes in a bottom-up approach x Process 1 Finding the best node for inserting new entries Read-Lock the nodes in a top-down approach 1 2 Y Z
RLink-Tree (Locking Problem) • The previous situation is called phantom problem. • It can be solved by predicate locks. • What are predicate locks ?? • You can find more about it here : • K. Eswaren, J. Gray, R. Lorie and I. Traiger, On the Notions of Consistency and Predicate Locks in a Database System, Comm. ACM, November 1976, Vol. 19, No. 11, pp. 624–633.
What is implemented? • Here, the root node is kept write-lock for the insertion of new nodes. Thus, only one process can insert at any time and we do not have multiple insertions. • The LSN and right links are the same as the original RLink-Tree. • Search algorithm and the read-lock mechanism is also the same as RLink-Tree.
Read Lock and Write Lock of Nodes public synchronized void writeLock() { while (this.readers != 0 || state == WRITE) { try { this.wait(); } catch (InterruptedException e) {} } this.state = WRITE; } public synchronized void writeUnlock() { this.state = READ; this.notifyAll(); } public synchronized void readLock() { while (this.state == WRITE) { try { this.wait(); } catch (InterruptedException e) {} } this.increment(); } public synchronized void readUnlock() { this.decrement(); this.notifyAll(); }
Possible Improvements • Having multiple insertion process without locking the root. • Finding better solution for phantom problem. • . . .
Next Phase • Verifying the implementations using different applications and tools such as Java PathFinder