예제 #1
0
 protected override sealed void FillBuffer()
 {
     if (_ofs == _valueSize)
     {
         Pos = -1;
         End = -1;
         return;
     }
     End   = _stream.Read(Buf, 0, Buf.Length, _ofs);
     _ofs += (ulong)End;
     Pos   = 0;
 }
예제 #2
0
 protected sealed override void FillBuffer()
 {
     if (_ofs == _valueSize)
     {
         Pos = -1;
         End = -1;
         return;
     }
     End   = _stream.Read(Buf.AsSpan(), _ofs);
     _ofs += (ulong)End;
     Pos   = 0;
 }
예제 #3
0
 public void Init(ref SpanReader spanReader)
 {
     if (_usedLen == 0)
     {
         var read = _stream.Read(_buf, _ofs);
         spanReader.Buf = _buf.AsSpan(0, read);
         _usedOfs       = 0;
         _usedLen       = (uint)read;
         _ofs          += (uint)read;
         return;
     }
     spanReader.Buf = _buf.AsSpan((int)_usedOfs, (int)_usedLen);
 }
        public int Read(Span <byte> data, ulong pos)
        {
            int res = _positionLessStream.Read(data, pos);

            Log("read size:{2}/{0} pos:{1}", data.Length, pos, res);
            int i  = 0;
            int j  = 0;
            var sb = new StringBuilder(8 + 16 * 3);

            while (i < res)
            {
                if (j == 16)
                {
                    Log(sb.ToString());
                    sb.Length = 0;
                    j         = 0;
                }
                if (j == 0)
                {
                    sb.AppendFormat("{0:X8}", pos + (uint)i);
                }
                sb.AppendFormat(" {0:X2}", data[i]);
                j++;
                i++;
            }
            if (j > 0)
            {
                Log(sb.ToString());
            }
            return(res);
        }
예제 #5
0
        public int Read(byte[] data, int offset, int size, ulong pos)
        {
            int res = _positionLessStream.Read(data, offset, size, pos);

            Log("read size:{2}/{0} pos:{1} datalen:{3} dataofs:{4}", size, pos, res, data.Length, offset);
            int i  = 0;
            int j  = 0;
            var sb = new StringBuilder(8 + 16 * 3);

            while (i < res)
            {
                if (j == 16)
                {
                    Log(sb.ToString());
                    sb.Length = 0;
                    j         = 0;
                }
                if (j == 0)
                {
                    sb.AppendFormat("{0:X8}", pos + (uint)i);
                }
                sb.AppendFormat(" {0:X2}", data[offset + i]);
                j++;
                i++;
            }
            if (j > 0)
            {
                Log(sb.ToString());
            }
            return(res);
        }
예제 #6
0
 public bool Open(IPositionLessStream positionLessStream, bool dispose)
 {
     lock (_log)
     {
         _log.WriteUInt8((byte)KVReplayOperation.Open);
         ulong size = positionLessStream.GetSize();
         _log.WriteVUInt64(size);
         ulong pos = 0;
         var   buf = new byte[4096];
         while (pos < size)
         {
             var read = positionLessStream.Read(buf, 0, buf.Length, pos);
             // Next 2 conditions should not happen or file is mutated when it should not
             if (read == 0)
             {
                 break;
             }
             if ((ulong)read > size - pos)
             {
                 read = (int)(size - pos);
             }
             _log.WriteBlock(buf, 0, read);
             pos += (ulong)read;
         }
         while (pos < size)
         {
             _log.WriteUInt8(0);
             pos++;
         }
         _log.FlushBuffer();
     }
     return(_db.Open(positionLessStream, dispose));
 }
예제 #7
0
 public bool Open(IPositionLessStream positionLessStream, bool dispose)
 {
     lock (_log)
     {
         _log.WriteUInt8((byte)KVReplayOperation.Open);
         ulong size = positionLessStream.GetSize();
         _log.WriteVUInt64(size);
         ulong pos = 0;
         var buf = new byte[4096];
         while (pos < size)
         {
             var read = positionLessStream.Read(buf, 0, buf.Length, pos);
             // Next 2 conditions should not happen or file is mutated when it should not
             if (read == 0) break;
             if ((ulong)read > size - pos) read = (int)(size - pos);
             _log.WriteBlock(buf, 0, read);
             pos += (ulong)read;
         }
         while (pos < size)
         {
             _log.WriteUInt8(0);
             pos++;
         }
         _log.FlushBuffer();
     }
     return _db.Open(positionLessStream, dispose);
 }
예제 #8
0
파일: KeyValueDB.cs 프로젝트: quyenbc/BTDB
 public bool Open(IPositionLessStream positionLessStream, bool dispose)
 {
     if (positionLessStream == null) throw new ArgumentNullException("positionLessStream");
     _positionLessStream = positionLessStream;
     _disposeStream = dispose;
     _spaceSoonReusable = null;
     _freeSpaceAllocatorOptimizer.GlobalInvalidate();
     _wasAnyCommits = false;
     bool newDB = false;
     if (positionLessStream.GetSize() == 0)
     {
         InitEmptyDB();
         newDB = true;
     }
     else
     {
         if (_positionLessStream.Read(_headerData, 0, TotalHeaderSize, 0) != TotalHeaderSize)
         {
             throw new BTDBException("Too short header");
         }
         _totalBytesRead += TotalHeaderSize;
         if (_headerData[0] != (byte)'B' || _headerData[1] != (byte)'T' || _headerData[2] != (byte)'D'
             || _headerData[3] != (byte)'B' || _headerData[4] != (byte)'1' || _headerData[5] != (byte)'0'
             || _headerData[6] != (byte)'0' || _headerData[7] != (byte)'2')
         {
             throw new BTDBException("Wrong header");
         }
     }
     _newState.Position = FirstRootOffset;
     _currentState.Position = SecondRootOffset;
     if (RetrieveStateFromHeaderBuffer(_newState))
     {
         if (RetrieveStateFromHeaderBuffer(_currentState))
         {
             if (_currentState.TransactionCounter != _newState.TransactionCounter)
             {
                 if (_currentState.TransactionCounter > _newState.TransactionCounter)
                 {
                     SwapCurrentAndNewState();
                 }
                 if (!CheckDB(_newState))
                 {
                     if (CheckDB(_currentState))
                     {
                         SwapCurrentAndNewState();
                     }
                     else
                     {
                         ThrowDatabaseCorrupted();
                     }
                 }
             }
         }
         else
         {
             if (!CheckDB(_newState))
             {
                 ThrowDatabaseCorrupted();
             }
         }
     }
     else
     {
         SwapCurrentAndNewState();
         if (RetrieveStateFromHeaderBuffer(_newState) == false)
         {
             throw new BTDBException("Both root headers corrupted");
         }
         if (!CheckDB(_newState))
         {
             ThrowDatabaseCorrupted();
         }
     }
     TransferNewStateToCurrentState();
     if (_currentState.TransactionAllocSize > 0)
     {
         throw new BTDBException("TransactionLog is not supported");
     }
     return newDB;
 }