-
Notifications
You must be signed in to change notification settings - Fork 945
Expose operation counts from Persistence layer (take 2) #2165
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 4 commits into
mrschmidt/indexfree-master
from
mrschmidt/indexfree-counter2
Sep 10, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
153 changes: 153 additions & 0 deletions
153
packages/firestore/test/unit/local/forwarding_counting_query_engine.ts
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 |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/** | ||
* @license | ||
* Copyright 2019 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { QueryEngine } from '../../../src/local/query_engine'; | ||
import { LocalDocumentsView } from '../../../src/local/local_documents_view'; | ||
import { PersistenceTransaction } from '../../../src/local/persistence'; | ||
import { Query } from '../../../src/core/query'; | ||
import { SnapshotVersion } from '../../../src/core/snapshot_version'; | ||
import { PersistencePromise } from '../../../src/local/persistence_promise'; | ||
import { DocumentMap } from '../../../src/model/collections'; | ||
import { RemoteDocumentCache } from '../../../src/local/remote_document_cache'; | ||
import { MutationQueue } from '../../../src/local/mutation_queue'; | ||
|
||
/** | ||
* A test-only query engine that forwards all API calls and exposes the number | ||
* of documents and mutations read. | ||
*/ | ||
export class CountingQueryEngine implements QueryEngine { | ||
/** | ||
* The number of mutations returned by the MutationQueue (since the last call | ||
* to `resetCounts()`) | ||
*/ | ||
mutationsRead = 0; | ||
|
||
/** | ||
* The number of documents returned by the RemoteDocumentCache (since the | ||
* last call to `resetCounts()`) | ||
*/ | ||
documentsRead = 0; | ||
|
||
constructor(private readonly queryEngine: QueryEngine) {} | ||
|
||
resetCounts(): void { | ||
this.mutationsRead = 0; | ||
this.documentsRead = 0; | ||
} | ||
|
||
getDocumentsMatchingQuery( | ||
transaction: PersistenceTransaction, | ||
query: Query, | ||
sinceReadTime: SnapshotVersion | ||
): PersistencePromise<DocumentMap> { | ||
return this.queryEngine.getDocumentsMatchingQuery( | ||
transaction, | ||
query, | ||
sinceReadTime | ||
); | ||
} | ||
|
||
setLocalDocumentsView(localDocuments: LocalDocumentsView): void { | ||
const view = new LocalDocumentsView( | ||
this.wrapRemoteDocumentCache(localDocuments.remoteDocumentCache), | ||
this.wrapMutationQueue(localDocuments.mutationQueue), | ||
localDocuments.indexManager | ||
); | ||
|
||
return this.queryEngine.setLocalDocumentsView(view); | ||
} | ||
|
||
private wrapRemoteDocumentCache( | ||
subject: RemoteDocumentCache | ||
): RemoteDocumentCache { | ||
return { | ||
getDocumentsMatchingQuery: (transaction, query, sinceReadTime) => { | ||
return subject | ||
.getDocumentsMatchingQuery(transaction, query, sinceReadTime) | ||
.next(result => { | ||
this.documentsRead += result.size; | ||
return result; | ||
}); | ||
}, | ||
getEntries: (transaction, documentKeys) => { | ||
return subject.getEntries(transaction, documentKeys).next(result => { | ||
this.documentsRead += result.size; | ||
return result; | ||
}); | ||
}, | ||
getEntry: (transaction, documentKey) => { | ||
return subject.getEntry(transaction, documentKey).next(result => { | ||
this.documentsRead += result ? 1 : 0; | ||
return result; | ||
}); | ||
}, | ||
getNewDocumentChanges: subject.getNewDocumentChanges, | ||
getSize: subject.getSize, | ||
newChangeBuffer: subject.newChangeBuffer | ||
}; | ||
} | ||
|
||
private wrapMutationQueue(subject: MutationQueue): MutationQueue { | ||
return { | ||
acknowledgeBatch: subject.acknowledgeBatch, | ||
addMutationBatch: subject.addMutationBatch, | ||
checkEmpty: subject.checkEmpty, | ||
getAllMutationBatches: transaction => { | ||
return subject.getAllMutationBatches(transaction).next(result => { | ||
this.mutationsRead += result.length; | ||
return result; | ||
}); | ||
}, | ||
getAllMutationBatchesAffectingDocumentKey: (transaction, documentKey) => { | ||
return subject | ||
.getAllMutationBatchesAffectingDocumentKey(transaction, documentKey) | ||
.next(result => { | ||
this.mutationsRead += result.length; | ||
return result; | ||
}); | ||
}, | ||
getAllMutationBatchesAffectingDocumentKeys: ( | ||
transaction, | ||
documentKeys | ||
) => { | ||
return subject | ||
.getAllMutationBatchesAffectingDocumentKeys(transaction, documentKeys) | ||
.next(result => { | ||
this.mutationsRead += result.length; | ||
return result; | ||
}); | ||
}, | ||
getAllMutationBatchesAffectingQuery: (transaction, query) => { | ||
return subject | ||
.getAllMutationBatchesAffectingQuery(transaction, query) | ||
.next(result => { | ||
this.mutationsRead += result.length; | ||
return result; | ||
}); | ||
}, | ||
getHighestUnacknowledgedBatchId: subject.getHighestUnacknowledgedBatchId, | ||
getLastStreamToken: subject.getLastStreamToken, | ||
getNextMutationBatchAfterBatchId: subject.getNextMutationBatchAfterBatchId, | ||
lookupMutationBatch: subject.lookupMutationBatch, | ||
lookupMutationKeys: subject.lookupMutationKeys, | ||
performConsistencyCheck: subject.performConsistencyCheck, | ||
removeCachedMutationKeys: subject.removeCachedMutationKeys, | ||
removeMutationBatch: subject.removeMutationBatch, | ||
setLastStreamToken: subject.setLastStreamToken | ||
}; | ||
} | ||
} |
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
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'm guessing this is why coverage decreased. Making these readonly probably costs us a few bytes. 😄