Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Index / TermInfosWriter.cs
blob5c58dbfb5e364f1406dfe2a43eb17d4f17f17f12
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using Directory = Lucene.Net.Store.Directory;
18 using OutputStream = Lucene.Net.Store.OutputStream;
19 using StringHelper = Lucene.Net.Util.StringHelper;
20 namespace Lucene.Net.Index
23 /// <summary>This stores a monotonically increasing set of <Term, TermInfo> pairs in a
24 /// Directory. A TermInfos can be written once, in order.
25 /// </summary>
27 sealed public class TermInfosWriter
29 /// <summary>The file format version, a negative number. </summary>
30 public const int FORMAT = - 2;
32 private FieldInfos fieldInfos;
33 private OutputStream output;
34 private Term lastTerm = new Term("", "");
35 private TermInfo lastTi = new TermInfo();
36 private long size = 0;
38 // TODO: the default values for these two parameters should be settable from
39 // IndexWriter. However, once that's done, folks will start setting them to
40 // ridiculous values and complaining that things don't work well, as with
41 // mergeFactor. So, let's wait until a number of folks find that alternate
42 // values work better. Note that both of these values are stored in the
43 // segment, so that it's safe to change these w/o rebuilding all indexes.
45 /// <summary>Expert: The fraction of terms in the "dictionary" which should be stored
46 /// in RAM. Smaller values use more memory, but make searching slightly
47 /// faster, while larger values use less memory and make searching slightly
48 /// slower. Searching is typically not dominated by dictionary lookup, so
49 /// tweaking this is rarely useful.
50 /// </summary>
51 internal int indexInterval = 128;
53 /// <summary>Expert: The fraction of {@link TermDocs} entries stored in skip tables,
54 /// used to accellerate {@link TermDocs#SkipTo(int)}. Larger values result in
55 /// smaller indexes, greater acceleration, but fewer accelerable cases, while
56 /// smaller values result in bigger indexes, less acceleration and more
57 /// accelerable cases. More detailed experiments would be useful here.
58 /// </summary>
59 internal int skipInterval = 16;
61 private long lastIndexPointer = 0;
62 private bool isIndex = false;
64 private TermInfosWriter other = null;
66 public /*internal*/ TermInfosWriter(Directory directory, System.String segment, FieldInfos fis)
68 Initialize(directory, segment, fis, false);
69 other = new TermInfosWriter(directory, segment, fis, true);
70 other.other = this;
73 private TermInfosWriter(Directory directory, System.String segment, FieldInfos fis, bool isIndex)
75 Initialize(directory, segment, fis, isIndex);
78 private void Initialize(Directory directory, System.String segment, FieldInfos fis, bool isi)
80 fieldInfos = fis;
81 isIndex = isi;
82 output = directory.CreateFile(segment + (isIndex?".tii":".tis"));
83 output.WriteInt(FORMAT); // write format
84 output.WriteLong(0); // leave space for size
85 output.WriteInt(indexInterval); // write indexInterval
86 output.WriteInt(skipInterval); // write skipInterval
89 /// <summary>Adds a new <Term, TermInfo> pair to the set.
90 /// Term must be lexicographically greater than all previous Terms added.
91 /// TermInfo pointers must be positive and greater than all previous.
92 /// </summary>
93 public /*internal*/ void Add(Term term, TermInfo ti)
95 if (!isIndex && term.CompareTo(lastTerm) <= 0)
96 throw new System.IO.IOException("term out of order");
97 if (ti.freqPointer < lastTi.freqPointer)
98 throw new System.IO.IOException("freqPointer out of order");
99 if (ti.proxPointer < lastTi.proxPointer)
100 throw new System.IO.IOException("proxPointer out of order");
102 if (!isIndex && size % indexInterval == 0)
103 other.Add(lastTerm, lastTi); // add an index term
105 WriteTerm(term); // write term
106 output.WriteVInt(ti.docFreq); // write doc freq
107 output.WriteVLong(ti.freqPointer - lastTi.freqPointer); // write pointers
108 output.WriteVLong(ti.proxPointer - lastTi.proxPointer);
110 if (ti.docFreq >= skipInterval)
112 output.WriteVInt(ti.skipOffset);
115 if (isIndex)
117 output.WriteVLong(other.output.GetFilePointer() - lastIndexPointer);
118 lastIndexPointer = other.output.GetFilePointer(); // write pointer
121 lastTi.Set(ti);
122 size++;
125 private void WriteTerm(Term term)
127 int start = StringHelper.StringDifference(lastTerm.text, term.text);
128 int length = term.text.Length - start;
130 output.WriteVInt(start); // write shared prefix length
131 output.WriteVInt(length); // write delta length
132 output.WriteChars(term.text, start, length); // write delta chars
134 output.WriteVInt(fieldInfos.FieldNumber(term.field)); // write Field num
136 lastTerm = term;
141 /// <summary>Called to complete TermInfos creation. </summary>
142 public /*internal*/ void Close()
144 output.Seek(4); // write size after format
145 output.WriteLong(size);
146 output.Close();
148 if (!isIndex)
149 other.Close();