Exemplo n.º 1
0
 public CheckPoint SaveChunk(uint startBlock, uint endBlock)
 {
     lock (LoadLock)
     {
         var list   = new List <CheckPointBlock>();
         var header = new CheckPointHeader
         {
             StartBlock = startBlock,
             EndBlock   = endBlock,
             BlockCount = endBlock - startBlock + 1
         };
         for (var i = startBlock; i <= endBlock; i++)
         {
             list.Add(this[i]);
         }
         var ms = new MemoryStream();
         header.Offsets = new uint[list.Count + 1];
         header.SaveToStream(ms);
         long headerSize = ms.Position;
         ms.Position = 0;
         using (var bw = new BinaryWriter(ms, Encoding.Default, true))
         {
             uint i = 0;
             foreach (var b in list)
             {
                 header.Offsets[i] = (uint)(ms.Position + headerSize);
                 b.SaveToStream(ms);
                 i++;
             }
         }
         var memoryStream = new MemoryStream();
         header.SaveToStream(memoryStream);
         ms.Position = 0;
         ms.CopyTo(memoryStream);
         memoryStream.Write(header.Hash, 0, header.Hash.Length);
         if (_stream != null)
         {
             _stream.Dispose();
             _stream = null;
         }
         memoryStream.Position = 0;
         var checkPoint = new CheckPoint(memoryStream);
         ms.Dispose();
         memoryStream.Dispose();
         GC.Collect();
         return(checkPoint);
     }
 }
Exemplo n.º 2
0
        public void Append(CheckPoint checkPoint)
        {
            lock (LoadLock)
            {
                var list = new List <CheckPointBlock>();
                if (Header != null)
                {
                    for (uint i = Header.StartBlock; i < Header.EndBlock + 1; i++)
                    {
                        list.Add(this[i]);
                    }
                    if (Header.BlockCount > checkPoint.Header.BlockCount)
                    {
                        return;
                    }
                    Header.BlockCount = checkPoint.Header.BlockCount;
                    Header.Hash       = checkPoint.Header.Hash;
                    Header.EndBlock   = checkPoint.Header.EndBlock;
                }
                else
                {
                    Header = checkPoint.Header;
                }
                for (uint i = 0; i != checkPoint.Header.EndBlock - checkPoint.Header.StartBlock + 1; i++)
                {
//		    log.Info($"Added block {i}");
                    list.Add(checkPoint[i]);
                }
                var ms = new MemoryStream();
                Header.Offsets = new uint[list.Count + 1];
                Header.SaveToStream(ms);
                long headerSize = ms.Position;
                ms.Position = 0;
//		log.Info("checkPoint 1");
                using (var bw = new BinaryWriter(ms, Encoding.Default, true))
                {
                    uint i = 0;
                    foreach (var b in list)
                    {
                        Header.Offsets[i] = (uint)(ms.Position + headerSize);
                        b.SaveToStream(ms);
                        i++;
                    }
                }
//		log.Info("checkPoint 2");
                var memoryStream = new MemoryStream();
                Header.SaveToStream(memoryStream);
                ms.Position = 0;
                ms.CopyTo(memoryStream);
                memoryStream.Write(Header.Hash, 0, Header.Hash.Length);
                if (_stream != null)
                {
                    _stream.Dispose();
                    _stream = null;
                }
                memoryStream.Position = 0;
//		log.Info("checkPoint 3");
                LoadFromStream(memoryStream);
//		log.Info("checkPoint 4");
                ms.Dispose();
                memoryStream.Dispose();
                GC.Collect();
//		log.Info("checkPoint appended");
            }
        }