Add --enable-deletion option to buildindex. If used, buildindex will remove deleted...
[beagle.git] / beagled / Lucene.Net / Index / TermBuffer.cs
blob566a5710e1d917174ec2b8ee027610a7a62a8f87
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 IndexInput = Lucene.Net.Store.IndexInput;
18 namespace Lucene.Net.Index
21 sealed class TermBuffer : System.ICloneable
23 private static readonly char[] NO_CHARS = new char[0];
25 private System.String field;
26 private char[] text = NO_CHARS;
27 private int textLength;
28 private Term term; // cached
30 public int CompareTo(TermBuffer other)
32 if ((System.Object) field == (System.Object) other.field)
33 // fields are interned
34 return CompareChars(text, textLength, other.text, other.textLength);
35 else
36 return String.CompareOrdinal(field, other.field);
39 private static int CompareChars(char[] v1, int len1, char[] v2, int len2)
41 int end = System.Math.Min(len1, len2);
42 for (int k = 0; k < end; k++)
44 char c1 = v1[k];
45 char c2 = v2[k];
46 if (c1 != c2)
48 return c1 - c2;
51 return len1 - len2;
54 private void SetTextLength(int newLength)
56 if (text.Length < newLength)
58 char[] newText = new char[newLength];
59 Array.Copy(text, 0, newText, 0, textLength);
60 text = newText;
62 textLength = newLength;
65 public void Read(IndexInput input, FieldInfos fieldInfos)
67 this.term = null; // invalidate cache
68 int start = input.ReadVInt();
69 int length = input.ReadVInt();
70 int totalLength = start + length;
71 SetTextLength(totalLength);
72 input.ReadChars(this.text, start, length);
73 this.field = fieldInfos.FieldName(input.ReadVInt());
76 public void Set(Term term)
78 if (term == null)
80 Reset();
81 return ;
84 // copy text into the buffer
85 SetTextLength(term.Text().Length);
86 text = term.Text().ToCharArray();
88 this.field = term.Field();
89 this.term = term;
92 public void Set(TermBuffer other)
94 SetTextLength(other.textLength);
95 Array.Copy(other.text, 0, text, 0, textLength);
97 this.field = other.field;
98 this.term = other.term;
101 public void Reset()
103 this.field = null;
104 this.textLength = 0;
105 this.term = null;
108 public Term ToTerm()
110 if (field == null)
111 // unset
112 return null;
114 if (term == null)
115 term = new Term(field, new System.String(text, 0, textLength), false);
117 return term;
120 public System.Object Clone()
122 TermBuffer clone = null;
125 clone = (TermBuffer) base.MemberwiseClone();
127 catch (System.Exception e)
131 clone.text = new char[text.Length];
132 Array.Copy(text, 0, clone.text, 0, textLength);
134 return clone;