Commit 79c023b4 authored by Marc Gravell's avatar Marc Gravell

Allow Execute to take lists rather than just arrays - avoids having to call ToArray()

parent bd20a4bc
......@@ -78,7 +78,7 @@ public bool CreateIndex(Schema schema, IndexOptions options)
f.SerializeRedisArgs(args);
}
return (string)_db.Execute("FT.CREATE", args.ToArray()) == "OK";
return (string)_db.Execute("FT.CREATE", args) == "OK";
}
/// <summary>
......@@ -92,7 +92,7 @@ public SearchResult Search(Query q)
args.Add(_boxedIndexName);
q.SerializeRedisArgs(args);
var resp = (RedisResult[])_db.Execute("FT.SEARCH", args.ToArray());
var resp = (RedisResult[])_db.Execute("FT.SEARCH", args);
return new SearchResult(resp, !q.NoContent, q.WithScores, q.WithPayloads);
}
......@@ -105,7 +105,7 @@ public SearchResult Search(Query q)
/// <param name="noSave">if set, we only index the document and do not save its contents. This allows fetching just doc ids</param>
/// <param name="replace">if set, and the document already exists, we reindex and update it</param>
/// <param name="payload">if set, we can save a payload in the index to be retrieved or evaluated by scoring functions on the server</param>
public bool AddDocument(string docId, double score, Dictionary<string, RedisValue> fields, bool noSave, bool replace, byte[] payload)
public bool AddDocument(string docId, Dictionary<string, RedisValue> fields, double score = 1.0, bool noSave = false, bool replace = false, byte[] payload = null)
{
var args = new List<object> { _boxedIndexName, docId, score };
if (noSave)
......@@ -130,21 +130,14 @@ public bool AddDocument(string docId, double score, Dictionary<string, RedisValu
args.Add(ent.Value);
}
return (string)_db.Execute("FT.ADD", args.ToArray()) == "OK";
return (string)_db.Execute("FT.ADD", args) == "OK";
}
/// <summary>
/// replaceDocument is a convenience for calling addDocument with replace=true
/// </summary>
public bool ReplaceDocument(string docId, double score, Dictionary<string, RedisValue> fields)
=> AddDocument(docId, score, fields, false, true, null);
/** See above */
public bool AddDocument(string docId, double score, Dictionary<string, RedisValue> fields)
=> AddDocument(docId, score, fields, false, false, null);
/** See above */
public bool AddDocument(string docId, Dictionary<string, RedisValue> fields)
=> AddDocument(docId, 1, fields, false, false, null);
public bool ReplaceDocument(string docId, Dictionary<string, RedisValue> fields, double score = 1.0, byte[] payload = null)
=> AddDocument(docId, fields, score, false, true, payload);
/// <summary>
/// Index a document already in redis as a HASH key.
......@@ -162,7 +155,7 @@ public bool AddHash(string docId, double score, bool replace)
args.Add("REPLACE".Literal());
}
return (string)_db.Execute("FT.ADDHASH", args.ToArray()) == "OK";
return (string)_db.Execute("FT.ADDHASH", args) == "OK";
}
/// <summary>
......@@ -191,8 +184,7 @@ public bool AddHash(string docId, double score, bool replace)
/// <returns>true if it has been deleted, false if it did not exist</returns>
public bool DeleteDocument(string docId)
{
long r = (long)_db.Execute("FT.DEL", _boxedIndexName, docId);
return r == 1;
return (long)_db.Execute("FT.DEL", _boxedIndexName, docId) == 1;
}
/// <summary>
......@@ -209,8 +201,7 @@ public bool DropIndex()
/// </summary>
public long OptimizeIndex()
{
long ret = (long)_db.Execute("FT.OPTIMIZE", _boxedIndexName);
return ret;
return (long)_db.Execute("FT.OPTIMIZE", _boxedIndexName);
}
}
}
......@@ -547,7 +547,7 @@ public interface IDatabase : IRedis, IDatabaseAsync
/// a direct API
/// </summary>
/// <returns>A dynamic representation of the command's result</returns>
RedisResult Execute(string command, object[] args, CommandFlags flags = CommandFlags.None);
RedisResult Execute(string command, ICollection<object> args, CommandFlags flags = CommandFlags.None);
/// <summary>
......
......@@ -363,7 +363,7 @@ public long Publish(RedisChannel channel, RedisValue message, CommandFlags flags
public RedisResult Execute(string command, params object[] args)
=> Execute(command, args, CommandFlags.None);
public RedisResult Execute(string command, object[] args, CommandFlags flags = CommandFlags.None)
public RedisResult Execute(string command, ICollection<object> args, CommandFlags flags = CommandFlags.None)
{
return Inner.Execute(command, ToInner(args), flags);
}
......
......@@ -713,23 +713,28 @@ protected RedisKey ToInnerOrDefault(RedisKey outer)
return ToInner(outer);
}
}
protected object[] ToInner(object[] args)
protected ICollection<object> ToInner(ICollection<object> args)
{
if (args != null && args.Any(x => x is RedisKey || x is RedisChannel))
{
var withPrefix = new object[args.Length];
for (int i = 0; i < args.Length; i++)
var withPrefix = new object[args.Count];
int i = 0;
foreach(var oldArg in args)
{
var arg = args[i];
if (arg is RedisKey)
object newArg;
if (oldArg is RedisKey)
{
arg = ToInner((RedisKey)arg);
newArg = ToInner((RedisKey)oldArg);
}
else if (arg is RedisChannel)
else if (oldArg is RedisChannel)
{
arg = ToInner((RedisChannel)arg);
newArg = ToInner((RedisChannel)oldArg);
}
withPrefix[i] = arg;
else
{
newArg = oldArg;
}
withPrefix[i++] = newArg;
}
args = withPrefix;
}
......
......@@ -1061,7 +1061,7 @@ public RedisResult ScriptEvaluate(string script, RedisKey[] keys = null, RedisVa
}
public RedisResult Execute(string command, params object[] args)
=> Execute(command, args, CommandFlags.None);
public RedisResult Execute(string command, object[] args, CommandFlags flags = CommandFlags.None)
public RedisResult Execute(string command, ICollection<object> args, CommandFlags flags = CommandFlags.None)
{
var msg = new ExecuteMessage(Database, flags, command, args);
return ExecuteSync(msg, ResultProcessor.ScriptResult);
......@@ -2447,18 +2447,17 @@ private sealed class ExecuteMessage : Message
{
private readonly string _command;
private static readonly object[] NoArgs = new object[0];
private readonly object[] args;
public ExecuteMessage(int db, CommandFlags flags, string command, object[] args) : base(db, flags, RedisCommand.UNKNOWN)
private readonly ICollection<object> args;
public ExecuteMessage(int db, CommandFlags flags, string command, ICollection<object> args) : base(db, flags, RedisCommand.UNKNOWN)
{
_command = command;
this.args = args ?? NoArgs;
}
internal override void WriteImpl(PhysicalConnection physical)
{
physical.WriteHeader(_command, args.Length);
for(int i = 0; i < args.Length; i++)
physical.WriteHeader(_command, args.Count);
foreach(object arg in args)
{
object arg = args[i];
if (arg is RedisKey)
{
physical.Write((RedisKey)arg);
......@@ -2478,9 +2477,8 @@ internal override void WriteImpl(PhysicalConnection physical)
public override int GetHashSlot(ServerSelectionStrategy serverSelectionStrategy)
{
int slot = ServerSelectionStrategy.NoSlot;
for (int i = 0 ; i < args.Length ; i++)
foreach(object arg in args)
{
object arg = args[i];
if(arg is RedisKey)
{
slot = serverSelectionStrategy.CombineSlot(slot, (RedisKey)arg);
......
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