示例#1
0
        private async Task <List <IListElement <T> > > ReadListElementsAsync()
        {
            try
            {
                using (var stream = FabricFile.Open(this.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan))
                {
                    var intSegment = new ArraySegment <byte>(new byte[sizeof(int)]);

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

                    if (metadataMarker != CheckpointFileHelper.MetadataMarker)
                    {
                        throw new InvalidDataException(
                                  string.Format(
                                      "Stream does not begin with metadata marker.  Expected: {0} Actual: {1}",
                                      CheckpointFileHelper.MetadataMarker,
                                      metadataMarker));
                    }

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

                    if (versionRead != FileVersion)
                    {
                        throw new InvalidDataException(
                                  string.Format("Unsupported ReliableConcurrentQueue checkpoint version.  Expected: {0}  Actual: {1}", FileVersion, versionRead));
                    }

                    var listElements = new List <IListElement <T> >();
                    while (true)
                    {
                        var marker = await SerializationHelper.ReadIntAsync(intSegment, stream).ConfigureAwait(false);

                        if (marker == CheckpointFileHelper.BeginFrameMarker)
                        {
                            var chunkListElements = await CheckpointFrame <T> .ReadAsync(stream, this.valueSerializer, this.traceType).ConfigureAwait(false);

                            listElements.AddRange(chunkListElements);
                        }
                        else if (marker == CheckpointFileHelper.EndFramesMarker)
                        {
                            return(listElements);
                        }
                        else
                        {
                            throw new InvalidDataException(string.Format("marker: {0}", marker));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                FabricEvents.Events.ReliableConcurrentQueue_ReadCheckpointError("ReliableConcurrentQueueCheckpointFile.ReadCheckpointFileAsync@", ex.ToString());
                throw;
            }
        }
示例#2
0
        public async Task WriteAsync()
        {
            if (this.ListElements == null)
            {
                TestableAssertHelper.FailArgumentNull(this.traceType, "Checkpoint.WriteAsync", "this.ListElements == null");
            }
            if (this.valueSerializer == null)
            {
                TestableAssertHelper.FailArgumentNull(this.traceType, "Checkpoint.WriteAsync", "this.valueSerializer == null");
            }

            using (var stream = FabricFile.Open(this.FilePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.SequentialScan))
            {
                await
                stream.WriteAsync(
                    CheckpointFileHelper.MetadataMarkerSegment.Array,
                    CheckpointFileHelper.MetadataMarkerSegment.Offset,
                    CheckpointFileHelper.MetadataMarkerSegment.Count).ConfigureAwait(false);

                var versionBytes = BitConverter.GetBytes(FileVersion);
                await stream.WriteAsync(versionBytes, 0, versionBytes.Length).ConfigureAwait(false);

                var frame = new CheckpointFrame <T>(this.valueSerializer);
                foreach (var listElement in this.ListElements)
                {
                    if (frame.Size >= CheckpointFileHelper.ChunkWriteThreshold)
                    {
                        await
                        stream.WriteAsync(
                            CheckpointFileHelper.BeginChunkMarkerSegment.Array,
                            CheckpointFileHelper.BeginChunkMarkerSegment.Offset,
                            CheckpointFileHelper.BeginChunkMarkerSegment.Count).ConfigureAwait(false);

                        await frame.WriteAsync(stream).ConfigureAwait(false);

                        frame.Reset();
                    }

                    frame.AddListElement(listElement);
                }

                if (frame.ListElementsCount > 0)
                {
                    await
                    stream.WriteAsync(
                        CheckpointFileHelper.BeginChunkMarkerSegment.Array,
                        CheckpointFileHelper.BeginChunkMarkerSegment.Offset,
                        CheckpointFileHelper.BeginChunkMarkerSegment.Count).ConfigureAwait(false);

                    await frame.WriteAsync(stream).ConfigureAwait(false);

                    frame.Reset();
                }

                await
                stream.WriteAsync(
                    CheckpointFileHelper.EndFramesMarkerSegment.Array,
                    CheckpointFileHelper.EndFramesMarkerSegment.Offset,
                    CheckpointFileHelper.EndFramesMarkerSegment.Count).ConfigureAwait(false);
            }
        }