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

chore: introduce sorted_map #1558

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_library(dfly_core compact_object.cc dragonfly_core.cc extent_tree.cc
external_alloc.cc interpreter.cc json_object.cc mi_memory_resource.cc sds_utils.cc
segment_allocator.cc simple_lru_counter.cc small_string.cc tx_queue.cc dense_set.cc
segment_allocator.cc simple_lru_counter.cc small_string.cc sorted_map.cc
tx_queue.cc dense_set.cc
string_set.cc string_map.cc detail/bitpacking.cc)

cxx_link(dfly_core base query_parser absl::flat_hash_map absl::str_format redis_lib TRDP::lua lua_modules
Expand All @@ -19,5 +20,6 @@ cxx_test(json_test dfly_core TRDP::jsoncons LABELS DFLY)
cxx_test(simple_lru_counter_test dfly_core LABELS DFLY)
cxx_test(string_set_test dfly_core LABELS DFLY)
cxx_test(string_map_test dfly_core LABELS DFLY)
cxx_test(sorted_map_test dfly_core LABELS DFLY)

add_subdirectory(search)
46 changes: 12 additions & 34 deletions src/core/compact_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
#include "base/logging.h"
#include "base/pod_array.h"
#include "core/detail/bitpacking.h"
#include "core/sorted_map.h"
#include "core/string_map.h"
#include "core/string_set.h"

Expand All @@ -46,14 +47,6 @@ size_t QlMAllocSize(quicklist* ql) {
return res + ql->count * 16; // we account for each member 16 bytes.
}

// Approximated dictionary size.
size_t DictMallocSize(dict* d) {
size_t res = zmalloc_usable_size(d->ht_table[0]) + zmalloc_usable_size(d->ht_table[1]) +
znallocx(sizeof(dict));

return res + dictSize(d) * 16; // approximation.
}

inline void FreeObjSet(unsigned encoding, void* ptr, MemoryResource* mr) {
switch (encoding) {
case kEncodingStrMap: {
Expand Down Expand Up @@ -107,8 +100,8 @@ size_t MallocUsedZSet(unsigned encoding, void* ptr) {
case OBJ_ENCODING_LISTPACK:
return lpBytes(reinterpret_cast<uint8_t*>(ptr));
case OBJ_ENCODING_SKIPLIST: {
zset* zs = (zset*)ptr;
return DictMallocSize(zs->dict);
detail::SortedMap* ss = (detail::SortedMap*)ptr;
return ss->MallocSize(); // DictMallocSize(zs->dict);
}
}
LOG(DFATAL) << "Unknown set encoding type " << encoding;
Expand All @@ -134,11 +127,10 @@ inline void FreeObjHash(unsigned encoding, void* ptr) {
}

inline void FreeObjZset(unsigned encoding, void* ptr) {
zset* zs = (zset*)ptr;
detail::SortedMap* zs = (detail::SortedMap*)ptr;
switch (encoding) {
case OBJ_ENCODING_SKIPLIST:
zs = (zset*)ptr;
zsetFree(zs);
delete zs;
break;
case OBJ_ENCODING_LISTPACK:
zfree(ptr);
Expand Down Expand Up @@ -238,8 +230,8 @@ size_t RobjWrapper::Size() const {
case OBJ_ZSET: {
switch (encoding_) {
case OBJ_ENCODING_SKIPLIST: {
zset* zs = (zset*)inner_obj_;
return zs->zsl->length;
SortedMap* ss = (SortedMap*)inner_obj_;
return ss->Size();
}
case OBJ_ENCODING_LISTPACK:
return lpLength((uint8_t*)inner_obj_) / 2;
Expand Down Expand Up @@ -427,13 +419,9 @@ int RobjWrapper::ZsetAdd(double score, sds ele, int in_flags, int* out_flags, do
* becomes too long *before* executing zzlInsert. */
if (zl_len + 1 > server.zset_max_listpack_entries ||
sdslen(ele) > server.zset_max_listpack_value || !lpSafeToAdd(lp, sdslen(ele))) {
robj self{.type = type_,
.encoding = encoding_,
.lru = 0,
.refcount = OBJ_STATIC_REFCOUNT,
.ptr = inner_obj_};
zsetConvert(&self, OBJ_ENCODING_SKIPLIST);
inner_obj_ = self.ptr;
unique_ptr<SortedMap> ss = SortedMap::FromListPack(lp);
lpFree(lp);
inner_obj_ = ss.release();
encoding_ = OBJ_ENCODING_SKIPLIST;
} else {
inner_obj_ = zzlInsert(lp, ele, score);
Expand All @@ -449,18 +437,8 @@ int RobjWrapper::ZsetAdd(double score, sds ele, int in_flags, int* out_flags, do
}

CHECK_EQ(encoding_, OBJ_ENCODING_SKIPLIST);

// TODO: to factor out OBJ_ENCODING_SKIPLIST functionality into a separate class.
robj self{.type = type_,
.encoding = encoding_,
.lru = 0,
.refcount = OBJ_STATIC_REFCOUNT,
.ptr = inner_obj_};

int res = zsetAdd(&self, score, ele, in_flags, out_flags, newscore);
inner_obj_ = self.ptr;
encoding_ = self.encoding;
return res;
SortedMap* ss = (SortedMap*)inner_obj_;
return ss->Add(score, ele, in_flags, out_flags, newscore);
}

bool RobjWrapper::Reallocate(MemoryResource* mr) {
Expand Down
6 changes: 4 additions & 2 deletions src/core/compact_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class RobjWrapper {
void SetString(std::string_view s, MemoryResource* mr);
void Init(unsigned type, unsigned encoding, void* inner);

// Equivalent to zsetAdd
int AddZsetMember(std::string_view member, double score, int in_flags, int* out_flags,
double* newscore);

unsigned type() const {
return type_;
}
Expand Down Expand Up @@ -440,5 +444,3 @@ class CompactObjectView {
};

} // namespace dfly

#undef PMR_NS
Loading