FSTLevelDBQueryCache.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
  17. #include <leveldb/db.h>
  18. #include <leveldb/write_batch.h>
  19. #include <string>
  20. #import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
  21. #import "Firestore/Source/Core/FSTQuery.h"
  22. #import "Firestore/Source/Local/FSTLevelDBKey.h"
  23. #import "Firestore/Source/Local/FSTLocalSerializer.h"
  24. #import "Firestore/Source/Local/FSTQueryData.h"
  25. #import "Firestore/Source/Local/FSTWriteGroup.h"
  26. #import "Firestore/Source/Model/FSTDocumentKey.h"
  27. #import "Firestore/Source/Util/FSTAssert.h"
  28. #include "Firestore/Port/ordered_code.h"
  29. #include "Firestore/Port/string_util.h"
  30. NS_ASSUME_NONNULL_BEGIN
  31. using Firestore::OrderedCode;
  32. using Firestore::StringView;
  33. using leveldb::DB;
  34. using leveldb::Iterator;
  35. using leveldb::ReadOptions;
  36. using leveldb::Slice;
  37. using leveldb::Status;
  38. using leveldb::WriteOptions;
  39. /**
  40. * Returns a standard set of read options.
  41. *
  42. * For now this is paranoid, but perhaps disable that in production builds.
  43. */
  44. static ReadOptions GetStandardReadOptions() {
  45. ReadOptions options;
  46. options.verify_checksums = true;
  47. return options;
  48. }
  49. @interface FSTLevelDBQueryCache ()
  50. /** A write-through cached copy of the metadata for the query cache. */
  51. @property(nonatomic, strong, nullable) FSTPBTargetGlobal *metadata;
  52. @property(nonatomic, strong, readonly) FSTLocalSerializer *serializer;
  53. @end
  54. @implementation FSTLevelDBQueryCache {
  55. // The DB pointer is shared with all cooperating LevelDB-related objects.
  56. std::shared_ptr<DB> _db;
  57. /**
  58. * The last received snapshot version. This is part of `metadata` but we store it separately to
  59. * avoid extra conversion to/from GPBTimestamp.
  60. */
  61. FSTSnapshotVersion *_lastRemoteSnapshotVersion;
  62. }
  63. - (instancetype)initWithDB:(std::shared_ptr<DB>)db serializer:(FSTLocalSerializer *)serializer {
  64. if (self = [super init]) {
  65. FSTAssert(db, @"db must not be NULL");
  66. _db = db;
  67. _serializer = serializer;
  68. }
  69. return self;
  70. }
  71. - (void)start {
  72. std::string key = [FSTLevelDBTargetGlobalKey key];
  73. FSTPBTargetGlobal *metadata = [self metadataForKey:key];
  74. if (!metadata) {
  75. metadata = [FSTPBTargetGlobal message];
  76. }
  77. _lastRemoteSnapshotVersion = [self.serializer decodedVersion:metadata.lastRemoteSnapshotVersion];
  78. self.metadata = metadata;
  79. }
  80. #pragma mark - FSTQueryCache implementation
  81. - (FSTTargetID)highestTargetID {
  82. return self.metadata.highestTargetId;
  83. }
  84. - (FSTSnapshotVersion *)lastRemoteSnapshotVersion {
  85. return _lastRemoteSnapshotVersion;
  86. }
  87. - (void)setLastRemoteSnapshotVersion:(FSTSnapshotVersion *)snapshotVersion
  88. group:(FSTWriteGroup *)group {
  89. _lastRemoteSnapshotVersion = snapshotVersion;
  90. self.metadata.lastRemoteSnapshotVersion = [self.serializer encodedVersion:snapshotVersion];
  91. [group setMessage:self.metadata forKey:[FSTLevelDBTargetGlobalKey key]];
  92. }
  93. - (void)shutdown {
  94. _db.reset();
  95. }
  96. - (void)addQueryData:(FSTQueryData *)queryData group:(FSTWriteGroup *)group {
  97. // TODO(mcg): actually populate listen sequence number
  98. FSTTargetID targetID = queryData.targetID;
  99. std::string key = [FSTLevelDBTargetKey keyWithTargetID:targetID];
  100. [group setMessage:[self.serializer encodedQueryData:queryData] forKey:key];
  101. NSString *canonicalID = queryData.query.canonicalID;
  102. std::string indexKey =
  103. [FSTLevelDBQueryTargetKey keyWithCanonicalID:canonicalID targetID:targetID];
  104. std::string emptyBuffer;
  105. [group setData:emptyBuffer forKey:indexKey];
  106. FSTPBTargetGlobal *metadata = self.metadata;
  107. if (targetID > metadata.highestTargetId) {
  108. metadata.highestTargetId = targetID;
  109. [group setMessage:metadata forKey:[FSTLevelDBTargetGlobalKey key]];
  110. }
  111. }
  112. - (void)removeQueryData:(FSTQueryData *)queryData group:(FSTWriteGroup *)group {
  113. FSTTargetID targetID = queryData.targetID;
  114. [self removeMatchingKeysForTargetID:targetID group:group];
  115. std::string key = [FSTLevelDBTargetKey keyWithTargetID:targetID];
  116. [group removeMessageForKey:key];
  117. std::string indexKey =
  118. [FSTLevelDBQueryTargetKey keyWithCanonicalID:queryData.query.canonicalID targetID:targetID];
  119. [group removeMessageForKey:indexKey];
  120. }
  121. /**
  122. * Looks up the query global metadata associated with the given key.
  123. *
  124. * @return the parsed protocol buffer message or nil if the row referenced by the given key does
  125. * not exist.
  126. */
  127. - (nullable FSTPBTargetGlobal *)metadataForKey:(const std::string &)key {
  128. std::string value;
  129. Status status = _db->Get(GetStandardReadOptions(), key, &value);
  130. if (status.IsNotFound()) {
  131. return nil;
  132. } else if (!status.ok()) {
  133. FSTFail(@"metadataForKey: failed loading key %s with status: %s", key.c_str(),
  134. status.ToString().c_str());
  135. }
  136. NSData *data =
  137. [[NSData alloc] initWithBytesNoCopy:(void *)value.data() length:value.size() freeWhenDone:NO];
  138. NSError *error;
  139. FSTPBTargetGlobal *proto = [FSTPBTargetGlobal parseFromData:data error:&error];
  140. if (!proto) {
  141. FSTFail(@"FSTPBTargetGlobal failed to parse: %@", error);
  142. }
  143. return proto;
  144. }
  145. /**
  146. * Parses the given bytes as an FSTPBTarget protocol buffer and then converts to the equivalent
  147. * query data.
  148. */
  149. - (FSTQueryData *)decodedTargetWithSlice:(Slice)slice {
  150. NSData *data =
  151. [[NSData alloc] initWithBytesNoCopy:(void *)slice.data() length:slice.size() freeWhenDone:NO];
  152. NSError *error;
  153. FSTPBTarget *proto = [FSTPBTarget parseFromData:data error:&error];
  154. if (!proto) {
  155. FSTFail(@"FSTPBTarget failed to parse: %@", error);
  156. }
  157. return [self.serializer decodedQueryData:proto];
  158. }
  159. - (nullable FSTQueryData *)queryDataForQuery:(FSTQuery *)query {
  160. // Scan the query-target index starting with a prefix starting with the given query's canonicalID.
  161. // Note that this is a scan rather than a get because canonicalIDs are not required to be unique
  162. // per target.
  163. Slice canonicalID = StringView(query.canonicalID);
  164. std::unique_ptr<Iterator> indexItererator(_db->NewIterator(GetStandardReadOptions()));
  165. std::string indexPrefix = [FSTLevelDBQueryTargetKey keyPrefixWithCanonicalID:canonicalID];
  166. indexItererator->Seek(indexPrefix);
  167. // Simultaneously scan the targets table. This works because each (canonicalID, targetID) pair is
  168. // unique and ordered, so when scanning a table prefixed by exactly one canonicalID, all the
  169. // targetIDs will be unique and in order.
  170. std::string targetPrefix = [FSTLevelDBTargetKey keyPrefix];
  171. std::unique_ptr<Iterator> targetIterator(_db->NewIterator(GetStandardReadOptions()));
  172. FSTLevelDBQueryTargetKey *rowKey = [[FSTLevelDBQueryTargetKey alloc] init];
  173. for (; indexItererator->Valid(); indexItererator->Next()) {
  174. Slice indexKey = indexItererator->key();
  175. // Only consider rows matching exactly the specific canonicalID of interest.
  176. if (!indexKey.starts_with(indexPrefix) || ![rowKey decodeKey:indexKey] ||
  177. canonicalID != rowKey.canonicalID) {
  178. // End of this canonicalID's possible targets.
  179. break;
  180. }
  181. // Each row is a unique combination of canonicalID and targetID, so this foreign key reference
  182. // can only occur once.
  183. std::string targetKey = [FSTLevelDBTargetKey keyWithTargetID:rowKey.targetID];
  184. targetIterator->Seek(targetKey);
  185. if (!targetIterator->Valid() || targetIterator->key() != targetKey) {
  186. NSString *foundKeyDescription = @"the end of the table";
  187. if (targetIterator->Valid()) {
  188. foundKeyDescription = [FSTLevelDBKey descriptionForKey:targetIterator->key()];
  189. }
  190. FSTFail(
  191. @"Dangling query-target reference found: "
  192. @"%@ points to %@; seeking there found %@",
  193. [FSTLevelDBKey descriptionForKey:indexKey], [FSTLevelDBKey descriptionForKey:targetKey],
  194. foundKeyDescription);
  195. }
  196. // Finally after finding a potential match, check that the query is actually equal to the
  197. // requested query.
  198. FSTQueryData *target = [self decodedTargetWithSlice:targetIterator->value()];
  199. if ([target.query isEqual:query]) {
  200. return target;
  201. }
  202. }
  203. return nil;
  204. }
  205. #pragma mark Matching Key tracking
  206. - (void)addMatchingKeys:(FSTDocumentKeySet *)keys
  207. forTargetID:(FSTTargetID)targetID
  208. group:(FSTWriteGroup *)group {
  209. // Store an empty value in the index which is equivalent to serializing a GPBEmpty message. In the
  210. // future if we wanted to store some other kind of value here, we can parse these empty values as
  211. // with some other protocol buffer (and the parser will see all default values).
  212. std::string emptyBuffer;
  213. [keys enumerateObjectsUsingBlock:^(FSTDocumentKey *documentKey, BOOL *stop) {
  214. [group setData:emptyBuffer
  215. forKey:[FSTLevelDBTargetDocumentKey keyWithTargetID:targetID documentKey:documentKey]];
  216. [group setData:emptyBuffer
  217. forKey:[FSTLevelDBDocumentTargetKey keyWithDocumentKey:documentKey targetID:targetID]];
  218. }];
  219. }
  220. - (void)removeMatchingKeys:(FSTDocumentKeySet *)keys
  221. forTargetID:(FSTTargetID)targetID
  222. group:(FSTWriteGroup *)group {
  223. [keys enumerateObjectsUsingBlock:^(FSTDocumentKey *key, BOOL *stop) {
  224. [group
  225. removeMessageForKey:[FSTLevelDBTargetDocumentKey keyWithTargetID:targetID documentKey:key]];
  226. [group
  227. removeMessageForKey:[FSTLevelDBDocumentTargetKey keyWithDocumentKey:key targetID:targetID]];
  228. [self.garbageCollector addPotentialGarbageKey:key];
  229. }];
  230. }
  231. - (void)removeMatchingKeysForTargetID:(FSTTargetID)targetID group:(FSTWriteGroup *)group {
  232. std::string indexPrefix = [FSTLevelDBTargetDocumentKey keyPrefixWithTargetID:targetID];
  233. std::unique_ptr<Iterator> indexIterator(_db->NewIterator(GetStandardReadOptions()));
  234. indexIterator->Seek(indexPrefix);
  235. FSTLevelDBTargetDocumentKey *rowKey = [[FSTLevelDBTargetDocumentKey alloc] init];
  236. for (; indexIterator->Valid(); indexIterator->Next()) {
  237. Slice indexKey = indexIterator->key();
  238. // Only consider rows matching this specific targetID.
  239. if (![rowKey decodeKey:indexKey] || rowKey.targetID != targetID) {
  240. break;
  241. }
  242. FSTDocumentKey *documentKey = rowKey.documentKey;
  243. // Delete both index rows
  244. [group removeMessageForKey:indexKey];
  245. [group removeMessageForKey:[FSTLevelDBDocumentTargetKey keyWithDocumentKey:documentKey
  246. targetID:targetID]];
  247. [self.garbageCollector addPotentialGarbageKey:documentKey];
  248. }
  249. }
  250. - (FSTDocumentKeySet *)matchingKeysForTargetID:(FSTTargetID)targetID {
  251. std::string indexPrefix = [FSTLevelDBTargetDocumentKey keyPrefixWithTargetID:targetID];
  252. std::unique_ptr<Iterator> indexIterator(_db->NewIterator(GetStandardReadOptions()));
  253. indexIterator->Seek(indexPrefix);
  254. FSTDocumentKeySet *result = [FSTDocumentKeySet keySet];
  255. FSTLevelDBTargetDocumentKey *rowKey = [[FSTLevelDBTargetDocumentKey alloc] init];
  256. for (; indexIterator->Valid(); indexIterator->Next()) {
  257. Slice indexKey = indexIterator->key();
  258. // Only consider rows matching this specific targetID.
  259. if (![rowKey decodeKey:indexKey] || rowKey.targetID != targetID) {
  260. break;
  261. }
  262. result = [result setByAddingObject:rowKey.documentKey];
  263. }
  264. return result;
  265. }
  266. #pragma mark - FSTGarbageSource implementation
  267. - (BOOL)containsKey:(FSTDocumentKey *)key {
  268. std::string indexPrefix = [FSTLevelDBDocumentTargetKey keyPrefixWithResourcePath:key.path];
  269. std::unique_ptr<Iterator> indexIterator(_db->NewIterator(GetStandardReadOptions()));
  270. indexIterator->Seek(indexPrefix);
  271. if (indexIterator->Valid()) {
  272. FSTLevelDBDocumentTargetKey *rowKey = [[FSTLevelDBDocumentTargetKey alloc] init];
  273. if ([rowKey decodeKey:indexIterator->key()] && [rowKey.documentKey isEqualToKey:key]) {
  274. return YES;
  275. }
  276. }
  277. return NO;
  278. }
  279. @end
  280. NS_ASSUME_NONNULL_END