From 887c3a2d72cbebf95c7454811508bc823d984437 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Wed, 13 Feb 2013 06:19:31 +0000 Subject: [PATCH] fix nocommits git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene4765@1445478 13f79535-47bb-0310-9956-ffa450edef68 --- .../index/SingletonSortedSetDocValues.java | 64 +++++++++++++++++++ .../lucene/index/SortedSetDocValues.java | 2 +- .../apache/lucene/search/FieldCacheImpl.java | 10 ++- .../apache/lucene/search/TestFieldCache.java | 9 ++- .../lucene/index/AssertingAtomicReader.java | 6 +- 5 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 lucene/core/src/java/org/apache/lucene/index/SingletonSortedSetDocValues.java diff --git a/lucene/core/src/java/org/apache/lucene/index/SingletonSortedSetDocValues.java b/lucene/core/src/java/org/apache/lucene/index/SingletonSortedSetDocValues.java new file mode 100644 index 00000000000..76c5a3680a7 --- /dev/null +++ b/lucene/core/src/java/org/apache/lucene/index/SingletonSortedSetDocValues.java @@ -0,0 +1,64 @@ +package org.apache.lucene.index; + +/* + * 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 org.apache.lucene.util.BytesRef; + +/** + * Exposes multi-valued view over a single-valued instance. + *

+ * This can be used if you want to have one multi-valued implementation + * against e.g. FieldCache.getDocTermOrds that also works for single-valued + * fields. + */ +public class SingletonSortedSetDocValues extends SortedSetDocValues { + private final SortedDocValues in; + private int docID; + + /** Creates a multi-valued view over the provided SortedDocValues */ + public SingletonSortedSetDocValues(SortedDocValues in) { + this.in = in; + assert NO_MORE_ORDS == -1; // this allows our nextOrd() to work for missing values without a check + } + + @Override + public long nextOrd() { + return in.getOrd(docID); + } + + @Override + public void setDocument(int docID) { + this.docID = docID; + } + + @Override + public void lookupOrd(long ord, BytesRef result) { + // cast is ok: single-valued cannot exceed Integer.MAX_VALUE + in.lookupOrd((int)ord, result); + } + + @Override + public long getValueCount() { + return in.getValueCount(); + } + + @Override + public long lookupTerm(BytesRef key) { + return in.lookupTerm(key); + } +} diff --git a/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValues.java b/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValues.java index cd6d5a75d31..4279b74b28b 100644 --- a/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValues.java +++ b/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValues.java @@ -33,7 +33,7 @@ public abstract class SortedSetDocValues { * constructors, typically implicit.) */ protected SortedSetDocValues() {} - public static final long NO_MORE_ORDS = Long.MAX_VALUE; + public static final long NO_MORE_ORDS = -1; /** * Returns the next ordinal for the current document (previously diff --git a/lucene/core/src/java/org/apache/lucene/search/FieldCacheImpl.java b/lucene/core/src/java/org/apache/lucene/search/FieldCacheImpl.java index 0898a2a5709..c06204b2c69 100644 --- a/lucene/core/src/java/org/apache/lucene/search/FieldCacheImpl.java +++ b/lucene/core/src/java/org/apache/lucene/search/FieldCacheImpl.java @@ -33,6 +33,7 @@ import org.apache.lucene.index.FieldInfo; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.NumericDocValues; import org.apache.lucene.index.SegmentReader; +import org.apache.lucene.index.SingletonSortedSetDocValues; import org.apache.lucene.index.SortedDocValues; import org.apache.lucene.index.SortedSetDocValues; import org.apache.lucene.index.Terms; @@ -1304,14 +1305,18 @@ class FieldCacheImpl implements FieldCache { } } + // TODO: this if DocTermsIndex was already created, we + // should share it... public SortedSetDocValues getDocTermOrds(AtomicReader reader, String field) throws IOException { SortedSetDocValues dv = reader.getSortedSetDocValues(field); if (dv != null) { return dv; } - // nocommit: actually if they have a SortedDV (either indexed as DV or cached), we should return an impl - // over that: its like a specialized single-value case of this thing... + SortedDocValues sdv = reader.getSortedDocValues(field); + if (sdv != null) { + return new SingletonSortedSetDocValues(sdv); + } DocTermOrds dto = (DocTermOrds) caches.get(DocTermOrds.class).get(reader, new CacheKey(field, null), false); return dto.iterator(dto.getOrdTermsEnum(reader)); @@ -1325,7 +1330,6 @@ class FieldCacheImpl implements FieldCache { @Override protected Object createValue(AtomicReader reader, CacheKey key, boolean setDocsWithField /* ignored */) throws IOException { - // No DocValues impl yet (DocValues are single valued...): return new DocTermOrds(reader, key.field); } } diff --git a/lucene/core/src/test/org/apache/lucene/search/TestFieldCache.java b/lucene/core/src/test/org/apache/lucene/search/TestFieldCache.java index 9a8158c3523..d566adb27ae 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestFieldCache.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestFieldCache.java @@ -254,8 +254,11 @@ public class TestFieldCache extends LuceneTestCase { // getDocTermOrds SortedSetDocValues termOrds = cache.getDocTermOrds(reader, "theRandomUnicodeMultiValuedField"); - // nocommit: test this with reflection or something, that its really from the same DTO - // assertSame("Second request to cache return same DocTermOrds", termOrds, cache.getDocTermOrds(reader, "theRandomUnicodeMultiValuedField")); + int numEntries = cache.getCacheEntries().length; + // ask for it again, and check that we didnt create any additional entries: + termOrds = cache.getDocTermOrds(reader, "theRandomUnicodeMultiValuedField"); + assertEquals(numEntries, cache.getCacheEntries().length); + for (int i = 0; i < NUM_DOCS; i++) { termOrds.setDocument(i); // This will remove identical terms. A DocTermOrds doesn't return duplicate ords for a docId @@ -275,8 +278,8 @@ public class TestFieldCache extends LuceneTestCase { } // test bad field - // nocommit: what exactly does this test? termOrds = cache.getDocTermOrds(reader, "bogusfield"); + assertTrue(termOrds.getValueCount() == 0); FieldCache.DEFAULT.purge(reader); } diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java b/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java index 2734f40a091..7bcd9f616dd 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java +++ b/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java @@ -472,8 +472,8 @@ public class AssertingAtomicReader extends FilterAtomicReader { public long nextOrd() { assert lastOrd != NO_MORE_ORDS; long ord = in.nextOrd(); - assert ord == NO_MORE_ORDS || ord < valueCount; - assert ord > lastOrd; + assert ord < valueCount; + assert ord == NO_MORE_ORDS || ord > lastOrd; lastOrd = ord; return ord; } @@ -482,7 +482,7 @@ public class AssertingAtomicReader extends FilterAtomicReader { public void setDocument(int docID) { assert docID >= 0 && docID < maxDoc : "docid=" + docID + ",maxDoc=" + maxDoc; in.setDocument(docID); - lastOrd = -1; + lastOrd = -2; } @Override