Skip to content

Commit

Permalink
Couchbase integration
Browse files Browse the repository at this point in the history
  • Loading branch information
engineering87 committed Nov 10, 2024
1 parent a57ea2f commit b520c55
Show file tree
Hide file tree
Showing 19 changed files with 523 additions and 13 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Through SharpConnector, you can use a consistent interface to perform Insert, Ge
* **LiteDB (embedded document database)**
* **EnyimMemcached (key-value)**
* **RavenDB (document-oriented)**
* **Couchbase (document-oriented)**

SharpConnector thus simplifies the development process, providing flexibility and compatibility across diverse NoSQL paradigms without the need to handle specific database implementations.

Expand Down Expand Up @@ -74,6 +75,7 @@ SharpConnector uses the following externals references:
* **LiteDB** see license [here](https://github.com/mbdavid/LiteDB/blob/master/LICENSE)
* **EnyimMemcached** see license [here](https://github.com/enyim/EnyimMemcached/blob/develop/LICENSE)
* **RavenDB** see license [here](https://github.com/ravendb/ravendb/blob/v5.2/LICENSE.txt)
* **Couchbase** see license [here](https://github.com/couchbase/couchbase-net-client/blob/master/LICENSE)

### Contact
Please contact at francesco.delre[at]protonmail.com for any details.
10 changes: 10 additions & 0 deletions src/SharpConnector.Api/SharpConnector.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<Content Remove="appsettings.couchbase.json" />
<Content Remove="appsettings.litedb.json" />
<Content Remove="appsettings.memcached.json" />
<Content Remove="appsettings.mongodb.json" />
Expand All @@ -16,6 +17,9 @@
</ItemGroup>

<ItemGroup>
<None Include="appsettings.couchbase.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="appsettings.litedb.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand All @@ -41,4 +45,10 @@
<ProjectReference Include="..\SharpConnector\SharpConnector.csproj" />
</ItemGroup>

<ItemGroup>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions src/SharpConnector.Api/appsettings.couchbase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"ConnectorConfig": {
"Instance": "Couchbase",
"ConnectionString": "couchbase://localhost",
"Username": "Administrator",
"Password": "password",
"BucketName": "example_bucket",
"Options": {
"EnableTls": false,
"KvTimeout": 2500,
"QueryTimeout": 7500,
"AnalyticsTimeout": 10000,
"SearchTimeout": 5000,
"ManagementTimeout": 10000
}
}
16 changes: 13 additions & 3 deletions src/SharpConnector.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
{
"ConnectorConfig": {
"Instance": "RavenDb",
"DatabaseName": "test",
"ConnectionString": "http://live-test.ravendb.net"
"Instance": "Couchbase",
"ConnectionString": "couchbase://localhost",
"Username": "username",
"Password": "password",
"BucketName": "example_bucket",
"Options": {
"EnableTls": false,
"KvTimeout": 2500,
"QueryTimeout": 7500,
"AnalyticsTimeout": 10000,
"SearchTimeout": 5000,
"ManagementTimeout": 10000
}
},
"Logging": {
"LogLevel": {
Expand Down
40 changes: 40 additions & 0 deletions src/SharpConnector/Configuration/CouchbaseConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// (c) 2020 Francesco Del Re <[email protected]>
// This code is licensed under MIT license (see LICENSE.txt for details)
using Microsoft.Extensions.Configuration;
using SharpConnector.Enums;
using SharpConnector.Interfaces;
using System.Linq;

namespace SharpConnector.Configuration
{
public class CouchbaseConfig : IConnectorConfig
{
public string ConnectionString { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string BucketName { get; set; }

// Not used
public int DatabaseNumber { get; private set; }
public string DatabaseName { get; private set; }
public string CollectionName { get; private set; }

public CouchbaseConfig(IConfiguration section)
{
var sectionChildren = section.GetChildren();
var configurationSections = sectionChildren.ToList();

var sectionChildrenConnectionString = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.connectionstring.ToString());
ConnectionString = sectionChildrenConnectionString?.Value;

var sectionChildrenBucketName = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.bucketname.ToString());
BucketName = sectionChildrenBucketName?.Value.Trim();

var sectionChildrenUsername = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.username.ToString());
Username = sectionChildrenBucketName?.Value.Trim();

var sectionChildrenPassword = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.password.ToString());
Password = sectionChildrenPassword?.Value.Trim();
}
}
}
7 changes: 6 additions & 1 deletion src/SharpConnector/Configuration/LiteDbConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ public class LiteDbConfig : IConnectorConfig
{
public string ConnectionString { get; }
public string DatabaseName { get; }

// Not used
public string CollectionName { get; private set; }
public int DatabaseNumber { get; private set; }
public string Username { get; private set; }
public string Password { get; private set; }
public string BucketName { get; private set; }

public LiteDbConfig(IConfiguration section)
{
Expand All @@ -26,7 +31,7 @@ public LiteDbConfig(IConfiguration section)
ConnectionString = sectionChildrenConnectionString?.Value;

var sectionChildrenDatabaseName = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.databasename.ToString());
DatabaseName = sectionChildrenDatabaseName?.Value;
DatabaseName = sectionChildrenDatabaseName?.Value.Trim();
}
}
}
7 changes: 6 additions & 1 deletion src/SharpConnector/Configuration/MemcachedConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ namespace SharpConnector.Configuration
{
public class MemcachedConfig : IConnectorConfig
{
public int DatabaseNumber { get; private set; }
public string ConnectionString { get; }

// Not used
public int DatabaseNumber { get; private set; }
public string DatabaseName { get; private set; }
public string CollectionName { get; private set; }
public string Username { get; private set; }
public string Password { get; private set; }
public string BucketName { get; private set; }

public MemcachedConfig(IConfiguration section)
{
Expand Down
9 changes: 7 additions & 2 deletions src/SharpConnector/Configuration/MongoDbConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public class MongoDbConfig : IConnectorConfig
public string ConnectionString { get; }
public string DatabaseName { get; }
public string CollectionName { get; }

// Not used
public int DatabaseNumber { get; private set; }
public string Username { get; private set; }
public string Password { get; private set; }
public string BucketName { get; private set; }

public MongoDbConfig(IConfiguration section)
{
Expand All @@ -26,10 +31,10 @@ public MongoDbConfig(IConfiguration section)
ConnectionString = sectionChildrenConnectionString?.Value;

var sectionChildrenDatabaseName = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.databasename.ToString());
DatabaseName = sectionChildrenDatabaseName?.Value;
DatabaseName = sectionChildrenDatabaseName?.Value.Trim();

var sectionChildrenCollectionName = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.collectionname.ToString());
CollectionName = sectionChildrenCollectionName?.Value;
CollectionName = sectionChildrenCollectionName?.Value.Trim();
}
}
}
7 changes: 6 additions & 1 deletion src/SharpConnector/Configuration/RavenDbConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ public class RavenDbConfig : IConnectorConfig
{
public string ConnectionString { get; }
public string DatabaseName { get; }

// Not used
public string CollectionName { get; private set; }
public int DatabaseNumber { get; private set; }
public string Username { get; private set; }
public string Password { get; private set; }
public string BucketName { get; private set; }

public RavenDbConfig(IConfiguration section)
{
Expand All @@ -26,7 +31,7 @@ public RavenDbConfig(IConfiguration section)
ConnectionString = sectionChildrenConnectionString?.Value;

var sectionChildrenDatabaseName = configurationSections.FirstOrDefault(s => s.Key.ToLower() == AppConfigParameterEnums.databasename.ToString());
DatabaseName = sectionChildrenDatabaseName?.Value;
DatabaseName = sectionChildrenDatabaseName?.Value.Trim();
}
}
}
5 changes: 5 additions & 0 deletions src/SharpConnector/Configuration/RedisConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ public class RedisConfig : IConnectorConfig
{
public string ConnectionString { get; }
public int DatabaseNumber { get; } // 0 default

// Not used
public string DatabaseName { get; private set; }
public string CollectionName { get; private set; }
public string Username { get; private set; }
public string Password { get; private set; }
public string BucketName { get; private set; }

public RedisConfig(IConfiguration section)
{
Expand Down
71 changes: 71 additions & 0 deletions src/SharpConnector/Connectors/Couchbase/CouchbaseAccess.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// (c) 2020 Francesco Del Re <[email protected]>
// This code is licensed under MIT license (see LICENSE.txt for details)
using Couchbase.KeyValue;
using Couchbase;
using System;
using System.Threading.Tasks;
using SharpConnector.Configuration;

namespace SharpConnector.Connectors.Couchbase
{
internal class CouchbaseAccess : IAsyncDisposable
{
private readonly Lazy<Task<ICluster>> _cluster;
private readonly string _bucketName;
private IBucket _bucket;

public CouchbaseAccess(CouchbaseConfig config)
{
_cluster = new Lazy<Task<ICluster>>(() => ConnectClusterAsync(config));
_bucketName = config.BucketName;
}

public string BucketName => _bucketName;

private async Task<ICluster> ConnectClusterAsync(CouchbaseConfig config)
{
var clusterOptions = new ClusterOptions
{
UserName = config.Username,
Password = config.Password
};
return await Cluster.ConnectAsync(config.ConnectionString, clusterOptions);
}

public async Task<ICluster> GetClusterAsync()
{
return await _cluster.Value;
}

public async Task<IBucket> GetBucketAsync()
{
if (_bucket == null)
{
var cluster = await GetClusterAsync();
_bucket = await cluster.BucketAsync(_bucketName);
}
return _bucket;
}

public async Task<ICouchbaseCollection> GetCollectionAsync(string collectionName)
{
var bucket = await GetBucketAsync();
return await bucket.DefaultCollectionAsync();
}

public async ValueTask DisposeAsync()
{
if (_bucket != null)
{
await _bucket.DisposeAsync();
_bucket = null;
}

if (_cluster.IsValueCreated)
{
var cluster = await _cluster.Value;
await cluster.DisposeAsync();
}
}
}
}
Loading

0 comments on commit b520c55

Please sign in to comment.