18
18
19
19
#import < Protobuf/GPBProtocolBuffers.h>
20
20
21
+ #include < memory>
21
22
#include < utility>
22
23
23
24
#import " Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
26
27
#import " Firestore/Source/Local/FSTQueryData.h"
27
28
#import " Firestore/Source/Local/FSTReferenceSet.h"
28
29
30
+ #include " Firestore/core/src/firebase/firestore/local/memory_query_cache.h"
29
31
#include " Firestore/core/src/firebase/firestore/model/document_key.h"
30
32
#include " Firestore/core/src/firebase/firestore/model/snapshot_version.h"
33
+ #include " absl/memory/memory.h"
31
34
35
+ using firebase::firestore::local::MemoryQueryCache;
32
36
using firebase::firestore::model::DocumentKey;
33
37
using firebase::firestore::model::DocumentKeySet;
34
38
using firebase::firestore::model::ListenSequenceNumber;
37
41
38
42
NS_ASSUME_NONNULL_BEGIN
39
43
40
- @interface FSTMemoryQueryCache ()
41
-
42
- /* * Maps a query to the data about that query. */
43
- @property (nonatomic , strong , readonly ) NSMutableDictionary <FSTQuery *, FSTQueryData *> *queries;
44
-
45
- /* * A ordered bidirectional mapping between documents and the remote target IDs. */
46
- @property (nonatomic , strong , readonly ) FSTReferenceSet *references;
47
-
48
- /* * The highest numbered target ID encountered. */
49
- @property (nonatomic , assign ) TargetId highestTargetID;
50
-
51
- @property (nonatomic , assign ) ListenSequenceNumber highestListenSequenceNumber;
52
-
53
- @end
54
-
55
44
@implementation FSTMemoryQueryCache {
56
- FSTMemoryPersistence *_persistence;
57
- /* * The last received snapshot version. */
58
- SnapshotVersion _lastRemoteSnapshotVersion;
45
+ std::unique_ptr<MemoryQueryCache> _cache;
59
46
}
60
47
61
48
- (instancetype )initWithPersistence : (FSTMemoryPersistence *)persistence {
62
49
if (self = [super init ]) {
63
- _persistence = persistence;
64
- _queries = [NSMutableDictionary dictionary ];
65
- _references = [[FSTReferenceSet alloc ] init ];
66
- _lastRemoteSnapshotVersion = SnapshotVersion::None ();
50
+ _cache = absl::make_unique<MemoryQueryCache>(persistence);
67
51
}
68
52
return self;
69
53
}
@@ -72,112 +56,74 @@ - (instancetype)initWithPersistence:(FSTMemoryPersistence *)persistence {
72
56
#pragma mark Query tracking
73
57
74
58
- (TargetId)highestTargetID {
75
- return _highestTargetID ;
59
+ return _cache-> highest_target_id () ;
76
60
}
77
61
78
62
- (ListenSequenceNumber)highestListenSequenceNumber {
79
- return _highestListenSequenceNumber ;
63
+ return _cache-> highest_listen_sequence_number () ;
80
64
}
81
65
82
66
- (const SnapshotVersion &)lastRemoteSnapshotVersion {
83
- return _lastRemoteSnapshotVersion ;
67
+ return _cache-> last_remote_snapshot_version () ;
84
68
}
85
69
86
70
- (void )setLastRemoteSnapshotVersion : (SnapshotVersion)snapshotVersion {
87
- _lastRemoteSnapshotVersion = std::move (snapshotVersion);
71
+ _cache-> set_last_remote_snapshot_version ( std::move (snapshotVersion) );
88
72
}
89
73
90
74
- (void )addQueryData : (FSTQueryData *)queryData {
91
- self.queries [queryData.query] = queryData;
92
- if (queryData.targetID > self.highestTargetID ) {
93
- self.highestTargetID = queryData.targetID ;
94
- }
95
- if (queryData.sequenceNumber > self.highestListenSequenceNumber ) {
96
- self.highestListenSequenceNumber = queryData.sequenceNumber ;
97
- }
75
+ _cache->AddTarget (queryData);
98
76
}
99
77
100
78
- (void )updateQueryData : (FSTQueryData *)queryData {
101
- self.queries [queryData.query] = queryData;
102
- if (queryData.targetID > self.highestTargetID ) {
103
- self.highestTargetID = queryData.targetID ;
104
- }
105
- if (queryData.sequenceNumber > self.highestListenSequenceNumber ) {
106
- self.highestListenSequenceNumber = queryData.sequenceNumber ;
107
- }
79
+ _cache->UpdateTarget (queryData);
108
80
}
109
81
110
82
- (int32_t )count {
111
- return ( int32_t )[ self .queries count ] ;
83
+ return _cache-> count () ;
112
84
}
113
85
114
86
- (void )removeQueryData : (FSTQueryData *)queryData {
115
- [self .queries removeObjectForKey: queryData.query];
116
- [self .references removeReferencesForID: queryData.targetID];
87
+ _cache->RemoveTarget (queryData);
117
88
}
118
89
119
90
- (nullable FSTQueryData *)queryDataForQuery : (FSTQuery *)query {
120
- return self. queries [ query] ;
91
+ return _cache-> GetTarget ( query) ;
121
92
}
122
93
123
94
- (void )enumerateTargetsUsingBlock : (void (^)(FSTQueryData *queryData, BOOL *stop))block {
124
- [self .queries
125
- enumerateKeysAndObjectsUsingBlock: ^(FSTQuery *key, FSTQueryData *queryData, BOOL *stop) {
126
- block (queryData, stop);
127
- }];
95
+ _cache->EnumerateTargets (block);
128
96
}
129
97
130
98
- (int )removeQueriesThroughSequenceNumber : (ListenSequenceNumber)sequenceNumber
131
99
liveQueries : (NSDictionary <NSNumber *, FSTQueryData *> *)liveQueries {
132
- NSMutableArray <FSTQuery *> *toRemove = [NSMutableArray array ];
133
- [self .queries
134
- enumerateKeysAndObjectsUsingBlock: ^(FSTQuery *query, FSTQueryData *queryData, BOOL *stop) {
135
- if (queryData.sequenceNumber <= sequenceNumber) {
136
- if (liveQueries[@(queryData.targetID)] == nil ) {
137
- [toRemove addObject: query];
138
- [self .references removeReferencesForID: queryData.targetID];
139
- }
140
- }
141
- }];
142
- [self .queries removeObjectsForKeys: toRemove];
143
- return (int )[toRemove count ];
100
+ return _cache->RemoveTargets (sequenceNumber, liveQueries);
144
101
}
145
102
146
103
#pragma mark Reference tracking
147
104
148
105
- (void )addMatchingKeys : (const DocumentKeySet &)keys forTargetID : (TargetId)targetID {
149
- [self .references addReferencesToKeys: keys forID: targetID];
150
- for (const DocumentKey &key : keys) {
151
- [_persistence.referenceDelegate addReference: key];
152
- }
106
+ _cache->AddMatchingKeys (keys, targetID);
153
107
}
154
108
155
109
- (void )removeMatchingKeys : (const DocumentKeySet &)keys forTargetID : (TargetId)targetID {
156
- [self .references removeReferencesToKeys: keys forID: targetID];
157
- for (const DocumentKey &key : keys) {
158
- [_persistence.referenceDelegate removeReference: key];
159
- }
110
+ _cache->RemoveMatchingKeys (keys, targetID);
160
111
}
161
112
162
113
- (void )removeMatchingKeysForTargetID : (TargetId)targetID {
163
- [ self .references removeReferencesForID: targetID] ;
114
+ _cache-> RemoveAllKeysForTarget ( targetID) ;
164
115
}
165
116
166
117
- (DocumentKeySet)matchingKeysForTargetID : (TargetId)targetID {
167
- return [ self .references referencedKeysForID: targetID] ;
118
+ return _cache-> GetMatchingKeys ( targetID) ;
168
119
}
169
120
170
121
- (BOOL )containsKey : (const firebase::firestore::model::DocumentKey &)key {
171
- return [ self .references containsKey: key] ;
122
+ return _cache-> Contains ( key) ;
172
123
}
173
124
174
125
- (size_t )byteSizeWithSerializer : (FSTLocalSerializer *)serializer {
175
- __block size_t count = 0 ;
176
- [self .queries
177
- enumerateKeysAndObjectsUsingBlock: ^(FSTQuery *key, FSTQueryData *queryData, BOOL *stop) {
178
- count += [[serializer encodedQueryData: queryData] serializedSize ];
179
- }];
180
- return count;
126
+ return _cache->CalculateByteSize (serializer);
181
127
}
182
128
183
129
@end
0 commit comments