public LogBuffers(string logFileName) { var fileInfo = new FileInfo(logFileName); var logLength = fileInfo.Length; var termLength = LogBufferDescriptor.ComputeTermLength(logLength); LogBufferDescriptor.CheckTermLength(termLength); _termLength = termLength; // if log length exceeds MAX_INT we need multiple mapped buffers, (see FileChannel.map doc). if (logLength < int.MaxValue) { var mappedBuffer = IoUtil.MapExistingFile(logFileName); _mappedByteBuffers = new[] { mappedBuffer }; var metaDataSectionOffset = termLength * LogBufferDescriptor.PARTITION_COUNT; for (var i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++) { var metaDataOffset = metaDataSectionOffset + (i * LogBufferDescriptor.TERM_META_DATA_LENGTH); _atomicBuffers[i] = new UnsafeBuffer(mappedBuffer.Pointer, i * termLength, termLength); _atomicBuffers[i + LogBufferDescriptor.PARTITION_COUNT] = new UnsafeBuffer(mappedBuffer.Pointer, metaDataOffset, LogBufferDescriptor.TERM_META_DATA_LENGTH); } _atomicBuffers[_atomicBuffers.Length - 1] = new UnsafeBuffer(mappedBuffer.Pointer, (int)(logLength - LogBufferDescriptor.LOG_META_DATA_LENGTH), LogBufferDescriptor.LOG_META_DATA_LENGTH); } else { _mappedByteBuffers = new MappedByteBuffer[LogBufferDescriptor.PARTITION_COUNT + 1]; var metaDataSectionOffset = termLength * (long)LogBufferDescriptor.PARTITION_COUNT; var metaDataSectionLength = (int)(logLength - metaDataSectionOffset); var memoryMappedFile = IoUtil.OpenMemoryMappedFile(logFileName); var metaDataMappedBuffer = new MappedByteBuffer(memoryMappedFile, metaDataSectionOffset, metaDataSectionLength); _mappedByteBuffers[_mappedByteBuffers.Length - 1] = metaDataMappedBuffer; for (var i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++) { _mappedByteBuffers[i] = new MappedByteBuffer(memoryMappedFile, termLength * (long)i, termLength); _atomicBuffers[i] = new UnsafeBuffer(_mappedByteBuffers[i].Pointer, termLength); _atomicBuffers[i + LogBufferDescriptor.PARTITION_COUNT] = new UnsafeBuffer(metaDataMappedBuffer.Pointer, i * LogBufferDescriptor.TERM_META_DATA_LENGTH, LogBufferDescriptor.TERM_META_DATA_LENGTH); } _atomicBuffers[_atomicBuffers.Length - 1] = new UnsafeBuffer(metaDataMappedBuffer.Pointer, metaDataSectionLength - LogBufferDescriptor.LOG_META_DATA_LENGTH, LogBufferDescriptor.LOG_META_DATA_LENGTH); } // TODO try/catch foreach (var buffer in _atomicBuffers) { buffer.VerifyAlignment(); } }
public LogBuffers(string logFileName, int termLength = LogBufferDescriptor.TERM_MIN_LENGTH) { try { long logLength = LogBufferDescriptor.PARTITION_COUNT * (LogBufferDescriptor.TERM_META_DATA_LENGTH + termLength) + LogBufferDescriptor.LOG_META_DATA_LENGTH; termLength = LogBufferDescriptor.ComputeTermLength(logLength); LogBufferDescriptor.CheckTermLength(termLength); _df = new DirectFile(logFileName, logLength); _termLength = termLength; // if log length exceeds MAX_INT we need multiple mapped buffers, (see FileChannel.map doc). if (logLength < int.MaxValue) { int metaDataSectionOffset = termLength * LogBufferDescriptor.PARTITION_COUNT; for (int i = 0; i < LogBufferDescriptor.PARTITION_COUNT; i++) { int metaDataOffset = metaDataSectionOffset + (i * LogBufferDescriptor.TERM_META_DATA_LENGTH); _buffers[i] = new DirectBuffer(termLength, _df.Buffer.Data + i * termLength); _buffers[i + LogBufferDescriptor.PARTITION_COUNT] = new DirectBuffer(LogBufferDescriptor.TERM_META_DATA_LENGTH, _df.Buffer.Data + metaDataOffset); _partitions[i] = new LogBufferPartition(_buffers[i], _buffers[i + LogBufferDescriptor.PARTITION_COUNT]); } _buffers[_buffers.Length - 1] = new DirectBuffer(LogBufferDescriptor.LOG_META_DATA_LENGTH, _df.Buffer.Data + (int)(logLength - LogBufferDescriptor.LOG_META_DATA_LENGTH)); } else { throw new NotImplementedException("TODO Check .NET mapping limit"); } } catch (IOException ex) { throw new AggregateException(ex); } foreach (var buffer in _buffers) { buffer.VerifyAlignment(8); } }