001: package org.apache.lucene.store;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021: import java.util.HashMap;
022:
023: import org.apache.lucene.util.LuceneTestCase;
024:
025: /** Test huge RAMFile with more than Integer.MAX_VALUE bytes. */
026: public class TestHugeRamFile extends LuceneTestCase {
027:
028: private static final long MAX_VALUE = (long) 2
029: * (long) Integer.MAX_VALUE;
030:
031: /** Fake a huge ram file by using the same byte buffer for all
032: * buffers under maxint. */
033: private static class DenseRAMFile extends RAMFile {
034: private long capacity = 0;
035: private HashMap singleBuffers = new HashMap();
036:
037: byte[] newBuffer(int size) {
038: capacity += size;
039: if (capacity <= MAX_VALUE) {
040: // below maxint we reuse buffers
041: byte buf[] = (byte[]) singleBuffers.get(new Integer(
042: size));
043: if (buf == null) {
044: buf = new byte[size];
045: //System.out.println("allocate: "+size);
046: singleBuffers.put(new Integer(size), buf);
047: }
048: return buf;
049: }
050: //System.out.println("allocate: "+size); System.out.flush();
051: return new byte[size];
052: }
053: }
054:
055: /** Test huge RAMFile with more than Integer.MAX_VALUE bytes. (LUCENE-957) */
056: public void testHugeFile() throws IOException {
057: DenseRAMFile f = new DenseRAMFile();
058: // output part
059: RAMOutputStream out = new RAMOutputStream(f);
060: byte b1[] = new byte[RAMOutputStream.BUFFER_SIZE];
061: byte b2[] = new byte[RAMOutputStream.BUFFER_SIZE / 3];
062: for (int i = 0; i < b1.length; i++) {
063: b1[i] = (byte) (i & 0x0007F);
064: }
065: for (int i = 0; i < b2.length; i++) {
066: b2[i] = (byte) (i & 0x0003F);
067: }
068: long n = 0;
069: assertEquals("output length must match", n, out.length());
070: while (n <= MAX_VALUE - b1.length) {
071: out.writeBytes(b1, 0, b1.length);
072: out.flush();
073: n += b1.length;
074: assertEquals("output length must match", n, out.length());
075: }
076: //System.out.println("after writing b1's, length = "+out.length()+" (MAX_VALUE="+MAX_VALUE+")");
077: int m = b2.length;
078: long L = 12;
079: for (int j = 0; j < L; j++) {
080: for (int i = 0; i < b2.length; i++) {
081: b2[i]++;
082: }
083: out.writeBytes(b2, 0, m);
084: out.flush();
085: n += m;
086: assertEquals("output length must match", n, out.length());
087: }
088: out.close();
089: // input part
090: RAMInputStream in = new RAMInputStream(f);
091: assertEquals("input length must match", n, in.length());
092: //System.out.println("input length = "+in.length()+" % 1024 = "+in.length()%1024);
093: for (int j = 0; j < L; j++) {
094: long loc = n - (L - j) * m;
095: in.seek(loc / 3);
096: in.seek(loc);
097: for (int i = 0; i < m; i++) {
098: byte bt = in.readByte();
099: byte expected = (byte) (1 + j + (i & 0x0003F));
100: assertEquals(
101: "must read same value that was written! j=" + j
102: + " i=" + i, expected, bt);
103: }
104: }
105: }
106: }
|