Skip to content

Commit

Permalink
Merge pull request #8 from meitu/feature/cmd-flushall
Browse files Browse the repository at this point in the history
ADD: cmd flushall
  • Loading branch information
git-hulk committed Aug 22, 2019
2 parents 076a058 + ab47ed3 commit 3c10b95
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/redis_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ class CommandFlushDB : public Commander {
}
};

class CommandFlushAll : public Commander {
public:
CommandFlushAll() : Commander("flushall", 1, false) {}
Status Execute(Server *svr, Connection *conn, std::string *output) override {
if (!conn->IsAdmin()) {
*output = Redis::Error("only administrator can use flushall command");
return Status::OK();
}
Redis::Database redis(svr->storage_, conn->GetNamespace());
rocksdb::Status s = redis.FlushAll();
LOG(WARNING) << "All DB keys was flushed, addr: " << conn->GetAddr();
if (s.ok()) {
*output = Redis::SimpleString("OK");
return Status::OK();
}
return Status(Status::RedisExecErr, s.ToString());
}
};

class CommandPing : public Commander {
public:
CommandPing() : Commander("ping", 1, false) {}
Expand Down Expand Up @@ -3239,6 +3258,10 @@ std::map<std::string, CommanderFactory> command_table = {
[]() -> std::unique_ptr<Commander> {
return std::unique_ptr<Commander>(new CommandFlushDB);
}},
{"flushall",
[]() -> std::unique_ptr<Commander> {
return std::unique_ptr<Commander>(new CommandFlushAll);
}},
{"dbsize",
[]() -> std::unique_ptr<Commander> {
return std::unique_ptr<Commander>(new CommandDBSize);
Expand Down
38 changes: 37 additions & 1 deletion src/redis_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,43 @@ rocksdb::Status Database::FlushDB() {
for (iter->Seek(prefix);
iter->Valid() && iter->key().starts_with(prefix);
iter->Next()) {
db_->Delete(rocksdb::WriteOptions(), metadata_cf_handle_, iter->key());
auto s = db_->Delete(rocksdb::WriteOptions(), metadata_cf_handle_, iter->key());
if (!s.ok()) {
delete iter;
return s;
}
}
delete iter;
return rocksdb::Status::OK();
}

rocksdb::Status Database::FlushAll() {
LatestSnapShot ss(db_);
rocksdb::ReadOptions read_options;
read_options.snapshot = ss.GetSnapShot();
read_options.fill_cache = false;
auto iter = db_->NewIterator(read_options, metadata_cf_handle_);
iter->SeekToFirst();
if (!iter->Valid()) {
delete iter;
return rocksdb::Status::OK();
}
auto first_key = iter->key().ToString();
iter->SeekToLast();
if (!iter->Valid()) {
delete iter;
return rocksdb::Status::OK();
}
auto last_key = iter->key().ToString();
auto s = db_->DeleteRange(rocksdb::WriteOptions(), metadata_cf_handle_, first_key, last_key);
if (!s.ok()) {
delete iter;
return s;
}
s = db_->Delete(rocksdb::WriteOptions(), metadata_cf_handle_, last_key);
if (!s.ok()) {
delete iter;
return s;
}
delete iter;
return rocksdb::Status::OK();
Expand Down
1 change: 1 addition & 0 deletions src/redis_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Database {
rocksdb::Status Type(const Slice &user_key, RedisType *type);
rocksdb::Status Dump(const Slice &user_key, std::vector<std::string> *infos);
rocksdb::Status FlushDB();
rocksdb::Status FlushAll();
void GetKeyNumStats(const std::string &prefix, KeyNumStats *stats);
void Keys(std::string prefix, std::vector<std::string> *keys = nullptr, KeyNumStats *stats = nullptr);
rocksdb::Status Scan(const std::string &cursor,
Expand Down
70 changes: 70 additions & 0 deletions tests/functional/key_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,73 @@ def test_scan():

ret = conn.delete(key)
assert (ret == 1)


def test_flushdb():
key = "test_flushdb"
key_zset = key + "_zset"
conn = get_redis_conn()
ret = conn.set(key, "bar")
assert ret
ret = conn.zadd(key_zset, 'a', 1.3)
assert (ret == 1)

ret = conn.flushdb()
assert ret

ret = conn.exists(key)
assert (ret == False)
ret = conn.exists(key_zset)
assert (ret == False)

ret = conn.set(key, "bar")
assert ret
ret = conn.get(key)
assert (ret == "bar")
ret = conn.zadd(key_zset, 'a', 1.3)
assert (ret == 1)
ret = conn.zscore(key_zset, 'a')
assert (ret == 1.3)

ret = conn.flushdb()
assert ret

ret = conn.exists(key)
assert (ret == False)
ret = conn.exists(key_zset)
assert (ret == False)


def test_flushall():
key = "test_flushall"
key_zset = key + "_zset"
conn = get_redis_conn()
ret = conn.set(key, "bar")
assert (ret)
ret = conn.zadd(key_zset, 'a', 1.3)
assert (ret == 1)

ret = conn.flushall()
assert (ret)

ret = conn.exists(key)
assert (ret == False)
ret = conn.exists(key_zset)
assert (ret == False)

ret = conn.set(key, "bar")
assert ret
ret = conn.get(key)
assert (ret == "bar")
ret = conn.zadd(key_zset, 'a', 1.3)
assert (ret == 1)
ret = conn.zscore(key_zset, 'a')
assert (ret == 1.3)

ret = conn.flushdb()
assert ret

ret = conn.exists(key)
assert (ret == False)
ret = conn.exists(key_zset)
assert (ret == False)

0 comments on commit 3c10b95

Please sign in to comment.