24 namespace Lucene.Net.Index
34 public const int FORMAT = - 3;
38 public const int FORMAT_VERSION_UTF8_LENGTH_IN_BYTES = - 4;
41 public static readonly
int FORMAT_CURRENT = FORMAT_VERSION_UTF8_LENGTH_IN_BYTES;
43 private bool isDisposed;
63 internal int indexInterval = 128;
71 internal int skipInterval = 16;
76 internal int maxSkipLevels = 10;
78 private long lastIndexPointer;
80 private byte[] lastTermBytes =
new byte[10];
81 private int lastTermBytesLength = 0;
82 private int lastFieldNumber = - 1;
89 Initialize(directory, segment, fis, interval,
false);
96 Initialize(directory, segment, fis, interval, isIndex);
99 private void Initialize(
Directory directory, System.String segment, FieldInfos fis,
int interval,
bool isi)
101 indexInterval = interval;
104 output = directory.CreateOutput(segment + (isIndex?
".tii":
".tis"));
105 output.WriteInt(FORMAT_CURRENT);
107 output.WriteInt(indexInterval);
108 output.WriteInt(skipInterval);
109 output.WriteInt(maxSkipLevels);
110 System.Diagnostics.Debug.Assert(InitUTF16Results());
115 UnicodeUtil.UTF16toUTF8(term.Text, 0, term.Text.Length, utf8Result);
116 Add(fieldInfos.FieldNumber(term.Field), utf8Result.result, utf8Result.length, ti);
124 private bool InitUTF16Results()
132 private int CompareToLastTerm(
int fieldNumber, byte[] termBytes,
int termBytesLength)
135 if (lastFieldNumber != fieldNumber)
137 int cmp = String.CompareOrdinal(fieldInfos.FieldName(lastFieldNumber), fieldInfos.FieldName(fieldNumber));
142 if (cmp != 0 || lastFieldNumber != - 1)
146 UnicodeUtil.UTF8toUTF16(lastTermBytes, 0, lastTermBytesLength, utf16Result1);
147 UnicodeUtil.UTF8toUTF16(termBytes, 0, termBytesLength, utf16Result2);
149 if (utf16Result1.length < utf16Result2.length)
150 len = utf16Result1.length;
152 len = utf16Result2.length;
154 for (
int i = 0; i < len; i++)
156 char ch1 = utf16Result1.result[i];
157 char ch2 = utf16Result2.result[i];
161 return utf16Result1.length - utf16Result2.length;
168 internal void Add(
int fieldNumber, byte[] termBytes,
int termBytesLength,
TermInfo ti)
171 System.Diagnostics.Debug.Assert(CompareToLastTerm(fieldNumber, termBytes, termBytesLength) < 0 ||
172 (isIndex && termBytesLength == 0 && lastTermBytesLength == 0),
173 "Terms are out of order: field=" + fieldInfos.FieldName(fieldNumber) +
" (number " + fieldNumber +
")" +
174 " lastField=" + fieldInfos.FieldName(lastFieldNumber) +
" (number " + lastFieldNumber +
")" +
175 " text=" + System.Text.Encoding.UTF8.GetString(termBytes, 0, termBytesLength) +
" lastText=" + System.Text.Encoding.UTF8.GetString(lastTermBytes, 0, lastTermBytesLength));
177 System.Diagnostics.Debug.Assert(ti.freqPointer >= lastTi.freqPointer,
"freqPointer out of order (" + ti.freqPointer +
" < " + lastTi.freqPointer +
")");
178 System.Diagnostics.Debug.Assert(ti.proxPointer >= lastTi.proxPointer,
"proxPointer out of order (" + ti.proxPointer +
" < " + lastTi.proxPointer +
")");
180 if (!isIndex && size % indexInterval == 0)
181 other.Add(lastFieldNumber, lastTermBytes, lastTermBytesLength, lastTi);
183 WriteTerm(fieldNumber, termBytes, termBytesLength);
185 output.WriteVInt(ti.docFreq);
186 output.WriteVLong(ti.freqPointer - lastTi.freqPointer);
187 output.WriteVLong(ti.proxPointer - lastTi.proxPointer);
189 if (ti.docFreq >= skipInterval)
191 output.WriteVInt(ti.skipOffset);
196 output.WriteVLong(other.output.FilePointer - lastIndexPointer);
197 lastIndexPointer = other.output.FilePointer;
200 lastFieldNumber = fieldNumber;
205 private void WriteTerm(
int fieldNumber, byte[] termBytes,
int termBytesLength)
211 int limit = termBytesLength < lastTermBytesLength?termBytesLength:lastTermBytesLength;
212 while (start < limit)
214 if (termBytes[start] != lastTermBytes[start])
219 int length = termBytesLength - start;
220 output.WriteVInt(start);
221 output.WriteVInt(length);
222 output.WriteBytes(termBytes, start, length);
223 output.WriteVInt(fieldNumber);
224 if (lastTermBytes.Length < termBytesLength)
226 byte[] newArray =
new byte[(int) (termBytesLength * 1.5)];
227 Array.Copy(lastTermBytes, 0, newArray, 0, start);
228 lastTermBytes = newArray;
230 Array.Copy(termBytes, start, lastTermBytes, start, length);
231 lastTermBytesLength = termBytesLength;
235 public void Dispose()
238 if (isDisposed)
return;
241 output.WriteLong(size);