Commit 41fd4a6b authored by Marc Gravell's avatar Marc Gravell

Client should only *demand* IDatabaseAsync, but *allow* IDatabase

parent ddbcdb83
...@@ -51,14 +51,18 @@ private static void SerializeRedisArgs(IndexOptions flags, List<object> args) ...@@ -51,14 +51,18 @@ private static void SerializeRedisArgs(IndexOptions flags, List<object> args)
args.Add("NOSCOREIDX".Literal()); args.Add("NOSCOREIDX".Literal());
} }
} }
IDatabase _db; private readonly IDatabaseAsync _db;
private object _boxedIndexName; private IDatabase DbSync
=> (_db as IDatabase) ?? throw new InvalidOperationException("Synchronous operations are not available on this database instance");
private readonly object _boxedIndexName;
public RedisKey IndexName => (RedisKey)_boxedIndexName; public RedisKey IndexName => (RedisKey)_boxedIndexName;
public Client(RedisKey indexName, IDatabase db) public Client(RedisKey indexName, IDatabaseAsync db)
{ {
_db = db; _db = db ?? throw new ArgumentNullException(nameof(db));
_boxedIndexName = indexName; // only box once, not per-command _boxedIndexName = indexName; // only box once, not per-command
} }
public Client(RedisKey indexName, IDatabase db) : this(indexName, (IDatabaseAsync)db) { }
/// <summary> /// <summary>
/// Create the index definition in redis /// Create the index definition in redis
...@@ -79,7 +83,7 @@ public bool CreateIndex(Schema schema, IndexOptions options) ...@@ -79,7 +83,7 @@ public bool CreateIndex(Schema schema, IndexOptions options)
f.SerializeRedisArgs(args); f.SerializeRedisArgs(args);
} }
return (string)_db.Execute("FT.CREATE", args) == "OK"; return (string)DbSync.Execute("FT.CREATE", args) == "OK";
} }
/// <summary> /// <summary>
...@@ -115,7 +119,7 @@ public SearchResult Search(Query q) ...@@ -115,7 +119,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); var resp = (RedisResult[])DbSync.Execute("FT.SEARCH", args);
return new SearchResult(resp, !q.NoContent, q.WithScores, q.WithPayloads); return new SearchResult(resp, !q.NoContent, q.WithScores, q.WithPayloads);
} }
...@@ -146,7 +150,7 @@ public async Task<SearchResult> SearchAsync(Query q) ...@@ -146,7 +150,7 @@ public async Task<SearchResult> SearchAsync(Query q)
public bool AddDocument(string docId, Dictionary<string, RedisValue> fields, double score = 1.0, bool noSave = false, bool replace = false, byte[] payload = null) public bool AddDocument(string docId, Dictionary<string, RedisValue> fields, double score = 1.0, bool noSave = false, bool replace = false, byte[] payload = null)
{ {
var args = BuildAddDocumentArgs(docId, fields, score, noSave, replace, payload); var args = BuildAddDocumentArgs(docId, fields, score, noSave, replace, payload);
return (string)_db.Execute("FT.ADD", args) == "OK"; return (string)DbSync.Execute("FT.ADD", args) == "OK";
} }
/// <summary> /// <summary>
...@@ -217,7 +221,7 @@ public bool AddHash(string docId, double score, bool replace) ...@@ -217,7 +221,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) == "OK"; return (string)DbSync.Execute("FT.ADDHASH", args) == "OK";
} }
/// <summary> /// <summary>
/// Index a document already in redis as a HASH key. /// Index a document already in redis as a HASH key.
...@@ -243,7 +247,7 @@ public async Task<bool> AddHashAsync(string docId, double score, bool replace) ...@@ -243,7 +247,7 @@ public async Task<bool> AddHashAsync(string docId, double score, bool replace)
/// <returns>a map of key/value pairs</returns> /// <returns>a map of key/value pairs</returns>
public Dictionary<string, RedisValue> GetInfo() public Dictionary<string, RedisValue> GetInfo()
{ {
return ParseGetInfo(_db.Execute("FT.INFO", _boxedIndexName)); return ParseGetInfo(DbSync.Execute("FT.INFO", _boxedIndexName));
} }
/// <summary> /// <summary>
/// Get the index info, including memory consumption and other statistics. /// Get the index info, including memory consumption and other statistics.
...@@ -274,7 +278,7 @@ public async Task<bool> AddHashAsync(string docId, double score, bool replace) ...@@ -274,7 +278,7 @@ public async Task<bool> AddHashAsync(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)
{ {
return (long)_db.Execute("FT.DEL", _boxedIndexName, docId) == 1; return (long)DbSync.Execute("FT.DEL", _boxedIndexName, docId) == 1;
} }
/// <summary> /// <summary>
...@@ -293,7 +297,7 @@ public async Task<bool> DeleteDocumentAsync(string docId) ...@@ -293,7 +297,7 @@ public async Task<bool> DeleteDocumentAsync(string docId)
/// <returns>true on success</returns> /// <returns>true on success</returns>
public bool DropIndex() public bool DropIndex()
{ {
return (string)_db.Execute("FT.DROP", _boxedIndexName) == "OK"; return (string)DbSync.Execute("FT.DROP", _boxedIndexName) == "OK";
} }
/// <summary> /// <summary>
/// Drop the index and all associated keys, including documents /// Drop the index and all associated keys, including documents
...@@ -309,7 +313,7 @@ public async Task<bool> DropIndexAsync() ...@@ -309,7 +313,7 @@ public async Task<bool> DropIndexAsync()
/// </summary> /// </summary>
public long OptimizeIndex() public long OptimizeIndex()
{ {
return (long)_db.Execute("FT.OPTIMIZE", _boxedIndexName); return (long)DbSync.Execute("FT.OPTIMIZE", _boxedIndexName);
} }
/// <summary> /// <summary>
......
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