-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: store aggregates and regular values in same collection to save …
…money on collections in Cosmos DB
- Loading branch information
Showing
22 changed files
with
210 additions
and
116 deletions.
There are no files selected for viewing
File renamed without changes.
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
11 changes: 11 additions & 0 deletions
11
src/Covid19Api.Repositories.Abstractions/ICountryStatisticsWriteRepository.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,11 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Covid19Api.Domain; | ||
|
||
namespace Covid19Api.Repositories.Abstractions | ||
{ | ||
public interface ICountryStatisticsWriteRepository | ||
{ | ||
Task StoreManyAsync(IEnumerable<CountryStatistic> countryStats); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/Covid19Api.Repositories.Abstractions/IGlobalStatisticsWriteRepository.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,10 @@ | ||
using System.Threading.Tasks; | ||
using Covid19Api.Domain; | ||
|
||
namespace Covid19Api.Repositories.Abstractions | ||
{ | ||
public interface IGlobalStatisticsWriteRepository | ||
{ | ||
Task StoreAsync(GlobalStatistics globalStatistics); | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
src/Covid19Api.Repositories/CountryStatisticsWriteRepository.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,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Covid19Api.Domain; | ||
using Covid19Api.Mongo; | ||
using Covid19Api.Repositories.Abstractions; | ||
using Covid19Api.Repositories.Extensions; | ||
using MongoDB.Driver; | ||
|
||
namespace Covid19Api.Repositories | ||
{ | ||
public class CountryStatisticsWriteRepository : ICountryStatisticsWriteRepository | ||
{ | ||
private readonly Covid19ApiDbContext context; | ||
|
||
public CountryStatisticsWriteRepository(Covid19ApiDbContext context) | ||
{ | ||
this.context = context; | ||
} | ||
|
||
public async Task StoreManyAsync(IEnumerable<CountryStatistic> countryStats) | ||
{ | ||
var collection = this.GetCollection(); | ||
|
||
var updates = countryStats.Select(currentStats => | ||
{ | ||
var filterDefinition = | ||
new FilterDefinitionBuilder<CountryStatistic>().Where(existingStats => | ||
existingStats.Id == currentStats.Id); | ||
|
||
return new ReplaceOneModel<CountryStatistic>(filterDefinition, currentStats) | ||
{ | ||
IsUpsert = true | ||
}; | ||
}) | ||
.ToList(); | ||
|
||
foreach (var chunk in updates.CreateChunks(50)) | ||
{ | ||
try | ||
{ | ||
await collection.BulkWriteAsync(chunk, new BulkWriteOptions | ||
{ | ||
IsOrdered = false, | ||
}); | ||
} | ||
catch (Exception exception) when (exception is MongoBulkWriteException) | ||
{ | ||
// Might happen when having duplicate ids! | ||
} | ||
} | ||
} | ||
|
||
private IMongoCollection<CountryStatistic> GetCollection() | ||
=> this.context.Database.GetCollection<CountryStatistic>(CollectionNames.CountryStatistics); | ||
} | ||
} |
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
Oops, something went wrong.