1 package org.apache.lucene.util;
3 import java.io.IOException;
4 import java.util.Random;
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;
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
19 * http://www.apache.org/licenses/LICENSE-2.0
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.
29 * Test utility - slow directory
31 // TODO: move to test-framework and sometimes use in tests?
32 public class SlowRAMDirectory extends RAMDirectory {
34 private static final int IO_SLEEP_THRESHOLD = 50;
36 private Random random;
37 private int sleepMillis;
39 public void setSleepMillis(int sleepMillis) {
40 this.sleepMillis = sleepMillis;
43 public SlowRAMDirectory(int sleepMillis, Random random) {
44 this.sleepMillis = sleepMillis;
49 public IndexOutput createOutput(String name) throws IOException {
50 if (sleepMillis != -1) {
51 return new SlowIndexOutput(super.createOutput(name));
54 return super.createOutput(name);
58 public IndexInput openInput(String name) throws IOException {
59 if (sleepMillis != -1) {
60 return new SlowIndexInput(super.openInput(name));
62 return super.openInput(name);
66 public IndexInput openInput(String name, int bufferSize) throws IOException {
67 if (sleepMillis != -1) {
68 return new SlowIndexInput(super.openInput(name, bufferSize));
70 return super.openInput(name, bufferSize);
73 void doSleep(int length) {
74 int sTime = length<10 ? sleepMillis : (int) (sleepMillis * Math.log(length));
76 sTime = random.nextInt(sTime);
80 } catch (InterruptedException e) {
81 throw new ThreadInterruptedException(e);
86 * Delegate class to wrap an IndexInput and delay reading bytes by some
89 private class SlowIndexInput extends IndexInput {
90 private IndexInput ii;
91 private int numRead = 0;
93 public SlowIndexInput(IndexInput ii) {
98 public byte readByte() throws IOException {
99 if (numRead >= IO_SLEEP_THRESHOLD) {
104 return ii.readByte();
108 public void readBytes(byte[] b, int offset, int len) throws IOException {
109 if (numRead >= IO_SLEEP_THRESHOLD) {
114 ii.readBytes(b, offset, len);
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); }
128 * Delegate class to wrap an IndexOutput and delay writing bytes by some
131 private class SlowIndexOutput extends IndexOutput {
133 private IndexOutput io;
134 private int numWrote;
136 public SlowIndexOutput(IndexOutput io) {
141 public void writeByte(byte b) throws IOException {
142 if (numWrote >= IO_SLEEP_THRESHOLD) {
151 public void writeBytes(byte[] b, int offset, int length) throws IOException {
152 if (numWrote >= IO_SLEEP_THRESHOLD) {
157 io.writeBytes(b, offset, length);
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); }