Example #1
0
        public void Commit()
        {
            lock (SyncRoot)
            {
                FreeOldVersions();

                using (MemoryStream ms = new MemoryStream())
                {
                    Serialize(new BinaryWriter(ms));

                    //if there is not enough space between the header and the last flush location, write after the last flush, else write immediately after the header.
                    bool writeNext = header.LastFlush != Ptr.NULL && ms.Length > header.LastFlush.Position - AtomicHeader.SIZE + 1;

                    long pos = writeNext ? header.LastFlush.PositionPlusSize : AtomicHeader.SIZE + 1;

                    System.Seek(pos, SeekOrigin.Begin);
                    System.Write(ms.GetBuffer(), 0, (int)ms.Length);

                    //atomic write
                    header.LastFlush = new Ptr(pos, ms.Length);
                    header.Serialize(System);
                }

                currentVersion++;
            }
        }
Example #2
0
        public void Commit()
        {
            lock (SyncRoot)
            {
                Stream.Flush();

                FreeOldVersions();

                using (MemoryStream ms = new MemoryStream())
                {
                    if (header.SystemData != Ptr.NULL)
                    {
                        space.Free(header.SystemData);
                    }

                    Serialize(new BinaryWriter(ms));

                    Ptr ptr = space.Alloc(ms.Length);
                    Stream.Seek(ptr.Position, SeekOrigin.Begin);
                    Stream.Write(ms.GetBuffer(), 0, (int)ms.Length);

                    header.SystemData = ptr;

                    //atomic write
                    header.Serialize(Stream);

                    if (ptr.PositionPlusSize > maxPositionPlusSize)
                    {
                        maxPositionPlusSize = ptr.PositionPlusSize;
                    }
                }

                Stream.Flush();

                //try to truncate the stream
                if (Stream.Length > maxPositionPlusSize)
                {
                    Stream.SetLength(maxPositionPlusSize);
                }

                currentVersion++;
            }
        }