pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / index / PrefixCodedTerms.java
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 java.util.Iterator;
22
23 import org.apache.lucene.store.IndexInput;
24 import org.apache.lucene.store.RAMFile;
25 import org.apache.lucene.store.RAMInputStream;
26 import org.apache.lucene.store.RAMOutputStream;
27 import org.apache.lucene.util.BytesRef;
28 import org.apache.lucene.util.StringHelper;
29
30 /**
31  * Prefix codes term instances (prefixes are shared)
32  * @lucene.experimental
33  */
34 class PrefixCodedTerms implements Iterable<Term> {
35   final RAMFile buffer;
36   
37   private PrefixCodedTerms(RAMFile buffer) {
38     this.buffer = buffer;
39   }
40   
41   /** @return size in bytes */
42   public long getSizeInBytes() {
43     return buffer.getSizeInBytes();
44   }
45   
46   /** @return iterator over the bytes */
47   public Iterator<Term> iterator() {
48     return new PrefixCodedTermsIterator();
49   }
50   
51   class PrefixCodedTermsIterator implements Iterator<Term> {
52     final IndexInput input;
53     String field = "";
54     BytesRef bytes = new BytesRef();
55     Term term = new Term(field, "");
56
57     PrefixCodedTermsIterator() {
58       try {
59         input = new RAMInputStream("PrefixCodedTermsIterator", buffer);
60       } catch (IOException e) {
61         throw new RuntimeException(e);
62       }
63     }
64
65     public boolean hasNext() {
66       return input.getFilePointer() < input.length();
67     }
68     
69     public Term next() {
70       assert hasNext();
71       try {
72         int code = input.readVInt();
73         if ((code & 1) != 0) {
74           // new field
75           field = StringHelper.intern(input.readString());
76         }
77         int prefix = code >>> 1;
78         int suffix = input.readVInt();
79         bytes.grow(prefix + suffix);
80         input.readBytes(bytes.bytes, prefix, suffix);
81         bytes.length = prefix + suffix;
82         term.set(field, bytes.utf8ToString());
83         return term;
84       } catch (IOException e) {
85         throw new RuntimeException(e);
86       }
87     }
88     
89     public void remove() {
90       throw new UnsupportedOperationException();
91     }
92   }
93   
94   /** Builds a PrefixCodedTerms: call add repeatedly, then finish. */
95   public static class Builder {
96     private RAMFile buffer = new RAMFile();
97     private RAMOutputStream output = new RAMOutputStream(buffer);
98     private Term lastTerm = new Term("");
99     private BytesRef lastBytes = new BytesRef();
100     private BytesRef scratch = new BytesRef();
101
102     /** add a term */
103     public void add(Term term) {
104       assert lastTerm.equals(new Term("")) || term.compareTo(lastTerm) > 0;
105
106       scratch.copy(term.text);
107       try {
108         int prefix = sharedPrefix(lastBytes, scratch);
109         int suffix = scratch.length - prefix;
110         if (term.field.equals(lastTerm.field)) {
111           output.writeVInt(prefix << 1);
112         } else {
113           output.writeVInt(prefix << 1 | 1);
114           output.writeString(term.field);
115         }
116         output.writeVInt(suffix);
117         output.writeBytes(scratch.bytes, scratch.offset + prefix, suffix);
118         lastBytes.copy(scratch);
119         lastTerm.text = term.text;
120         lastTerm.field = term.field;
121       } catch (IOException e) {
122         throw new RuntimeException(e);
123       }
124     }
125     
126     /** return finalized form */
127     public PrefixCodedTerms finish() {
128       try {
129         output.close();
130         return new PrefixCodedTerms(buffer);
131       } catch (IOException e) {
132         throw new RuntimeException(e);
133       }
134     }
135     
136     private int sharedPrefix(BytesRef term1, BytesRef term2) {
137       int pos1 = 0;
138       int pos1End = pos1 + Math.min(term1.length, term2.length);
139       int pos2 = 0;
140       while(pos1 < pos1End) {
141         if (term1.bytes[term1.offset + pos1] != term2.bytes[term2.offset + pos2]) {
142           return pos1;
143         }
144         pos1++;
145         pos2++;
146       }
147       return pos1;
148     }
149   }
150 }