Search in sources :

Example 21 with Version

use of org.apache.lucene.util.Version in project lucene-solr by apache.

the class Lucene62RWSegmentInfoFormat method write.

@Override
public void write(Directory dir, SegmentInfo si, IOContext ioContext) throws IOException {
    final String fileName = IndexFileNames.segmentFileName(si.name, "", Lucene62SegmentInfoFormat.SI_EXTENSION);
    try (IndexOutput output = dir.createOutput(fileName, ioContext)) {
        // Only add the file once we've successfully created it, else IFD assert can trip:
        si.addFile(fileName);
        CodecUtil.writeIndexHeader(output, Lucene62SegmentInfoFormat.CODEC_NAME, Lucene62SegmentInfoFormat.VERSION_CURRENT, si.getId(), "");
        Version version = si.getVersion();
        if (version.major < 5) {
            throw new IllegalArgumentException("invalid major version: should be >= 5 but got: " + version.major + " segment=" + si);
        }
        // Write the Lucene version that created this segment, since 3.1
        output.writeInt(version.major);
        output.writeInt(version.minor);
        output.writeInt(version.bugfix);
        assert version.prerelease == 0;
        output.writeInt(si.maxDoc());
        output.writeByte((byte) (si.getUseCompoundFile() ? SegmentInfo.YES : SegmentInfo.NO));
        output.writeMapOfStrings(si.getDiagnostics());
        Set<String> files = si.files();
        for (String file : files) {
            if (!IndexFileNames.parseSegmentName(file).equals(si.name)) {
                throw new IllegalArgumentException("invalid files: expected segment=" + si.name + ", got=" + files);
            }
        }
        output.writeSetOfStrings(files);
        output.writeMapOfStrings(si.getAttributes());
        Sort indexSort = si.getIndexSort();
        int numSortFields = indexSort == null ? 0 : indexSort.getSort().length;
        output.writeVInt(numSortFields);
        for (int i = 0; i < numSortFields; ++i) {
            SortField sortField = indexSort.getSort()[i];
            SortField.Type sortType = sortField.getType();
            output.writeString(sortField.getField());
            int sortTypeID;
            switch(sortField.getType()) {
                case STRING:
                    sortTypeID = 0;
                    break;
                case LONG:
                    sortTypeID = 1;
                    break;
                case INT:
                    sortTypeID = 2;
                    break;
                case DOUBLE:
                    sortTypeID = 3;
                    break;
                case FLOAT:
                    sortTypeID = 4;
                    break;
                case CUSTOM:
                    if (sortField instanceof SortedSetSortField) {
                        sortTypeID = 5;
                        sortType = SortField.Type.STRING;
                    } else if (sortField instanceof SortedNumericSortField) {
                        sortTypeID = 6;
                        sortType = ((SortedNumericSortField) sortField).getNumericType();
                    } else {
                        throw new IllegalStateException("Unexpected SortedNumericSortField " + sortField);
                    }
                    break;
                default:
                    throw new IllegalStateException("Unexpected sort type: " + sortField.getType());
            }
            output.writeVInt(sortTypeID);
            if (sortTypeID == 5) {
                SortedSetSortField ssf = (SortedSetSortField) sortField;
                if (ssf.getSelector() == SortedSetSelector.Type.MIN) {
                    output.writeByte((byte) 0);
                } else if (ssf.getSelector() == SortedSetSelector.Type.MAX) {
                    output.writeByte((byte) 1);
                } else if (ssf.getSelector() == SortedSetSelector.Type.MIDDLE_MIN) {
                    output.writeByte((byte) 2);
                } else if (ssf.getSelector() == SortedSetSelector.Type.MIDDLE_MAX) {
                    output.writeByte((byte) 3);
                } else {
                    throw new IllegalStateException("Unexpected SortedSetSelector type: " + ssf.getSelector());
                }
            } else if (sortTypeID == 6) {
                SortedNumericSortField snsf = (SortedNumericSortField) sortField;
                if (snsf.getNumericType() == SortField.Type.LONG) {
                    output.writeByte((byte) 0);
                } else if (snsf.getNumericType() == SortField.Type.INT) {
                    output.writeByte((byte) 1);
                } else if (snsf.getNumericType() == SortField.Type.DOUBLE) {
                    output.writeByte((byte) 2);
                } else if (snsf.getNumericType() == SortField.Type.FLOAT) {
                    output.writeByte((byte) 3);
                } else {
                    throw new IllegalStateException("Unexpected SortedNumericSelector type: " + snsf.getNumericType());
                }
                if (snsf.getSelector() == SortedNumericSelector.Type.MIN) {
                    output.writeByte((byte) 0);
                } else if (snsf.getSelector() == SortedNumericSelector.Type.MAX) {
                    output.writeByte((byte) 1);
                } else {
                    throw new IllegalStateException("Unexpected sorted numeric selector type: " + snsf.getSelector());
                }
            }
            output.writeByte((byte) (sortField.getReverse() ? 0 : 1));
            // write missing value 
            Object missingValue = sortField.getMissingValue();
            if (missingValue == null) {
                output.writeByte((byte) 0);
            } else {
                switch(sortType) {
                    case STRING:
                        if (missingValue == SortField.STRING_LAST) {
                            output.writeByte((byte) 1);
                        } else if (missingValue == SortField.STRING_FIRST) {
                            output.writeByte((byte) 2);
                        } else {
                            throw new AssertionError("unrecognized missing value for STRING field \"" + sortField.getField() + "\": " + missingValue);
                        }
                        break;
                    case LONG:
                        output.writeByte((byte) 1);
                        output.writeLong(((Long) missingValue).longValue());
                        break;
                    case INT:
                        output.writeByte((byte) 1);
                        output.writeInt(((Integer) missingValue).intValue());
                        break;
                    case DOUBLE:
                        output.writeByte((byte) 1);
                        output.writeLong(Double.doubleToLongBits(((Double) missingValue).doubleValue()));
                        break;
                    case FLOAT:
                        output.writeByte((byte) 1);
                        output.writeInt(Float.floatToIntBits(((Float) missingValue).floatValue()));
                        break;
                    default:
                        throw new IllegalStateException("Unexpected sort type: " + sortField.getType());
                }
            }
        }
        CodecUtil.writeFooter(output);
    }
}
Also used : SortedSetSortField(org.apache.lucene.search.SortedSetSortField) IndexOutput(org.apache.lucene.store.IndexOutput) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortField(org.apache.lucene.search.SortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) Version(org.apache.lucene.util.Version) Sort(org.apache.lucene.search.Sort)

Example 22 with Version

use of org.apache.lucene.util.Version in project lucene-solr by apache.

the class SegmentInfos method write.

/** Write ourselves to the provided {@link IndexOutput} */
public void write(Directory directory, IndexOutput out) throws IOException {
    CodecUtil.writeIndexHeader(out, "segments", VERSION_CURRENT, StringHelper.randomId(), Long.toString(generation, Character.MAX_RADIX));
    out.writeVInt(Version.LATEST.major);
    out.writeVInt(Version.LATEST.minor);
    out.writeVInt(Version.LATEST.bugfix);
    //System.out.println(Thread.currentThread().getName() + ": now write " + out.getName() + " with version=" + version);
    out.writeVInt(indexCreatedVersionMajor);
    out.writeLong(version);
    // write counter
    out.writeInt(counter);
    out.writeInt(size());
    if (size() > 0) {
        Version minSegmentVersion = null;
        // any SegmentInfo; this makes it cleaner to throw IndexFormatTooOldExc at read time:
        for (SegmentCommitInfo siPerCommit : this) {
            Version segmentVersion = siPerCommit.info.getVersion();
            if (minSegmentVersion == null || segmentVersion.onOrAfter(minSegmentVersion) == false) {
                minSegmentVersion = segmentVersion;
            }
        }
        out.writeVInt(minSegmentVersion.major);
        out.writeVInt(minSegmentVersion.minor);
        out.writeVInt(minSegmentVersion.bugfix);
    }
    // write infos
    for (SegmentCommitInfo siPerCommit : this) {
        SegmentInfo si = siPerCommit.info;
        if (indexCreatedVersionMajor >= 7 && si.minVersion == null) {
            throw new IllegalStateException("Segments must record minVersion if they have been created on or after Lucene 7: " + si);
        }
        out.writeString(si.name);
        byte[] segmentID = si.getId();
        // TODO: remove this in lucene 6, we don't need to include 4.x segments in commits anymore
        if (segmentID == null) {
            out.writeByte((byte) 0);
        } else {
            if (segmentID.length != StringHelper.ID_LENGTH) {
                throw new IllegalStateException("cannot write segment: invalid id segment=" + si.name + "id=" + StringHelper.idToString(segmentID));
            }
            out.writeByte((byte) 1);
            out.writeBytes(segmentID, segmentID.length);
        }
        out.writeString(si.getCodec().getName());
        out.writeLong(siPerCommit.getDelGen());
        int delCount = siPerCommit.getDelCount();
        if (delCount < 0 || delCount > si.maxDoc()) {
            throw new IllegalStateException("cannot write segment: invalid maxDoc segment=" + si.name + " maxDoc=" + si.maxDoc() + " delCount=" + delCount);
        }
        out.writeInt(delCount);
        out.writeLong(siPerCommit.getFieldInfosGen());
        out.writeLong(siPerCommit.getDocValuesGen());
        out.writeSetOfStrings(siPerCommit.getFieldInfosFiles());
        final Map<Integer, Set<String>> dvUpdatesFiles = siPerCommit.getDocValuesUpdatesFiles();
        out.writeInt(dvUpdatesFiles.size());
        for (Entry<Integer, Set<String>> e : dvUpdatesFiles.entrySet()) {
            out.writeInt(e.getKey());
            out.writeSetOfStrings(e.getValue());
        }
    }
    out.writeMapOfStrings(userData);
    CodecUtil.writeFooter(out);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Version(org.apache.lucene.util.Version)

Aggregations

Version (org.apache.lucene.util.Version)22 Directory (org.apache.lucene.store.Directory)7 Sort (org.apache.lucene.search.Sort)6 SortField (org.apache.lucene.search.SortField)5 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)5 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)5 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 Codec (org.apache.lucene.codecs.Codec)4 Document (org.apache.lucene.document.Document)4 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)4 SegmentInfo (org.apache.lucene.index.SegmentInfo)4 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Collectors (java.util.stream.Collectors)3 IntStream (java.util.stream.IntStream)3 Store (org.apache.lucene.document.Field.Store)3 TextField (org.apache.lucene.document.TextField)3 DirectoryReader (org.apache.lucene.index.DirectoryReader)3