Skip to content

Commit dc2e33e

Browse files
authored
Renaming Async for bulk operations for Insertion (#338)
1 parent b95ac22 commit dc2e33e

File tree

4 files changed

+77
-28
lines changed

4 files changed

+77
-28
lines changed

src/Redis.OM/Redis.OM.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
<RootNamespace>Redis.OM</RootNamespace>
77
<Nullable>enable</Nullable>
88
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
9-
<PackageVersion>0.4.2</PackageVersion>
10-
<Version>0.4.2</Version>
11-
<PackageReleaseNotes>https://github.com/redis/redis-om-dotnet/releases/tag/v0.4.2</PackageReleaseNotes>
9+
<PackageVersion>0.5.0</PackageVersion>
10+
<Version>0.5.0</Version>
11+
<PackageReleaseNotes>https://github.com/redis/redis-om-dotnet/releases/tag/v0.5.0</PackageReleaseNotes>
1212
<Description>Object Mapping and More for Redis</Description>
1313
<Title>Redis OM</Title>
1414
<Authors>Steve Lorello</Authors>

src/Redis.OM/Searching/IRedisCollection.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,24 @@ public interface IRedisCollection<T> : IOrderedQueryable<T>, IAsyncEnumerable<T>
9292
/// </summary>
9393
/// <param name="items">The items to insert.</param>
9494
/// <returns>The list of Keys.</returns>
95-
Task<List<string>> Insert(IEnumerable<T> items);
95+
Task<List<string>> InsertAsync(IEnumerable<T> items);
9696

9797
/// <summary>
9898
/// Inserts list of items into redis.
9999
/// </summary>
100100
/// <param name="items">The items to insert.</param>
101101
/// <param name="timeSpan">The timespan of the document's (TTL).</param>
102102
/// /// <returns>The list of Keys.</returns>
103-
Task<List<string>> Insert(IEnumerable<T> items, TimeSpan timeSpan);
103+
Task<List<string>> InsertAsync(IEnumerable<T> items, TimeSpan timeSpan);
104+
105+
/// <summary>
106+
/// Inserts list of items into redis.
107+
/// </summary>
108+
/// <param name="items">The item.</param>
109+
/// <param name="when">Condition to insert the document under.</param>
110+
/// <param name="timeSpan">The expiration time of the document (TTL).</param>
111+
/// <returns>the Id of the newly inserted item, or null if not inserted.</returns>
112+
Task<List<string?>> InsertAsync(IEnumerable<T> items, WhenKey when, TimeSpan? timeSpan = null);
104113

105114
/// <summary>
106115
/// finds an item by it's ID or keyname.
@@ -302,4 +311,4 @@ public interface IRedisCollection<T> : IOrderedQueryable<T>, IAsyncEnumerable<T>
302311
/// <returns>A dictionary correlating the ids provided to the objects in Redis.</returns>
303312
Task<IDictionary<string, T?>> FindByIdsAsync(IEnumerable<string> ids);
304313
}
305-
}
314+
}

src/Redis.OM/Searching/RedisCollection.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ public async Task<string> InsertAsync(T item, TimeSpan timeSpan)
657657
}
658658

659659
/// <inheritdoc/>
660-
public async Task<List<string>> Insert(IEnumerable<T> items)
660+
public async Task<List<string>> InsertAsync(IEnumerable<T> items)
661661
{
662662
var distinct = items.Distinct().ToArray();
663663
if (!distinct.Any())
@@ -676,7 +676,7 @@ public async Task<List<string>> Insert(IEnumerable<T> items)
676676
}
677677

678678
/// <inheritdoc/>
679-
public async Task<List<string>> Insert(IEnumerable<T> items, TimeSpan timeSpan)
679+
public async Task<List<string>> InsertAsync(IEnumerable<T> items, TimeSpan timeSpan)
680680
{
681681
var distinct = items.Distinct().ToArray();
682682
if (!distinct.Any())
@@ -694,6 +694,25 @@ public async Task<List<string>> Insert(IEnumerable<T> items, TimeSpan timeSpan)
694694
return result.ToList();
695695
}
696696

697+
/// <inheritdoc/>
698+
public async Task<List<string?>> InsertAsync(IEnumerable<T> items, WhenKey when, TimeSpan? timeSpan = null)
699+
{
700+
var distinct = items.Distinct().ToArray();
701+
if (!distinct.Any())
702+
{
703+
return new List<string?>();
704+
}
705+
706+
var tasks = new List<Task<string?>>();
707+
foreach (var item in distinct)
708+
{
709+
tasks.Add(((RedisQueryProvider)Provider).Connection.SetAsync(item, when, timeSpan));
710+
}
711+
712+
var result = await Task.WhenAll(tasks);
713+
return result.ToList();
714+
}
715+
697716
/// <inheritdoc/>
698717
public T? FindById(string id)
699718
{
@@ -800,4 +819,4 @@ private void SaveToStateManager(string key, object value)
800819
}
801820
}
802821
}
803-
}
822+
}

test/Redis.OM.Unit.Tests/RediSearchTests/BulkOperationsTests.cs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using Xunit;
89

@@ -15,6 +16,7 @@ public BulkOperationsTests(RedisSetup setup)
1516
{
1617
_connection = setup.Connection;
1718
}
19+
1820
private IRedisConnection _connection = null;
1921

2022
[Fact]
@@ -27,7 +29,7 @@ public async Task Test_Bulk_InsertAsync()
2729
new Person() { Name = "Jeeva", Age = 22, NickNames = new[] { "Jee", "Jeev", "J" } },
2830
new Person() { Name = "Martin", Age = 60, NickNames = new[] { "Mart", "Mat", "tin" } }
2931
};
30-
var keys = await collection.Insert(persons);
32+
var keys = await collection.InsertAsync(persons);
3133

3234
var people = collection.Where(x => x.NickNames.Contains("Bob") || x.NickNames.Contains("Alie")).ToList();
3335
Assert.Contains(people, x => x.Name == persons.First().Name);
@@ -43,7 +45,7 @@ public async Task Test_Inserts_TwiceWith_SaveDataWith_ExactFields()
4345
new Person() { Name = "Jeeva", Age = 22, NickNames = new[] { "Jee", "Jeev", "J" } },
4446
new Person() { Name = "Martin", Age = 61, NickNames = new[] { "Mart", "Mat", "tin" } }
4547
};
46-
var keys = await collection.Insert(persons); //performs JSON.SET create keys and emit the list of keys.
48+
var keys = await collection.InsertAsync(persons); //performs JSON.SET create keys and emit the list of keys.
4749

4850
var persons2 = new List<Person>() {
4951
new Person() { Name = "Alice", Age = 14, NickNames = new[] { "Ally", "Alie", "Al" }, IsEngineer = true },
@@ -52,9 +54,9 @@ public async Task Test_Inserts_TwiceWith_SaveDataWith_ExactFields()
5254
new Person() { Name = "Martin", Age = 61, NickNames = new[] { "Mart", "Mat", "tin" }, TagField = "Martin" }
5355
};
5456

55-
var keys2 = await collection.Insert(persons2); //create keys and emit the list of keys.
57+
var keys2 = await collection.InsertAsync(persons2); //create keys and emit the list of keys.
5658

57-
var people = collection.Where(x => x.Age >= 20 && x.Age <=30).ToList();
59+
var people = collection.Where(x => x.Age >= 20 && x.Age <= 30).ToList();
5860
Assert.NotEqual(keys, keys2); //not performs any re-indexing because keys are not same.
5961
}
6062

@@ -68,7 +70,7 @@ public async Task Test_BulkInsert_WithSameIds()
6870
new Person() { Name = "Jeeva", Age = 22, NickNames = new[] { "Jee", "Jeev", "J" }, },
6971
new Person() { Name = "Martin", Age = 60, NickNames = new[] { "Mart", "Mat", "tin" }, }
7072
};
71-
await collection.Insert(persons);
73+
await collection.InsertAsync(persons);
7274
var people = collection.Where(x => x.NickNames.Contains("Jeeva") || x.NickNames.Contains("Alie")).ToList();
7375
Assert.False(people.First().Name == persons.First().Name); // this fails because the Name field of people doesn't contains the Name value Alice
7476
}
@@ -82,7 +84,7 @@ public async Task Test_BulkInsert_HashesWith_Expiration()
8284
new HashPerson() { Name = "Phineas", Age = 14, IsEngineer = true, TagField = "SummerVacation" }
8385
};
8486

85-
await collection.Insert(PhineasFerb, TimeSpan.FromMilliseconds(8000));
87+
await collection.InsertAsync(PhineasFerb, TimeSpan.FromMilliseconds(8000));
8688
var ttl = (long)_connection.Execute("PTTL", PhineasFerb[0].GetKey());
8789
Assert.True(ttl <= 8000);
8890
Assert.True(ttl >= 1000);
@@ -97,7 +99,7 @@ public async Task Test_BulkInsert_WithExpiration()
9799
new Person() { Name = "Phineas", Age = 14, IsEngineer = true, TagField = "SummerVacation", NickNames = new[] { "Phineas", "Triangle Head", "Phine" } }
98100
};
99101

100-
await collection.Insert(PhineasFerb, TimeSpan.FromSeconds(8));
102+
await collection.InsertAsync(PhineasFerb, TimeSpan.FromSeconds(8));
101103
var ttl = (long)_connection.Execute("PTTL", PhineasFerb[0].GetKey());
102104
Assert.True(ttl <= 8000);
103105
Assert.True(ttl >= 1000);
@@ -114,13 +116,33 @@ public async Task Test_Bulk_Insert_Del()
114116
new Person() { Name = "Perry", Age = 5, IsEngineer = false, TagField = "Agent", Address = new Address { State = "Tri-State Area "} }
115117
};
116118

117-
await collection.Insert(PhineasFerbShow);
119+
await collection.InsertAsync(PhineasFerbShow);
118120
var searchByState = collection.Where(x => x.Address.State == "Tri-State Area").ToList();
119121
await collection.DeleteAsync(searchByState);
120122
var searchByTag = collection.FindById(searchByState[0].GetKey());
121123
Assert.Null(searchByTag);
122124
}
123125

126+
[Fact]
127+
public async Task Test_Bulk_Insert_WithWhen()
128+
{
129+
var collection = new RedisCollection<Person>(_connection);
130+
var PhineasFerbShow = new List<Person>() {
131+
new Person() { Id="1", Name = "Ferb", Age = 14, IsEngineer = true, TagField = "SummerVacation" , Address = new Address { State = "Tri-State Area"} },
132+
new Person() {Id="2", Name = "Phineas", Age = 14, IsEngineer = true, TagField = "SummerVacation", Address = new Address { State = "Tri-State Area"} },
133+
new Person() {Id="3", Name = "Dr.Doofenshmirtz", Age = 38, IsEngineer = true, TagField = "Villain", Address = new Address { State = "Tri-State Area"} },
134+
new Person() {Id="4", Name = "Perry", Age = 5, IsEngineer = false, TagField = "Agent", Address = new Address { State = "Tri-State Area "} }
135+
};
136+
137+
var res = await collection.InsertAsync(PhineasFerbShow, WhenKey.NotExists, TimeSpan.FromMilliseconds(10000));
138+
Assert.True(res.All(x => x != null));
139+
Thread.Sleep(1100);
140+
res = await collection.InsertAsync(PhineasFerbShow, WhenKey.NotExists, TimeSpan.FromMilliseconds(5000));
141+
Assert.True(res.All(x => x == null));
142+
res = await collection.InsertAsync(PhineasFerbShow, WhenKey.Always, TimeSpan.FromMilliseconds(6000));
143+
Assert.True(res.All(x => x != null));
144+
}
145+
124146
[Fact]
125147
public async Task Test_Bulk_InsertAsync_DelAsync_ForHashes()
126148
{
@@ -132,7 +154,7 @@ public async Task Test_Bulk_InsertAsync_DelAsync_ForHashes()
132154
new HashPerson() { Name = "Perry", Age = 5, IsEngineer = false, TagField = "Agent", Address = new Address { State = "Tri-State Area "} }
133155
};
134156

135-
await collection.Insert(PhineasFerbShow);
157+
await collection.InsertAsync(PhineasFerbShow);
136158
var searchByName = await collection.Where(x => x.Name == "Dr.Doofenshmirtz" || x.Name == "Perry").ToListAsync();
137159
await collection.DeleteAsync(searchByName);
138160
var searchByTag = await collection.FindByIdAsync(searchByName[0].GetKey());
@@ -149,7 +171,7 @@ public async Task Test_Bulk_UpdateAsync()
149171
new Person() { Name = "Monkey D. Garp", Age = 70, NickNames = new[] { "Garp", "Garps", "Hero of the Navy" }, TagField = "Navy" },
150172
new Person() { Name = "Shanks", Age = 50, NickNames = new[] { "Shanks", "Red-Hair" }, TagField = "Red-Haired Pirates" }
151173
};
152-
var keys = await collection.Insert(onepiece);
174+
var keys = await collection.InsertAsync(onepiece);
153175
var people = collection.Where(x => x.NickNames.Contains("Luffy") || x.NickNames.Contains("Shanks")).ToList();
154176
Assert.Equal(onepiece[0].Age, people[0].Age);
155177
people[0].Age = 25;
@@ -168,7 +190,7 @@ public async Task Test_Bulk_UpdateSync_WithHashesNumeric()
168190
new HashPerson() { Name = "Monkey D. Garp", Age = 70, NickNames = new List<string> { "Garp", "Garps", "Hero of the Navy" }, TagField = "Navy" },
169191
new HashPerson() { Name = "Shanks", Age = 50, NickNames = new List<string> { "Shanks", "Red-Hair" }, TagField = "Red-Haired Pirates" }
170192
};
171-
var keys = collection.Insert(onepiece);
193+
var keys = collection.InsertAsync(onepiece);
172194
var people = collection.Where(x => x.Name.Contains("Luffy") || x.Name.Contains("Shanks")).ToList();
173195
Assert.Equal(onepiece[0].Age, people[0].Age);
174196
people[0].Height = 20.2;
@@ -178,7 +200,6 @@ public async Task Test_Bulk_UpdateSync_WithHashesNumeric()
178200
Assert.NotEqual(onepiece[0].Age, people[0].Age);
179201
}
180202

181-
182203
[Fact]
183204
public async Task Test_BulkUpdate_WithEmbbedObject()
184205
{
@@ -189,11 +210,11 @@ public async Task Test_BulkUpdate_WithEmbbedObject()
189210
new Person() { Name = "Monkey D. Garp", Age = 70, NickNames = new[] { "Garp", "Garps", "Hero of the Navy" }, TagField = "Navy" },
190211
new Person() { Name = "Shanks", Age = 50, NickNames = new[] { "Shanks", "Red-Hair" }, TagField = "Red-Haired Pirates" }
191212
};
192-
var keys = collection.Insert(onepiece);
213+
var keys = collection.InsertAsync(onepiece);
193214
var people = collection.Where(x => x.NickNames.Contains("Luffy") || x.NickNames.Contains("Shanks")).ToList();
194215
people[0].Address = new Address { City = "Goa Kingdom" };
195216
people[1].Address = new Address { City = "Goa Kingdom" };
196-
await collection.UpdateAsync(people);
217+
await collection.UpdateAsync(people);
197218
Assert.Contains(people, x => x.Name == onepiece.First().Name);
198219
}
199220

@@ -218,7 +239,7 @@ public async Task Test_Bulk50_Records_Insert_Update_Del_Async()
218239
}
219240
);
220241
}
221-
var keys = await collection.Insert(people); // 1000 records in an avg of 200ms.
242+
var keys = await collection.InsertAsync(people); // 1000 records in an avg of 200ms.
222243
var listofPeople = (await collection.FindByIdsAsync(keys)).Values.ToList();
223244
for (int i = 0; i < keys.Count; i++)
224245
{
@@ -248,14 +269,14 @@ public async Task TestBulk_Insert_Update_Del_Async_WithHashes()
248269
new HashPerson() { Name = "Perry", Age = 5, IsEngineer = false, TagField = "Agent", Address = new Address { State = "Tri-State Area "} }
249270
};
250271

251-
await collection.Insert(PhineasFerbShow);
252-
var searchByName = await collection.Where(x => x.Name == "Dr.Doofenshmirtz" || x.Name == "Perry").ToListAsync();
272+
await collection.InsertAsync(PhineasFerbShow);
273+
var searchByName = await collection.Where(x => x.Name == "Dr.Doofenshmirtz" || x.Name == "Perry").ToListAsync();
253274
searchByName[0].TagField = "Vacation";
254275
searchByName[1].DepartmentNumber = 2;
255276
await collection.UpdateAsync(searchByName);
256277
await collection.DeleteAsync(searchByName);
257278
var searchByTag = await collection.FindByIdAsync(searchByName[0].GetKey());
258279
Assert.Null(searchByTag);
259280
}
260-
}
261-
}
281+
}
282+
}

0 commit comments

Comments
 (0)