add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / index / TestMultiLevelSkipList.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.io.Reader;
22
23 import org.apache.lucene.analysis.Analyzer;
24 import org.apache.lucene.analysis.LowerCaseTokenizer;
25 import org.apache.lucene.analysis.TokenFilter;
26 import org.apache.lucene.analysis.TokenStream;
27 import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.Field.Index;
30 import org.apache.lucene.document.Field.Store;
31 import org.apache.lucene.store.IndexInput;
32 import org.apache.lucene.store.RAMDirectory;
33 import org.apache.lucene.util.LuceneTestCase;
34
35 /**
36  * This testcase tests whether multi-level skipping is being used
37  * to reduce I/O while skipping through posting lists.
38  * 
39  * Skipping in general is already covered by several other
40  * testcases.
41  * 
42  */
43 public class TestMultiLevelSkipList extends LuceneTestCase {
44   @Override
45   public void setUp() throws Exception {
46     super.setUp();
47     PayloadFilter.count = 0;
48   }
49
50   public void testSimpleSkip() throws IOException {
51     RAMDirectory dir = new RAMDirectory();
52     IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer()).setMergePolicy(newLogMergePolicy()));
53     Term term = new Term("test", "a");
54     for (int i = 0; i < 5000; i++) {
55       Document d1 = new Document();
56       d1.add(newField(term.field(), term.text(), Store.NO, Index.ANALYZED));
57       writer.addDocument(d1);
58     }
59     writer.commit();
60     writer.optimize();
61     writer.close();
62
63     IndexReader reader = SegmentReader.getOnlySegmentReader(dir);
64     SegmentTermPositions tp = (SegmentTermPositions) reader.termPositions();
65     tp.freqStream = new CountingStream(tp.freqStream);
66
67     for (int i = 0; i < 2; i++) {
68       counter = 0;
69       tp.seek(term);
70
71       checkSkipTo(tp, 14, 185); // no skips
72       checkSkipTo(tp, 17, 190); // one skip on level 0
73       checkSkipTo(tp, 287, 200); // one skip on level 1, two on level 0
74     
75       // this test would fail if we had only one skip level,
76       // because than more bytes would be read from the freqStream
77       checkSkipTo(tp, 4800, 250);// one skip on level 2
78     }
79   }
80
81   public void checkSkipTo(TermPositions tp, int target, int maxCounter) throws IOException {
82     tp.skipTo(target);
83     if (maxCounter < counter) {
84       fail("Too many bytes read: " + counter + " vs " + maxCounter);
85     }
86
87     assertEquals("Wrong document " + tp.doc() + " after skipTo target " + target, target, tp.doc());
88     assertEquals("Frequency is not 1: " + tp.freq(), 1,tp.freq());
89     tp.nextPosition();
90     byte[] b = new byte[1];
91     tp.getPayload(b, 0);
92     assertEquals("Wrong payload for the target " + target + ": " + b[0], (byte) target, b[0]);
93   }
94
95   private static class PayloadAnalyzer extends Analyzer {
96     @Override
97     public TokenStream tokenStream(String fieldName, Reader reader) {
98       return new PayloadFilter(new LowerCaseTokenizer(TEST_VERSION_CURRENT, reader));
99     }
100
101   }
102
103   private static class PayloadFilter extends TokenFilter {
104     static int count = 0;
105     
106     PayloadAttribute payloadAtt;
107     
108     protected PayloadFilter(TokenStream input) {
109       super(input);
110       payloadAtt = addAttribute(PayloadAttribute.class);
111     }
112
113     @Override
114     public boolean incrementToken() throws IOException {
115       boolean hasNext = input.incrementToken();
116       if (hasNext) {
117         payloadAtt.setPayload(new Payload(new byte[] { (byte) count++ }));
118       } 
119       return hasNext;
120     }
121
122   }
123
124   private int counter = 0;
125
126   // Simply extends IndexInput in a way that we are able to count the number
127   // of bytes read
128   class CountingStream extends IndexInput {
129     private IndexInput input;
130
131     CountingStream(IndexInput input) {
132       this.input = input;
133     }
134
135     @Override
136     public byte readByte() throws IOException {
137       TestMultiLevelSkipList.this.counter++;
138       return this.input.readByte();
139     }
140
141     @Override
142     public void readBytes(byte[] b, int offset, int len) throws IOException {
143       TestMultiLevelSkipList.this.counter += len;
144       this.input.readBytes(b, offset, len);
145     }
146
147     @Override
148     public void close() throws IOException {
149       this.input.close();
150     }
151
152     @Override
153     public long getFilePointer() {
154       return this.input.getFilePointer();
155     }
156
157     @Override
158     public void seek(long pos) throws IOException {
159       this.input.seek(pos);
160     }
161
162     @Override
163     public long length() {
164       return this.input.length();
165     }
166
167     @Override
168     public Object clone() {
169       return new CountingStream((IndexInput) this.input.clone());
170     }
171
172   }
173 }