[hg] main-silver: Cache/Heap ratio is customizable.

  • From: Tomas Zezula < >
  • To:
  • Subject: [hg] main-silver: Cache/Heap ratio is customizable.
  • Date: Wed, 06 Feb 2013 13:58:32 -0800

changeset a88bb35bb42d in main-silver ((none))
details: http://hg.netbeans.org/main-silver/rev/a88bb35bb42d
description:
        Cache/Heap ratio is customizable.

diffstat:

 
parsing.api/src/org/netbeans/modules/parsing/impl/indexing/ClusteredIndexables.java
         |  49 +++++++--
 
parsing.api/test/unit/src/org/netbeans/modules/parsing/impl/indexing/DocumentStoreTest.java
 |  24 ++++-
 2 files changed, 59 insertions(+), 14 deletions(-)

diffs (178 lines):

diff --git 
a/parsing.api/src/org/netbeans/modules/parsing/impl/indexing/ClusteredIndexables.java
 
b/parsing.api/src/org/netbeans/modules/parsing/impl/indexing/ClusteredIndexables.java
--- 
a/parsing.api/src/org/netbeans/modules/parsing/impl/indexing/ClusteredIndexables.java
+++ 
b/parsing.api/src/org/netbeans/modules/parsing/impl/indexing/ClusteredIndexables.java
@@ -86,8 +86,6 @@
     public static final String DELETE = "ci-delete-set";    //NOI18N
     public static final String INDEX = "ci-index-set";      //NOI18N
 
-    private static final Logger LOG = 
Logger.getLogger(ClusteredIndexables.class.getName());
-
     // 
-----------------------------------------------------------------------
     // Public implementation
     // 
-----------------------------------------------------------------------
@@ -148,7 +146,12 @@
     // 
-----------------------------------------------------------------------
     // Private implementation
     // 
-----------------------------------------------------------------------
+    private static final Logger LOG = 
Logger.getLogger(ClusteredIndexables.class.getName());
     private static final String ALL_MIME_TYPES = ""; //NOI18N
+    private static final String PROP_CACHE_HEAP_RATIO = 
"ClusteredIndexables.cacheHeapRatio";  //NOI18N
+    private static final double DEFAULT_CACHE_HEAP_RATIO = 0.1;
+    private static final long DATA_CACHE_SIZE = (long) 
+            (Runtime.getRuntime().maxMemory() * getCacheHeapRatio());
     private final List<Indexable> indexables;
     private final BitSet sorted;
     private final Map<String, BitSet> mimeTypeClusters = new HashMap<String, 
BitSet>();
@@ -165,6 +168,28 @@
         return tmpIt == null ? -1 : tmpIt.index();
     }
 
+    private static double getCacheHeapRatio() {
+        final String sval = System.getProperty(PROP_CACHE_HEAP_RATIO);
+        if (sval != null) {
+            try {
+                final double val = Double.valueOf(sval);
+                if (val < 0.05 || val > 1.0) {
+                    throw new NumberFormatException();
+                }
+                return val;
+            } catch (NumberFormatException nfe) {
+                LOG.log(
+                  Level.INFO,
+                  "Invalid value of {0} property: {1}", //NOI18N
+                  new Object[] {
+                      PROP_CACHE_HEAP_RATIO,
+                      sval
+                  });
+            }
+        }
+        return DEFAULT_CACHE_HEAP_RATIO;
+    }
+
     private static interface IndexedIterator<T> extends Iterator<T> {
         int index();
     }
@@ -452,7 +477,7 @@
                     deleteFromDeleted == null &&
                     deleteFromIndex == null;
                 assert dataRef == null;
-                toAdd = new DocumentStore();
+                toAdd = new DocumentStore(DATA_CACHE_SIZE);
                 toDeleteOutOfOrder = new ArrayList<String>();
                 deleteFromDeleted = new BitSet();
                 deleteFromIndex = new BitSet();
@@ -808,8 +833,8 @@
 
         private static final int INITIAL_DOC_COUNT = 100;
         private static final int INITIAL_DATA_SIZE = 1<<10;
-        private static final long DATA_CACHE_SIZE = (long) 
(Runtime.getRuntime().maxMemory() * 0.1);
 
+        private final long dataCacheSize;
         private final Map<String,Integer> fieldNames;
         private int[] docs;
         private char[] data;
@@ -819,11 +844,15 @@
         private int size;
 
 
-        DocumentStore() {
-            fieldNames = new LinkedHashMap<String, Integer>();
-            docs = new int[INITIAL_DOC_COUNT];
-            data = new char[INITIAL_DATA_SIZE];
-
+        DocumentStore(final long dataCacheSize) {
+            this.dataCacheSize = dataCacheSize;
+            this.fieldNames = new LinkedHashMap<String, Integer>();
+            this.docs = new int[INITIAL_DOC_COUNT];
+            this.data = new char[INITIAL_DATA_SIZE];
+            LOG.log(
+                Level.FINE,
+                "DocumentStore flush size: {0}",    //NOI18N
+                dataCacheSize);
         }
 
         @Override
@@ -860,7 +889,7 @@
                 docsPointer += 2;
                 if (data.length < dataPointer + fldValue.length()) {
                     data = Arrays.copyOf(data, 
newLength(data.length,dataPointer + fldValue.length()));
-                    res = data.length<<1 > DATA_CACHE_SIZE;
+                    res = data.length<<1 > dataCacheSize;
                     LOG.log(
                         Level.FINE,
                         "New data size: {0}, flush: {1}",   //NOI18N
diff --git 
a/parsing.api/test/unit/src/org/netbeans/modules/parsing/impl/indexing/DocumentStoreTest.java
 
b/parsing.api/test/unit/src/org/netbeans/modules/parsing/impl/indexing/DocumentStoreTest.java
--- 
a/parsing.api/test/unit/src/org/netbeans/modules/parsing/impl/indexing/DocumentStoreTest.java
+++ 
b/parsing.api/test/unit/src/org/netbeans/modules/parsing/impl/indexing/DocumentStoreTest.java
@@ -46,6 +46,7 @@
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 import java.util.Random;
+import static junit.framework.Assert.assertFalse;
 import org.netbeans.api.annotations.common.NonNull;
 import org.netbeans.junit.NbTestCase;
 import org.netbeans.modules.parsing.lucene.support.IndexDocument;
@@ -56,13 +57,16 @@
  */
 public class DocumentStoreTest extends NbTestCase {
 
+    private static final long CACHE_SIZE =
+            (long) (Runtime.getRuntime().maxMemory() * 0.1);
+
     public DocumentStoreTest(@NonNull final String name) {
         super(name);
     }
 
     public void testBasicOperations() {
         final int testSize = 100000;
-        final Collection<IndexDocument> store = new 
ClusteredIndexables.DocumentStore();
+        final Collection<IndexDocument> store = new 
ClusteredIndexables.DocumentStore(CACHE_SIZE);
         final String path = getWorkDirPath();
         for (int i=0; i<testSize; i++) {
             store.add(fill(ClusteredIndexables.createDocument(
@@ -116,7 +120,7 @@
     }
 
     public void testLargeFieldDocumentAdded() {
-        final ClusteredIndexables.DocumentStore store = new 
ClusteredIndexables.DocumentStore();
+        final ClusteredIndexables.DocumentStore store = new 
ClusteredIndexables.DocumentStore(CACHE_SIZE);
         final IndexDocument doc = 
ClusteredIndexables.createDocument(getWorkDirPath());
         final String bigValue = newRandomString(16<<10);
         doc.addPair("big", bigValue, true, true);               //NOI18N
@@ -132,7 +136,7 @@
     }
 
     public void testFieldOnBoundsAdded() {
-        final ClusteredIndexables.DocumentStore store = new 
ClusteredIndexables.DocumentStore();
+        final ClusteredIndexables.DocumentStore store = new 
ClusteredIndexables.DocumentStore(CACHE_SIZE);
         final String value = newRandomString(2<<10);
         final String padding = newRandomString(1<<4);
         final IndexDocument doc = ClusteredIndexables.createDocument(value);
@@ -148,7 +152,7 @@
     }
 
     public void testIterator() {
-        final ClusteredIndexables.DocumentStore store = new 
ClusteredIndexables.DocumentStore();
+        final ClusteredIndexables.DocumentStore store = new 
ClusteredIndexables.DocumentStore(CACHE_SIZE);
         final Iterator<IndexDocument> it = store.iterator();
         assertFalse(it.hasNext());
         boolean thrown = false;
@@ -160,6 +164,18 @@
         assertTrue(thrown);
     }
 
+    public void testCacheFlush() {
+        final ClusteredIndexables.DocumentStore store = new 
ClusteredIndexables.DocumentStore(4<<10);   //4KB
+        final String val = newRandomString((1<<9)); //1KB
+        
assertFalse(store.addDocument(ClusteredIndexables.createDocument(val)));
+        
assertFalse(store.addDocument(ClusteredIndexables.createDocument(val)));
+        
assertFalse(store.addDocument(ClusteredIndexables.createDocument(val)));
+        
assertFalse(store.addDocument(ClusteredIndexables.createDocument(val)));
+        
assertTrue(store.addDocument(ClusteredIndexables.createDocument(val)));
+        store.clear();
+        
assertFalse(store.addDocument(ClusteredIndexables.createDocument(val)));
+    }
+
     @NonNull
     private static String newRandomString(final int len) {
         final Random rnd = new Random();

[hg] main-silver: Cache/Heap ratio is customizable.

Tomas Zezula 02/06/2013

Project Features

About this Project

Editor was started in November 2009, is owned by Martin Ryzl, and has 147 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20131025.e7cbc9d). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close