Put any object conveniently as below:

// ...
User user;
db_->Put("Tom", user);
// or
db_->Put("Tom", user, WriteOptions());
This commit is contained in:
xth 2023-05-16 21:19:50 +08:00
parent 068d5ee1a3
commit 08e7397975
2 changed files with 10 additions and 0 deletions

View File

@ -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.

View File

@ -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)) {}