Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Index / TermBuffer.cs
blobd70e7e763345aeb8beae5f8f71295a526e24555f
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 InputStream = Lucene.Net.Store.InputStream;
18 namespace Lucene.Net.Index
21 public class TermBuffer:System.ICloneable {
22 private static char[] NO_CHARS = new char[0];
24 private String field;
25 private char[] text = NO_CHARS;
26 private int textLength;
27 private Term term; // cached
29 public int CompareTo(TermBuffer other)
31 if (field == other.field) // fields are interned
32 return CompareChars(text, textLength, other.text, other.textLength);
33 else
34 return field.CompareTo(other.field);
37 private static int CompareChars(char[] v1, int len1, char[] v2, int len2)
39 int end = Math.Min(len1, len2);
40 for (int k = 0; k < end; k++) {
41 char c1 = v1[k];
42 char c2 = v2[k];
43 if (c1 != c2) {
44 return c1 - c2;
47 return len1 - len2;
50 private void SetTextLength(int newLength)
52 if (text.Length < newLength) {
53 char[] newText = new char[newLength];
54 System.Array.Copy(text, 0, newText, 0, textLength);
55 text = newText;
57 textLength = newLength;
60 public void Read(InputStream input, FieldInfos fieldInfos)
62 this.term = null; // invalidate cache
63 int start = input.ReadVInt();
64 int length = input.ReadVInt();
65 int totalLength = start + length;
66 SetTextLength(totalLength);
67 input.ReadChars(this.text, start, length);
68 this.field = fieldInfos.FieldName(input.ReadVInt());
71 public void Set(Term term)
73 if (term == null) {
74 Reset();
75 return;
78 // copy text into the buffer
79 SetTextLength(term.Text().Length);
80 text = term.Text().ToCharArray(0, term.Text().Length);
82 this.field = term.Field();
83 this.term = term;
86 public void Set(TermBuffer other)
88 SetTextLength(other.textLength);
89 System.Array.Copy(other.text, 0, text, 0, textLength);
91 this.field = other.field;
92 this.term = other.term;
95 public void Reset()
97 this.field = null;
98 this.textLength = 0;
99 this.term = null;
102 public Term ToTerm()
104 if (field == null) // unset
105 return null;
107 if (term == null)
108 term = new Term(field, new String(text, 0, textLength), false);
110 return term;
113 public System.Object Clone()
115 TermBuffer clone = null;
118 clone = (TermBuffer) base.MemberwiseClone();
120 catch (System.Exception)
124 clone.text = new char[text.Length];
125 System.Array.Copy(text, 0, clone.text, 0, textLength);
127 return clone;