~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/facet/src/test/org/apache/lucene/util/SlowRAMDirectory.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.util;
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
 
import java.util.Random;
22
 
 
23
 
import org.apache.lucene.store.IndexInput;
24
 
import org.apache.lucene.store.IndexOutput;
25
 
import org.apache.lucene.store.RAMDirectory;
26
 
 
27
 
/**
28
 
 * Test utility - slow directory
29
 
 */
30
 
// TODO: move to test-framework and sometimes use in tests?
31
 
public class SlowRAMDirectory extends RAMDirectory {
32
 
 
33
 
  private static final int IO_SLEEP_THRESHOLD = 50;
34
 
  
35
 
  private Random random;
36
 
  private int sleepMillis;
37
 
 
38
 
  public void setSleepMillis(int sleepMillis) {
39
 
    this.sleepMillis = sleepMillis;
40
 
  }
41
 
  
42
 
  public SlowRAMDirectory(int sleepMillis, Random random) {
43
 
    this.sleepMillis = sleepMillis;
44
 
    this.random = random;
45
 
  }
46
 
 
47
 
  @Override
48
 
  public IndexOutput createOutput(String name) throws IOException {
49
 
    if (sleepMillis != -1) {
50
 
      return new SlowIndexOutput(super.createOutput(name));
51
 
    } 
52
 
 
53
 
    return super.createOutput(name);
54
 
  }
55
 
 
56
 
  @Override
57
 
  public IndexInput openInput(String name) throws IOException {
58
 
    if (sleepMillis != -1) {
59
 
      return new SlowIndexInput(super.openInput(name));
60
 
    } 
61
 
    return super.openInput(name);
62
 
  }
63
 
 
64
 
  @Override
65
 
  public IndexInput openInput(String name, int bufferSize) throws IOException {
66
 
    if (sleepMillis != -1) {
67
 
      return new SlowIndexInput(super.openInput(name, bufferSize));
68
 
    } 
69
 
    return super.openInput(name, bufferSize);
70
 
  }
71
 
 
72
 
  void doSleep(int length) {
73
 
    int sTime = length<10 ? sleepMillis : (int) (sleepMillis * Math.log(length));
74
 
    if (random!=null) {
75
 
      sTime = random.nextInt(sTime);
76
 
    }
77
 
    try {
78
 
      Thread.sleep(sTime);
79
 
    } catch (InterruptedException e) {
80
 
      throw new ThreadInterruptedException(e);
81
 
    }
82
 
  }
83
 
 
84
 
  /**
85
 
   * Delegate class to wrap an IndexInput and delay reading bytes by some
86
 
   * specified time.
87
 
   */
88
 
  private class SlowIndexInput extends IndexInput {
89
 
    private IndexInput ii;
90
 
    private int numRead = 0;
91
 
    
92
 
    public SlowIndexInput(IndexInput ii) {
93
 
      super("SlowIndexInput(" + 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
 
}