From 08e7397975299437e2bca094db3bfa6684700870 Mon Sep 17 00:00:00 2001 From: xth <xth@live.com> Date: Tue, 16 May 2023 21:19:50 +0800 Subject: [PATCH] Put any object conveniently as below: // ... User user; db_->Put("Tom", user); // or db_->Put("Tom", user, WriteOptions()); --- include/leveldb/db.h | 6 ++++++ include/leveldb/slice.h | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/include/leveldb/db.h b/include/leveldb/db.h index a13d147..5b40018 100644 --- a/include/leveldb/db.h +++ b/include/leveldb/db.h @@ -66,6 +66,12 @@ class LEVELDB_EXPORT DB { virtual Status Put(const WriteOptions& options, const Slice& key, const Slice& value) = 0; + // Convenience wrapper + Status Put(const Slice& key, const Slice& value, + const WriteOptions& options = WriteOptions()) { + return Put(options, key, value); + }; + // Remove the database entry (if any) for "key". Returns OK on // success, and a non-OK status on error. It is not an error if "key" // did not exist in the database. diff --git a/include/leveldb/slice.h b/include/leveldb/slice.h index 37cb821..e6f9657 100644 --- a/include/leveldb/slice.h +++ b/include/leveldb/slice.h @@ -35,6 +35,10 @@ class LEVELDB_EXPORT Slice { // Create a slice that refers to the contents of "s" Slice(const std::string& s) : data_(s.data()), size_(s.size()) {} + // For any object + template<typename T> + Slice(const T& s) : data_(reinterpret_cast<const char*>(&s)), size_(sizeof(T)) {} + // Create a slice that refers to s[0,strlen(s)-1] Slice(const char* s) : data_(s), size_(strlen(s)) {}