jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r350028 - in /incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene: CachingIndexReader.java DocId.java DocNumberCache.java
Date Wed, 30 Nov 2005 19:30:03 GMT
Author: mreutegg
Date: Wed Nov 30 11:29:57 2005
New Revision: 350028

URL: http://svn.apache.org/viewcvs?rev=350028&view=rev
Log:
Improve concurrent query performance

Modified:
    incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/CachingIndexReader.java
    incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/DocId.java
    incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/DocNumberCache.java

Modified: incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/CachingIndexReader.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/CachingIndexReader.java?rev=350028&r1=350027&r2=350028&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/CachingIndexReader.java
(original)
+++ incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/CachingIndexReader.java
Wed Nov 30 11:29:57 2005
@@ -88,9 +88,7 @@
     DocId getParent(int n, BitSet deleted) throws IOException {
         DocId parent;
         boolean existing = false;
-        synchronized (parents) {
-            parent = parents[n];
-        }
+        parent = parents[n];
 
         if (parent != null) {
             existing = true;
@@ -135,9 +133,7 @@
             }
 
             // finally put to cache
-            synchronized (parents) {
-                parents[n] = parent;
-            }
+            parents[n] = parent;
         }
         return parent;
     }

Modified: incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/DocId.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/DocId.java?rev=350028&r1=350027&r2=350028&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/DocId.java
(original)
+++ incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/DocId.java
Wed Nov 30 11:29:57 2005
@@ -177,6 +177,18 @@
         private final String uuid;
 
         /**
+         * The index reader that was used to calculate the document number.
+         * If <code>null</code> then the document number has not yet been
+         * calculated.
+         */
+        private IndexReader reader;
+
+        /**
+         * The previously calculated document number.
+         */
+        private int docNumber;
+
+        /**
          * Creates a <code>DocId</code> based on a Node uuid.
          *
          * @param uuid the Node uuid.
@@ -189,6 +201,11 @@
          * @inheritDoc
          */
         final int getDocumentNumber(IndexReader reader) throws IOException {
+            synchronized (this) {
+                if (reader == this.reader) {
+                    return docNumber;
+                }
+            }
             Term id = new Term(FieldNames.UUID, uuid);
             TermDocs docs = reader.termDocs(id);
             int doc = -1;
@@ -198,6 +215,10 @@
                 }
             } finally {
                 docs.close();
+            }
+            synchronized (this) {
+                docNumber = doc;
+                this.reader = reader;
             }
             return doc;
         }

Modified: incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/DocNumberCache.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/DocNumberCache.java?rev=350028&r1=350027&r2=350028&view=diff
==============================================================================
--- incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/DocNumberCache.java
(original)
+++ incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/query/lucene/DocNumberCache.java
Wed Nov 30 11:29:57 2005
@@ -35,9 +35,19 @@
     private static final long LOG_INTERVAL = 1000 * 10;
 
     /**
-     * LRU Map where key=uuid value=reader;docNumber
+     * The number of cache segments.
      */
-    private final LRUMap docNumbers;
+    private static final int CACHE_SEGMENTS = 0x10;
+
+    /**
+     * Mask to calculate segment number.
+     */
+    private static final int CACHE_SEGMENTS_MASK = CACHE_SEGMENTS - 1;
+
+    /**
+     * LRU Maps where key=uuid value=reader;docNumber
+     */
+    private final LRUMap[] docNumbers = new LRUMap[CACHE_SEGMENTS];
 
     /**
      * Timestamp of the last cache statistics log.
@@ -61,7 +71,14 @@
      * @param size the cache limit.
      */
     DocNumberCache(int size) {
-        docNumbers = new LRUMap(size);
+        size = size % CACHE_SEGMENTS;
+        if (size < 0x40) {
+            // minimum size is 0x40 * 0x10 = 1024
+            size = 0x40;
+        }
+        for (int i = 0; i < docNumbers.length; i++) {
+            docNumbers[i] = new LRUMap(size);
+        }
     }
 
     /**
@@ -73,26 +90,29 @@
      * @param reader the index reader from where the document number was read.
      * @param n the document number.
      */
-    synchronized void put(String uuid, CachingIndexReader reader, int n) {
-        Entry e = (Entry) docNumbers.get(uuid);
-        if (e != null) {
-            // existing entry
-            // ignore if reader is older than the one in entry
-            if (reader.getCreationTick() <= e.reader.getCreationTick()) {
-                if (log.isDebugEnabled()) {
-                    log.debug("Ignoring put(). New entry is not from a newer reader. " +
-                            "existing: " + e.reader.getCreationTick() +
-                            ", new: " + reader.getCreationTick());
+    void put(String uuid, CachingIndexReader reader, int n) {
+        LRUMap cacheSegment = docNumbers[getSegmentIndex(uuid.charAt(0))];
+        synchronized (cacheSegment) {
+            Entry e = (Entry) cacheSegment.get(uuid);
+            if (e != null) {
+                // existing entry
+                // ignore if reader is older than the one in entry
+                if (reader.getCreationTick() <= e.reader.getCreationTick()) {
+                    if (log.isDebugEnabled()) {
+                        log.debug("Ignoring put(). New entry is not from a newer reader.
" +
+                                "existing: " + e.reader.getCreationTick() +
+                                ", new: " + reader.getCreationTick());
+                    }
+                    e = null;
                 }
-                e = null;
+            } else {
+                // entry did not exist
+                e = new Entry(reader, n);
             }
-        } else {
-            // entry did not exist
-            e = new Entry(reader, n);
-        }
 
-        if (e != null) {
-            docNumbers.put(uuid, e);
+            if (e != null) {
+                cacheSegment.put(uuid, e);
+            }
         }
     }
 
@@ -103,8 +123,12 @@
      * @param uuid the key.
      * @return cache entry or <code>null</code>.
      */
-    synchronized Entry get(String uuid) {
-        Entry entry = (Entry) docNumbers.get(uuid);
+    Entry get(String uuid) {
+        LRUMap cacheSegment = docNumbers[getSegmentIndex(uuid.charAt(0))];
+        Entry entry;
+        synchronized (cacheSegment) {
+            entry = (Entry) cacheSegment.get(uuid);
+        }
         if (log.isInfoEnabled()) {
             accesses++;
             if (entry == null) {
@@ -117,8 +141,12 @@
                     ratio -= misses * 100L / accesses;
                 }
                 StringBuffer statistics = new StringBuffer();
-                statistics.append("size=").append(docNumbers.size());
-                statistics.append("/").append(docNumbers.maxSize());
+                int inUse = 0;
+                for (int i = 0; i < docNumbers.length; i++) {
+                    inUse += docNumbers[i].size();
+                }
+                statistics.append("size=").append(inUse);
+                statistics.append("/").append(docNumbers[0].maxSize() * CACHE_SEGMENTS);
                 statistics.append(", #accesses=").append(accesses);
                 statistics.append(", #hits=").append((accesses - misses));
                 statistics.append(", #misses=").append(misses);
@@ -130,6 +158,16 @@
             }
         }
         return entry;
+    }
+
+    /**
+     * Returns the segment index for character c.
+     */
+    private static final int getSegmentIndex(char c) {
+        if (c > '9') {
+            c += 9;
+        }
+        return c & CACHE_SEGMENTS_MASK;
     }
 
     public static final class Entry {



Mime
View raw message