Skip to content

Instantly share code, notes, and snippets.

@alexwirz
Created June 10, 2014 17:37
Show Gist options
  • Save alexwirz/3fa10d14ecae0c17d872 to your computer and use it in GitHub Desktop.
Save alexwirz/3fa10d14ecae0c17d872 to your computer and use it in GitHub Desktop.
Stupid composite key implementation.
private static async Task Demo()
{
var cancellationTokenSource = new CancellationTokenSource();
using (var db = await Fdb.OpenAsync())
{
var partition = db.Partition("my_data");
var firstId = Guid.NewGuid();
var firstSequence = new[] { "foo", "bar", "baz" };
// writing the data:
using (var trans = db.BeginTransaction(cancellationTokenSource.Token))
{
var version = 0;
foreach (var item in firstSequence)
{
trans.Set(
partition.Pack<Guid, int>(firstId, ++version),
partition.Pack<string>(item));
}
await trans.CommitAsync();
}
// get all items for a sequence:
using (var trans = db.BeginTransaction(cancellationTokenSource.Token))
{
var range = trans.GetRange(FdbKeyRange.PrefixedBy(partition.Pack(firstId)));
var items = range.Select(kv => kv.Value.ToTuple().Last());
await items.ForEachAsync (Console.WriteLine);
// output: foo, bar, baz
}
// get all items starting with the third:
using (var trans = db.BeginTransaction(cancellationTokenSource.Token))
{
var range = trans.GetRange(
FdbKeyRange.PrefixedBy(partition.Pack(firstId)));
var items = range.Skip(2).Select(kv => kv.Value.ToTuple().Last());
await items.ForEachAsync(Console.WriteLine);
// output: baz
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment