Skip to content

Commit

Permalink
fix: enhance wrapper and access classes
Browse files Browse the repository at this point in the history
  • Loading branch information
engineering87 committed Nov 30, 2024
1 parent 292e4ec commit 08f3920
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 122 deletions.
23 changes: 21 additions & 2 deletions src/SharpConnector/Connectors/LiteDb/LiteDbAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ namespace SharpConnector.Connectors.LiteDb
public class LiteDbAccess : IDisposable
{
public ILiteCollection<LiteDbConnectorEntity> Collection { get; }
private LiteDatabase _liteDatabase;
private readonly LiteDatabase _liteDatabase;

private bool _disposed;

/// <summary>
/// Create a new LiteDbAccess instance.
/// </summary>
/// <param name="liteDbConfig">The LiteDb configuration.</param>
public LiteDbAccess(LiteDbConfig liteDbConfig)
{
if (liteDbConfig == null)
{
throw new ArgumentNullException(nameof(liteDbConfig));
}

_liteDatabase = new LiteDatabase(liteDbConfig.ConnectionString);
var collectionName = liteDbConfig.CollectionName;
Collection = _liteDatabase.GetCollection<LiteDbConnectorEntity>(collectionName);
Expand All @@ -28,8 +35,20 @@ public LiteDbAccess(LiteDbConfig liteDbConfig)
/// </summary>
public void Dispose()
{
_liteDatabase?.Dispose();
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_liteDatabase?.Dispose();
}
_disposed = true;
}
}
}
}
33 changes: 16 additions & 17 deletions src/SharpConnector/Connectors/LiteDb/LiteDbWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public LiteDbConnectorEntity Get(string key)
/// </summary>
/// <param name="key">The key of the object.</param>
/// <returns></returns>
public Task<LiteDbConnectorEntity> GetAsync(string key)
public ValueTask<LiteDbConnectorEntity> GetAsync(string key)
{
// LiteDb library does not implement asynchronous operations
var entity = _liteDbAccess.Collection
.Find(Query.EQ("Key", new BsonValue(key)))
.FirstOrDefault();
return Task.FromResult(entity);
return ValueTask.FromResult(entity);
}

/// <summary>
Expand All @@ -54,17 +54,17 @@ public Task<LiteDbConnectorEntity> GetAsync(string key)
/// <returns></returns>
public IEnumerable<LiteDbConnectorEntity> GetAll()
{
return _liteDbAccess.Collection.Find(x=> true).ToList();
return _liteDbAccess.Collection.Find(_ => true).ToList();
}

/// <summary>
/// Get all the values asynchronously.
/// </summary>
/// <returns></returns>
public Task<IEnumerable<LiteDbConnectorEntity>> GetAllAsync()
public ValueTask<IEnumerable<LiteDbConnectorEntity>> GetAllAsync()
{
var entities = GetAll();
return Task.FromResult(entities);
return ValueTask.FromResult(entities);
}


Expand All @@ -84,11 +84,11 @@ public bool Insert(LiteDbConnectorEntity connectorEntity)
/// </summary>
/// <param name="connectorEntity">The ConnectorEntity to store.</param>
/// <returns></returns>
public Task<bool> InsertAsync(LiteDbConnectorEntity connectorEntity)
public ValueTask<bool> InsertAsync(LiteDbConnectorEntity connectorEntity)
{
// LiteDb library does not implement asynchronous operations
var result = Insert(connectorEntity);
return Task.FromResult(result);
return ValueTask.FromResult(result);
}

/// <summary>
Expand All @@ -98,19 +98,19 @@ public Task<bool> InsertAsync(LiteDbConnectorEntity connectorEntity)
/// <returns></returns>
public bool InsertMany(List<LiteDbConnectorEntity> connectorEntities)
{
_liteDbAccess.Collection.InsertBulk(connectorEntities);
return true;
var insertedCount = _liteDbAccess.Collection.InsertBulk(connectorEntities);
return insertedCount == connectorEntities?.Count;
}

/// <summary>
/// Asynchronously inserts multiple ConnectorEntities.
/// </summary>
/// <param name="connectorEntities">The list of ConnectorEntities to store.</param>
/// <returns>True if all operations succeeded, false otherwise.</returns>
public Task<bool> InsertManyAsync(List<LiteDbConnectorEntity> connectorEntities)
public ValueTask<bool> InsertManyAsync(List<LiteDbConnectorEntity> connectorEntities)
{
InsertMany(connectorEntities);
return Task.FromResult(true);
return ValueTask.FromResult(true);
}

/// <summary>
Expand All @@ -128,11 +128,11 @@ public bool Delete(string key)
/// </summary>
/// <param name="key">The key of the object.</param>
/// <returns></returns>
public Task<bool> DeleteAsync(string key)
public ValueTask<bool> DeleteAsync(string key)
{
// LiteDb library does not implement asynchronous operations yet
var delete = Delete(key);
return Task.FromResult(delete);
return ValueTask.FromResult(delete);
}

/// <summary>
Expand All @@ -142,20 +142,19 @@ public Task<bool> DeleteAsync(string key)
/// <returns></returns>
public bool Update(LiteDbConnectorEntity connectorEntity)
{
Delete(connectorEntity.Key);
return Insert(connectorEntity);
return _liteDbAccess.Collection.Update(connectorEntity);
}

/// <summary>
/// Updates the specified Key.
/// </summary>
/// <param name="connectorEntity">The ConnectorEntity to update.</param>
/// <returns></returns>
public Task<bool> UpdateAsync(LiteDbConnectorEntity connectorEntity)
public ValueTask<bool> UpdateAsync(LiteDbConnectorEntity connectorEntity)
{
// LiteDb library does not implement asynchronous operations yet
var result = Update(connectorEntity);
return Task.FromResult(result);
return ValueTask.FromResult(result);
}
}
}
7 changes: 6 additions & 1 deletion src/SharpConnector/Connectors/Memcached/MemcachedWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public MemcachedWrapper(MemcachedConfig memcachedConfig)
_memcachedAccess = new MemcachedAccess(memcachedConfig);
}

private int GetExpiration(ConnectorEntity connectorEntity)
{
return connectorEntity.Expiration?.Seconds ?? 0;
}

/// <summary>
/// Get the value of Key.
/// </summary>
Expand Down Expand Up @@ -50,7 +55,7 @@ public async Task<ConnectorEntity> GetAsync(string key)
/// <returns>A list of ConnectorEntities.</returns>
public async Task<List<ConnectorEntity>> GetAllAsync()

Check warning on line 56 in src/SharpConnector/Connectors/Memcached/MemcachedWrapper.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 56 in src/SharpConnector/Connectors/Memcached/MemcachedWrapper.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
throw new NotSupportedException("This operation is not supported.");
throw new NotImplementedException();
}

/// <summary>
Expand Down
14 changes: 8 additions & 6 deletions src/SharpConnector/Connectors/MongoDb/MongoDbAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,29 @@ namespace SharpConnector.Connectors.MongoDb
public class MongoDbAccess : IDisposable
{
public IMongoCollection<ConnectorEntity> Collection { get; }
private MongoClient _client;
private readonly MongoClient _client;

/// <summary>
/// Create a new MongoDbAccess instance.
/// </summary>
/// <param name="mongoDbConfig">The MongoDb configuration.</param>
public MongoDbAccess(MongoDbConfig mongoDbConfig)
{
var databaseName = mongoDbConfig.DatabaseName;
var collectionName = mongoDbConfig.CollectionName;
if (mongoDbConfig == null)
{
throw new ArgumentNullException(nameof(mongoDbConfig), "MongoDB configuration cannot be null.");
}

_client = new MongoClient(mongoDbConfig.ConnectionString);
var db = _client.GetDatabase(databaseName);
Collection = db.GetCollection<ConnectorEntity>(collectionName);
var database = _client.GetDatabase(mongoDbConfig.DatabaseName);
Collection = database.GetCollection<ConnectorEntity>(mongoDbConfig.CollectionName);
}

/// <summary>
/// Dispose of the MongoClient.
/// </summary>
public void Dispose()
{
_client = null;
GC.SuppressFinalize(this);
}
}
Expand Down
57 changes: 32 additions & 25 deletions src/SharpConnector/Connectors/MongoDb/MongoDbWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public MongoDbWrapper(MongoDbConfig mongoDbConfig)
public ConnectorEntity Get(string key)
{
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", key);

return _mongoDbAccess.Collection
.Find(filter)
.FirstOrDefault();
Expand All @@ -41,9 +42,10 @@ public ConnectorEntity Get(string key)
/// <returns></returns>
public async Task<ConnectorEntity> GetAsync(string key)
{
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", key);
var entity = await _mongoDbAccess.Collection.FindAsync(filter);
return await entity.FirstOrDefaultAsync();
var filter = Builders<ConnectorEntity>.Filter.Eq(x => x.Key, key);
var cursor = await _mongoDbAccess.Collection.FindAsync(filter).ConfigureAwait(false);

return await cursor.FirstOrDefaultAsync().ConfigureAwait(false);
}

/// <summary>
Expand All @@ -64,7 +66,7 @@ public List<ConnectorEntity> GetAll()
public async Task<List<ConnectorEntity>> GetAllAsync()
{
return await _mongoDbAccess.Collection
.Find(x => true)
.Find(FilterDefinition<ConnectorEntity>.Empty)
.ToListAsync()
.ConfigureAwait(false);
}
Expand All @@ -76,11 +78,11 @@ public async Task<List<ConnectorEntity>> GetAllAsync()
/// <returns></returns>
public bool Insert(ConnectorEntity connectorEntity)
{
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", connectorEntity.Key);
var filter = Builders<ConnectorEntity>.Filter.Eq(x => x.Key, connectorEntity.Key);
var options = new ReplaceOptions { IsUpsert = true };
_mongoDbAccess.Collection
.ReplaceOne(filter, connectorEntity, options);
return true;

var result = _mongoDbAccess.Collection.ReplaceOne(filter, connectorEntity, options);
return result.IsAcknowledged;
}

/// <summary>
Expand All @@ -92,10 +94,9 @@ public async Task<bool> InsertAsync(ConnectorEntity connectorEntity)
{
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", connectorEntity.Key);
var options = new ReplaceOptions { IsUpsert = true };
await _mongoDbAccess.Collection
.ReplaceOneAsync(filter, connectorEntity, options)
.ConfigureAwait(false);
return true;

var result = await _mongoDbAccess.Collection.ReplaceOneAsync(filter, connectorEntity, options).ConfigureAwait(false);
return result.IsAcknowledged;
}

/// <summary>
Expand All @@ -106,6 +107,7 @@ await _mongoDbAccess.Collection
public bool InsertMany(List<ConnectorEntity> connectorEntities)
{
var options = new InsertManyOptions { IsOrdered = false };

_mongoDbAccess.Collection
.InsertMany(connectorEntities, options);
return true;
Expand All @@ -119,6 +121,7 @@ public bool InsertMany(List<ConnectorEntity> connectorEntities)
public async Task<bool> InsertManyAsync(List<ConnectorEntity> connectorEntities)
{
var options = new InsertManyOptions { IsOrdered = false };

await _mongoDbAccess.Collection
.InsertManyAsync(connectorEntities, options)
.ConfigureAwait(false);
Expand All @@ -132,9 +135,10 @@ await _mongoDbAccess.Collection
/// <returns></returns>
public bool Delete(string key)
{
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", key);
return _mongoDbAccess.Collection
.DeleteOne(filter).IsAcknowledged;
var filter = Builders<ConnectorEntity>.Filter.Eq(x => x.Key, key);

var result = _mongoDbAccess.Collection.DeleteOne(filter);
return result.IsAcknowledged && result.DeletedCount > 0;
}

/// <summary>
Expand All @@ -144,7 +148,8 @@ public bool Delete(string key)
/// <returns>A boolean indicating if the deletion was acknowledged.</returns>
public async Task<bool> DeleteAsync(string key)
{
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", key);
var filter = Builders<ConnectorEntity>.Filter.Eq(x => x.Key, key);

var result = await _mongoDbAccess.Collection
.DeleteOneAsync(filter)
.ConfigureAwait(false);
Expand All @@ -158,10 +163,12 @@ public async Task<bool> DeleteAsync(string key)
/// <returns></returns>
public bool Update(ConnectorEntity connectorEntity)
{
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", connectorEntity.Key);
var update = Builders<ConnectorEntity>.Update.Set("Payload", connectorEntity.Payload);
return _mongoDbAccess.Collection
.UpdateOne(filter, update).IsAcknowledged;
var filter = Builders<ConnectorEntity>.Filter.Eq(x => x.Key, connectorEntity.Key);
var update = Builders<ConnectorEntity>.Update
.Set(x => x.Payload, connectorEntity.Payload);

var result = _mongoDbAccess.Collection.UpdateOne(filter, update);
return result.IsAcknowledged && result.ModifiedCount > 0;
}

/// <summary>
Expand All @@ -171,11 +178,11 @@ public bool Update(ConnectorEntity connectorEntity)
/// <returns>A boolean indicating if the update was acknowledged.</returns>
public async Task<bool> UpdateAsync(ConnectorEntity connectorEntity)
{
var filter = Builders<ConnectorEntity>.Filter.Eq("Key", connectorEntity.Key);
var update = Builders<ConnectorEntity>.Update.Set("Payload", connectorEntity.Payload);
var result = await _mongoDbAccess.Collection
.UpdateOneAsync(filter, update)
.ConfigureAwait(false);
var filter = Builders<ConnectorEntity>.Filter.Eq(x => x.Key, connectorEntity.Key);
var update = Builders<ConnectorEntity>.Update
.Set(x => x.Payload, connectorEntity.Payload);

var result = await _mongoDbAccess.Collection.UpdateOneAsync(filter, update).ConfigureAwait(false);
return result.IsAcknowledged && result.ModifiedCount > 0;
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/SharpConnector/Connectors/RavenDb/RavenDbAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ public class RavenDbAccess : IDisposable
/// <param name="ravenDbConfig"></param>
public RavenDbAccess(RavenDbConfig ravenDbConfig)
{
IDocumentStore documentStore = new DocumentStore
if (DocumentStore == null)
{
Urls = new[] { ravenDbConfig.ConnectionString },
Database = ravenDbConfig.DatabaseName
};
DocumentStore = new Lazy<IDocumentStore>(() =>
{
var documentStore = new DocumentStore
{
Urls = [ravenDbConfig.ConnectionString],
Database = ravenDbConfig.DatabaseName
};
documentStore.Initialize();
DocumentStore = new Lazy<IDocumentStore>(documentStore);
documentStore.Initialize();
return documentStore;
});
}
}

/// <summary>
Expand Down
Loading

0 comments on commit 08f3920

Please sign in to comment.