1 package org.apache.lucene.util;
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.Random;
23 import org.apache.lucene.store.IndexInput;
24 import org.apache.lucene.store.IndexOutput;
25 import org.apache.lucene.store.RAMDirectory;
28 * Test utility - slow directory
30 // TODO: move to test-framework and sometimes use in tests?
31 public class SlowRAMDirectory extends RAMDirectory {
33 private static final int IO_SLEEP_THRESHOLD = 50;
35 private Random random;
36 private int sleepMillis;
38 public void setSleepMillis(int sleepMillis) {
39 this.sleepMillis = sleepMillis;
42 public SlowRAMDirectory(int sleepMillis, Random random) {
43 this.sleepMillis = sleepMillis;
48 public IndexOutput createOutput(String name) throws IOException {
49 if (sleepMillis != -1) {
50 return new SlowIndexOutput(super.createOutput(name));
53 return super.createOutput(name);
57 public IndexInput openInput(String name) throws IOException {
58 if (sleepMillis != -1) {
59 return new SlowIndexInput(super.openInput(name));
61 return super.openInput(name);
65 public IndexInput openInput(String name, int bufferSize) throws IOException {
66 if (sleepMillis != -1) {
67 return new SlowIndexInput(super.openInput(name, bufferSize));
69 return super.openInput(name, bufferSize);
72 void doSleep(int length) {
73 int sTime = length<10 ? sleepMillis : (int) (sleepMillis * Math.log(length));
75 sTime = random.nextInt(sTime);
79 } catch (InterruptedException e) {
80 throw new ThreadInterruptedException(e);
85 * Delegate class to wrap an IndexInput and delay reading bytes by some
88 private class SlowIndexInput extends IndexInput {
89 private IndexInput ii;
90 private int numRead = 0;
92 public SlowIndexInput(IndexInput ii) {
93 super("SlowIndexInput(" + 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); }