From fb6af795e65368f9a6bc10b42dce804dcc1e6f60 Mon Sep 17 00:00:00 2001
From: abobrov <abobrov@localhost>
Date: Thu, 12 Jul 2007 19:20:32 +0000
Subject: [PATCH] - skip cache persisting phase if the cache is empty.

---
 opendj-sdk/opends/src/server/org/opends/server/extensions/FileSystemEntryCache.java |   48 ++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/extensions/FileSystemEntryCache.java b/opendj-sdk/opends/src/server/org/opends/server/extensions/FileSystemEntryCache.java
index e0a1063..bddb334 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/extensions/FileSystemEntryCache.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/extensions/FileSystemEntryCache.java
@@ -557,8 +557,8 @@
     // already exist at this point all we have to do is to serialize cache
     // index maps @see FileSystemEntryCacheIndex and put them under indexkey
     // allowing for the index to be restored and cache contents reused upon
-    // the next initialization.
-    if (persistentCache) {
+    // the next initialization. If this cache is empty skip persisting phase.
+    if (persistentCache && !dnMap.isEmpty()) {
       FileSystemEntryCacheIndex entryCacheIndex =
           new FileSystemEntryCacheIndex();
       // There must be at least one backend at this stage.
@@ -892,13 +892,21 @@
       if (entryID == null) {
         return;
       }
-      for (Map<Long,DN> map : backendMap.values()) {
+      Set<Backend> backendSet = backendMap.keySet();
+      Iterator<Backend> backendIterator = backendSet.iterator();
+      while (backendIterator.hasNext()) {
+        Map<Long,DN> map = backendMap.get(backendIterator.next());
         if ((map.get(entryID) != null) &&
             (map.get(entryID).equals(entryDN))) {
           map.remove(entryID);
+          // If this backend becomes empty now
+          // remove it from the backend map.
+          if (map.isEmpty()) {
+            backendIterator.remove();
+          }
+          break;
         }
       }
-
       dnMap.remove(entryDN);
       entryCacheDB.delete(null,
         new DatabaseEntry(entryDN.toNormalizedString().getBytes("UTF-8")));
@@ -1302,7 +1310,13 @@
     // Read configuration.
     newConfigEntryDN = configuration.dn();
     newLockTimeout   = configuration.getLockTimeout();
-    newMaxEntries    = configuration.getMaxEntries();
+
+    // If the value of zero arrives make sure it is traslated
+    // to the maximum possible value we can cap maxEntries to.
+    newMaxEntries = configuration.getMaxEntries();
+    if (newMaxEntries <= 0) {
+      newMaxEntries = DEFAULT_FSCACHE_MAX_ENTRIES;
+    }
 
     // Maximum memory/space this cache can utilize.
     newMaxAllowedMemory = configuration.getMaxMemorySize();
@@ -1522,8 +1536,11 @@
           new DatabaseEntry(
           entry.encode(encodeConfig))) == OperationStatus.SUCCESS) {
 
-        // Add the entry to the cache maps.
-        dnMap.put(entry.getDN(), entryID);
+        // Add the entry to the cache maps. The order in which maps
+        // are populated is important since invoking put on rotator
+        // map can cause the eldest map entry to be removed @see
+        // LinkedHashMapRotator.removeEldestEntry() therefore every
+        // cache map has to be up to date if / when that happens.
         Map<Long,DN> map = backendMap.get(backend);
         if (map == null) {
           map = new LinkedHashMap<Long,DN>();
@@ -1532,6 +1549,7 @@
         } else {
           map.put(entryID, entry.getDN());
         }
+        dnMap.put(entry.getDN(), entryID);
       }
 
       // We'll always return true in this case, even if we didn't actually add
@@ -1626,14 +1644,24 @@
         cacheWriteLock.lock();
         try {
           // Remove the the eldest entry from supporting maps.
-          cacheEntryKey.setData(
-              ((DN) eldest.getKey()).toNormalizedString().getBytes("UTF-8"));
+          DN entryDN = (DN) eldest.getKey();
           long entryID = ((Long) eldest.getValue()).longValue();
+          cacheEntryKey.setData(
+              entryDN.toNormalizedString().getBytes("UTF-8"));
           Set<Backend> backendSet = backendMap.keySet();
           Iterator<Backend> backendIterator = backendSet.iterator();
           while (backendIterator.hasNext()) {
             Map<Long,DN> map = backendMap.get(backendIterator.next());
-            map.remove(entryID);
+            if ((map.get(entryID) != null) &&
+                (map.get(entryID).equals(entryDN))) {
+              map.remove(entryID);
+              // If this backend becomes empty now
+              // remove it from the backend map.
+              if (map.isEmpty()) {
+                backendIterator.remove();
+              }
+              break;
+            }
           }
           // Remove the the eldest entry from the database.
           entryCacheDB.delete(null, cacheEntryKey);

--
Gitblit v1.10.0