Apache Solr open-source search software
LUCENE-5969: let AssertingCodec implement RandomAccessOrds, and test it directly too
Robert Muir6efd37464f69
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingDocValuesFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingDocValuesFormat.javanew file mode 100644index 00000000000..c15ae9be08f--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingDocValuesFormat.java@@ -0,0 +1,31 @@+package org.apache.lucene.codecs.asserting;++/*+ * 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.codecs.Codec;+import org.apache.lucene.index.BasePostingsFormatTestCase;++/** Test AssertingPostingsFormat directly */+public class TestAssertingDocValuesFormat extends BasePostingsFormatTestCase {+ private final Codec codec = new AssertingCodec();++ @Override+ protected Codec getCodec() {+ return codec;+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingNormsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingNormsFormat.javanew file mode 100644index 00000000000..d5adad045a7--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingNormsFormat.java@@ -0,0 +1,31 @@+package org.apache.lucene.codecs.asserting;++/*+ * 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.codecs.Codec;+import org.apache.lucene.index.BaseNormsFormatTestCase;++/** Test AssertingNormsFormat directly */+public class TestAssertingNormsFormat extends BaseNormsFormatTestCase {+ private final Codec codec = new AssertingCodec();++ @Override+ protected Codec getCodec() {+ return codec;+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingPostingsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingPostingsFormat.javanew file mode 100644index 00000000000..4c004114b6c--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingPostingsFormat.java@@ -0,0 +1,31 @@+package org.apache.lucene.codecs.asserting;++/*+ * 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.codecs.Codec;+import org.apache.lucene.index.BaseDocValuesFormatTestCase;++/** Test AssertingDocValuesFormat directly */+public class TestAssertingPostingsFormat extends BaseDocValuesFormatTestCase {+ private final Codec codec = new AssertingCodec();++ @Override+ protected Codec getCodec() {+ return codec;+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingStoredFieldsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingStoredFieldsFormat.javanew file mode 100644index 00000000000..73fcf93348f--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingStoredFieldsFormat.java@@ -0,0 +1,31 @@+package org.apache.lucene.codecs.asserting;++/*+ * 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.codecs.Codec;+import org.apache.lucene.index.BaseTermVectorsFormatTestCase;++/** Test AssertingTermVectorsFormat directly */+public class TestAssertingStoredFieldsFormat extends BaseTermVectorsFormatTestCase {+ private final Codec codec = new AssertingCodec();++ @Override+ protected Codec getCodec() {+ return codec;+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingTermVectorsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingTermVectorsFormat.javanew file mode 100644index 00000000000..2717efda0e4--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/codecs/asserting/TestAssertingTermVectorsFormat.java@@ -0,0 +1,31 @@+package org.apache.lucene.codecs.asserting;++/*+ * 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.codecs.Codec;+import org.apache.lucene.index.BaseStoredFieldsFormatTestCase;++/** Test AssertingStoredFieldsFormat directly */+public class TestAssertingTermVectorsFormat extends BaseStoredFieldsFormatTestCase {+ private final Codec codec = new AssertingCodec();++ @Override+ protected Codec getCodec() {+ return codec;+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSortedSetSelector.java b/lucene/core/src/test/org/apache/lucene/search/TestSortedSetSelector.javaindex 7b2f48c6635..101ec40ae80 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestSortedSetSelector.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestSortedSetSelector.java@@ -17,9 +17,6 @@ package org.apache.lucene.search;* limitations under the License.*/-import org.apache.lucene.codecs.Codec;-import org.apache.lucene.codecs.lucene410.Lucene410DocValuesFormat;-import org.apache.lucene.codecs.memory.DirectDocValuesFormat;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.SortedSetDocValuesField;@@ -28,30 +25,12 @@ import org.apache.lucene.index.RandomIndexWriter;import org.apache.lucene.store.Directory;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.LuceneTestCase;-import org.apache.lucene.util.TestUtil;-import org.junit.AfterClass;-import org.junit.BeforeClass;+import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;/** Tests for SortedSetSortField selectors other than MIN,* these require optional codec support (random access to ordinals) */+@SuppressCodecs("Memory") // TODO: fix MemoryDV to support random access to ordinalspublic class TestSortedSetSelector extends LuceneTestCase {- static Codec savedCodec;-- @BeforeClass- public static void beforeClass() throws Exception {- savedCodec = Codec.getDefault();- // currently only these codecs that support random access ordinals- int victim = random().nextInt(2);- switch(victim) {- case 0: Codec.setDefault(TestUtil.alwaysDocValuesFormat(new DirectDocValuesFormat())); break;- default: Codec.setDefault(TestUtil.alwaysDocValuesFormat(new Lucene410DocValuesFormat()));- }- }-- @AfterClass- public static void afterClass() throws Exception {- Codec.setDefault(savedCodec);- }public void testMax() throws Exception {Directory dir = newDirectory();diff --git a/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingDocValuesFormat.java b/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingDocValuesFormat.javaindex ffa56c0ea75..661f99c4274 100644--- a/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingDocValuesFormat.java+++ b/lucene/test-framework/src/java/org/apache/lucene/codecs/asserting/AssertingDocValuesFormat.java@@ -28,11 +28,14 @@ import org.apache.lucene.index.AssertingAtomicReader;import org.apache.lucene.index.BinaryDocValues;import org.apache.lucene.index.FieldInfo;import org.apache.lucene.index.NumericDocValues;+import org.apache.lucene.index.RandomAccessOrds;import org.apache.lucene.index.SegmentReadState;import org.apache.lucene.index.SegmentWriteState;import org.apache.lucene.index.SortedDocValues;import org.apache.lucene.index.SortedNumericDocValues;import org.apache.lucene.index.SortedSetDocValues;+import org.apache.lucene.index.AssertingAtomicReader.AssertingRandomAccessOrds;+import org.apache.lucene.index.AssertingAtomicReader.AssertingSortedSetDocValues;import org.apache.lucene.util.Accountable;import org.apache.lucene.util.Bits;import org.apache.lucene.util.BytesRef;@@ -258,7 +261,11 @@ public class AssertingDocValuesFormat extends DocValuesFormat {assert field.getDocValuesType() == FieldInfo.DocValuesType.SORTED_SET;SortedSetDocValues values = in.getSortedSet(field);assert values != null;- return new AssertingAtomicReader.AssertingSortedSetDocValues(values, maxDoc);+ if (values instanceof RandomAccessOrds) {+ return new AssertingRandomAccessOrds((RandomAccessOrds) values, maxDoc);+ } else {+ return new AssertingSortedSetDocValues(values, maxDoc);+ }}@Overridediff --git a/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.java b/lucene/test-framework/src/java/org/apache/lucene/index/AssertingAtomicReader.javaindex cd9b163ae51..b669f75a1c2 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@@ -546,6 +546,77 @@ public class AssertingAtomicReader extends FilterAtomicReader {}}+ /** Wraps a RandomAccessOrds but with additional asserts */+ public static class AssertingRandomAccessOrds extends RandomAccessOrds {+ private final RandomAccessOrds in;+ private final int maxDoc;+ private final long valueCount;+ long lastOrd = NO_MORE_ORDS;++ public AssertingRandomAccessOrds(RandomAccessOrds in, int maxDoc) {+ this.in = in;+ this.maxDoc = maxDoc;+ this.valueCount = in.getValueCount();+ assert valueCount >= 0;+ }++ @Override+ public long nextOrd() {+ assert lastOrd != NO_MORE_ORDS;+ long ord = in.nextOrd();+ assert ord < valueCount;+ assert ord == NO_MORE_ORDS || ord > lastOrd;+ lastOrd = ord;+ return ord;+ }++ @Override+ public void setDocument(int docID) {+ assert docID >= 0 && docID < maxDoc : "docid=" + docID + ",maxDoc=" + maxDoc;+ in.setDocument(docID);+ lastOrd = -2;+ }++ @Override+ public BytesRef lookupOrd(long ord) {+ assert ord >= 0 && ord < valueCount;+ final BytesRef result = in.lookupOrd(ord);+ assert result.isValid();+ return result;+ }++ @Override+ public long getValueCount() {+ long valueCount = in.getValueCount();+ assert valueCount == this.valueCount; // should not change+ return valueCount;+ }++ @Override+ public long lookupTerm(BytesRef key) {+ assert key.isValid();+ long result = in.lookupTerm(key);+ assert result < valueCount;+ assert key.isValid();+ return result;+ }++ @Override+ public long ordAt(int index) {+ assert index < in.cardinality();+ long ord = in.ordAt(index);+ assert ord >= 0 && ord < valueCount;+ return ord;+ }++ @Override+ public int cardinality() {+ int cardinality = in.cardinality();+ assert cardinality >= 0;+ return cardinality;+ }+ }+/** Wraps a SortedSetDocValues but with additional asserts */public static class AssertingSortedSetDocValues extends SortedSetDocValues {private final SortedSetDocValues in;@@ -665,7 +736,11 @@ public class AssertingAtomicReader extends FilterAtomicReader {if (dv != null) {assert fi != null;assert fi.getDocValuesType() == FieldInfo.DocValuesType.SORTED_SET;- return new AssertingSortedSetDocValues(dv, maxDoc());+ if (dv instanceof RandomAccessOrds) {+ return new AssertingRandomAccessOrds((RandomAccessOrds) dv, maxDoc());+ } else {+ return new AssertingSortedSetDocValues(dv, maxDoc());+ }} else {assert fi == null || fi.getDocValuesType() != FieldInfo.DocValuesType.SORTED_SET;return null;