예제 #1
0
파일: Store.cs 프로젝트: vdedyukhin/psi
        /// <summary>
        /// Deserializes data from a stream of Message{BufferReader}.
        /// </summary>
        /// <typeparam name="T">The type of data expected after deserialization</typeparam>
        /// <param name="source">The stream containing the serialized data</param>
        /// <param name="serializers">An optional collection of known types to use</param>
        /// <param name="reusableInstance">An optional preallocated instance ot use as a buffer. This parameter is required when deserializing <see cref="Shared{T}"/> instances if the deserializer is expected to use a <see cref="SharedPool{T}"/></param>
        /// <param name="deliveryPolicy">An optional delivery policy.</param>
        /// <returns>A stream of messages of type T, with their original envelope</returns>
        public static IProducer <T> Deserialize <T>(this IProducer <Message <BufferReader> > source, KnownSerializers serializers = null, T reusableInstance = default(T), DeliveryPolicy deliveryPolicy = null)
        {
            var deserializer = new DeserializerComponent <T>(source.Out.Pipeline, serializers ?? KnownSerializers.Default, reusableInstance);

            source.PipeTo(deserializer, deliveryPolicy);
            return(deserializer.Out);
        }
예제 #2
0
        /// <summary>
        /// Opens the specified storage stream for reading and returns a stream instance that can be used to consume the messages.
        /// The returned stream will publish data read from the store once the pipeline is running.
        /// </summary>
        /// <typeparam name="T">The expected type of the storage stream to open.
        /// This type will be used to deserialize the stream messages.</typeparam>
        /// <param name="streamName">The name of the storage stream to open</param>
        /// <param name="reusableInstance">An optional instance to reuse (as a buffer) when deserializing the data</param>
        /// <returns>A stream that publishes the data read from the store.</returns>
        public IProducer <T> OpenStream <T>(string streamName, T reusableInstance = default(T))
        {
            if (this.streams.TryGetValue(streamName, out object stream))
            {
                return((IProducer <T>)stream); // if the types don't match, invalid cast exception is the appropriate error
            }

            var meta = this.reader.OpenStream(streamName);

            // register this stream with the store catalog
            this.pipeline.ConfigurationStore.Set(Store.StreamMetadataNamespace, streamName, meta);

            // create the deserialization sub-pipeline (and validate that we can deserialize this stream)
            var splitterOut  = this.splitter.Add(meta.Id);
            var deserializer = new DeserializerComponent <T>(this.pipeline, this.serializers, reusableInstance);

            deserializer.Out.Name = streamName;
            splitterOut.PipeTo(deserializer, DeliveryPolicy.Immediate);
            this.streams[streamName] = deserializer.Out;
            return(deserializer.Out);
        }