Example #1
0
        internal static OperationData WriteRecord(
            LogRecord record,
            BinaryWriter bw,
            bool isPhysicalWrite       = true,
            bool setRecordLength       = true,
            bool forceRecomputeOffsets = false)
        {
            // NOTE:- The binary writer is not where the real data is written
            // It is only passed in to avoid each log record from creating its own writer

            // The real data of the log record is returned in the operation data
            // As a result, reset the position of the writer before starting to write

            bw.BaseStream.Position = 0;

            var operationData = new OperationData();

            record.Write(bw, operationData, isPhysicalWrite, forceRecomputeOffsets);
            uint recordLength = 0;

            foreach (var data in operationData)
            {
                recordLength += (uint)data.Count;
            }

            if (setRecordLength)
            {
                record.RecordLength = recordLength;
            }

            var mm = bw.BaseStream as MemoryStream;
            var startingPosition = bw.BaseStream.Position;

            bw.Write(recordLength);
            var arraySegment = new ArraySegment <byte>(
                mm.GetBuffer(),
                (int)startingPosition,
                (int)mm.Position - (int)startingPosition);

            // Append and prepend record length in a fixed encoding of sizeof(int) bytes
            operationData.Add(arraySegment);
            operationData.Insert(0, arraySegment);

            return(operationData);
        }