/// <summary> /// Maps a file into a set of buffers </summary> internal virtual ByteBuffer[] Map(MMapIndexInput input, FileStream fc, long offset, long length) { if (Number.URShift(length, ChunkSizePower) >= int.MaxValue) throw new ArgumentException("RandomAccessFile too big for chunk size: " + fc.ToString()); long chunkSize = 1L << ChunkSizePower; // we always allocate one more buffer, the last one may be a 0 byte one int nrBuffers = (int)((long)((ulong)length >> ChunkSizePower)) + 1; ByteBuffer[] buffers = new ByteBuffer[nrBuffers]; /* public static MemoryMappedFile CreateFromFile(FileStream fileStream, String mapName, Int64 capacity, MemoryMappedFileAccess access, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability, bool leaveOpen) */ if (input.memoryMappedFile == null) { input.memoryMappedFile = MemoryMappedFile.CreateFromFile(fc, null, length == 0 ? 100 : length, MemoryMappedFileAccess.Read, null, HandleInheritability.Inheritable, false); } long bufferStart = 0L; for (int bufNr = 0; bufNr < nrBuffers; bufNr++) { int bufSize = (int)((length > (bufferStart + chunkSize)) ? chunkSize : (length - bufferStart)); //LUCENE TO-DO buffers[bufNr] = new MemoryMappedFileByteBuffer(input.memoryMappedFile.CreateViewAccessor(offset + bufferStart, bufSize, MemoryMappedFileAccess.Read), -1, 0, bufSize, bufSize); //buffers[bufNr] = fc.Map(FileStream.MapMode.READ_ONLY, offset + bufferStart, bufSize); bufferStart += bufSize; } return buffers; }
/// <summary> /// Maps a file into a set of buffers </summary> internal virtual ByteBuffer[] Map(MMapIndexInput input, FileStream fc, long offset, long length) { if (Number.URShift(length, ChunkSizePower) >= int.MaxValue) throw new ArgumentException("RandomAccessFile too big for chunk size: " + fc.ToString()); long chunkSize = 1L << ChunkSizePower; // we always allocate one more buffer, the last one may be a 0 byte one int nrBuffers = (int)((long)((ulong)length >> ChunkSizePower)) + 1; ByteBuffer[] buffers = new ByteBuffer[nrBuffers]; /* public static MemoryMappedFile CreateFromFile(FileStream fileStream, String mapName, Int64 capacity, MemoryMappedFileAccess access, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability, bool leaveOpen) */ long fileCapacity = length == 0 ? ushort.MaxValue : length; if (input.memoryMappedFile == null) { input.memoryMappedFile = MemoryMappedFile.CreateFromFile(fc, null, fileCapacity, MemoryMappedFileAccess.ReadWrite, null, HandleInheritability.Inheritable, false); } long bufferStart = 0L; for (int bufNr = 0; bufNr < nrBuffers; bufNr++) { int bufSize = (int)((length > (bufferStart + chunkSize)) ? chunkSize : (length - bufferStart)); // LUCENENET: We get a file access exception if we create a 0 byte file at the end of the range. // We can fix this by moving back 1 byte if the bufSize is 0. int adjust = 0; if (bufSize == 0 && bufNr == (nrBuffers - 1) && (offset + bufferStart) > 0) { adjust = 1; } buffers[bufNr] = new MemoryMappedFileByteBuffer(input.memoryMappedFile.CreateViewAccessor((offset + bufferStart) - adjust, bufSize, MemoryMappedFileAccess.Read), -1, 0, bufSize, bufSize); bufferStart += bufSize; } return buffers; }