Apache Lucene open-source search software
catch up with trunk
Simon Willnauer45e10d5ac953
diff --cc lucene/src/java/org/apache/lucene/index/codecs/docvalues/DocValuesCodec.javaindex dec6204f30b,00000000000..75b330daa65mode 100644,000000..100644--- a/lucene/src/java/org/apache/lucene/index/codecs/docvalues/DocValuesCodec.java+++ b/lucene/src/java/org/apache/lucene/index/codecs/docvalues/DocValuesCodec.java@@@ -1,295 -1,0 +1,296 @@@+package org.apache.lucene.index.codecs.docvalues;++/**+ * 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 java.util.ArrayList;+import java.util.HashMap;+import java.util.HashSet;+import java.util.Iterator;+import java.util.List;+import java.util.Map;+import java.util.Set;+import java.util.Map.Entry;++import org.apache.lucene.index.FieldInfo;- import org.apache.lucene.index.FieldInfos;+import org.apache.lucene.index.FieldsEnum;+import org.apache.lucene.index.SegmentInfo;+import org.apache.lucene.index.SegmentReadState;+import org.apache.lucene.index.SegmentWriteState;+import org.apache.lucene.index.Terms;+import org.apache.lucene.index.TermsEnum;+import org.apache.lucene.index.codecs.Codec;+import org.apache.lucene.index.codecs.FieldsConsumer;+import org.apache.lucene.index.codecs.FieldsProducer;+import org.apache.lucene.index.codecs.TermsConsumer;+import org.apache.lucene.index.values.DocValues;+import org.apache.lucene.index.values.Writer;+import org.apache.lucene.store.Directory;+import org.apache.lucene.util.AttributeSource;++/**+ * A codec that adds DocValues support to a given codec transparently.+ */+public class DocValuesCodec extends Codec {+ private final Map<String, WrappingFieldsConsumer> consumers = new HashMap<String, WrappingFieldsConsumer>();+ private final Codec other;++ public DocValuesCodec(Codec other) {+ this.name = "docvalues_" + other.name;+ this.other = other;+ }++ @Override+ public FieldsConsumer fieldsConsumer(SegmentWriteState state)+ throws IOException {+ WrappingFieldsConsumer consumer;+ if ((consumer = consumers.get(state.segmentName)) == null) {+ consumer = new WrappingFieldsConsumer(other);+ }+ consumer.state = state; // nocommit this is a hack and only necessary since+ // we want to initialized the wrapped+ // fieldsConsumer lazily with a SegmentWriteState created after the docvalue+ // ones is. We should fix this in DocumentWriter I guess. See+ // DocFieldProcessor too!+ return consumer;+ }++ private static class WrappingFieldsConsumer extends FieldsConsumer {+ SegmentWriteState state;+ private final List<DocValuesConsumer> docValuesConsumers = new ArrayList<DocValuesConsumer>();+ private FieldsConsumer wrappedConsumer;+ private final Codec other;++ public WrappingFieldsConsumer(Codec other) {+ this.other = other;+ }++ @Override+ public void close() throws IOException {+ synchronized (this) {+ if (wrappedConsumer != null)+ wrappedConsumer.close();+ }+ }++ @Override+ public synchronized DocValuesConsumer addValuesField(FieldInfo field)+ throws IOException {+ DocValuesConsumer consumer = DocValuesConsumer.create(state.segmentName,- state.directory, field, null); // TODO: set comparator here++ // TODO: set comparator here++ //TODO can we have a compound file per segment and codec for docvalues?++ state.directory, field, state.codecId +"-"+ field.number, null);+ docValuesConsumers.add(consumer);+ return consumer;+ }++ @Override+ public TermsConsumer addField(FieldInfo field) throws IOException {+ synchronized (this) {+ if (wrappedConsumer == null)+ wrappedConsumer = other.fieldsConsumer(state);+ }+ return wrappedConsumer.addField(field);+ }+ }++ @Override+ public FieldsProducer fieldsProducer(SegmentReadState state)+ throws IOException {+ Directory dir = state.dir;+ Set<String> files = new HashSet<String>();+- other.files(dir, state.segmentInfo, files);++ other.files(dir, state.segmentInfo, state.codecId, files);+ for (String string : files) {+ if (dir.fileExists(string))- return new WrappingFielsdProducer(state.segmentInfo, state.dir,- state.fieldInfos, other.fieldsProducer(state));++ return new WrappingFielsdProducer(state, other.fieldsProducer(state));+ }- return new WrappingFielsdProducer(state.segmentInfo, state.dir,- state.fieldInfos, FieldsProducer.EMPTY);++ return new WrappingFielsdProducer(state, FieldsProducer.EMPTY);++ }++ @Override- public void files(Directory dir, SegmentInfo segmentInfo, Set<String> files)- throws IOException {++ public void files(Directory dir, SegmentInfo segmentInfo, String codecId,++ Set<String> files) throws IOException {+ Set<String> otherFiles = new HashSet<String>();- other.files(dir, segmentInfo, otherFiles);- for (String string : otherFiles) { // under some circumstances we only write DocValues- // so other files will be added even if they don't exist++ other.files(dir, segmentInfo, codecId, otherFiles);++ for (String string : otherFiles) { // under some circumstances we only write++ // DocValues++ // so other files will be added even if++ // they don't exist+ if (dir.fileExists(string))+ files.add(string);+ }-++ //TODO can we have a compound file per segment and codec for docvalues?+ for (String file : dir.listAll()) {- if (file.startsWith(segmentInfo.name)++ if (file.startsWith(segmentInfo.name+"_" + codecId)+ && (file.endsWith(Writer.DATA_EXTENSION) || file+ .endsWith(Writer.INDEX_EXTENSION))) {+ files.add(file);+ }+ }++ }++ @Override+ public void getExtensions(Set<String> extensions) {+ other.getExtensions(extensions);+ extensions.add(Writer.DATA_EXTENSION);+ extensions.add(Writer.INDEX_EXTENSION);+ }++ static class WrappingFielsdProducer extends DocValuesProducerBase {++ private final FieldsProducer other;+- WrappingFielsdProducer(SegmentInfo si, Directory dir, FieldInfos fieldInfo,- FieldsProducer other) throws IOException {- super(si, dir, fieldInfo);++ WrappingFielsdProducer(SegmentReadState state, FieldsProducer other)++ throws IOException {++ super(state.segmentInfo, state.dir, state.fieldInfos, state.codecId);+ this.other = other;+ }++ @Override+ public void close() throws IOException {+ try {+ other.close();+ } finally {+ super.close();+ }+ }++ @Override+ public void loadTermsIndex(int indexDivisor) throws IOException {+ other.loadTermsIndex(indexDivisor);+ }++ @Override+ public FieldsEnum iterator() throws IOException {+ return new WrappingFieldsEnum(other.iterator(), docValues.entrySet()+ .iterator());+ }++ @Override+ public Terms terms(String field) throws IOException {+ return other.terms(field);+ }+ }++ static abstract class NameValue<V> {+ String name;+ V value;++ NameValue<?> smaller(NameValue<?> other) throws IOException {+ if (other.name == null) {+ if (this.name == null) {+ return null;+ }+ return this;+ } else if (this.name == null) {+ return other;+ }+ final int res = this.name.compareTo(other.name);+ if (res < 0)+ return this;+ if (res == 0)+ other.name = this.name;+ return other;+ }++ abstract NameValue<V> next() throws IOException;+ }++ static class FieldsEnumNameValue extends NameValue<FieldsEnum> {+ @Override+ NameValue<FieldsEnum> next() throws IOException {+ name = value.next();+ return this;+ }++ }++ static class DocValueNameValue extends NameValue<DocValues> {+ Iterator<Entry<String, DocValues>> iter;++ @Override+ NameValue<DocValues> next() {+ if (iter.hasNext()) {+ Entry<String, DocValues> next = iter.next();+ value = next.getValue();+ name = next.getKey();+ } else {+ name = null;+ }+ return this;+ }++ }++ static class WrappingFieldsEnum extends FieldsEnum {+ private final DocValueNameValue docValues = new DocValueNameValue();+ private final NameValue<FieldsEnum> fieldsEnum = new FieldsEnumNameValue();+ private NameValue<?> coordinator;++ @Override+ public AttributeSource attributes() {+ return fieldsEnum.value.attributes();+ }++ public WrappingFieldsEnum(FieldsEnum wrapped,+ Iterator<Entry<String, DocValues>> docValues) {+ this.docValues.iter = docValues;+ this.fieldsEnum.value = wrapped;+ coordinator = null;++ }++ @Override+ public DocValues docValues() throws IOException {+ if (docValues.name == coordinator.name)+ return docValues.value;+ return null;+ }++ @Override+ public String next() throws IOException {+ if (coordinator == null) {+ coordinator = fieldsEnum.next().smaller(docValues.next());+ // old = coordinator.name;+ } else {+ String current = coordinator.name;+ if (current == docValues.name) {+ docValues.next();+ }+ if (current == fieldsEnum.name) {+ fieldsEnum.next();+ }+ coordinator = docValues.smaller(fieldsEnum);++ }+ return coordinator == null ? null : coordinator.name;++ }++ @Override+ public TermsEnum terms() throws IOException {+ if (fieldsEnum.name == coordinator.name)+ return fieldsEnum.value.terms();+ return null;+ }++ }++}diff --cc lucene/src/java/org/apache/lucene/index/codecs/docvalues/DocValuesConsumer.javaindex 0ca72d38261,00000000000..3d39701779dmode 100644,000000..100644--- a/lucene/src/java/org/apache/lucene/index/codecs/docvalues/DocValuesConsumer.java+++ b/lucene/src/java/org/apache/lucene/index/codecs/docvalues/DocValuesConsumer.java@@@ -1,97 -1,0 +1,97 @@@+package org.apache.lucene.index.codecs.docvalues;++/**+ * 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 java.util.Collection;+import java.util.Comparator;++import org.apache.lucene.index.FieldInfo;+import org.apache.lucene.index.IndexReader;+import org.apache.lucene.index.values.DocValues;+import org.apache.lucene.index.values.ValuesAttribute;+import org.apache.lucene.index.values.Writer;+import org.apache.lucene.store.Directory;+import org.apache.lucene.util.Bits;+import org.apache.lucene.util.BytesRef;++/**+ * @lucene.experimental+ */+// TODO this might need to go in the codec package since is a direct relative to+// TermsConsumer+public abstract class DocValuesConsumer {++ public abstract void add(int docID, ValuesAttribute attr) throws IOException;++ public abstract void finish(int docCount) throws IOException;++ public abstract void files(Collection<String> files) throws IOException;++ public void merge(org.apache.lucene.index.codecs.MergeState mergeState,+ DocValues values) throws IOException {+ // TODO we need some kind of compatibility notation for values such+ // that two slightly different segments can be merged eg. fixed vs.+ // variable byte len or float32 vs. float64+ int docBase = 0;+ boolean merged = false;+ /*+ * We ignore the given DocValues here and merge from the subReaders directly+ * to support bulk copies on the DocValues Writer level. if this gets merged+ * with MultiDocValues the writer can not optimize for bulk-copyable data+ */+ for (final IndexReader reader : mergeState.readers) {+ final DocValues r = reader.docValues(mergeState.fieldInfo.name);+ if (r != null) {+ merged = true;+ merge(new Writer.MergeState(r, docBase, reader.maxDoc(), reader+ .getDeletedDocs()));+ }+ docBase += reader.numDocs();+ }+ if (merged)+ finish(mergeState.mergedDocCount);+ }++ protected abstract void merge(MergeState mergeState) throws IOException;++ /*+ * specialized auxiliary MergeState is necessary since we don't want to+ * exploit internals up to the codec ones+ */+ public static class MergeState {+ public final DocValues reader;+ public final int docBase;+ public final int docCount;+ public final Bits bits;++ public MergeState(DocValues reader, int docBase, int docCount, Bits bits) {+ assert reader != null;+ this.reader = reader;+ this.docBase = docBase;+ this.docCount = docCount;+ this.bits = bits;+ }+ }++ public static DocValuesConsumer create(String segmentName,- Directory directory, FieldInfo field, Comparator<BytesRef> comp)++ Directory directory, FieldInfo field, String codecId, Comparator<BytesRef> comp)+ throws IOException {- final String id = segmentName + "_" + field.number;++ final String id = segmentName + "_" + codecId;+ return Writer.create(field.getDocValues(), id, directory, comp);+ }+}diff --cc lucene/src/java/org/apache/lucene/index/codecs/docvalues/DocValuesProducerBase.javaindex 8cdc41b5161,00000000000..426f9277894mode 100644,000000..100644--- a/lucene/src/java/org/apache/lucene/index/codecs/docvalues/DocValuesProducerBase.java+++ b/lucene/src/java/org/apache/lucene/index/codecs/docvalues/DocValuesProducerBase.java@@@ -1,99 -1,0 +1,99 @@@+package org.apache.lucene.index.codecs.docvalues;+/**+ * 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 java.util.Collection;+import java.util.TreeMap;++import org.apache.lucene.index.FieldInfo;+import org.apache.lucene.index.FieldInfos;+import org.apache.lucene.index.IndexFileNames;+import org.apache.lucene.index.SegmentInfo;+import org.apache.lucene.index.codecs.FieldsProducer;+import org.apache.lucene.index.values.Bytes;+import org.apache.lucene.index.values.DocValues;+import org.apache.lucene.index.values.Floats;+import org.apache.lucene.index.values.Ints;+import org.apache.lucene.index.values.Values;+import org.apache.lucene.index.values.Writer;+import org.apache.lucene.store.Directory;++public abstract class DocValuesProducerBase extends FieldsProducer{++ protected final TreeMap<String, DocValues> docValues = new TreeMap<String, DocValues>();+- protected DocValuesProducerBase(SegmentInfo si, Directory dir, FieldInfos fieldInfo) throws IOException {- load(fieldInfo, si.name, si.docCount, dir);++ protected DocValuesProducerBase(SegmentInfo si, Directory dir, FieldInfos fieldInfo, String codecId) throws IOException {++ load(fieldInfo, si.name, si.docCount, dir, codecId);+ }++ @Override+ public DocValues docValues(String field) throws IOException {+ return docValues.get(field);+ }++ // Only opens files... doesn't actually load any values+ protected void load(FieldInfos fieldInfos, String segment, int docCount,- Directory dir) throws IOException {++ Directory dir, String codecId) throws IOException {+ final int numFields = fieldInfos.size();+ for (int i = 0; i < numFields; i++) {+ final FieldInfo fieldInfo = fieldInfos.fieldInfo(i);+ final Values v = fieldInfo.getDocValues();+ final String field = fieldInfo.name;- final String id = IndexFileNames.segmentFileName(segment, Integer- .toString(fieldInfo.number),"");++ //TODO can we have a compound file per segment and codec for docvalues?++ final String id = IndexFileNames.segmentFileName(segment, codecId+"-"+fieldInfo.number, "");+ if (v != null && dir.fileExists(id + "." + Writer.DATA_EXTENSION)) {+ docValues.put(field, loadDocValues(docCount, dir, id, v));+ }+ }+ }++ protected DocValues loadDocValues(int docCount, Directory dir, String id,+ Values v) throws IOException {+ switch (v) {+ case PACKED_INTS:+ return Ints.getValues(dir, id, false);+ case PACKED_INTS_FIXED:+ return Ints.getValues(dir, id, true);+ case SIMPLE_FLOAT_4BYTE:+ return Floats.getValues(dir, id, docCount);+ case SIMPLE_FLOAT_8BYTE:+ return Floats.getValues(dir, id, docCount);+ case BYTES_FIXED_STRAIGHT:+ return Bytes.getValues(dir, id, Bytes.Mode.STRAIGHT, true, docCount);+ case BYTES_FIXED_DEREF:+ return Bytes.getValues(dir, id, Bytes.Mode.DEREF, true, docCount);+ case BYTES_FIXED_SORTED:+ return Bytes.getValues(dir, id, Bytes.Mode.SORTED, true, docCount);+ case BYTES_VAR_STRAIGHT:+ return Bytes.getValues(dir, id, Bytes.Mode.STRAIGHT, false, docCount);+ case BYTES_VAR_DEREF:+ return Bytes.getValues(dir, id, Bytes.Mode.DEREF, false, docCount);+ case BYTES_VAR_SORTED:+ return Bytes.getValues(dir, id, Bytes.Mode.SORTED, false, docCount);+ default:+ throw new IllegalStateException("unrecognized index values mode " + v);+ }+ }++ @Override+ public void close() throws IOException {+ Collection<DocValues> values = docValues.values();+ for (DocValues docValues : values) {+ docValues.close();+ }+ }+}