Skip to content

Commit

Permalink
add Setnx() and Append() (OpenAtomFoundation#1)
Browse files Browse the repository at this point in the history
* add examples

* add Setnx() and Append()

* bugfix add the lock before Put()

* bugfix logic errors“
  • Loading branch information
Leviathan1995 authored and KernelMaker committed Jan 17, 2018
1 parent 8da8e20 commit fb6540e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@
# Others
tags
src/build_version.cc
examples/strings_example
examples/db
18 changes: 18 additions & 0 deletions examples/strings_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,34 @@ int main() {
printf("Open failed, error: %s\n", s.ToString().c_str());
return -1;
}
// Set
s = db.Set("TEST_KEY", "TEST_VALUE");
printf("Set return: %s\n", s.ToString().c_str());

// Get
std::string value;
s = db.Get("TEST_KEY", &value);
printf("Get return: %s, value: %s\n", s.ToString().c_str(), value.c_str());

// Setnx
int32_t ret;
s = db.Setnx("TEST_KEY", "TEST_VALUE", &ret);
printf("Setnx return: %s, value: %s, ret: %d\n", s.ToString().c_str(), value.c_str(), ret);

// Append
std::string append_value;
s = db.Append("TEST_KEY", "APPEND_VALUE", &ret);
s = db.Get("TEST_KEY", &append_value);
printf("Append return: %s, value: %s, ret: %d\n", s.ToString().c_str(), append_value.c_str(), ret);

// Expire
s = db.Expire("TEST_KEY", 1);
printf("Expire return: %s\n", s.ToString().c_str());
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
s = db.Get("TEST_KEY", &value);
printf("Get return: %s, value: %s\n", s.ToString().c_str(), value.c_str());

// Compact
s = db.Compact();
printf("Compact return: %s\n", s.ToString().c_str());

Expand Down
2 changes: 2 additions & 0 deletions include/blackwidow/blackwidow.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class BlackWidow {
// Strings Commands
Status Set(const Slice& key, const Slice& value);
Status Get(const Slice& key, std::string* value);
Status Setnx(const Slice& key, const Slice& value, int32_t* ret);
Status Append(const Slice& key, const Slice& value, int32_t* ret);

// Keys Commands
Status Expire(const Slice& key, int32_t ttl);
Expand Down
8 changes: 8 additions & 0 deletions src/blackwidow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ Status BlackWidow::Get(const Slice& key, std::string* value) {
return strings_db_->Get(key, value);
}

Status BlackWidow::Setnx(const Slice& key, const Slice& value, int32_t* ret) {
return strings_db_->Setnx(key, value, ret);
}

Status BlackWidow::Append(const Slice& key, const Slice& value, int32_t* ret) {
return strings_db_->Append(key, value, ret);
}

Status BlackWidow::Expire(const Slice& key, int32_t ttl) {
return strings_db_->Expire(key, ttl);
}
Expand Down
50 changes: 50 additions & 0 deletions src/redis_strings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,56 @@ Status RedisStrings::Get(const Slice& key, std::string* value) {
return s;
}

Status RedisStrings::Setnx(const Slice& key, const Slice& value, int32_t* ret) {
*ret = 0;
std::string old_value;
ScopeRecordLock l(lock_mgr_, key);
Status s = db_->Get(default_read_options_, key, &old_value);
if (s.ok()) {
ParsedInternalStringsValue internal_value(&old_value);
if (internal_value.IsStale()) {
InternalStringsValue new_value(value);
s = db_->Put(default_write_options_, key, new_value.Encode());
if (s.ok()) {
*ret = 1;
}
}
} else if (s.IsNotFound()) {
InternalStringsValue new_value(value);
s = db_->Put(default_write_options_, key, new_value.Encode());
if (s.ok()) {
*ret = 1;
}
}
return s;
}

Status RedisStrings::Append(const Slice& key, const Slice& value, int32_t* ret) {
std::string old_value;
*ret = 0;
ScopeRecordLock l(lock_mgr_, key);
Status s = db_->Get(default_read_options_, key, &old_value);
if (s.ok()) {
ParsedInternalStringsValue internal_value(&old_value);
if (internal_value.IsStale()) {
*ret = value.size();
InternalStringsValue new_value(value);
return db_->Put(default_write_options_, key, new_value.Encode());
} else {
internal_value.StripSuffix();
*ret = old_value.size() + value.size();
old_value += value.data();
InternalStringsValue new_value(old_value);
return db_->Put(default_write_options_, key, new_value.Encode());
}
} else if (s.IsNotFound()) {
*ret = value.size();
InternalStringsValue internal_value(value);
return db_->Put(default_write_options_, key, internal_value.Encode());
}
return s;
}

Status RedisStrings::Expire(const Slice& key, int32_t ttl) {
std::string value;
ScopeRecordLock l(lock_mgr_, key);
Expand Down
2 changes: 2 additions & 0 deletions src/redis_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class RedisStrings : public Redis {
// Strings Commands
Status Set(const Slice& key, const Slice& value);
Status Get(const Slice& key, std::string* value);
Status Setnx(const Slice& key, const Slice& value, int32_t* ret);
Status Append(const Slice& key, const Slice& value, int32_t* ret);

// Common Commands
virtual Status Open(const rocksdb::Options& options,
Expand Down

0 comments on commit fb6540e

Please sign in to comment.