-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add squashed update after deleting document history #53
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,7 +224,6 @@ async def read(self) -> AsyncIterator[Tuple[bytes, bytes]]: # type: ignore | |
raise YDocNotFound | ||
|
||
async def write(self, data: bytes) -> None: | ||
metadata = await self.get_metadata() | ||
await self.db_initialized | ||
async with aiosqlite.connect(self.db_path) as db: | ||
# first, determine time elapsed since last update | ||
|
@@ -235,11 +234,26 @@ async def write(self, data: bytes) -> None: | |
row = await cursor.fetchone() | ||
diff = (time.time() - row[0]) if row else 0 | ||
|
||
# if diff > document_ttl, delete document history | ||
if diff > self.document_ttl: | ||
# squash updates | ||
ydoc = Y.YDoc() | ||
async with db.execute( | ||
"SELECT yupdate FROM yupdates WHERE path = ?", (self.path,) | ||
) as cursor: | ||
async for update, in cursor: | ||
Y.apply_update(ydoc, update) | ||
# delete history | ||
await db.execute("DELETE FROM yupdates WHERE path = ?", (self.path,)) | ||
# insert squashed updates | ||
squashed_update = Y.encode_state_as_update(ydoc) | ||
metadata = await self.get_metadata() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we lift this definition out of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so, as the metadata can depend on time, for instance. |
||
await db.execute( | ||
"INSERT INTO yupdates VALUES (?, ?, ?, ?)", | ||
(self.path, squashed_update, metadata, time.time()), | ||
) | ||
|
||
# finally, write this update to the DB | ||
metadata = await self.get_metadata() | ||
await db.execute( | ||
"INSERT INTO yupdates VALUES (?, ?, ?, ?)", | ||
(self.path, data, metadata, time.time()), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.