From 281a429e6f526e356353103ae3b60d45d75791c9 Mon Sep 17 00:00:00 2001
From: abobrov <abobrov@localhost>
Date: Wed, 11 Jul 2007 23:03:46 +0000
Subject: [PATCH] - [Issue 1588] consolidate cache entry locking code:   getEntry(lck) locking code logic is now consolidated in a single method    inside the EntryCache abstract class effectively moving it out of entry    cache implementing subclasses.

---
 opends/src/server/org/opends/server/extensions/SoftReferenceEntryCache.java |  243 +----------------------------------------------
 1 files changed, 9 insertions(+), 234 deletions(-)

diff --git a/opends/src/server/org/opends/server/extensions/SoftReferenceEntryCache.java b/opends/src/server/org/opends/server/extensions/SoftReferenceEntryCache.java
index 3f298b2..101f382 100644
--- a/opends/src/server/org/opends/server/extensions/SoftReferenceEntryCache.java
+++ b/opends/src/server/org/opends/server/extensions/SoftReferenceEntryCache.java
@@ -35,7 +35,6 @@
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.locks.Lock;
 
 import org.opends.server.admin.server.ConfigurationChangeListener;
 import org.opends.server.admin.std.server.EntryCacheCfg;
@@ -52,7 +51,6 @@
 import org.opends.server.types.Entry;
 import org.opends.server.types.InitializationException;
 import org.opends.server.types.LockManager;
-import org.opends.server.types.LockType;
 import org.opends.server.types.SearchFilter;
 
 
@@ -105,10 +103,6 @@
   // cache.
   private HashSet<SearchFilter> includeFilters;
 
-  // The maximum length of time that we will wait while trying to obtain a lock
-  // on an entry.
-  private long lockTimeout;
-
   // The reference queue that will be used to notify us whenever a soft
   // reference is freed.
   private ReferenceQueue<CacheEntry> referenceQueue;
@@ -254,237 +248,18 @@
   /**
    * {@inheritDoc}
    */
-  public Entry getEntry(DN entryDN, LockType lockType,
-                        List<Lock> lockList)
+  protected DN getEntryDN(Backend backend, long entryID)
   {
-    SoftReference<CacheEntry> ref = dnMap.get(entryDN);
-    if (ref == null)
-    {
-      return null;
-    }
-    else
-    {
-      CacheEntry cacheEntry = ref.get();
-      if (cacheEntry == null)
-      {
-        return null;
-      }
-      else
-      {
-        switch (lockType)
-        {
-          case READ:
-            // Try to obtain a read lock for the entry, but don't wait too long
-            // so only try once.
-            Lock readLock = LockManager.lockRead(entryDN);
-            if (readLock == null)
-            {
-              // We couldn't get the lock, so we have to return null.
-              return null;
-            }
-            else
-            {
-              try
-              {
-                lockList.add(readLock);
-                return cacheEntry.getEntry();
-              }
-              catch (Exception e)
-              {
-                if (debugEnabled())
-                {
-                  TRACER.debugCaught(DebugLogLevel.ERROR, e);
-                }
-
-                // The attempt to add the lock to the list failed, so we need to
-                // release the lock and return null.
-                try
-                {
-                  LockManager.unlock(entryDN, readLock);
-                }
-                catch (Exception e2)
-                {
-                  if (debugEnabled())
-                  {
-                    TRACER.debugCaught(DebugLogLevel.ERROR, e2);
-                  }
-                }
-
-                return null;
-              }
-            }
-
-          case WRITE:
-            // Try to obtain a write lock for the entry, but don't wait too long
-            // so only try once.
-            Lock writeLock = LockManager.lockWrite(entryDN);
-            if (writeLock == null)
-            {
-              // We couldn't get the lock, so we have to return null.
-              return null;
-            }
-            else
-            {
-              try
-              {
-                lockList.add(writeLock);
-                return cacheEntry.getEntry();
-              }
-              catch (Exception e)
-              {
-                if (debugEnabled())
-                {
-                  TRACER.debugCaught(DebugLogLevel.ERROR, e);
-                }
-
-                // The attempt to add the lock to the list failed, so we need to
-                // release the lock and return null.
-                try
-                {
-                  LockManager.unlock(entryDN, writeLock);
-                }
-                catch (Exception e2)
-                {
-                  if (debugEnabled())
-                  {
-                    TRACER.debugCaught(DebugLogLevel.ERROR, e2);
-                  }
-                }
-
-                return null;
-              }
-            }
-
-          case NONE:
-            // There is no lock required, so we can just return the entry.
-            return cacheEntry.getEntry();
-
-          default:
-            // This is an unknown type of lock, so we can't provide it.
-            return null;
-        }
+    // Locate specific backend map and return the entry DN by ID.
+    ConcurrentHashMap<Long,SoftReference<CacheEntry>>
+      backendMap = idMap.get(backend);
+    if (backendMap != null) {
+      SoftReference<CacheEntry> ref = backendMap.get(entryID);
+      if ((ref != null) && (ref.get() != null)) {
+        return ref.get().getDN();
       }
     }
-  }
-
-
-
-  /**
-   * {@inheritDoc}
-   */
-  public Entry getEntry(Backend backend, long entryID,
-                        LockType lockType, List<Lock> lockList)
-  {
-    ConcurrentHashMap<Long,SoftReference<CacheEntry>> map = idMap.get(backend);
-    if (map == null)
-    {
-      return null;
-    }
-
-    SoftReference<CacheEntry> ref = map.get(entryID);
-    if (ref == null)
-    {
-      return null;
-    }
-
-    CacheEntry cacheEntry = ref.get();
-    if (cacheEntry == null)
-    {
-      return null;
-    }
-
-    switch (lockType)
-    {
-      case READ:
-        // Try to obtain a read lock for the entry, but don't wait too long so
-        // only try once.
-        Lock readLock = LockManager.lockRead(cacheEntry.getDN());
-        if (readLock == null)
-        {
-          // We couldn't get the lock, so we have to return null.
-          return null;
-        }
-        else
-        {
-          try
-          {
-            lockList.add(readLock);
-            return cacheEntry.getEntry();
-          }
-          catch (Exception e)
-          {
-            if (debugEnabled())
-            {
-              TRACER.debugCaught(DebugLogLevel.ERROR, e);
-            }
-
-            // The attempt to add the lock to the list failed, so we need to
-            // release the lock and return null.
-            try
-            {
-              LockManager.unlock(cacheEntry.getDN(), readLock);
-            }
-            catch (Exception e2)
-            {
-              if (debugEnabled())
-              {
-                TRACER.debugCaught(DebugLogLevel.ERROR, e2);
-              }
-            }
-
-            return null;
-          }
-        }
-
-      case WRITE:
-        // Try to obtain a write lock for the entry, but don't wait too long so
-        // only try once.
-        Lock writeLock = LockManager.lockWrite(cacheEntry.getDN());
-        if (writeLock == null)
-        {
-          // We couldn't get the lock, so we have to return null.
-          return null;
-        }
-        else
-        {
-          try
-          {
-            lockList.add(writeLock);
-            return cacheEntry.getEntry();
-          }
-          catch (Exception e)
-          {
-            if (debugEnabled())
-            {
-              TRACER.debugCaught(DebugLogLevel.ERROR, e);
-            }
-
-            // The attempt to add the lock to the list failed, so we need to
-            // release the lock and return null.
-            try
-            {
-              LockManager.unlock(cacheEntry.getDN(), writeLock);
-            }
-            catch (Exception e2)
-            {
-              if (debugEnabled())
-              {
-                TRACER.debugCaught(DebugLogLevel.ERROR, e2);
-              }
-            }
-
-            return null;
-          }
-        }
-
-      case NONE:
-        // There is no lock required, so we can just return the entry.
-        return cacheEntry.getEntry();
-
-      default:
-        // This is an unknown type of lock, so we can't provide it.
-        return null;
-    }
+    return null;
   }
 
 

--
Gitblit v1.10.0