Save This Page
Home » lucene-2.4.1-src » org.apache » lucene » index » [javadoc | source]
    1   package org.apache.lucene.index;
    2   
    3   /**
    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
   10    *
   11    *     http://www.apache.org/licenses/LICENSE-2.0
   12    *
   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.
   18    */
   19   
   20   import java.io.IOException;
   21   import org.apache.lucene.store.IndexInput;
   22   import org.apache.lucene.util.UnicodeUtil;
   23   
   24   final class TermBuffer implements Cloneable {
   25   
   26     private String field;
   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)
   30   
   31     private UnicodeUtil.UTF16Result text = new UnicodeUtil.UTF16Result();
   32     private UnicodeUtil.UTF8Result bytes = new UnicodeUtil.UTF8Result();
   33   
   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);
   37       else
   38         return field.compareTo(other.field);
   39     }
   40   
   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++) {
   45         char c1 = chars1[k];
   46         char c2 = chars2[k];
   47         if (c1 != c2) {
   48           return c1 - c2;
   49         }
   50       }
   51       return len1 - len2;
   52     }
   53   
   54     /** Call this if the IndexInput passed to {@link #read}
   55      *  stores terms in the "modified UTF8" (pre LUCENE-510)
   56      *  format. */
   57     void setPreUTF8Strings() {
   58       preUTF8Strings = true;
   59     }
   60   
   61     public final void read(IndexInput input, FieldInfos fieldInfos)
   62       throws IOException {
   63       this.term = null;                           // invalidate cache
   64       int start = input.readVInt();
   65       int length = input.readVInt();
   66       int totalLength = start + length;
   67       if (preUTF8Strings) {
   68         text.setLength(totalLength);
   69         input.readChars(text.result, start, length);
   70       } else {
   71   
   72         if (dirty) {
   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);
   78           dirty = false;
   79         } else {
   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);
   84         }
   85       }
   86       this.field = fieldInfos.fieldName(input.readVInt());
   87     }
   88   
   89     public final void set(Term term) {
   90       if (term == null) {
   91         reset();
   92         return;
   93       }
   94       final String termText = term.text();
   95       final int termLen = termText.length();
   96       text.setLength(termLen);
   97       termText.getChars(0, termLen, text.result, 0);
   98       dirty = true;
   99       field = term.field();
  100       this.term = term;
  101     }
  102   
  103     public final void set(TermBuffer other) {
  104       text.copyText(other.text);
  105       dirty = true;
  106       field = other.field;
  107       term = other.term;
  108     }
  109   
  110     public void reset() {
  111       field = null;
  112       text.setLength(0);
  113       term = null;
  114       dirty = true;
  115     }
  116   
  117     public Term toTerm() {
  118       if (field == null)                            // unset
  119         return null;
  120   
  121       if (term == null)
  122         term = new Term(field, new String(text.result, 0, text.length), false);
  123   
  124       return term;
  125     }
  126   
  127     protected Object clone() {
  128       TermBuffer clone = null;
  129       try {
  130         clone = (TermBuffer)super.clone();
  131       } catch (CloneNotSupportedException e) {}
  132   
  133       clone.dirty = true;
  134       clone.bytes = new UnicodeUtil.UTF8Result();
  135       clone.text = new UnicodeUtil.UTF16Result();
  136       clone.text.copyText(text);
  137       return clone;
  138     }
  139   }

Save This Page
Home » lucene-2.4.1-src » org.apache » lucene » index » [javadoc | source]