FileDocCategorySizeDatePackage
SingleHostConcurrentStorageLock.javaAPI DocApache Lucene 2.1.05984Wed Feb 14 10:46:04 GMT 2007org.apache.lucene.gdata.storage.lucenestorage

SingleHostConcurrentStorageLock

public class SingleHostConcurrentStorageLock extends Object implements ConcurrentStorageLock
author
Simon Willnauer

Fields Summary
private static volatile ConcurrentStorageLock
INSTANCE
private final Map
locks
private final Map
threads
private final ReentrantReadWriteLock
synLock
private final Lock
readLock
private final Lock
writeLock
private final AtomicBoolean
isClosed
Constructors Summary
private SingleHostConcurrentStorageLock()

          
      
        super();
        this.locks = new HashMap<String,Thread>(10);
        this.threads = new HashMap<Thread,String>(10);
    
Methods Summary
public voidclose()

see
org.apache.lucene.gdata.storage.lucenestorage.ConcurrentStorageLock#close()

        this.writeLock.lock();
        try{
            if(this.isClosed.get())
                throw new IllegalStateException("Lock has been closed");
            this.isClosed.set(true);
            this.locks.clear();
            this.threads.clear();
            INSTANCE = new SingleHostConcurrentStorageLock();
        }finally{
            this.writeLock.unlock();
        }
    
protected voidforceClear()

        this.writeLock.lock();
        try{
            if(this.isClosed.get())
                throw new IllegalStateException("Lock has been closed");
            this.locks.clear();
            this.threads.clear();
            
        }finally{
            this.writeLock.unlock();
        }
    
protected static synchronized ConcurrentStorageLockgetConcurrentStorageLock()

        if(INSTANCE == null)
            INSTANCE = new SingleHostConcurrentStorageLock();
        return INSTANCE;
    
public booleanisKeyLocked(java.lang.String key)

see
org.apache.lucene.gdata.storage.lucenestorage.ConcurrentStorageLock#isKeyLocked(java.lang.String)

        this.readLock.lock();
        try{
            if(this.isClosed.get())
                throw new IllegalStateException("Lock has been closed");
           return this.locks.containsKey(key);
        }finally{
            this.readLock.unlock();
        }
    
public booleanreleaseLock(java.lang.String key)

see
org.apache.lucene.gdata.storage.lucenestorage.ConcurrentStorageLock#releaseLock(java.lang.String)

        this.writeLock.lock();
        try{
            if(this.isClosed.get())
                throw new IllegalStateException("Lock has been closed");
            Thread t = Thread.currentThread();
            if(!this.threads.containsKey(t))
                return false;
            
            if(!this.locks.containsKey(key))
                return false;
            if(t != this.locks.get(key))
                throw new ConcurrencyException("Illegal lock access -- current thread is not owner");
            this.locks.remove(key);
            this.threads.remove(t);
            return true;
            
        }finally{
            this.writeLock.unlock();
        }
        
    
public booleanreleaseThreadLocks()

see
org.apache.lucene.gdata.storage.lucenestorage.ConcurrentStorageLock#releaseThreadLocks()

        this.writeLock.lock();
        try{
            if(this.isClosed.get())
                throw new IllegalStateException("Lock has been closed");
            Thread t = Thread.currentThread();
            if(!this.threads.containsKey(t))
                return false;
            String key = this.threads.get(t);
            this.threads.remove(t);
            if(!this.locks.containsKey(key))
                return false;
            this.locks.remove(key);
            return true;
            
        }finally{
            this.writeLock.unlock();
        }
    
public booleansetLock(java.lang.String key)

see
org.apache.lucene.gdata.storage.lucenestorage.ConcurrentStorageLock#setLock(java.lang.String)

       this.writeLock.lock();
       try{
           if(this.isClosed.get())
               throw new IllegalStateException("Lock has been closed");
           Thread t = Thread.currentThread();
           if(this.threads.containsKey(t))
               throw new ConcurrencyException("one thread must not obtain more than one lock -- single thread can not modify more than one resource");
           if(this.locks.containsKey(key)){
               return false;
           }
           this.locks.put(key, t);
           this.threads.put(t,key);
           return true;
           
       }finally{
           this.writeLock.unlock();
       }