예제 #1
0
        /// <summary>Writes the buffer to cache using pages</summary>
        /// <param name="buffer">source buffer</param>
        /// <param name="length">how many bytes to write</param>
        /// <exception cref="Db4objects.Db4o.Ext.Db4oIOException"></exception>
        public override void Write(byte[] buffer, int length)
        {
            ValidateReadOnly();
            long startAddress = _position;
            int  bytesToWrite = length;
            int  bufferOffset = 0;

            while (bytesToWrite > 0)
            {
                // page doesn't need to loadFromDisk if the whole page is dirty
                bool loadFromDisk         = (bytesToWrite < _pageSize) || (startAddress % _pageSize != 0);
                CachedIoAdapter.Page page = GetPage(startAddress, loadFromDisk);
                page.EnsureEndAddress(GetLength());
                int writtenBytes = page.Write(buffer, bufferOffset, startAddress, bytesToWrite);
                FlushIfHeaderBlockPage(page);
                MovePageToHead(page);
                bytesToWrite -= writtenBytes;
                startAddress += writtenBytes;
                bufferOffset += writtenBytes;
            }
            long endAddress = startAddress;

            _position   = endAddress;
            _fileLength = Math.Max(endAddress, _fileLength);
        }
예제 #2
0
 /// <exception cref="Db4objects.Db4o.Ext.Db4oIOException"></exception>
 private CachedIoAdapter.Page GetPage(long startAddress, bool loadFromDisk)
 {
     CachedIoAdapter.Page page = GetPageFromCache(startAddress);
     if (page != null)
     {
         if (ContainsHeaderBlock(page))
         {
             GetPageFromDisk(page, startAddress);
         }
         page.EnsureEndAddress(_fileLength);
         return(page);
     }
     // in case that page is not found in the cache
     page = GetFreePageFromCache();
     if (loadFromDisk)
     {
         GetPageFromDisk(page, startAddress);
     }
     else
     {
         ResetPageAddress(page, startAddress);
     }
     return(page);
 }