Skip to content

Commit f64abf2

Browse files
authored
Fix GetSnapshotCollection creating new collection on every invocation (#402)
1 parent 4cc4815 commit f64abf2

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/Akka.Persistence.MongoDb/Snapshot/MongoDbSnapshotStore.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,22 @@ private IMongoDatabase GetMongoDb()
107107

108108
private async Task<IMongoCollection<SnapshotEntry>> GetSnapshotCollection(CancellationToken token)
109109
{
110-
_snapshotCollection_DoNotUseDirectly = GetMongoDb().GetCollection<SnapshotEntry>(_settings.Collection);
111-
112-
if (!_settings.AutoInitialize)
110+
if (_snapshotCollection_DoNotUseDirectly is not null)
113111
return _snapshotCollection_DoNotUseDirectly;
114112

115-
var modelWithAscendingPersistenceIdAndDescendingSequenceNr =
116-
new CreateIndexModel<SnapshotEntry>(Builders<SnapshotEntry>.IndexKeys
117-
.Ascending(entry => entry.PersistenceId)
118-
.Descending(entry => entry.SequenceNr));
119-
120-
await _snapshotCollection_DoNotUseDirectly.Indexes
121-
.CreateOneAsync(modelWithAscendingPersistenceIdAndDescendingSequenceNr,
122-
cancellationToken: token);
113+
_snapshotCollection_DoNotUseDirectly = GetMongoDb().GetCollection<SnapshotEntry>(_settings.Collection);
114+
115+
if (_settings.AutoInitialize)
116+
{
117+
var modelWithAscendingPersistenceIdAndDescendingSequenceNr =
118+
new CreateIndexModel<SnapshotEntry>(Builders<SnapshotEntry>.IndexKeys
119+
.Ascending(entry => entry.PersistenceId)
120+
.Descending(entry => entry.SequenceNr));
121+
122+
await _snapshotCollection_DoNotUseDirectly.Indexes
123+
.CreateOneAsync(modelWithAscendingPersistenceIdAndDescendingSequenceNr,
124+
cancellationToken: token);
125+
}
123126

124127
return _snapshotCollection_DoNotUseDirectly;
125128
}

0 commit comments

Comments
 (0)