pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / index / TestByteSlices.java
1 package org.apache.lucene.index;
2
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.
15  */
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.apache.lucene.util.LuceneTestCase;
21
22 public class TestByteSlices extends LuceneTestCase {
23
24   private static class ByteBlockAllocator extends ByteBlockPool.Allocator {
25     ArrayList<byte[]> freeByteBlocks = new ArrayList<byte[]>();
26     
27     /* Allocate another byte[] from the shared pool */
28     @Override
29     synchronized byte[] getByteBlock() {
30       final int size = freeByteBlocks.size();
31       final byte[] b;
32       if (0 == size)
33         b = new byte[DocumentsWriter.BYTE_BLOCK_SIZE];
34       else
35         b =  freeByteBlocks.remove(size-1);
36       return b;
37     }
38
39     /* Return a byte[] to the pool */
40     @Override
41     synchronized void recycleByteBlocks(byte[][] blocks, int start, int end) {
42       for(int i=start;i<end;i++)
43         freeByteBlocks.add(blocks[i]);
44     }
45
46     @Override
47     synchronized void recycleByteBlocks(List<byte[]> blocks) {
48       final int size = blocks.size();
49       for(int i=0;i<size;i++)
50         freeByteBlocks.add(blocks.get(i));
51     }
52   }
53
54   public void testBasic() throws Throwable {
55     ByteBlockPool pool = new ByteBlockPool(new ByteBlockAllocator());
56
57     final int NUM_STREAM = atLeast(100);
58
59     ByteSliceWriter writer = new ByteSliceWriter(pool);
60
61     int[] starts = new int[NUM_STREAM];
62     int[] uptos = new int[NUM_STREAM];
63     int[] counters = new int[NUM_STREAM];
64
65     ByteSliceReader reader = new ByteSliceReader();
66
67     for(int ti=0;ti<100;ti++) {
68
69       for(int stream=0;stream<NUM_STREAM;stream++) {
70         starts[stream] = -1;
71         counters[stream] = 0;
72       }
73       
74       int num = atLeast(10000);
75       for (int iter = 0; iter < num; iter++) {
76         int stream = random.nextInt(NUM_STREAM);
77         if (VERBOSE)
78           System.out.println("write stream=" + stream);
79
80         if (starts[stream] == -1) {
81           final int spot = pool.newSlice(ByteBlockPool.FIRST_LEVEL_SIZE);
82           starts[stream] = uptos[stream] = spot + pool.byteOffset;
83           if (VERBOSE)
84             System.out.println("  init to " + starts[stream]);
85         }
86
87         writer.init(uptos[stream]);
88         int numValue = random.nextInt(20);
89         for(int j=0;j<numValue;j++) {
90           if (VERBOSE)
91             System.out.println("    write " + (counters[stream]+j));
92           // write some large (incl. negative) ints:
93           writer.writeVInt(random.nextInt());
94           writer.writeVInt(counters[stream]+j);
95         }
96         counters[stream] += numValue;
97         uptos[stream] = writer.getAddress();
98         if (VERBOSE)
99           System.out.println("    addr now " + uptos[stream]);
100       }
101     
102       for(int stream=0;stream<NUM_STREAM;stream++) {
103         if (VERBOSE)
104           System.out.println("  stream=" + stream + " count=" + counters[stream]);
105
106         if (starts[stream] != -1 && starts[stream] != uptos[stream]) {
107           reader.init(pool, starts[stream], uptos[stream]);
108           for(int j=0;j<counters[stream];j++) {
109             reader.readVInt();
110             assertEquals(j, reader.readVInt()); 
111           }
112         }
113       }
114
115       pool.reset();
116     }
117   }
118 }