Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add sortedint data type #16

Merged
merged 6 commits into from
Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
FIX: sortedint 's id should use stoull (uint64_t)
FIX: code style
  • Loading branch information
karelrooted committed Sep 3, 2019
commit 1a62f28e59497c78af5d987638cdf48bc61642b6
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ target_sources(kvrocks PRIVATE
src/redis_bitmap.h
src/redis_pubsub.cc
src/redis_pubsub.h
src/redis_sortint.cc
src/redis_sortint.h
src/redis_sortedint.cc
src/redis_sortedint.h
src/lock_manager.cc
src/rocksdb_crc32c.h
src/config.cc
Expand Down Expand Up @@ -195,8 +195,8 @@ target_sources(kvrocks2redis PRIVATE
src/redis_bitmap.h
src/redis_pubsub.cc
src/redis_pubsub.h
src/redis_sortint.cc
src/redis_sortint.h
src/redis_sortedint.cc
src/redis_sortedint.h
src/replication.cc
src/replication.h
src/lock_manager.cc
Expand Down Expand Up @@ -255,8 +255,8 @@ add_executable(unittest
src/redis_list.cc
src/redis_set.cc
src/redis_zset.cc
src/redis_sortint.cc
src/redis_sortint.h
src/redis_sortedint.cc
src/redis_sortedint.h
src/util.cc
src/storage.cc
src/lock_manager.cc
Expand Down
56 changes: 28 additions & 28 deletions src/redis_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "redis_string.h"
#include "redis_zset.h"
#include "redis_pubsub.h"
#include "redis_sortint.h"
#include "redis_sortedint.h"
#include "replication.h"
#include "util.h"
#include "storage.h"
Expand Down Expand Up @@ -2434,14 +2434,14 @@ class CommandZInterStore : public CommandZUnionStore {
}
};

class CommandSortintAdd : public Commander {
class CommandSortedintAdd : public Commander {
public:
CommandSortintAdd() : Commander("siadd", -3, true) {}
CommandSortedintAdd() : Commander("siadd", -3, true) {}

Status Parse(const std::vector<std::string> &args) override {
try {
for (unsigned i = 2; i < args.size(); i++) {
auto id = std::stoi(args[i]);
auto id = std::stoull(args[i]);
ids_.emplace_back(id);
}
} catch (const std::exception &e) {
Expand All @@ -2451,9 +2451,9 @@ class CommandSortintAdd : public Commander {
}

Status Execute(Server *svr, Connection *conn, std::string *output) override {
Redis::Sortint sortint_db(svr->storage_, conn->GetNamespace());
Redis::Sortedint sortedint_db(svr->storage_, conn->GetNamespace());
int ret;
rocksdb::Status s = sortint_db.Add(args_[1], ids_, &ret);
rocksdb::Status s = sortedint_db.Add(args_[1], ids_, &ret);
if (!s.ok()) {
return Status(Status::RedisExecErr, s.ToString());
}
Expand All @@ -2465,14 +2465,14 @@ class CommandSortintAdd : public Commander {
std::vector<uint64_t> ids_;
};

class CommandSortintRem : public Commander {
class CommandSortedintRem : public Commander {
public:
CommandSortintRem() : Commander("sirem", -3, true) {}
CommandSortedintRem() : Commander("sirem", -3, true) {}

Status Parse(const std::vector<std::string> &args) override {
try {
for (unsigned i = 2; i < args.size(); i++) {
auto id = std::stoi(args[i]);
auto id = std::stoull(args[i]);
ids_.emplace_back(id);
}
} catch (const std::exception &e) {
Expand All @@ -2482,9 +2482,9 @@ class CommandSortintRem : public Commander {
}

Status Execute(Server *svr, Connection *conn, std::string *output) override {
Redis::Sortint sortint_db(svr->storage_, conn->GetNamespace());
Redis::Sortedint sortedint_db(svr->storage_, conn->GetNamespace());
int ret;
rocksdb::Status s = sortint_db.Remove(args_[1], ids_, &ret);
rocksdb::Status s = sortedint_db.Remove(args_[1], ids_, &ret);
if (!s.ok()) {
return Status(Status::RedisExecErr, s.ToString());
}
Expand All @@ -2496,14 +2496,14 @@ class CommandSortintRem : public Commander {
std::vector<uint64_t> ids_;
};

class CommandSortintCard : public Commander {
class CommandSortedintCard : public Commander {
public:
CommandSortintCard() : Commander("sicard", 2, false) {}
CommandSortedintCard() : Commander("sicard", 2, false) {}

Status Execute(Server *svr, Connection *conn, std::string *output) override {
Redis::Sortint sortint_db(svr->storage_, conn->GetNamespace());
Redis::Sortedint sortedint_db(svr->storage_, conn->GetNamespace());
int ret;
rocksdb::Status s = sortint_db.Card(args_[1], &ret);
rocksdb::Status s = sortedint_db.Card(args_[1], &ret);
if (!s.ok()) {
return Status(Status::RedisExecErr, s.ToString());
}
Expand All @@ -2512,9 +2512,9 @@ class CommandSortintCard : public Commander {
}
};

class CommandSortintRange : public Commander {
class CommandSortedintRange : public Commander {
public:
explicit CommandSortintRange(bool reversed = false) : Commander("sirange", -4, false) {
explicit CommandSortedintRange(bool reversed = false) : Commander("sirange", -4, false) {
reversed_ = reversed;
}

Expand All @@ -2523,7 +2523,7 @@ class CommandSortintRange : public Commander {
offset_ = std::stoi(args[2]);
limit_ = std::stoi(args[3]);
if (args.size() == 5) {
cursor_id_ = std::stoi(args[4]);
cursor_id_ = std::stoull(args[4]);
}
} catch (const std::exception &e) {
return Status(Status::RedisParseErr, kValueNotInterger);
Expand All @@ -2532,9 +2532,9 @@ class CommandSortintRange : public Commander {
}

Status Execute(Server *svr, Connection *conn, std::string *output) override {
Redis::Sortint sortint_db(svr->storage_, conn->GetNamespace());
Redis::Sortedint sortedint_db(svr->storage_, conn->GetNamespace());
std::vector<uint64_t> ids;
rocksdb::Status s = sortint_db.Range(args_[1], cursor_id_, offset_, limit_, reversed_, &ids);
rocksdb::Status s = sortedint_db.Range(args_[1], cursor_id_, offset_, limit_, reversed_, &ids);
if (!s.ok()) {
return Status(Status::RedisExecErr, s.ToString());
}
Expand All @@ -2552,9 +2552,9 @@ class CommandSortintRange : public Commander {
bool reversed_ = false;
};

class CommandSortintRevRange : public CommandSortintRange {
class CommandSortedintRevRange : public CommandSortedintRange {
public:
CommandSortintRevRange() : CommandSortintRange(true) { name_ = "sirevrange"; }
CommandSortedintRevRange() : CommandSortedintRange(true) { name_ = "sirevrange"; }
};

class CommandInfo : public Commander {
Expand Down Expand Up @@ -3873,26 +3873,26 @@ std::map<std::string, CommanderFactory> command_table = {
[]() -> std::unique_ptr<Commander> {
return std::unique_ptr<Commander>(new CommandPubSub);
}},
// sortint command
// Sortedint command
{"siadd",
[]() -> std::unique_ptr<Commander> {
return std::unique_ptr<Commander>(new CommandSortintAdd);
return std::unique_ptr<Commander>(new CommandSortedintAdd);
}},
{"sirem",
[]() -> std::unique_ptr<Commander> {
return std::unique_ptr<Commander>(new CommandSortintRem);
return std::unique_ptr<Commander>(new CommandSortedintRem);
}},
{"sicard",
[]() -> std::unique_ptr<Commander> {
return std::unique_ptr<Commander>(new CommandSortintCard);
return std::unique_ptr<Commander>(new CommandSortedintCard);
}},
{"sirange",
[]() -> std::unique_ptr<Commander> {
return std::unique_ptr<Commander>(new CommandSortintRange);
return std::unique_ptr<Commander>(new CommandSortedintRange);
}},
{"sirevrange",
[]() -> std::unique_ptr<Commander> {
return std::unique_ptr<Commander>(new CommandSortintRevRange);
return std::unique_ptr<Commander>(new CommandSortedintRevRange);
}},

// internal management cmd
Expand Down
6 changes: 3 additions & 3 deletions src/redis_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum RedisType {
kRedisSet,
kRedisZSet,
kRedisBitmap,
kRedisSortint,
kRedisSortedint,
};

enum RedisCommand {
Expand Down Expand Up @@ -114,9 +114,9 @@ class BitmapMetadata : public Metadata {
BitmapMetadata(): Metadata(kRedisBitmap){}
};

class SortintMetadata : public Metadata {
class SortedintMetadata : public Metadata {
public:
SortintMetadata() : Metadata(kRedisSortint) {}
SortedintMetadata() : Metadata(kRedisSortedint) {}
};

class ListMetadata : public Metadata {
Expand Down
42 changes: 19 additions & 23 deletions src/redis_sortint.cc → src/redis_sortedint.cc
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
#include "redis_sortint.h"
#include "redis_sortedint.h"

#include <map>
#include <iostream>
#include <limits>

namespace Redis {

rocksdb::Status Sortint::GetMetadata(const Slice &ns_key, SortintMetadata *metadata) {
return Database::GetMetadata(kRedisSortint, ns_key, metadata);
rocksdb::Status Sortedint::GetMetadata(const Slice &ns_key, SortedintMetadata *metadata) {
return Database::GetMetadata(kRedisSortedint, ns_key, metadata);
}

rocksdb::Status Sortint::Add(const Slice &user_key, std::vector<uint64_t> ids, int *ret) {
rocksdb::Status Sortedint::Add(const Slice &user_key, std::vector<uint64_t> ids, int *ret) {
*ret = 0;

std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);

LockGuard guard(storage_->GetLockManager(), ns_key);
SortintMetadata metadata;
SortedintMetadata metadata;
rocksdb::Status s = GetMetadata(ns_key, &metadata);
if (!s.ok() && !s.IsNotFound()) return s;

std::string value;
rocksdb::WriteBatch batch;
WriteBatchLogData log_data(kRedisSortint);
WriteBatchLogData log_data(kRedisSortedint);
batch.PutLogData(log_data.Encode());
std::string sub_key;
for (const auto id : ids) {
Expand All @@ -44,20 +44,20 @@ rocksdb::Status Sortint::Add(const Slice &user_key, std::vector<uint64_t> ids, i
return storage_->Write(rocksdb::WriteOptions(), &batch);
}

rocksdb::Status Sortint::Remove(const Slice &user_key, std::vector<uint64_t> ids, int *ret) {
rocksdb::Status Sortedint::Remove(const Slice &user_key, std::vector<uint64_t> ids, int *ret) {
*ret = 0;

std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);

LockGuard guard(storage_->GetLockManager(), ns_key);
SortintMetadata metadata;
SortedintMetadata metadata;
rocksdb::Status s = GetMetadata(ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;

std::string value, sub_key;
rocksdb::WriteBatch batch;
WriteBatchLogData log_data(kRedisSortint);
WriteBatchLogData log_data(kRedisSortedint);
batch.PutLogData(log_data.Encode());
for (const auto id : ids) {
std::string id_buf;
Expand All @@ -68,28 +68,27 @@ rocksdb::Status Sortint::Remove(const Slice &user_key, std::vector<uint64_t> ids
batch.Delete(sub_key);
*ret += 1;
}
if (*ret > 0) {
metadata.size -= *ret;
std::string bytes;
metadata.Encode(&bytes);
batch.Put(metadata_cf_handle_, ns_key, bytes);
}
if (*ret == 0) return rocksdb::Status::OK();
metadata.size -= *ret;
std::string bytes;
metadata.Encode(&bytes);
batch.Put(metadata_cf_handle_, ns_key, bytes);
return storage_->Write(rocksdb::WriteOptions(), &batch);
}

rocksdb::Status Sortint::Card(const Slice &user_key, int *ret) {
rocksdb::Status Sortedint::Card(const Slice &user_key, int *ret) {
*ret = 0;
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);

SortintMetadata metadata;
SortedintMetadata metadata;
rocksdb::Status s = GetMetadata(ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
*ret = metadata.size;
return rocksdb::Status::OK();
}

rocksdb::Status Sortint::Range(const Slice &user_key,
rocksdb::Status Sortedint::Range(const Slice &user_key,
uint64_t cursor_id,
uint64_t offset,
uint64_t limit,
Expand All @@ -100,7 +99,7 @@ rocksdb::Status Sortint::Range(const Slice &user_key,
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);

SortintMetadata metadata;
SortedintMetadata metadata;
rocksdb::Status s = GetMetadata(ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;

Expand All @@ -124,10 +123,7 @@ rocksdb::Status Sortint::Range(const Slice &user_key,
InternalKey ikey(iter->key());
Slice sub_key = ikey.GetSubKey();
GetFixed64(&sub_key, &id);
if (id == cursor_id) {
continue;
}
if (offset >= 0 && pos++ < offset) continue;
if ( id == cursor_id || pos++ < offset ) continue;
ids->emplace_back(id);
if (limit > 0 && ids && ids->size() >= limit) break;
}
Expand Down
6 changes: 3 additions & 3 deletions src/redis_sortint.h → src/redis_sortedint.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace Redis {

class Sortint : public Database {
class Sortedint : public Database {
public:
explicit Sortint(Engine::Storage *storage, const std::string &ns) : Database(storage, ns) {}
explicit Sortedint(Engine::Storage *storage, const std::string &ns) : Database(storage, ns) {}
rocksdb::Status Card(const Slice &user_key, int *ret);
rocksdb::Status Add(const Slice &user_key, std::vector<uint64_t> ids, int *ret);
rocksdb::Status Remove(const Slice &user_key, std::vector<uint64_t> ids, int *ret);
Expand All @@ -22,7 +22,7 @@ class Sortint : public Database {
std::vector<uint64_t> *ids);

private:
rocksdb::Status GetMetadata(const Slice &ns_key, SortintMetadata *metadata);
rocksdb::Status GetMetadata(const Slice &ns_key, SortedintMetadata *metadata);
};

} // namespace Redis