mapleFU commented on code in PR #2277:
URL: https://github.com/apache/kvrocks/pull/2277#discussion_r1583090680
##########
src/storage/rdb.cc:
##########
@@ -1005,3 +1000,36 @@ Status RDB::rdbSaveBinaryDoubleValue(double val) {
memrev64ifbe(&val);
return stream_->Write((const char *)(&val), sizeof(val));
}
+
+Status RDB::rdbSaveZipListObject(const std::string &elem) {
+ // calc total ziplist size
+ uint prevlen = 0;
+ const size_t ziplist_size = zlHeaderSize + zlEndSize + elem.length() +
+ ZipList::ZipStorePrevEntryLength(nullptr, 0,
prevlen) +
+ ZipList::ZipStoreEntryEncoding(nullptr, 0,
elem.length());
+ auto zl_string = std::string(ziplist_size, '\0');
+ auto zl_ptr = reinterpret_cast<unsigned char *>(&zl_string[0]);
+
+ // set ziplist header
+ ZipList::SetZipListBytes(zl_ptr, ziplist_size,
(static_cast<uint32_t>(ziplist_size)));
+ ZipList::SetZipListTailOffset(zl_ptr, ziplist_size,
intrev32ifbe(zlHeaderSize));
+
+ // set ziplist entry
+ auto pos = ZipList::GetZipListEntryHead(zl_ptr, ziplist_size);
+ pos += ZipList::ZipStorePrevEntryLength(pos, ziplist_size, prevlen);
+ pos += ZipList::ZipStoreEntryEncoding(pos, ziplist_size, elem.length());
+ assert(pos + elem.length() <= zl_ptr + ziplist_size);
+ memcpy(pos, elem.c_str(), elem.length());
+
+ // set ziplist end
+ ZipList::SetZipListLength(zl_ptr, ziplist_size, 1);
+ zl_ptr[ziplist_size - 1] = zlEnd;
+
+ if (ziplist_size > 0) {
Review Comment:
I'm reading the logic here, may I ask that when would `ziplist_size == 0`?
Since if `ziplist_size == 0`, the code above would hit memory issue? If it
cannot be 0, we should remove this branch?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]