Skip to content

Commit

Permalink
Add Sample operation support, fixes #109
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenniak committed Sep 22, 2013
1 parent a84a64b commit ef88fca
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
28 changes: 28 additions & 0 deletions rethinkdb-net-test/Integration/ManyObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> firstRunIds = new HashSet<string>();
foreach (var o in connection.Run(query))
{
firstRunIds.Add(o.Id);
count += 1;
}
Assert.That(count, Is.EqualTo(10));

HashSet<string> secondRunIds = new HashSet<string>();
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));
}
}
}

5 changes: 5 additions & 0 deletions rethinkdb-net/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ public static ISequenceQuery<Tuple<TGroupKeyType, TReductionType>> GroupBy<TObje
return new GroupByQuery<TObject, TReductionType, TGroupKeyType>(sequenceQuery, reductionObject, groupKeyConstructor);
}

public static ISequenceQuery<T> Sample<T>(this ISequenceQuery<T> target, int count)
{
return new SampleQuery<T>(target, count);
}

#endregion
#region Prebuilt GroupBy reductions

Expand Down
38 changes: 38 additions & 0 deletions rethinkdb-net/QueryTerm/SampleQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using RethinkDb.Spec;
using System;

namespace RethinkDb.QueryTerm
{
public class SampleQuery<T> : ISequenceQuery<T>
{
private readonly ISequenceQuery<T> sequenceQuery;
private readonly int number;

public SampleQuery(ISequenceQuery<T> 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;
}
}
}
1 change: 1 addition & 0 deletions rethinkdb-net/rethinkdb-net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
<Compile Include="DatumConverters\AbstractDatumConverterFactory.cs" />
<Compile Include="DatumConverters\AbstractValueTypeDatumConverter.cs" />
<Compile Include="DatumConverters\AbstractReferenceTypeDatumConverter.cs" />
<Compile Include="QueryTerm\SampleQuery.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down

0 comments on commit ef88fca

Please sign in to comment.