mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-09-30 18:29:36 +02:00
sync: use builders instead of linq for mongodb queries
This commit is contained in:
1 parent
ad4e43e879
commit
98c5f0c96f
1 file changed
+20
-43
@@ -32,6 +32,7 @@ using Notesnook.API.Hubs;
|
|||||||
using Notesnook.API.Interfaces;
|
using Notesnook.API.Interfaces;
|
||||||
using Notesnook.API.Models;
|
using Notesnook.API.Models;
|
||||||
using Streetwriters.Common;
|
using Streetwriters.Common;
|
||||||
|
using Streetwriters.Data.DbContexts;
|
||||||
using Streetwriters.Data.Interfaces;
|
using Streetwriters.Data.Interfaces;
|
||||||
using Streetwriters.Data.Repositories;
|
using Streetwriters.Data.Repositories;
|
||||||
|
|
||||||
@@ -52,7 +53,7 @@ namespace Notesnook.API.Repositories
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly List<string> ALGORITHMS = new List<string> { Algorithms.Default };
|
private readonly List<string> ALGORITHMS = [Algorithms.Default];
|
||||||
private bool IsValidAlgorithm(string algorithm)
|
private bool IsValidAlgorithm(string algorithm)
|
||||||
{
|
{
|
||||||
return ALGORITHMS.Contains(algorithm);
|
return ALGORITHMS.Contains(algorithm);
|
||||||
@@ -60,25 +61,27 @@ namespace Notesnook.API.Repositories
|
|||||||
|
|
||||||
public Task<long> CountItemsSyncedAfterAsync(string userId, long timestamp)
|
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)
|
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,
|
BatchSize = batchSize,
|
||||||
AllowDiskUse = true,
|
AllowDiskUse = true,
|
||||||
AllowPartialResults = false,
|
AllowPartialResults = false,
|
||||||
NoCursorTimeout = true,
|
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)
|
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>
|
return Collection.FindAsync(Builders<SyncItem>.Filter.And(filters), new FindOptions<SyncItem>
|
||||||
{
|
{
|
||||||
@@ -89,46 +92,14 @@ namespace Notesnook.API.Repositories
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
public void DeleteByUserId(string userId)
|
||||||
{
|
{
|
||||||
dbContext.AddCommand((handle, ct) => Collection.DeleteManyAsync(handle, (i) => i.UserId == userId, cancellationToken: ct));
|
var filter = Builders<SyncItem>.Filter.Eq("UserId", userId);
|
||||||
}
|
var writes = new List<WriteModel<SyncItem>>
|
||||||
|
|
||||||
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.");
|
new DeleteManyModel<SyncItem>(filter)
|
||||||
}
|
};
|
||||||
|
dbContext.AddCommand((handle, ct) => Collection.BulkWriteAsync(handle, writes, options: null, ct));
|
||||||
if (!IsValidAlgorithm(item.Algorithm))
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Upsert(SyncItem item, string userId, long dateSynced)
|
public void Upsert(SyncItem item, string userId, long dateSynced)
|
||||||
@@ -153,6 +124,12 @@ namespace Notesnook.API.Repositories
|
|||||||
item.DateSynced = dateSynced;
|
item.DateSynced = dateSynced;
|
||||||
item.UserId = userId;
|
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);
|
// await base.UpsertAsync(item, (x) => (x.ItemId == item.ItemId) && x.UserId == userId);
|
||||||
base.Upsert(item, (x) => x.UserId == userId && x.ItemId == item.ItemId);
|
base.Upsert(item, (x) => x.UserId == userId && x.ItemId == item.ItemId);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user