AbraxasDB - 2 - Queries, indexes, and value readers

CSharp · Console

/Program.cs

// @nuget: AbraxasDB -Version 1.0.4
// AbraxasDB playground 2: query, add an index, and read selected values.
// Try changing minimumAge. Add a record before or after CreateIndex.
// This small example demonstrates behavior; it is not a performance benchmark.
using System;
using System.IO;
using AbraxasDB;

public static class Program
{
    /// <summary>Runs an age predicate before and after index creation, verifies index maintenance after insertion, and reads a selected property.<br/></summary>
    public static void Main()
    {
        const int minimumAge = 40;
        string directory = Path.Combine(Path.GetTempPath(), "abraxas-query-" + Guid.NewGuid().ToString("N"));
        try
        {
            using (var database = RecordBase.Create(directory, "playground"))
            {
                var customers = database.CreateOrOpenStore<Customer>("customers");
                ulong adaId = customers.Put(new Customer { Name = "Ada", Age = 37 });
                customers.Put(new Customer { Name = "Grace", Age = 85 });
                customers.Put(new Customer { Name = "Katherine", Age = 45 });

                var condition = customers.Where.Prop(customer => customer.Age)
                    .GreaterOrEqual(minimumAge).EndCondition;
                var beforeIndex = customers.CountWhere(condition);
                Console.WriteLine("Age >= " + minimumAge + ", before adding an index: " + beforeIndex);

                customers.CreateIndex(customer => customer.Age, IndexDirection.Ascending);
                var afterIndex = customers.CountWhere(condition);
                Console.WriteLine("Same condition after adding an age index: " + afterIndex);
                if (beforeIndex != afterIndex) throw new InvalidOperationException("Query results changed after indexing.");

                customers.Put(new Customer { Name = "Dorothy", Age = 50 });
                var afterInsert = customers.CountWhere(condition);
                Console.WriteLine("After inserting Dorothy, age 50: " + afterInsert);
                if (afterInsert != afterIndex + (50 >= minimumAge ? 1UL : 0UL))
                    throw new InvalidOperationException("The index did not reflect the new record.");

                using (var reader = customers.Readers.Open(new[] { adaId }))
                {
                    if (!reader.ReadNoLinks()) throw new InvalidOperationException("Ada was not found.");
                    Console.WriteLine("Read just Ada's name through a value reader: " + reader.Get<string>(".Name"));
                }
            }
            Console.WriteLine("EXAMPLE_02_PASS");
        }
        finally
        {
            if (Directory.Exists(directory)) Directory.Delete(directory, true);
        }
    }
}

/// <summary>A plain record used by both the age predicate and the selective value reader.<br/></summary>
public sealed class Customer
{
    /// <summary>Gets or sets the customer name.<br/></summary>
    public string Name { get; set; } = "";
    /// <summary>Gets or sets the indexed age.<br/></summary>
    public int Age { get; set; }
}
;