/// <summary>Returns a Key Selector pair that defines the range of all items contained under this tuple</summary> public static KeySelectorPair ToSelectorPair([NotNull] this ITuple tuple) { if (tuple == null) { throw new ArgumentNullException(nameof(tuple)); } return(KeySelectorPair.StartsWith(tuple.ToSlice())); }
private static async Task BenchMergeSortAsync(IFdbDatabase db, int N, int K, int B, CancellationToken ct) { Console.WriteLine($"=== BenchMergeSort(N={N:N0}, K={K:N0}, B={B:N0}) ==="); // create multiple lists var location = db.GlobalSpace.Partition.ByKey("MergeSort"); await db.ClearRangeAsync(location, ct); var sources = Enumerable.Range(0, K).Select(i => 'A' + i).ToArray(); var rnd = new Random(); // insert a number of random number lists Console.Write($"> Inserting {(K * N):N0} items... "); foreach (var source in sources) { using (var tr = db.BeginTransaction(ct)) { var list = location.Partition.ByKey(source); for (int i = 0; i < N; i++) { tr.Set(list.Keys.Encode(rnd.Next()), Slice.FromInt32(i)); } await tr.CommitAsync(); } } Console.WriteLine("Done"); // merge/sort them to get only one (hopefully sorted) list using (var tr = db.BeginTransaction(ct)) { var mergesort = tr .MergeSort( sources.Select(source => KeySelectorPair.StartsWith(location.Keys.Encode(source))), (kvp) => location.Keys.DecodeLast <int>(kvp.Key) ) .Take(B) .Select(kvp => location.Keys.Unpack(kvp.Key)); Console.Write($"> MergeSort with limit {B:N0}... "); var sw = Stopwatch.StartNew(); var results = await mergesort.ToListAsync(); sw.Stop(); Console.WriteLine("Done"); Console.WriteLine($"Took {FormatTimeMilli(sw.Elapsed.TotalMilliseconds)} to merge sort {results.Count:N0} results from {K} lists of {N} items each"); //foreach (var result in results) //{ // Console.WriteLine(result.Get<int>(-1)); //} } Console.WriteLine(); }
public static FdbQueryRangeExpression RangeStartsWith(Slice prefix, FdbRangeOptions options = null) { // starts_with('A') means ['A', B') return(Range(KeySelectorPair.StartsWith(prefix), options)); }