Commit 80d7d50a authored by Nick Craver's avatar Nick Craver

Cleanup for NRediSearch

Lots of documentation and formatting fixes, no functional changes.
parent 965453e2
This diff is collapsed.
......@@ -17,7 +17,7 @@ public class BuilderTest : RediSearchTestBase
public BuilderTest(ITestOutputHelper output) : base(output) { }
[Fact]
public void testTag()
public void TestTag()
{
Value v = Tags("foo");
Assert.Equal("{foo}", v.ToString());
......@@ -26,16 +26,13 @@ public void testTag()
}
[Fact]
public void testEmptyTag()
public void TestEmptyTag()
{
Assert.Throws<ArgumentException>(() =>
{
Tags();
});
Assert.Throws<ArgumentException>(() => Tags());
}
[Fact]
public void testRange()
public void TestRange()
{
Value v = Between(1, 10);
Assert.Equal("[1.0 10.0]", v.ToString());
......@@ -60,7 +57,7 @@ public void testRange()
}
[Fact]
public void testIntersectionBasic()
public void TestIntersectionBasic()
{
INode n = Intersect().Add("name", "mark");
Assert.Equal("@name:mark", n.ToString());
......@@ -70,7 +67,7 @@ public void testIntersectionBasic()
}
[Fact]
public void testIntersectionNested()
public void TestIntersectionNested()
{
INode n = Intersect().
Add(Union("name", Value("mark"), Value("dvir"))).
......@@ -79,14 +76,15 @@ public void testIntersectionNested()
Assert.Equal("(@name:(mark|dvir) @time:[100.0 200.0] -@created:[-inf (1000.0])", n.ToString());
}
static string GetArgsString(AggregationRequest request)
private static string GetArgsString(AggregationRequest request)
{
var args = new List<object>();
request.SerializeRedisArgs(args);
return string.Join(" ", args);
}
[Fact]
public void testAggregation()
public void TestAggregation()
{
Assert.Equal("*", GetArgsString(new AggregationRequest()));
AggregationRequest r = new AggregationRequest().
......
......@@ -10,7 +10,7 @@ namespace NRediSearch.Test
public abstract class RediSearchTestBase : IDisposable
{
protected readonly ITestOutputHelper Output;
public RediSearchTestBase(ITestOutputHelper output)
protected RediSearchTestBase(ITestOutputHelper output)
{
muxer = GetWithFT(output);
Output = output;
......@@ -82,7 +82,7 @@ internal static ConnectionMultiplexer GetWithFT(ITestOutputHelper output)
return conn;
}
static Dictionary<string, RedisValue> Parse(RedisResult module)
private static Dictionary<string, RedisValue> Parse(RedisResult module)
{
var data = new Dictionary<string, RedisValue>();
var lines = (RedisResult[])module;
......
......@@ -9,7 +9,7 @@ namespace NRediSearch.Aggregation.Reducers
public abstract class Reducer
{
public override string ToString() => Name;
private string _field;
private readonly string _field;
internal Reducer(string field) => _field = field;
......
......@@ -6,23 +6,22 @@ namespace NRediSearch.Aggregation.Reducers
{
public static class Reducers
{
public static Reducer Count() => CountReducer.Instance;
sealed class CountReducer : Reducer
private sealed class CountReducer : Reducer
{
internal static readonly Reducer Instance = new CountReducer();
private CountReducer() : base(null) { }
public override string Name => "COUNT";
}
sealed class SingleFieldReducer : Reducer
private sealed class SingleFieldReducer : Reducer
{
private readonly string _name;
public override string Name { get; }
internal SingleFieldReducer(string name, string field) : base(field)
{
_name = name;
Name = name;
}
public override string Name => _name;
}
public static Reducer CountDistinct(string field) => new SingleFieldReducer("COUNT_DISTINCT", field);
......@@ -41,7 +40,7 @@ internal SingleFieldReducer(string name, string field) : base(field)
public static Reducer Quantile(string field, double percentile) => new QuantileReducer(field, percentile);
sealed class QuantileReducer : Reducer
private sealed class QuantileReducer : Reducer
{
private readonly double _percentile;
public QuantileReducer(string field, double percentile) : base(field)
......@@ -57,7 +56,7 @@ protected override void AddOwnArgs(List<object> args)
public override string Name => "QUANTILE";
}
public static Reducer FirstValue(string field, SortedField sortBy) => new FirstValueReducer(field, sortBy);
sealed class FirstValueReducer : Reducer
private sealed class FirstValueReducer : Reducer
{
private readonly SortedField? _sortBy;
public FirstValueReducer(string field, SortedField? sortBy) : base(field)
......@@ -85,7 +84,7 @@ protected override void AddOwnArgs(List<object> args)
public static Reducer RandomSample(string field, int size) => new RandomSampleReducer(field, size);
sealed class RandomSampleReducer : Reducer
private sealed class RandomSampleReducer : Reducer
{
private readonly int _size;
public RandomSampleReducer(string field, int size) : base(field)
......@@ -100,6 +99,5 @@ protected override void AddOwnArgs(List<object> args)
args.Add(_size.Boxed());
}
}
}
}
......@@ -50,9 +50,11 @@ public ConfiguredIndexOptions(IndexOptions options)
{
_options = options;
}
/// <summary>
/// Set a custom stopword list
/// Set a custom stopword list.
/// </summary>
/// <param name="stopwords">The new stopwords to use.</param>
public ConfiguredIndexOptions SetStopwords(params string[] stopwords)
{
_stopwords = stopwords ?? throw new ArgumentNullException(nameof(stopwords));
......@@ -179,7 +181,6 @@ public async Task<bool> CreateIndexAsync(Schema schema, IndexOptions options)
return (string)await _db.ExecuteAsync("FT.CREATE", args).ConfigureAwait(false) == "OK";
}
/// <summary>
/// Create the index definition in redis
/// </summary>
......@@ -435,7 +436,6 @@ public async Task<bool> AddHashAsync(RedisKey docId, double score, bool replace)
private static Dictionary<string, RedisValue> ParseGetInfo(RedisResult value)
{
var res = (RedisResult[])value;
var info = new Dictionary<string, RedisValue>();
for (int i = 0; i < res.Length; i += 2)
......@@ -659,40 +659,47 @@ public async Task<string> ExplainAsync(Query q)
}
/// <summary>
/// Get a document from the index
/// Get a document from the index.
/// </summary>
/// <param name="docId">The document ID to retrieve</param>
/// <param name="docId">The document ID to retrieve.</param>
/// <returns>The document as stored in the index. If the document does not exist, null is returned.</returns>
public Document GetDocument(string docId)
=> Document.Parse(docId, DbSync.Execute("FT.GET", _boxedIndexName, docId));
/// <summary>
/// Get a document from the index
/// Get a document from the index.
/// </summary>
/// <param name="docId">The document ID to retrieve</param>
/// <param name="docId">The document ID to retrieve.</param>
/// <returns>The document as stored in the index. If the document does not exist, null is returned.</returns>
public async Task<Document> GetDocumentAsync(string docId)
=> Document.Parse(docId, await _db.ExecuteAsync("FT.GET", _boxedIndexName, docId));
=> Document.Parse(docId, await _db.ExecuteAsync("FT.GET", _boxedIndexName, docId).ConfigureAwait(false));
/// <summary>
/// Replace specific fields in a document. Unlike #replaceDocument(), fields not present in the field list
/// are not erased, but retained. This avoids reindexing the entire document if the new values are not
/// indexed (though a reindex will happen
/// indexed (though a reindex will happen).
/// </summary>
/// <param name="docId">The ID of the document.</param>
/// <param name="fields">The fields and values to update.</param>
/// <param name="score">The new score of the document.</param>
public bool UpdateDocument(string docId, Dictionary<string, RedisValue> fields, double score = 1.0)
{
var args = BuildAddDocumentArgs(docId, fields, score, false, AddOptions.ReplacementPolicy.Partial, null, null);
return (string)DbSync.Execute("FT.ADD", args) == "OK";
}
/// <summary>
/// Replace specific fields in a document. Unlike #replaceDocument(), fields not present in the field list
/// are not erased, but retained. This avoids reindexing the entire document if the new values are not
/// indexed (though a reindex will happen
/// </summary>
/// <param name="docId">The ID of the document.</param>
/// <param name="fields">The fields and values to update.</param>
/// <param name="score">The new score of the document.</param>
public async Task<bool> UpdateDocumentAsync(string docId, Dictionary<string, RedisValue> fields, double score = 1.0)
{
var args = BuildAddDocumentArgs(docId, fields, score, false, AddOptions.ReplacementPolicy.Partial, null, null);
return ((string)await _db.ExecuteAsync("FT.ADD", args).ConfigureAwait(false) == "OK");
return (string)await _db.ExecuteAsync("FT.ADD", args).ConfigureAwait(false) == "OK";
}
}
}
......@@ -30,7 +30,6 @@ public Document(string id, Dictionary<string, RedisValue> fields, double score,
public IEnumerable<KeyValuePair<string, RedisValue>> GetProperties() => _properties;
public static Document Load(string id, double score, byte[] payload, RedisValue[] fields)
{
Document ret = new Document(id, score, payload);
......@@ -57,7 +56,7 @@ internal static Document Parse(string docId, RedisResult result)
if (result == null || result.IsNull) return null;
var arr = (RedisResult[])result;
var doc = new Document(docId);
for(int i = 0; i < arr.Length; )
{
doc[(string)arr[i++]] = (RedisValue)arr[i++];
......
......@@ -12,12 +12,13 @@ public static class Extensions
/// <summary>
/// Set a custom stopword list
/// </summary>
/// <param name="options">The <see cref="IndexOptions"/> to set stopwords on.</param>
/// <param name="stopwords">The stopwords to set.</param>
public static ConfiguredIndexOptions SetStopwords(this IndexOptions options, params string[] stopwords)
=> new ConfiguredIndexOptions(options).SetStopwords(stopwords);
internal static string AsRedisString(this double value, bool forceDecimal = false)
{
if (double.IsNegativeInfinity(value))
{
return "-inf";
......
......@@ -38,13 +38,13 @@ public static object Literal(this string value)
return boxed;
}
const int BOXED_MIN = -1, BOXED_MAX = 20;
static readonly object[] s_Boxed = Enumerable.Range(BOXED_MIN, BOXED_MAX - BOXED_MIN).Select(i => (object)i).ToArray();
private const int BOXED_MIN = -1, BOXED_MAX = 20;
private static readonly object[] s_Boxed = Enumerable.Range(BOXED_MIN, BOXED_MAX - BOXED_MIN).Select(i => (object)i).ToArray();
/// <summary>
/// Obtain a pre-boxed integer if possible, else box the inbound value
/// </summary>
/// <param name="value">The value to get a pre-boxed integer for.</param>
public static object Boxed(this int value) => value >= BOXED_MIN && value <= BOXED_MAX ? s_Boxed[value - BOXED_MIN] : value;
}
}
This diff is collapsed.
......@@ -74,20 +74,20 @@ protected bool ShouldUseParens(ParenMode mode)
}
}
public virtual string ToString(ParenMode parenMode)
public virtual string ToString(ParenMode mode)
{
StringBuilder sb = new StringBuilder();
if (ShouldUseParens(parenMode))
if (ShouldUseParens(mode))
{
sb.Append("(");
}
var sj = new StringJoiner(sb, GetJoinString());
foreach (var n in children)
{
sj.Add(n.ToString(parenMode));
sj.Add(n.ToString(mode));
}
if (ShouldUseParens(parenMode))
if (ShouldUseParens(mode))
{
sb.Append(")");
}
......
......@@ -6,7 +6,7 @@ namespace NRediSearch.QueryBuilder
{
public sealed class RangeValue : Value
{
private double from, to;
private readonly double from, to;
private bool inclusiveMin = true, inclusiveMax = true;
public override bool IsCombinable() => false;
......
......@@ -6,9 +6,9 @@ namespace NRediSearch.QueryBuilder
{
internal ref struct StringJoiner // this is to replace a Java feature cleanly
{
readonly StringBuilder _sb;
readonly string _delimiter;
bool _isFirst;
private readonly StringBuilder _sb;
private readonly string _delimiter;
private bool _isFirst;
public StringJoiner(StringBuilder sb, string delimiter)
{
_sb = sb;
......
......@@ -16,7 +16,7 @@ public ValueNode(string field, string joinstr, params Value[] values)
_joinString = joinstr;
}
private static Value[] fromStrings(string[] values)
private static Value[] FromStrings(string[] values)
{
Value[] objs = new Value[values.Length];
for (int i = 0; i < values.Length; i++)
......@@ -27,7 +27,7 @@ private static Value[] fromStrings(string[] values)
}
public ValueNode(string field, string joinstr, params string[] values)
: this(field, joinstr, fromStrings(values)) { }
: this(field, joinstr, FromStrings(values)) { }
private string FormatField()
{
......@@ -69,7 +69,7 @@ private string ToStringDefault(ParenMode mode)
var sj = new StringJoiner(sb, _joinString);
foreach (var v in _values)
{
sj.Add(FormatField() + v.ToString());
sj.Add(FormatField() + v);
}
if (useParen)
{
......
......@@ -23,7 +23,6 @@ public ValueValue(string s)
internal static Value[] Value(string[] s) => Array.ConvertAll(s, _ => Value(_));
public static RangeValue Between(double from, double to) => new RangeValue(from, to);
public static RangeValue Between(int from, int to) => new RangeValue((double)from, (double)to);
......@@ -47,7 +46,7 @@ public static Value Tags(params string[] tags)
}
return new TagValue("{" + string.Join(" | ", tags) + "}");
}
sealed class TagValue : Value
private sealed class TagValue : Value
{
private readonly string s;
public TagValue(string s) { this.s = s; }
......
......@@ -80,9 +80,10 @@ internal override void SerializeRedisArgs(List<object> args)
public List<Field> Fields { get; } = new List<Field>();
/// <summary>
/// Add a field to the schema
/// Add a field to the schema.
/// </summary>
/// <returns>the schema object</returns>
/// <param name="field">The <see cref="Field"/> to add.</param>
/// <returns>The <see cref="Schema"/> object.</returns>
public Schema AddField(Field field)
{
Fields.Add(field ?? throw new ArgumentNullException(nameof(field)));
......@@ -90,11 +91,11 @@ public Schema AddField(Field field)
}
/// <summary>
/// Add a text field to the schema with a given weight
/// Add a text field to the schema with a given weight.
/// </summary>
/// <param name="name">the field's name</param>
/// <param name="weight">its weight, a positive floating point number</param>
/// <returns>the schema object</returns>
/// <param name="name">The field's name.</param>
/// <param name="weight">Its weight, a positive floating point number.</param>
/// <returns>The <see cref="Schema"/> object.</returns>
public Schema AddTextField(string name, double weight = 1.0)
{
Fields.Add(new TextField(name, weight));
......@@ -102,11 +103,11 @@ public Schema AddTextField(string name, double weight = 1.0)
}
/// <summary>
/// Add a text field that can be sorted on
/// Add a text field that can be sorted on.
/// </summary>
/// <param name="name">the field's name</param>
/// <param name="weight">its weight, a positive floating point number</param>
/// <returns>the schema object</returns>
/// <param name="name">The field's name.</param>
/// <param name="weight">Its weight, a positive floating point number.</param>
/// <returns>The <see cref="Schema"/> object.</returns>
public Schema AddSortableTextField(string name, double weight = 1.0)
{
Fields.Add(new TextField(name, weight, true));
......@@ -114,10 +115,10 @@ public Schema AddSortableTextField(string name, double weight = 1.0)
}
/// <summary>
/// Add a numeric field to the schema
/// Add a numeric field to the schema.
/// </summary>
/// <param name="name">the field's name</param>
/// <returns>the schema object</returns>
/// <param name="name">The field's name.</param>
/// <returns>The <see cref="Schema"/> object.</returns>
public Schema AddGeoField(string name)
{
Fields.Add(new Field(name, FieldType.Geo, false));
......@@ -125,10 +126,10 @@ public Schema AddGeoField(string name)
}
/// <summary>
/// Add a numeric field to the schema
/// Add a numeric field to the schema.
/// </summary>
/// <param name="name">the field's name</param>
/// <returns>the schema object</returns>
/// <param name="name">The field's name.</param>
/// <returns>The <see cref="Schema"/> object.</returns>
public Schema AddNumericField(string name)
{
Fields.Add(new Field(name, FieldType.Numeric, false));
......@@ -136,10 +137,10 @@ public Schema AddNumericField(string name)
}
/// <summary>
/// Add a numeric field that can be sorted on
/// Add a numeric field that can be sorted on.
/// </summary>
/// <param name="name">the field's name</param>
/// <returns>the schema object</returns>
/// <param name="name">The field's name.</param>
/// <returns>The <see cref="Schema"/> object.</returns>
public Schema AddSortableNumericField(string name)
{
Fields.Add(new Field(name, FieldType.Numeric, true));
......@@ -166,11 +167,11 @@ internal override void SerializeRedisArgs(List<object> args)
}
/// <summary>
/// Add a TAG field
/// Add a TAG field.
/// </summary>
/// <param name="name">the field's name</param>
/// <param name="separator">tag separator</param>
/// <returns>the schema object</returns>
/// <param name="name">The field's name.</param>
/// <param name="separator">The tag separator.</param>
/// <returns>The <see cref="Schema"/> object.</returns>
public Schema AddTagField(string name, string separator = ",")
{
Fields.Add(new TagField(name, separator));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment