1 package org.apache.lucene.index; 2 3 18 19 20 import java.io.IOException ; 21 import org.apache.lucene.store.IndexOutput; 22 import org.apache.lucene.store.Directory; 23 import org.apache.lucene.util.StringHelper; 24 25 27 28 final class TermInfosWriter { 29 30 public static final int FORMAT = -2; 31 32 private FieldInfos fieldInfos; 33 private IndexOutput output; 34 private Term lastTerm = new Term("", ""); 35 private TermInfo lastTi = new TermInfo(); 36 private long size = 0; 37 38 45 50 int indexInterval = 128; 51 52 57 int skipInterval = 16; 58 59 private long lastIndexPointer = 0; 60 private boolean isIndex = false; 61 62 private TermInfosWriter other = null; 63 64 TermInfosWriter(Directory directory, String segment, FieldInfos fis, 65 int interval) 66 throws IOException { 67 initialize(directory, segment, fis, interval, false); 68 other = new TermInfosWriter(directory, segment, fis, interval, true); 69 other.other = this; 70 } 71 72 private TermInfosWriter(Directory directory, String segment, FieldInfos fis, 73 int interval, boolean isIndex) throws IOException { 74 initialize(directory, segment, fis, interval, isIndex); 75 } 76 77 private void initialize(Directory directory, String segment, FieldInfos fis, 78 int interval, boolean isi) throws IOException { 79 indexInterval = interval; 80 fieldInfos = fis; 81 isIndex = isi; 82 output = directory.createOutput(segment + (isIndex ? ".tii" : ".tis")); 83 output.writeInt(FORMAT); output.writeLong(0); output.writeInt(indexInterval); output.writeInt(skipInterval); } 88 89 92 final void add(Term term, TermInfo ti) 93 throws IOException { 94 if (!isIndex && term.compareTo(lastTerm) <= 0) 95 throw new IOException ("term out of order"); 96 if (ti.freqPointer < lastTi.freqPointer) 97 throw new IOException ("freqPointer out of order"); 98 if (ti.proxPointer < lastTi.proxPointer) 99 throw new IOException ("proxPointer out of order"); 100 101 if (!isIndex && size % indexInterval == 0) 102 other.add(lastTerm, lastTi); 104 writeTerm(term); output.writeVInt(ti.docFreq); output.writeVLong(ti.freqPointer - lastTi.freqPointer); output.writeVLong(ti.proxPointer - lastTi.proxPointer); 108 109 if (ti.docFreq >= skipInterval) { 110 output.writeVInt(ti.skipOffset); 111 } 112 113 if (isIndex) { 114 output.writeVLong(other.output.getFilePointer() - lastIndexPointer); 115 lastIndexPointer = other.output.getFilePointer(); } 117 118 lastTi.set(ti); 119 size++; 120 } 121 122 private final void writeTerm(Term term) 123 throws IOException { 124 int start = StringHelper.stringDifference(lastTerm.text, term.text); 125 int length = term.text.length() - start; 126 127 output.writeVInt(start); output.writeVInt(length); output.writeChars(term.text, start, length); 131 output.writeVInt(fieldInfos.fieldNumber(term.field)); 133 lastTerm = term; 134 } 135 136 137 138 139 final void close() throws IOException { 140 output.seek(4); output.writeLong(size); 142 output.close(); 143 144 if (!isIndex) 145 other.close(); 146 } 147 148 } 149 | Popular Tags |