You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ry...@apache.org on 2010/09/25 21:32:38 UTC

svn commit: r1001303 [2/2] - in /lucene/dev/trunk/lucene: ./ src/java/org/apache/lucene/search/ src/java/org/apache/lucene/search/cache/ src/java/org/apache/lucene/util/ src/test/org/apache/lucene/search/cache/

Added: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/EntryCreatorWithOptions.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/EntryCreatorWithOptions.java?rev=1001303&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/EntryCreatorWithOptions.java (added)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/EntryCreatorWithOptions.java Sat Sep 25 19:32:37 2010
@@ -0,0 +1,41 @@
+package org.apache.lucene.search.cache;
+
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+public abstract class EntryCreatorWithOptions<T> extends EntryCreator<T>
+{
+  public static final int OPTION_VALIDATE = 1;
+
+  public int flags;
+
+  public EntryCreatorWithOptions( int flag ) {
+    this.flags = flag;
+  }
+
+  @Override
+  public boolean shouldValidate() {
+    return hasOption( OPTION_VALIDATE );
+  }
+
+  public boolean hasOption( int key )
+  {
+    return (flags & key) == key;
+  }
+}

Added: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/EntryKey.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/EntryKey.java?rev=1001303&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/EntryKey.java (added)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/EntryKey.java Sat Sep 25 19:32:37 2010
@@ -0,0 +1,9 @@
+package org.apache.lucene.search.cache;
+
+
+/**
+ * A Simple marker class -- Perhaps it could/should just be an Object
+ */
+public abstract class EntryKey {
+
+}

Added: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/FloatValuesCreator.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/FloatValuesCreator.java?rev=1001303&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/FloatValuesCreator.java (added)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/FloatValuesCreator.java Sat Sep 25 19:32:37 2010
@@ -0,0 +1,150 @@
+package org.apache.lucene.search.cache;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.IOException;
+
+import org.apache.lucene.index.DocsEnum;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.MultiFields;
+import org.apache.lucene.index.Terms;
+import org.apache.lucene.index.TermsEnum;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.FieldCache;
+import org.apache.lucene.search.FieldCache.FloatParser;
+import org.apache.lucene.search.cache.CachedArray.FloatValues;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.OpenBitSet;
+
+public class FloatValuesCreator extends CachedArrayCreator<FloatValues>
+{
+  protected FloatParser parser;
+
+  public FloatValuesCreator( String field, FloatParser parser, int options )
+  {
+    super( field, options );
+    this.parser = parser;
+  }
+
+  public FloatValuesCreator( String field, FloatParser parser )
+  {
+    super( field );
+    this.parser = parser;
+  }
+
+  @Override
+  public Class getArrayType() {
+    return Float.class;
+  }
+
+
+  //--------------------------------------------------------------------------------
+  //--------------------------------------------------------------------------------
+
+  @Override
+  public FloatValues create(IndexReader reader) throws IOException {
+    return validate( new FloatValues(), reader );
+  }
+
+  @Override
+  public FloatValues validate(FloatValues entry, IndexReader reader) throws IOException {
+    boolean ok = false;
+    if( hasOption(OPTION_CACHE_VALUES) ) {
+      ok = true;
+      if( entry.values == null ) {
+        fillFloatValues(entry, reader, field);
+      }
+    }
+    if( hasOption(OPTION_CACHE_BITS) ) {
+      ok = true;
+      if( entry.valid == null ) {
+        fillValidBits(entry, reader, field);
+      }
+    }
+    if( !ok ) {
+      throw new RuntimeException( "the config must cache values and/or bits" );
+    }
+    return entry;
+  }
+
+  protected void fillFloatValues( FloatValues vals, IndexReader reader, String field ) throws IOException
+  {
+    if( parser == null ) {
+      try {
+        parser = FieldCache.DEFAULT_FLOAT_PARSER;
+        fillFloatValues( vals, reader, field );
+        return;
+      }
+      catch (NumberFormatException ne) {
+        vals.parserHashCode = null; // wipe the previous one
+        parser = FieldCache.NUMERIC_UTILS_FLOAT_PARSER;
+        fillFloatValues( vals, reader, field );
+        return;
+      }
+    }
+    assertSameParserAndResetCounts(vals, parser);
+
+    Terms terms = MultiFields.getTerms(reader, field);
+    int maxDoc = reader.maxDoc();
+    vals.values = null;
+    if (terms != null) {
+      final TermsEnum termsEnum = terms.iterator();
+      final Bits delDocs = MultiFields.getDeletedDocs(reader);
+      OpenBitSet validBits = (hasOption(OPTION_CACHE_BITS)) ? new OpenBitSet( maxDoc ) : null;
+      DocsEnum docs = null;
+      try {
+        while(true) {
+          final BytesRef term = termsEnum.next();
+          if (term == null) {
+            break;
+          }
+          final float termval = parser.parseFloat(term);
+          docs = termsEnum.docs(delDocs, docs);
+          while (true) {
+            final int docID = docs.nextDoc();
+            if (docID == DocIdSetIterator.NO_MORE_DOCS) {
+              break;
+            }
+            if(vals.values == null) {
+              vals.values = new float[maxDoc];
+            }
+            vals.values[docID] = termval;
+            vals.numDocs++;
+            if( validBits != null ) {
+              validBits.set( docID );
+            }
+          }
+          vals.numTerms++;
+        }
+      } catch (FieldCache.StopFillCacheException stop) {}
+
+      if( vals.valid == null ) {
+        vals.valid = checkMatchAllBits( delDocs, validBits, vals.numDocs, maxDoc );
+      }
+    }
+
+    if(vals.values == null) {
+      vals.values = new float[maxDoc];
+    }
+
+    if( vals.valid == null && vals.numDocs < 1 ) {
+      vals.valid = new Bits.MatchNoBits( maxDoc );
+    }
+  }
+}
\ No newline at end of file

Added: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/IntValuesCreator.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/IntValuesCreator.java?rev=1001303&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/IntValuesCreator.java (added)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/IntValuesCreator.java Sat Sep 25 19:32:37 2010
@@ -0,0 +1,150 @@
+package org.apache.lucene.search.cache;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.IOException;
+
+import org.apache.lucene.index.DocsEnum;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.MultiFields;
+import org.apache.lucene.index.Terms;
+import org.apache.lucene.index.TermsEnum;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.FieldCache;
+import org.apache.lucene.search.FieldCache.IntParser;
+import org.apache.lucene.search.cache.CachedArray.IntValues;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.OpenBitSet;
+
+public class IntValuesCreator extends CachedArrayCreator<IntValues>
+{
+  protected IntParser parser;
+
+  public IntValuesCreator( String field, IntParser parser, int options )
+  {
+    super( field, options );
+    this.parser = parser;
+  }
+
+  public IntValuesCreator( String field, IntParser parser )
+  {
+    super( field );
+    this.parser = parser;
+  }
+
+  @Override
+  public Class getArrayType() {
+    return Integer.class;
+  }
+
+
+  //--------------------------------------------------------------------------------
+  //--------------------------------------------------------------------------------
+
+  @Override
+  public IntValues create(IndexReader reader) throws IOException {
+    return validate( new IntValues(), reader );
+  }
+
+  @Override
+  public IntValues validate(IntValues entry, IndexReader reader) throws IOException {
+    boolean ok = false;
+    if( hasOption(OPTION_CACHE_VALUES) ) {
+      ok = true;
+      if( entry.values == null ) {
+        fillIntValues(entry, reader, field);
+      }
+    }
+    if( hasOption(OPTION_CACHE_BITS) ) {
+      ok = true;
+      if( entry.valid == null ) {
+        fillValidBits(entry, reader, field);
+      }
+    }
+    if( !ok ) {
+      throw new RuntimeException( "the config must cache values and/or bits" );
+    }
+    return entry;
+  }
+
+  protected void fillIntValues( IntValues vals, IndexReader reader, String field ) throws IOException
+  {
+    if( parser == null ) {
+      try {
+        parser = FieldCache.DEFAULT_INT_PARSER;
+        fillIntValues( vals, reader, field );
+        return;
+      }
+      catch (NumberFormatException ne) {
+        vals.parserHashCode = null;
+        parser = FieldCache.NUMERIC_UTILS_INT_PARSER;
+        fillIntValues( vals, reader, field );
+        return;
+      }
+    }
+    assertSameParserAndResetCounts(vals, parser);
+    
+    Terms terms = MultiFields.getTerms(reader, field);
+    int maxDoc = reader.maxDoc();
+    vals.values = null;
+    if (terms != null) {
+      final TermsEnum termsEnum = terms.iterator();
+      final Bits delDocs = MultiFields.getDeletedDocs(reader);
+      OpenBitSet validBits = (hasOption(OPTION_CACHE_BITS)) ? new OpenBitSet( maxDoc ) : null;
+      DocsEnum docs = null;
+      try {
+        while(true) {
+          final BytesRef term = termsEnum.next();
+          if (term == null) {
+            break;
+          }
+          final int termval = parser.parseInt(term);
+          docs = termsEnum.docs(delDocs, docs);
+          while (true) {
+            final int docID = docs.nextDoc();
+            if (docID == DocIdSetIterator.NO_MORE_DOCS) {
+              break;
+            }
+            if(vals.values == null) {
+              vals.values = new int[maxDoc];
+            }
+            vals.values[docID] = termval;
+            vals.numDocs++;
+            if( validBits != null ) {
+              validBits.set( docID );
+            }
+          }
+          vals.numTerms++;
+        }
+      } catch (FieldCache.StopFillCacheException stop) {}
+
+      if( vals.valid == null ) {
+        vals.valid = checkMatchAllBits( delDocs, validBits, vals.numDocs, maxDoc );
+      }
+    }
+
+    if(vals.values == null) {
+      vals.values = new int[maxDoc];
+    }
+
+    if( vals.valid == null && vals.numDocs < 1 ) {
+      vals.valid = new Bits.MatchNoBits( maxDoc );
+    }
+  }
+}
\ No newline at end of file

Added: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/LongValuesCreator.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/LongValuesCreator.java?rev=1001303&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/LongValuesCreator.java (added)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/LongValuesCreator.java Sat Sep 25 19:32:37 2010
@@ -0,0 +1,150 @@
+package org.apache.lucene.search.cache;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.IOException;
+
+import org.apache.lucene.index.DocsEnum;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.MultiFields;
+import org.apache.lucene.index.Terms;
+import org.apache.lucene.index.TermsEnum;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.FieldCache;
+import org.apache.lucene.search.FieldCache.LongParser;
+import org.apache.lucene.search.cache.CachedArray.LongValues;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.OpenBitSet;
+
+public class LongValuesCreator extends CachedArrayCreator<LongValues>
+{
+  protected LongParser parser;
+
+  public LongValuesCreator( String field, LongParser parser, int options )
+  {
+    super( field, options );
+    this.parser = parser;
+  }
+
+  public LongValuesCreator( String field, LongParser parser )
+  {
+    super( field );
+    this.parser = parser;
+  }
+
+  @Override
+  public Class getArrayType() {
+    return Long.class;
+  }
+
+
+  //--------------------------------------------------------------------------------
+  //--------------------------------------------------------------------------------
+
+  @Override
+  public LongValues create(IndexReader reader) throws IOException {
+    return validate( new LongValues(), reader );
+  }
+
+  @Override
+  public LongValues validate(LongValues entry, IndexReader reader) throws IOException {
+    boolean ok = false;
+    if( hasOption(OPTION_CACHE_VALUES) ) {
+      ok = true;
+      if( entry.values == null ) {
+        fillLongValues(entry, reader, field);
+      }
+    }
+    if( hasOption(OPTION_CACHE_BITS) ) {
+      ok = true;
+      if( entry.valid == null ) {
+        fillValidBits(entry, reader, field);
+      }
+    }
+    if( !ok ) {
+      throw new RuntimeException( "the config must cache values and/or bits" );
+    }
+    return entry;
+  }
+
+  protected void fillLongValues( LongValues vals, IndexReader reader, String field ) throws IOException
+  {
+    if( parser == null ) {
+      try {
+        parser = FieldCache.DEFAULT_LONG_PARSER;
+        fillLongValues( vals, reader, field );
+        return;
+      }
+      catch (NumberFormatException ne) {
+        vals.parserHashCode = null; // wipe the previous one
+        parser = FieldCache.NUMERIC_UTILS_LONG_PARSER;
+        fillLongValues( vals, reader, field );
+        return;
+      }
+    }
+    assertSameParserAndResetCounts(vals, parser);
+
+    Terms terms = MultiFields.getTerms(reader, field);
+    int maxDoc = reader.maxDoc();
+    vals.values = null;
+    if (terms != null) {
+      final TermsEnum termsEnum = terms.iterator();
+      final Bits delDocs = MultiFields.getDeletedDocs(reader);
+      OpenBitSet validBits = (hasOption(OPTION_CACHE_BITS)) ? new OpenBitSet( maxDoc ) : null;
+      DocsEnum docs = null;
+      try {
+        while(true) {
+          final BytesRef term = termsEnum.next();
+          if (term == null) {
+            break;
+          }
+          final long termval = parser.parseLong(term);
+          docs = termsEnum.docs(delDocs, docs);
+          while (true) {
+            final int docID = docs.nextDoc();
+            if (docID == DocIdSetIterator.NO_MORE_DOCS) {
+              break;
+            }
+            if(vals.values == null) {
+              vals.values = new long[maxDoc];
+            }
+            vals.values[docID] = termval;
+            vals.numDocs++;
+            if( validBits != null ) {
+              validBits.set( docID );
+            }
+          }
+          vals.numTerms++;
+        }
+      } catch (FieldCache.StopFillCacheException stop) {}
+
+      if( vals.valid == null ) {
+        vals.valid = checkMatchAllBits( delDocs, validBits, vals.numDocs, maxDoc );
+      }
+    }
+
+    if(vals.values == null) {
+      vals.values = new long[maxDoc];
+    }
+
+    if( vals.valid == null && vals.numDocs < 1 ) {
+      vals.valid = new Bits.MatchNoBits( maxDoc );
+    }
+  }
+}
\ No newline at end of file

Added: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/ShortValuesCreator.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/ShortValuesCreator.java?rev=1001303&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/ShortValuesCreator.java (added)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/ShortValuesCreator.java Sat Sep 25 19:32:37 2010
@@ -0,0 +1,132 @@
+package org.apache.lucene.search.cache;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.IOException;
+
+import org.apache.lucene.index.DocsEnum;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.MultiFields;
+import org.apache.lucene.index.Terms;
+import org.apache.lucene.index.TermsEnum;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.FieldCache;
+import org.apache.lucene.search.FieldCache.ShortParser;
+import org.apache.lucene.search.cache.CachedArray.ShortValues;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.OpenBitSet;
+
+public class ShortValuesCreator extends CachedArrayCreator<ShortValues>
+{
+  protected ShortParser parser;
+
+  public ShortValuesCreator( String field, ShortParser parser, int options )
+  {
+    super( field, options );
+    this.parser = parser;
+  }
+
+  public ShortValuesCreator( String field, ShortParser parser )
+  {
+    super( field );
+    this.parser = parser;
+  }
+
+  @Override
+  public Class getArrayType() {
+    return Short.class;
+  }
+
+
+  //--------------------------------------------------------------------------------
+  //--------------------------------------------------------------------------------
+
+  @Override
+  public ShortValues create(IndexReader reader) throws IOException {
+    return validate( new ShortValues(), reader );
+  }
+
+  @Override
+  public ShortValues validate(ShortValues entry, IndexReader reader) throws IOException {
+    boolean ok = false;
+    if( hasOption(OPTION_CACHE_VALUES) ) {
+      ok = true;
+      if( entry.values == null ) {
+        fillShortValues(entry, reader, field);
+      }
+    }
+    if( hasOption(OPTION_CACHE_BITS) ) {
+      ok = true;
+      if( entry.valid == null ) {
+        fillValidBits(entry, reader, field);
+      }
+    }
+    if( !ok ) {
+      throw new RuntimeException( "the config must cache values and/or bits" );
+    }
+    return entry;
+  }
+
+  protected void fillShortValues( ShortValues vals, IndexReader reader, String field ) throws IOException
+  {
+    if( parser == null ) {
+      parser = FieldCache.DEFAULT_SHORT_PARSER;
+    }
+    assertSameParserAndResetCounts(vals, parser);
+
+    Terms terms = MultiFields.getTerms(reader, field);
+    int maxDoc = reader.maxDoc();
+    vals.values = new short[maxDoc];
+    if (terms != null) {
+      final TermsEnum termsEnum = terms.iterator();
+      final Bits delDocs = MultiFields.getDeletedDocs(reader);
+      OpenBitSet validBits = (hasOption(OPTION_CACHE_BITS)) ? new OpenBitSet( maxDoc ) : null;
+      DocsEnum docs = null;
+      try {
+        while(true) {
+          final BytesRef term = termsEnum.next();
+          if (term == null) {
+            break;
+          }
+          final Short termval = parser.parseShort(term);
+          docs = termsEnum.docs(delDocs, docs);
+          while (true) {
+            final int docID = docs.nextDoc();
+            if (docID == DocIdSetIterator.NO_MORE_DOCS) {
+              break;
+            }
+            vals.values[docID] = termval;
+            vals.numDocs++;
+            if( validBits != null ) {
+              validBits.set( docID );
+            }
+          }
+          vals.numTerms++;
+        }
+      } catch (FieldCache.StopFillCacheException stop) {}
+
+      if( vals.valid == null ) {
+        vals.valid = checkMatchAllBits( delDocs, validBits, vals.numDocs, maxDoc );
+      }
+    }
+    if( vals.valid == null && vals.numDocs < 1 ) {
+      vals.valid = new Bits.MatchNoBits( maxDoc );
+    }
+  }
+}
\ No newline at end of file

Added: lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/SimpleEntryKey.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/SimpleEntryKey.java?rev=1001303&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/SimpleEntryKey.java (added)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/cache/SimpleEntryKey.java Sat Sep 25 19:32:37 2010
@@ -0,0 +1,59 @@
+package org.apache.lucene.search.cache;
+
+public class SimpleEntryKey extends EntryKey
+{
+  public final Class clazz;
+  public final Object[] args;
+  public final int hash;
+
+  public SimpleEntryKey( Class clazz, Object ... args ) {
+    this.clazz = clazz;
+    this.args = args;
+
+    int hash = clazz.hashCode();
+    if( args != null ) {
+      for( Object obj : args ) {
+        hash ^= obj.hashCode();
+      }
+    }
+    this.hash = hash;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if( obj instanceof SimpleEntryKey ) {
+      SimpleEntryKey key = (SimpleEntryKey)obj;
+      if( key.hash != hash ||
+          key.clazz != clazz ||
+          key.args.length != args.length ) {
+        return false;
+      }
+
+      // In the off chance that the hash etc is all the same
+      // we should actually check the values
+      for( int i=0; i<args.length; i++ ) {
+        if( !args[i].equals( key.args[i] ) ) {
+          return false;
+        }
+      }
+      return true;
+    }
+    return false;
+  }
+
+  @Override
+  public int hashCode() {
+    return hash;
+  }
+
+  @Override
+  public String toString() {
+    StringBuilder str = new StringBuilder();
+    str.append( '[' ).append( clazz.getName() ).append( ':' );
+    for( Object v : args ) {
+      str.append( v ).append( ':' );
+    }
+    str.append( hash ).append( ']' );
+    return str.toString();
+  }
+}

Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/Bits.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/Bits.java?rev=1001303&r1=1001302&r2=1001303&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/Bits.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/util/Bits.java Sat Sep 25 19:32:37 2010
@@ -26,4 +26,36 @@ public interface Bits {
   public int length();
 
   public static final Bits[] EMPTY_ARRAY = new Bits[0];
+  
+  public static class MatchAllBits implements Bits {
+    final int len;
+    
+    public MatchAllBits( int len ) {
+      this.len = len;
+    }
+
+    public boolean get(int index) {
+      return true;
+    }
+
+    public int length() {
+      return len;
+    }
+  }
+
+  public static class MatchNoBits implements Bits {
+    final int len;
+    
+    public MatchNoBits( int len ) {
+      this.len = len;
+    }
+
+    public boolean get(int index) {
+      return false;
+    }
+
+    public int length() {
+      return len;
+    }
+  }
 }

Added: lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/cache/TestEntryCreators.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/cache/TestEntryCreators.java?rev=1001303&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/cache/TestEntryCreators.java (added)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/search/cache/TestEntryCreators.java Sat Sep 25 19:32:37 2010
@@ -0,0 +1,190 @@
+package org.apache.lucene.search.cache;
+
+/**
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.IOException;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.search.FieldCache;
+import org.apache.lucene.search.FieldCache.*;
+import org.apache.lucene.search.FieldCache.Parser;
+import org.apache.lucene.search.FieldCache.ShortParser;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.OpenBitSet;
+
+import static org.hamcrest.CoreMatchers.*;
+
+public class TestEntryCreators extends LuceneTestCase {
+  protected IndexReader reader;
+  private static final int NUM_DOCS = 500 * RANDOM_MULTIPLIER;
+  private Directory directory;
+
+  static class NumberTypeTester {
+    String funcName;
+    Class<? extends CachedArrayCreator> creator;
+    Class<? extends Parser> parser;
+    String field;
+    Number[] values;
+    
+    public NumberTypeTester( String f, String func, Class<? extends CachedArrayCreator> creator, Class<? extends Parser> parser ) {
+      field = f;
+      funcName = func;
+      this.creator = creator;
+      this.parser = parser;
+      values = new Number[NUM_DOCS];
+    }
+    public String toString()
+    {
+      return field;
+    }
+  }
+  private NumberTypeTester[] typeTests;
+  
+  
+  @Override
+  public void setUp() throws Exception {
+    super.setUp();
+    directory = newDirectory();
+    RandomIndexWriter writer= new RandomIndexWriter(random, directory);
+
+    typeTests = new NumberTypeTester[] {
+        new NumberTypeTester( "theRandomByte",   "getBytes",   ByteValuesCreator.class,   ByteParser.class ),
+        new NumberTypeTester( "theRandomShort",  "getShorts",  ShortValuesCreator.class,  ShortParser.class  ),
+        new NumberTypeTester( "theRandomInt",    "getInts",    IntValuesCreator.class,    IntParser.class  ),
+        new NumberTypeTester( "theRandomLong",   "getLongs",   LongValuesCreator.class,   LongParser.class  ),
+        new NumberTypeTester( "theRandomFloat",  "getFloats",  FloatValuesCreator.class,  FloatParser.class  ),
+        new NumberTypeTester( "theRandomDouble", "getDoubles", DoubleValuesCreator.class, DoubleParser.class  ),
+    };
+    
+    for (int i = 0; i < NUM_DOCS; i++){
+      Document doc = new Document();
+      
+      // Test the valid bits
+      for( NumberTypeTester tester : typeTests ) {
+        if (random.nextInt(20) != 17 && i > 1) {
+          tester.values[i] = 10 + random.nextInt( 20 ); // get some field overlap
+          doc.add(newField(tester.field, String.valueOf(tester.values[i]), 
+              Field.Store.NO, Field.Index.NOT_ANALYZED ));
+        }
+      }
+      writer.addDocument(doc);
+    }
+
+    reader = writer.getReader();
+    writer.close();
+  }
+
+  @Override
+  public void tearDown() throws Exception {
+    reader.close();
+    directory.close();
+    super.tearDown();
+  }
+
+  public void testKeys() throws IOException {
+    // Check that the keys are unique for different fields
+
+    EntryKey key_1 = new ByteValuesCreator( "field1", null ).getCacheKey();
+    EntryKey key_2 = new ByteValuesCreator( "field2", null ).getCacheKey();
+    assertThat("different fields should have a different key", key_1, not(key_2) );
+
+    key_1 = new ByteValuesCreator(  "field1", null ).getCacheKey();
+    key_2 = new ShortValuesCreator( "field1", null ).getCacheKey();
+    assertThat( "same field different type should have different key", key_1, not( key_2 ) );
+
+    key_1 = new ByteValuesCreator( "ff", null ).getCacheKey();
+    key_2 = new ByteValuesCreator( "ff", null ).getCacheKey();
+    assertThat( "same args should have same key", key_1, is( key_2 ) );
+
+    key_1 = new ByteValuesCreator( "ff", null, ByteValuesCreator.OPTION_CACHE_BITS ^ ByteValuesCreator.OPTION_CACHE_VALUES ).getCacheKey();
+    key_2 = new ByteValuesCreator( "ff", null ).getCacheKey();
+    assertThat( "different options should share same key", key_1, is( key_2 ) );
+
+    key_1 = new IntValuesCreator( "ff", FieldCache.DEFAULT_INT_PARSER ).getCacheKey();
+    key_2 = new IntValuesCreator( "ff", FieldCache.NUMERIC_UTILS_INT_PARSER ).getCacheKey();
+    assertThat( "diferent parser should have same key", key_1, is( key_2 ) );
+  }
+  
+  private CachedArray getWithReflection( FieldCache cache, NumberTypeTester tester, int flags ) throws IOException 
+  {
+    try {
+      Method getXXX = cache.getClass().getMethod( tester.funcName, IndexReader.class, String.class, EntryCreator.class );
+      Constructor constructor = tester.creator.getConstructor( String.class, tester.parser, Integer.TYPE );
+      CachedArrayCreator creator = (CachedArrayCreator)constructor.newInstance( tester.field, null, flags );
+      return (CachedArray) getXXX.invoke(cache, reader, tester.field, creator );
+    }
+    catch( Exception ex ) {
+      throw new RuntimeException( "Reflection failed", ex );
+    }
+  }
+
+  public void testCachedArrays() throws IOException 
+  {
+    FieldCache cache = FieldCache.DEFAULT;
+
+    // Check the Different CachedArray Types
+    CachedArray last = null;
+    CachedArray justbits = null;
+    
+    for( NumberTypeTester tester : typeTests ) {
+      justbits = getWithReflection( cache, tester, CachedArrayCreator.OPTION_CACHE_BITS );
+      assertNull( "should not get values : "+tester, justbits.getRawArray() );
+      assertNotNull( "should get bits : "+tester, justbits.valid );
+      last = getWithReflection( cache, tester, CachedArrayCreator.CACHE_VALUES_AND_BITS );
+      assertEquals( "should use same cached object : "+tester, justbits, last );
+      assertNull( "Validate=false shoudl not regenerate : "+tester, justbits.getRawArray() ); 
+      last = getWithReflection( cache, tester, CachedArrayCreator.CACHE_VALUES_AND_BITS_VALIDATE );
+      assertEquals( "should use same cached object : "+tester, justbits, last );
+      assertNotNull( "Validate=true should add the Array : "+tester, justbits.getRawArray() ); 
+      checkCachedArrayValuesAndBits( tester, last );
+    }
+  }
+
+  private void checkCachedArrayValuesAndBits( NumberTypeTester tester, CachedArray cachedVals )
+  {
+//    for( int i=0; i<NUM_DOCS; i++ ) {
+//      System.out.println( i + "] "+ tester.values[i] + " :: " + cachedVals.valid.get(i) );
+//    }
+    
+    int numDocs =0;
+    Set<Number> distinctTerms = new HashSet<Number>();
+    for( int i=0; i<NUM_DOCS; i++ ) {
+      Number v = tester.values[i];
+      boolean isValid = cachedVals.valid.get(i);
+      if( v != null ) {
+        numDocs++;
+        distinctTerms.add( v );
+        assertTrue( "Valid bit should be true ("+i+"="+tester.values[i]+") "+tester, isValid );        
+      }
+      else {
+        assertFalse( "Valid bit should be false ("+i+") "+tester, isValid );        
+      }
+    }
+    assertEquals( "Cached numTerms does not match : "+tester, distinctTerms.size(), cachedVals.numTerms );
+    assertEquals( "Cached numDocs does not match : "+tester, numDocs, cachedVals.numDocs );
+    assertEquals( "Ordinal should match numDocs : "+tester, numDocs, ((OpenBitSet)cachedVals.valid).cardinality() );
+  }
+
+}