15
15
* limitations under the License.
16
16
*/
17
17
18
- import { DocumentKeySet } from '../model/collections' ;
18
+ import {
19
+ documentKeySet ,
20
+ DocumentKeySet ,
21
+ MutationMap ,
22
+ OverlayMap ,
23
+ newOverlayMap
24
+ } from '../model/collections' ;
19
25
import { DocumentKey } from '../model/document_key' ;
20
26
import { Mutation } from '../model/mutation' ;
21
27
import { Overlay } from '../model/overlay' ;
@@ -35,7 +41,7 @@ export class MemoryDocumentOverlayCache implements DocumentOverlayCache {
35
41
private overlays = new SortedMap < DocumentKey , Overlay > (
36
42
DocumentKey . comparator
37
43
) ;
38
- private overlayByBatchId = new Map < number , Set < DocumentKey > > ( ) ;
44
+ private overlayByBatchId = new Map < number , DocumentKeySet > ( ) ;
39
45
40
46
getOverlay (
41
47
transaction : PersistenceTransaction ,
@@ -47,9 +53,9 @@ export class MemoryDocumentOverlayCache implements DocumentOverlayCache {
47
53
saveOverlays (
48
54
transaction : PersistenceTransaction ,
49
55
largestBatchId : number ,
50
- overlays : Map < DocumentKey , Mutation >
56
+ overlays : MutationMap
51
57
) : PersistencePromise < void > {
52
- overlays . forEach ( mutation => {
58
+ overlays . forEach ( ( _ , mutation ) => {
53
59
this . saveOverlay ( transaction , largestBatchId , mutation ) ;
54
60
} ) ;
55
61
return PersistencePromise . resolve ( ) ;
@@ -72,8 +78,8 @@ export class MemoryDocumentOverlayCache implements DocumentOverlayCache {
72
78
transaction : PersistenceTransaction ,
73
79
collection : ResourcePath ,
74
80
sinceBatchId : number
75
- ) : PersistencePromise < Map < DocumentKey , Overlay > > {
76
- const result = new Map < DocumentKey , Overlay > ( ) ;
81
+ ) : PersistencePromise < OverlayMap > {
82
+ const result = newOverlayMap ( ) ;
77
83
78
84
const immediateChildrenPathLength = collection . length + 1 ;
79
85
const prefix = new DocumentKey ( collection . child ( '' ) ) ;
@@ -102,8 +108,8 @@ export class MemoryDocumentOverlayCache implements DocumentOverlayCache {
102
108
collectionGroup : string ,
103
109
sinceBatchId : number ,
104
110
count : number
105
- ) : PersistencePromise < Map < DocumentKey , Overlay > > {
106
- let batchIdToOverlays = new SortedMap < number , Map < DocumentKey , Overlay > > (
111
+ ) : PersistencePromise < OverlayMap > {
112
+ let batchIdToOverlays = new SortedMap < number , OverlayMap > (
107
113
( key1 : number , key2 : number ) => key1 - key2
108
114
) ;
109
115
@@ -118,7 +124,7 @@ export class MemoryDocumentOverlayCache implements DocumentOverlayCache {
118
124
if ( overlay . largestBatchId > sinceBatchId ) {
119
125
let overlaysForBatchId = batchIdToOverlays . get ( overlay . largestBatchId ) ;
120
126
if ( overlaysForBatchId === null ) {
121
- overlaysForBatchId = new Map < DocumentKey , Overlay > ( ) ;
127
+ overlaysForBatchId = newOverlayMap ( ) ;
122
128
batchIdToOverlays = batchIdToOverlays . insert (
123
129
overlay . largestBatchId ,
124
130
overlaysForBatchId
@@ -128,13 +134,13 @@ export class MemoryDocumentOverlayCache implements DocumentOverlayCache {
128
134
}
129
135
}
130
136
131
- const result = new Map < DocumentKey , Overlay > ( ) ;
137
+ const result = newOverlayMap ( ) ;
132
138
const batchIter = batchIdToOverlays . getIterator ( ) ;
133
139
while ( batchIter . hasNext ( ) ) {
134
140
const entry = batchIter . getNext ( ) ;
135
141
const overlays = entry . value ;
136
- overlays . forEach ( ( overlay , key ) => result . set ( key , overlay ) ) ;
137
- if ( result . size >= count ) {
142
+ overlays . forEach ( ( key , overlay ) => result . set ( key , overlay ) ) ;
143
+ if ( result . size ( ) >= count ) {
138
144
break ;
139
145
}
140
146
}
@@ -153,7 +159,10 @@ export class MemoryDocumentOverlayCache implements DocumentOverlayCache {
153
159
// Remove the association of the overlay to its batch id.
154
160
const existing = this . overlays . get ( mutation . key ) ;
155
161
if ( existing !== null ) {
156
- this . overlayByBatchId . get ( existing . largestBatchId ) ! . delete ( mutation . key ) ;
162
+ const newSet = this . overlayByBatchId
163
+ . get ( existing . largestBatchId ) !
164
+ . delete ( mutation . key ) ;
165
+ this . overlayByBatchId . set ( existing . largestBatchId , newSet ) ;
157
166
}
158
167
159
168
this . overlays = this . overlays . insert (
@@ -164,9 +173,9 @@ export class MemoryDocumentOverlayCache implements DocumentOverlayCache {
164
173
// Create the association of this overlay to the given largestBatchId.
165
174
let batch = this . overlayByBatchId . get ( largestBatchId ) ;
166
175
if ( batch === undefined ) {
167
- batch = new Set < DocumentKey > ( ) ;
176
+ batch = documentKeySet ( ) ;
168
177
this . overlayByBatchId . set ( largestBatchId , batch ) ;
169
178
}
170
- batch . add ( mutation . key ) ;
179
+ this . overlayByBatchId . set ( largestBatchId , batch . add ( mutation . key ) ) ;
171
180
}
172
181
}
0 commit comments