X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/index/TermVectorsWriter.java diff --git a/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/index/TermVectorsWriter.java b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/index/TermVectorsWriter.java new file mode 100644 index 0000000..4f41e9d --- /dev/null +++ b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/index/TermVectorsWriter.java @@ -0,0 +1,206 @@ +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.store.Directory; +import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util.StringHelper; +import org.apache.lucene.util.UnicodeUtil; + +import java.io.IOException; + +final class TermVectorsWriter { + + private IndexOutput tvx = null, tvd = null, tvf = null; + private FieldInfos fieldInfos; + final UnicodeUtil.UTF8Result[] utf8Results = new UnicodeUtil.UTF8Result[] {new UnicodeUtil.UTF8Result(), + new UnicodeUtil.UTF8Result()}; + + public TermVectorsWriter(Directory directory, String segment, + FieldInfos fieldInfos) throws IOException { + boolean success = false; + try { + // Open files for TermVector storage + tvx = directory.createOutput(IndexFileNames.segmentFileName(segment, IndexFileNames.VECTORS_INDEX_EXTENSION)); + tvx.writeInt(TermVectorsReader.FORMAT_CURRENT); + tvd = directory.createOutput(IndexFileNames.segmentFileName(segment, IndexFileNames.VECTORS_DOCUMENTS_EXTENSION)); + tvd.writeInt(TermVectorsReader.FORMAT_CURRENT); + tvf = directory.createOutput(IndexFileNames.segmentFileName(segment, IndexFileNames.VECTORS_FIELDS_EXTENSION)); + tvf.writeInt(TermVectorsReader.FORMAT_CURRENT); + success = true; + } finally { + if (!success) { + IOUtils.closeWhileHandlingException(tvx, tvd, tvf); + } + } + + this.fieldInfos = fieldInfos; + } + + /** + * Add a complete document specified by all its term vectors. If document has no + * term vectors, add value for tvx. + * + * @param vectors + * @throws IOException + */ + public final void addAllDocVectors(TermFreqVector[] vectors) throws IOException { + + tvx.writeLong(tvd.getFilePointer()); + tvx.writeLong(tvf.getFilePointer()); + + if (vectors != null) { + final int numFields = vectors.length; + tvd.writeVInt(numFields); + + long[] fieldPointers = new long[numFields]; + + for (int i=0; i 0 && tpVector.getTermPositions(0) != null; + storeOffsets = tpVector.size() > 0 && tpVector.getOffsets(0) != null; + bits = (byte) ((storePositions ? TermVectorsReader.STORE_POSITIONS_WITH_TERMVECTOR : 0) + + (storeOffsets ? TermVectorsReader.STORE_OFFSET_WITH_TERMVECTOR : 0)); + } else { + tpVector = null; + bits = 0; + storePositions = false; + storeOffsets = false; + } + + tvf.writeVInt(bits); + + final String[] terms = vectors[i].getTerms(); + final int[] freqs = vectors[i].getTermFrequencies(); + + int utf8Upto = 0; + utf8Results[1].length = 0; + + for (int j=0; j 1) { + long lastFieldPointer = fieldPointers[0]; + for (int i=1; i