Beispiel #1
0
        /// <summary>
        /// Deserialzies the FileMetadata from stream.
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="tracer"></param>
        /// <returns></returns>
        /// <remarks>
        /// 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 static FileMetadata Read(InMemoryBinaryReader reader, string tracer)
        {
            Utility.Assert(reader.IsAligned(), "must be aligned");

            var totalNumberOfEntries   = reader.ReadInt64();
            var numberOfValidEntries   = reader.ReadInt64();
            var numberOfDeletedEntries = reader.ReadInt64();
            var timeStamp = reader.ReadInt64();

            var fileId = reader.ReadUInt32();

            reader.ReadPaddingUntilAligned(true);

            var fileName = reader.ReadString();

            reader.ReadPaddingUntilAligned(false);

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

            return(new FileMetadata(tracer, fileId,
                                    fileName, totalNumberOfEntries,
                                    numberOfValidEntries, timeStamp,
                                    numberOfDeletedEntries, false,
                                    FileMetadata.InvalidTimeStamp,
                                    FileMetadata.InvalidTimeStamp));
        }
        private static async Task <ConditionalValue <CheckpointManager <T> > > TryReadCheckpointFile(
            string directory,
            string fileName,
            IStateSerializer <T> valueSerializer,
            string traceType)
        {
            var filePath = Path.Combine(directory, fileName);

            if (!FabricFile.Exists(filePath))
            {
                return(new ConditionalValue <CheckpointManager <T> >(false, default(CheckpointManager <T>)));
            }

            using (var stream = FabricFile.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan))
            {
                var intSegment = new ArraySegment <byte>(new byte[sizeof(int)]);

                var versionRead = await SerializationHelper.ReadIntAsync(intSegment, stream).ConfigureAwait(false);

                if (versionRead != FileVersion)
                {
                    throw new InvalidDataException(string.Format("versionRead '{0}' != FileVersion '{1}'", versionRead, FileVersion));
                }

                var nameLength = await SerializationHelper.ReadIntAsync(intSegment, stream).ConfigureAwait(false);

                if (nameLength < 0)
                {
                    throw new InvalidDataException(string.Format("nameLength '{0}' < 0", nameLength));
                }

                if (nameLength == 0)
                {
                    return(new ConditionalValue <CheckpointManager <T> >(true, new CheckpointManager <T>(null, directory, fileName, valueSerializer, traceType)));
                }

                var nameSegment = new ArraySegment <byte>(new byte[nameLength]);
                await SerializationHelper.ReadBytesAsync(nameSegment, nameLength, stream).ConfigureAwait(false);

                string name;

                using (var reader = new InMemoryBinaryReader(new MemoryStream(nameSegment.Array)))
                {
                    name = reader.ReadString();
                }

                var path = Path.Combine(directory, name);
                if (!FabricFile.Exists(path))
                {
                    throw new InvalidDataException(string.Format("Current checkpoint file does not exist: {0}", path));
                }

                return(new ConditionalValue <CheckpointManager <T> >(
                           true,
                           new CheckpointManager <T>(new Checkpoint <T>(directory, name, valueSerializer, traceType), directory, fileName, valueSerializer, traceType)));
            }
        }