jihuayu commented on code in PR #1489:
URL:
https://github.com/apache/incubator-kvrocks/pull/1489#discussion_r1233985519
##########
src/server/server.cc:
##########
@@ -1753,3 +1755,50 @@ std::list<std::pair<std::string, uint32_t>>
Server::GetSlaveHostAndPort() {
slave_threads_mu_.unlock();
return result;
}
+
+// The numeric cursor consists of a 32-bit hash, a 16-bit time stamp, and a
16-bit counter, with the highest bit set to
+// 1 to prevent a zero cursor from occurring. The hash is used to prevent
information leakage. The time_stamp is used to
+// prevent the generation of the same cursor in the extremely short period
before and after a restart.
+static uint64_t GetNumberCursor(const std::string &key_name, uint16_t counter)
{
+ auto hash = static_cast<uint32_t>(std::hash<std::string>{}(key_name));
+ auto time_stamp = static_cast<uint16_t>(util::GetTimeStamp());
+ // set first bit 1, so number cursor not be 0
+ return static_cast<uint64_t>(hash) | static_cast<uint64_t>(time_stamp) << 32
| static_cast<uint64_t>(counter) << 48 |
+ static_cast<uint64_t>(1) << 63;
+}
+
+std::string Server::GenerateCursorFromKeyName(const std::string &key_name,
const char *prefix) {
+ if (!config_->redis_cursor_compatible) {
+ // add prefix for SCAN
+ return prefix + key_name;
+ }
+ auto counter = cursor_counter_.fetch_add(1);
+ auto number_cursor = GetNumberCursor(key_name, counter);
+ auto index = counter % CURSOR_DICT_SIZE;
+ cursor_dict_[index] = {number_cursor, key_name};
+ return std::to_string(number_cursor);
+}
+
+std::string Server::GetKeyNameFromCursor(const std::string &cursor) {
+ // When cursor is 0, cursor string is empty
+ if (cursor.empty() || !config_->redis_cursor_compatible) {
+ return cursor;
+ }
+
+ auto s = ParseInt<uint64_t>(cursor, 10);
+ // When Cursor 0 or not a Integer return empty string.
+ // Although the parameter 'cursor' is not expected to be 0, we still added a
check for 0 to increase the robustness of
+ // the code.
+ if (!s.IsOK() || *s == 0) {
+ return {};
+ }
+ auto cursor_num = *s;
+ // Because the index information is fully stored in the cursor, we can
directly obtain the index from the cursor.
+ auto index = (cursor_num >> 48) % CURSOR_DICT_SIZE;
Review Comment:
CURSOR_DICT_SIZE must less than 2**15, I add `static_assert` in .h file.
https://github.com/apache/incubator-kvrocks/pull/1489/files#diff-a57adfc622093e1a4bedd3c0d7727e6c42b126a015fd5f2c166aed385c2557b3R94
--
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]