/// <summary>
        /// Gets concentrated frames from the provided stream
        /// </summary>
        /// <param name="stream">the database to use</param>
        /// <returns></returns>
        public static SortedList <DateTime, FrameData> GetFrames(this TreeStream <HistorianKey, HistorianValue> stream)
        {
            SortedList <DateTime, FrameDataConstructor> results = new SortedList <DateTime, FrameDataConstructor>();
            ulong lastTime = ulong.MinValue;
            FrameDataConstructor lastFrame = null;
            HistorianKey         key       = new HistorianKey();
            HistorianValue       value     = new HistorianValue();

            while (stream.Read(key, value))
            {
                if (lastFrame == null || key.Timestamp != lastTime)
                {
                    lastTime = key.Timestamp;
                    DateTime timestamp = new DateTime((long)lastTime);

                    if (!results.TryGetValue(timestamp, out lastFrame))
                    {
                        lastFrame = new FrameDataConstructor();
                        results.Add(timestamp, lastFrame);
                    }
                }
                lastFrame.PointId.Add(key.PointID);
                lastFrame.Values.Add(value.ToStruct());
            }
            List <FrameData> data = new List <FrameData>(results.Count);

            data.AddRange(results.Values.Select(x => x.ToFrameData()));
            return(SortedListConstructor.Create(results.Keys, data));
        }
        public void Test1()
        {
            MemoryPoolTest.TestMemoryLeak();
            DebugStopwatch sw = new DebugStopwatch();

            for (int max = 10; max < 10000; max *= 2)
            {
                Action add1 = () =>
                {
                    SortedList <int, int> list = new SortedList <int, int>();
                    for (int x = 0; x < max; x++)
                    {
                        list.Add(x, x);
                    }
                };

                Action add2 = () =>
                {
                    List <int> keys   = new List <int>(max);
                    List <int> values = new List <int>(max);

                    for (int x = 0; x < max; x++)
                    {
                        keys.Add(x);
                        values.Add(x);
                    }

                    var sl = SortedListConstructor.Create(keys, values);
                };

                //var makeList = new SortedListConstructorUnsafe<int, int>();
                //Action add3 = () =>
                //{
                //    List<int> keys = new List<int>(max);
                //    List<int> values = new List<int>(max);

                //    for (int x = 0; x < max; x++)
                //    {
                //        keys.Add(x);
                //        values.Add(x);
                //    }

                //    var sl = makeList.Create(keys, values);
                //    //var sl = SortedListConstructor.CreateUnsafe(keys, values);

                //};
                System.Console.WriteLine("Old Method " + max + " " + sw.TimeEvent(add1) * 1000000);
                System.Console.WriteLine("New Method " + max + " " + sw.TimeEvent(add2) * 1000000);
                //Console.WriteLine("Unsafe Method " + max + " " + sw.TimeEvent(add3) * 1000000);
                MemoryPoolTest.TestMemoryLeak();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Rounds the frame to the nearest level of specified tolerance.
        /// </summary>
        /// <param name="origional">the frame to round</param>
        /// <param name="tolerance">the timespan to round on.</param>
        /// <returns>A new frame that is rounded.</returns>
        public static SortedList <DateTime, FrameData> RoundToTolerance(this SortedList <DateTime, FrameData> origional, TimeSpan tolerance)
        {
            SortedList <DateTime, FrameData> results = new SortedList <DateTime, FrameData>();

            SortedList <DateTime, List <FrameData> > buckets = new SortedList <DateTime, List <FrameData> >();

            foreach (var items in origional)
            {
                DateTime         roundedDate = items.Key.Round(tolerance);
                List <FrameData> frames;
                if (!buckets.TryGetValue(roundedDate, out frames))
                {
                    frames = new List <FrameData>();
                    buckets.Add(roundedDate, frames);
                }
                frames.Add(items.Value);
            }

            foreach (var bucket in buckets)
            {
                if (bucket.Value.Count == 1)
                {
                    results.Add(bucket.Key, bucket.Value[0]);
                }
                else
                {
                    int          count = bucket.Value.Sum(x => x.Points.Count);
                    List <ulong> keys  = new List <ulong>(count);
                    List <HistorianValueStruct> values = new List <HistorianValueStruct>(count);

                    FrameData tempFrame = new FrameData();
                    tempFrame.Points = new SortedList <ulong, HistorianValueStruct>();

                    var allFrames = new List <EnumerableHelper>();

                    foreach (var frame in bucket.Value)
                    {
                        allFrames.Add(new EnumerableHelper(frame));
                    }

                    while (true)
                    {
                        EnumerableHelper lowestKey = null;

                        foreach (var item in allFrames)
                        {
                            lowestKey = Min(lowestKey, item);
                        }

                        if (lowestKey == null)
                        {
                            break;
                        }

                        keys.Add(lowestKey.PointId);
                        values.Add(lowestKey.Value);

                        //tempFrame.Points.Add(lowestKey.PointId, lowestKey.Value);
                        lowestKey.Read();
                    }
                    tempFrame.Points = SortedListConstructor.Create(keys, values);
                    results.Add(bucket.Key, tempFrame);
                }
            }
            return(results);
        }
 public FrameData(List <ulong> pointId, List <HistorianValueStruct> values)
 {
     Points = SortedListConstructor.Create(pointId, values);
 }