Commit d3c88667 authored by Savorboard's avatar Savorboard

Convert file format

parent 42ea47cf
// Copyright (c) .NET Core Community. All rights reserved. // Copyright (c) .NET Core Community. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information. // Licensed under the MIT License. See License.txt in the project root for license information.
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using DotNetCore.CAP.Dashboard; using DotNetCore.CAP.Dashboard;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MongoDB.Driver; using MongoDB.Driver;
namespace DotNetCore.CAP.MongoDB namespace DotNetCore.CAP.MongoDB
{ {
public class MongoDBStorage : IStorage public class MongoDBStorage : IStorage
{ {
private readonly CapOptions _capOptions; private readonly CapOptions _capOptions;
private readonly IMongoClient _client; private readonly IMongoClient _client;
private readonly ILogger<MongoDBStorage> _logger; private readonly ILogger<MongoDBStorage> _logger;
private readonly MongoDBOptions _options; private readonly MongoDBOptions _options;
public MongoDBStorage(CapOptions capOptions, public MongoDBStorage(CapOptions capOptions,
MongoDBOptions options, MongoDBOptions options,
IMongoClient client, IMongoClient client,
ILogger<MongoDBStorage> logger) ILogger<MongoDBStorage> logger)
{ {
_capOptions = capOptions; _capOptions = capOptions;
_options = options; _options = options;
_client = client; _client = client;
_logger = logger; _logger = logger;
} }
public IStorageConnection GetConnection() public IStorageConnection GetConnection()
{ {
return new MongoDBStorageConnection(_capOptions, _options, _client); return new MongoDBStorageConnection(_capOptions, _options, _client);
} }
public IMonitoringApi GetMonitoringApi() public IMonitoringApi GetMonitoringApi()
{ {
return new MongoDBMonitoringApi(_client, _options); return new MongoDBMonitoringApi(_client, _options);
} }
public async Task InitializeAsync(CancellationToken cancellationToken) public async Task InitializeAsync(CancellationToken cancellationToken)
{ {
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
return; return;
} }
var database = _client.GetDatabase(_options.DatabaseName); var database = _client.GetDatabase(_options.DatabaseName);
var names = (await database.ListCollectionNamesAsync(cancellationToken: cancellationToken))?.ToList(); var names = (await database.ListCollectionNamesAsync(cancellationToken: cancellationToken))?.ToList();
if (names.All(n => n != _options.ReceivedCollection)) if (names.All(n => n != _options.ReceivedCollection))
{ {
await database.CreateCollectionAsync(_options.ReceivedCollection, cancellationToken: cancellationToken); await database.CreateCollectionAsync(_options.ReceivedCollection, cancellationToken: cancellationToken);
} }
if (names.All(n => n != _options.PublishedCollection)) if (names.All(n => n != _options.PublishedCollection))
{ {
await database.CreateCollectionAsync(_options.PublishedCollection, await database.CreateCollectionAsync(_options.PublishedCollection,
cancellationToken: cancellationToken); cancellationToken: cancellationToken);
} }
var receivedMessageIndexNames = new string[] { var receivedMessageIndexNames = new string[] {
nameof(ReceivedMessage.Name), nameof(ReceivedMessage.Added), nameof(ReceivedMessage.ExpiresAt), nameof(ReceivedMessage.Name), nameof(ReceivedMessage.Added), nameof(ReceivedMessage.ExpiresAt),
nameof(ReceivedMessage.StatusName), nameof(ReceivedMessage.Retries), nameof(ReceivedMessage.Version) }; nameof(ReceivedMessage.StatusName), nameof(ReceivedMessage.Retries), nameof(ReceivedMessage.Version) };
...@@ -99,7 +99,7 @@ namespace DotNetCore.CAP.MongoDB ...@@ -99,7 +99,7 @@ namespace DotNetCore.CAP.MongoDB
}).ToArray(); }).ToArray();
await col.Indexes.CreateManyAsync(indexes, cancellationToken); await col.Indexes.CreateManyAsync(indexes, cancellationToken);
} }
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment