Skip to content

Commit

Permalink
CSHARP-4440: Incorporate MongoDB.Labs.Search library (#989)
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisDog committed Jan 19, 2023
1 parent 0bb42fa commit 9189a58
Show file tree
Hide file tree
Showing 47 changed files with 6,487 additions and 115 deletions.
7 changes: 7 additions & 0 deletions build.cake
Expand Up @@ -240,6 +240,13 @@ Task("TestAtlasDataLake")
action: (BuildConfig buildConfig, Path testProject) =>
RunTests(buildConfig, testProject, filter: "Category=\"AtlasDataLake\""));

Task("TestAtlasSearch")
.IsDependentOn("Build")
.DoesForEach(
items: GetFiles("./**/MongoDB.Driver.Tests.csproj"),
action: (BuildConfig buildConfig, Path testProject) =>
RunTests(buildConfig, testProject, filter: "Category=\"AtlasSearch\""));

Task("TestOcsp")
.IsDependentOn("Build")
.DoesForEach(
Expand Down
20 changes: 20 additions & 0 deletions evergreen/evergreen.yml
Expand Up @@ -684,6 +684,15 @@ functions:
${PREPARE_SHELL}
evergreen/run-atlas-data-lake-test.sh
run-atlas-search-test:
- command: shell.exec
type: test
params:
working_dir: mongo-csharp-driver
script: |
${PREPARE_SHELL}
ATLAS_SEARCH="${ATLAS_SEARCH}" evergreen/run-atlas-search-test.sh
run-ocsp-test:
- command: shell.exec
type: test
Expand Down Expand Up @@ -1202,6 +1211,10 @@ tasks:
- func: bootstrap-mongohoused
- func: run-atlas-data-lake-test

- name: atlas-search-test
commands:
- func: run-atlas-search-test

- name: test-serverless-net472
exec_timeout_secs: 2700 # 45 minutes: 15 for setup + 30 for tests
commands:
Expand Down Expand Up @@ -2068,6 +2081,13 @@ buildvariants:
tasks:
- name: atlas-data-lake-test

- name: atlas-search-test
display_name: "Atlas Search Tests"
run_on:
- windows-64-vs2017-test
tasks:
- name: atlas-search-test

- name: gssapi-auth-tests-windows
run_on:
- windows-64-vs2017-test
Expand Down
17 changes: 17 additions & 0 deletions evergreen/run-atlas-search-test.sh
@@ -0,0 +1,17 @@
#!/usr/bin/env bash

set -o xtrace
set -o errexit # Exit the script with error if any of the commands fail

# Environment variables produced as output
# ATLAS_SEARCH_TESTS_ENABLED Enable atlas search tests.

############################################
# Main Program #
############################################

echo "Running Atlas Search driver tests"

export ATLAS_SEARCH_TESTS_ENABLED=true

powershell.exe .\\build.ps1 --target=TestAtlasSearch
141 changes: 72 additions & 69 deletions src/MongoDB.Driver.Core/Core/Misc/Ensure.cs
Expand Up @@ -27,6 +27,22 @@ namespace MongoDB.Driver.Core.Misc
[DebuggerStepThrough]
public static class Ensure
{
/// <summary>
/// Ensures that the value of a parameter is not null.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static Nullable<T> HasValue<T>(Nullable<T> value, string paramName) where T : struct
{
if (!value.HasValue)
{
throw new ArgumentException("The Nullable parameter must have a value.", paramName);
}
return value;
}

/// <summary>
/// Ensures that the value of a parameter is between a minimum and a maximum value.
/// </summary>
Expand Down Expand Up @@ -65,34 +81,36 @@ public static T IsEqualTo<T>(T value, T comparand, string paramName)
}

/// <summary>
/// Ensures that the value of a parameter is greater than or equal to a comparand.
/// Ensures that the value of a parameter is greater than a comparand.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="comparand">The comparand.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static T IsGreaterThanOrEqualTo<T>(T value, T comparand, string paramName) where T : IComparable<T>
public static T IsGreaterThan<T>(T value, T comparand, string paramName) where T : IComparable<T>
{
if (value.CompareTo(comparand) < 0)
if (value.CompareTo(comparand) <= 0)
{
var message = string.Format("Value is not greater than or equal to {1}: {0}.", value, comparand);
var message = $"Value is not greater than {comparand}: {value}.";
throw new ArgumentOutOfRangeException(paramName, message);
}
return value;
}

/// <summary>
/// Ensures that the value of a parameter is greater than or equal to zero.
/// Ensures that the value of a parameter is greater than or equal to a comparand.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="comparand">The comparand.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static int IsGreaterThanOrEqualToZero(int value, string paramName)
public static T IsGreaterThanOrEqualTo<T>(T value, T comparand, string paramName) where T : IComparable<T>
{
if (value < 0)
if (value.CompareTo(comparand) < 0)
{
var message = string.Format("Value is not greater than or equal to 0: {0}.", value);
var message = string.Format("Value is not greater than or equal to {1}: {0}.", value, comparand);
throw new ArgumentOutOfRangeException(paramName, message);
}
return value;
Expand All @@ -104,79 +122,62 @@ public static int IsGreaterThanOrEqualToZero(int value, string paramName)
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static long IsGreaterThanOrEqualToZero(long value, string paramName)
{
if (value < 0)
{
var message = string.Format("Value is not greater than or equal to 0: {0}.", value);
throw new ArgumentOutOfRangeException(paramName, message);
}
return value;
}
public static int IsGreaterThanOrEqualToZero(int value, string paramName) =>
IsGreaterThanOrEqualTo(value, 0, paramName);

/// <summary>
/// Ensures that the value of a parameter is greater than or equal to zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static TimeSpan IsGreaterThanOrEqualToZero(TimeSpan value, string paramName)
{
if (value < TimeSpan.Zero)
{
var message = string.Format("Value is not greater than or equal to zero: {0}.", TimeSpanParser.ToString(value));
throw new ArgumentOutOfRangeException(paramName, message);
}
return value;
}
public static long IsGreaterThanOrEqualToZero(long value, string paramName) =>
IsGreaterThanOrEqualTo(value, 0, paramName);

/// <summary>
/// Ensures that the value of a parameter is greater than or equal to zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static TimeSpan IsGreaterThanOrEqualToZero(TimeSpan value, string paramName) =>
IsGreaterThanOrEqualTo(value, TimeSpan.Zero, paramName);

/// <summary>
/// Ensures that the value of a parameter is greater than zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static int IsGreaterThanZero(int value, string paramName)
{
if (value <= 0)
{
var message = string.Format("Value is not greater than zero: {0}.", value);
throw new ArgumentOutOfRangeException(paramName, message);
}
return value;
}
public static int IsGreaterThanZero(int value, string paramName) =>
IsGreaterThan(value, 0, paramName);

/// <summary>
/// Ensures that the value of a parameter is greater than zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static long IsGreaterThanZero(long value, string paramName)
{
if (value <= 0)
{
var message = string.Format("Value is not greater than zero: {0}.", value);
throw new ArgumentOutOfRangeException(paramName, message);
}
return value;
}
public static long IsGreaterThanZero(long value, string paramName) =>
IsGreaterThan(value, 0, paramName);

/// <summary>
/// Ensures that the value of a parameter is greater than zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static TimeSpan IsGreaterThanZero(TimeSpan value, string paramName)
{
if (value <= TimeSpan.Zero)
{
var message = string.Format("Value is not greater than zero: {0}.", value);
throw new ArgumentOutOfRangeException(paramName, message);
}
return value;
}
public static double IsGreaterThanZero(double value, string paramName) =>
IsGreaterThan(value, 0, paramName);

/// <summary>
/// Ensures that the value of a parameter is greater than zero.
/// </summary>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static TimeSpan IsGreaterThanZero(TimeSpan value, string paramName) =>
IsGreaterThan(value, TimeSpan.Zero, paramName);

/// <summary>
/// Ensures that the value of a parameter is infinite or greater than or equal to zero.
Expand Down Expand Up @@ -247,22 +248,6 @@ public static IEnumerable<T> IsNotNullAndDoesNotContainAnyNulls<T>(IEnumerable<T
return values;
}

/// <summary>
/// Ensures that the value of a parameter is not null.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static Nullable<T> HasValue<T>(Nullable<T> value, string paramName) where T : struct
{
if (!value.HasValue)
{
throw new ArgumentException("The Nullable parameter must have a value.", paramName);
}
return value;
}

/// <summary>
/// Ensures that the value of a parameter is not null or empty.
/// </summary>
Expand Down Expand Up @@ -298,6 +283,24 @@ public static string IsNotNullOrEmpty(string value, string paramName)
return value;
}

/// <summary>
/// Ensures that the value of a parameter is null or is between a minimum and a maximum value.
/// </summary>
/// <typeparam name="T">Type type of the value.</typeparam>
/// <param name="value">The value of the parameter.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="paramName">The name of the parameter.</param>
/// <returns>The value of the parameter.</returns>
public static T? IsNullOrBetween<T>(T? value, T min, T max, string paramName) where T : struct, IComparable<T>
{
if (value != null)
{
IsBetween(value.Value, min, max, paramName);
}
return value;
}

/// <summary>
/// Ensures that the value of a parameter is null or greater than or equal to zero.
/// </summary>
Expand Down
25 changes: 22 additions & 3 deletions src/MongoDB.Driver/AggregateFluent.cs
Expand Up @@ -13,13 +13,14 @@
* limitations under the License.
*/

using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Core.Misc;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Search;

namespace MongoDB.Driver
{
Expand Down Expand Up @@ -238,6 +239,24 @@ public override IAggregateFluent<TNewResult> ReplaceWith<TNewResult>(AggregateEx
return WithPipeline(_pipeline.ReplaceWith(newRoot));
}

public override IAggregateFluent<TResult> Search(
SearchDefinition<TResult> searchDefinition,
SearchHighlightOptions<TResult> highlight = null,
string indexName = null,
SearchCountOptions count = null,
bool returnStoredSource = false)
{
return WithPipeline(_pipeline.Search(searchDefinition, highlight, indexName, count, returnStoredSource));
}

public override IAggregateFluent<SearchMetaResult> SearchMeta(
SearchDefinition<TResult> searchDefinition,
string indexName = null,
SearchCountOptions count = null)
{
return WithPipeline(_pipeline.SearchMeta(searchDefinition, indexName, count));
}

public override IAggregateFluent<BsonDocument> SetWindowFields<TWindowFields>(
AggregateExpressionDefinition<ISetWindowFieldsPartition<TResult>, TWindowFields> output)
{
Expand Down
21 changes: 21 additions & 0 deletions src/MongoDB.Driver/AggregateFluentBase.cs
Expand Up @@ -19,6 +19,7 @@
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Search;

namespace MongoDB.Driver
{
Expand Down Expand Up @@ -215,6 +216,26 @@ public virtual IAggregateFluent<TNewResult> ReplaceWith<TNewResult>(AggregateExp
throw new NotImplementedException();
}

/// <inheritdoc />
public virtual IAggregateFluent<TResult> Search(
SearchDefinition<TResult> searchDefinition,
SearchHighlightOptions<TResult> highlight = null,
string indexName = null,
SearchCountOptions count = null,
bool returnStoredSource = false)
{
throw new NotImplementedException();
}

/// <inheritdoc />
public virtual IAggregateFluent<SearchMetaResult> SearchMeta(
SearchDefinition<TResult> searchDefinition,
string indexName = null,
SearchCountOptions count = null)
{
throw new NotImplementedException();
}

/// <inheritdoc />
public virtual IAggregateFluent<BsonDocument> SetWindowFields<TWindowFields>(
AggregateExpressionDefinition<ISetWindowFieldsPartition<TResult>, TWindowFields> output)
Expand Down

0 comments on commit 9189a58

Please sign in to comment.