-
Notifications
You must be signed in to change notification settings - Fork 927
Make getNewDocumentChanges() idempotent #2255
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
Merged
schmidt-sebastian
merged 15 commits into
mrschmidt/idempotent
from
mrschmidt/getnewchanges
Oct 11, 2019
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
48642ad
Add runTransaction parameter
schmidt-sebastian e4cdfe0
Add transaction retries
schmidt-sebastian d3669b6
Unit test fix
schmidt-sebastian 10c2f8e
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian a1e5a00
First round of feedback
schmidt-sebastian aee20aa
Lint
schmidt-sebastian 06f9e83
Mark (most) readonly calls as idempotent
schmidt-sebastian 2dc4170
Fix unit tests
schmidt-sebastian d6ed20b
Make getNewDocumentChanges() idempotent
schmidt-sebastian 7166a3e
Lint fix
schmidt-sebastian 9933f56
More lint
schmidt-sebastian 57f22d8
Merge
schmidt-sebastian 58b3e41
Review comments
schmidt-sebastian 877a2ca
Merge branch 'mrschmidt/idempotent' into mrschmidt/getnewchanges
schmidt-sebastian 7f6e989
Reduce diff
schmidt-sebastian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,9 +55,6 @@ import { | |
import { ObjectMap } from '../util/obj_map'; | ||
|
||
export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache { | ||
/** The read time of the last entry consumed by `getNewDocumentChanges()`. */ | ||
private lastProcessedReadTime = SnapshotVersion.MIN; | ||
|
||
/** | ||
* @param {LocalSerializer} serializer The document serializer. | ||
* @param {IndexManager} indexManager The query indexes that need to be maintained. | ||
|
@@ -67,18 +64,6 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache { | |
private readonly indexManager: IndexManager | ||
) {} | ||
|
||
/** | ||
* Starts up the remote document cache. | ||
* | ||
* Reads the ID of the last document change from the documentChanges store. | ||
* Existing changes will not be returned as part of | ||
* `getNewDocumentChanges()`. | ||
*/ | ||
// PORTING NOTE: This is only used for multi-tab synchronization. | ||
start(transaction: SimpleDbTransaction): PersistencePromise<void> { | ||
return this.synchronizeLastProcessedReadTime(transaction); | ||
} | ||
|
||
/** | ||
* Adds the supplied entries to the cache. | ||
* | ||
|
@@ -313,59 +298,74 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache { | |
.next(() => results); | ||
} | ||
|
||
getNewDocumentChanges( | ||
transaction: PersistenceTransaction | ||
): PersistencePromise<MaybeDocumentMap> { | ||
/** | ||
* Returns the set of documents that have been updated since the specified read | ||
* time. | ||
*/ | ||
// PORTING NOTE: This is only used for multi-tab synchronization. | ||
getDocumentChanges( | ||
transaction: PersistenceTransaction, | ||
sinceReadTime: SnapshotVersion | ||
): PersistencePromise<{ | ||
changedDocs: MaybeDocumentMap; | ||
readTime: SnapshotVersion; | ||
}> { | ||
let changedDocs = maybeDocumentMap(); | ||
|
||
const lastReadTime = this.serializer.toDbTimestampKey( | ||
this.lastProcessedReadTime | ||
); | ||
let lastReadTime = this.serializer.toDbTimestampKey(sinceReadTime); | ||
|
||
const documentsStore = remoteDocumentsStore(transaction); | ||
const range = IDBKeyRange.lowerBound(lastReadTime, true); | ||
return documentsStore | ||
.iterate( | ||
{ index: DbRemoteDocument.readTimeIndex, range }, | ||
(_, dbRemoteDoc) => { | ||
// Unlike `getEntry()` and others, `getNewDocumentChanges()` parses | ||
// Unlike `getEntry()` and others, `getDocumentChanges()` parses | ||
// the documents directly since we want to keep sentinel deletes. | ||
const doc = this.serializer.fromDbRemoteDocument(dbRemoteDoc); | ||
changedDocs = changedDocs.insert(doc.key, doc); | ||
this.lastProcessedReadTime = this.serializer.fromDbTimestampKey( | ||
dbRemoteDoc.readTime! | ||
); | ||
lastReadTime = dbRemoteDoc.readTime!; | ||
} | ||
) | ||
.next(() => changedDocs); | ||
.next(() => { | ||
return { | ||
changedDocs, | ||
readTime: this.serializer.fromDbTimestampKey(lastReadTime) | ||
}; | ||
}); | ||
} | ||
|
||
/** | ||
* Sets the last processed read time to the maximum read time of the backing | ||
* object store, allowing calls to getNewDocumentChanges() to return subsequent | ||
* changes. | ||
* Returns the last document that has changed, as well as the read time of the | ||
* last change. If no document has changed, returns SnapshotVersion.MIN. | ||
*/ | ||
private synchronizeLastProcessedReadTime( | ||
transaction: SimpleDbTransaction | ||
): PersistencePromise<void> { | ||
const documentsStore = SimpleDb.getStore< | ||
DbRemoteDocumentKey, | ||
DbRemoteDocument | ||
>(transaction, DbRemoteDocument.store); | ||
|
||
// If there are no existing entries, we set `lastProcessedReadTime` to 0. | ||
this.lastProcessedReadTime = SnapshotVersion.forDeletedDoc(); | ||
return documentsStore.iterate( | ||
{ index: DbRemoteDocument.readTimeIndex, reverse: true }, | ||
(key, value, control) => { | ||
if (value.readTime) { | ||
this.lastProcessedReadTime = this.serializer.fromDbTimestampKey( | ||
value.readTime | ||
); | ||
// PORTING NOTE: This is only used for multi-tab synchronization. | ||
getLastDocumentChange( | ||
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. This is used in the |
||
transaction: PersistenceTransaction | ||
): PersistencePromise<{ | ||
changedDoc: MaybeDocument | undefined; | ||
readTime: SnapshotVersion; | ||
}> { | ||
const documentsStore = remoteDocumentsStore(transaction); | ||
|
||
// If there are no existing entries, we return SnapshotVersion.MIN. | ||
let readTime = SnapshotVersion.MIN; | ||
let changedDoc: MaybeDocument | undefined; | ||
|
||
return documentsStore | ||
.iterate( | ||
{ index: DbRemoteDocument.readTimeIndex, reverse: true }, | ||
(key, dbRemoteDoc, control) => { | ||
changedDoc = this.serializer.fromDbRemoteDocument(dbRemoteDoc); | ||
if (dbRemoteDoc.readTime) { | ||
readTime = this.serializer.fromDbTimestampKey(dbRemoteDoc.readTime); | ||
} | ||
control.done(); | ||
} | ||
control.done(); | ||
} | ||
); | ||
) | ||
.next(() => { | ||
return { changedDoc, readTime }; | ||
}); | ||
} | ||
|
||
newChangeBuffer(options?: { | ||
|
@@ -413,7 +413,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache { | |
doc.version.isEqual(SnapshotVersion.forDeletedDoc()) | ||
) { | ||
// The document is a sentinel removal and should only be used in the | ||
// `getNewDocumentChanges()`. | ||
// `getDocumentChanges()`. | ||
return null; | ||
} | ||
|
||
|
@@ -438,7 +438,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache { | |
/** | ||
* @param documentCache The IndexedDbRemoteDocumentCache to apply the changes to. | ||
* @param trackRemovals Whether to create sentinel deletes that can be tracked by | ||
* `getNewDocumentChanges()`. | ||
* `getDocumentChanges()`. | ||
*/ | ||
constructor( | ||
private readonly documentCache: IndexedDbRemoteDocumentCache, | ||
|
@@ -478,7 +478,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache { | |
// In order to track removals, we store a "sentinel delete" in the | ||
// RemoteDocumentCache. This entry is represented by a NoDocument | ||
// with a version of 0 and ignored by `maybeDecodeDocument()` but | ||
// preserved in `getNewDocumentChanges()`. | ||
// preserved in `getDocumentChanges()`. | ||
const deletedDoc = this.documentCache.serializer.toDbRemoteDocument( | ||
new NoDocument(key, SnapshotVersion.forDeletedDoc()), | ||
this.readTime | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
FWIW, the
getNewDocumentChanges
still makes sense while explicitly passing thereadTime
. That name helps convey that it's a diff of changes.I don't oppose renaming it to
getDocumentChanges
if that's your preference, but I don't think it needed to change.