FSTWriteGroup.mm 3.8 KB

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