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