pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / store / TestCopyBytes.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
22 import org.apache.lucene.util.LuceneTestCase;
23 import org.apache.lucene.util._TestUtil;
24
25 import org.junit.Test;
26
27 public class TestCopyBytes extends LuceneTestCase {
28
29   private byte value(int idx) {
30     return (byte) ((idx%256) * (1+(idx/256)));
31   }
32
33
34   @Test
35   public void testCopyBytes() throws Exception {
36     int num = atLeast(10);
37     for(int iter=0;iter<num;iter++) {
38       Directory dir = newDirectory();
39       if (VERBOSE) {
40         System.out.println("TEST: iter=" + iter + " dir=" + dir);
41       }
42
43       // make random file
44       IndexOutput out = dir.createOutput("test");
45       byte[] bytes = new byte[_TestUtil.nextInt(random, 1, 77777)];
46       final int size = _TestUtil.nextInt(random, 1, 1777777);
47       int upto = 0;
48       int byteUpto = 0;
49       while(upto < size) {
50         bytes[byteUpto++] = value(upto);
51         upto++;
52         if (byteUpto == bytes.length) {
53           out.writeBytes(bytes, 0, bytes.length);
54           byteUpto = 0;
55         }
56       }
57
58       out.writeBytes(bytes, 0, byteUpto);
59       assertEquals(size, out.getFilePointer());
60       out.close();
61       assertEquals(size, dir.fileLength("test"));
62
63       // copy from test -> test2
64       final IndexInput in = dir.openInput("test");
65
66       out = dir.createOutput("test2");
67
68       upto = 0;
69       while(upto < size) {
70         if (random.nextBoolean()) {
71           out.writeByte(in.readByte());
72           upto++;
73         } else {
74           final int chunk = Math.min(_TestUtil.nextInt(random, 1, bytes.length), size-upto);
75           out.copyBytes(in, chunk);
76           upto += chunk;
77         }
78       }
79       assertEquals(size, upto);
80       out.close();
81       in.close();
82
83       // verify
84       IndexInput in2 = dir.openInput("test2");
85       upto = 0;
86       while(upto < size) {
87         if (random.nextBoolean()) {
88           final byte v = in2.readByte();
89           assertEquals(value(upto), v);
90           upto++;
91         } else {
92           final int limit = Math.min(_TestUtil.nextInt(random, 1, bytes.length), size-upto);
93           in2.readBytes(bytes, 0, limit);
94           for(int byteIdx=0;byteIdx<limit;byteIdx++) {
95             assertEquals(value(upto), bytes[byteIdx]);
96             upto++;
97           }
98         }
99       }
100       in2.close();
101
102       dir.deleteFile("test");
103       dir.deleteFile("test2");
104       
105       dir.close();
106     }
107   }
108   
109   // LUCENE-3541
110   public void testCopyBytesWithThreads() throws Exception {
111     int datalen = _TestUtil.nextInt(random, 101, 10000);
112     byte data[] = new byte[datalen];
113     random.nextBytes(data);
114     
115     Directory d = newDirectory();
116     IndexOutput output = d.createOutput("data");
117     output.writeBytes(data, 0, datalen);
118     output.close();
119     
120     IndexInput input = d.openInput("data");
121     IndexOutput outputHeader = d.createOutput("header");
122     // copy our 100-byte header
123     input.copyBytes(outputHeader, 100);
124     outputHeader.close();
125     
126     // now make N copies of the remaining bytes
127     CopyThread copies[] = new CopyThread[10];
128     for (int i = 0; i < copies.length; i++) {
129       copies[i] = new CopyThread((IndexInput) input.clone(), d.createOutput("copy" + i));
130     }
131     
132     for (int i = 0; i < copies.length; i++) {
133       copies[i].start();
134     }
135     
136     for (int i = 0; i < copies.length; i++) {
137       copies[i].join();
138     }
139     
140     for (int i = 0; i < copies.length; i++) {
141       IndexInput copiedData = d.openInput("copy" + i);
142       byte[] dataCopy = new byte[datalen];
143       System.arraycopy(data, 0, dataCopy, 0, 100); // copy the header for easy testing
144       copiedData.readBytes(dataCopy, 100, datalen-100);
145       assertArrayEquals(data, dataCopy);
146       copiedData.close();
147     }
148     input.close();
149     d.close();
150     
151   }
152   
153   static class CopyThread extends Thread {
154     final IndexInput src;
155     final IndexOutput dst;
156     
157     CopyThread(IndexInput src, IndexOutput dst) {
158       this.src = src;
159       this.dst = dst;
160     }
161
162     @Override
163     public void run() {
164       try {
165         src.copyBytes(dst, src.length()-100);
166         dst.close();
167       } catch (IOException ex) {
168         throw new RuntimeException(ex);
169       }
170     }
171   }
172 }