|
1 | | -// ========================================================================== |
2 | | -// Squidex Headless CMS |
3 | | -// ========================================================================== |
4 | | -// Copyright (c) Squidex UG (haftungsbeschraenkt) |
5 | | -// All rights reserved. Licensed under the MIT license. |
6 | | -// ========================================================================== |
7 | | - |
8 | | -using Microsoft.Extensions.Options; |
9 | | -using MongoDB.Bson; |
10 | | -using MongoDB.Bson.Serialization; |
11 | | -using MongoDB.Driver; |
12 | | -using MongoDB.Driver.Core.Clusters; |
13 | | -using Squidex.Hosting; |
14 | | - |
15 | | -namespace Squidex.Events.Mongo; |
16 | | - |
17 | | -public partial class MongoEventStore( |
18 | | - IMongoDatabase database, |
19 | | - IOptions<MongoEventStoreOptions> options) |
20 | | - : IEventStore, IInitializable |
21 | | -{ |
22 | | - private static readonly FilterDefinitionBuilder<MongoEventCommit> Filter = |
23 | | - Builders<MongoEventCommit>.Filter; |
24 | | - |
25 | | - private static readonly ProjectionDefinitionBuilder<MongoEventCommit> Projection = |
26 | | - Builders<MongoEventCommit>.Projection; |
27 | | - |
28 | | - private static readonly SortDefinitionBuilder<MongoEventCommit> Sort = |
29 | | - Builders<MongoEventCommit>.Sort; |
30 | | - |
31 | | - private readonly IMongoCollection<MongoEventCommit> collection = |
32 | | - database.GetCollection<MongoEventCommit>( |
33 | | - options.Value.CollectionName, |
34 | | - new MongoCollectionSettings { WriteConcern = WriteConcern.WMajority }); |
35 | | - |
36 | | - private readonly IMongoCollection<BsonDocument> rawCollection = |
37 | | - database.GetCollection<BsonDocument>( |
38 | | - options.Value.CollectionName, |
39 | | - new MongoCollectionSettings { WriteConcern = WriteConcern.WMajority }); |
40 | | - |
41 | | - private QueryStrategy queryStrategy; |
42 | | - |
43 | | - public IMongoCollection<BsonDocument> RawCollection => rawCollection; |
44 | | - |
45 | | - public IMongoCollection<MongoEventCommit> TypedCollection => collection; |
46 | | - |
47 | | - public bool CanUseChangeStreams { get; private set; } |
48 | | - |
49 | | - public async Task InitializeAsync( |
50 | | - CancellationToken ct) |
51 | | - { |
52 | | - var versionInfo = await MongoVersionInfo.DetectAsync(database, ct); |
53 | | - |
54 | | - queryStrategy = versionInfo.Dervivate == MongoDerivate.MongoDB ? |
55 | | - new QueryByTimestamp() : |
56 | | - new QueryByGlobalPosition(collection); |
57 | | - await queryStrategy.InitializeAsync(collection, ct); |
58 | | - |
59 | | - await collection.Indexes.CreateOneAsync( |
60 | | - new CreateIndexModel<MongoEventCommit>( |
61 | | - Builders<MongoEventCommit>.IndexKeys |
62 | | - .Ascending(x => x.EventStream) |
63 | | - .Descending(x => x.EventStreamOffset), |
64 | | - new CreateIndexOptions |
65 | | - { |
66 | | - Unique = true, |
67 | | - }), |
68 | | - cancellationToken: ct); |
69 | | - |
70 | | - var clusterVersion = versionInfo.Major; |
71 | | - var clusteredAsReplica = database.Client.Cluster.Description.Type == ClusterType.ReplicaSet; |
72 | | - |
73 | | - CanUseChangeStreams = clusteredAsReplica && clusterVersion >= 4; |
74 | | - |
75 | | - BsonSerializer.TryRegisterSerializer(new MongoHeaderValueSerializer()); |
76 | | - } |
77 | | -} |
| 1 | +// ========================================================================== |
| 2 | +// Squidex Headless CMS |
| 3 | +// ========================================================================== |
| 4 | +// Copyright (c) Squidex UG (haftungsbeschraenkt) |
| 5 | +// All rights reserved. Licensed under the MIT license. |
| 6 | +// ========================================================================== |
| 7 | + |
| 8 | +using Microsoft.Extensions.Options; |
| 9 | +using MongoDB.Bson; |
| 10 | +using MongoDB.Bson.Serialization; |
| 11 | +using MongoDB.Driver; |
| 12 | +using MongoDB.Driver.Core.Clusters; |
| 13 | +using Squidex.Hosting; |
| 14 | + |
| 15 | +namespace Squidex.Events.Mongo; |
| 16 | + |
| 17 | +public partial class MongoEventStore( |
| 18 | + IMongoDatabase database, |
| 19 | + IOptions<MongoEventStoreOptions> options) |
| 20 | + : IEventStore, IInitializable |
| 21 | +{ |
| 22 | + private static readonly FilterDefinitionBuilder<MongoEventCommit> Filter = |
| 23 | + Builders<MongoEventCommit>.Filter; |
| 24 | + |
| 25 | + private static readonly ProjectionDefinitionBuilder<MongoEventCommit> Projection = |
| 26 | + Builders<MongoEventCommit>.Projection; |
| 27 | + |
| 28 | + private static readonly SortDefinitionBuilder<MongoEventCommit> Sort = |
| 29 | + Builders<MongoEventCommit>.Sort; |
| 30 | + |
| 31 | + private readonly IMongoCollection<MongoEventCommit> collection = |
| 32 | + database.GetCollection<MongoEventCommit>( |
| 33 | + options.Value.CollectionName, |
| 34 | + new MongoCollectionSettings { WriteConcern = WriteConcern.WMajority }); |
| 35 | + |
| 36 | + private readonly IMongoCollection<BsonDocument> rawCollection = |
| 37 | + database.GetCollection<BsonDocument>( |
| 38 | + options.Value.CollectionName, |
| 39 | + new MongoCollectionSettings { WriteConcern = WriteConcern.WMajority }); |
| 40 | + |
| 41 | + private QueryStrategy queryStrategy; |
| 42 | + |
| 43 | + public IMongoCollection<BsonDocument> RawCollection => rawCollection; |
| 44 | + |
| 45 | + public IMongoCollection<MongoEventCommit> TypedCollection => collection; |
| 46 | + |
| 47 | + public bool CanUseChangeStreams { get; private set; } |
| 48 | + |
| 49 | + public async Task InitializeAsync( |
| 50 | + CancellationToken ct) |
| 51 | + { |
| 52 | + var versionInfo = await MongoVersionInfo.DetectAsync(database, ct); |
| 53 | + |
| 54 | + queryStrategy = versionInfo.Dervivate == MongoDerivate.MongoDB ? |
| 55 | + new QueryByTimestamp() : |
| 56 | + new QueryByGlobalPosition(collection); |
| 57 | + await queryStrategy.InitializeAsync(collection, ct); |
| 58 | + |
| 59 | + await collection.Indexes.CreateOneAsync( |
| 60 | + new CreateIndexModel<MongoEventCommit>( |
| 61 | + Builders<MongoEventCommit>.IndexKeys |
| 62 | + .Ascending(x => x.EventStream) |
| 63 | + .Descending(x => x.EventStreamOffset), |
| 64 | + new CreateIndexOptions |
| 65 | + { |
| 66 | + Unique = true, |
| 67 | + }), |
| 68 | + cancellationToken: ct); |
| 69 | + |
| 70 | + var clusterVersion = versionInfo.Major; |
| 71 | + var clusteredAsReplica = database.Client.Cluster.Description.Type == ClusterType.ReplicaSet; |
| 72 | + |
| 73 | + CanUseChangeStreams = (clusteredAsReplica && clusterVersion >= 4) || options.Value.UseChangeStreams; |
| 74 | + |
| 75 | + BsonSerializer.TryRegisterSerializer(new MongoHeaderValueSerializer()); |
| 76 | + } |
| 77 | +} |
0 commit comments