pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / store / TestHugeRamFile.java
1 package org.apache.lucene.store;
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.HashMap;
22
23 import org.apache.lucene.util.LuceneTestCase;
24
25 /** Test huge RAMFile with more than Integer.MAX_VALUE bytes. */
26 public class TestHugeRamFile extends LuceneTestCase {
27   
28   private static final long MAX_VALUE = (long) 2 * (long) Integer.MAX_VALUE;
29
30   /** Fake a huge ram file by using the same byte buffer for all 
31    * buffers under maxint. */
32   private static class DenseRAMFile extends RAMFile {
33     private long capacity = 0;
34     private HashMap<Integer,byte[]> singleBuffers = new HashMap<Integer,byte[]>();
35     @Override
36     protected byte[] newBuffer(int size) {
37       capacity += size;
38       if (capacity <= MAX_VALUE) {
39         // below maxint we reuse buffers
40         byte buf[] = singleBuffers.get(Integer.valueOf(size));
41         if (buf==null) {
42           buf = new byte[size]; 
43           //System.out.println("allocate: "+size);
44           singleBuffers.put(Integer.valueOf(size),buf);
45         }
46         return buf;
47       }
48       //System.out.println("allocate: "+size); System.out.flush();
49       return new byte[size];
50     }
51   }
52   
53   /** Test huge RAMFile with more than Integer.MAX_VALUE bytes. (LUCENE-957) */
54   public void testHugeFile() throws IOException {
55     DenseRAMFile f = new DenseRAMFile();
56     // output part
57     RAMOutputStream out = new RAMOutputStream(f);
58     byte b1[] = new byte[RAMOutputStream.BUFFER_SIZE];
59     byte b2[] = new byte[RAMOutputStream.BUFFER_SIZE / 3];
60     for (int i = 0; i < b1.length; i++) {
61       b1[i] = (byte) (i & 0x0007F);
62     }
63     for (int i = 0; i < b2.length; i++) {
64       b2[i] = (byte) (i & 0x0003F);
65     }
66     long n = 0;
67     assertEquals("output length must match",n,out.length());
68     while (n <= MAX_VALUE - b1.length) {
69       out.writeBytes(b1,0,b1.length);
70       out.flush();
71       n += b1.length;
72       assertEquals("output length must match",n,out.length());
73     }
74     //System.out.println("after writing b1's, length = "+out.length()+" (MAX_VALUE="+MAX_VALUE+")");
75     int m = b2.length;
76     long L = 12;
77     for (int j=0; j<L; j++) {
78       for (int i = 0; i < b2.length; i++) {
79         b2[i]++;
80       }
81       out.writeBytes(b2,0,m);
82       out.flush();
83       n += m;
84       assertEquals("output length must match",n,out.length());
85     }
86     out.close();
87     // input part
88     RAMInputStream in = new RAMInputStream("testcase", f);
89     assertEquals("input length must match",n,in.length());
90     //System.out.println("input length = "+in.length()+" % 1024 = "+in.length()%1024);
91     for (int j=0; j<L; j++) {
92       long loc = n - (L-j)*m; 
93       in.seek(loc/3);
94       in.seek(loc);
95       for (int i=0; i<m; i++) {
96         byte bt = in.readByte();
97         byte expected = (byte) (1 + j + (i & 0x0003F));
98         assertEquals("must read same value that was written! j="+j+" i="+i,expected,bt);
99       }
100     }
101   }
102 }