22 namespace Lucene.Net.Store
64 private class AnonymousClassPrivilegedExceptionAction
66 public AnonymousClassPrivilegedExceptionAction(byte[] buffer,
MMapDirectory enclosingInstance)
68 InitBlock(buffer, enclosingInstance);
70 private void InitBlock(byte[] buffer,
MMapDirectory enclosingInstance)
73 this.enclosingInstance = enclosingInstance;
75 private byte[] buffer;
81 return enclosingInstance;
85 public virtual System.Object Run()
98 throw new NotImplementedException(
"Port issue: sun.misc.Cleaner()");
103 private void InitBlock()
105 maxBBuf =
Constants.JRE_IS_64BIT?System.Int32.MaxValue:(256 * 1024 * 1024);
117 : base(path, lockFactory)
134 private bool useUnmapHack =
false;
152 public virtual bool UseUnmap
154 get {
return useUnmapHack; }
157 if (value && !UNMAP_SUPPORTED)
158 throw new System.ArgumentException(
"Unmap hack not supported on this platform!");
159 this.useUnmapHack = value;
167 internal void CleanMapping(System.IO.MemoryStream buffer)
175 throw new NotImplementedException(
"Port issue: java.security.AccessController.doPrivileged()");
178 catch (System.Exception e)
180 System.IO.IOException ioe =
new System.IO.IOException(
"unable to unmap the mapped buffer", e.InnerException);
196 public virtual int MaxChunkSize
198 get {
return maxBBuf; }
202 throw new System.ArgumentException(
"Maximum chunk size for mmap must be >0");
203 this.maxBBuf = value;
211 this.enclosingInstance = enclosingInstance;
213 private MMapDirectory enclosingInstance;
214 public MMapDirectory Enclosing_Instance
218 return enclosingInstance;
223 private System.IO.MemoryStream buffer;
225 private bool isClone;
226 private bool isDisposed;
228 internal MMapIndexInput(MMapDirectory enclosingInstance, System.IO.FileStream raf)
230 byte[] data =
new byte[raf.Length];
231 raf.Read(data, 0, (
int) raf.Length);
233 InitBlock(enclosingInstance);
234 this.length = raf.Length;
235 this.buffer =
new System.IO.MemoryStream(data);
238 public override byte ReadByte()
242 return (byte) buffer.ReadByte();
244 catch (ObjectDisposedException)
246 throw new System.IO.IOException(
"read past EOF");
250 public override void ReadBytes(byte[] b,
int offset,
int len)
254 buffer.Read(b, offset, len);
256 catch (ObjectDisposedException)
258 throw new System.IO.IOException(
"read past EOF");
262 public override long FilePointer
266 return buffer.Position;
270 public override void Seek(
long pos)
272 buffer.Seek(pos, System.IO.SeekOrigin.Begin);
275 public override long Length()
280 public override System.Object Clone()
284 MMapIndexInput clone = (MMapIndexInput) base.Clone();
285 clone.isClone =
true;
290 protected override void Dispose(
bool isDisposing)
292 if (isDisposed)
return;
296 if (isClone || buffer == null)
301 Enclosing_Instance.CleanMapping(buffer);
316 protected internal class MultiMMapIndexInput:
IndexInput, System.ICloneable
318 private void InitBlock(MMapDirectory enclosingInstance)
320 this.enclosingInstance = enclosingInstance;
322 private MMapDirectory enclosingInstance;
323 public MMapDirectory Enclosing_Instance
327 return enclosingInstance;
332 private System.IO.MemoryStream[] buffers;
333 private int[] bufSizes;
337 private bool isDisposed;
339 private int curBufIndex;
340 private int maxBufSize;
342 private System.IO.MemoryStream curBuf;
343 private int curAvail;
345 private bool isClone =
false;
347 public MultiMMapIndexInput(MMapDirectory enclosingInstance, System.IO.FileStream raf,
int maxBufSize)
349 InitBlock(enclosingInstance);
350 this.length = raf.Length;
351 this.maxBufSize = maxBufSize;
354 throw new System.ArgumentException(
"Non positive maxBufSize: " + maxBufSize);
356 if ((length / maxBufSize) > System.Int32.MaxValue)
358 throw new System.ArgumentException(
"RandomAccessFile too big for maximum buffer size: " + raf.ToString());
361 int nrBuffers = (int) (length / maxBufSize);
362 if (((
long) nrBuffers * maxBufSize) < length)
365 this.buffers =
new System.IO.MemoryStream[nrBuffers];
366 this.bufSizes =
new int[nrBuffers];
368 long bufferStart = 0;
369 System.IO.FileStream rafc = raf;
370 for (
int bufNr = 0; bufNr < nrBuffers; bufNr++)
372 byte[] data =
new byte[rafc.Length];
373 raf.Read(data, 0, (
int) rafc.Length);
375 int bufSize = (length > (bufferStart + maxBufSize))?maxBufSize:(int) (length - bufferStart);
376 this.buffers[bufNr] =
new System.IO.MemoryStream(data);
377 this.bufSizes[bufNr] = bufSize;
378 bufferStart += bufSize;
383 public override byte ReadByte()
390 if (curBufIndex >= buffers.Length)
391 throw new System.IO.IOException(
"read past EOF");
392 curBuf = buffers[curBufIndex];
393 curBuf.Seek(0, System.IO.SeekOrigin.Begin);
394 curAvail = bufSizes[curBufIndex];
397 return (byte) curBuf.ReadByte();
400 public override void ReadBytes(byte[] b,
int offset,
int len)
402 while (len > curAvail)
404 curBuf.Read(b, offset, curAvail);
408 if (curBufIndex >= buffers.Length)
409 throw new System.IO.IOException(
"read past EOF");
410 curBuf = buffers[curBufIndex];
411 curBuf.Seek(0, System.IO.SeekOrigin.Begin);
412 curAvail = bufSizes[curBufIndex];
414 curBuf.Read(b, offset, len);
418 public override long FilePointer
420 get {
return ((
long) curBufIndex*maxBufSize) + curBuf.Position; }
423 public override void Seek(
long pos)
425 curBufIndex = (int) (pos / maxBufSize);
426 curBuf = buffers[curBufIndex];
427 int bufOffset = (int) (pos - ((
long) curBufIndex * maxBufSize));
428 curBuf.Seek(bufOffset, System.IO.SeekOrigin.Begin);
429 curAvail = bufSizes[curBufIndex] - bufOffset;
432 public override long Length()
437 public override System.Object Clone()
439 MultiMMapIndexInput clone = (MultiMMapIndexInput) base.Clone();
440 clone.isClone =
true;
441 clone.buffers =
new System.IO.MemoryStream[buffers.Length];
445 for (
int bufNr = 0; bufNr < buffers.Length; bufNr++)
447 clone.buffers[bufNr] = buffers[bufNr];
451 clone.Seek(FilePointer);
453 catch (System.IO.IOException ioe)
455 System.SystemException newException =
new System.SystemException(ioe.Message, ioe);
461 protected override void Dispose(
bool disposing)
463 if (isDisposed)
return;
464 if (isClone || buffers == null)
468 for (
int bufNr = 0; bufNr < buffers.Length; bufNr++)
473 Enclosing_Instance.CleanMapping(buffers[bufNr]);
477 buffers[bufNr] = null;
490 public override IndexInput OpenInput(System.String name,
int bufferSize)
493 System.String path = System.IO.Path.Combine(
Directory.FullName, name);
494 System.IO.FileStream raf =
new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
497 return (raf.Length <= (
long) maxBBuf)?(
IndexInput)
new MMapIndexInput(
this, raf):(
IndexInput)
new MultiMMapIndexInput(
this, raf, maxBBuf);
509 return new SimpleFSDirectory.SimpleFSIndexOutput(
new System.IO.FileInfo(System.IO.Path.Combine(internalDirectory.FullName, name)));
523 throw new NotImplementedException(
"Port issue: sun.misc.Cleaner.clean()");
527 catch (System.Exception)