FSTLevelDBRemoteDocumentCache.mm 4.6 KB

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