Skip to content

Commit

Permalink
add Setex() Psetex() and Strlen() (OpenAtomFoundation#2)
Browse files Browse the repository at this point in the history
* add Setex() Psetex() and Strlen()

* add Setnx() and Append() (OpenAtomFoundation#1)

* add examples

* add Setnx() and Append()

* bugfix add the lock before Put()

* bugfix logic errors“

* remove Psetex()

* fix confict

* fix conflict

* remove Psetex() example

* assert ttl argument greater than zero
  • Loading branch information
Axlgrep authored and KernelMaker committed Jan 17, 2018
1 parent fb6540e commit 2a69631
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/strings_example.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,16 @@ int main() {
s = db.Compact();
printf("Compact return: %s\n", s.ToString().c_str());

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

s = db.Set("TEST_KEY", "TEST_VALUE");
int32_t len = 0;
s = db.Strlen("TEST_KEY", &len);
printf("Strlen return: %s, strlen: %d\n", s.ToString().c_str(), len);

return 0;
}
2 changes: 2 additions & 0 deletions include/blackwidow/blackwidow.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class BlackWidow {
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);
Status Setex(const Slice& key, const Slice& value, int32_t ttl);
Status Strlen(const Slice& key, int32_t* len);

// 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 @@ -57,4 +57,12 @@ Status BlackWidow::Expire(const Slice& key, int32_t ttl) {
return strings_db_->Expire(key, ttl);
}

Status BlackWidow::Setex(const Slice& key, const Slice& value, int32_t ttl) {
return strings_db_->Setex(key, value, ttl);
}

Status BlackWidow::Strlen(const Slice& key, int32_t* len) {
return strings_db_->Strlen(key, len);
}

} // namespace blackwidow
20 changes: 20 additions & 0 deletions src/redis_strings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,24 @@ Status RedisStrings::CompactRange(const rocksdb::Slice* begin,
return db_->CompactRange(default_compact_range_options_, begin, end);
}

Status RedisStrings::Setex(const Slice& key, const Slice& value, int32_t ttl) {
//the ttl argument must greater than zero, to be compatible with redis
assert(ttl > 0);
InternalStringsValue internal_value(value);
internal_value.SetRelativeTimestamp(ttl);
ScopeRecordLock l(lock_mgr_, key);
return db_->Put(default_write_options_, key, internal_value.Encode());
}

Status RedisStrings::Strlen(const Slice& key, int32_t *len) {
std::string value;
Status s = Get(key, &value);
if (s.ok()) {
*len = value.size();
} else {
*len = 0;
}
return s;
}

} // namespace blackwidow
2 changes: 2 additions & 0 deletions src/redis_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class RedisStrings : public Redis {
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);
Status Setex(const Slice& key, const Slice& value, int32_t ttl);
Status Strlen(const Slice& key, int32_t *len);

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

0 comments on commit 2a69631

Please sign in to comment.