AbraxasDB - 3 - Files, backups, and restore

CSharp · Console

/Program.cs

// @nuget: AbraxasDB -Version 1.0.4
// AbraxasDB playground 3: real files, a snapshot, and an independent restore.
// Try changing durabilityMode between Deferred and Standard.
// The backup captures two records; a later third record stays outside it.
// All files are temporary. This does not promise persistence between Run requests.
using System;
using System.IO;
using AbraxasDB;

public static class Program
{
    /// <summary>Creates a database and backup, appends a record after the snapshot, and verifies the restored database contains the earlier state.<br/></summary>
    public static void Main()
    {
        const RecordBaseConfiguration.DurabilityMode durabilityMode = RecordBaseConfiguration.DurabilityMode.Deferred;
        string root = Path.Combine(Path.GetTempPath(), "abraxas-backup-" + Guid.NewGuid().ToString("N"));
        string liveDirectory = Path.Combine(root, "live");
        string backupDirectory = Path.Combine(root, "backup");
        string restoredDirectory = Path.Combine(root, "restored");
        var configuration = RecordBaseConfiguration.Create(durabilityMode: durabilityMode);
        ulong adaId;
        try
        {
            Console.WriteLine("Durability mode: " + durabilityMode);
            using (var database = RecordBase.Create(liveDirectory, "playground", configuration))
            {
                var customers = database.CreateOrOpenStore<Customer>("customers");
                adaId = customers.Put(new Customer { Name = "Ada", Age = 37 });
                customers.Put(new Customer { Name = "Grace", Age = 85 });
                database.CreateBackup(backupDirectory);
                Console.WriteLine("Created a backup containing two customers.");

                customers.Put(new Customer { Name = "Katherine", Age = 45 });
                Console.WriteLine("Live database after another insert: " + customers.Aggregates.Count());
                if (customers.Aggregates.Count() != 3) throw new InvalidOperationException("Live count was unexpected.");
            }

            RecordBase.RestoreBackup(backupDirectory, restoredDirectory, "playground", configuration);
            using (var restored = RecordBase.Open(restoredDirectory, "playground", configuration))
            {
                var customers = restored.OpenStore<Customer>("customers");
                Console.WriteLine("Restored database count: " + customers.Aggregates.Count());
                Console.WriteLine("Restored Ada: " + customers.Get(adaId).Name);
                if (customers.Aggregates.Count() != 2 || customers.Get(adaId).Name != "Ada")
                    throw new InvalidOperationException("The restored data did not match the snapshot.");
            }

            Console.WriteLine("Live database files:");
            foreach (string file in Directory.GetFiles(liveDirectory))
                Console.WriteLine("  " + Path.GetFileName(file) + " (" + new FileInfo(file).Length + " bytes)");
            Console.WriteLine("EXAMPLE_03_PASS");
        }
        finally
        {
            if (Directory.Exists(root)) Directory.Delete(root, true);
        }
    }
}

/// <summary>An ordinary record persisted in the live database and recovered from its snapshot.<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 customer age.<br/></summary>
    public int Age { get; set; }
}
;