Coverage Report - org.galagosearch.core.parse.StringPooler
 
Classes in this File Line Coverage Branch Coverage Complexity
StringPooler
87%
13/15
75%
6/8
0
 
 1  
 // BSD License (http://www.galagosearch.org/license)
 2  
 
 3  
 package org.galagosearch.core.parse;
 4  
 
 5  
 import java.util.HashMap;
 6  
 
 7  
 /**
 8  
  * The point of this class is to replace strings in document objects with
 9  
  * already-used copies.  This can greatly reduce the amount of memory used
 10  
  * by the system.
 11  
  *
 12  
  * @author trevor
 13  
  */
 14  16
 public class StringPooler {
 15  16
     HashMap<String, String> pool = new HashMap<String, String>();
 16  
 
 17  
     /**
 18  
      * Replaces the strings within this document with strings in a
 19  
      * string pool.
 20  
      * 
 21  
      * @param document
 22  
      */
 23  
     public void transform(Document document) {
 24  92
         for (int i = 0; i < document.terms.size(); i++) {
 25  80
             String term = document.terms.get(i);
 26  
 
 27  80
             if (term == null) {
 28  0
                 continue;
 29  
             }
 30  80
             String cached = pool.get(term);
 31  
 
 32  80
             if (cached == null) {
 33  76
                 term = new String(term);
 34  76
                 pool.put(term, term);
 35  
             } else {
 36  4
                 term = cached;
 37  
             }
 38  
 
 39  80
             document.terms.set(i, term);
 40  
         }
 41  
 
 42  
         // The choice of 10000 is arbitrary; it seemed big enough to make a difference.
 43  12
         if (pool.size() > 10000) {
 44  0
             pool.clear();
 45  
         }
 46  12
     }
 47  
 }