public void Test_FdbQueryUnionExpression() { var expr1 = FdbQueryIndexLookupExpression <int, string> .Lookup( FooBarIndex, (x) => x == "world" ); var expr2 = FdbQueryIndexLookupExpression <int, long> .Lookup( FooBazIndex, (x) => x == 1234L ); var expr = FdbQueryExpressions.Union( expr1, expr2 ); Log(expr); Assert.That(expr, Is.Not.Null); Assert.That(expr.Terms, Is.Not.Null); Assert.That(expr.Terms.Count, Is.EqualTo(2)); Assert.That(expr.Terms[0], Is.SameAs(expr1)); Assert.That(expr.Terms[1], Is.SameAs(expr2)); Assert.That(expr.Type, Is.EqualTo(typeof(IAsyncEnumerable <int>))); Assert.That(expr.ElementType, Is.EqualTo(typeof(int))); Log(FdbQueryExpressions.ExplainSequence(expr)); }
/// <summary>Return all the keys that share a common prefix</summary> /// <param name="query">Source database query</param> /// <param name="prefix">Shared prefix</param> /// <returns>Query that will return the keys that share the specified <paramref name="prefix"/></returns> public static IFdbAsyncSequenceQueryable <KeyValuePair <Slice, Slice> > RangeStartsWith(this IFdbDatabaseQueryable query, Slice prefix) { if (query == null) { throw new ArgumentNullException(nameof(query)); } var expr = FdbQueryExpressions.RangeStartsWith(prefix); return(query.Provider.CreateSequenceQuery(expr)); }
/// <summary>Return a range of keys</summary> /// <param name="query">Source database query</param> /// <param name="range">Pair of key selectors</param> /// <returns>Query that will return the keys from the specified <paramref name="range"/></returns> public static IFdbAsyncSequenceQueryable <KeyValuePair <Slice, Slice> > Range(this IFdbTransactionQueryable query, KeySelectorPair range) { if (query == null) { throw new ArgumentNullException(nameof(query)); } var expr = FdbQueryExpressions.Range(range); return(query.Provider.CreateSequenceQuery(expr)); }
/// <summary>Returns the first element of a sequence query</summary> public static Task <int> CountAsync <T>(this IFdbAsyncSequenceQueryable <T> query, CancellationToken ct = default(CancellationToken)) { if (query == null) { throw new ArgumentNullException(nameof(query)); } var expr = FdbQueryExpressions.Single <T, int>( (FdbQuerySequenceExpression <T>)query.Expression, "CountAsync", (source, _ct) => source.CountAsync(_ct) ); return(query.Provider.CreateQuery <int>(expr).ExecuteSingle(ct)); }
public void Test_FdbQueryRangeExpression() { var expr = FdbQueryExpressions.Range( KeySelectorPair.Create(TuPack.ToKeyRange("Foo")) ); Log(expr); Assert.That(expr, Is.Not.Null); Assert.That(expr.Range.Begin.Key.ToString(), Is.EqualTo("<02>Foo<00><00>")); Assert.That(expr.Range.End.Key.ToString(), Is.EqualTo("<02>Foo<00><FF>")); Assert.That(expr.Type, Is.EqualTo(typeof(IAsyncEnumerable <KeyValuePair <Slice, Slice> >))); Assert.That(expr.ElementType, Is.EqualTo(typeof(KeyValuePair <Slice, Slice>))); Log(FdbQueryExpressions.ExplainSequence(expr)); }
public void Test_FdbQueryRangeExpression() { var expr = FdbQueryExpressions.Range( FdbTuple.Create("Foo").ToSelectorPair() ); Console.WriteLine(expr); Assert.That(expr, Is.Not.Null); Assert.That(expr.Range.Begin.Key.ToString(), Is.EqualTo("<02>Foo<00>")); Assert.That(expr.Range.End.Key.ToString(), Is.EqualTo("<02>Foo<01>")); Assert.That(expr.Type, Is.EqualTo(typeof(IFdbAsyncEnumerable <KeyValuePair <Slice, Slice> >))); Assert.That(expr.ElementType, Is.EqualTo(typeof(KeyValuePair <Slice, Slice>))); Console.WriteLine(FdbQueryExpressions.ExplainSequence(expr)); }
public void Test_FdbQueryFilterExpression() { var expr = FdbQueryExpressions.Filter( FdbQueryExpressions.RangeStartsWith(TuPack.EncodeKey("Hello", "World")), (kvp) => kvp.Value.ToInt32() % 2 == 0 ); Log(expr); Assert.That(expr, Is.Not.Null); Assert.That(expr.Source, Is.Not.Null.And.InstanceOf <FdbQueryRangeExpression>()); Assert.That(expr.Filter, Is.Not.Null); Assert.That(expr.Type, Is.EqualTo(typeof(IAsyncEnumerable <KeyValuePair <Slice, Slice> >))); Assert.That(expr.ElementType, Is.EqualTo(typeof(KeyValuePair <Slice, Slice>))); Log(FdbQueryExpressions.ExplainSequence(expr)); }
public void Test_FdbQueryTransformExpression() { var expr = FdbQueryExpressions.Transform( FdbQueryExpressions.RangeStartsWith(TuPack.EncodeKey("Hello", "World")), (kvp) => kvp.Value.ToUnicode() ); Log(expr); Assert.That(expr, Is.Not.Null); Assert.That(expr.Source, Is.Not.Null.And.InstanceOf <FdbQueryRangeExpression>()); Assert.That(expr.Transform, Is.Not.Null); Assert.That(expr.Type, Is.EqualTo(typeof(IAsyncEnumerable <string>))); Assert.That(expr.ElementType, Is.EqualTo(typeof(string))); Log(FdbQueryExpressions.ExplainSequence(expr)); }
/// <summary>Returns the first element of a sequence query</summary> public static Task <T> FirstOrDefaultAsync <T>(this IFdbAsyncSequenceQueryable <T> query, CancellationToken ct = default(CancellationToken)) { if (query == null) { throw new ArgumentNullException(nameof(query)); } if (ct.IsCancellationRequested) { return(Task.FromCanceled <T>(ct)); } var expr = FdbQueryExpressions.Single <T, T>( (FdbQuerySequenceExpression <T>)query.Expression, "FirstOrDefaultAsync", (source, _ct) => source.FirstOrDefaultAsync(_ct) ); return(query.Provider.CreateQuery <T>(expr).ExecuteSingle(ct)); }
public void Test_FdbQueryIndexLookupExpression_From_Lambda() { var expr = FdbQueryIndexLookupExpression <int, string> .Lookup( FooBarIndex, (bar) => bar == "world" ); Log(expr); Assert.That(expr, Is.Not.Null); Assert.That(expr.Index, Is.SameAs(FooBarIndex)); //TODO: .Index.Index does not look very nice Assert.That(expr.Operator, Is.EqualTo(ExpressionType.Equal)); Assert.That(expr.Value, Is.Not.Null); Assert.That(expr.Value, Is.InstanceOf <ConstantExpression>().With.Property("Value").EqualTo("world")); Assert.That(expr.Type, Is.EqualTo(typeof(IAsyncEnumerable <int>))); Assert.That(expr.ElementType, Is.EqualTo(typeof(int))); Log(FdbQueryExpressions.ExplainSequence(expr)); }
/// <summary>Filters a sequence query of values based on a predicate.</summary> public static IFdbAsyncSequenceQueryable <T> Where <T>(this IFdbAsyncSequenceQueryable <T> query, Expression <Func <T, bool> > predicate) { if (query == null) { throw new ArgumentNullException(nameof(query)); } if (predicate == null) { throw new ArgumentNullException(nameof(predicate)); } var sourceExpr = query.Expression as FdbQuerySequenceExpression <T>; if (sourceExpr == null) { throw new ArgumentException("query"); } var expr = FdbQueryExpressions.Filter(sourceExpr, predicate); return(query.Provider.CreateSequenceQuery <T>(expr)); }
/// <summary>Projects each element of a sequence query into a new form.</summary> public static IFdbAsyncSequenceQueryable <R> Select <T, R>(this IFdbAsyncSequenceQueryable <T> query, Expression <Func <T, R> > selector) { if (query == null) { throw new ArgumentNullException(nameof(query)); } if (selector == null) { throw new ArgumentNullException(nameof(selector)); } var sourceExpr = query.Expression as FdbQuerySequenceExpression <T>; if (sourceExpr == null) { throw new ArgumentException("query"); } var expr = FdbQueryExpressions.Transform(sourceExpr, selector); return(query.Provider.CreateSequenceQuery <R>(expr)); }