DZone
Database Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Database Zone > Writing a Time Series Database With Voron

Writing a Time Series Database With Voron

Ever wonder what it would be like to develop a time-series database using Voron? The author of this post did. Read on for details.

Oren Eini user avatar by
Oren Eini
·
Apr. 30, 17 · Database Zone · Tutorial
Like (1)
Save
Tweet
2.70K Views

Join the DZone community and get the full member experience.

Join For Free

Following up on this post, I wondered what it would be like if I were to implement this with Voron. Given that Voron was explicitly designed to be a low-level storage engine suitable for varying needs, it is an interesting experiment.

Let's define upfront what we want to do:

public interface ITimeSeriesDatabase
{
   Task Append(BlittableJsonReaderObject key, DateTime time, double value);

   IEnumerable<double> Query(Dictionary<string, string> matches, DateTime from, DateTime to);
}

We use BlittableJsonReaderObject as the key in the add because initializing a dictionary per add call would be ridiculously expensive. The blittable instance is much cheaper, and its associated memory can be cleaned when it is done much more easily.

Here is how we handle the append:

public Task Append(BlittableJsonReaderObject key, DateTime time, double value)
{
    var entry = new Entry
    {
        Key = key,
        Time = time,
        Value = value
    };

    _entries.Enqueue(entry);
    _hasEntries.Set();

    return entry.Tcs.Task;
}

There isn’t much here, and that is quite intentional. What you are seeing here is transaction merging. Instead of having to compete on the same lock, we just place the value to be written on the queue and wait for it to complete. The other side of that is the transaction merging itself:

private unsafe void TransactionMerger()
{
    var existing = new HashSet<long>();
    var tasks = new List<TaskCompletionSource<object>>();
    while (_cts.IsCancellationRequested == false)
    {
        existing.Clear();
        tasks.Clear();
        double value;
        using (var tx = _storageEnvironment.WriteTransaction())
        using (Slice.From(tx.Allocator, "dummy-val", out Slice dummy))
        using (Slice.External(tx.Allocator, (byte*)&value, sizeof(double), out Slice valueSlice))
        {
            var fst = tx.FixedTreeFor(dummy, valSize: sizeof(double));

            int count = 0;
            Entry entry;
            var propertiesInsertionBuffer = new BlittableJsonReaderObject.PropertiesInsertionBuffer();
            while (_entries.TryDequeue(out entry))
            {

                var keyHash = (long)entry.Key.DebugHash;

                using (Slice.External(tx.Allocator, (byte*)&keyHash, sizeof(long), out Slice slice))
                {
                    fst.RepurposeInstance(slice, clone: false);

                    value = entry.Value;
                    fst.Add(entry.Time.Ticks, valueSlice);
                }
                if (existing.Add(keyHash))
                {
                    int propCount = entry.Key.GetPropertiesByInsertionOrder(propertiesInsertionBuffer);
                    var prop = new BlittableJsonReaderObject.PropertyDetails();

                    for (int i = 0; i < propCount; i++)
                    {
                        entry.Key.GetPropertyByIndex(propertiesInsertionBuffer.Properties[i], ref prop);
                        var tree = tx.CreateTree(prop.Name);
                        var matches = tree.FixedTreeFor(prop.Value.ToString());
                        matches.Add(keyHash);
                    }
                }
                tasks.Add(entry.Tcs);
                if (count++ > 25_000)
                {
                    _hasEntries.Set();
                    break;
                }
            }
            tx.Commit();
        }
        foreach (var tcs in tasks)
        {
            tcs.TrySetResult(null);
        }
        _hasEntries.Wait();
        _hasEntries.Reset();
    }
}

Please note that I didn’t really focus on performance here, just to make sure that this is clear. Basically, we use the hash of the key as the time series ID and then we break the key into name/value pieces and record the time series IDs of all the series with that particular name/value. That allows us to get the list of all the series that match a particular name/value easily, and from there, we can do more complex filtering.

We are using a FixedSizeTree for quite a lot, this is basically a tree whose key is always a long, and whose value is predefined (in this case, a double), and we just store that based on the time series id.

Querying this will require us to first find all the series that match the query, then find the relevant values in the time range for the specified series, and that is all.

Time series Database

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Low Code Demands More Creativity From Developers
  • Data Science Project Folder Structure
  • How Does the Internet Speed Test Work?
  • How to Build a Simple CLI With Oclif

Comments

Database Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo