コード例 #1
0
        public async Task <bool> WriteNextAsync(OperationData operationData)
        {
            if (this.isDisposed)
            {
                throw new FabricObjectClosedException();
            }

            if (operationData == null)
            {
                throw new ArgumentNullException("operationData");
            }

            if (operationData.Count != 1)
            {
                throw new ArgumentException(string.Format("Unexpected operationData count: {0}", operationData.Count));
            }

            ArraySegment <byte> dataSegment;

            using (var enumerator = operationData.GetEnumerator())
            {
                bool res = enumerator.MoveNext();
                if (!res)
                {
                    throw new InvalidDataException("operationData enumeration is empty");
                }

                dataSegment = enumerator.Current;
            }

            if (dataSegment.Count <= 0)
            {
                throw new ArgumentException("Empty operationData data segment.");
            }

            // the first chunk should contain only metadata (the version), so at least one chunk will follow
            if (this.isFirstChunk)
            {
                this.isFirstChunk = false;
                await this.fileStream.WriteAsync(dataSegment.Array, dataSegment.Offset, dataSegment.Count).ConfigureAwait(false);

                return(false);
            }

            // subsequent chunks should be checkpoint frames
            return(await CheckpointFileHelper.WriteFrameAsync(dataSegment, this.fileStream, this.traceType).ConfigureAwait(false));

            // todo: trace
        }
コード例 #2
0
        public MetadataOperationData(OperationData operationData)
        {
            if (operationData == null)
            {
                throw new ArgumentNullException("operationData");
            }

            if (operationData.Count != 1)
            {
                throw new InvalidDataException(string.Format("Unexpected operationData count: {0}", operationData.Count));
            }

            ArraySegment <byte> dataSegment;

            using (var enumerator = operationData.GetEnumerator())
            {
                bool res = enumerator.MoveNext();
                if (!res)
                {
                    throw new InvalidDataException("operationData enumeration is empty");
                }

                dataSegment = enumerator.Current;
            }

            if (dataSegment.Count <= 0)
            {
                throw new InvalidDataException("Empty operationData data segment.");
            }

            // Array segment zero contains the metadata
            var metadataBytes = operationData[0];

            if (metadataBytes.Count < SerializedMetadataSize)
            {
                throw new InvalidDataException(string.Format("Unexpected metadata length: {0}", metadataBytes.Count));
            }

            using (var stream = new MemoryStream(metadataBytes.Array, metadataBytes.Offset, metadataBytes.Count))
                using (var reader = new BinaryReader(stream, Encoding.UTF8))
                {
                    // Read version.
                    var versionRead = reader.ReadInt16();

                    // Check version.
                    if (versionRead != Constants.SerializedVersion)
                    {
                        throw new InvalidDataException(
                                  string.Format("Version mismatch.  Expected version {0}, but read version {1}", Constants.SerializedVersion, versionRead));
                    }

                    // Read operation type.
                    var operationType = (OperationType)reader.ReadByte();
                    if (operationType == OperationType.Invalid)
                    {
                        throw new InvalidDataException("Invalid operationType.");
                    }

                    // Read item id.
                    var itemId = reader.ReadInt64();

                    this.Version       = versionRead;
                    this.OperationType = operationType;
                    this.ItemId        = itemId;
                }
        }