FSTLevelDBRemoteDocumentCache.mm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/FSTLevelDBRemoteDocumentCache.h"
  17. #include <string>
  18. #import "Firestore/Protos/objc/firestore/local/MaybeDocument.pbobjc.h"
  19. #import "Firestore/Source/Core/FSTQuery.h"
  20. #import "Firestore/Source/Local/FSTLevelDB.h"
  21. #import "Firestore/Source/Local/FSTLevelDBKey.h"
  22. #import "Firestore/Source/Local/FSTLocalSerializer.h"
  23. #import "Firestore/Source/Model/FSTDocument.h"
  24. #import "Firestore/Source/Model/FSTDocumentDictionary.h"
  25. #import "Firestore/Source/Model/FSTDocumentSet.h"
  26. #include "Firestore/core/src/firebase/firestore/local/leveldb_transaction.h"
  27. #include "Firestore/core/src/firebase/firestore/model/document_key.h"
  28. #include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
  29. #include "leveldb/db.h"
  30. #include "leveldb/write_batch.h"
  31. NS_ASSUME_NONNULL_BEGIN
  32. using firebase::firestore::local::LevelDbTransaction;
  33. using firebase::firestore::model::DocumentKey;
  34. using leveldb::DB;
  35. using leveldb::Status;
  36. @interface FSTLevelDBRemoteDocumentCache ()
  37. @property(nonatomic, strong, readonly) FSTLocalSerializer *serializer;
  38. @end
  39. @implementation FSTLevelDBRemoteDocumentCache {
  40. FSTLevelDB *_db;
  41. }
  42. - (instancetype)initWithDB:(FSTLevelDB *)db serializer:(FSTLocalSerializer *)serializer {
  43. if (self = [super init]) {
  44. _db = db;
  45. _serializer = serializer;
  46. }
  47. return self;
  48. }
  49. - (void)addEntry:(FSTMaybeDocument *)document {
  50. std::string key = [self remoteDocumentKey:document.key];
  51. _db.currentTransaction->Put(key, [self.serializer encodedMaybeDocument:document]);
  52. }
  53. - (void)removeEntryForKey:(const DocumentKey &)documentKey {
  54. std::string key = [self remoteDocumentKey:documentKey];
  55. _db.currentTransaction->Delete(key);
  56. }
  57. - (nullable FSTMaybeDocument *)entryForKey:(const DocumentKey &)documentKey {
  58. std::string key = [FSTLevelDBRemoteDocumentKey keyWithDocumentKey:documentKey];
  59. std::string value;
  60. Status status = _db.currentTransaction->Get(key, &value);
  61. if (status.IsNotFound()) {
  62. return nil;
  63. } else if (status.ok()) {
  64. return [self decodeMaybeDocument:value withKey:documentKey];
  65. } else {
  66. HARD_FAIL("Fetch document for key (%s) failed with status: %s", documentKey.ToString(),
  67. status.ToString());
  68. }
  69. }
  70. - (FSTDocumentDictionary *)documentsMatchingQuery:(FSTQuery *)query {
  71. FSTDocumentDictionary *results = [FSTDocumentDictionary documentDictionary];
  72. // Documents are ordered by key, so we can use a prefix scan to narrow down
  73. // the documents we need to match the query against.
  74. std::string startKey = [FSTLevelDBRemoteDocumentKey keyPrefixWithResourcePath:query.path];
  75. auto it = _db.currentTransaction->NewIterator();
  76. it->Seek(startKey);
  77. FSTLevelDBRemoteDocumentKey *currentKey = [[FSTLevelDBRemoteDocumentKey alloc] init];
  78. for (; it->Valid() && [currentKey decodeKey:it->key()]; it->Next()) {
  79. FSTMaybeDocument *maybeDoc =
  80. [self decodeMaybeDocument:it->value() withKey:currentKey.documentKey];
  81. if (!query.path.IsPrefixOf(maybeDoc.key.path())) {
  82. break;
  83. } else if ([maybeDoc isKindOfClass:[FSTDocument class]]) {
  84. results = [results dictionaryBySettingObject:(FSTDocument *)maybeDoc forKey:maybeDoc.key];
  85. }
  86. }
  87. return results;
  88. }
  89. - (std::string)remoteDocumentKey:(const DocumentKey &)key {
  90. return [FSTLevelDBRemoteDocumentKey keyWithDocumentKey:key];
  91. }
  92. - (FSTMaybeDocument *)decodeMaybeDocument:(absl::string_view)encoded
  93. withKey:(const DocumentKey &)documentKey {
  94. NSData *data = [[NSData alloc] initWithBytesNoCopy:(void *)encoded.data()
  95. length:encoded.size()
  96. freeWhenDone:NO];
  97. NSError *error;
  98. FSTPBMaybeDocument *proto = [FSTPBMaybeDocument parseFromData:data error:&error];
  99. if (!proto) {
  100. HARD_FAIL("FSTPBMaybeDocument failed to parse: %s", error);
  101. }
  102. FSTMaybeDocument *maybeDocument = [self.serializer decodedMaybeDocument:proto];
  103. HARD_ASSERT([maybeDocument.key isEqualToKey:documentKey],
  104. "Read document has key (%s) instead of expected key (%s).",
  105. maybeDocument.key.ToString(), documentKey.ToString());
  106. return maybeDocument;
  107. }
  108. @end
  109. NS_ASSUME_NONNULL_END