mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-08-16 13:30:22 +02:00
sync: add upsertmany for faster bulk upserts
This commit is contained in:
@@ -80,21 +80,21 @@ namespace Notesnook.API.Hubs
|
|||||||
await base.OnConnectedAsync();
|
await base.OnConnectedAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Action<SyncItem, string, long> MapTypeToUpsertAction(string type)
|
private Action<IEnumerable<SyncItem>, string, long> MapTypeToUpsertAction(string type)
|
||||||
{
|
{
|
||||||
return type switch
|
return type switch
|
||||||
{
|
{
|
||||||
"settingitem" => Repositories.Settings.Upsert,
|
"settingitem" => Repositories.Settings.UpsertMany,
|
||||||
"attachment" => Repositories.Attachments.Upsert,
|
"attachment" => Repositories.Attachments.UpsertMany,
|
||||||
"note" => Repositories.Notes.Upsert,
|
"note" => Repositories.Notes.UpsertMany,
|
||||||
"notebook" => Repositories.Notebooks.Upsert,
|
"notebook" => Repositories.Notebooks.UpsertMany,
|
||||||
"content" => Repositories.Contents.Upsert,
|
"content" => Repositories.Contents.UpsertMany,
|
||||||
"shortcut" => Repositories.Shortcuts.Upsert,
|
"shortcut" => Repositories.Shortcuts.UpsertMany,
|
||||||
"reminder" => Repositories.Reminders.Upsert,
|
"reminder" => Repositories.Reminders.UpsertMany,
|
||||||
"relation" => Repositories.Relations.Upsert,
|
"relation" => Repositories.Relations.UpsertMany,
|
||||||
"color" => Repositories.Colors.Upsert,
|
"color" => Repositories.Colors.UpsertMany,
|
||||||
"vault" => Repositories.Vaults.Upsert,
|
"vault" => Repositories.Vaults.UpsertMany,
|
||||||
"tag" => Repositories.Tags.Upsert,
|
"tag" => Repositories.Tags.UpsertMany,
|
||||||
_ => null,
|
_ => null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,8 +130,45 @@ namespace Notesnook.API.Repositories
|
|||||||
);
|
);
|
||||||
|
|
||||||
dbContext.AddCommand((handle, ct) => Collection.ReplaceOneAsync(handle, filter, item, new ReplaceOptions { IsUpsert = true }, ct));
|
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);
|
|
||||||
|
public void UpsertMany(IEnumerable<SyncItem> items, string userId, long dateSynced)
|
||||||
|
{
|
||||||
|
var userIdFilter = Builders<SyncItem>.Filter.Eq("UserId", userId);
|
||||||
|
var writes = new List<WriteModel<SyncItem>>();
|
||||||
|
foreach (var item in items)
|
||||||
|
{
|
||||||
|
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))
|
||||||
|
{
|
||||||
|
throw new Exception($"Invalid alg identifier {item.Algorithm}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle case where the cipher is corrupted.
|
||||||
|
if (!IsBase64String(item.Cipher))
|
||||||
|
{
|
||||||
|
Slogger<SyncHub>.Error("Upsert", "Corrupted", item.ItemId, item.Length.ToString(), item.Cipher);
|
||||||
|
throw new Exception($"Corrupted item \"{item.ItemId}\" in collection \"{this.collectionName}\". Please report this error to support@streetwriters.co.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var filter = Builders<SyncItem>.Filter.And(
|
||||||
|
userIdFilter,
|
||||||
|
Builders<SyncItem>.Filter.Eq("ItemId", item.ItemId)
|
||||||
|
);
|
||||||
|
|
||||||
|
item.DateSynced = dateSynced;
|
||||||
|
item.UserId = userId;
|
||||||
|
|
||||||
|
writes.Add(new ReplaceOneModel<SyncItem>(filter, item)
|
||||||
|
{
|
||||||
|
IsUpsert = true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
dbContext.AddCommand((handle, ct) => Collection.BulkWriteAsync(handle, writes, options: new BulkWriteOptions { IsOrdered = false }, ct));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool IsBase64String(string value)
|
private static bool IsBase64String(string value)
|
||||||
|
|||||||
Reference in New Issue
Block a user