/// <summary>
        /// Write aggregated data to a provided stream.
        /// </summary>
        /// <param name="destination">Destination stream.</param>
        /// <returns>True if data was successfully serialized.</returns>
        public bool WriteToStream(Stream destination)
        {
            if (this.data == null || (this.data.Count == 0 && !this.data.HasUnmergedData))
            {
                return(false);
            }

            this.data.Merge();
            using (var writer = new PersistedDataWriter(destination, this.DimensionSet, this.memoryStreamManager))
            {
                writer.WriteData(this.Name, this.StartTime.ToUniversalTime(), this.EndTime.ToUniversalTime(),
                                 (uint)this.data.Count, this.Sources, this.data);
            }

            return(true);
        }
Exemplo n.º 2
0
        public bool Serialize(string name, Stream destination)
        {
            if (!this.Sealed)
            {
                return(false);
            }

            // If we already have data on the disk stream if from there instead of paying re-serialization cost.
            // We prefer to do this even if the data is actually loaded.
            if (this.Filename != null && !this.Dirty)
            {
                try
                {
                    var bytes = new byte[8192];
                    using (var fs = new FileStream(this.Filename, FileMode.Open, FileAccess.Read))
                    {
                        int readBytes;
                        while ((readBytes = fs.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            destination.Write(bytes, 0, readBytes);
                        }
                    }

                    return(true);
                }
                catch (FileNotFoundException)
                {
                    return(false);
                }
            }

            this.Pin();
            try
            {
                using (var writer = new PersistedDataWriter(destination, this.DimensionSet, this.memoryStreamManager))
                {
                    writer.WriteData(name, this.StartTime, this.EndTime, (uint)this.data.Count, this.sources.Values,
                                     this.data);
                }

                return(true);
            }
            finally
            {
                this.Unpin();
            }
        }
Exemplo n.º 3
0
        public Stream WriteToMemoryStream()
        {
            lock (this)
            {
                if (this.data == null)
                {
                    return(null);
                }
                var stream = new MemoryStream();

                using (var writer = new PersistedDataWriter(stream, this.DimensionSet, this.memoryStreamManager))
                {
                    writer.WriteData(string.Empty, this.StartTime, this.EndTime, (uint)this.data.Count,
                                     this.sources.Values, this.data);
                }
                stream.Flush();
                stream.Position = 0;
                return(stream);
            }
        }
Exemplo n.º 4
0
        private void WriteToFile()
        {
            lock (this)
            {
                if (this.Filename == null || !this.Dirty || this.data == null)
                {
                    return;
                }

                this.Flush();

                Events.Write.BeginWritingData(this.Filename);
                using (var stream = new FileStream(this.Filename, FileMode.Create, FileAccess.Write, FileShare.Read))
                    using (var writer = new PersistedDataWriter(stream, this.DimensionSet, this.memoryStreamManager))
                    {
                        writer.WriteData(string.Empty, this.StartTime, this.EndTime, (uint)this.data.Count,
                                         this.sources.Values, this.data);
                    }

                this.dirty      = false;
                this.data.Dirty = false;
                Events.Write.EndWritingData(this.Filename);
            }
        }