16
16
*/
17
17
18
18
import { ParsedSetData , ParsedUpdateData } from '../api/user_data_reader' ;
19
- import { documentVersionMap } from '../model/collections' ;
20
19
import { Document , MaybeDocument , NoDocument } from '../model/document' ;
21
20
22
21
import { DocumentKey } from '../model/document_key' ;
@@ -34,14 +33,15 @@ import {
34
33
import { fail , debugAssert } from '../util/assert' ;
35
34
import { Code , FirestoreError } from '../util/error' ;
36
35
import { SnapshotVersion } from './snapshot_version' ;
36
+ import { ResourcePath } from '../model/path' ;
37
37
38
38
/**
39
39
* Internal transaction object responsible for accumulating the mutations to
40
40
* perform and the base versions for any documents read.
41
41
*/
42
42
export class Transaction {
43
43
// The version of each document that was read during this transaction.
44
- private readVersions = documentVersionMap ( ) ;
44
+ private readVersions = new Map < /* path */ string , SnapshotVersion > ( ) ;
45
45
private mutations : Mutation [ ] = [ ] ;
46
46
private committed = false ;
47
47
@@ -106,14 +106,15 @@ export class Transaction {
106
106
if ( this . lastWriteError ) {
107
107
throw this . lastWriteError ;
108
108
}
109
- let unwritten = this . readVersions ;
109
+ const unwritten = this . readVersions ;
110
110
// For each mutation, note that the doc was written.
111
111
this . mutations . forEach ( mutation => {
112
- unwritten = unwritten . remove ( mutation . key ) ;
112
+ unwritten . delete ( mutation . key . toString ( ) ) ;
113
113
} ) ;
114
114
// For each document that was read but not written to, we want to perform
115
115
// a `verify` operation.
116
- unwritten . forEach ( ( key , _version ) => {
116
+ unwritten . forEach ( ( _ , path ) => {
117
+ const key = new DocumentKey ( ResourcePath . fromString ( path ) ) ;
117
118
this . mutations . push ( new VerifyMutation ( key , this . precondition ( key ) ) ) ;
118
119
} ) ;
119
120
await invokeCommitRpc ( this . datastore , this . mutations ) ;
@@ -132,8 +133,8 @@ export class Transaction {
132
133
throw fail ( 'Document in a transaction was a ' + doc . constructor . name ) ;
133
134
}
134
135
135
- const existingVersion = this . readVersions . get ( doc . key ) ;
136
- if ( existingVersion !== null ) {
136
+ const existingVersion = this . readVersions . get ( doc . key . toString ( ) ) ;
137
+ if ( existingVersion ) {
137
138
if ( ! docVersion . isEqual ( existingVersion ) ) {
138
139
// This transaction will fail no matter what.
139
140
throw new FirestoreError (
@@ -142,7 +143,7 @@ export class Transaction {
142
143
) ;
143
144
}
144
145
} else {
145
- this . readVersions = this . readVersions . insert ( doc . key , docVersion ) ;
146
+ this . readVersions . set ( doc . key . toString ( ) , docVersion ) ;
146
147
}
147
148
}
148
149
@@ -151,7 +152,7 @@ export class Transaction {
151
152
* as a precondition, or no precondition if it was not read.
152
153
*/
153
154
private precondition ( key : DocumentKey ) : Precondition {
154
- const version = this . readVersions . get ( key ) ;
155
+ const version = this . readVersions . get ( key . toString ( ) ) ;
155
156
if ( ! this . writtenDocs . has ( key ) && version ) {
156
157
return Precondition . updateTime ( version ) ;
157
158
} else {
@@ -163,7 +164,7 @@ export class Transaction {
163
164
* Returns the precondition for a document if the operation is an update.
164
165
*/
165
166
private preconditionForUpdate ( key : DocumentKey ) : Precondition {
166
- const version = this . readVersions . get ( key ) ;
167
+ const version = this . readVersions . get ( key . toString ( ) ) ;
167
168
// The first time a document is written, we want to take into account the
168
169
// read time and existence
169
170
if ( ! this . writtenDocs . has ( key ) && version ) {
0 commit comments