From ef88fcaca8cb45201367393f0e369595fbc49e98 Mon Sep 17 00:00:00 2001 From: Mathieu Fenniak Date: Sat, 21 Sep 2013 21:10:43 -0600 Subject: [PATCH] Add Sample operation support, fixes #109 --- .../Integration/ManyObjectTests.cs | 28 ++++++++++++++ rethinkdb-net/Query.cs | 5 +++ rethinkdb-net/QueryTerm/SampleQuery.cs | 38 +++++++++++++++++++ rethinkdb-net/rethinkdb-net.csproj | 1 + 4 files changed, 72 insertions(+) create mode 100644 rethinkdb-net/QueryTerm/SampleQuery.cs diff --git a/rethinkdb-net-test/Integration/ManyObjectTests.cs b/rethinkdb-net-test/Integration/ManyObjectTests.cs index 98f485c..6ad2698 100644 --- a/rethinkdb-net-test/Integration/ManyObjectTests.cs +++ b/rethinkdb-net-test/Integration/ManyObjectTests.cs @@ -105,6 +105,34 @@ public void ReuseSynchronousEnumerable() Assert.That(count, Is.EqualTo(1005)); } } + + [Test] + public void Sample() + { + var query = testTable.Sample(10); + + int count = 0; + HashSet firstRunIds = new HashSet(); + foreach (var o in connection.Run(query)) + { + firstRunIds.Add(o.Id); + count += 1; + } + Assert.That(count, Is.EqualTo(10)); + + HashSet secondRunIds = new HashSet(); + foreach (var o in connection.Run(query)) + { + secondRunIds.Add(o.Id); + count += 1; + } + Assert.That(count, Is.EqualTo(20)); + + // Two random sets of ten objects shouldn't have a lot of the same objects, if any; otherwise our Sample + // query might not be doing quite what we expect... + firstRunIds.IntersectWith(secondRunIds); + Assert.That(firstRunIds.Count, Is.LessThan(5)); + } } } diff --git a/rethinkdb-net/Query.cs b/rethinkdb-net/Query.cs index 70ef17a..83cbb4f 100644 --- a/rethinkdb-net/Query.cs +++ b/rethinkdb-net/Query.cs @@ -320,6 +320,11 @@ public static ISequenceQuery> GroupBy(sequenceQuery, reductionObject, groupKeyConstructor); } + public static ISequenceQuery Sample(this ISequenceQuery target, int count) + { + return new SampleQuery(target, count); + } + #endregion #region Prebuilt GroupBy reductions diff --git a/rethinkdb-net/QueryTerm/SampleQuery.cs b/rethinkdb-net/QueryTerm/SampleQuery.cs new file mode 100644 index 0000000..eff5002 --- /dev/null +++ b/rethinkdb-net/QueryTerm/SampleQuery.cs @@ -0,0 +1,38 @@ +using RethinkDb.Spec; +using System; + +namespace RethinkDb.QueryTerm +{ + public class SampleQuery : ISequenceQuery + { + private readonly ISequenceQuery sequenceQuery; + private readonly int number; + + public SampleQuery(ISequenceQuery sequenceQuery, int number) + { + this.sequenceQuery = sequenceQuery; + this.number = number; + } + + public Term GenerateTerm(IDatumConverterFactory datumConverterFactory) + { + var sampleTerm = new Term() + { + type = Term.TermType.SAMPLE, + }; + sampleTerm.args.Add(sequenceQuery.GenerateTerm(datumConverterFactory)); + sampleTerm.args.Add( + new Term() + { + type = Term.TermType.DATUM, + datum = new Datum() + { + type = Datum.DatumType.R_NUM, + r_num = number, + } + } + ); + return sampleTerm; + } + } +} \ No newline at end of file diff --git a/rethinkdb-net/rethinkdb-net.csproj b/rethinkdb-net/rethinkdb-net.csproj index 81978b6..b3383bc 100644 --- a/rethinkdb-net/rethinkdb-net.csproj +++ b/rethinkdb-net/rethinkdb-net.csproj @@ -156,6 +156,7 @@ +