Esempio n. 1
0
        /// <summary>
        /// Serializes the FileMetadata into an in-memory binary writer.
        /// </summary>
        /// <param name="writer"></param>
        /// <remarks>
        /// This serialization is 8 byte aligned.
        ///
        /// Name                    || Size
        ///
        /// TotalNumberOfEntries       8
        /// NumberOfValidEntries       8
        /// NumberOfDeletedEntries     8
        /// TimeStamp                  8
        ///
        /// FileId                     4
        /// CanBeDeleted               1
        /// RESERVED                   3
        ///
        /// FileName                   N
        /// PADDING                    (N % 8 ==0) ? 0 : 8 - (N % 8)
        ///
        /// RESERVED: Fixed padding that is usable to add fields in future.
        /// PADDING:  Due to dynamic size, cannot be used for adding fields.
        ///
        /// </remarks>
        public void Write(InMemoryBinaryWriter writer)
        {
            Utility.Assert(writer.IsAligned(), "must be aligned");

            // Serialize the metadata.
            writer.Write(this.TotalNumberOfEntries);
            writer.Write(this.NumberOfValidEntries);
            writer.Write(this.NumberOfDeletedEntries);
            writer.Write(this.TimeStamp);

            // With RESERVE
            writer.Write(this.FileId);
            writer.WritePaddingUntilAligned();

            // With PADDING
            writer.Write(this.FileName);
            writer.WritePaddingUntilAligned();

#if !DotNetCoreClr
            FabricEvents.Events.CheckpointFileMetadata(this.tracer, DifferentialStoreConstants.FileMetadata_Write, FileName, FileId,
                                                       TotalNumberOfEntries, NumberOfDeletedEntries, NumberOfInvalidEntries);
#endif

            Utility.Assert(writer.IsAligned(), "must be aligned");
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="memoryBuffer"></param>
        /// <param name="item"></param>
        /// <param name="keySerializer"></param>
        /// <param name="timeStamp"></param>
        /// <remarks>
        /// The data is written is 8 bytes aligned.
        ///
        /// Name                    Type        Size
        ///
        /// KeySize                 int         4
        /// Kind                    byte        1
        /// RESERVED                            3
        /// VersionSequenceNumber   long        8
        ///
        /// (DeletedVersion)
        /// timeStamp               long        8
        ///
        /// (Inserted || Updated)
        /// Offset                  long        8
        /// ValueChecksum           ulong       8
        /// ValueSize               int         4
        /// RESERVED                            4
        ///
        /// Key                     TKey        N
        /// PADDING                             (N % 8 ==0) ? 0 : 8 - (N % 8)
        ///
        /// RESERVED: Fixed padding that is usable to add fields in future.
        /// PADDING:  Due to dynamic size, cannot be used for adding fields.
        ///
        /// Note: Larges Key size supported is int.MaxValue in bytes.
        /// </remarks>
        private void WriteKey <TKey, TValue>(
            InMemoryBinaryWriter memoryBuffer, KeyValuePair <TKey, TVersionedItem <TValue> > item, IStateSerializer <TKey> keySerializer, long timeStamp)
        {
            Utility.Assert(memoryBuffer.IsAligned(), "must be aligned");
            var recordPosition = memoryBuffer.BaseStream.Position;

            memoryBuffer.BaseStream.Position += sizeof(int);
            memoryBuffer.Write((byte)item.Value.Kind);            // RecordKind
            memoryBuffer.WritePaddingUntilAligned();              // RESERVED

            memoryBuffer.Write(item.Value.VersionSequenceNumber); // LSN

            if (item.Value.Kind == RecordKind.DeletedVersion)
            {
                memoryBuffer.Write(timeStamp);
            }
            else
            {
                // Deleted items don't have a value.  We only serialize value properties for non-deleted items.
                memoryBuffer.Write(item.Value.Offset);        // value offset
                memoryBuffer.Write(item.Value.ValueChecksum); // value checksum
                memoryBuffer.Write(item.Value.ValueSize);     // value size
                memoryBuffer.WritePaddingUntilAligned();      // RESERVED
            }

            var keyPosition = memoryBuffer.BaseStream.Position;

            keySerializer.Write(item.Key, memoryBuffer);
            var keyEndPosition = memoryBuffer.BaseStream.Position;

            Diagnostics.Assert(
                keyEndPosition >= keyPosition,
                DifferentialStoreConstants.TraceType,
                "User's key IStateSerializer moved the stream position backwards unexpectedly!");

            memoryBuffer.BaseStream.Position = recordPosition;
            memoryBuffer.Write(checked ((int)(keyEndPosition - keyPosition)));
            memoryBuffer.BaseStream.Position = keyEndPosition;

            memoryBuffer.WritePaddingUntilAligned();
            Utility.Assert(memoryBuffer.IsAligned(), "must be aligned");

            // Update in-memory metadata.
            this.Properties.KeyCount++;
        }