Skip to content

Commit 9f14683

Browse files
Fix queries.
1 parent f1a9397 commit 9f14683

File tree

8 files changed

+547
-436
lines changed

8 files changed

+547
-436
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageProjectUrl>https://github.com/squidex/squidex</PackageProjectUrl>
1212
<PublishRepositoryUrl>true</PublishRepositoryUrl>
1313
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
14-
<Version>7.33.0</Version>
14+
<Version>7.34.0</Version>
1515
</PropertyGroup>
1616

1717
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
Lines changed: 77 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,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;
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+
}
Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
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 Squidex.Hosting.Configuration;
9-
10-
namespace Squidex.Events.Mongo;
11-
12-
public sealed class MongoEventStoreOptions : IValidatableOptions
13-
{
14-
public TimeSpan PollingInterval { get; set; } = TimeSpan.FromSeconds(5);
15-
16-
public string CollectionName { get; set; } = "Events2";
17-
18-
public string PositionCollectionName { get; set; } = "Event2Position";
19-
20-
public IEnumerable<ConfigurationError> Validate()
21-
{
22-
if (PollingInterval < TimeSpan.Zero || PollingInterval > TimeSpan.FromMinutes(10))
23-
{
24-
yield return new ConfigurationError("Value must be between 00:00:00 and 00:10:00.", nameof(PollingInterval));
25-
}
26-
}
27-
}
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 Squidex.Hosting.Configuration;
9+
10+
namespace Squidex.Events.Mongo;
11+
12+
public sealed class MongoEventStoreOptions : IValidatableOptions
13+
{
14+
public TimeSpan PollingInterval { get; set; } = TimeSpan.FromSeconds(5);
15+
16+
public string CollectionName { get; set; } = "Events2";
17+
18+
public string PositionCollectionName { get; set; } = "Event2Position";
19+
20+
public bool UseChangeStreams { get; set; }
21+
22+
public IEnumerable<ConfigurationError> Validate()
23+
{
24+
if (PollingInterval < TimeSpan.Zero || PollingInterval > TimeSpan.FromMinutes(10))
25+
{
26+
yield return new ConfigurationError("Value must be between 00:00:00 and 00:10:00.", nameof(PollingInterval));
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)