1 package org.apache.lucene.store;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
21 import java.util.HashMap;
23 import org.apache.lucene.util.LuceneTestCase;
25 /** Test huge RAMFile with more than Integer.MAX_VALUE bytes. */
26 public class TestHugeRamFile extends LuceneTestCase {
28 private static final long MAX_VALUE = (long) 2 * (long) Integer.MAX_VALUE;
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[]>();
36 protected byte[] newBuffer(int size) {
38 if (capacity <= MAX_VALUE) {
39 // below maxint we reuse buffers
40 byte buf[] = singleBuffers.get(Integer.valueOf(size));
43 //System.out.println("allocate: "+size);
44 singleBuffers.put(Integer.valueOf(size),buf);
48 //System.out.println("allocate: "+size); System.out.flush();
49 return new byte[size];
53 /** Test huge RAMFile with more than Integer.MAX_VALUE bytes. (LUCENE-957) */
54 public void testHugeFile() throws IOException {
55 DenseRAMFile f = new DenseRAMFile();
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);
63 for (int i = 0; i < b2.length; i++) {
64 b2[i] = (byte) (i & 0x0003F);
67 assertEquals("output length must match",n,out.length());
68 while (n <= MAX_VALUE - b1.length) {
69 out.writeBytes(b1,0,b1.length);
72 assertEquals("output length must match",n,out.length());
74 //System.out.println("after writing b1's, length = "+out.length()+" (MAX_VALUE="+MAX_VALUE+")");
77 for (int j=0; j<L; j++) {
78 for (int i = 0; i < b2.length; i++) {
81 out.writeBytes(b2,0,m);
84 assertEquals("output length must match",n,out.length());
88 RAMInputStream in = new RAMInputStream(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;
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);