Skip to content

Commit d90596c

Browse files
authored
Clean up warnings in leveldb_transaction.cc (#1711)
* absl::string_view should be passed by value * use absl::StrCat and friends for concatenation * use C++11 range-based for loops * avoid sign conversion
1 parent 9fc23fb commit d90596c

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

Firestore/core/src/firebase/firestore/local/leveldb_transaction.cc

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "Firestore/core/src/firebase/firestore/util/hard_assert.h"
2121
#include "Firestore/core/src/firebase/firestore/util/log.h"
2222
#include "absl/memory/memory.h"
23+
#include "absl/strings/str_cat.h"
2324
#include "leveldb/write_batch.h"
2425

2526
using leveldb::DB;
@@ -161,8 +162,7 @@ const WriteOptions& LevelDbTransaction::DefaultWriteOptions() {
161162
return options;
162163
}
163164

164-
void LevelDbTransaction::Put(const absl::string_view& key,
165-
const absl::string_view& value) {
165+
void LevelDbTransaction::Put(absl::string_view key, absl::string_view value) {
166166
std::string key_string(key);
167167
std::string value_string(value);
168168
mutations_[key_string] = value_string;
@@ -175,13 +175,12 @@ LevelDbTransaction::NewIterator() {
175175
return absl::make_unique<LevelDbTransaction::Iterator>(this);
176176
}
177177

178-
Status LevelDbTransaction::Get(const absl::string_view& key,
179-
std::string* value) {
178+
Status LevelDbTransaction::Get(absl::string_view key, std::string* value) {
180179
std::string key_string(key);
181180
if (deletions_.find(key_string) != deletions_.end()) {
182181
return Status::NotFound(key_string + " is not present in the transaction");
183182
} else {
184-
Mutations::iterator iter(mutations_.find(key_string));
183+
Mutations::iterator iter{mutations_.find(key_string)};
185184
if (iter != mutations_.end()) {
186185
*value = iter->second;
187186
return Status::OK();
@@ -191,7 +190,7 @@ Status LevelDbTransaction::Get(const absl::string_view& key,
191190
}
192191
}
193192

194-
void LevelDbTransaction::Delete(const absl::string_view& key) {
193+
void LevelDbTransaction::Delete(absl::string_view key) {
195194
std::string to_delete(key);
196195
deletions_.insert(to_delete);
197196
mutations_.erase(to_delete);
@@ -200,12 +199,12 @@ void LevelDbTransaction::Delete(const absl::string_view& key) {
200199

201200
void LevelDbTransaction::Commit() {
202201
WriteBatch batch;
203-
for (auto it = deletions_.begin(); it != deletions_.end(); it++) {
204-
batch.Delete(*it);
202+
for (const auto& deletion : deletions_) {
203+
batch.Delete(deletion);
205204
}
206205

207-
for (auto it = mutations_.begin(); it != mutations_.end(); it++) {
208-
batch.Put(it->first, it->second);
206+
for (const auto& entry : mutations_) {
207+
batch.Put(entry.first, entry.second);
209208
}
210209

211210
LOG_DEBUG("Committing transaction: %s", ToString());
@@ -216,21 +215,21 @@ void LevelDbTransaction::Commit() {
216215
}
217216

218217
std::string LevelDbTransaction::ToString() {
219-
std::string dest("<LevelDbTransaction " + label_ + ": ");
220-
int64_t changes = deletions_.size() + mutations_.size();
221-
int64_t bytes = 0; // accumulator for size of individual mutations.
218+
std::string dest = absl::StrCat("<LevelDbTransaction ", label_, ": ");
219+
size_t changes = deletions_.size() + mutations_.size();
220+
size_t bytes = 0; // accumulator for size of individual mutations.
222221
dest += std::to_string(changes) + " changes ";
223222
std::string items; // accumulator for individual changes.
224-
for (auto it = deletions_.begin(); it != deletions_.end(); it++) {
225-
items += "\n - Delete " + DescribeKey(*it);
223+
for (const auto& deletion : deletions_) {
224+
absl::StrAppend(&items, "\n - Delete ", DescribeKey(deletion));
226225
}
227-
for (auto it = mutations_.begin(); it != mutations_.end(); it++) {
228-
int64_t change_bytes = it->second.length();
226+
for (const auto& entry : mutations_) {
227+
size_t change_bytes = entry.second.size();
229228
bytes += change_bytes;
230-
items += "\n - Put " + DescribeKey(it->first) + " (" +
231-
std::to_string(change_bytes) + " bytes)";
229+
absl::StrAppend(&items, "\n - Put ", DescribeKey(entry.first), " (",
230+
change_bytes, " bytes)");
232231
}
233-
dest += "(" + std::to_string(bytes) + " bytes):" + items + ">";
232+
absl::StrAppend(&dest, "(", bytes, " bytes):", items, ">");
234233
return dest;
235234
}
236235

Firestore/core/src/firebase/firestore/local/leveldb_transaction.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ class LevelDbTransaction {
154154
* Remove the database entry (if any) for "key". It is not an error if "key"
155155
* did not exist in the database.
156156
*/
157-
void Delete(const absl::string_view& key);
157+
void Delete(absl::string_view key);
158158

159159
#if __OBJC__
160160
/**
161161
* Schedules the row identified by `key` to be set to the given protocol
162162
* buffer message when this transaction commits.
163163
*/
164-
void Put(const absl::string_view& key, GPBMessage* message) {
164+
void Put(absl::string_view key, GPBMessage* message) {
165165
NSData* data = [message data];
166166
std::string key_string(key);
167167
mutations_[key_string] = std::string((const char*)data.bytes, data.length);
@@ -173,15 +173,15 @@ class LevelDbTransaction {
173173
* Schedules the row identified by `key` to be set to `value` when this
174174
* transaction commits.
175175
*/
176-
void Put(const absl::string_view& key, const absl::string_view& value);
176+
void Put(absl::string_view key, absl::string_view value);
177177

178178
/**
179179
* Sets the contents of `value` to the latest known value for the given key,
180180
* including any pending mutations and `Status::OK` is returned. If the key
181181
* doesn't exist in leveldb, or it is scheduled for deletion in this
182182
* transaction, `Status::NotFound` is returned.
183183
*/
184-
leveldb::Status Get(const absl::string_view& key, std::string* value);
184+
leveldb::Status Get(absl::string_view key, std::string* value);
185185

186186
/**
187187
* Returns a new Iterator over the pending changes in this transaction, merged

0 commit comments

Comments
 (0)