1 package org.apache.lucene.index;
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 import java.util.ArrayList;
18 import java.util.List;
20 import org.apache.lucene.util.LuceneTestCase;
22 public class TestByteSlices extends LuceneTestCase {
24 private static class ByteBlockAllocator extends ByteBlockPool.Allocator {
25 ArrayList<byte[]> freeByteBlocks = new ArrayList<byte[]>();
27 /* Allocate another byte[] from the shared pool */
29 synchronized byte[] getByteBlock() {
30 final int size = freeByteBlocks.size();
33 b = new byte[DocumentsWriter.BYTE_BLOCK_SIZE];
35 b = freeByteBlocks.remove(size-1);
39 /* Return a byte[] to the pool */
41 synchronized void recycleByteBlocks(byte[][] blocks, int start, int end) {
42 for(int i=start;i<end;i++)
43 freeByteBlocks.add(blocks[i]);
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));
54 public void testBasic() throws Throwable {
55 ByteBlockPool pool = new ByteBlockPool(new ByteBlockAllocator());
57 final int NUM_STREAM = atLeast(100);
59 ByteSliceWriter writer = new ByteSliceWriter(pool);
61 int[] starts = new int[NUM_STREAM];
62 int[] uptos = new int[NUM_STREAM];
63 int[] counters = new int[NUM_STREAM];
65 ByteSliceReader reader = new ByteSliceReader();
67 for(int ti=0;ti<100;ti++) {
69 for(int stream=0;stream<NUM_STREAM;stream++) {
74 int num = atLeast(10000);
75 for (int iter = 0; iter < num; iter++) {
76 int stream = random.nextInt(NUM_STREAM);
78 System.out.println("write stream=" + stream);
80 if (starts[stream] == -1) {
81 final int spot = pool.newSlice(ByteBlockPool.FIRST_LEVEL_SIZE);
82 starts[stream] = uptos[stream] = spot + pool.byteOffset;
84 System.out.println(" init to " + starts[stream]);
87 writer.init(uptos[stream]);
88 int numValue = random.nextInt(20);
89 for(int j=0;j<numValue;j++) {
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);
96 counters[stream] += numValue;
97 uptos[stream] = writer.getAddress();
99 System.out.println(" addr now " + uptos[stream]);
102 for(int stream=0;stream<NUM_STREAM;stream++) {
104 System.out.println(" stream=" + stream + " count=" + counters[stream]);
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++) {
110 assertEquals(j, reader.readVInt());