nyurik / timeseriesdb

Automatically exported from code.google.com/p/timeseriesdb
GNU General Public License v3.0
13 stars 6 forks source link

Ticks read back as null after writing to compressed file #16

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

I'm having an issue where I'm writing ticks of a compressed file and when 
reading them back, the values are null.

Any ideas why this could be happening?

Thanks
Georgios

Original issue reported on code.google.com by georgi...@gmail.com on 23 Oct 2014 at 5:09

GoogleCodeExporter commented 9 years ago
Tick def: class { DateTime, decimal, decimal } - I pass the DateTime FieldInfo 
to the ctor to use as index!

Original comment by georgi...@gmail.com on 23 Oct 2014 at 5:49

GoogleCodeExporter commented 9 years ago
I also tried copying your UtcDateTime struct and using that but still no luck :S

PS: *Some* of the values are null, not all

Original comment by georgi...@gmail.com on 23 Oct 2014 at 6:37

GoogleCodeExporter commented 9 years ago
Thx for reporting, could you submit a simple unit test that shows the error?

Original comment by yastrak...@wikimedia.org on 23 Oct 2014 at 7:00

GoogleCodeExporter commented 9 years ago
Thank God you're still watching the project :)

test.txt: https://www.dropbox.com/s/l5g3f6orjos1jv4/data.txt?dl=0

Code:

class Program { 
public static void Main() {
const string test = @"c:\users\georgios\desktop\test";
            string fn = Path.Combine(test, "foo.bts");

            var flatWrittenTicks = File.ReadAllLines(Path.Combine(test, "data.txt"))
                .Select(s => s.Split(' '))
                .Select(s => new Tick(DateTime.Parse(s[0], null, DateTimeStyles.AssumeUniversal).ToUniversalTime(), decimal.Parse(s[1]), decimal.Parse(s[2])))
                .ToArray();

            File.Delete(fn);
            using (var file = new BinCompressedSeriesFile<UtcDateTime, Tick>(fn, typeof (Tick).GetField("DateTime")))
            {
                file.UniqueIndexes = true;
                file.InitializeNewFile();
                file.AppendData(new[] { new ArraySegment<Tick>(flatWrittenTicks), });
            }

            using (var file = (BinCompressedSeriesFile<UtcDateTime, Tick>) BinaryFile.Open(fn))
            {
                file.ValidateOnRead = true;

                var segments = file.StreamSegments().ToArray();
                var flatReadTicks = segments.SelectMany(it => it).ToArray();

                Console.WriteLine("Count: {0} written vs {1} read", flatWrittenTicks.Length, flatReadTicks.Length);
                for (int i = 0; i < flatWrittenTicks.Length; ++i)
                    Console.WriteLine("\t{2:0000}: {0} vs {1}", flatWrittenTicks[i], flatReadTicks[i], i);

                Trace.Assert(flatWrittenTicks.SequenceEqual(flatReadTicks));
            }
}
}

public class Tick : IEquatable<Tick>
    {
        public UtcDateTime DateTime;

        public decimal Ask;
        public decimal Bid;

        #region Ctors
        public Tick()
        {
        }

        public Tick(
            UtcDateTime dateTime,
            decimal ask,
            decimal bid)
        {
            DateTime = dateTime;
            Ask = ask;
            Bid = bid;
        }

        public Tick(O2GOfferRow offerRow)
        {
            DateTime = (DateTime)offerRow.Time;
            Ask = (decimal)offerRow.Ask;
            Bid = (decimal)offerRow.Bid;
        }
        #endregion

        #region Equals/ToString
        public bool Equals(Tick other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return DateTime.Equals(other.DateTime) && Bid == other.Bid && Ask == other.Ask;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((Tick)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                var hashCode = DateTime.GetHashCode();
                hashCode = (hashCode * 397) ^ Bid.GetHashCode();
                hashCode = (hashCode * 397) ^ Ask.GetHashCode();
                return hashCode;
            }
        }

        public override string ToString()
        {
            return string.Format("{0:o} {1} / {2}", DateTime, Ask, Bid);
        }
        #endregion
    }

Original comment by georgi...@gmail.com on 23 Oct 2014 at 8:29

GoogleCodeExporter commented 9 years ago
Any luck fixing this? I've got my hands tied - tried to debug it myself but got 
a bit lost...

Original comment by georgi...@gmail.com on 29 Oct 2014 at 2:16

GoogleCodeExporter commented 9 years ago
Hi Georgios, i'm traveling until next week, and my C# dev env is on a different 
computer. I will look at it as soon as i get back.

Original comment by yastrak...@wikimedia.org on 29 Oct 2014 at 4:00