diff options
author | Greg Soltis <gsoltis@google.com> | 2018-03-22 09:51:30 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-22 09:51:30 -0700 |
commit | 7a512f4a367aa4c2e75b53531d9b92ad37f130fe (patch) | |
tree | 9b0e2e1d14193ef1eedccefdc089d14be119a783 /Firestore | |
parent | cf630bfee60694f9bf1577972df169badda0b6e0 (diff) |
Add `ToString()` to `LevelDbTransaction` (#946)
* Initial plumbing for leveldb local parts
* Add model::BatchId
* Port leveldb_key.{h,cc}
* Add string StartsWith
* Add leveldb_key_test.cc to the project
* Revert back to using leveldb::Slice for read/describe
These operations universally operate on keys obtained from leveldb so
it's actually unhelpful to force all the callers to make
absl::string_views from them.
* Start work on ToString for transactions
* Add ToString() method to LevelDbTransaction
* Style
* lint
* Revert addition of util::StartsWith
Diffstat (limited to 'Firestore')
3 files changed, 49 insertions, 0 deletions
diff --git a/Firestore/Example/Tests/Local/FSTLevelDBTransactionTests.mm b/Firestore/Example/Tests/Local/FSTLevelDBTransactionTests.mm index 704f8c6..2a1efb0 100644 --- a/Firestore/Example/Tests/Local/FSTLevelDBTransactionTests.mm +++ b/Firestore/Example/Tests/Local/FSTLevelDBTransactionTests.mm @@ -20,7 +20,9 @@ #include <absl/strings/string_view.h> #include <leveldb/db.h> #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h" +#import "Firestore/Protos/objc/firestore/local/Mutation.pbobjc.h" #import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h" +#include "Firestore/core/src/firebase/firestore/local/leveldb_key.h" NS_ASSUME_NONNULL_BEGIN @@ -29,6 +31,7 @@ using leveldb::Options; using leveldb::ReadOptions; using leveldb::WriteOptions; using leveldb::Status; +using firebase::firestore::local::LevelDbMutationKey; using firebase::firestore::local::LevelDbTransaction; @interface FSTLevelDBTransactionTests : XCTestCase @@ -269,6 +272,30 @@ using firebase::firestore::local::LevelDbTransaction; XCTAssertFalse(it->Valid()); } +- (void)testToString { + std::string key = LevelDbMutationKey::Key("user1", 42); + FSTPBWriteBatch *message = [FSTPBWriteBatch message]; + message.batchId = 42; + + LevelDbTransaction transaction(_db.get()); + std::string description = transaction.ToString(); + XCTAssertEqual(description, "<LevelDbTransaction: 0 changes (0 bytes):>"); + + transaction.Put(key, message); + description = transaction.ToString(); + XCTAssertEqual(description, + "<LevelDbTransaction: 1 changes (2 bytes):\n" + " - Put [mutation: user_id=user1 batch_id=42] (2 bytes)>"); + + std::string key2 = LevelDbMutationKey::Key("user1", 43); + transaction.Delete(key2); + description = transaction.ToString(); + XCTAssertEqual(description, + "<LevelDbTransaction: 2 changes (2 bytes):\n" + " - Delete [mutation: user_id=user1 batch_id=43]\n" + " - Put [mutation: user_id=user1 batch_id=42] (2 bytes)>"); +} + @end NS_ASSUME_NONNULL_END diff --git a/Firestore/core/src/firebase/firestore/local/leveldb_transaction.cc b/Firestore/core/src/firebase/firestore/local/leveldb_transaction.cc index af72716..4e4a313 100644 --- a/Firestore/core/src/firebase/firestore/local/leveldb_transaction.cc +++ b/Firestore/core/src/firebase/firestore/local/leveldb_transaction.cc @@ -18,6 +18,7 @@ #include <leveldb/write_batch.h> +#include "Firestore/core/src/firebase/firestore/local/leveldb_key.h" #include "Firestore/core/src/firebase/firestore/util/firebase_assert.h" using leveldb::DB; @@ -206,6 +207,25 @@ void LevelDbTransaction::Commit() { status.ToString().c_str()); } +std::string LevelDbTransaction::ToString() { + std::string dest("<LevelDbTransaction: "); + int64_t changes = deletions_.size() + mutations_.size(); + int64_t bytes = 0; // accumulator for size of individual mutations. + dest += std::to_string(changes) + " changes "; + std::string items; // accumulator for individual changes. + for (auto it = deletions_.begin(); it != deletions_.end(); it++) { + items += "\n - Delete " + Describe(*it); + } + for (auto it = mutations_.begin(); it != mutations_.end(); it++) { + int64_t change_bytes = it->second.length(); + bytes += change_bytes; + items += "\n - Put " + Describe(it->first) + " (" + + std::to_string(change_bytes) + " bytes)"; + } + dest += "(" + std::to_string(bytes) + " bytes):" + items + ">"; + return dest; +} + } // namespace local } // namespace firestore } // namespace firebase diff --git a/Firestore/core/src/firebase/firestore/local/leveldb_transaction.h b/Firestore/core/src/firebase/firestore/local/leveldb_transaction.h index 015a6cb..b219a69 100644 --- a/Firestore/core/src/firebase/firestore/local/leveldb_transaction.h +++ b/Firestore/core/src/firebase/firestore/local/leveldb_transaction.h @@ -188,6 +188,8 @@ class LevelDbTransaction { */ void Commit(); + std::string ToString(); + private: leveldb::DB* db_; Mutations mutations_; |