-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a57ea2f
commit b520c55
Showing
19 changed files
with
523 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/SharpConnector/Connectors/Couchbase/CouchbaseAccess.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.