-
Notifications
You must be signed in to change notification settings - Fork 996
Add Numeric Add #1368
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
Merged
Add Numeric Add #1368
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
03e0063
Add Numeric Add
schmidt-sebastian 4f835a4
Review comments
schmidt-sebastian 2a9ce2a
Rename numericAdd() to increment()
schmidt-sebastian 83ddee8
Merge branch 'master' into mrschmidt-increment3
schmidt-sebastian e62de99
Use increment in Protobuf message
schmidt-sebastian 06ff0d3
[AUTOMATED]: Prettier Code Styling
schmidt-sebastian 9c4490c
Remove unused 'size' varibale
schmidt-sebastian d83d07a
Merge
schmidt-sebastian 81332de
Test fix
schmidt-sebastian 3e64e2f
[AUTOMATED]: Prettier Code Styling
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
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
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 |
|---|---|---|
|
|
@@ -27,9 +27,9 @@ import { | |
| maybeDocumentMap, | ||
| MaybeDocumentMap | ||
| } from '../model/collections'; | ||
| import { MaybeDocument } from '../model/document'; | ||
| import { Document, MaybeDocument } from '../model/document'; | ||
| import { DocumentKey } from '../model/document_key'; | ||
| import { Mutation } from '../model/mutation'; | ||
| import { Mutation, PatchMutation, Precondition } from '../model/mutation'; | ||
| import { | ||
| BATCHID_UNKNOWN, | ||
| MutationBatch, | ||
|
|
@@ -40,6 +40,7 @@ import { assert } from '../util/assert'; | |
| import * as log from '../util/log'; | ||
| import * as objUtils from '../util/obj'; | ||
|
|
||
| import { ObjectValue } from '../model/field_value'; | ||
| import { LocalDocumentsView } from './local_documents_view'; | ||
| import { LocalViewChanges } from './local_view_changes'; | ||
| import { LruGarbageCollector, LruResults } from './lru_garbage_collector'; | ||
|
|
@@ -245,24 +246,65 @@ export class LocalStore { | |
| } | ||
| /* Accept locally generated Mutations and commit them to storage. */ | ||
| localWrite(mutations: Mutation[]): Promise<LocalWriteResult> { | ||
| const localWriteTime = Timestamp.now(); | ||
| const keys = mutations.reduce( | ||
| (keys, m) => keys.add(m.key), | ||
| documentKeySet() | ||
| ); | ||
|
Contributor
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. Nice!
Contributor
Author
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. s/Nice/Oh... Nice/g |
||
|
|
||
| return this.persistence.runTransaction( | ||
| 'Locally write mutations', | ||
| 'readwrite', | ||
| txn => { | ||
| let batch: MutationBatch; | ||
| const localWriteTime = Timestamp.now(); | ||
| return this.mutationQueue | ||
| .addMutationBatch(txn, localWriteTime, mutations) | ||
| .next(promisedBatch => { | ||
| batch = promisedBatch; | ||
| // TODO(koss): This is doing an N^2 update by replaying ALL the | ||
| // mutations on each document (instead of just the ones added) in | ||
| // this batch. | ||
| const keys = batch.keys(); | ||
| return this.localDocuments.getDocuments(txn, keys); | ||
| }) | ||
| .next((changedDocuments: MaybeDocumentMap) => { | ||
| return { batchId: batch.batchId, changes: changedDocuments }; | ||
| // Load and apply all existing mutations. This lets us compute the | ||
| // current base state for all non-idempotent transforms before applying | ||
| // any additional user-provided writes. | ||
| return this.localDocuments | ||
| .getDocuments(txn, keys) | ||
| .next(existingDocs => { | ||
| // For non-idempotent mutations (such as `FieldValue.increment()`), | ||
| // we record the base state in a separate patch mutation. This is | ||
| // later used to guarantee consistent values and prevents flicker | ||
| // even if the backend sends us an update that already includes our | ||
| // transform. | ||
| const baseMutations: Mutation[] = []; | ||
|
|
||
| for (const mutation of mutations) { | ||
| const maybeDoc = existingDocs.get(mutation.key); | ||
| if (!mutation.isIdempotent) { | ||
| // Theoretically, we should only include non-idempotent fields | ||
| // in this field mask as this mask is used to populate the base | ||
| // state for all DocumentTransforms. By including all fields, | ||
| // we incorrectly prevent rebasing of idempotent transforms | ||
| // (such as `arrayUnion()`) when any non-idempotent transforms | ||
| // are present. | ||
| const fieldMask = mutation.fieldMask; | ||
| if (fieldMask) { | ||
| const baseValues = | ||
| maybeDoc instanceof Document | ||
| ? fieldMask.applyTo(maybeDoc.data) | ||
| : ObjectValue.EMPTY; | ||
| // NOTE: The base state should only be applied if there's some | ||
| // existing document to override, so use a Precondition of | ||
| // exists=true | ||
| baseMutations.push( | ||
| new PatchMutation( | ||
| mutation.key, | ||
| baseValues, | ||
| fieldMask, | ||
| Precondition.exists(true) | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return this.mutationQueue | ||
| .addMutationBatch(txn, localWriteTime, baseMutations, mutations) | ||
| .next(batch => { | ||
| const changes = batch.applyToLocalDocumentSet(existingDocs); | ||
| return { batchId: batch.batchId, changes }; | ||
| }); | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
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
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.
I would do
public readonlyto make the intent clear.