Skip to content

Commit 3e65cb5

Browse files
committed
Enhanced code
1 parent 9ddc105 commit 3e65cb5

File tree

15 files changed

+378
-102
lines changed

15 files changed

+378
-102
lines changed

src/SharpConnector/Connectors/LiteDb/LiteDbWrapper.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public LiteDbWrapper(LiteDbConfig liteDbConfig)
2929
/// <returns></returns>
3030
public LiteDbConnectorEntity Get(string key)
3131
{
32-
return _liteDbAccess.Collection.Find(Query.EQ("Key", new BsonValue(key))).FirstOrDefault();
32+
return _liteDbAccess.Collection
33+
.Find(Query.EQ("Key", new BsonValue(key)))
34+
.FirstOrDefault();
3335
}
3436

3537
/// <summary>
@@ -40,7 +42,9 @@ public LiteDbConnectorEntity Get(string key)
4042
public Task<LiteDbConnectorEntity> GetAsync(string key)
4143
{
4244
// LiteDb library does not implement asynchronous operations
43-
var entity = _liteDbAccess.Collection.Find(Query.EQ("Key", new BsonValue(key))).FirstOrDefault();
45+
var entity = _liteDbAccess.Collection
46+
.Find(Query.EQ("Key", new BsonValue(key)))
47+
.FirstOrDefault();
4448
return Task.FromResult(entity);
4549
}
4650

@@ -53,6 +57,17 @@ public IEnumerable<LiteDbConnectorEntity> GetAll()
5357
return _liteDbAccess.Collection.Find(x=> true).ToList();
5458
}
5559

60+
/// <summary>
61+
/// Get all the values asynchronously.
62+
/// </summary>
63+
/// <returns></returns>
64+
public Task<IEnumerable<LiteDbConnectorEntity>> GetAllAsync()
65+
{
66+
var entities = GetAll();
67+
return Task.FromResult(entities);
68+
}
69+
70+
5671
/// <summary>
5772
/// Set the Key to hold the value.
5873
/// </summary>
@@ -72,9 +87,8 @@ public bool Insert(LiteDbConnectorEntity connectorEntity)
7287
public Task<bool> InsertAsync(LiteDbConnectorEntity connectorEntity)
7388
{
7489
// LiteDb library does not implement asynchronous operations
75-
Delete(connectorEntity.Key);
76-
var insert = Insert(connectorEntity);
77-
return Task.FromResult(insert);
90+
var result = Insert(connectorEntity);
91+
return Task.FromResult(result);
7892
}
7993

8094
/// <summary>
@@ -88,6 +102,17 @@ public bool InsertMany(List<LiteDbConnectorEntity> connectorEntities)
88102
return true;
89103
}
90104

105+
/// <summary>
106+
/// Asynchronously inserts multiple ConnectorEntities.
107+
/// </summary>
108+
/// <param name="connectorEntities">The list of ConnectorEntities to store.</param>
109+
/// <returns>True if all operations succeeded, false otherwise.</returns>
110+
public Task<bool> InsertManyAsync(List<LiteDbConnectorEntity> connectorEntities)
111+
{
112+
InsertMany(connectorEntities);
113+
return Task.FromResult(true);
114+
}
115+
91116
/// <summary>
92117
/// Removes the specified Key.
93118
/// </summary>
@@ -117,8 +142,7 @@ public Task<bool> DeleteAsync(string key)
117142
/// <returns></returns>
118143
public bool Update(LiteDbConnectorEntity connectorEntity)
119144
{
120-
// TODO switch to single access with UpdateMany
121-
_liteDbAccess.Collection.DeleteMany(Query.EQ("Key", new BsonValue(connectorEntity.Key)));
145+
Delete(connectorEntity.Key);
122146
return Insert(connectorEntity);
123147
}
124148

@@ -130,8 +154,8 @@ public bool Update(LiteDbConnectorEntity connectorEntity)
130154
public Task<bool> UpdateAsync(LiteDbConnectorEntity connectorEntity)
131155
{
132156
// LiteDb library does not implement asynchronous operations yet
133-
var update = Update(connectorEntity);
134-
return Task.FromResult(update);
157+
var result = Update(connectorEntity);
158+
return Task.FromResult(result);
135159
}
136160
}
137161
}

src/SharpConnector/Connectors/Memcached/MemcachedWrapper.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
using Newtonsoft.Json;
1+
// (c) 2021 Francesco Del Re <[email protected]>
2+
// This code is licensed under MIT license (see LICENSE.txt for details)
3+
using Newtonsoft.Json;
24
using SharpConnector.Configuration;
35
using SharpConnector.Entities;
6+
using System;
47
using System.Collections.Generic;
8+
using System.Linq;
59
using System.Threading.Tasks;
610

711
namespace SharpConnector.Connectors.Memcached
@@ -40,6 +44,15 @@ public async Task<ConnectorEntity> GetAsync(string key)
4044
return result?.Value;
4145
}
4246

47+
/// <summary>
48+
/// Get all ConnectorEntities asynchronously.
49+
/// </summary>
50+
/// <returns>A list of ConnectorEntities.</returns>
51+
public async Task<List<ConnectorEntity>> GetAllAsync()
52+
{
53+
throw new NotSupportedException("This operation is not supported.");
54+
}
55+
4356
/// <summary>
4457
/// Set the Key to hold the value.
4558
/// </summary>
@@ -73,18 +86,19 @@ public Task<bool> InsertAsync(ConnectorEntity connectorEntity)
7386
/// <returns></returns>
7487
public bool InsertMany(List<ConnectorEntity> connectorEntities)
7588
{
76-
var result = true;
77-
foreach (var entity in connectorEntities)
78-
{
79-
var seconds = entity.Expiration?.Seconds ?? 0;
80-
var currentResult = _memcachedAccess.MemcachedClient.Add(entity.Key,
81-
JsonConvert.SerializeObject(entity),
82-
seconds);
89+
return connectorEntities.All(Insert);
90+
}
8391

84-
if (currentResult == false)
85-
result = false;
86-
}
87-
return result;
92+
/// <summary>
93+
/// Asynchronously inserts multiple ConnectorEntities.
94+
/// </summary>
95+
/// <param name="connectorEntities">The list of ConnectorEntities to store.</param>
96+
/// <returns>True if all operations succeeded, false otherwise.</returns>
97+
public async Task<bool> InsertManyAsync(List<ConnectorEntity> connectorEntities)
98+
{
99+
var tasks = connectorEntities.Select(InsertAsync);
100+
var results = await Task.WhenAll(tasks);
101+
return results.All(r => r);
88102
}
89103

90104
/// <summary>

src/SharpConnector/Connectors/MongoDb/MongoDbWrapper.cs

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public MongoDbWrapper(MongoDbConfig mongoDbConfig)
2929
public ConnectorEntity Get(string key)
3030
{
3131
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", key);
32-
return _mongoDbAccess.Collection.Find(filter).FirstOrDefault();
32+
return _mongoDbAccess.Collection
33+
.Find(filter)
34+
.FirstOrDefault();
3335
}
3436

3537
/// <summary>
@@ -50,7 +52,21 @@ public async Task<ConnectorEntity> GetAsync(string key)
5052
/// <returns></returns>
5153
public List<ConnectorEntity> GetAll()
5254
{
53-
return _mongoDbAccess.Collection.Find(x => true).ToList();
55+
return _mongoDbAccess.Collection
56+
.Find(x => true)
57+
.ToList();
58+
}
59+
60+
/// <summary>
61+
/// Asynchronously get all the values.
62+
/// </summary>
63+
/// <returns></returns>
64+
public async Task<List<ConnectorEntity>> GetAllAsync()
65+
{
66+
return await _mongoDbAccess.Collection
67+
.Find(x => true)
68+
.ToListAsync()
69+
.ConfigureAwait(false);
5470
}
5571

5672
/// <summary>
@@ -60,8 +76,10 @@ public List<ConnectorEntity> GetAll()
6076
/// <returns></returns>
6177
public bool Insert(ConnectorEntity connectorEntity)
6278
{
63-
Delete(connectorEntity.Key);
64-
_mongoDbAccess.Collection.InsertOne(connectorEntity);
79+
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", connectorEntity.Key);
80+
var options = new ReplaceOptions { IsUpsert = true };
81+
_mongoDbAccess.Collection
82+
.ReplaceOne(filter, connectorEntity, options);
6583
return true;
6684
}
6785

@@ -70,11 +88,14 @@ public bool Insert(ConnectorEntity connectorEntity)
7088
/// </summary>
7189
/// <param name="connectorEntity">The ConnectorEntity to store.</param>
7290
/// <returns></returns>
73-
public Task<bool> InsertAsync(ConnectorEntity connectorEntity)
91+
public async Task<bool> InsertAsync(ConnectorEntity connectorEntity)
7492
{
75-
DeleteAsync(connectorEntity.Key);
76-
var insert = _mongoDbAccess.Collection.InsertOneAsync(connectorEntity);
77-
return Task.FromResult(insert.IsCompletedSuccessfully);
93+
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", connectorEntity.Key);
94+
var options = new ReplaceOptions { IsUpsert = true };
95+
await _mongoDbAccess.Collection
96+
.ReplaceOneAsync(filter, connectorEntity, options)
97+
.ConfigureAwait(false);
98+
return true;
7899
}
79100

80101
/// <summary>
@@ -84,7 +105,23 @@ public Task<bool> InsertAsync(ConnectorEntity connectorEntity)
84105
/// <returns></returns>
85106
public bool InsertMany(List<ConnectorEntity> connectorEntities)
86107
{
87-
_mongoDbAccess.Collection.InsertMany(connectorEntities);
108+
var options = new InsertManyOptions { IsOrdered = false };
109+
_mongoDbAccess.Collection
110+
.InsertMany(connectorEntities, options);
111+
return true;
112+
}
113+
114+
/// <summary>
115+
/// Asynchronously inserts multiple ConnectorEntities.
116+
/// </summary>
117+
/// <param name="connectorEntities">The list of ConnectorEntities to store.</param>
118+
/// <returns>A boolean indicating if the operation completed successfully.</returns>
119+
public async Task<bool> InsertManyAsync(List<ConnectorEntity> connectorEntities)
120+
{
121+
var options = new InsertManyOptions { IsOrdered = false };
122+
await _mongoDbAccess.Collection
123+
.InsertManyAsync(connectorEntities, options)
124+
.ConfigureAwait(false);
88125
return true;
89126
}
90127

@@ -96,19 +133,22 @@ public bool InsertMany(List<ConnectorEntity> connectorEntities)
96133
public bool Delete(string key)
97134
{
98135
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", key);
99-
return _mongoDbAccess.Collection.DeleteOne(filter).IsAcknowledged;
136+
return _mongoDbAccess.Collection
137+
.DeleteOne(filter).IsAcknowledged;
100138
}
101139

102140
/// <summary>
103-
/// Removes the specified Key.
141+
/// Asynchronously removes the specified Key.
104142
/// </summary>
105-
/// <param name="key">The key of the object.</param>
106-
/// <returns></returns>
107-
public Task<bool> DeleteAsync(string key)
143+
/// <param name="key">The key of the object to delete.</param>
144+
/// <returns>A boolean indicating if the deletion was acknowledged.</returns>
145+
public async Task<bool> DeleteAsync(string key)
108146
{
109147
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", key);
110-
var delete = _mongoDbAccess.Collection.DeleteOneAsync(filter);
111-
return Task.FromResult(delete.IsCompletedSuccessfully);
148+
var result = await _mongoDbAccess.Collection
149+
.DeleteOneAsync(filter)
150+
.ConfigureAwait(false);
151+
return result.IsAcknowledged && result.DeletedCount > 0;
112152
}
113153

114154
/// <summary>
@@ -120,19 +160,23 @@ public bool Update(ConnectorEntity connectorEntity)
120160
{
121161
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", connectorEntity.Key);
122162
var update = Builders<ConnectorEntity>.Update.Set("Payload", connectorEntity.Payload);
123-
return _mongoDbAccess.Collection.UpdateOne(filter, update).IsAcknowledged;
163+
return _mongoDbAccess.Collection
164+
.UpdateOne(filter, update).IsAcknowledged;
124165
}
125166

126167
/// <summary>
127168
/// Updates the specified Key.
128169
/// </summary>
129170
/// <param name="connectorEntity">The ConnectorEntity to store.</param>
130-
/// <returns></returns>
131-
public Task<bool> UpdateAsync(ConnectorEntity connectorEntity)
171+
/// <returns>A boolean indicating if the update was acknowledged.</returns>
172+
public async Task<bool> UpdateAsync(ConnectorEntity connectorEntity)
132173
{
133174
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", connectorEntity.Key);
134175
var update = Builders<ConnectorEntity>.Update.Set("Payload", connectorEntity.Payload);
135-
return Task.FromResult(_mongoDbAccess.Collection.UpdateOneAsync(filter, update).IsCompletedSuccessfully);
176+
var result = await _mongoDbAccess.Collection
177+
.UpdateOneAsync(filter, update)
178+
.ConfigureAwait(false);
179+
return result.IsAcknowledged && result.ModifiedCount > 0;
136180
}
137181
}
138182
}

src/SharpConnector/Connectors/RavenDb/RavenDbWrapper.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// (c) 2021 Francesco Del Re <[email protected]>
22
// This code is licensed under MIT license (see LICENSE.txt for details)
3+
using Raven.Client.Documents;
34
using SharpConnector.Configuration;
45
using SharpConnector.Entities;
56
using System.Collections.Generic;
@@ -57,6 +58,22 @@ public List<ConnectorEntity> GetAll()
5758
}
5859
}
5960

61+
/// <summary>
62+
/// Get all values asynchronously.
63+
/// </summary>
64+
/// <returns>A task representing the asynchronous operation, containing a list of ConnectorEntities.</returns>
65+
public async Task<List<ConnectorEntity>> GetAllAsync()
66+
{
67+
using (var session = _ravenDbAccess.Store.OpenAsyncSession())
68+
{
69+
var entities = await session
70+
.Query<ConnectorEntity>()
71+
.Customize(cr => cr.WaitForNonStaleResults())
72+
.ToListAsync();
73+
return entities;
74+
}
75+
}
76+
6077
/// <summary>
6178
/// Set the Key to hold the value.
6279
/// </summary>
@@ -105,6 +122,24 @@ public bool InsertMany(List<ConnectorEntity> connectorEntities)
105122
return true;
106123
}
107124

125+
/// <summary>
126+
/// Asynchronously insert multiple ConnectorEntities.
127+
/// </summary>
128+
/// <param name="connectorEntities">The ConnectorEntities to store.</param>
129+
/// <returns>A task representing the asynchronous operation.</returns>
130+
public async Task<bool> InsertManyAsync(List<ConnectorEntity> connectorEntities)
131+
{
132+
using (var session = _ravenDbAccess.Store.OpenAsyncSession())
133+
{
134+
foreach (var entity in connectorEntities)
135+
{
136+
await session.StoreAsync(entity, entity.Key);
137+
}
138+
await session.SaveChangesAsync();
139+
}
140+
return true;
141+
}
142+
108143
/// <summary>
109144
/// Removes the specified Key.
110145
/// </summary>
@@ -145,6 +180,7 @@ public bool Update(ConnectorEntity connectorEntity)
145180
using (var session = _ravenDbAccess.Store.OpenSession())
146181
{
147182
var entity = session.Load<ConnectorEntity>(connectorEntity.Key);
183+
if (entity == null) return false;
148184
entity.Payload = connectorEntity.Payload;
149185
entity.Expiration = connectorEntity.Expiration;
150186
session.SaveChanges();
@@ -162,6 +198,7 @@ public async Task<bool> UpdateAsync(ConnectorEntity connectorEntity)
162198
using (var session = _ravenDbAccess.Store.OpenAsyncSession())
163199
{
164200
var entity = await session.LoadAsync<ConnectorEntity>(connectorEntity.Key);
201+
if (entity == null) return false;
165202
entity.Payload = connectorEntity.Payload;
166203
entity.Expiration = connectorEntity.Expiration;
167204
await session.SaveChangesAsync();

0 commit comments

Comments
 (0)