FSTWriteGroup.mm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "FSTWriteGroup.h"
  17. #import <Protobuf/GPBProtocolBuffers.h>
  18. #include <leveldb/db.h>
  19. #include <leveldb/write_batch.h>
  20. #import "FSTLevelDBKey.h"
  21. #import "FSTAssert.h"
  22. #include "ordered_code.h"
  23. using Firestore::OrderedCode;
  24. using Firestore::StringView;
  25. using leveldb::DB;
  26. using leveldb::Slice;
  27. using leveldb::Status;
  28. using leveldb::WriteBatch;
  29. using leveldb::WriteOptions;
  30. NS_ASSUME_NONNULL_BEGIN
  31. namespace Firestore {
  32. /**
  33. * A WriteBatch::Handler implementation that extracts batch details from a leveldb::WriteBatch.
  34. * This is used for describing a write batch primarily in log messages after a failure.
  35. */
  36. class BatchDescription : public WriteBatch::Handler {
  37. public:
  38. BatchDescription() : ops_(0), size_(0), message_([NSMutableString string]) {}
  39. virtual ~BatchDescription();
  40. virtual void Put(const Slice &key, const Slice &value);
  41. virtual void Delete(const Slice &key);
  42. // Converts the batch to a printable string description of it
  43. NSString *ToString() const {
  44. return [NSString
  45. stringWithFormat:@"%d changes (%lu bytes):%@", ops_, (unsigned long)size_, message_];
  46. }
  47. // Disallow copies and moves
  48. BatchDescription(const BatchDescription &) = delete;
  49. BatchDescription &operator=(const BatchDescription &) = delete;
  50. BatchDescription(BatchDescription &&) = delete;
  51. BatchDescription &operator=(BatchDescription &&) = delete;
  52. private:
  53. int ops_;
  54. size_t size_;
  55. NSMutableString *message_;
  56. };
  57. BatchDescription::~BatchDescription() {}
  58. void BatchDescription::Put(const Slice &key, const Slice &value) {
  59. ops_ += 1;
  60. size_ += value.size();
  61. [message_ appendFormat:@"\n - Put %@ (%lu bytes)", [FSTLevelDBKey descriptionForKey:key],
  62. (unsigned long)value.size()];
  63. }
  64. void BatchDescription::Delete(const Slice &key) {
  65. ops_ += 1;
  66. [message_ appendFormat:@"\n - Delete %@", [FSTLevelDBKey descriptionForKey:key]];
  67. }
  68. } // namespace Firestore
  69. @interface FSTWriteGroup ()
  70. - (instancetype)initWithAction:(NSString *)action NS_DESIGNATED_INITIALIZER;
  71. @end
  72. @implementation FSTWriteGroup {
  73. int _changes;
  74. WriteBatch _contents;
  75. }
  76. + (instancetype)groupWithAction:(NSString *)action {
  77. return [[FSTWriteGroup alloc] initWithAction:action];
  78. }
  79. - (instancetype)initWithAction:(NSString *)action {
  80. if (self = [super init]) {
  81. _action = action;
  82. }
  83. return self;
  84. }
  85. - (NSString *)description {
  86. Firestore::BatchDescription description;
  87. Status status = _contents.Iterate(&description);
  88. if (!status.ok()) {
  89. FSTFail(@"Iterate over write batch should not fail");
  90. }
  91. return [NSString
  92. stringWithFormat:@"<FSTWriteGroup for %@: %@>", self.action, description.ToString()];
  93. }
  94. - (void)removeMessageForKey:(StringView)key {
  95. _contents.Delete(key);
  96. _changes += 1;
  97. }
  98. - (void)setMessage:(GPBMessage *)message forKey:(StringView)key {
  99. NSData *data = [message data];
  100. Slice value((const char *)data.bytes, data.length);
  101. _contents.Put(key, value);
  102. _changes += 1;
  103. }
  104. - (void)setData:(StringView)data forKey:(StringView)key {
  105. _contents.Put(key, data);
  106. _changes += 1;
  107. }
  108. - (leveldb::Status)writeToDB:(std::shared_ptr<leveldb::DB>)db {
  109. return db->Write(leveldb::WriteOptions(), &_contents);
  110. }
  111. - (BOOL)isEmpty {
  112. return _changes == 0;
  113. }
  114. @end
  115. NS_ASSUME_NONNULL_END