From 98c5f0c96fe353cb9155b25ca9ec289196c4d0a2 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Fri, 7 Jun 2024 11:12:31 +0500 Subject: [PATCH] sync: use builders instead of linq for mongodb queries --- .../Repositories/SyncItemsRepository.cs | 63 ++++++------------- 1 file changed, 20 insertions(+), 43 deletions(-) diff --git a/Notesnook.API/Repositories/SyncItemsRepository.cs b/Notesnook.API/Repositories/SyncItemsRepository.cs index 509d0c5..73b2c60 100644 --- a/Notesnook.API/Repositories/SyncItemsRepository.cs +++ b/Notesnook.API/Repositories/SyncItemsRepository.cs @@ -32,6 +32,7 @@ You should have received a copy of the Affero GNU General Public License using Notesnook.API.Interfaces; using Notesnook.API.Models; using Streetwriters.Common; +using Streetwriters.Data.DbContexts; using Streetwriters.Data.Interfaces; using Streetwriters.Data.Repositories; @@ -52,7 +53,7 @@ public SyncItemsRepository(IDbContext dbContext, IMongoCollection coll #endif } - private readonly List ALGORITHMS = new List { Algorithms.Default }; + private readonly List ALGORITHMS = [Algorithms.Default]; private bool IsValidAlgorithm(string algorithm) { return ALGORITHMS.Contains(algorithm); @@ -60,25 +61,27 @@ private bool IsValidAlgorithm(string algorithm) public Task CountItemsSyncedAfterAsync(string userId, long timestamp) { - return Collection.CountDocumentsAsync(n => (n.DateSynced > timestamp) && n.UserId.Equals(userId)); + var filter = Builders.Filter.And(Builders.Filter.Gt("DateSynced", timestamp), Builders.Filter.Eq("UserId", userId)); + return Collection.CountDocumentsAsync(filter); } public Task> FindItemsSyncedAfter(string userId, long timestamp, int batchSize) { - return Collection.FindAsync(n => (n.DateSynced > timestamp) && n.UserId.Equals(userId), new FindOptions + var filter = Builders.Filter.And(Builders.Filter.Gt("DateSynced", timestamp), Builders.Filter.Eq("UserId", userId)); + return Collection.FindAsync(filter, new FindOptions { BatchSize = batchSize, AllowDiskUse = true, AllowPartialResults = false, NoCursorTimeout = true, - Sort = new SortDefinitionBuilder().Ascending((a) => a.Id) + Sort = new SortDefinitionBuilder().Ascending("_id") }); } public Task> FindItemsById(string userId, IEnumerable ids, bool all, int batchSize) { - var filters = new List>(new[] { Builders.Filter.Eq((i) => i.UserId, userId) }); + var filters = new List>(new[] { Builders.Filter.Eq("UserId", userId) }); - if (!all) filters.Add(Builders.Filter.In((i) => i.ItemId, ids)); + if (!all) filters.Add(Builders.Filter.In("ItemId", ids)); return Collection.FindAsync(Builders.Filter.And(filters), new FindOptions { @@ -89,46 +92,14 @@ public Task> FindItemsById(string userId, IEnumerable> GetIdsAsync(string userId, int batchSize) - { - var filter = Builders.Filter.Eq((i) => i.UserId, userId); - return Collection.FindAsync(filter, new FindOptions - { - BatchSize = batchSize, - AllowDiskUse = true, - AllowPartialResults = false, - NoCursorTimeout = true, - Sort = new SortDefinitionBuilder().Ascending((a) => a.Id), - Projection = Builders.Projection.Include((i) => i.ItemId) - }); - } - // public async Task DeleteIdsAsync(string[] ids, string userId, CancellationToken token = default(CancellationToken)) - // { - // await Collection.DeleteManyAsync((i) => ids.Contains(i.Id) && i.UserId == userId, token); - // } - public void DeleteByUserId(string userId) { - dbContext.AddCommand((handle, ct) => Collection.DeleteManyAsync(handle, (i) => i.UserId == userId, cancellationToken: ct)); - } - - public async Task UpsertAsync(SyncItem item, string userId, long dateSynced) - { - - if (item.Length > 15 * 1024 * 1024) - { - throw new Exception($"Size of item \"{item.ItemId}\" is too large. Maximum allowed size is 15 MB."); - } - - if (!IsValidAlgorithm(item.Algorithm)) + var filter = Builders.Filter.Eq("UserId", userId); + var writes = new List> { - throw new Exception($"Invalid alg identifier {item.Algorithm}"); - } - - item.DateSynced = dateSynced; - item.UserId = userId; - - await base.UpsertAsync(item, (x) => (x.ItemId == item.ItemId) && x.UserId == userId); + new DeleteManyModel(filter) + }; + dbContext.AddCommand((handle, ct) => Collection.BulkWriteAsync(handle, writes, options: null, ct)); } public void Upsert(SyncItem item, string userId, long dateSynced) @@ -153,6 +124,12 @@ public void Upsert(SyncItem item, string userId, long dateSynced) item.DateSynced = dateSynced; item.UserId = userId; + var filter = Builders.Filter.And( + Builders.Filter.Eq("UserId", userId), + Builders.Filter.Eq("ItemId", item.ItemId) + ); + + dbContext.AddCommand((handle, ct) => Collection.ReplaceOneAsync(handle, filter, item, new ReplaceOptions { IsUpsert = true }, ct)); // await base.UpsertAsync(item, (x) => (x.ItemId == item.ItemId) && x.UserId == userId); base.Upsert(item, (x) => x.UserId == userId && x.ItemId == item.ItemId); }