Ejemplo n.º 1
0
        public DualBinaryStore(IStreamProvider provider, long start, long maxLength)
        {
            if (provider == null) throw new ArgumentNullException("provider");
            if (start < 0) throw new ArgumentException("start parameter must have a value equal or greater than 0");
            if (maxLength <= 0) throw new ArgumentException("maxLength parameter must have a value greater than 0");

            _provider = provider;
            _start = start;
            _maxLength = maxLength;

            _writeHeaderStream = provider.AcquireWriteStream();
            _writeLeftStream = provider.AcquireWriteStream();
            _writeRightStream = provider.AcquireWriteStream();

            _writeHeaderStream.Seek(start, SeekOrigin.Begin);
            if (_writeHeaderStream.Length > start)
            {
                using (var readStream = _provider.AcquireReadStream())
                {
                    readStream.Seek(start, SeekOrigin.Begin);
                    var headerBuffer = new byte[16];
                    readStream.Read(headerBuffer, 0, headerBuffer.Length);
                    _leftOffset = BitConverter.ToInt64(headerBuffer, 0);
                    _rightOffset = BitConverter.ToInt64(headerBuffer, 8);

                    // If the buffer was not generated by the store
                    if (_leftOffset <= 0) _leftOffset = 16;
                    if (_rightOffset <= 0) _rightOffset = maxLength;
                }

                _writeLeftStream.Seek(start + _leftOffset, SeekOrigin.Begin);
                _writeRightStream.Seek(start + _rightOffset, SeekOrigin.Begin);
            }
            else
            {
                _leftOffset = 16;
                _rightOffset = maxLength;

                var headerBuffer = BitConverter.GetBytes(_leftOffset);
                _writeHeaderStream.Write(headerBuffer, 0, headerBuffer.Length);
                headerBuffer = BitConverter.GetBytes(_rightOffset);
                _writeHeaderStream.Write(headerBuffer, 0, headerBuffer.Length);
                _writeHeaderStream.Seek(-16, SeekOrigin.Current);

                _writeRightStream.Seek(start + _leftOffset, SeekOrigin.Begin);
                var buffer = new byte[maxLength - _leftOffset];
                _writeRightStream.Write(buffer, 0, buffer.Length);
                _writeLeftStream.Seek(start + _leftOffset, SeekOrigin.Begin);
            }

            _lastFlushedLeftOffset = _leftOffset;
            _lastFlushedRightOffset = _rightOffset;

            _left = new LeftBinaryStoreSegment(this);
            _right = new RightBinaryStoreSegment(this);
        }
Ejemplo n.º 2
0
        private BinaryStore(IStreamProvider provider, long start, long length, bool canGrow)
        {
            _writeStream = provider.AcquireWriteStream();

            _provider = provider;
            _start = start;
            _length = length;
            _canGrow = canGrow;

            if (_writeStream.Length > start)
            {
                using (var readStream = _provider.AcquireReadStream())
                {
                    readStream.Seek(start, SeekOrigin.Begin);
                    var offsetBuffer = new byte[8];
                    readStream.Read(offsetBuffer, 0, offsetBuffer.Length);
                    _currentOffset = BitConverter.ToInt64(offsetBuffer, 0);
                }
            }
            else
            {
                _currentOffset = 8;
                var offsetBuffer = BitConverter.GetBytes(_currentOffset);
                _writeStream.Seek(start, SeekOrigin.Begin);
                _writeStream.Write(offsetBuffer, 0, offsetBuffer.Length);

                if (length > 0)
                {
                    var requiredFileSize = _start + _length;
                    var buffer = new byte[requiredFileSize - _writeStream.Length - 8];
                    _writeStream.Write(buffer, 0, buffer.Length);
                }
            }

            _lastFlushOffset = _currentOffset;
            _writeStream.Seek(start + _currentOffset, SeekOrigin.Begin);

            _offsetWriteStream = provider.AcquireWriteStream();
            _offsetWriteStream.Seek(start, SeekOrigin.Begin);
        }
Ejemplo n.º 3
0
        private BinaryStore(IStreamProvider provider, long start, long maxLength)
        {
            _writeStream = provider.AcquireWriteStream();

            _provider  = provider;
            _start     = start;
            _maxLength = maxLength;

            if (_writeStream.Length > start)
            {
                using (var readStream = _provider.AcquireReadStream())
                {
                    readStream.Seek(start, SeekOrigin.Begin);
                    var offsetBuffer = new byte[8];
                    readStream.Read(offsetBuffer, 0, offsetBuffer.Length);
                    _currentOffset = BitConverter.ToInt64(offsetBuffer, 0);
                }
            }
            else
            {
                _currentOffset = 8;
                var offsetBuffer = BitConverter.GetBytes(_currentOffset);
                _writeStream.Seek(start, SeekOrigin.Begin);
                _writeStream.Write(offsetBuffer, 0, offsetBuffer.Length);

                if (maxLength > 0)
                {
                    var requiredFileSize = _start + _maxLength;
                    var buffer           = new byte[requiredFileSize - _writeStream.Length - 8];
                    _writeStream.Write(buffer, 0, buffer.Length);
                }
            }

            _lastFlushOffset = _currentOffset;
            _writeStream.Seek(start + _currentOffset, SeekOrigin.Begin);

            _offsetWriteStream = provider.AcquireWriteStream();
            _offsetWriteStream.Seek(start, SeekOrigin.Begin);
        }
Ejemplo n.º 4
0
 public void Write(long storeOffset, byte[] data)
 {
     using (_writeOffsetLock.Enter(storeOffset))
     {
         using (var offsetWriteStream = _provider.AcquireWriteStream())
         {
             offsetWriteStream.Seek(storeOffset, SeekOrigin.Begin);
             offsetWriteStream.Write(data, 0, data.Length);
             if (_lastFlushOffset > storeOffset)
             {
                 _lastFlushOffset = storeOffset - 1;
             }
         }
     }
 }