Skip to content

Commit

Permalink
reimplement db
Browse files Browse the repository at this point in the history
  • Loading branch information
fake committed Nov 25, 2016
1 parent 0623ed6 commit a3a758f
Show file tree
Hide file tree
Showing 26 changed files with 901 additions and 737 deletions.
53 changes: 34 additions & 19 deletions base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ struct expiration {
};

class db;
struct redis_key {
sstring _key;
size_t _hash;
redis_key() = default;
redis_key(redis_key&) = default;
redis_key(sstring key) : _key(key), _hash(std::hash<sstring>()(_key)) {}
redis_key(sstring&& key, size_t&& hash) : _key(std::move(key)), _hash(std::move(hash)) {}
redis_key(redis_key&& rk) : _key(std::move(rk._key)), _hash(rk._hash) {}
inline const size_t hash() const { return _hash; }
inline const sstring& key() const { return _key; }
inline const size_t size() const { return _key.size(); }
inline const char* data() const { return _key.c_str(); }
};

// The defination of `item was copied from apps/memcached
class item : public slab_item_base {
public:
Expand All @@ -149,7 +163,7 @@ private:
uint64_t _uint64;
int64_t _int64;
double _double;
char _data[];
char _data[0];
} _u;
friend class dict;
static constexpr uint32_t field_alignment = alignof(void*);
Expand Down Expand Up @@ -182,25 +196,25 @@ public:
return sizeof(item) + align_up(static_cast<uint32_t>(key_size), field_alignment) + sizeof(double);
}
public:
item(uint32_t slab_page_index, const sstring& key, size_t kh, sstring&& value)
item(uint32_t slab_page_index, const redis_key& key, sstring&& value)
: _value_size(value.size())
, _key_size(key.size())
, _key_hash(kh)
, _key_hash(key.hash())
, _slab_page_index(slab_page_index)
, _ref_count(0U)
, _type(REDIS_RAW_STRING)
, _expire(0)
{
memcpy(_u._data, value.c_str(), _value_size);
if (_key_size > 0) {
memcpy(_u._data + align_up(_value_size, field_alignment), key.c_str(), _key_size);
memcpy(_u._data + align_up(_value_size, field_alignment), key.data(), _key_size);
}
}

item(uint32_t slab_page_index, const sstring& key, size_t kh, const std::experimental::string_view& value, sstring&& append)
item(uint32_t slab_page_index, const redis_key& key, const std::experimental::string_view& value, sstring&& append)
: _value_size(value.size())
, _key_size(key.size())
, _key_hash(kh)
, _key_hash(key.hash())
, _slab_page_index(slab_page_index)
, _ref_count(0U)
, _type(REDIS_RAW_STRING)
Expand All @@ -210,7 +224,7 @@ public:
memcpy(_u._data + _value_size, append.c_str(), append.size());
_value_size += append.size();
if (_key_size > 0) {
memcpy(_u._data + align_up(_value_size, field_alignment), key.c_str(), _key_size);
memcpy(_u._data + align_up(_value_size, field_alignment), key.data(), _key_size);
}
}

Expand All @@ -226,63 +240,63 @@ public:
memcpy(_u._data, value.c_str(), _value_size);
}

item(uint32_t slab_page_index, const sstring& key, size_t kh, uint64_t value)
item(uint32_t slab_page_index, const redis_key& key, uint64_t value)
: _value_size(sizeof(uint64_t))
, _key_size(key.size())
, _key_hash(kh)
, _key_hash(key.hash())
, _slab_page_index(slab_page_index)
, _ref_count(0U)
, _type(REDIS_RAW_UINT64)
, _expire(0)
{
_u._uint64 = value;
if (_key_size > 0) {
memcpy(_u._data + align_up(_value_size, field_alignment), key.c_str(), _key_size);
memcpy(_u._data + align_up(_value_size, field_alignment), key.data(), _key_size);
}
}

item(uint32_t slab_page_index, const sstring& key, size_t kh, double value)
item(uint32_t slab_page_index, const redis_key& key, double value)
: _value_size(sizeof(double))
, _key_size(key.size())
, _key_hash(kh)
, _key_hash(key.hash())
, _slab_page_index(slab_page_index)
, _ref_count(0U)
, _type(REDIS_RAW_DOUBLE)
, _expire(0)
{
_u._double = value;
if (_key_size > 0) {
memcpy(_u._data + align_up(_value_size, field_alignment), key.c_str(), _key_size);
memcpy(_u._data + align_up(_value_size, field_alignment), key.data(), _key_size);
}
}

item(uint32_t slab_page_index, const sstring& key, size_t kh, int64_t value)
item(uint32_t slab_page_index, const redis_key& key, int64_t value)
: _value_size(sizeof(int64_t))
, _key_size(key.size())
, _key_hash(kh)
, _key_hash(key.hash())
, _slab_page_index(slab_page_index)
, _ref_count(0U)
, _type(REDIS_RAW_INT64)
, _expire(0)
{
_u._int64 = value;
if (_key_size > 0) {
memcpy(_u._data + align_up(_value_size, field_alignment), key.c_str(), _key_size);
memcpy(_u._data + align_up(_value_size, field_alignment), key.data(), _key_size);
}
}

item(uint32_t slab_page_index, const sstring& key, size_t kh, object* ptr, uint8_t type)
item(uint32_t slab_page_index, const redis_key& key, object* ptr, uint8_t type)
: _value_size(sizeof(void*))
, _key_size(key.size())
, _key_hash(kh)
, _key_hash(key.hash())
, _slab_page_index(slab_page_index)
, _ref_count(0U)
, _type(type)
, _expire(0)
{
_u._ptr = ptr;
if (_key_size > 0) {
memcpy(_u._data + align_up(_value_size, field_alignment), key.c_str(), _key_size);
memcpy(_u._data + align_up(_value_size, field_alignment), key.data(), _key_size);
}
}

Expand Down Expand Up @@ -352,6 +366,7 @@ public:
assert(it->_ref_count >= 0);
}
};

static const sstring msg_crlf {"\r\n"};
static const sstring msg_ok {"+OK\r\n"};
static const sstring msg_pong {"+PONG\r\n"};
Expand Down
10 changes: 10 additions & 0 deletions db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,25 @@ namespace redis {
static constexpr double default_slab_growth_factor = 1.25;
static constexpr uint64_t default_slab_page_size = 1UL*MB;
static constexpr uint64_t default_per_cpu_slab_size = 0UL; // zero means reclaimer is enabled.
static const sstring LIST { "list" };
static const sstring DICT { "dict" };
static const sstring MISC { "misc" };
static const sstring SET { "set" };

__thread slab_allocator<item>* _slab;
__thread redis_commands* _redis_commands_ptr;

db::db() : _store(new dict())
, _misc_storage(MISC, _store)
, _list_storage(LIST, _store)
, _dict_storage(DICT, _store)
{
_slab = new slab_allocator<item>(default_slab_growth_factor,
default_per_cpu_slab_size, default_slab_page_size,
[this](item& item_ref) { intrusive_ptr_release(&item_ref); });
_redis_commands_ptr = new redis_commands();
}

db::~db()
{
if (_store != nullptr) {
Expand Down
Loading

0 comments on commit a3a758f

Please sign in to comment.