FSTLevelDBKey.mm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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/FSTLevelDBKey.h"
  17. #include <string>
  18. #include <utility>
  19. #include <vector>
  20. #import "Firestore/Source/Model/FSTDocumentKey.h"
  21. #include "Firestore/core/src/firebase/firestore/model/resource_path.h"
  22. #include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
  23. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  24. namespace util = firebase::firestore::util;
  25. namespace util = firebase::firestore::util;
  26. using firebase::firestore::model::ResourcePath;
  27. NS_ASSUME_NONNULL_BEGIN
  28. using firebase::firestore::model::ResourcePath;
  29. using firebase::firestore::util::OrderedCode;
  30. using Firestore::StringView;
  31. using leveldb::Slice;
  32. static const char *kVersionGlobalTable = "version";
  33. static const char *kMutationsTable = "mutation";
  34. static const char *kDocumentMutationsTable = "document_mutation";
  35. static const char *kMutationQueuesTable = "mutation_queue";
  36. static const char *kTargetGlobalTable = "target_global";
  37. static const char *kTargetsTable = "target";
  38. static const char *kQueryTargetsTable = "query_target";
  39. static const char *kTargetDocumentsTable = "target_document";
  40. static const char *kDocumentTargetsTable = "document_target";
  41. static const char *kRemoteDocumentsTable = "remote_document";
  42. /**
  43. * Labels for the components of keys. These serve to make keys self-describing.
  44. *
  45. * These are intended to sort similarly to keys in the server storage format.
  46. *
  47. * Note that the server writes component labels using the equivalent to
  48. * OrderedCode::WriteSignedNumDecreasing. This means that despite the higher numeric value, a
  49. * terminator sorts before a path segment. In order to avoid needing the WriteSignedNumDecreasing
  50. * code just for these values, this enum's values are in the reverse order to the server side.
  51. *
  52. * Most server-side values don't apply here. For example, the server embeds projects, databases,
  53. * namespaces and similar values in its entity keys where the clients just open a different
  54. * leveldb. Similarly, many of these values don't apply to the server since the server is backed
  55. * by spanner which natively has concepts of tables and indexes. Where there's overlap, a comment
  56. * denotes the server value from the storage_format_internal.proto.
  57. */
  58. typedef NS_ENUM(int64_t, FSTComponentLabel) {
  59. /**
  60. * A terminator is the final component of a key. All complete keys have a terminator and a key
  61. * is known to be a key prefix if it doesn't have a terminator.
  62. */
  63. FSTComponentLabelTerminator = 0, // TERMINATOR_COMPONENT = 63, server-side
  64. /** A table name component names the logical table to which the key belongs. */
  65. FSTComponentLabelTableName = 5,
  66. /** A component containing the batch ID of a mutation. */
  67. FSTComponentLabelBatchID = 10,
  68. /** A component containing the canonical ID of a query. */
  69. FSTComponentLabelCanonicalID = 11,
  70. /** A component containing the target ID of a query. */
  71. FSTComponentLabelTargetID = 12,
  72. /** A component containing a user ID. */
  73. FSTComponentLabelUserID = 13,
  74. /**
  75. * A path segment describes just a single segment in a resource path. Path segments that occur
  76. * sequentially in a key represent successive segments in a single path.
  77. *
  78. * This value must be greater than FSTComponentLabelTerminator to ensure that longer paths sort
  79. * after paths that are prefixes of them.
  80. *
  81. * This value must also be larger than other separators so that path suffixes sort after other
  82. * key components.
  83. */
  84. FSTComponentLabelPathSegment = 62, // PATH = 60, server-side
  85. /** The maximum value that can be encoded by WriteSignedNumIncreasing in a single byte. */
  86. FSTComponentLabelUnknown = 63,
  87. };
  88. namespace {
  89. /** Writes a component label to the given key destination. */
  90. void WriteComponentLabel(std::string *dest, FSTComponentLabel label) {
  91. OrderedCode::WriteSignedNumIncreasing(dest, label);
  92. }
  93. /**
  94. * Reads a component label from the given key contents.
  95. *
  96. * If the read is unsuccessful, returns NO, and changes none of its arguments.
  97. *
  98. * If the read is successful, returns YES, contents will be updated to the next unread byte, and
  99. * label will be set to the decoded label value.
  100. */
  101. BOOL ReadComponentLabel(leveldb::Slice *contents, FSTComponentLabel *label) {
  102. int64_t rawResult = 0;
  103. absl::string_view tmp(contents->data(), contents->size());
  104. if (OrderedCode::ReadSignedNumIncreasing(&tmp, &rawResult)) {
  105. if (rawResult >= FSTComponentLabelTerminator && rawResult <= FSTComponentLabelUnknown) {
  106. *label = static_cast<FSTComponentLabel>(rawResult);
  107. *contents = leveldb::Slice(tmp.data(), tmp.size());
  108. return YES;
  109. }
  110. }
  111. return NO;
  112. }
  113. /**
  114. * Reads a component label from the given key contents.
  115. *
  116. * If the read is unsuccessful or if the read was successful but the label that was read did not
  117. * match the expectedLabel returns NO and changes none of its arguments.
  118. *
  119. * If the read is successful, returns YES and contents will be updated to the next unread byte.
  120. */
  121. BOOL ReadComponentLabelMatching(absl::string_view *contents, FSTComponentLabel expectedLabel) {
  122. int64_t rawResult = 0;
  123. absl::string_view tmp = *contents;
  124. if (OrderedCode::ReadSignedNumIncreasing(&tmp, &rawResult)) {
  125. if (rawResult == expectedLabel) {
  126. *contents = tmp;
  127. return YES;
  128. }
  129. }
  130. return NO;
  131. }
  132. /**
  133. * Reads a signed number from the given key contents and verifies that the value fits in a 32-bit
  134. * integer.
  135. *
  136. * If the read is unsuccessful or the number that was read was out of bounds for an int32_t,
  137. * returns NO, and changes none of its arguments.
  138. *
  139. * If the read is successful, returns YES, contents will be updated to the next unread byte, and
  140. * result will be set to the decoded integer value.
  141. */
  142. BOOL ReadInt32(Slice *contents, int32_t *result) {
  143. int64_t rawResult = 0;
  144. absl::string_view tmp(contents->data(), contents->size());
  145. if (OrderedCode::ReadSignedNumIncreasing(&tmp, &rawResult)) {
  146. if (rawResult >= INT32_MIN && rawResult <= INT32_MAX) {
  147. *contents = leveldb::Slice(tmp.data(), tmp.size());
  148. *result = static_cast<int32_t>(rawResult);
  149. return YES;
  150. }
  151. }
  152. return NO;
  153. }
  154. /** Writes a component label and a signed integer to the given key destination. */
  155. void WriteLabeledInt32(std::string *dest, FSTComponentLabel label, int32_t value) {
  156. WriteComponentLabel(dest, label);
  157. OrderedCode::WriteSignedNumIncreasing(dest, value);
  158. }
  159. /**
  160. * Reads a component label and signed number from the given key contents and verifies that the
  161. * label matches the expectedLabel and the value fits in a 32-bit integer.
  162. *
  163. * If the read is unsuccessful, the label didn't match, or the number that was read was out of
  164. * bounds for an int32_t, returns NO, and changes none of its arguments.
  165. *
  166. * If the read is successful, returns YES, contents will be updated to the next unread byte, and
  167. * value will be set to the decoded integer value.
  168. */
  169. BOOL ReadLabeledInt32(Slice *contents, FSTComponentLabel expectedLabel, int32_t *value) {
  170. absl::string_view tmp(contents->data(), contents->size());
  171. if (ReadComponentLabelMatching(&tmp, expectedLabel)) {
  172. Slice tmpSlice = leveldb::Slice(tmp.data(), tmp.size());
  173. if (ReadInt32(&tmpSlice, value)) {
  174. *contents = tmpSlice;
  175. return YES;
  176. }
  177. }
  178. return NO;
  179. }
  180. /** Writes a component label and an encoded string to the given key destination. */
  181. void WriteLabeledString(std::string *dest, FSTComponentLabel label, StringView value) {
  182. WriteComponentLabel(dest, label);
  183. OrderedCode::WriteString(dest, value);
  184. }
  185. /**
  186. * Reads a component label and a string from the given key contents and verifies that the label
  187. * matches the expectedLabel.
  188. *
  189. * If the read is unsuccessful or the label didn't match, returns NO, and changes none of its
  190. * arguments.
  191. *
  192. * If the read is successful, returns YES, contents will be updated to the next unread byte, and
  193. * value will be set to the decoded string value.
  194. */
  195. BOOL ReadLabeledString(Slice *contents, FSTComponentLabel expectedLabel, std::string *value) {
  196. absl::string_view tmp(contents->data(), contents->size());
  197. if (ReadComponentLabelMatching(&tmp, expectedLabel)) {
  198. if (OrderedCode::ReadString(&tmp, value)) {
  199. *contents = leveldb::Slice(tmp.data(), tmp.size());
  200. return YES;
  201. }
  202. }
  203. return NO;
  204. }
  205. /**
  206. * Reads a component label and a string from the given key contents and verifies that the label
  207. * matches the expectedLabel and the string matches the expectedValue.
  208. *
  209. * If the read is unsuccessful, the label or didn't match, or the string value didn't match,
  210. * returns NO, and changes none of its arguments.
  211. *
  212. * If the read is successful, returns YES, contents will be updated to the next unread byte.
  213. */
  214. BOOL ReadLabeledStringMatching(Slice *contents,
  215. FSTComponentLabel expectedLabel,
  216. const char *expectedValue) {
  217. std::string value;
  218. Slice tmp = *contents;
  219. if (ReadLabeledString(&tmp, expectedLabel, &value)) {
  220. if (value == expectedValue) {
  221. *contents = tmp;
  222. return YES;
  223. }
  224. }
  225. return NO;
  226. }
  227. /**
  228. * For each segment in the given resource path writes an FSTComponentLabelPathSegment component
  229. * label and a string containing the path segment.
  230. */
  231. void WriteResourcePath(std::string *dest, const ResourcePath &path) {
  232. for (const auto &segment : path) {
  233. WriteComponentLabel(dest, FSTComponentLabelPathSegment);
  234. OrderedCode::WriteString(dest, segment);
  235. }
  236. }
  237. /**
  238. * Reads component labels and strings from the given key contents until it finds a component label
  239. * other that FSTComponentLabelPathSegment. All matched path segments are assembled into a resource
  240. * path and wrapped in an FSTDocumentKey.
  241. *
  242. * If the read is unsuccessful or the document key is invalid, returns NO, and changes none of its
  243. * arguments.
  244. *
  245. * If the read is successful, returns YES, contents will be updated to the next unread byte, and
  246. * value will be set to the decoded document key.
  247. */
  248. BOOL ReadDocumentKey(Slice *contents, FSTDocumentKey *__strong *result) {
  249. Slice completeSegments = *contents;
  250. std::string segment;
  251. std::vector<std::string> path_segments;
  252. for (;;) {
  253. // Advance a temporary slice to avoid advancing contents into the next key component which may
  254. // not be a path segment.
  255. absl::string_view readPosition(completeSegments.data(), completeSegments.size());
  256. if (!ReadComponentLabelMatching(&readPosition, FSTComponentLabelPathSegment)) {
  257. break;
  258. }
  259. if (!OrderedCode::ReadString(&readPosition, &segment)) {
  260. return NO;
  261. }
  262. path_segments.push_back(std::move(segment));
  263. segment.clear();
  264. completeSegments = leveldb::Slice(readPosition.data(), readPosition.size());
  265. }
  266. ResourcePath path{std::move(path_segments)};
  267. if (path.size() > 0 && [FSTDocumentKey isDocumentKey:path]) {
  268. *contents = completeSegments;
  269. *result = [FSTDocumentKey keyWithPath:path];
  270. return YES;
  271. }
  272. return NO;
  273. }
  274. // Trivial shortcuts that make reading and writing components type-safe.
  275. inline void WriteTerminator(std::string *dest) {
  276. OrderedCode::WriteSignedNumIncreasing(dest, FSTComponentLabelTerminator);
  277. }
  278. inline BOOL ReadTerminator(Slice *contents) {
  279. absl::string_view tmp(contents->data(), contents->size());
  280. BOOL result = ReadComponentLabelMatching(&tmp, FSTComponentLabelTerminator);
  281. *contents = leveldb::Slice(tmp.data(), tmp.size());
  282. return result;
  283. }
  284. inline void WriteTableName(std::string *dest, const char *tableName) {
  285. WriteLabeledString(dest, FSTComponentLabelTableName, tableName);
  286. }
  287. inline BOOL ReadTableNameMatching(Slice *contents, const char *expectedTableName) {
  288. return ReadLabeledStringMatching(contents, FSTComponentLabelTableName, expectedTableName);
  289. }
  290. inline void WriteBatchID(std::string *dest, FSTBatchID batchID) {
  291. WriteLabeledInt32(dest, FSTComponentLabelBatchID, batchID);
  292. }
  293. inline BOOL ReadBatchID(Slice *contents, FSTBatchID *batchID) {
  294. return ReadLabeledInt32(contents, FSTComponentLabelBatchID, batchID);
  295. }
  296. inline void WriteCanonicalID(std::string *dest, StringView canonicalID) {
  297. WriteLabeledString(dest, FSTComponentLabelCanonicalID, canonicalID);
  298. }
  299. inline BOOL ReadCanonicalID(Slice *contents, std::string *canonicalID) {
  300. return ReadLabeledString(contents, FSTComponentLabelCanonicalID, canonicalID);
  301. }
  302. inline void WriteTargetID(std::string *dest, FSTTargetID targetID) {
  303. WriteLabeledInt32(dest, FSTComponentLabelTargetID, targetID);
  304. }
  305. inline BOOL ReadTargetID(Slice *contents, FSTTargetID *targetID) {
  306. return ReadLabeledInt32(contents, FSTComponentLabelTargetID, targetID);
  307. }
  308. inline void WriteUserID(std::string *dest, StringView userID) {
  309. WriteLabeledString(dest, FSTComponentLabelUserID, userID);
  310. }
  311. inline BOOL ReadUserID(Slice *contents, std::string *userID) {
  312. return ReadLabeledString(contents, FSTComponentLabelUserID, userID);
  313. }
  314. /** Returns a base64-encoded string for an invalid key, used for debug-friendly description text. */
  315. NSString *InvalidKey(const Slice &key) {
  316. NSData *keyData =
  317. [[NSData alloc] initWithBytesNoCopy:(void *)key.data() length:key.size() freeWhenDone:NO];
  318. return [keyData base64EncodedStringWithOptions:0];
  319. }
  320. } // namespace
  321. @implementation FSTLevelDBKey
  322. + (NSString *)descriptionForKey:(StringView)key {
  323. Slice contents = key;
  324. BOOL isTerminated = NO;
  325. NSMutableString *description = [NSMutableString string];
  326. [description appendString:@"["];
  327. while (contents.size() > 0) {
  328. Slice tmp = contents;
  329. FSTComponentLabel label = FSTComponentLabelUnknown;
  330. if (!ReadComponentLabel(&tmp, &label)) {
  331. break;
  332. }
  333. if (label == FSTComponentLabelTerminator) {
  334. isTerminated = YES;
  335. contents = tmp;
  336. break;
  337. }
  338. // Reset tmp since all the different read routines expect to see the separator first
  339. tmp = contents;
  340. if (label == FSTComponentLabelPathSegment) {
  341. FSTDocumentKey *documentKey = nil;
  342. if (!ReadDocumentKey(&tmp, &documentKey)) {
  343. break;
  344. }
  345. [description appendFormat:@" key=%s", documentKey.path.CanonicalString().c_str()];
  346. } else if (label == FSTComponentLabelTableName) {
  347. std::string table;
  348. if (!ReadLabeledString(&tmp, FSTComponentLabelTableName, &table)) {
  349. break;
  350. }
  351. [description appendFormat:@"%s:", table.c_str()];
  352. } else if (label == FSTComponentLabelBatchID) {
  353. FSTBatchID batchID;
  354. if (!ReadBatchID(&tmp, &batchID)) {
  355. break;
  356. }
  357. [description appendFormat:@" batchID=%d", batchID];
  358. } else if (label == FSTComponentLabelCanonicalID) {
  359. std::string canonicalID;
  360. if (!ReadCanonicalID(&tmp, &canonicalID)) {
  361. break;
  362. }
  363. [description appendFormat:@" canonicalID=%s", canonicalID.c_str()];
  364. } else if (label == FSTComponentLabelTargetID) {
  365. FSTTargetID targetID;
  366. if (!ReadTargetID(&tmp, &targetID)) {
  367. break;
  368. }
  369. [description appendFormat:@" targetID=%d", targetID];
  370. } else if (label == FSTComponentLabelUserID) {
  371. std::string userID;
  372. if (!ReadUserID(&tmp, &userID)) {
  373. break;
  374. }
  375. [description appendFormat:@" userID=%s", userID.c_str()];
  376. } else {
  377. [description appendFormat:@" unknown label=%d", (int)label];
  378. break;
  379. }
  380. contents = tmp;
  381. }
  382. if (contents.size() > 0) {
  383. [description appendFormat:@" invalid key=<%@>", InvalidKey(key)];
  384. } else if (!isTerminated) {
  385. [description appendFormat:@" incomplete key"];
  386. }
  387. [description appendString:@"]"];
  388. return description;
  389. }
  390. @end
  391. @implementation FSTLevelDBVersionKey
  392. + (std::string)key {
  393. std::string result;
  394. WriteTableName(&result, kVersionGlobalTable);
  395. WriteTerminator(&result);
  396. return result;
  397. }
  398. @end
  399. @implementation FSTLevelDBMutationKey {
  400. std::string _userID;
  401. }
  402. + (std::string)keyPrefix {
  403. std::string result;
  404. WriteTableName(&result, kMutationsTable);
  405. return result;
  406. }
  407. + (std::string)keyPrefixWithUserID:(StringView)userID {
  408. std::string result;
  409. WriteTableName(&result, kMutationsTable);
  410. WriteUserID(&result, userID);
  411. return result;
  412. }
  413. + (std::string)keyWithUserID:(StringView)userID batchID:(FSTBatchID)batchID {
  414. std::string result;
  415. WriteTableName(&result, kMutationsTable);
  416. WriteUserID(&result, userID);
  417. WriteBatchID(&result, batchID);
  418. WriteTerminator(&result);
  419. return result;
  420. }
  421. - (const std::string &)userID {
  422. return _userID;
  423. }
  424. - (BOOL)decodeKey:(StringView)key {
  425. _userID.clear();
  426. Slice contents = key;
  427. return ReadTableNameMatching(&contents, kMutationsTable) && ReadUserID(&contents, &_userID) &&
  428. ReadBatchID(&contents, &_batchID) && ReadTerminator(&contents);
  429. }
  430. @end
  431. @implementation FSTLevelDBDocumentMutationKey {
  432. std::string _userID;
  433. }
  434. + (std::string)keyPrefix {
  435. std::string result;
  436. WriteTableName(&result, kDocumentMutationsTable);
  437. return result;
  438. }
  439. + (std::string)keyPrefixWithUserID:(StringView)userID {
  440. std::string result;
  441. WriteTableName(&result, kDocumentMutationsTable);
  442. WriteUserID(&result, userID);
  443. return result;
  444. }
  445. + (std::string)keyPrefixWithUserID:(StringView)userID
  446. resourcePath:(const ResourcePath &)resourcePath {
  447. std::string result;
  448. WriteTableName(&result, kDocumentMutationsTable);
  449. WriteUserID(&result, userID);
  450. WriteResourcePath(&result, resourcePath);
  451. return result;
  452. }
  453. + (std::string)keyWithUserID:(StringView)userID
  454. documentKey:(FSTDocumentKey *)documentKey
  455. batchID:(FSTBatchID)batchID {
  456. std::string result;
  457. WriteTableName(&result, kDocumentMutationsTable);
  458. WriteUserID(&result, userID);
  459. WriteResourcePath(&result, documentKey.path);
  460. WriteBatchID(&result, batchID);
  461. WriteTerminator(&result);
  462. return result;
  463. }
  464. - (const std::string &)userID {
  465. return _userID;
  466. }
  467. - (BOOL)decodeKey:(StringView)key {
  468. _userID.clear();
  469. _documentKey = nil;
  470. Slice contents = key;
  471. return ReadTableNameMatching(&contents, kDocumentMutationsTable) &&
  472. ReadUserID(&contents, &_userID) && ReadDocumentKey(&contents, &_documentKey) &&
  473. ReadBatchID(&contents, &_batchID) && ReadTerminator(&contents);
  474. }
  475. @end
  476. @implementation FSTLevelDBMutationQueueKey {
  477. std::string _userID;
  478. }
  479. + (std::string)keyPrefix {
  480. std::string result;
  481. WriteTableName(&result, kMutationQueuesTable);
  482. return result;
  483. }
  484. + (std::string)keyWithUserID:(StringView)userID {
  485. std::string result;
  486. WriteTableName(&result, kMutationQueuesTable);
  487. WriteUserID(&result, userID);
  488. WriteTerminator(&result);
  489. return result;
  490. }
  491. - (const std::string &)userID {
  492. return _userID;
  493. }
  494. - (BOOL)decodeKey:(StringView)key {
  495. _userID.clear();
  496. Slice contents = key;
  497. return ReadTableNameMatching(&contents, kMutationQueuesTable) &&
  498. ReadUserID(&contents, &_userID) && ReadTerminator(&contents);
  499. }
  500. @end
  501. @implementation FSTLevelDBTargetGlobalKey
  502. + (std::string)key {
  503. std::string result;
  504. WriteTableName(&result, kTargetGlobalTable);
  505. WriteTerminator(&result);
  506. return result;
  507. }
  508. - (BOOL)decodeKey:(StringView)key {
  509. Slice contents = key;
  510. return ReadTableNameMatching(&contents, kTargetGlobalTable) && ReadTerminator(&contents);
  511. }
  512. @end
  513. @implementation FSTLevelDBTargetKey
  514. + (std::string)keyPrefix {
  515. std::string result;
  516. WriteTableName(&result, kTargetsTable);
  517. return result;
  518. }
  519. + (std::string)keyWithTargetID:(FSTTargetID)targetID {
  520. std::string result;
  521. WriteTableName(&result, kTargetsTable);
  522. WriteTargetID(&result, targetID);
  523. WriteTerminator(&result);
  524. return result;
  525. }
  526. - (BOOL)decodeKey:(StringView)key {
  527. Slice contents = key;
  528. return ReadTableNameMatching(&contents, kTargetsTable) && ReadTargetID(&contents, &_targetID) &&
  529. ReadTerminator(&contents);
  530. }
  531. @end
  532. @implementation FSTLevelDBQueryTargetKey {
  533. std::string _canonicalID;
  534. }
  535. + (std::string)keyPrefix {
  536. std::string result;
  537. WriteTableName(&result, kQueryTargetsTable);
  538. return result;
  539. }
  540. + (std::string)keyPrefixWithCanonicalID:(StringView)canonicalID {
  541. std::string result;
  542. WriteTableName(&result, kQueryTargetsTable);
  543. WriteCanonicalID(&result, canonicalID);
  544. return result;
  545. }
  546. + (std::string)keyWithCanonicalID:(StringView)canonicalID targetID:(FSTTargetID)targetID {
  547. std::string result;
  548. WriteTableName(&result, kQueryTargetsTable);
  549. WriteCanonicalID(&result, canonicalID);
  550. WriteTargetID(&result, targetID);
  551. WriteTerminator(&result);
  552. return result;
  553. }
  554. - (const std::string &)canonicalID {
  555. return _canonicalID;
  556. }
  557. - (BOOL)decodeKey:(StringView)key {
  558. _canonicalID.clear();
  559. Slice contents = key;
  560. return ReadTableNameMatching(&contents, kQueryTargetsTable) &&
  561. ReadCanonicalID(&contents, &_canonicalID) && ReadTargetID(&contents, &_targetID) &&
  562. ReadTerminator(&contents);
  563. }
  564. @end
  565. @implementation FSTLevelDBTargetDocumentKey
  566. + (std::string)keyPrefix {
  567. std::string result;
  568. WriteTableName(&result, kTargetDocumentsTable);
  569. return result;
  570. }
  571. + (std::string)keyPrefixWithTargetID:(FSTTargetID)targetID {
  572. std::string result;
  573. WriteTableName(&result, kTargetDocumentsTable);
  574. WriteTargetID(&result, targetID);
  575. return result;
  576. }
  577. + (std::string)keyWithTargetID:(FSTTargetID)targetID documentKey:(FSTDocumentKey *)documentKey {
  578. std::string result;
  579. WriteTableName(&result, kTargetDocumentsTable);
  580. WriteTargetID(&result, targetID);
  581. WriteResourcePath(&result, documentKey.path);
  582. WriteTerminator(&result);
  583. return result;
  584. }
  585. - (BOOL)decodeKey:(Firestore::StringView)key {
  586. _documentKey = nil;
  587. leveldb::Slice contents = key;
  588. return ReadTableNameMatching(&contents, kTargetDocumentsTable) &&
  589. ReadTargetID(&contents, &_targetID) && ReadDocumentKey(&contents, &_documentKey) &&
  590. ReadTerminator(&contents);
  591. }
  592. @end
  593. @implementation FSTLevelDBDocumentTargetKey
  594. + (std::string)keyPrefix {
  595. std::string result;
  596. WriteTableName(&result, kDocumentTargetsTable);
  597. return result;
  598. }
  599. + (std::string)keyPrefixWithResourcePath:(const ResourcePath &)resourcePath {
  600. std::string result;
  601. WriteTableName(&result, kDocumentTargetsTable);
  602. WriteResourcePath(&result, resourcePath);
  603. return result;
  604. }
  605. + (std::string)keyWithDocumentKey:(FSTDocumentKey *)documentKey targetID:(FSTTargetID)targetID {
  606. std::string result;
  607. WriteTableName(&result, kDocumentTargetsTable);
  608. WriteResourcePath(&result, documentKey.path);
  609. WriteTargetID(&result, targetID);
  610. WriteTerminator(&result);
  611. return result;
  612. }
  613. - (BOOL)decodeKey:(Firestore::StringView)key {
  614. _documentKey = nil;
  615. leveldb::Slice contents = key;
  616. return ReadTableNameMatching(&contents, kDocumentTargetsTable) &&
  617. ReadDocumentKey(&contents, &_documentKey) && ReadTargetID(&contents, &_targetID) &&
  618. ReadTerminator(&contents);
  619. }
  620. @end
  621. @implementation FSTLevelDBRemoteDocumentKey
  622. + (std::string)keyPrefix {
  623. std::string result;
  624. WriteTableName(&result, kRemoteDocumentsTable);
  625. return result;
  626. }
  627. + (std::string)keyPrefixWithResourcePath:(const ResourcePath &)path {
  628. std::string result;
  629. WriteTableName(&result, kRemoteDocumentsTable);
  630. WriteResourcePath(&result, path);
  631. return result;
  632. }
  633. + (std::string)keyWithDocumentKey:(FSTDocumentKey *)key {
  634. std::string result;
  635. WriteTableName(&result, kRemoteDocumentsTable);
  636. WriteResourcePath(&result, key.path);
  637. WriteTerminator(&result);
  638. return result;
  639. }
  640. - (BOOL)decodeKey:(StringView)key {
  641. _documentKey = nil;
  642. Slice contents = key;
  643. return ReadTableNameMatching(&contents, kRemoteDocumentsTable) &&
  644. ReadDocumentKey(&contents, &_documentKey) && ReadTerminator(&contents);
  645. }
  646. @end
  647. NS_ASSUME_NONNULL_END