public async Task Test_Can_MergeSort()
        {
            int K = 3;
            int N = 100;

            using (var db = await OpenTestPartitionAsync())
            {
                var location = await GetCleanDirectory(db, "Queries", "MergeSort");

                // clear!
                await db.ClearRangeAsync(location, this.Cancellation);

                // create K lists
                var lists = Enumerable.Range(0, K).Select(i => location.Partition.ByKey(i)).ToArray();

                // lists[0] contains all multiples of K ([0, 0], [K, 1], [2K, 2], ...)
                // lists[1] contains all multiples of K, offset by 1 ([1, 0], [K+1, 1], [2K+1, 2], ...)
                // lists[k-1] contains all multiples of K, offset by k-1 ([K-1, 0], [2K-1, 1], [3K-1, 2], ...)

                // more generally: lists[k][i] = (..., MergeSort, k, (i * K) + k) = (k, i)

                for (int k = 0; k < K; k++)
                {
                    using (var tr = db.BeginTransaction(this.Cancellation))
                    {
                        for (int i = 0; i < N; i++)
                        {
                            tr.Set(lists[k].Keys.Encode((i * K) + k), FdbTuple.EncodeKey(k, i));
                        }
                        await tr.CommitAsync();
                    }
                }

                // MergeSorting all lists together should produce all integers from 0 to (K*N)-1, in order
                // we use the last part of the key for sorting

                using (var tr = db.BeginTransaction(this.Cancellation))
                {
                    var merge = tr.MergeSort(
                        lists.Select(list => FdbKeySelectorPair.Create(list.Keys.ToRange())),
                        kvp => location.Keys.DecodeLast <int>(kvp.Key)
                        );

                    Assert.That(merge, Is.Not.Null);
                    Assert.That(merge, Is.InstanceOf <FdbMergeSortIterator <KeyValuePair <Slice, Slice>, int, KeyValuePair <Slice, Slice> > >());

                    var results = await merge.ToListAsync();

                    Assert.That(results, Is.Not.Null);
                    Assert.That(results.Count, Is.EqualTo(K * N));

                    for (int i = 0; i < K * N; i++)
                    {
                        Assert.That(location.ExtractKey(results[i].Key), Is.EqualTo(FdbTuple.EncodeKey(i % K, i)));
                        Assert.That(results[i].Value, Is.EqualTo(FdbTuple.EncodeKey(i % K, i / K)));
                    }
                }
            }
        }
        public async Task Test_Range_Except()
        {
            int K = 3;
            int N = 100;

            using (var db = await OpenTestPartitionAsync())
            {
                // get a clean new directory
                var location = await GetCleanDirectory(db, "Queries", "Except");

                // create K lists
                var lists = Enumerable.Range(0, K).Select(i => location.Partition.ByKey(i)).ToArray();

                // lists[0] contains all multiples of 1
                // lists[1] contains all multiples of 2
                // lists[k-1] contains all multiples of K

                // more generally: lists[k][i] = (..., Intersect, k, i * (k + 1)) = (k, i)

                var series = Enumerable.Range(1, K).Select(k => Enumerable.Range(1, N).Select(x => k * x).ToArray()).ToArray();
                //foreach(var serie in series)
                //{
                //	Console.WriteLine(String.Join(", ", serie));
                //}

                for (int k = 0; k < K; k++)
                {
                    //Console.WriteLine("> k = " + k);
                    using (var tr = db.BeginTransaction(this.Cancellation))
                    {
                        for (int i = 0; i < N; i++)
                        {
                            var key   = lists[k].Keys.Encode(series[k][i]);
                            var value = FdbTuple.EncodeKey(k, i);
                            //Console.WriteLine("> " + key + " = " + value);
                            tr.Set(key, value);
                        }
                        await tr.CommitAsync();
                    }
                }

                // Intersect all lists together should produce all integers that are prime numbers
                IEnumerable <int> xs = series[0];
                for (int i = 1; i < K; i++)
                {
                    xs = xs.Except(series[i]);
                }
                var expected = xs.ToArray();
                Log("Expected: {0}", String.Join(", ", expected));

                using (var tr = db.BeginTransaction(this.Cancellation))
                {
                    var merge = tr.Except(
                        lists.Select(list => FdbKeySelectorPair.Create(list.Keys.ToRange())),
                        kvp => location.Keys.DecodeLast <int>(kvp.Key)
                        );

                    Assert.That(merge, Is.Not.Null);
                    Assert.That(merge, Is.InstanceOf <FdbExceptIterator <KeyValuePair <Slice, Slice>, int, KeyValuePair <Slice, Slice> > >());

                    var results = await merge.ToListAsync();

                    Assert.That(results, Is.Not.Null);

                    Assert.That(results.Count, Is.EqualTo(expected.Length));

                    for (int i = 0; i < results.Count; i++)
                    {
                        Assert.That(location.Keys.DecodeLast <int>(results[i].Key), Is.EqualTo(expected[i]));
                    }
                }
            }
        }