nezuo / lapis

A DataStore abstraction library for Roblox
https://nezuo.github.io/lapis/
MIT License
55 stars 10 forks source link

Fix save merging #13

Closed nezuo closed 1 year ago

nezuo commented 1 year ago

Save merging is when you override a save that hasn't been processed yet with a new one.

-- This could result in only a single save call since they get merged.
document:save()
document:save()
document:save()

This fixes an edge case where we merge saves when it's not safe. For example, if you call save and then call close while the save is running UpdateAsync it would result in the document never being closed because the merge doesn't take affect since the UpdateAsync call has already been made.

Essentially it's always safe to merge saves unless UpdateAsync is running.

This PR fixes this edge case by keeping track of ongoingSaves and pendingSaves. ongoingSaves are saves that have been added to the throttle queue. pendingSaves are saves that are waiting for their respective ongoingSave to finish. We only merge pending saves now, we do not merge pending saves with ongoing saves. This means the merge algorithm is not completely optimal since it can be safe to merge saves even if they are in the throttle queue as long as they haven't called UpdateAsync. I plan to use the optimal algorithm eventually.