Skip to content

Commit

Permalink
sync: use builders instead of linq for mongodb queries
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Jun 7, 2024
1 parent ad4e43e commit 98c5f0c
Showing 1 changed file with 20 additions and 43 deletions.
63 changes: 20 additions & 43 deletions Notesnook.API/Repositories/SyncItemsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -52,33 +53,35 @@ public SyncItemsRepository(IDbContext dbContext, IMongoCollection<SyncItem> coll
#endif
}

private readonly List<string> ALGORITHMS = new List<string> { Algorithms.Default };
private readonly List<string> ALGORITHMS = [Algorithms.Default];
private bool IsValidAlgorithm(string algorithm)
{
return ALGORITHMS.Contains(algorithm);
}

public Task<long> CountItemsSyncedAfterAsync(string userId, long timestamp)
{
return Collection.CountDocumentsAsync(n => (n.DateSynced > timestamp) && n.UserId.Equals(userId));
var filter = Builders<SyncItem>.Filter.And(Builders<SyncItem>.Filter.Gt("DateSynced", timestamp), Builders<SyncItem>.Filter.Eq("UserId", userId));
return Collection.CountDocumentsAsync(filter);
}
public Task<IAsyncCursor<SyncItem>> FindItemsSyncedAfter(string userId, long timestamp, int batchSize)
{
return Collection.FindAsync(n => (n.DateSynced > timestamp) && n.UserId.Equals(userId), new FindOptions<SyncItem>
var filter = Builders<SyncItem>.Filter.And(Builders<SyncItem>.Filter.Gt("DateSynced", timestamp), Builders<SyncItem>.Filter.Eq("UserId", userId));
return Collection.FindAsync(filter, new FindOptions<SyncItem>
{
BatchSize = batchSize,
AllowDiskUse = true,
AllowPartialResults = false,
NoCursorTimeout = true,
Sort = new SortDefinitionBuilder<SyncItem>().Ascending((a) => a.Id)
Sort = new SortDefinitionBuilder<SyncItem>().Ascending("_id")
});
}

public Task<IAsyncCursor<SyncItem>> FindItemsById(string userId, IEnumerable<string> ids, bool all, int batchSize)
{
var filters = new List<FilterDefinition<SyncItem>>(new[] { Builders<SyncItem>.Filter.Eq((i) => i.UserId, userId) });
var filters = new List<FilterDefinition<SyncItem>>(new[] { Builders<SyncItem>.Filter.Eq("UserId", userId) });

if (!all) filters.Add(Builders<SyncItem>.Filter.In((i) => i.ItemId, ids));
if (!all) filters.Add(Builders<SyncItem>.Filter.In("ItemId", ids));

return Collection.FindAsync(Builders<SyncItem>.Filter.And(filters), new FindOptions<SyncItem>
{
Expand All @@ -89,46 +92,14 @@ public Task<IAsyncCursor<SyncItem>> FindItemsById(string userId, IEnumerable<str
});
}

public Task<IAsyncCursor<SyncItem>> GetIdsAsync(string userId, int batchSize)
{
var filter = Builders<SyncItem>.Filter.Eq((i) => i.UserId, userId);
return Collection.FindAsync(filter, new FindOptions<SyncItem>
{
BatchSize = batchSize,
AllowDiskUse = true,
AllowPartialResults = false,
NoCursorTimeout = true,
Sort = new SortDefinitionBuilder<SyncItem>().Ascending((a) => a.Id),
Projection = Builders<SyncItem>.Projection.Include((i) => i.ItemId)
});
}
// public async Task DeleteIdsAsync(string[] ids, string userId, CancellationToken token = default(CancellationToken))
// {
// await Collection.DeleteManyAsync<T>((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<SyncItem>.Filter.Eq("UserId", userId);
var writes = new List<WriteModel<SyncItem>>
{
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<SyncItem>(filter)
};
dbContext.AddCommand((handle, ct) => Collection.BulkWriteAsync(handle, writes, options: null, ct));
}

public void Upsert(SyncItem item, string userId, long dateSynced)
Expand All @@ -153,6 +124,12 @@ public void Upsert(SyncItem item, string userId, long dateSynced)
item.DateSynced = dateSynced;
item.UserId = userId;

var filter = Builders<SyncItem>.Filter.And(
Builders<SyncItem>.Filter.Eq("UserId", userId),
Builders<SyncItem>.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);
}
Expand Down

0 comments on commit 98c5f0c

Please sign in to comment.