Skip to content

Commit 41fd4a6

Browse files
committed
Client should only *demand* IDatabaseAsync, but *allow* IDatabase
1 parent ddbcdb8 commit 41fd4a6

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

NRediSearch/Client.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,18 @@ private static void SerializeRedisArgs(IndexOptions flags, List<object> args)
5151
args.Add("NOSCOREIDX".Literal());
5252
}
5353
}
54-
IDatabase _db;
55-
private object _boxedIndexName;
54+
private readonly IDatabaseAsync _db;
55+
private IDatabase DbSync
56+
=> (_db as IDatabase) ?? throw new InvalidOperationException("Synchronous operations are not available on this database instance");
57+
58+
private readonly object _boxedIndexName;
5659
public RedisKey IndexName => (RedisKey)_boxedIndexName;
57-
public Client(RedisKey indexName, IDatabase db)
60+
public Client(RedisKey indexName, IDatabaseAsync db)
5861
{
59-
_db = db;
62+
_db = db ?? throw new ArgumentNullException(nameof(db));
6063
_boxedIndexName = indexName; // only box once, not per-command
6164
}
65+
public Client(RedisKey indexName, IDatabase db) : this(indexName, (IDatabaseAsync)db) { }
6266

6367
/// <summary>
6468
/// Create the index definition in redis
@@ -79,7 +83,7 @@ public bool CreateIndex(Schema schema, IndexOptions options)
7983
f.SerializeRedisArgs(args);
8084
}
8185

82-
return (string)_db.Execute("FT.CREATE", args) == "OK";
86+
return (string)DbSync.Execute("FT.CREATE", args) == "OK";
8387
}
8488

8589
/// <summary>
@@ -115,7 +119,7 @@ public SearchResult Search(Query q)
115119
args.Add(_boxedIndexName);
116120
q.SerializeRedisArgs(args);
117121

118-
var resp = (RedisResult[])_db.Execute("FT.SEARCH", args);
122+
var resp = (RedisResult[])DbSync.Execute("FT.SEARCH", args);
119123
return new SearchResult(resp, !q.NoContent, q.WithScores, q.WithPayloads);
120124
}
121125

@@ -146,7 +150,7 @@ public async Task<SearchResult> SearchAsync(Query q)
146150
public bool AddDocument(string docId, Dictionary<string, RedisValue> fields, double score = 1.0, bool noSave = false, bool replace = false, byte[] payload = null)
147151
{
148152
var args = BuildAddDocumentArgs(docId, fields, score, noSave, replace, payload);
149-
return (string)_db.Execute("FT.ADD", args) == "OK";
153+
return (string)DbSync.Execute("FT.ADD", args) == "OK";
150154
}
151155

152156
/// <summary>
@@ -217,7 +221,7 @@ public bool AddHash(string docId, double score, bool replace)
217221
{
218222
args.Add("REPLACE".Literal());
219223
}
220-
return (string)_db.Execute("FT.ADDHASH", args) == "OK";
224+
return (string)DbSync.Execute("FT.ADDHASH", args) == "OK";
221225
}
222226
/// <summary>
223227
/// 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)
243247
/// <returns>a map of key/value pairs</returns>
244248
public Dictionary<string, RedisValue> GetInfo()
245249
{
246-
return ParseGetInfo(_db.Execute("FT.INFO", _boxedIndexName));
250+
return ParseGetInfo(DbSync.Execute("FT.INFO", _boxedIndexName));
247251
}
248252
/// <summary>
249253
/// Get the index info, including memory consumption and other statistics.
@@ -274,7 +278,7 @@ static Dictionary<string, RedisValue> ParseGetInfo(RedisResult value)
274278
/// <returns>true if it has been deleted, false if it did not exist</returns>
275279
public bool DeleteDocument(string docId)
276280
{
277-
return (long)_db.Execute("FT.DEL", _boxedIndexName, docId) == 1;
281+
return (long)DbSync.Execute("FT.DEL", _boxedIndexName, docId) == 1;
278282
}
279283

280284
/// <summary>
@@ -293,7 +297,7 @@ public async Task<bool> DeleteDocumentAsync(string docId)
293297
/// <returns>true on success</returns>
294298
public bool DropIndex()
295299
{
296-
return (string)_db.Execute("FT.DROP", _boxedIndexName) == "OK";
300+
return (string)DbSync.Execute("FT.DROP", _boxedIndexName) == "OK";
297301
}
298302
/// <summary>
299303
/// Drop the index and all associated keys, including documents
@@ -309,7 +313,7 @@ public async Task<bool> DropIndexAsync()
309313
/// </summary>
310314
public long OptimizeIndex()
311315
{
312-
return (long)_db.Execute("FT.OPTIMIZE", _boxedIndexName);
316+
return (long)DbSync.Execute("FT.OPTIMIZE", _boxedIndexName);
313317
}
314318

315319
/// <summary>

0 commit comments

Comments
 (0)