1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
21 import org.apache.lucene.store.IndexInput;
22 import org.apache.lucene.util.UnicodeUtil;
24 final class TermBuffer implements Cloneable {
27 private Term term; // cached
28 private boolean preUTF8Strings; // true if strings are stored in modified UTF8 encoding (LUCENE-510)
29 private boolean dirty; // true if text was set externally (ie not read via UTF8 bytes)
31 private UnicodeUtil.UTF16Result text = new UnicodeUtil.UTF16Result();
32 private UnicodeUtil.UTF8Result bytes = new UnicodeUtil.UTF8Result();
34 public final int compareTo(TermBuffer other) {
35 if (field == other.field) // fields are interned
36 return compareChars(text.result, text.length, other.text.result, other.text.length);
38 return field.compareTo(other.field);
41 private static final int compareChars(char[] chars1, int len1,
42 char[] chars2, int len2) {
43 final int end = len1 < len2 ? len1:len2;
44 for (int k = 0; k < end; k++) {
54 /** Call this if the IndexInput passed to {@link #read}
55 * stores terms in the "modified UTF8" (pre LUCENE-510)
57 void setPreUTF8Strings() {
58 preUTF8Strings = true;
61 public final void read(IndexInput input, FieldInfos fieldInfos)
63 this.term = null; // invalidate cache
64 int start = input.readVInt();
65 int length = input.readVInt();
66 int totalLength = start + length;
68 text.setLength(totalLength);
69 input.readChars(text.result, start, length);
73 // Fully convert all bytes since bytes is dirty
74 UnicodeUtil.UTF16toUTF8(text.result, 0, text.length, bytes);
75 bytes.setLength(totalLength);
76 input.readBytes(bytes.result, start, length);
77 UnicodeUtil.UTF8toUTF16(bytes.result, 0, totalLength, text);
80 // Incrementally convert only the UTF8 bytes that are new:
81 bytes.setLength(totalLength);
82 input.readBytes(bytes.result, start, length);
83 UnicodeUtil.UTF8toUTF16(bytes.result, start, length, text);
86 this.field = fieldInfos.fieldName(input.readVInt());
89 public final void set(Term term) {
94 final String termText = term.text();
95 final int termLen = termText.length();
96 text.setLength(termLen);
97 termText.getChars(0, termLen, text.result, 0);
103 public final void set(TermBuffer other) {
104 text.copyText(other.text);
110 public void reset() {
117 public Term toTerm() {
118 if (field == null) // unset
122 term = new Term(field, new String(text.result, 0, text.length), false);
128 protected Object clone() {
129 TermBuffer clone = null;
131 clone = (TermBuffer)super.clone();
132 } catch (CloneNotSupportedException e) {}
135 clone.bytes = new UnicodeUtil.UTF8Result();
136 clone.text = new UnicodeUtil.UTF16Result();
137 clone.text.copyText(text);