add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / test / org / apache / lucene / util / SlowRAMDirectory.java
1 package org.apache.lucene.util;
2
3 import java.io.IOException;
4 import java.util.Random;
5
6 import org.apache.lucene.store.IndexInput;
7 import org.apache.lucene.store.IndexOutput;
8 import org.apache.lucene.store.RAMDirectory;
9 import org.apache.lucene.util.ThreadInterruptedException;
10
11 /**
12  * Licensed to the Apache Software Foundation (ASF) under one or more
13  * contributor license agreements.  See the NOTICE file distributed with
14  * this work for additional information regarding copyright ownership.
15  * The ASF licenses this file to You under the Apache License, Version 2.0
16  * (the "License"); you may not use this file except in compliance with
17  * the License.  You may obtain a copy of the License at
18  *
19  *     http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  */
27
28 /**
29  * Test utility - slow directory
30  */
31 // TODO: move to test-framework and sometimes use in tests?
32 public class SlowRAMDirectory extends RAMDirectory {
33
34   private static final int IO_SLEEP_THRESHOLD = 50;
35   
36   private Random random;
37   private int sleepMillis;
38
39   public void setSleepMillis(int sleepMillis) {
40     this.sleepMillis = sleepMillis;
41   }
42   
43   public SlowRAMDirectory(int sleepMillis, Random random) {
44     this.sleepMillis = sleepMillis;
45     this.random = random;
46   }
47
48   @Override
49   public IndexOutput createOutput(String name) throws IOException {
50     if (sleepMillis != -1) {
51       return new SlowIndexOutput(super.createOutput(name));
52     } 
53
54     return super.createOutput(name);
55   }
56
57   @Override
58   public IndexInput openInput(String name) throws IOException {
59     if (sleepMillis != -1) {
60       return new SlowIndexInput(super.openInput(name));
61     } 
62     return super.openInput(name);
63   }
64
65   @Override
66   public IndexInput openInput(String name, int bufferSize) throws IOException {
67     if (sleepMillis != -1) {
68       return new SlowIndexInput(super.openInput(name, bufferSize));
69     } 
70     return super.openInput(name, bufferSize);
71   }
72
73   void doSleep(int length) {
74     int sTime = length<10 ? sleepMillis : (int) (sleepMillis * Math.log(length));
75     if (random!=null) {
76       sTime = random.nextInt(sTime);
77     }
78     try {
79       Thread.sleep(sTime);
80     } catch (InterruptedException e) {
81       throw new ThreadInterruptedException(e);
82     }
83   }
84
85   /**
86    * Delegate class to wrap an IndexInput and delay reading bytes by some
87    * specified time.
88    */
89   private class SlowIndexInput extends IndexInput {
90     private IndexInput ii;
91     private int numRead = 0;
92     
93     public SlowIndexInput(IndexInput ii) {
94       this.ii = ii;
95     }
96     
97     @Override
98     public byte readByte() throws IOException {
99       if (numRead >= IO_SLEEP_THRESHOLD) {
100         doSleep(0);
101         numRead = 0;
102       }
103       ++numRead;
104       return ii.readByte();
105     }
106     
107     @Override
108     public void readBytes(byte[] b, int offset, int len) throws IOException {
109       if (numRead >= IO_SLEEP_THRESHOLD) {
110         doSleep(len);
111         numRead = 0;
112       }
113       numRead += len;
114       ii.readBytes(b, offset, len);
115     }
116     
117     @Override public Object clone() { return ii.clone(); }
118     @Override public void close() throws IOException { ii.close(); }
119     @Override public boolean equals(Object o) { return ii.equals(o); }
120     @Override public long getFilePointer() { return ii.getFilePointer(); }
121     @Override public int hashCode() { return ii.hashCode(); }
122     @Override public long length() { return ii.length(); }
123     @Override public void seek(long pos) throws IOException { ii.seek(pos); }
124     
125   }
126   
127   /**
128    * Delegate class to wrap an IndexOutput and delay writing bytes by some
129    * specified time.
130    */
131   private class SlowIndexOutput extends IndexOutput {
132     
133     private IndexOutput io;
134     private int numWrote;
135     
136     public SlowIndexOutput(IndexOutput io) {
137       this.io = io;
138     }
139     
140     @Override
141     public void writeByte(byte b) throws IOException {
142       if (numWrote >= IO_SLEEP_THRESHOLD) {
143         doSleep(0);
144         numWrote = 0;
145       }
146       ++numWrote;
147       io.writeByte(b);
148     }
149     
150     @Override
151     public void writeBytes(byte[] b, int offset, int length) throws IOException {
152       if (numWrote >= IO_SLEEP_THRESHOLD) {
153         doSleep(length);
154         numWrote = 0;
155       }
156       numWrote += length;
157       io.writeBytes(b, offset, length);
158     }
159     
160     @Override public void close() throws IOException { io.close(); }
161     @Override public void flush() throws IOException { io.flush(); }
162     @Override public long getFilePointer() { return io.getFilePointer(); }
163     @Override public long length() throws IOException { return io.length(); }
164     @Override public void seek(long pos) throws IOException { io.seek(pos); }
165   }
166   
167 }