forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlowRAMDirectory.cs
More file actions
268 lines (229 loc) · 8.43 KB
/
Copy pathSlowRAMDirectory.cs
File metadata and controls
268 lines (229 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Lucene version compatibility level 4.8.1
using RandomizedTesting.Generators;
using System;
using System.Diagnostics;
using System.Threading;
namespace Lucene.Net.Facet
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using IndexInput = Lucene.Net.Store.IndexInput;
using IndexOutput = Lucene.Net.Store.IndexOutput;
using IOContext = Lucene.Net.Store.IOContext;
using RAMDirectory = Lucene.Net.Store.RAMDirectory;
/// <summary>
/// Test utility - slow directory
/// </summary>
// TODO: move to test-framework and sometimes use in tests?
public class SlowRAMDirectory : RAMDirectory
{
private const int IO_SLEEP_THRESHOLD = 50;
internal Random random;
private int sleepMillis;
public virtual void SetSleepMillis(int sleepMillis)
{
this.sleepMillis = sleepMillis;
}
public SlowRAMDirectory(int sleepMillis, Random random)
{
this.sleepMillis = sleepMillis;
this.random = random;
}
public override IndexOutput CreateOutput(string name, IOContext context)
{
if (sleepMillis != -1)
{
return new SlowIndexOutput(this, base.CreateOutput(name, context));
}
return base.CreateOutput(name, context);
}
public override IndexInput OpenInput(string name, IOContext context)
{
if (sleepMillis != -1)
{
return new SlowIndexInput(this, base.OpenInput(name, context));
}
return base.OpenInput(name, context);
}
internal virtual void DoSleep(Random random, int length)
{
int sTime = length < 10 ? sleepMillis : (int)(sleepMillis * Math.Log(length));
if (random != null)
{
sTime = random.Next(sTime);
}
try
{
Sleep(sTime);
}
catch (Exception ie) when (ie.IsInterruptedException())
{
throw new Util.ThreadInterruptedException(ie);
}
}
// LUCENENET specific - We can't use Thread.Sleep which depends on the clock
// interrupt frequency, and that frequency might be too low for low values like 1 millisecond.
private static void Sleep(int milliseconds)
{
long ticks = (long)((Stopwatch.Frequency / (double)1000) * milliseconds); // ticks per millisecond * milliseconds = total delay ticks
long initialTick = Stopwatch.GetTimestamp();
long targetTick = initialTick + ticks;
while (Stopwatch.GetTimestamp() < targetTick)
{
Thread.SpinWait(1);
}
}
/// <summary>
/// Make a private random. </summary>
internal virtual Random ForkRandom()
{
if (random is null)
{
return null;
}
return new J2N.Randomizer(random.NextInt64());
}
/// <summary>
/// Delegate class to wrap an IndexInput and delay reading bytes by some
/// specified time.
/// </summary>
private class SlowIndexInput : IndexInput
{
private readonly SlowRAMDirectory outerInstance;
private readonly IndexInput ii;
private int numRead = 0;
private readonly Random rand;
public SlowIndexInput(SlowRAMDirectory outerInstance, IndexInput ii)
: base("SlowIndexInput(" + ii + ")")
{
this.outerInstance = outerInstance;
this.rand = outerInstance.ForkRandom();
this.ii = ii;
}
public override byte ReadByte()
{
if (numRead >= IO_SLEEP_THRESHOLD)
{
outerInstance.DoSleep(rand, 0);
numRead = 0;
}
++numRead;
return ii.ReadByte();
}
// LUCENENET: Use Span<byte> instead of byte[] for better compatibility.
public override void ReadBytes(Span<byte> destination)
{
int len = destination.Length;
if (numRead >= IO_SLEEP_THRESHOLD)
{
outerInstance.DoSleep(rand, len);
numRead = 0;
}
numRead += len;
ii.ReadBytes(destination);
}
// TODO: is it intentional that clone doesn't wrap?
public override object Clone()
{
return ii.Clone();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
ii.Dispose();
}
}
public override bool Equals(object o)
{
return ii.Equals(o);
}
public override long Position => ii.Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream
public override int GetHashCode()
{
return ii.GetHashCode();
}
public override long Length => ii.Length;
public override void Seek(long pos)
{
ii.Seek(pos);
}
}
/// <summary>
/// Delegate class to wrap an IndexOutput and delay writing bytes by some
/// specified time.
/// </summary>
private class SlowIndexOutput : IndexOutput
{
private readonly SlowRAMDirectory outerInstance;
private readonly IndexOutput io;
private int numWrote;
private readonly Random rand;
public SlowIndexOutput(SlowRAMDirectory outerInstance, IndexOutput io)
{
this.outerInstance = outerInstance;
this.io = io;
this.rand = outerInstance.ForkRandom();
}
public override void WriteByte(byte b)
{
if (numWrote >= IO_SLEEP_THRESHOLD)
{
outerInstance.DoSleep(rand, 0);
numWrote = 0;
}
++numWrote;
io.WriteByte(b);
}
// LUCENENET: Use ReadOnlySpan<byte> instead of byte[] for better compatibility.
public override void WriteBytes(ReadOnlySpan<byte> source)
{
int length = source.Length;
if (numWrote >= IO_SLEEP_THRESHOLD)
{
outerInstance.DoSleep(rand, length);
numWrote = 0;
}
numWrote += length;
io.WriteBytes(source);
}
[Obsolete]
public override void Seek(long pos)
{
io.Seek(pos);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
io.Dispose();
}
}
public override void Flush()
{
io.Flush();
}
public override long Position => io.Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream
public override long Length
{
get => io.Length;
set => throw IllegalStateException.Create("Length is readonly"); // LUCENENET specific: We cannot override get without also overriding set, so we throw if it is set
}
public override long Checksum => io.Checksum;
}
}
}