You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by ti...@apache.org on 2022/10/13 02:44:08 UTC
[incubator-kvrocks] branch unstable updated: Replace cpplint with clang-format (#979)
This is an automated email from the ASF dual-hosted git repository.
tison pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git
The following commit(s) were added to refs/heads/unstable by this push:
new 81d3792 Replace cpplint with clang-format (#979)
81d3792 is described below
commit 81d37927dfb2798138c8569af7c6a44dd376c3f4
Author: Twice <tw...@gmail.com>
AuthorDate: Thu Oct 13 10:44:02 2022 +0800
Replace cpplint with clang-format (#979)
---
.clang-format | 200 ++++++++
.github/workflows/kvrocks.yaml | 24 +-
src/batch_extractor.cc | 54 +-
src/batch_extractor.h | 14 +-
src/cluster.cc | 122 +++--
src/cluster.h | 35 +-
src/compact_filter.cc | 49 +-
src/compact_filter.h | 42 +-
src/compaction_checker.cc | 24 +-
src/compaction_checker.h | 4 +-
src/config.cc | 648 ++++++++++++------------
src/config.h | 6 +-
src/config_type.h | 58 +--
src/config_util.cc | 22 +-
src/config_util.h | 2 +-
src/cron.cc | 42 +-
src/cron.h | 12 +-
src/db_util.h | 7 +-
src/encoding.cc | 96 ++--
src/encoding.h | 6 +-
src/event_listener.cc | 54 +-
src/event_listener.h | 9 +-
src/event_util.h | 14 +-
src/fd_util.h | 21 +-
src/geohash.cc | 106 ++--
src/geohash.h | 36 +-
src/lock_manager.cc | 18 +-
src/lock_manager.h | 16 +-
src/log_collector.cc | 8 +-
src/log_collector.h | 10 +-
src/main.cc | 67 +--
src/parse_util.h | 24 +-
src/rand.cc | 55 +-
src/redis_bitmap.cc | 243 +++++----
src/redis_bitmap.h | 7 +-
src/redis_bitmap_string.cc | 38 +-
src/redis_bitmap_string.h | 11 +-
src/redis_cmd.cc | 528 +++++++++----------
src/redis_cmd.h | 30 +-
src/redis_connection.cc | 81 ++-
src/redis_connection.h | 8 +-
src/redis_db.cc | 59 +--
src/redis_db.h | 46 +-
src/redis_disk.cc | 62 +--
src/redis_disk.h | 9 +-
src/redis_geo.cc | 108 ++--
src/redis_geo.h | 71 +--
src/redis_hash.cc | 23 +-
src/redis_hash.h | 24 +-
src/redis_list.cc | 44 +-
src/redis_list.h | 5 +-
src/redis_metadata.cc | 65 ++-
src/redis_metadata.h | 22 +-
src/redis_pubsub.h | 4 +-
src/redis_reply.cc | 30 +-
src/redis_reply.h | 6 +-
src/redis_request.cc | 6 +-
src/redis_set.cc | 17 +-
src/redis_set.h | 10 +-
src/redis_slot.cc | 70 +--
src/redis_slot.h | 2 +-
src/redis_sortedint.cc | 23 +-
src/redis_sortedint.h | 13 +-
src/redis_stream.cc | 31 +-
src/redis_stream.h | 28 +-
src/redis_stream_base.cc | 22 +-
src/redis_stream_base.h | 28 +-
src/redis_string.cc | 93 ++--
src/redis_string.h | 6 +-
src/redis_zset.cc | 116 ++---
src/redis_zset.h | 54 +-
src/replication.cc | 195 +++-----
src/replication.h | 47 +-
src/rocksdb_crc32c.h | 5 +-
src/rw_lock.h | 32 +-
src/scope_exit.h | 8 +-
src/scripting.cc | 974 ++++++++++++++++++------------------
src/scripting.h | 10 +-
src/server.cc | 305 +++++------
src/server.h | 56 +--
src/sha1.cc | 164 ++++--
src/slot_import.cc | 29 +-
src/slot_import.h | 2 +-
src/slot_migrate.cc | 96 ++--
src/slot_migrate.h | 69 ++-
src/stats.cc | 13 +-
src/stats.h | 7 +-
src/status.h | 82 ++-
src/storage.cc | 156 +++---
src/storage.h | 48 +-
src/table_properties_collector.cc | 40 +-
src/table_properties_collector.h | 30 +-
src/task_runner.cc | 3 +-
src/task_runner.h | 11 +-
src/tls_util.cc | 16 +-
src/tls_util.h | 22 +-
src/util.cc | 141 +++---
src/util.h | 8 +-
src/worker.cc | 75 ++-
src/worker.h | 28 +-
tests/cppunit/cluster_test.cc | 66 +--
tests/cppunit/compact_test.cc | 14 +-
tests/cppunit/config_test.cc | 118 ++---
tests/cppunit/cron_test.cc | 5 +-
tests/cppunit/disk_test.cc | 89 ++--
tests/cppunit/log_collector_test.cc | 4 +-
tests/cppunit/main.cc | 4 +-
tests/cppunit/parse_util.cc | 3 +-
tests/cppunit/rwlock_test.cc | 42 +-
tests/cppunit/status_test.cc | 73 ++-
tests/cppunit/string_reply_test.cc | 33 +-
tests/cppunit/string_util_test.cc | 24 +-
tests/cppunit/t_bitmap_test.cc | 25 +-
tests/cppunit/t_encoding_test.cc | 4 +-
tests/cppunit/t_geo_test.cc | 28 +-
tests/cppunit/t_hash_test.cc | 32 +-
tests/cppunit/t_list_test.cc | 84 ++--
tests/cppunit/t_metadata_test.cc | 23 +-
tests/cppunit/t_set_test.cc | 26 +-
tests/cppunit/t_sortedint_test.cc | 14 +-
tests/cppunit/t_stream_test.cc | 26 +-
tests/cppunit/t_string_test.cc | 47 +-
tests/cppunit/t_zset_test.cc | 86 ++--
tests/cppunit/task_runner_test.cc | 12 +-
tests/cppunit/test_base.h | 7 +-
tools/kvrocks2redis/config.cc | 4 +-
tools/kvrocks2redis/config.h | 2 +-
tools/kvrocks2redis/main.cc | 23 +-
tools/kvrocks2redis/parser.cc | 14 +-
tools/kvrocks2redis/parser.h | 21 +-
tools/kvrocks2redis/redis_writer.cc | 17 +-
tools/kvrocks2redis/redis_writer.h | 8 +-
tools/kvrocks2redis/sync.cc | 24 +-
tools/kvrocks2redis/sync.h | 8 +-
tools/kvrocks2redis/writer.cc | 2 +
tools/kvrocks2redis/writer.h | 5 +-
x.py | 64 ++-
137 files changed, 3608 insertions(+), 4093 deletions(-)
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..2edf43e
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,200 @@
+---
+Language: Cpp
+# BasedOnStyle: Google
+AccessModifierOffset: -1
+AlignAfterOpenBracket: Align
+AlignConsecutiveMacros: None
+AlignConsecutiveAssignments: None
+AlignConsecutiveBitFields: None
+AlignConsecutiveDeclarations: None
+AlignEscapedNewlines: Left
+AlignOperands: Align
+AlignTrailingComments: true
+AllowAllArgumentsOnNextLine: true
+AllowAllConstructorInitializersOnNextLine: true
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowShortEnumsOnASingleLine: true
+AllowShortBlocksOnASingleLine: Never
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: All
+AllowShortLambdasOnASingleLine: All
+AllowShortIfStatementsOnASingleLine: WithoutElse
+AllowShortLoopsOnASingleLine: true
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: true
+AlwaysBreakTemplateDeclarations: Yes
+AttributeMacros:
+ - __capability
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+ AfterCaseLabel: false
+ AfterClass: false
+ AfterControlStatement: Never
+ AfterEnum: false
+ AfterFunction: false
+ AfterNamespace: false
+ AfterObjCDeclaration: false
+ AfterStruct: false
+ AfterUnion: false
+ AfterExternBlock: false
+ BeforeCatch: false
+ BeforeElse: false
+ BeforeLambdaBody: false
+ BeforeWhile: false
+ IndentBraces: false
+ SplitEmptyFunction: true
+ SplitEmptyRecord: true
+ SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeConceptDeclarations: true
+BreakBeforeBraces: Attach
+BreakBeforeInheritanceComma: false
+BreakInheritanceList: BeforeColon
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeColon
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: true
+ColumnLimit: 120
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: true
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+Cpp11BracedListStyle: true
+DeriveLineEnding: true
+DerivePointerAlignment: true
+DisableFormat: false
+EmptyLineBeforeAccessModifier: LogicalBlock
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: true
+ForEachMacros:
+ - foreach
+ - Q_FOREACH
+ - BOOST_FOREACH
+StatementAttributeLikeMacros:
+ - Q_EMIT
+IncludeBlocks: Regroup
+IncludeCategories:
+ - Regex: '^<ext/.*\.h>'
+ Priority: 2
+ SortPriority: 0
+ CaseSensitive: false
+ - Regex: '^<.*\.h>'
+ Priority: 1
+ SortPriority: 0
+ CaseSensitive: false
+ - Regex: '^<.*'
+ Priority: 2
+ SortPriority: 0
+ CaseSensitive: false
+ - Regex: '.*'
+ Priority: 3
+ SortPriority: 0
+ CaseSensitive: false
+IncludeIsMainRegex: '([-_](test|unittest))?$'
+IncludeIsMainSourceRegex: ''
+IndentCaseLabels: true
+IndentCaseBlocks: false
+IndentGotoLabels: true
+IndentPPDirectives: None
+IndentExternBlock: AfterExternBlock
+IndentRequires: false
+IndentWidth: 2
+IndentWrappedFunctionNames: false
+InsertTrailingCommas: None
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Never
+ObjCBlockIndentWidth: 2
+ObjCBreakBeforeNestedBlockParam: true
+ObjCSpaceAfterProperty: false
+ObjCSpaceBeforeProtocolList: true
+PenaltyBreakAssignment: 2
+PenaltyBreakBeforeFirstCallParameter: 1
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakString: 1000
+PenaltyBreakTemplateDeclaration: 10
+PenaltyExcessCharacter: 1000000
+PenaltyReturnTypeOnItsOwnLine: 200
+PenaltyIndentedWhitespace: 0
+PointerAlignment: Left
+RawStringFormats:
+ - Language: Cpp
+ Delimiters:
+ - cc
+ - CC
+ - cpp
+ - Cpp
+ - CPP
+ - 'c++'
+ - 'C++'
+ CanonicalDelimiter: ''
+ BasedOnStyle: google
+ - Language: TextProto
+ Delimiters:
+ - pb
+ - PB
+ - proto
+ - PROTO
+ EnclosingFunctions:
+ - EqualsProto
+ - EquivToProto
+ - PARSE_PARTIAL_TEXT_PROTO
+ - PARSE_TEST_PROTO
+ - PARSE_TEXT_PROTO
+ - ParseTextOrDie
+ - ParseTextProtoOrDie
+ - ParseTestProto
+ - ParsePartialTestProto
+ CanonicalDelimiter: ''
+ BasedOnStyle: google
+ReflowComments: true
+SortIncludes: true
+SortJavaStaticImport: Before
+SortUsingDeclarations: true
+SpaceAfterCStyleCast: false
+SpaceAfterLogicalNot: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCaseColon: false
+SpaceBeforeCpp11BracedList: false
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatements
+SpaceAroundPointerQualifiers: Default
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyBlock: false
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 2
+SpacesInAngles: false
+SpacesInConditionalStatement: false
+SpacesInContainerLiterals: true
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+SpaceBeforeSquareBrackets: false
+BitFieldColonSpacing: Both
+Standard: Auto
+StatementMacros:
+ - Q_UNUSED
+ - QT_REQUIRE_VERSION
+TabWidth: 8
+UseCRLF: false
+UseTab: Never
+WhitespaceSensitiveMacros:
+ - STRINGIZE
+ - PP_STRINGIZE
+ - BOOST_PP_STRINGIZE
+ - NS_SWIFT_NAME
+ - CF_SWIFT_NAME
+...
+
diff --git a/.github/workflows/kvrocks.yaml b/.github/workflows/kvrocks.yaml
index d2635cf..5088b5e 100644
--- a/.github/workflows/kvrocks.yaml
+++ b/.github/workflows/kvrocks.yaml
@@ -38,7 +38,7 @@ jobs:
lint:
name: Lint and check code
- runs-on: ubuntu-latest
+ runs-on: ubuntu-20.04
steps:
- name: Checkout Code Base
uses: actions/checkout@v3
@@ -46,19 +46,27 @@ jobs:
run: |
sudo apt update
sudo apt install -y cppcheck
- - name: Setup Python
- uses: actions/setup-python@v4
- with:
- python-version: 3.x
+ sudo apt install -y clang-format-12
- name: Setup Go
uses: actions/setup-go@v3
with:
go-version: 1.19.x
- - name: Setup dependencies
- run: pip install cpplint==1.6.1
+ - name: Check code format
+ id: check-format
+ run: ./x.py check format --clang-format-path clang-format-12
+ - name: Format code
+ if: always() && steps.check-format.outcome != 'success'
+ run: |
+ ./x.py format --clang-format-path clang-format-12
+ git diff -p > clang-format.patch
+ cat clang-format.patch
+ - name: Upload format patch
+ uses: actions/upload-artifact@v3
+ if: always() && steps.check-format.outcome != 'success'
+ with:
+ path: clang-format.patch
- name: Lint and check code
run: |
- ./x.py check cpplint
./x.py check cppcheck
./x.py check golangci-lint
diff --git a/src/batch_extractor.cc b/src/batch_extractor.cc
index 991efa2..a48768c 100644
--- a/src/batch_extractor.cc
+++ b/src/batch_extractor.cc
@@ -20,14 +20,14 @@
#include "batch_extractor.h"
-#include <rocksdb/write_batch.h>
#include <glog/logging.h>
+#include <rocksdb/write_batch.h>
-#include "server.h"
+#include "parse_util.h"
#include "redis_bitmap.h"
-#include "redis_slot.h"
#include "redis_reply.h"
-#include "parse_util.h"
+#include "redis_slot.h"
+#include "server.h"
void WriteBatchExtractor::LogData(const rocksdb::Slice &blob) {
// Currently, we only have two kinds of log data
@@ -42,8 +42,7 @@ void WriteBatchExtractor::LogData(const rocksdb::Slice &blob) {
}
}
-rocksdb::Status WriteBatchExtractor::PutCF(uint32_t column_family_id, const Slice &key,
- const Slice &value) {
+rocksdb::Status WriteBatchExtractor::PutCF(uint32_t column_family_id, const Slice &key, const Slice &value) {
if (column_family_id == kColumnFamilyIDZSetScore) {
return rocksdb::Status::OK();
}
@@ -71,7 +70,7 @@ rocksdb::Status WriteBatchExtractor::PutCF(uint32_t column_family_id, const Slic
if (!parse_result) {
return rocksdb::Status::InvalidArgument(parse_result.Msg());
}
- RedisCommand cmd = static_cast<RedisCommand >(*parse_result);
+ RedisCommand cmd = static_cast<RedisCommand>(*parse_result);
if (cmd == kRedisCmdExpire) {
command_args = {"EXPIREAT", user_key, std::to_string(metadata.expire)};
resp_commands_[ns].emplace_back(Redis::Command2RESP(command_args));
@@ -91,7 +90,8 @@ rocksdb::Status WriteBatchExtractor::PutCF(uint32_t column_family_id, const Slic
sub_key = ikey.GetSubKey().ToString();
ns = ikey.GetNamespace().ToString();
switch (log_data_.GetRedisType()) {
- case kRedisHash:command_args = {"HSET", user_key, sub_key, value.ToString()};
+ case kRedisHash:
+ command_args = {"HSET", user_key, sub_key, value.ToString()};
break;
case kRedisList: {
auto args = log_data_.GetArguments();
@@ -103,7 +103,7 @@ rocksdb::Status WriteBatchExtractor::PutCF(uint32_t column_family_id, const Slic
if (!parse_result) {
return rocksdb::Status::InvalidArgument(parse_result.Msg());
}
- RedisCommand cmd = static_cast<RedisCommand >(*parse_result);
+ RedisCommand cmd = static_cast<RedisCommand>(*parse_result);
switch (cmd) {
case kRedisCmdLSet:
if (args->size() < 2) {
@@ -116,7 +116,7 @@ rocksdb::Status WriteBatchExtractor::PutCF(uint32_t column_family_id, const Slic
if (first_seen_) {
if (args->size() < 4) {
LOG(ERROR)
- << "Fail to parse write_batch in putcf cmd linsert : args error, should contain before pivot value";
+ << "Fail to parse write_batch in putcf cmd linsert : args error, should contain before pivot value";
return rocksdb::Status::OK();
}
command_args = {"LINSERT", user_key, (*args)[1] == "1" ? "before" : "after", (*args)[2], (*args)[3]};
@@ -126,11 +126,13 @@ rocksdb::Status WriteBatchExtractor::PutCF(uint32_t column_family_id, const Slic
case kRedisCmdLRem:
// lrem will be parsed in deletecf, so ignore this putcf
break;
- default:command_args = {cmd == kRedisCmdLPush ? "LPUSH" : "RPUSH", user_key, value.ToString()};
+ default:
+ command_args = {cmd == kRedisCmdLPush ? "LPUSH" : "RPUSH", user_key, value.ToString()};
}
break;
}
- case kRedisSet:command_args = {"SADD", user_key, sub_key};
+ case kRedisSet:
+ command_args = {"SADD", user_key, sub_key};
break;
case kRedisZSet: {
double score = DecodeDouble(value.data());
@@ -147,7 +149,7 @@ rocksdb::Status WriteBatchExtractor::PutCF(uint32_t column_family_id, const Slic
if (!parse_result) {
return rocksdb::Status::InvalidArgument(parse_result.Msg());
}
- RedisCommand cmd = static_cast<RedisCommand >(*parse_result);
+ RedisCommand cmd = static_cast<RedisCommand>(*parse_result);
switch (cmd) {
case kRedisCmdSetBit: {
if (args->size() < 2) {
@@ -166,7 +168,7 @@ rocksdb::Status WriteBatchExtractor::PutCF(uint32_t column_family_id, const Slic
if (first_seen_) {
if (args->size() < 4) {
LOG(ERROR)
- << "Fail to parse write_batch in putcf cmd bitop : args error, should at least contain srckey";
+ << "Fail to parse write_batch in putcf cmd bitop : args error, should at least contain srckey";
return rocksdb::Status::OK();
}
command_args = {"BITOP", (*args)[1], user_key};
@@ -186,7 +188,8 @@ rocksdb::Status WriteBatchExtractor::PutCF(uint32_t column_family_id, const Slic
}
break;
}
- default: break;
+ default:
+ break;
}
}
@@ -218,11 +221,14 @@ rocksdb::Status WriteBatchExtractor::DeleteCF(uint32_t column_family_id, const S
sub_key = ikey.GetSubKey().ToString();
ns = ikey.GetNamespace().ToString();
switch (log_data_.GetRedisType()) {
- case kRedisHash: command_args = {"HDEL", user_key, sub_key};
+ case kRedisHash:
+ command_args = {"HDEL", user_key, sub_key};
break;
- case kRedisSet: command_args = {"SREM", user_key, sub_key};
+ case kRedisSet:
+ command_args = {"SREM", user_key, sub_key};
break;
- case kRedisZSet: command_args = {"ZREM", user_key, sub_key};
+ case kRedisZSet:
+ command_args = {"ZREM", user_key, sub_key};
break;
case kRedisList: {
auto args = log_data_.GetArguments();
@@ -234,7 +240,7 @@ rocksdb::Status WriteBatchExtractor::DeleteCF(uint32_t column_family_id, const S
if (!parse_result) {
return rocksdb::Status::InvalidArgument(parse_result.Msg());
}
- RedisCommand cmd = static_cast<RedisCommand >(*parse_result);
+ RedisCommand cmd = static_cast<RedisCommand>(*parse_result);
switch (cmd) {
case kRedisCmdLTrim:
if (first_seen_) {
@@ -256,7 +262,8 @@ rocksdb::Status WriteBatchExtractor::DeleteCF(uint32_t column_family_id, const S
first_seen_ = false;
}
break;
- default:command_args = {cmd == kRedisCmdLPop ? "LPOP" : "RPOP", user_key};
+ default:
+ command_args = {cmd == kRedisCmdLPop ? "LPOP" : "RPOP", user_key};
}
break;
}
@@ -266,7 +273,8 @@ rocksdb::Status WriteBatchExtractor::DeleteCF(uint32_t column_family_id, const S
}
break;
}
- default: break;
+ default:
+ break;
}
}
@@ -276,8 +284,8 @@ rocksdb::Status WriteBatchExtractor::DeleteCF(uint32_t column_family_id, const S
return rocksdb::Status::OK();
}
-rocksdb::Status WriteBatchExtractor::DeleteRangeCF(uint32_t column_family_id,
- const Slice& begin_key, const Slice& end_key) {
+rocksdb::Status WriteBatchExtractor::DeleteRangeCF(uint32_t column_family_id, const Slice &begin_key,
+ const Slice &end_key) {
// Do nothing about DeleteRange operations
return rocksdb::Status::OK();
}
diff --git a/src/batch_extractor.h b/src/batch_extractor.h
index ddc7128..a787e65 100644
--- a/src/batch_extractor.h
+++ b/src/batch_extractor.h
@@ -19,29 +19,27 @@
*/
#pragma once
+#include <map>
#include <string>
#include <vector>
-#include <map>
#include "redis_db.h"
+#include "redis_metadata.h"
#include "status.h"
#include "storage.h"
-#include "redis_metadata.h"
-
// An extractor to extract update from raw writebatch
class WriteBatchExtractor : public rocksdb::WriteBatch::Handler {
public:
explicit WriteBatchExtractor(bool is_slotid_encoded, int16_t slot = -1, bool to_redis = false)
- : is_slotid_encoded_(is_slotid_encoded), slot_(slot), to_redis_(to_redis) {}
+ : is_slotid_encoded_(is_slotid_encoded), slot_(slot), to_redis_(to_redis) {}
void LogData(const rocksdb::Slice &blob) override;
- rocksdb::Status PutCF(uint32_t column_family_id, const Slice &key,
- const Slice &value) override;
+ rocksdb::Status PutCF(uint32_t column_family_id, const Slice &key, const Slice &value) override;
rocksdb::Status DeleteCF(uint32_t column_family_id, const Slice &key) override;
- rocksdb::Status DeleteRangeCF(uint32_t column_family_id,
- const Slice& begin_key, const Slice& end_key) override;
+ rocksdb::Status DeleteRangeCF(uint32_t column_family_id, const Slice &begin_key, const Slice &end_key) override;
std::map<std::string, std::vector<std::string>> *GetRESPCommands() { return &resp_commands_; }
+
private:
std::map<std::string, std::vector<std::string>> resp_commands_;
Redis::WriteBatchLogData log_data_;
diff --git a/src/cluster.cc b/src/cluster.cc
index fa47f36..ffcaa1f 100644
--- a/src/cluster.cc
+++ b/src/cluster.cc
@@ -18,17 +18,18 @@
*
*/
+#include "cluster.h"
+
+#include <algorithm>
#include <cassert>
#include <cstring>
-#include <algorithm>
#include <memory>
-#include "util.h"
-#include "server.h"
-#include "cluster.h"
+#include "parse_util.h"
#include "redis_cmd.h"
#include "replication.h"
-#include "parse_util.h"
+#include "server.h"
+#include "util.h"
const char *errInvalidNodeID = "Invalid cluster node id";
const char *errInvalidSlotID = "Invalid slot id";
@@ -40,13 +41,12 @@ const char *errClusterNoInitialized = "CLUSTERDOWN The cluster is not initialize
const char *errInvalidClusterNodeInfo = "Invalid cluster nodes info";
const char *errInvalidImportState = "Invalid import state";
-ClusterNode::ClusterNode(std::string id, std::string host, int port,
- int role, std::string master_id, std::bitset<kClusterSlots> slots):
- id_(id), host_(host), port_(port), role_(role),
- master_id_(master_id), slots_(slots) { }
+ClusterNode::ClusterNode(std::string id, std::string host, int port, int role, std::string master_id,
+ std::bitset<kClusterSlots> slots)
+ : id_(id), host_(host), port_(port), role_(role), master_id_(master_id), slots_(slots) {}
-Cluster::Cluster(Server *svr, std::vector<std::string> binds, int port) :
- svr_(svr), binds_(binds), port_(port), size_(0), version_(-1), myself_(nullptr) {
+Cluster::Cluster(Server *svr, std::vector<std::string> binds, int port)
+ : svr_(svr), binds_(binds), port_(port), size_(0), version_(-1), myself_(nullptr) {
for (unsigned i = 0; i < kClusterSlots; i++) {
slots_nodes_[i] = nullptr;
}
@@ -62,7 +62,7 @@ bool Cluster::SubCommandIsExecExclusive(const std::string &subcommand) {
return true;
} else if (strcasecmp("setnodeid", subcommand.c_str()) == 0) {
return true;
- } else if (strcasecmp("setslot", subcommand.c_str()) == 0) {
+ } else if (strcasecmp("setslot", subcommand.c_str()) == 0) {
return true;
} else if (strcasecmp("import", subcommand.c_str()) == 0) {
return true;
@@ -108,7 +108,7 @@ Status Cluster::SetSlot(int slot, std::string node_id, int64_t new_version) {
if (!IsValidSlot(slot)) {
return Status(Status::NotOK, errInvalidSlotID);
}
- if (node_id.size() != kClusterNodeIdLen) {
+ if (node_id.size() != kClusterNodeIdLen) {
return Status(Status::NotOK, errInvalidNodeID);
}
@@ -195,9 +195,7 @@ Status Cluster::SetClusterNodes(const std::string &nodes_str, int64_t version, b
// Find myself
if (myid_.empty() || force) {
for (auto &n : nodes_) {
- if (n.second->port_ == port_ &&
- std::find(binds_.begin(), binds_.end(), n.second->host_)
- != binds_.end()) {
+ if (n.second->port_ == port_ && std::find(binds_.begin(), binds_.end(), n.second->host_) != binds_.end()) {
myid_ = n.first;
break;
}
@@ -240,19 +238,15 @@ void Cluster::SetMasterSlaveRepl() {
std::shared_ptr<ClusterNode> master = nodes_[myself_->master_id_];
Status s = svr_->AddMaster(master->host_, master->port_, 0);
if (s.IsOK()) {
- LOG(INFO) << "SLAVE OF " << master->host_ << ":" << master->port_
- << " enabled by cluster topology setting";
+ LOG(INFO) << "SLAVE OF " << master->host_ << ":" << master->port_ << " enabled by cluster topology setting";
} else {
LOG(WARNING) << "SLAVE OF " << master->host_ << ":" << master->port_
- << " enabled by cluster topology setting, encounter error: "
- << s.Msg();
+ << " enabled by cluster topology setting, encounter error: " << s.Msg();
}
}
}
-bool Cluster::IsNotMaster() {
- return myself_ == nullptr || myself_->role_ != kClusterMaster || svr_->IsSlave();
-}
+bool Cluster::IsNotMaster() { return myself_ == nullptr || myself_->role_ != kClusterMaster || svr_->IsSlave(); }
Status Cluster::SetSlotMigrated(int slot, const std::string &ip_port) {
if (!IsValidSlot(slot)) {
@@ -297,10 +291,8 @@ Status Cluster::MigrateSlot(int slot, const std::string &dst_node_id) {
}
const auto dst = nodes_[dst_node_id];
- Status s = svr_->slot_migrate_->MigrateStart(svr_, dst_node_id, dst->host_,
- dst->port_, slot,
- svr_->GetConfig()->migrate_speed,
- svr_->GetConfig()->pipeline_size,
+ Status s = svr_->slot_migrate_->MigrateStart(svr_, dst_node_id, dst->host_, dst->port_, slot,
+ svr_->GetConfig()->migrate_speed, svr_->GetConfig()->pipeline_size,
svr_->GetConfig()->sequence_gap);
return s;
}
@@ -330,8 +322,7 @@ Status Cluster::ImportSlot(Redis::Connection *conn, int slot, int state) {
case kImportSuccess:
if (!svr_->slot_import_->Success(slot)) {
LOG(ERROR) << "[import] Failed to set slot importing success, maybe slot is wrong"
- << ", received slot: " << slot
- << ", current slot: " << svr_->slot_import_->GetSlot();
+ << ", received slot: " << slot << ", current slot: " << svr_->slot_import_->GetSlot();
return Status(Status::NotOK, "Failed to set slot " + std::to_string(slot) + " importing success");
}
LOG(INFO) << "[import] Succeed to import slot " << slot;
@@ -339,8 +330,7 @@ Status Cluster::ImportSlot(Redis::Connection *conn, int slot, int state) {
case kImportFailed:
if (!svr_->slot_import_->Fail(slot)) {
LOG(ERROR) << "[import] Failed to set slot importing error, maybe slot is wrong"
- << ", received slot: " << slot
- << ", current slot: " << svr_->slot_import_->GetSlot();
+ << ", received slot: " << slot << ", current slot: " << svr_->slot_import_->GetSlot();
return Status(Status::NotOK, "Failed to set slot " + std::to_string(slot) + " importing error");
}
LOG(INFO) << "[import] Failed to import slot " << slot;
@@ -363,15 +353,26 @@ Status Cluster::GetClusterInfo(std::string *cluster_infos) {
}
*cluster_infos =
- "cluster_state:ok\r\n"
- "cluster_slots_assigned:" + std::to_string(ok_slot) + "\r\n"
- "cluster_slots_ok:" + std::to_string(ok_slot) + "\r\n"
- "cluster_slots_pfail:0\r\n"
- "cluster_slots_fail:0\r\n"
- "cluster_known_nodes:" + std::to_string(nodes_.size()) +"\r\n"
- "cluster_size:" + std::to_string(size_) + "\r\n"
- "cluster_current_epoch:" + std::to_string(version_) + "\r\n"
- "cluster_my_epoch:" + std::to_string(version_) + "\r\n";
+ "cluster_state:ok\r\n"
+ "cluster_slots_assigned:" +
+ std::to_string(ok_slot) +
+ "\r\n"
+ "cluster_slots_ok:" +
+ std::to_string(ok_slot) +
+ "\r\n"
+ "cluster_slots_pfail:0\r\n"
+ "cluster_slots_fail:0\r\n"
+ "cluster_known_nodes:" +
+ std::to_string(nodes_.size()) +
+ "\r\n"
+ "cluster_size:" +
+ std::to_string(size_) +
+ "\r\n"
+ "cluster_current_epoch:" +
+ std::to_string(version_) +
+ "\r\n"
+ "cluster_my_epoch:" +
+ std::to_string(version_) + "\r\n";
if (myself_ != nullptr && myself_->role_ == kClusterMaster && !svr_->IsSlave()) {
// Get migrating status
@@ -415,7 +416,7 @@ Status Cluster::GetSlotsInfo(std::vector<SlotInfo> *slots_infos) {
}
// Generate slots info when occur different node with start or end of slot
if (i == kClusterSlots || n != slots_nodes_[i]) {
- slots_infos->emplace_back(GenSlotNodeInfo(start, i-1, n));
+ slots_infos->emplace_back(GenSlotNodeInfo(start, i - 1, n));
if (i == kClusterSlots) break;
n = slots_nodes_[i];
start = i;
@@ -428,7 +429,7 @@ SlotInfo Cluster::GenSlotNodeInfo(int start, int end, std::shared_ptr<ClusterNod
std::vector<SlotInfo::NodeInfo> vn;
vn.push_back({n->host_, n->port_, n->id_}); // itself
- for (const auto &id : n->replicas) { // replicas
+ for (const auto &id : n->replicas) { // replicas
if (nodes_.find(id) == nodes_.end()) continue;
vn.push_back({nodes_[id]->host_, nodes_[id]->port_, nodes_[id]->id_});
}
@@ -460,10 +461,10 @@ std::string Cluster::GenNodesDescription() {
}
// Generate slots info when occur different node with start or end of slot
if (i == kClusterSlots || n != slots_nodes_[i]) {
- if (start == i-1) {
+ if (start == i - 1) {
n->slots_info_ += std::to_string(start) + " ";
} else {
- n->slots_info_ += std::to_string(start)+ "-" + std::to_string(i-1) + " ";
+ n->slots_info_ += std::to_string(start) + "-" + std::to_string(i - 1) + " ";
}
if (i == kClusterSlots) break;
n = slots_nodes_[i];
@@ -478,8 +479,8 @@ std::string Cluster::GenNodesDescription() {
std::string node_str;
// ID, host, port
node_str.append(n->id_ + " ");
- node_str.append(n->host_ + ":" + std::to_string(n->port_) +
- "@" + std::to_string(n->port_+kClusterPortIncr) + " ");
+ node_str.append(n->host_ + ":" + std::to_string(n->port_) + "@" + std::to_string(n->port_ + kClusterPortIncr) +
+ " ");
// Flags
if (n->id_ == myid_) node_str.append("myself,");
@@ -490,9 +491,8 @@ std::string Cluster::GenNodesDescription() {
}
// Ping sent, pong received, config epoch, link status
- node_str.append(std::to_string(std::time(nullptr)*1000-1) + " " +
- std::to_string(std::time(nullptr)*1000) + " " +
- std::to_string(version_) + " " + "connected");
+ node_str.append(std::to_string(std::time(nullptr) * 1000 - 1) + " " + std::to_string(std::time(nullptr) * 1000) +
+ " " + std::to_string(version_) + " " + "connected");
// Slots
if (n->slots_info_.size() > 0) n->slots_info_.pop_back(); // Trim space
@@ -541,8 +541,7 @@ Status Cluster::ParseClusterNodes(const std::string &nodes_str, ClusterNodes *no
int role;
if (strcasecmp(fields[3].c_str(), "master") == 0) {
role = kClusterMaster;
- } else if (strcasecmp(fields[3].c_str(), "slave") == 0 ||
- strcasecmp(fields[3].c_str(), "replica") == 0) {
+ } else if (strcasecmp(fields[3].c_str(), "slave") == 0 || strcasecmp(fields[3].c_str(), "replica") == 0) {
role = kClusterSlave;
} else {
return Status(Status::ClusterInvalidInfo, "Invalid cluster node role");
@@ -561,8 +560,7 @@ Status Cluster::ParseClusterNodes(const std::string &nodes_str, ClusterNodes *no
return Status(Status::ClusterInvalidInfo, errInvalidClusterNodeInfo);
} else {
// Create slave node
- (*nodes)[id] = Util::MakeShared<ClusterNode>(
- id, host, port, role, master_id, slots);
+ (*nodes)[id] = Util::MakeShared<ClusterNode>(id, host, port, role, master_id, slots);
continue;
}
}
@@ -610,8 +608,7 @@ Status Cluster::ParseClusterNodes(const std::string &nodes_str, ClusterNodes *no
}
// Create master node
- (*nodes)[id] = Util::MakeShared<ClusterNode>(
- id, host, port, role, master_id, slots);
+ (*nodes)[id] = Util::MakeShared<ClusterNode>(id, host, port, role, master_id, slots);
}
return Status::OK();
}
@@ -623,8 +620,7 @@ bool Cluster::IsWriteForbiddenSlot(int slot) {
return false;
}
-Status Cluster::CanExecByMySelf(const Redis::CommandAttributes *attributes,
- const std::vector<std::string> &cmd_tokens,
+Status Cluster::CanExecByMySelf(const Redis::CommandAttributes *attributes, const std::vector<std::string> &cmd_tokens,
Redis::Connection *conn) {
std::vector<int> keys_indexes;
auto s = Redis::GetKeysFromCommand(attributes->name, cmd_tokens.size(), &keys_indexes);
@@ -638,8 +634,7 @@ Status Cluster::CanExecByMySelf(const Redis::CommandAttributes *attributes,
int cur_slot = GetSlotNumFromKey(cmd_tokens[i]);
if (slot == -1) slot = cur_slot;
if (slot != cur_slot) {
- return Status(Status::RedisExecErr,
- "CROSSSLOT Attempted to access keys that don't hash to the same slot");
+ return Status(Status::RedisExecErr, "CROSSSLOT Attempted to access keys that don't hash to the same slot");
}
}
if (slot == -1) return Status::OK();
@@ -670,14 +665,11 @@ Status Cluster::CanExecByMySelf(const Redis::CommandAttributes *attributes,
// the destination server. Before the central controller change the topology, the destination
// server should record the imported slots to accept new data of the imported slots.
return Status::OK(); // I'm serving the imported slot
- } else if (myself_ && myself_->role_ == kClusterSlave
- && attributes->is_write() == false
- && nodes_.find(myself_->master_id_) != nodes_.end()
- && nodes_[myself_->master_id_] == slots_nodes_[slot]) {
+ } else if (myself_ && myself_->role_ == kClusterSlave && attributes->is_write() == false &&
+ nodes_.find(myself_->master_id_) != nodes_.end() && nodes_[myself_->master_id_] == slots_nodes_[slot]) {
return Status::OK(); // My mater is serving this slot
} else {
- std::string ip_port = slots_nodes_[slot]->host_ + ":" +
- std::to_string(slots_nodes_[slot]->port_);
+ std::string ip_port = slots_nodes_[slot]->host_ + ":" + std::to_string(slots_nodes_[slot]->port_);
return Status(Status::RedisExecErr, "MOVED " + std::to_string(slot) + " " + ip_port);
}
}
diff --git a/src/cluster.h b/src/cluster.h
index e1e30f8..1c45b41 100644
--- a/src/cluster.h
+++ b/src/cluster.h
@@ -20,33 +20,33 @@
#pragma once
-#include <string>
-#include <vector>
+#include <algorithm>
#include <bitset>
+#include <map>
#include <memory>
-#include <algorithm>
-#include <unordered_map>
#include <set>
-#include <map>
+#include <string>
+#include <unordered_map>
+#include <vector>
-#include "status.h"
-#include "rw_lock.h"
#include "redis_cmd.h"
-#include "redis_slot.h"
#include "redis_connection.h"
+#include "redis_slot.h"
+#include "rw_lock.h"
+#include "status.h"
enum {
- kClusterMaster = 1,
- kClusterSlave = 2,
+ kClusterMaster = 1,
+ kClusterSlave = 2,
kClusterNodeIdLen = 40,
- kClusterPortIncr = 10000,
- kClusterSlots = HASH_SLOTS_SIZE,
+ kClusterPortIncr = 10000,
+ kClusterSlots = HASH_SLOTS_SIZE,
};
class ClusterNode {
public:
- explicit ClusterNode(std::string id, std::string host, int port,
- int role, std::string master_id, std::bitset<kClusterSlots> slots);
+ explicit ClusterNode(std::string id, std::string host, int port, int role, std::string master_id,
+ std::bitset<kClusterSlots> slots);
std::string id_;
std::string host_;
int port_;
@@ -88,8 +88,7 @@ class Cluster {
static bool IsValidSlot(int slot) { return slot >= 0 && slot < kClusterSlots; }
bool IsNotMaster();
bool IsWriteForbiddenSlot(int slot);
- Status CanExecByMySelf(const Redis::CommandAttributes *attributes,
- const std::vector<std::string> &cmd_tokens,
+ Status CanExecByMySelf(const Redis::CommandAttributes *attributes, const std::vector<std::string> &cmd_tokens,
Redis::Connection *conn);
void SetMasterSlaveRepl();
Status MigrateSlot(int slot, const std::string &dst_node_id);
@@ -102,13 +101,13 @@ class Cluster {
std::string GenNodesDescription();
SlotInfo GenSlotNodeInfo(int start, int end, std::shared_ptr<ClusterNode> n);
Status ParseClusterNodes(const std::string &nodes_str, ClusterNodes *nodes,
- std::unordered_map<int, std::string> *slots_nodes);
+ std::unordered_map<int, std::string> *slots_nodes);
Server *svr_;
std::vector<std::string> binds_;
int port_;
int size_;
int64_t version_;
- std::string myid_;
+ std::string myid_;
std::shared_ptr<ClusterNode> myself_;
ClusterNodes nodes_;
std::shared_ptr<ClusterNode> slots_nodes_[kClusterSlots];
diff --git a/src/compact_filter.cc b/src/compact_filter.cc
index 0786a81..67bdafb 100644
--- a/src/compact_filter.cc
+++ b/src/compact_filter.cc
@@ -30,36 +30,30 @@
namespace Engine {
using rocksdb::Slice;
-bool MetadataFilter::Filter(int level,
- const Slice &key,
- const Slice &value,
- std::string *new_value,
- bool *modified) const {
+bool MetadataFilter::Filter(int level, const Slice &key, const Slice &value, std::string *new_value,
+ bool *modified) const {
std::string ns, user_key, bytes = value.ToString();
Metadata metadata(kRedisNone, false);
rocksdb::Status s = metadata.Decode(bytes);
ExtractNamespaceKey(key, &ns, &user_key, stor_->IsSlotIdEncoded());
if (!s.ok()) {
LOG(WARNING) << "[compact_filter/metadata] Failed to decode,"
- << ", namespace: " << ns
- << ", key: " << user_key
- << ", err: " << s.ToString();
+ << ", namespace: " << ns << ", key: " << user_key << ", err: " << s.ToString();
return false;
}
DLOG(INFO) << "[compact_filter/metadata] "
- << "namespace: " << ns
- << ", key: " << user_key
+ << "namespace: " << ns << ", key: " << user_key
<< ", result: " << (metadata.Expired() ? "deleted" : "reserved");
return metadata.Expired();
}
-Status SubKeyFilter::GetMetadata(const InternalKey &ikey, Metadata* metadata) const {
+Status SubKeyFilter::GetMetadata(const InternalKey &ikey, Metadata *metadata) const {
std::string metadata_key;
auto db = stor_->GetDB();
const auto cf_handles = stor_->GetCFHandles();
// storage close the would delete the column family handler and DB
- if (!db || cf_handles->size() < 2) return Status(Status::NotOK, "storage is closed");
+ if (!db || cf_handles->size() < 2) return Status(Status::NotOK, "storage is closed");
ComposeNamespaceKey(ikey.GetNamespace(), ikey.GetKey(), &metadata_key, stor_->IsSlotIdEncoded());
if (cached_key_.empty() || metadata_key != cached_key_) {
@@ -85,22 +79,21 @@ Status SubKeyFilter::GetMetadata(const InternalKey &ikey, Metadata* metadata) co
rocksdb::Status s = metadata->Decode(cached_metadata_);
if (!s.ok()) {
cached_key_.clear();
- return Status(Status::NotOK, "decode error: " + s.ToString());;
+ return Status(Status::NotOK, "decode error: " + s.ToString());
+ ;
}
return Status::OK();
}
-bool SubKeyFilter::IsMetadataExpired(const InternalKey &ikey, const Metadata& metadata) const {
+bool SubKeyFilter::IsMetadataExpired(const InternalKey &ikey, const Metadata &metadata) const {
if (metadata.Type() == kRedisString // metadata key was overwrite by set command
- || metadata.Expired()
- || ikey.GetVersion() != metadata.version) {
+ || metadata.Expired() || ikey.GetVersion() != metadata.version) {
return true;
}
return false;
}
-rocksdb::CompactionFilter::Decision SubKeyFilter::FilterBlobByKey(int level, const Slice &key,
- std::string *new_value,
+rocksdb::CompactionFilter::Decision SubKeyFilter::FilterBlobByKey(int level, const Slice &key, std::string *new_value,
std::string *skip_until) const {
InternalKey ikey(key, stor_->IsSlotIdEncoded());
Metadata metadata(kRedisNone, false);
@@ -110,9 +103,8 @@ rocksdb::CompactionFilter::Decision SubKeyFilter::FilterBlobByKey(int level, con
}
if (!s.IsOK()) {
LOG(ERROR) << "[compact_filter/subkey] Failed to get metadata"
- << ", namespace: " << ikey.GetNamespace().ToString()
- << ", key: " << ikey.GetKey().ToString()
- << ", err: " << s.Msg();
+ << ", namespace: " << ikey.GetNamespace().ToString() << ", key: " << ikey.GetKey().ToString()
+ << ", err: " << s.Msg();
return rocksdb::CompactionFilter::Decision::kKeep;
}
// bitmap will be checked in Filter
@@ -124,11 +116,8 @@ rocksdb::CompactionFilter::Decision SubKeyFilter::FilterBlobByKey(int level, con
return result ? rocksdb::CompactionFilter::Decision::kRemove : rocksdb::CompactionFilter::Decision::kKeep;
}
-bool SubKeyFilter::Filter(int level,
- const Slice &key,
- const Slice &value,
- std::string *new_value,
- bool *modified) const {
+bool SubKeyFilter::Filter(int level, const Slice &key, const Slice &value, std::string *new_value,
+ bool *modified) const {
InternalKey ikey(key, stor_->IsSlotIdEncoded());
Metadata metadata(kRedisNone, false);
Status s = GetMetadata(ikey, &metadata);
@@ -137,14 +126,12 @@ bool SubKeyFilter::Filter(int level,
}
if (!s.IsOK()) {
LOG(ERROR) << "[compact_filter/subkey] Failed to get metadata"
- << ", namespace: " << ikey.GetNamespace().ToString()
- << ", key: " << ikey.GetKey().ToString()
- << ", err: " << s.Msg();
+ << ", namespace: " << ikey.GetNamespace().ToString() << ", key: " << ikey.GetKey().ToString()
+ << ", err: " << s.Msg();
return false;
}
- return IsMetadataExpired(ikey, metadata) ||
- (metadata.Type() == kRedisBitmap && Redis::Bitmap::IsEmptySegment(value));
+ return IsMetadataExpired(ikey, metadata) || (metadata.Type() == kRedisBitmap && Redis::Bitmap::IsEmptySegment(value));
}
} // namespace Engine
diff --git a/src/compact_filter.h b/src/compact_filter.h
index 478ed76..2a0a488 100644
--- a/src/compact_filter.h
+++ b/src/compact_filter.h
@@ -33,19 +33,17 @@
namespace Engine {
class MetadataFilter : public rocksdb::CompactionFilter {
public:
- explicit MetadataFilter(Storage *storage): stor_(storage) {}
+ explicit MetadataFilter(Storage *storage) : stor_(storage) {}
const char *Name() const override { return "MetadataFilter"; }
- bool Filter(int level, const Slice &key, const Slice &value,
- std::string *new_value, bool *modified) const override;
+ bool Filter(int level, const Slice &key, const Slice &value, std::string *new_value, bool *modified) const override;
+
private:
Engine::Storage *stor_;
};
class MetadataFilterFactory : public rocksdb::CompactionFilterFactory {
public:
- explicit MetadataFilterFactory(Engine::Storage *storage) {
- stor_ = storage;
- }
+ explicit MetadataFilterFactory(Engine::Storage *storage) { stor_ = storage; }
const char *Name() const override { return "MetadataFilterFactory"; }
std::unique_ptr<rocksdb::CompactionFilter> CreateCompactionFilter(
const rocksdb::CompactionFilter::Context &context) override {
@@ -58,19 +56,14 @@ class MetadataFilterFactory : public rocksdb::CompactionFilterFactory {
class SubKeyFilter : public rocksdb::CompactionFilter {
public:
- explicit SubKeyFilter(Storage *storage)
- : cached_key_(""),
- cached_metadata_(""),
- stor_(storage) {}
+ explicit SubKeyFilter(Storage *storage) : cached_key_(""), cached_metadata_(""), stor_(storage) {}
const char *Name() const override { return "SubkeyFilter"; }
- Status GetMetadata(const InternalKey &ikey, Metadata* metadata) const;
- bool IsMetadataExpired(const InternalKey &ikey, const Metadata& metadata) const;
- rocksdb::CompactionFilter::Decision FilterBlobByKey(int level, const Slice &key,
- std::string *new_value,
- std::string *skip_until) const override;
- bool Filter(int level, const Slice &key, const Slice &value,
- std::string *new_value, bool *modified) const override;
+ Status GetMetadata(const InternalKey &ikey, Metadata *metadata) const;
+ bool IsMetadataExpired(const InternalKey &ikey, const Metadata &metadata) const;
+ rocksdb::CompactionFilter::Decision FilterBlobByKey(int level, const Slice &key, std::string *new_value,
+ std::string *skip_until) const override;
+ bool Filter(int level, const Slice &key, const Slice &value, std::string *new_value, bool *modified) const override;
protected:
mutable std::string cached_key_;
@@ -80,15 +73,12 @@ class SubKeyFilter : public rocksdb::CompactionFilter {
class SubKeyFilterFactory : public rocksdb::CompactionFilterFactory {
public:
- explicit SubKeyFilterFactory(Engine::Storage *storage) {
- stor_ = storage;
- }
+ explicit SubKeyFilterFactory(Engine::Storage *storage) { stor_ = storage; }
const char *Name() const override { return "SubKeyFilterFactory"; }
std::unique_ptr<rocksdb::CompactionFilter> CreateCompactionFilter(
const rocksdb::CompactionFilter::Context &context) override {
- return std::unique_ptr<rocksdb::CompactionFilter>(
- new SubKeyFilter(stor_));
+ return std::unique_ptr<rocksdb::CompactionFilter>(new SubKeyFilter(stor_));
}
private:
@@ -98,8 +88,7 @@ class SubKeyFilterFactory : public rocksdb::CompactionFilterFactory {
class PropagateFilter : public rocksdb::CompactionFilter {
public:
const char *Name() const override { return "PropagateFilter"; }
- bool Filter(int level, const Slice &key, const Slice &value,
- std::string *new_value, bool *modified) const override {
+ bool Filter(int level, const Slice &key, const Slice &value, std::string *new_value, bool *modified) const override {
// We propagate Lua commands which don't store data,
// just in order to implement updating Lua state.
return key == Engine::kPropagateScriptCommand;
@@ -119,8 +108,9 @@ class PropagateFilterFactory : public rocksdb::CompactionFilterFactory {
class PubSubFilter : public rocksdb::CompactionFilter {
public:
const char *Name() const override { return "PubSubFilter"; }
- bool Filter(int level, const Slice &key, const Slice &value,
- std::string *new_value, bool *modified) const override { return true; }
+ bool Filter(int level, const Slice &key, const Slice &value, std::string *new_value, bool *modified) const override {
+ return true;
+ }
};
class PubSubFilterFactory : public rocksdb::CompactionFilterFactory {
diff --git a/src/compaction_checker.cc b/src/compaction_checker.cc
index 595c2f3..b22bf3e 100644
--- a/src/compaction_checker.cc
+++ b/src/compaction_checker.cc
@@ -19,9 +19,11 @@
*/
#include "compaction_checker.h"
+
#include <glog/logging.h>
-#include "storage.h"
+
#include "parse_util.h"
+#include "storage.h"
void CompactionChecker::CompactPropagateAndPubSubFiles() {
rocksdb::CompactRangeOptions compact_opts;
@@ -31,7 +33,7 @@ void CompactionChecker::CompactPropagateAndPubSubFiles() {
LOG(INFO) << "[compaction checker] Start the compact the column family: " << cf_name;
auto cf_handle = storage_->GetCFHandle(cf_name);
auto s = storage_->GetDB()->CompactRange(compact_opts, cf_handle, nullptr, nullptr);
- LOG(INFO) << "[compaction checker] Compact the column family: "<< cf_name <<" finished, result: " << s.ToString();
+ LOG(INFO) << "[compaction checker] Compact the column family: " << cf_name << " finished, result: " << s.ToString();
}
}
@@ -49,8 +51,8 @@ void CompactionChecker::PickCompactionFiles(const std::string &cf_name) {
if (props.size() <= 1) return;
size_t maxFilesToCompact = 1;
- if (props.size()/360 > maxFilesToCompact) {
- maxFilesToCompact = props.size()/360;
+ if (props.size() / 360 > maxFilesToCompact) {
+ maxFilesToCompact = props.size() / 360;
}
int64_t now, forceCompactSeconds = 2 * 24 * 3600;
rocksdb::Env::Default()->GetCurrentTime(&now);
@@ -67,8 +69,8 @@ void CompactionChecker::PickCompactionFiles(const std::string &cf_name) {
// file_creation_time is 0 which means the unknown condition in rocksdb
auto s = rocksdb::Env::Default()->GetFileModificationTime(iter.first, &file_creation_time);
if (!s.ok()) {
- LOG(INFO) << "[compaction checker] Failed to get the file creation time: "
- << iter.first << ", err: "<< s.ToString();
+ LOG(INFO) << "[compaction checker] Failed to get the file creation time: " << iter.first
+ << ", err: " << s.ToString();
continue;
}
}
@@ -79,8 +81,7 @@ void CompactionChecker::PickCompactionFiles(const std::string &cf_name) {
if (property_iter.first == "total_keys") {
auto parse_result = ParseInt<int>(property_iter.second, 10);
if (!parse_result) {
- LOG(ERROR) << "[compaction checker] Parse total_keys error: "
- << parse_result.Msg();
+ LOG(ERROR) << "[compaction checker] Parse total_keys error: " << parse_result.Msg();
continue;
}
total_keys = *parse_result;
@@ -88,8 +89,7 @@ void CompactionChecker::PickCompactionFiles(const std::string &cf_name) {
if (property_iter.first == "deleted_keys") {
auto parse_result = ParseInt<int>(property_iter.second, 10);
if (!parse_result) {
- LOG(ERROR) << "[compaction checker] Parse deleted_keys error: "
- << parse_result.Msg();
+ LOG(ERROR) << "[compaction checker] Parse deleted_keys error: " << parse_result.Msg();
continue;
}
deleted_keys = *parse_result;
@@ -104,7 +104,7 @@ void CompactionChecker::PickCompactionFiles(const std::string &cf_name) {
if (start_key.empty() || stop_key.empty()) continue;
// pick the file which was created more than 2 days
- if (file_creation_time < static_cast<uint64_t>(now-forceCompactSeconds)) {
+ if (file_creation_time < static_cast<uint64_t>(now - forceCompactSeconds)) {
LOG(INFO) << "[compaction checker] Going to compact the key in file(created more than 2 days): " << iter.first;
auto s = storage_->Compact(&start_key, &stop_key);
LOG(INFO) << "[compaction checker] Compact the key in file(created more than 2 days): " << iter.first
@@ -112,7 +112,7 @@ void CompactionChecker::PickCompactionFiles(const std::string &cf_name) {
maxFilesToCompact--;
}
// pick the file which has highest delete ratio
- double delete_ratio = static_cast<double>(deleted_keys)/static_cast<double>(total_keys);
+ double delete_ratio = static_cast<double>(deleted_keys) / static_cast<double>(total_keys);
if (total_keys != 0 && delete_ratio > best_delete_ratio) {
best_delete_ratio = delete_ratio;
best_filename = iter.first;
diff --git a/src/compaction_checker.h b/src/compaction_checker.h
index eb9ec55..c740ac8 100644
--- a/src/compaction_checker.h
+++ b/src/compaction_checker.h
@@ -23,14 +23,16 @@
#include <string>
#include <utility>
#include <vector>
+
#include "storage.h"
class CompactionChecker {
public:
- explicit CompactionChecker(Engine::Storage *storage):storage_(storage) {}
+ explicit CompactionChecker(Engine::Storage *storage) : storage_(storage) {}
~CompactionChecker() {}
void PickCompactionFiles(const std::string &cf_name);
void CompactPropagateAndPubSubFiles();
+
private:
Engine::Storage *storage_ = nullptr;
};
diff --git a/src/config.cc b/src/config.cc
index f10ff36..89f9767 100644
--- a/src/config.cc
+++ b/src/config.cc
@@ -20,57 +20,52 @@
#include "config.h"
-#include <rocksdb/env.h>
-
#include <fcntl.h>
+#include <rocksdb/env.h>
#include <strings.h>
+#include <algorithm>
+#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
+#include <limits>
#include <sstream>
-#include <vector>
#include <utility>
-#include <limits>
-#include <algorithm>
-#include <cctype>
+#include <vector>
#include "config_type.h"
#include "config_util.h"
+#include "parse_util.h"
#include "server.h"
#include "status.h"
#include "tls_util.h"
#include "util.h"
-#include "parse_util.h"
const char *kDefaultNamespace = "__namespace";
const char *errNotEnableBlobDB = "Must set rocksdb.enable_blob_files to yes first.";
const char *errNotSetLevelCompactionDynamicLevelBytes =
- "Must set rocksdb.level_compaction_dynamic_level_bytes yes first.";
+ "Must set rocksdb.level_compaction_dynamic_level_bytes yes first.";
const char *kDefaultBindAddress = "127.0.0.1";
-configEnum compression_type_enum[] = {
- {"no", rocksdb::CompressionType::kNoCompression},
- {"snappy", rocksdb::CompressionType::kSnappyCompression},
- {"lz4", rocksdb::CompressionType::kLZ4Compression},
- {"zstd", rocksdb::CompressionType::kZSTD},
- {nullptr, 0}
-};
-
-configEnum supervised_mode_enum[] = {
- {"no", SUPERVISED_NONE},
- {"auto", SUPERVISED_AUTODETECT},
- {"upstart", SUPERVISED_UPSTART},
- {"systemd", SUPERVISED_SYSTEMD},
- {nullptr, 0}
-};
+configEnum compression_type_enum[] = {{"no", rocksdb::CompressionType::kNoCompression},
+ {"snappy", rocksdb::CompressionType::kSnappyCompression},
+ {"lz4", rocksdb::CompressionType::kLZ4Compression},
+ {"zstd", rocksdb::CompressionType::kZSTD},
+ {nullptr, 0}};
+
+configEnum supervised_mode_enum[] = {{"no", SUPERVISED_NONE},
+ {"auto", SUPERVISED_AUTODETECT},
+ {"upstart", SUPERVISED_UPSTART},
+ {"systemd", SUPERVISED_SYSTEMD},
+ {nullptr, 0}};
std::string trimRocksDBPrefix(std::string s) {
if (strncasecmp(s.data(), "rocksdb.", 8)) return s;
- return s.substr(8, s.size()-8);
+ return s.substr(8, s.size() - 8);
}
int configEnumGetValue(configEnum *ce, const char *name) {
@@ -115,8 +110,7 @@ Config::Config() {
{"tls-ciphersuites", false, new StringField(&tls_ciphersuites, "")},
{"tls-prefer-server-ciphers", false, new YesNoField(&tls_prefer_server_ciphers, false)},
{"tls-session-caching", false, new YesNoField(&tls_session_caching, true)},
- {"tls-session-cache-size", false,
- new IntField(&tls_session_cache_size, 1024 * 20, 0, INT_MAX)},
+ {"tls-session-cache-size", false, new IntField(&tls_session_cache_size, 1024 * 20, 0, INT_MAX)},
{"tls-session-cache-timeout", false, new IntField(&tls_session_cache_timeout, 300, 0, INT_MAX)},
#endif
{"workers", true, new IntField(&workers, 8, 1, 256)},
@@ -149,8 +143,8 @@ Config::Config() {
{"use-rsid-psync", true, new YesNoField(&use_rsid_psync, false)},
{"profiling-sample-ratio", false, new IntField(&profiling_sample_ratio, 0, 0, 100)},
{"profiling-sample-record-max-len", false, new IntField(&profiling_sample_record_max_len, 256, 0, INT_MAX)},
- {"profiling-sample-record-threshold-ms",
- false, new IntField(&profiling_sample_record_threshold_ms, 100, 0, INT_MAX)},
+ {"profiling-sample-record-threshold-ms", false,
+ new IntField(&profiling_sample_record_threshold_ms, 100, 0, INT_MAX)},
{"slowlog-log-slower-than", false, new IntField(&slowlog_log_slower_than, 200000, -1, INT_MAX)},
{"profiling-sample-commands", false, new StringField(&profiling_sample_commands_, "")},
{"slowlog-max-len", false, new IntField(&slowlog_max_len, 128, 0, INT_MAX)},
@@ -176,45 +170,45 @@ Config::Config() {
{"rocksdb.max_background_flushes", true, new IntField(&RocksDB.max_background_flushes, 2, 0, 32)},
{"rocksdb.max_sub_compactions", false, new IntField(&RocksDB.max_sub_compactions, 1, 0, 16)},
{"rocksdb.delayed_write_rate", false, new Int64Field(&RocksDB.delayed_write_rate, 0, 0, INT64_MAX)},
- {"rocksdb.wal_ttl_seconds", true, new IntField(&RocksDB.WAL_ttl_seconds, 3*3600, 0, INT_MAX)},
+ {"rocksdb.wal_ttl_seconds", true, new IntField(&RocksDB.WAL_ttl_seconds, 3 * 3600, 0, INT_MAX)},
{"rocksdb.wal_size_limit_mb", true, new IntField(&RocksDB.WAL_size_limit_MB, 16384, 0, INT_MAX)},
- {"rocksdb.max_total_wal_size", false, new IntField(&RocksDB.max_total_wal_size, 64*4*2, 0, INT_MAX)},
+ {"rocksdb.max_total_wal_size", false, new IntField(&RocksDB.max_total_wal_size, 64 * 4 * 2, 0, INT_MAX)},
{"rocksdb.disable_auto_compactions", false, new YesNoField(&RocksDB.disable_auto_compactions, false)},
{"rocksdb.enable_pipelined_write", true, new YesNoField(&RocksDB.enable_pipelined_write, false)},
{"rocksdb.stats_dump_period_sec", false, new IntField(&RocksDB.stats_dump_period_sec, 0, 0, INT_MAX)},
{"rocksdb.cache_index_and_filter_blocks", true, new YesNoField(&RocksDB.cache_index_and_filter_blocks, false)},
{"rocksdb.subkey_block_cache_size", true, new IntField(&RocksDB.subkey_block_cache_size, 2048, 0, INT_MAX)},
{"rocksdb.metadata_block_cache_size", true, new IntField(&RocksDB.metadata_block_cache_size, 2048, 0, INT_MAX)},
- {"rocksdb.share_metadata_and_subkey_block_cache",
- true, new YesNoField(&RocksDB.share_metadata_and_subkey_block_cache, true)},
+ {"rocksdb.share_metadata_and_subkey_block_cache", true,
+ new YesNoField(&RocksDB.share_metadata_and_subkey_block_cache, true)},
{"rocksdb.row_cache_size", true, new IntField(&RocksDB.row_cache_size, 0, 0, INT_MAX)},
- {"rocksdb.compaction_readahead_size", false, new IntField(&RocksDB.compaction_readahead_size, 2*MiB, 0, 64*MiB)},
- {"rocksdb.level0_slowdown_writes_trigger",
- false, new IntField(&RocksDB.level0_slowdown_writes_trigger, 20, 1, 1024)},
- {"rocksdb.level0_stop_writes_trigger",
- false, new IntField(&RocksDB.level0_stop_writes_trigger, 40, 1, 1024)},
- {"rocksdb.level0_file_num_compaction_trigger",
- false, new IntField(&RocksDB.level0_file_num_compaction_trigger, 4, 1, 1024)},
+ {"rocksdb.compaction_readahead_size", false,
+ new IntField(&RocksDB.compaction_readahead_size, 2 * MiB, 0, 64 * MiB)},
+ {"rocksdb.level0_slowdown_writes_trigger", false,
+ new IntField(&RocksDB.level0_slowdown_writes_trigger, 20, 1, 1024)},
+ {"rocksdb.level0_stop_writes_trigger", false, new IntField(&RocksDB.level0_stop_writes_trigger, 40, 1, 1024)},
+ {"rocksdb.level0_file_num_compaction_trigger", false,
+ new IntField(&RocksDB.level0_file_num_compaction_trigger, 4, 1, 1024)},
{"rocksdb.enable_blob_files", false, new YesNoField(&RocksDB.enable_blob_files, false)},
{"rocksdb.min_blob_size", false, new IntField(&RocksDB.min_blob_size, 4096, 0, INT_MAX)},
{"rocksdb.blob_file_size", false, new IntField(&RocksDB.blob_file_size, 268435456, 0, INT_MAX)},
{"rocksdb.enable_blob_garbage_collection", false, new YesNoField(&RocksDB.enable_blob_garbage_collection, true)},
- {"rocksdb.blob_garbage_collection_age_cutoff",
- false, new IntField(&RocksDB.blob_garbage_collection_age_cutoff, 25, 0, 100)},
- {"rocksdb.max_bytes_for_level_base",
- false, new IntField(&RocksDB.max_bytes_for_level_base, 268435456, 0, INT_MAX)},
- {"rocksdb.max_bytes_for_level_multiplier",
- false, new IntField(&RocksDB.max_bytes_for_level_multiplier, 10, 1, 100)},
- {"rocksdb.level_compaction_dynamic_level_bytes",
- false, new YesNoField(&RocksDB.level_compaction_dynamic_level_bytes, false)},
+ {"rocksdb.blob_garbage_collection_age_cutoff", false,
+ new IntField(&RocksDB.blob_garbage_collection_age_cutoff, 25, 0, 100)},
+ {"rocksdb.max_bytes_for_level_base", false,
+ new IntField(&RocksDB.max_bytes_for_level_base, 268435456, 0, INT_MAX)},
+ {"rocksdb.max_bytes_for_level_multiplier", false,
+ new IntField(&RocksDB.max_bytes_for_level_multiplier, 10, 1, 100)},
+ {"rocksdb.level_compaction_dynamic_level_bytes", false,
+ new YesNoField(&RocksDB.level_compaction_dynamic_level_bytes, false)},
/* rocksdb write options */
{"rocksdb.write_options.sync", true, new YesNoField(&RocksDB.write_options.sync, false)},
{"rocksdb.write_options.disable_wal", true, new YesNoField(&RocksDB.write_options.disable_WAL, false)},
{"rocksdb.write_options.no_slowdown", true, new YesNoField(&RocksDB.write_options.no_slowdown, false)},
{"rocksdb.write_options.low_pri", true, new YesNoField(&RocksDB.write_options.low_pri, false)},
- {"rocksdb.write_options.memtable_insert_hint_per_batch",
- true, new YesNoField(&RocksDB.write_options.memtable_insert_hint_per_batch, false)},
+ {"rocksdb.write_options.memtable_insert_hint_per_batch", true,
+ new YesNoField(&RocksDB.write_options.memtable_insert_hint_per_batch, false)},
};
for (auto &wrapper : fields) {
auto &field = wrapper.field;
@@ -229,69 +223,75 @@ Config::Config() {
// to make sure that new value is valid.
void Config::initFieldValidator() {
std::map<std::string, validate_fn> validators = {
- {"requirepass", [this](const std::string& k, const std::string& v)->Status {
- if (v.empty() && !tokens.empty()) {
- return Status(Status::NotOK, "requirepass empty not allowed while the namespace exists");
- }
- if (tokens.find(v) != tokens.end()) {
- return Status(Status::NotOK, "requirepass is duplicated with namespace tokens");
- }
- return Status::OK();
- }},
- {"masterauth", [this](const std::string& k, const std::string& v)->Status {
- if (tokens.find(v) != tokens.end()) {
- return Status(Status::NotOK, "masterauth is duplicated with namespace tokens");
- }
- return Status::OK();
- }},
- {"compact-cron", [this](const std::string& k, const std::string& v)->Status {
- std::vector<std::string> args = Util::Split(v, " \t");
- return compact_cron.SetScheduleTime(args);
- }},
- {"bgsave-cron", [this](const std::string& k, const std::string& v)->Status {
- std::vector<std::string> args = Util::Split(v, " \t");
- return bgsave_cron.SetScheduleTime(args);
- }},
- {"compaction-checker-range", [this](const std::string& k, const std::string& v)->Status {
- if (v.empty()) {
- compaction_checker_range.Start = -1;
- compaction_checker_range.Stop = -1;
- return Status::OK();
- }
- std::vector<std::string> args = Util::Split(v, "-");
- if (args.size() != 2) {
- return Status(Status::NotOK, "invalid range format, the range should be between 0 and 24");
- }
- int64_t start, stop;
- Status s = Util::DecimalStringToNum(args[0], &start, 0, 24);
- if (!s.IsOK()) return s;
- s = Util::DecimalStringToNum(args[1], &stop, 0, 24);
- if (!s.IsOK()) return s;
- if (start > stop) return Status(Status::NotOK, "invalid range format, start should be smaller than stop");
- compaction_checker_range.Start = start;
- compaction_checker_range.Stop = stop;
- return Status::OK();
- }},
- {"rename-command", [](const std::string &k, const std::string &v) -> Status {
- std::vector<std::string> args = Util::Split(v, " \t");
- if (args.size() != 2) {
- return Status(Status::NotOK, "Invalid rename-command format");
- }
- auto commands = Redis::GetCommands();
- auto cmd_iter = commands->find(Util::ToLower(args[0]));
- if (cmd_iter == commands->end()) {
- return Status(Status::NotOK, "No such command in rename-command");
- }
- if (args[1] != "\"\"") {
- auto new_command_name = Util::ToLower(args[1]);
- if (commands->find(new_command_name) != commands->end()) {
- return Status(Status::NotOK, "Target command name already exists");
- }
- (*commands)[new_command_name] = cmd_iter->second;
- }
- commands->erase(cmd_iter);
- return Status::OK();
- }},
+ {"requirepass",
+ [this](const std::string &k, const std::string &v) -> Status {
+ if (v.empty() && !tokens.empty()) {
+ return Status(Status::NotOK, "requirepass empty not allowed while the namespace exists");
+ }
+ if (tokens.find(v) != tokens.end()) {
+ return Status(Status::NotOK, "requirepass is duplicated with namespace tokens");
+ }
+ return Status::OK();
+ }},
+ {"masterauth",
+ [this](const std::string &k, const std::string &v) -> Status {
+ if (tokens.find(v) != tokens.end()) {
+ return Status(Status::NotOK, "masterauth is duplicated with namespace tokens");
+ }
+ return Status::OK();
+ }},
+ {"compact-cron",
+ [this](const std::string &k, const std::string &v) -> Status {
+ std::vector<std::string> args = Util::Split(v, " \t");
+ return compact_cron.SetScheduleTime(args);
+ }},
+ {"bgsave-cron",
+ [this](const std::string &k, const std::string &v) -> Status {
+ std::vector<std::string> args = Util::Split(v, " \t");
+ return bgsave_cron.SetScheduleTime(args);
+ }},
+ {"compaction-checker-range",
+ [this](const std::string &k, const std::string &v) -> Status {
+ if (v.empty()) {
+ compaction_checker_range.Start = -1;
+ compaction_checker_range.Stop = -1;
+ return Status::OK();
+ }
+ std::vector<std::string> args = Util::Split(v, "-");
+ if (args.size() != 2) {
+ return Status(Status::NotOK, "invalid range format, the range should be between 0 and 24");
+ }
+ int64_t start, stop;
+ Status s = Util::DecimalStringToNum(args[0], &start, 0, 24);
+ if (!s.IsOK()) return s;
+ s = Util::DecimalStringToNum(args[1], &stop, 0, 24);
+ if (!s.IsOK()) return s;
+ if (start > stop) return Status(Status::NotOK, "invalid range format, start should be smaller than stop");
+ compaction_checker_range.Start = start;
+ compaction_checker_range.Stop = stop;
+ return Status::OK();
+ }},
+ {"rename-command",
+ [](const std::string &k, const std::string &v) -> Status {
+ std::vector<std::string> args = Util::Split(v, " \t");
+ if (args.size() != 2) {
+ return Status(Status::NotOK, "Invalid rename-command format");
+ }
+ auto commands = Redis::GetCommands();
+ auto cmd_iter = commands->find(Util::ToLower(args[0]));
+ if (cmd_iter == commands->end()) {
+ return Status(Status::NotOK, "No such command in rename-command");
+ }
+ if (args[1] != "\"\"") {
+ auto new_command_name = Util::ToLower(args[1]);
+ if (commands->find(new_command_name) != commands->end()) {
+ return Status(Status::NotOK, "Target command name already exists");
+ }
+ (*commands)[new_command_name] = cmd_iter->second;
+ }
+ commands->erase(cmd_iter);
+ return Status::OK();
+ }},
};
for (const auto &iter : validators) {
auto field_iter = fields_.find(iter.first);
@@ -305,16 +305,16 @@ void Config::initFieldValidator() {
// it may change related fileds or re-format the field. for example,
// when the 'dir' was set, the db-dir or backup-dir should be reset as well.
void Config::initFieldCallback() {
- auto set_db_option_cb = [](Server* srv, const std::string &k, const std::string& v)->Status {
+ auto set_db_option_cb = [](Server *srv, const std::string &k, const std::string &v) -> Status {
if (!srv) return Status::OK(); // srv is nullptr when load config from file
return srv->storage_->SetDBOption(trimRocksDBPrefix(k), v);
};
- auto set_cf_option_cb = [](Server* srv, const std::string &k, const std::string& v)->Status {
+ auto set_cf_option_cb = [](Server *srv, const std::string &k, const std::string &v) -> Status {
if (!srv) return Status::OK(); // srv is nullptr when load config from file
return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), v);
};
#ifdef ENABLE_OPENSSL
- auto set_tls_option = [](Server* srv, const std::string &k, const std::string& v) {
+ auto set_tls_option = [](Server *srv, const std::string &k, const std::string &v) {
if (!srv) return Status::OK(); // srv is nullptr when load config from file
auto new_ctx = CreateSSLContext(srv->GetConfig());
if (!new_ctx) {
@@ -326,190 +326,208 @@ void Config::initFieldCallback() {
#endif
std::map<std::string, callback_fn> callbacks = {
- {"dir", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- db_dir = dir + "/db";
- if (backup_dir.empty()) backup_dir = dir + "/backup";
- if (log_dir.empty()) log_dir = dir;
- checkpoint_dir = dir + "/checkpoint";
- sync_checkpoint_dir = dir + "/sync_checkpoint";
- backup_sync_dir = dir + "/backup_for_sync";
- return Status::OK();
- }},
- {"cluster-enabled", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (cluster_enabled) slot_id_encoded = true;
- return Status::OK();
- }},
- {"bind", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- trimRocksDBPrefix(k);
- std::vector<std::string> args = Util::Split(v, " \t");
- binds = std::move(args);
- return Status::OK();
- }},
- { "maxclients", [](Server* srv, const std::string &k, const std::string& v) -> Status {
- if (!srv) return Status::OK();
- srv->AdjustOpenFilesLimit();
- return Status::OK();
- }},
- {"slaveof", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (v.empty()) {
- return Status::OK();
- }
- std::vector<std::string> args = Util::Split(v, " \t");
- if (args.size() != 2) return Status(Status::NotOK, "wrong number of arguments");
- if (args[0] != "no" && args[1] != "one") {
- master_host = args[0];
- auto parse_result = ParseInt<int>(args[1].c_str(), NumericRange<int>{1, PORT_LIMIT - 1}, 10);
- if (!parse_result) {
- return Status(Status::NotOK, "should be between 0 and 65535");
- }
- master_port = *parse_result;
- }
- return Status::OK();
- }},
- {"profiling-sample-commands", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- std::vector<std::string> cmds = Util::Split(v, ",");
- profiling_sample_all_commands = false;
- profiling_sample_commands.clear();
- for (auto const &cmd : cmds) {
- if (cmd == "*") {
- profiling_sample_all_commands = true;
- profiling_sample_commands.clear();
- return Status::OK();
- }
- if (!Redis::IsCommandExists(cmd)) {
- return Status(Status::NotOK, cmd + " is not Kvrocks supported command");
- }
- // profiling_sample_commands use command's original name, regardless of rename-command directive
- profiling_sample_commands.insert(cmd);
- }
- return Status::OK();
- }},
- {"slowlog-max-len", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- srv->GetSlowLog()->SetMaxEntries(slowlog_max_len);
- return Status::OK();
- }},
- {"max-db-size", [](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- srv->storage_->CheckDBSizeLimit();
- return Status::OK();
- }},
- {"max-io-mb", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- srv->storage_->SetIORateLimit(static_cast<uint64_t>(max_io_mb));
- return Status::OK();
- }},
- {"profiling-sample-record-max-len", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- srv->GetPerfLog()->SetMaxEntries(profiling_sample_record_max_len);
- return Status::OK();
- }},
- {"migrate-speed", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- if (cluster_enabled) srv->slot_migrate_->SetMigrateSpeedLimit(migrate_speed);
- return Status::OK();
- }},
- {"migrate-pipeline-size", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- if (cluster_enabled) srv->slot_migrate_->SetPipelineSize(pipeline_size);
- return Status::OK();
- }},
- {"migrate-sequence-gap", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- if (cluster_enabled) srv->slot_migrate_->SetSequenceGapSize(sequence_gap);
- return Status::OK();
- }},
- {"rocksdb.target_file_size_base", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k),
- std::to_string(RocksDB.target_file_size_base * MiB));
- }},
- {"rocksdb.write_buffer_size", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k),
- std::to_string(RocksDB.write_buffer_size * MiB));
- }},
- {"rocksdb.disable_auto_compactions", [](Server* srv,
- const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- std::string disable_auto_compactions = v == "yes" ? "true" : "false";
- return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), disable_auto_compactions);
- }},
- {"rocksdb.max_total_wal_size", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- return srv->storage_->SetDBOption(trimRocksDBPrefix(k),
- std::to_string(RocksDB.max_total_wal_size * MiB));
- }},
- {"rocksdb.enable_blob_files", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- std::string enable_blob_files = RocksDB.enable_blob_files ? "true" : "false";
- return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), enable_blob_files);
- }},
- {"rocksdb.min_blob_size", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- if (!RocksDB.enable_blob_files) {
- return Status(Status::NotOK, errNotEnableBlobDB);
- }
- return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), v);
- }},
- {"rocksdb.blob_file_size", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- if (!RocksDB.enable_blob_files) {
- return Status(Status::NotOK, errNotEnableBlobDB);
- }
- return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k),
- std::to_string(RocksDB.blob_file_size));
- }},
- {"rocksdb.enable_blob_garbage_collection", [this](Server* srv, const std::string &k,
- const std::string& v)->Status {
- if (!srv) return Status::OK();
- if (!RocksDB.enable_blob_files) {
- return Status(Status::NotOK, errNotEnableBlobDB);
- }
- std::string enable_blob_garbage_collection = v == "yes" ? "true" : "false";
- return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), enable_blob_garbage_collection);
- }},
- {"rocksdb.blob_garbage_collection_age_cutoff", [this](Server* srv, const std::string &k,
- const std::string& v)->Status {
- if (!srv) return Status::OK();
- if (!RocksDB.enable_blob_files) {
- return Status(Status::NotOK, errNotEnableBlobDB);
- }
- int val;
- auto parse_result = ParseInt<int>(v, 10);
- if (!parse_result) {
- return Status(Status::NotOK, "Illegal blob_garbage_collection_age_cutoff value.");
- }
- val = *parse_result;
- if (val < 0 || val > 100) {
- return Status(Status::NotOK, "blob_garbage_collection_age_cutoff must >= 0 and <= 100.");
- }
-
- double cutoff = val / 100.0;
- return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), std::to_string(cutoff));
- }},
- {"rocksdb.level_compaction_dynamic_level_bytes", [](Server* srv, const std::string &k,
- const std::string& v)->Status {
- if (!srv) return Status::OK();
- std::string level_compaction_dynamic_level_bytes = v == "yes" ? "true" : "false";
- return srv->storage_->SetDBOption(trimRocksDBPrefix(k), level_compaction_dynamic_level_bytes);
- }},
- {"rocksdb.max_bytes_for_level_base", [this](Server* srv, const std::string &k, const std::string& v)->Status {
- if (!srv) return Status::OK();
- if (!RocksDB.level_compaction_dynamic_level_bytes) {
- return Status(Status::NotOK, errNotSetLevelCompactionDynamicLevelBytes);
- }
- return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k),
- std::to_string(RocksDB.max_bytes_for_level_base));
- }},
- {"rocksdb.max_bytes_for_level_multiplier", [this](Server* srv, const std::string &k,
- const std::string& v)->Status {
- if (!srv) return Status::OK();
- if (!RocksDB.level_compaction_dynamic_level_bytes) {
- return Status(Status::NotOK, errNotSetLevelCompactionDynamicLevelBytes);
- }
- return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), v);
- }},
+ {"dir",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ db_dir = dir + "/db";
+ if (backup_dir.empty()) backup_dir = dir + "/backup";
+ if (log_dir.empty()) log_dir = dir;
+ checkpoint_dir = dir + "/checkpoint";
+ sync_checkpoint_dir = dir + "/sync_checkpoint";
+ backup_sync_dir = dir + "/backup_for_sync";
+ return Status::OK();
+ }},
+ {"cluster-enabled",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (cluster_enabled) slot_id_encoded = true;
+ return Status::OK();
+ }},
+ {"bind",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ trimRocksDBPrefix(k);
+ std::vector<std::string> args = Util::Split(v, " \t");
+ binds = std::move(args);
+ return Status::OK();
+ }},
+ {"maxclients",
+ [](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ srv->AdjustOpenFilesLimit();
+ return Status::OK();
+ }},
+ {"slaveof",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (v.empty()) {
+ return Status::OK();
+ }
+ std::vector<std::string> args = Util::Split(v, " \t");
+ if (args.size() != 2) return Status(Status::NotOK, "wrong number of arguments");
+ if (args[0] != "no" && args[1] != "one") {
+ master_host = args[0];
+ auto parse_result = ParseInt<int>(args[1].c_str(), NumericRange<int>{1, PORT_LIMIT - 1}, 10);
+ if (!parse_result) {
+ return Status(Status::NotOK, "should be between 0 and 65535");
+ }
+ master_port = *parse_result;
+ }
+ return Status::OK();
+ }},
+ {"profiling-sample-commands",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ std::vector<std::string> cmds = Util::Split(v, ",");
+ profiling_sample_all_commands = false;
+ profiling_sample_commands.clear();
+ for (auto const &cmd : cmds) {
+ if (cmd == "*") {
+ profiling_sample_all_commands = true;
+ profiling_sample_commands.clear();
+ return Status::OK();
+ }
+ if (!Redis::IsCommandExists(cmd)) {
+ return Status(Status::NotOK, cmd + " is not Kvrocks supported command");
+ }
+ // profiling_sample_commands use command's original name, regardless of rename-command directive
+ profiling_sample_commands.insert(cmd);
+ }
+ return Status::OK();
+ }},
+ {"slowlog-max-len",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ srv->GetSlowLog()->SetMaxEntries(slowlog_max_len);
+ return Status::OK();
+ }},
+ {"max-db-size",
+ [](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ srv->storage_->CheckDBSizeLimit();
+ return Status::OK();
+ }},
+ {"max-io-mb",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ srv->storage_->SetIORateLimit(static_cast<uint64_t>(max_io_mb));
+ return Status::OK();
+ }},
+ {"profiling-sample-record-max-len",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ srv->GetPerfLog()->SetMaxEntries(profiling_sample_record_max_len);
+ return Status::OK();
+ }},
+ {"migrate-speed",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ if (cluster_enabled) srv->slot_migrate_->SetMigrateSpeedLimit(migrate_speed);
+ return Status::OK();
+ }},
+ {"migrate-pipeline-size",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ if (cluster_enabled) srv->slot_migrate_->SetPipelineSize(pipeline_size);
+ return Status::OK();
+ }},
+ {"migrate-sequence-gap",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ if (cluster_enabled) srv->slot_migrate_->SetSequenceGapSize(sequence_gap);
+ return Status::OK();
+ }},
+ {"rocksdb.target_file_size_base",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k),
+ std::to_string(RocksDB.target_file_size_base * MiB));
+ }},
+ {"rocksdb.write_buffer_size",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k),
+ std::to_string(RocksDB.write_buffer_size * MiB));
+ }},
+ {"rocksdb.disable_auto_compactions",
+ [](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ std::string disable_auto_compactions = v == "yes" ? "true" : "false";
+ return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), disable_auto_compactions);
+ }},
+ {"rocksdb.max_total_wal_size",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ return srv->storage_->SetDBOption(trimRocksDBPrefix(k), std::to_string(RocksDB.max_total_wal_size * MiB));
+ }},
+ {"rocksdb.enable_blob_files",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ std::string enable_blob_files = RocksDB.enable_blob_files ? "true" : "false";
+ return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), enable_blob_files);
+ }},
+ {"rocksdb.min_blob_size",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ if (!RocksDB.enable_blob_files) {
+ return Status(Status::NotOK, errNotEnableBlobDB);
+ }
+ return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), v);
+ }},
+ {"rocksdb.blob_file_size",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ if (!RocksDB.enable_blob_files) {
+ return Status(Status::NotOK, errNotEnableBlobDB);
+ }
+ return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), std::to_string(RocksDB.blob_file_size));
+ }},
+ {"rocksdb.enable_blob_garbage_collection",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ if (!RocksDB.enable_blob_files) {
+ return Status(Status::NotOK, errNotEnableBlobDB);
+ }
+ std::string enable_blob_garbage_collection = v == "yes" ? "true" : "false";
+ return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), enable_blob_garbage_collection);
+ }},
+ {"rocksdb.blob_garbage_collection_age_cutoff",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ if (!RocksDB.enable_blob_files) {
+ return Status(Status::NotOK, errNotEnableBlobDB);
+ }
+ int val;
+ auto parse_result = ParseInt<int>(v, 10);
+ if (!parse_result) {
+ return Status(Status::NotOK, "Illegal blob_garbage_collection_age_cutoff value.");
+ }
+ val = *parse_result;
+ if (val < 0 || val > 100) {
+ return Status(Status::NotOK, "blob_garbage_collection_age_cutoff must >= 0 and <= 100.");
+ }
+
+ double cutoff = val / 100.0;
+ return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), std::to_string(cutoff));
+ }},
+ {"rocksdb.level_compaction_dynamic_level_bytes",
+ [](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ std::string level_compaction_dynamic_level_bytes = v == "yes" ? "true" : "false";
+ return srv->storage_->SetDBOption(trimRocksDBPrefix(k), level_compaction_dynamic_level_bytes);
+ }},
+ {"rocksdb.max_bytes_for_level_base",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ if (!RocksDB.level_compaction_dynamic_level_bytes) {
+ return Status(Status::NotOK, errNotSetLevelCompactionDynamicLevelBytes);
+ }
+ return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k),
+ std::to_string(RocksDB.max_bytes_for_level_base));
+ }},
+ {"rocksdb.max_bytes_for_level_multiplier",
+ [this](Server *srv, const std::string &k, const std::string &v) -> Status {
+ if (!srv) return Status::OK();
+ if (!RocksDB.level_compaction_dynamic_level_bytes) {
+ return Status(Status::NotOK, errNotSetLevelCompactionDynamicLevelBytes);
+ }
+ return srv->storage_->SetColumnFamilyOption(trimRocksDBPrefix(k), v);
+ }},
{"rocksdb.max_open_files", set_db_option_cb},
{"rocksdb.stats_dump_period_sec", set_db_option_cb},
{"rocksdb.delayed_write_rate", set_db_option_cb},
@@ -550,7 +568,7 @@ void Config::SetMaster(const std::string &host, int port) {
master_port = port;
auto iter = fields_.find("slaveof");
if (iter != fields_.end()) {
- iter->second->Set(master_host+" "+std::to_string(master_port));
+ iter->second->Set(master_host + " " + std::to_string(master_port));
}
}
@@ -575,13 +593,13 @@ Status Config::parseConfigFromString(const std::string &input, int line_number)
const char ns_str[] = "namespace.";
size_t ns_str_size = sizeof(ns_str) - 1;
if (!strncasecmp(kv.first.data(), ns_str, ns_str_size)) {
- // namespace should keep key case-sensitive
- field_key = kv.first;
- tokens[kv.second] = kv.first.substr(ns_str_size);
+ // namespace should keep key case-sensitive
+ field_key = kv.first;
+ tokens[kv.second] = kv.first.substr(ns_str_size);
}
auto iter = fields_.find(field_key);
if (iter != fields_.end()) {
- auto& field = iter->second;
+ auto &field = iter->second;
field->line_number = line_number;
auto s = field->Set(kv.second);
if (!s.IsOK()) return s;
@@ -600,8 +618,9 @@ Status Config::finish() {
binds.emplace_back(kDefaultBindAddress);
}
if (cluster_enabled && binds.size() == 0) {
- return Status(Status::NotOK, "node is in cluster mode, but TCP listen address "
- "wasn't specified via configuration file");
+ return Status(Status::NotOK,
+ "node is in cluster mode, but TCP listen address "
+ "wasn't specified via configuration file");
}
if (master_port != 0 && binds.size() == 0) {
return Status(Status::NotOK, "replication doesn't supports unix socket");
@@ -638,7 +657,8 @@ Status Config::Load(const std::string &path) {
file.close();
} else {
std::cout << "Warn: no config file specified, using the default config. "
- "In order to specify a config file use kvrocks -c /path/to/kvrocks.conf" << std::endl;
+ "In order to specify a config file use kvrocks -c /path/to/kvrocks.conf"
+ << std::endl;
}
for (const auto &iter : fields_) {
@@ -647,8 +667,8 @@ Status Config::Load(const std::string &path) {
if (iter.second->line_number != 0 && iter.second->validate) {
auto s = iter.second->validate(iter.first, iter.second->ToString());
if (!s.IsOK()) {
- return Status(Status::NotOK, "at line: #L" + std::to_string(iter.second->line_number)
- + ", " + iter.first + " is invalid: " + s.Msg());
+ return Status(Status::NotOK, "at line: #L" + std::to_string(iter.second->line_number) + ", " + iter.first +
+ " is invalid: " + s.Msg());
}
}
}
@@ -657,7 +677,7 @@ Status Config::Load(const std::string &path) {
if (iter.second->callback) {
auto s = iter.second->callback(nullptr, iter.first, iter.second->ToString());
if (!s.IsOK()) {
- return Status(Status::NotOK, s.Msg()+" in key '"+iter.first+"'");
+ return Status(Status::NotOK, s.Msg() + " in key '" + iter.first + "'");
}
}
}
@@ -678,9 +698,9 @@ Status Config::Set(Server *svr, std::string key, const std::string &value) {
key = Util::ToLower(key);
auto iter = fields_.find(key);
if (iter == fields_.end() || iter->second->readonly) {
- return Status(Status::NotOK, "Unsupported CONFIG parameter: "+key);
+ return Status(Status::NotOK, "Unsupported CONFIG parameter: " + key);
}
- auto& field = iter->second;
+ auto &field = iter->second;
if (field->validate) {
auto s = field->validate(key, value);
if (!s.IsOK()) return s;
@@ -747,13 +767,13 @@ Status Config::Rewrite() {
if (remain.second.empty()) continue;
string_stream << remain.first << " " << remain.second << "\n";
}
- std::string tmp_path = path_+".tmp";
+ std::string tmp_path = path_ + ".tmp";
remove(tmp_path.data());
std::ofstream output_file(tmp_path, std::ios::out);
output_file.write(string_stream.str().c_str(), string_stream.str().size());
output_file.close();
if (rename(tmp_path.data(), path_.data()) < 0) {
- return Status(Status::NotOK, std::string("rename file encounter error: ")+strerror(errno));
+ return Status(Status::NotOK, std::string("rename file encounter error: ") + strerror(errno));
}
return Status::OK();
}
diff --git a/src/config.h b/src/config.h
index 2ad9e01..42143e9 100644
--- a/src/config.h
+++ b/src/config.h
@@ -60,12 +60,10 @@ struct CompactionCheckerRange {
int Start;
int Stop;
- bool Enabled() {
- return Start != -1 || Stop != -1;
- }
+ bool Enabled() { return Start != -1 || Stop != -1; }
};
-struct Config{
+struct Config {
public:
Config();
~Config() = default;
diff --git a/src/config_type.h b/src/config_type.h
index 6fa070b..2148cc0 100644
--- a/src/config_type.h
+++ b/src/config_type.h
@@ -23,14 +23,14 @@
#include <string>
#include <utility>
-#include "util.h"
#include "status.h"
+#include "util.h"
// forward declaration
class Server;
-typedef std::function<Status(const std::string&, const std::string&)> validate_fn;
-typedef std::function<Status(Server *srv, const std::string&, const std::string&)> callback_fn;
+typedef std::function<Status(const std::string &, const std::string &)> validate_fn;
+typedef std::function<Status(Server *srv, const std::string &, const std::string &)> callback_fn;
typedef struct configEnum {
const char *name;
@@ -55,15 +55,11 @@ class ConfigField {
callback_fn callback = nullptr;
};
-class StringField: public ConfigField {
+class StringField : public ConfigField {
public:
- StringField(std::string *receiver, std::string s): receiver_(receiver) {
- *receiver_ = std::move(s);
- }
+ StringField(std::string *receiver, std::string s) : receiver_(receiver) { *receiver_ = std::move(s); }
~StringField() override = default;
- std::string ToString() override {
- return *receiver_;
- }
+ std::string ToString() override { return *receiver_; }
Status Set(const std::string &v) override {
*receiver_ = v;
return Status::OK();
@@ -75,14 +71,9 @@ class StringField: public ConfigField {
class IntField : public ConfigField {
public:
- IntField(int *receiver, int n, int min, int max)
- : receiver_(receiver), min_(min), max_(max) {
- *receiver_ = n;
- }
+ IntField(int *receiver, int n, int min, int max) : receiver_(receiver), min_(min), max_(max) { *receiver_ = n; }
~IntField() override = default;
- std::string ToString() override {
- return std::to_string(*receiver_);
- }
+ std::string ToString() override { return std::to_string(*receiver_); }
Status ToNumber(int64_t *n) override {
*n = *receiver_;
return Status::OK();
@@ -103,14 +94,9 @@ class IntField : public ConfigField {
class OctalField : public ConfigField {
public:
- OctalField(int *receiver, int n, int min, int max)
- : receiver_(receiver), min_(min), max_(max) {
- *receiver_ = n;
- }
+ OctalField(int *receiver, int n, int min, int max) : receiver_(receiver), min_(min), max_(max) { *receiver_ = n; }
~OctalField() override = default;
- std::string ToString() override {
- return std::to_string(*receiver_);
- }
+ std::string ToString() override { return std::to_string(*receiver_); }
Status ToNumber(int64_t *n) override {
*n = *receiver_;
return Status::OK();
@@ -131,14 +117,11 @@ class OctalField : public ConfigField {
class Int64Field : public ConfigField {
public:
- Int64Field(int64_t *receiver, int64_t n, int64_t min, int64_t max)
- : receiver_(receiver), min_(min), max_(max) {
+ Int64Field(int64_t *receiver, int64_t n, int64_t min, int64_t max) : receiver_(receiver), min_(min), max_(max) {
*receiver_ = n;
}
~Int64Field() override = default;
- std::string ToString() override {
- return std::to_string(*receiver_);
- }
+ std::string ToString() override { return std::to_string(*receiver_); }
Status ToNumber(int64_t *n) override {
*n = *receiver_;
return Status::OK();
@@ -159,13 +142,9 @@ class Int64Field : public ConfigField {
class YesNoField : public ConfigField {
public:
- YesNoField(bool *receiver, bool b) : receiver_(receiver) {
- *receiver_ = b;
- }
+ YesNoField(bool *receiver, bool b) : receiver_(receiver) { *receiver_ = b; }
~YesNoField() override = default;
- std::string ToString() override {
- return *receiver_ ? "yes":"no";
- }
+ std::string ToString() override { return *receiver_ ? "yes" : "no"; }
Status ToBool(bool *b) override {
*b = *receiver_;
return Status::OK();
@@ -187,14 +166,9 @@ class YesNoField : public ConfigField {
class EnumField : public ConfigField {
public:
- EnumField(int *receiver, configEnum *enums, int e) :
- receiver_(receiver), enums_(enums) {
- *receiver_ = e;
- }
+ EnumField(int *receiver, configEnum *enums, int e) : receiver_(receiver), enums_(enums) { *receiver_ = e; }
~EnumField() override = default;
- std::string ToString() override {
- return configEnumGetName(enums_, *receiver_);
- }
+ std::string ToString() override { return configEnumGetName(enums_, *receiver_); }
Status ToNumber(int64_t *n) override {
*n = *receiver_;
return Status::OK();
diff --git a/src/config_util.cc b/src/config_util.cc
index 6f22ef8..8d45b4b 100644
--- a/src/config_util.cc
+++ b/src/config_util.cc
@@ -19,19 +19,19 @@
*/
#include "config_util.h"
-#include "util.h"
+#include "util.h"
StatusOr<ConfigKV> ParseConfigLine(const std::string& line) {
enum {
- KEY, // in (unquoted) key string
- NORMAL, // in unquoted value string
- QUOTED, // in quoted value string
- PRE_KEY_SPACE, // in whitespace characters before key
+ KEY, // in (unquoted) key string
+ NORMAL, // in unquoted value string
+ QUOTED, // in quoted value string
+ PRE_KEY_SPACE, // in whitespace characters before key
AFTER_KEY_SPACE, // in whitespace characters after key and before value
AFTER_VAL_SPACE, // in whitespace characters after value
- ESCAPE, // in escape character of quoted string
- ERROR // error state, e.g. encounter more than one value
+ ESCAPE, // in escape character of quoted string
+ ERROR // error state, e.g. encounter more than one value
} state = PRE_KEY_SPACE;
char quote; // single or double quote
@@ -135,7 +135,6 @@ StatusOr<ConfigKV> ParseConfigLine(const std::string& line) {
}
}
-
if (state == KEY) {
res.first = current_str;
state = AFTER_KEY_SPACE;
@@ -151,15 +150,14 @@ StatusOr<ConfigKV> ParseConfigLine(const std::string& line) {
return res;
}
-std::string DumpConfigLine(const ConfigKV &config) {
+std::string DumpConfigLine(const ConfigKV& config) {
std::string res;
res += config.first;
res += " ";
- if (std::any_of(config.second.begin(), config.second.end(), [](char c) {
- return std::isspace(c) || c == '"' || c == '\'' || c == '#';
- })) {
+ if (std::any_of(config.second.begin(), config.second.end(),
+ [](char c) { return std::isspace(c) || c == '"' || c == '\'' || c == '#'; })) {
res += '"';
for (char c : config.second) {
if (c == '\\') {
diff --git a/src/config_util.h b/src/config_util.h
index 9d781dd..718ebd4 100644
--- a/src/config_util.h
+++ b/src/config_util.h
@@ -21,8 +21,8 @@
#pragma once
#include <string>
-#include <vector>
#include <utility>
+#include <vector>
#include "status.h"
diff --git a/src/cron.cc b/src/cron.cc
index abcd4a0..19dc78b 100644
--- a/src/cron.cc
+++ b/src/cron.cc
@@ -19,18 +19,14 @@
*/
#include "cron.h"
+
#include <stdexcept>
#include <utility>
std::string Scheduler::ToString() const {
- auto param2String = [](int n)->std::string {
- return n == -1 ? "*" : std::to_string(n);
- };
- return param2String(minute) + " " +
- param2String(hour) + " " +
- param2String(mday) + " " +
- param2String(month) + " " +
- param2String(wday);
+ auto param2String = [](int n) -> std::string { return n == -1 ? "*" : std::to_string(n); };
+ return param2String(minute) + " " + param2String(hour) + " " + param2String(mday) + " " + param2String(month) + " " +
+ param2String(wday);
}
Status Cron::SetScheduleTime(const std::vector<std::string> &args) {
@@ -45,7 +41,7 @@ Status Cron::SetScheduleTime(const std::vector<std::string> &args) {
std::vector<Scheduler> new_schedulers;
Scheduler st;
for (size_t i = 0; i < args.size(); i += 5) {
- Status s = convertToScheduleTime(args[i], args[i+1], args[i+2], args[i+3], args[i+4], &st);
+ Status s = convertToScheduleTime(args[i], args[i + 1], args[i + 2], args[i + 3], args[i + 4], &st);
if (!s.IsOK()) {
return Status(Status::NotOK, "time expression format error : " + s.Msg());
}
@@ -56,18 +52,13 @@ Status Cron::SetScheduleTime(const std::vector<std::string> &args) {
}
bool Cron::IsTimeMatch(struct tm *tm) {
- if (tm->tm_min == last_tm_.tm_min &&
- tm->tm_hour == last_tm_.tm_hour &&
- tm->tm_mday == last_tm_.tm_mday &&
- tm->tm_mon == last_tm_.tm_mon &&
- tm->tm_wday == last_tm_.tm_wday) {
+ if (tm->tm_min == last_tm_.tm_min && tm->tm_hour == last_tm_.tm_hour && tm->tm_mday == last_tm_.tm_mday &&
+ tm->tm_mon == last_tm_.tm_mon && tm->tm_wday == last_tm_.tm_wday) {
return false;
}
for (const auto &st : schedulers_) {
- if ((st.minute == -1 || tm->tm_min == st.minute) &&
- (st.hour == -1 || tm->tm_hour == st.hour) &&
- (st.mday == -1 || tm->tm_mday == st.mday) &&
- (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
+ if ((st.minute == -1 || tm->tm_min == st.minute) && (st.hour == -1 || tm->tm_hour == st.hour) &&
+ (st.mday == -1 || tm->tm_mday == st.mday) && (st.month == -1 || (tm->tm_mon + 1) == st.month) &&
(st.wday == -1 || tm->tm_wday == st.wday)) {
last_tm_ = *tm;
return true;
@@ -76,25 +67,19 @@ bool Cron::IsTimeMatch(struct tm *tm) {
return false;
}
-bool Cron::IsEnabled() {
- return !schedulers_.empty();
-}
+bool Cron::IsEnabled() { return !schedulers_.empty(); }
std::string Cron::ToString() {
std::string ret;
for (size_t i = 0; i < schedulers_.size(); i++) {
ret += schedulers_[i].ToString();
- if (i != schedulers_.size()-1) ret += " ";
+ if (i != schedulers_.size() - 1) ret += " ";
}
return ret;
}
-Status Cron::convertToScheduleTime(const std::string &minute,
- const std::string &hour,
- const std::string &mday,
- const std::string &month,
- const std::string &wday,
- Scheduler *st) {
+Status Cron::convertToScheduleTime(const std::string &minute, const std::string &hour, const std::string &mday,
+ const std::string &month, const std::string &wday, Scheduler *st) {
Status s;
s = convertParam(minute, 0, 59, &st->minute);
if (!s.IsOK()) return s;
@@ -126,4 +111,3 @@ Status Cron::convertParam(const std::string ¶m, int lower_bound, int upper_b
}
return Status::OK();
}
-
diff --git a/src/cron.h b/src/cron.h
index b74e9e8..e301468 100644
--- a/src/cron.h
+++ b/src/cron.h
@@ -22,8 +22,9 @@
#include <ctime>
#include <iostream>
-#include <vector>
#include <string>
+#include <vector>
+
#include "status.h"
struct Scheduler {
@@ -50,12 +51,7 @@ class Cron {
std::vector<Scheduler> schedulers_;
struct tm last_tm_ = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr};
- Status convertToScheduleTime(
- const std::string &minute,
- const std::string &hour,
- const std::string &mday,
- const std::string &month,
- const std::string &wday,
- Scheduler *st);
+ Status convertToScheduleTime(const std::string &minute, const std::string &hour, const std::string &mday,
+ const std::string &month, const std::string &wday, Scheduler *st);
Status convertParam(const std::string ¶m, int lower_bound, int upper_bound, int *value);
};
diff --git a/src/db_util.h b/src/db_util.h
index d9ded71..fb330b3 100644
--- a/src/db_util.h
+++ b/src/db_util.h
@@ -30,11 +30,10 @@ namespace DBUtil {
struct UniqueIterator : std::unique_ptr<rocksdb::Iterator> {
using base_type = std::unique_ptr<rocksdb::Iterator>;
- explicit UniqueIterator(rocksdb::Iterator *iter) : base_type(iter) {}
+ explicit UniqueIterator(rocksdb::Iterator* iter) : base_type(iter) {}
UniqueIterator(rocksdb::DB* db, const rocksdb::ReadOptions& options, rocksdb::ColumnFamilyHandle* column_family)
- : base_type(db->NewIterator(options, column_family)) {}
- UniqueIterator(rocksdb::DB* db, const rocksdb::ReadOptions& options)
- : base_type(db->NewIterator(options)) {}
+ : base_type(db->NewIterator(options, column_family)) {}
+ UniqueIterator(rocksdb::DB* db, const rocksdb::ReadOptions& options) : base_type(db->NewIterator(options)) {}
};
} // namespace DBUtil
diff --git a/src/encoding.cc b/src/encoding.cc
index 01678ac..c029286 100644
--- a/src/encoding.cc
+++ b/src/encoding.cc
@@ -20,11 +20,11 @@
#include "encoding.h"
-#include <limits.h>
#include <float.h>
-#include <unistd.h>
+#include <limits.h>
#include <stdint.h>
#include <string.h>
+#include <unistd.h>
/* Byte ordering detection */
#include <sys/types.h> /* This will likely define BYTE_ORDER */
@@ -34,37 +34,32 @@
#ifndef BYTE_ORDER
#if (BSD >= 199103)
-# include <machine/endian.h>
+#include <machine/endian.h>
#else
#if defined(linux) || defined(__linux__)
-# include <endian.h>
+#include <endian.h>
#else
-#define LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */
-#define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */
-#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp)*/
-
-#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \
- defined(vax) || defined(ns32000) || defined(sun386) || \
- defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \
- defined(__alpha__) || defined(__alpha)
-#define BYTE_ORDER LITTLE_ENDIAN
+#define LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */
+#define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */
+#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp)*/
+
+#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || defined(vax) || defined(ns32000) || \
+ defined(sun386) || defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || defined(__alpha__) || \
+ defined(__alpha)
+#define BYTE_ORDER LITTLE_ENDIAN
#endif
-#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \
- defined(vax) || defined(ns32000) || defined(sun386) || \
- defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \
- defined(__alpha__) || defined(__alpha)
-#define BYTE_ORDER LITTLE_ENDIAN
+#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || defined(vax) || defined(ns32000) || \
+ defined(sun386) || defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || defined(__alpha__) || \
+ defined(__alpha)
+#define BYTE_ORDER LITTLE_ENDIAN
#endif
-#if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \
- defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \
- defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\
- defined(apollo) || defined(__convex__) || defined(_CRAY) || \
- defined(__hppa) || defined(__hp9000) || \
- defined(__hp9000s300) || defined(__hp9000s700) || \
- defined(BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc)
-#define BYTE_ORDER BIG_ENDIAN
+#if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || defined(is68k) || defined(tahoe) || \
+ defined(ibm032) || defined(ibm370) || defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) || \
+ defined(apollo) || defined(__convex__) || defined(_CRAY) || defined(__hppa) || defined(__hp9000) || \
+ defined(__hp9000s300) || defined(__hp9000s700) || defined(BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc)
+#define BYTE_ORDER BIG_ENDIAN
#endif
#endif /* linux */
#endif /* BSD */
@@ -92,19 +87,16 @@
#endif
#endif
-#if !defined(BYTE_ORDER) || \
- (BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN)
+#if !defined(BYTE_ORDER) || (BYTE_ORDER != BIG_ENDIAN && BYTE_ORDER != LITTLE_ENDIAN)
/* you must determine what the correct bit order is for
- * your compiler - the next line is an intentional error
- * which will force your compiles to bomb until you fix
- * the above macros.
- */
+ * your compiler - the next line is an intentional error
+ * which will force your compiles to bomb until you fix
+ * the above macros.
+ */
#error "Undefined or invalid BYTE_ORDER"
#endif
-void EncodeFixed8(char *buf, uint8_t value) {
- buf[0] = static_cast<uint8_t>(value & 0xff);
-}
+void EncodeFixed8(char *buf, uint8_t value) { buf[0] = static_cast<uint8_t>(value & 0xff); }
void EncodeFixed16(char *buf, uint16_t value) {
if (BYTE_ORDER == BIG_ENDIAN) {
@@ -230,8 +222,8 @@ uint16_t DecodeFixed16(const char *ptr) {
memcpy(&value, ptr, sizeof(value));
return value;
} else {
- return ((static_cast<uint16_t>(static_cast<uint8_t>(ptr[1])))
- | (static_cast<uint16_t>(static_cast<uint8_t>(ptr[0])) << 8));
+ return ((static_cast<uint16_t>(static_cast<uint8_t>(ptr[1]))) |
+ (static_cast<uint16_t>(static_cast<uint8_t>(ptr[0])) << 8));
}
}
@@ -241,10 +233,10 @@ uint32_t DecodeFixed32(const char *ptr) {
memcpy(&value, ptr, sizeof(value));
return value;
} else {
- return ((static_cast<uint32_t>(static_cast<uint8_t>(ptr[3])))
- | (static_cast<uint32_t>(static_cast<uint8_t>(ptr[2])) << 8)
- | (static_cast<uint32_t>(static_cast<uint8_t>(ptr[1])) << 16)
- | (static_cast<uint32_t>(static_cast<uint8_t>(ptr[0])) << 24));
+ return ((static_cast<uint32_t>(static_cast<uint8_t>(ptr[3]))) |
+ (static_cast<uint32_t>(static_cast<uint8_t>(ptr[2])) << 8) |
+ (static_cast<uint32_t>(static_cast<uint8_t>(ptr[1])) << 16) |
+ (static_cast<uint32_t>(static_cast<uint8_t>(ptr[0])) << 24));
}
}
@@ -255,14 +247,14 @@ uint64_t DecodeFixed64(const char *ptr) {
return value;
} else {
uint64_t hi = DecodeFixed32(ptr);
- uint64_t lo = DecodeFixed32(ptr+4);
+ uint64_t lo = DecodeFixed32(ptr + 4);
return (hi << 32) | lo;
}
}
double DecodeDouble(const char *ptr) {
uint64_t decoded = DecodeFixed64(ptr);
- if ((decoded>>63) == 0) {
+ if ((decoded >> 63) == 0) {
decoded ^= 0xffffffffffffffff;
} else {
decoded &= 0x7fffffffffffffff;
@@ -272,24 +264,24 @@ double DecodeDouble(const char *ptr) {
return value;
}
-char* EncodeVarint32(char *dst, uint32_t v) {
+char *EncodeVarint32(char *dst, uint32_t v) {
// Operate on characters as unsigneds
- unsigned char* ptr = reinterpret_cast<unsigned char*>(dst);
+ unsigned char *ptr = reinterpret_cast<unsigned char *>(dst);
do {
*ptr = 0x80 | v;
v >>= 7, ++ptr;
} while (v != 0);
*(ptr - 1) &= 0x7F;
- return reinterpret_cast<char*>(ptr);
+ return reinterpret_cast<char *>(ptr);
}
void PutVarint32(std::string *dst, uint32_t v) {
char buf[5];
- char* ptr = EncodeVarint32(buf, v);
+ char *ptr = EncodeVarint32(buf, v);
dst->append(buf, static_cast<size_t>(ptr - buf));
}
-const char* GetVarint32PtrFallback(const char *p, const char *limit, uint32_t *value) {
+const char *GetVarint32PtrFallback(const char *p, const char *limit, uint32_t *value) {
uint32_t result = 0;
for (uint32_t shift = 0; shift <= 28 && p < limit; shift += 7) {
uint32_t byte = static_cast<unsigned char>(*p);
@@ -306,7 +298,7 @@ const char* GetVarint32PtrFallback(const char *p, const char *limit, uint32_t *v
return nullptr;
}
-const char* GetVarint32Ptr(const char *p, const char *limit, uint32_t *value) {
+const char *GetVarint32Ptr(const char *p, const char *limit, uint32_t *value) {
if (p < limit) {
uint32_t result = static_cast<unsigned char>(*p);
if ((result & 0x80) == 0) {
@@ -318,9 +310,9 @@ const char* GetVarint32Ptr(const char *p, const char *limit, uint32_t *value) {
}
bool GetVarint32(rocksdb::Slice *input, uint32_t *value) {
- const char* p = input->data();
- const char* limit = p + input->size();
- const char* q = GetVarint32Ptr(p, limit, value);
+ const char *p = input->data();
+ const char *limit = p + input->size();
+ const char *q = GetVarint32Ptr(p, limit, value);
if (q == nullptr) {
return false;
} else {
diff --git a/src/encoding.h b/src/encoding.h
index cfe71f8..508b788 100644
--- a/src/encoding.h
+++ b/src/encoding.h
@@ -21,8 +21,8 @@
#pragma once
#include <rocksdb/slice.h>
-
#include <unistd.h>
+
#include <string>
bool GetFixed8(rocksdb::Slice *input, uint8_t *value);
@@ -46,6 +46,6 @@ uint64_t DecodeFixed64(const char *ptr);
double DecodeDouble(const char *ptr);
char *EncodeVarint32(char *dst, uint32_t v);
void PutVarint32(std::string *dst, uint32_t v);
-const char* GetVarint32PtrFallback(const char *p, const char *limit, uint32_t *value);
-const char* GetVarint32Ptr(const char *p, const char *limit, uint32_t *value);
+const char *GetVarint32PtrFallback(const char *p, const char *limit, uint32_t *value);
+const char *GetVarint32Ptr(const char *p, const char *limit, uint32_t *value);
bool GetVarint32(rocksdb::Slice *input, uint32_t *value);
diff --git a/src/event_listener.cc b/src/event_listener.cc
index ae17fd9..7973be2 100644
--- a/src/event_listener.cc
+++ b/src/event_listener.cc
@@ -19,12 +19,13 @@
*/
#include "event_listener.h"
+
+#include <map>
#include <string>
#include <vector>
-#include <map>
const std::string fileCreatedReason2String(const rocksdb::TableFileCreationReason reason) {
- std::vector<std::string> file_created_reason = { "flush", "compaction", "recovery", "misc"};
+ std::vector<std::string> file_created_reason = {"flush", "compaction", "recovery", "misc"};
if (static_cast<size_t>(reason) < file_created_reason.size()) {
return file_created_reason[static_cast<size_t>(reason)];
}
@@ -40,7 +41,7 @@ const std::string stallConditionType2String(const rocksdb::WriteStallCondition t
}
const std::string compressType2String(const rocksdb::CompressionType type) {
- std::map<rocksdb::CompressionType, std::string>compression_type_string_map = {
+ std::map<rocksdb::CompressionType, std::string> compression_type_string_map = {
{rocksdb::kNoCompression, "no"},
{rocksdb::kSnappyCompression, "snappy"},
{rocksdb::kZlibCompression, "zlib"},
@@ -50,8 +51,7 @@ const std::string compressType2String(const rocksdb::CompressionType type) {
{rocksdb::kXpressCompression, "xpress"},
{rocksdb::kZSTD, "zstd"},
{rocksdb::kZSTDNotFinalCompression, "zstd_not_final"},
- {rocksdb::kDisableCompressionOption, "disable"}
- };
+ {rocksdb::kDisableCompressionOption, "disable"}};
auto iter = compression_type_string_map.find(type);
if (iter == compression_type_string_map.end()) {
return "unknown";
@@ -60,11 +60,11 @@ const std::string compressType2String(const rocksdb::CompressionType type) {
}
bool isDiskQuotaExceeded(const rocksdb::Status &bg_error) {
- // EDQUOT: Disk quota exceeded (POSIX.1-2001)
- std::string exceeded_quota_str = "Disk quota exceeded";
- std::string err_msg = bg_error.ToString();
+ // EDQUOT: Disk quota exceeded (POSIX.1-2001)
+ std::string exceeded_quota_str = "Disk quota exceeded";
+ std::string err_msg = bg_error.ToString();
- return err_msg.find(exceeded_quota_str) != std::string::npos;
+ return err_msg.find(exceeded_quota_str) != std::string::npos;
}
void EventListener::OnCompactionCompleted(rocksdb::DB *db, const rocksdb::CompactionJobInfo &ci) {
@@ -73,31 +73,27 @@ void EventListener::OnCompactionCompleted(rocksdb::DB *db, const rocksdb::Compac
<< ", output compression type: " << compressType2String(ci.compression)
<< ", base input level(files): " << ci.base_input_level << "(" << ci.input_files.size() << ")"
<< ", output level(files): " << ci.output_level << "(" << ci.output_files.size() << ")"
- << ", input bytes: " << ci.stats.total_input_bytes
- << ", output bytes:" << ci.stats.total_output_bytes
- << ", is_manual_compaction:" << (ci.stats.is_manual_compaction ? "yes":"no")
+ << ", input bytes: " << ci.stats.total_input_bytes << ", output bytes:" << ci.stats.total_output_bytes
+ << ", is_manual_compaction:" << (ci.stats.is_manual_compaction ? "yes" : "no")
<< ", elapsed(micro): " << ci.stats.elapsed_micros;
storage_->IncrCompactionCount(1);
storage_->CheckDBSizeLimit();
}
void EventListener::OnFlushBegin(rocksdb::DB *db, const rocksdb::FlushJobInfo &fi) {
- LOG(INFO) << "[event_listener/flush_begin] column family: " << fi.cf_name
- << ", thread_id: " << fi.thread_id << ", job_id: " << fi.job_id
- << ", reason: " << static_cast<int>(fi.flush_reason);
+ LOG(INFO) << "[event_listener/flush_begin] column family: " << fi.cf_name << ", thread_id: " << fi.thread_id
+ << ", job_id: " << fi.job_id << ", reason: " << static_cast<int>(fi.flush_reason);
}
void EventListener::OnFlushCompleted(rocksdb::DB *db, const rocksdb::FlushJobInfo &fi) {
storage_->IncrFlushCount(1);
storage_->CheckDBSizeLimit();
- LOG(INFO) << "[event_listener/flush_completed] column family: " << fi.cf_name
- << ", thread_id: " << fi.thread_id << ", job_id: " << fi.job_id
- << ", file: " << fi.file_path
+ LOG(INFO) << "[event_listener/flush_completed] column family: " << fi.cf_name << ", thread_id: " << fi.thread_id
+ << ", job_id: " << fi.job_id << ", file: " << fi.file_path
<< ", reason: " << static_cast<int>(fi.flush_reason)
<< ", is_write_slowdown: " << (fi.triggered_writes_slowdown ? "yes" : "no")
- << ", is_write_stall: " << (fi.triggered_writes_stop? "yes" : "no")
- << ", largest seqno: " << fi.largest_seqno
- << ", smallest seqno: " << fi.smallest_seqno;
+ << ", is_write_stall: " << (fi.triggered_writes_stop ? "yes" : "no")
+ << ", largest seqno: " << fi.largest_seqno << ", smallest seqno: " << fi.smallest_seqno;
}
void EventListener::OnBackgroundError(rocksdb::BackgroundErrorReason reason, rocksdb::Status *bg_error) {
@@ -124,28 +120,22 @@ void EventListener::OnBackgroundError(rocksdb::BackgroundErrorReason reason, roc
storage_->SetDBInRetryableIOError(true);
}
- LOG(ERROR) << "[event_listener/background_error] reason: " << reason_str
- << ", bg_error: " << bg_error->ToString();
+ LOG(ERROR) << "[event_listener/background_error] reason: " << reason_str << ", bg_error: " << bg_error->ToString();
}
void EventListener::OnTableFileDeleted(const rocksdb::TableFileDeletionInfo &info) {
- LOG(INFO) << "[event_listener/table_file_deleted] db: " << info.db_name
- << ", sst file: " << info.file_path
+ LOG(INFO) << "[event_listener/table_file_deleted] db: " << info.db_name << ", sst file: " << info.file_path
<< ", status: " << info.status.ToString();
}
void EventListener::OnStallConditionsChanged(const rocksdb::WriteStallInfo &info) {
LOG(WARNING) << "[event_listener/stall_cond_changed] column family: " << info.cf_name
- << " write stall condition was changed, from "
- << stallConditionType2String(info.condition.prev)
+ << " write stall condition was changed, from " << stallConditionType2String(info.condition.prev)
<< " to " << stallConditionType2String(info.condition.cur);
}
void EventListener::OnTableFileCreated(const rocksdb::TableFileCreationInfo &info) {
LOG(INFO) << "[event_listener/table_file_created] column family: " << info.cf_name
- << ", file path: " << info.file_path
- << ", file size: " << info.file_size
- << ", job id: " << info.job_id
- << ", reason: " << fileCreatedReason2String(info.reason)
- << ", status: " << info.status.ToString();
+ << ", file path: " << info.file_path << ", file size: " << info.file_size << ", job id: " << info.job_id
+ << ", reason: " << fileCreatedReason2String(info.reason) << ", status: " << info.status.ToString();
}
diff --git a/src/event_listener.h b/src/event_listener.h
index 4683d2e..79d8d21 100644
--- a/src/event_listener.h
+++ b/src/event_listener.h
@@ -29,13 +29,14 @@ class EventListener : public rocksdb::EventListener {
public:
explicit EventListener(Engine::Storage *storage) : storage_(storage) {}
~EventListener() override = default;
- void OnFlushBegin(rocksdb::DB* db, const rocksdb::FlushJobInfo& fi) override;
+ void OnFlushBegin(rocksdb::DB *db, const rocksdb::FlushJobInfo &fi) override;
void OnFlushCompleted(rocksdb::DB *db, const rocksdb::FlushJobInfo &fi) override;
void OnCompactionCompleted(rocksdb::DB *db, const rocksdb::CompactionJobInfo &ci) override;
void OnBackgroundError(rocksdb::BackgroundErrorReason reason, rocksdb::Status *status) override;
- void OnTableFileDeleted(const rocksdb::TableFileDeletionInfo& info) override;
- void OnStallConditionsChanged(const rocksdb::WriteStallInfo& info) override;
- void OnTableFileCreated(const rocksdb::TableFileCreationInfo& info) override;
+ void OnTableFileDeleted(const rocksdb::TableFileDeletionInfo &info) override;
+ void OnStallConditionsChanged(const rocksdb::WriteStallInfo &info) override;
+ void OnTableFileCreated(const rocksdb::TableFileCreationInfo &info) override;
+
private:
Engine::Storage *storage_ = nullptr;
};
diff --git a/src/event_util.h b/src/event_util.h
index 81c06b5..b63964b 100644
--- a/src/event_util.h
+++ b/src/event_util.h
@@ -20,14 +20,16 @@
#pragma once
-#include <memory>
#include <cstdlib>
+#include <memory>
#include <utility>
#include "event2/buffer.h"
-template <typename F, F *f> struct StaticFunction {
- template <typename... Ts> auto operator()(Ts &&...args) const -> decltype(f(std::forward<Ts>(args)...)) {
+template <typename F, F *f>
+struct StaticFunction {
+ template <typename... Ts>
+ auto operator()(Ts &&...args) const -> decltype(f(std::forward<Ts>(args)...)) {
return f(std::forward<Ts>(args)...);
}
};
@@ -42,11 +44,11 @@ struct UniqueFreePtr : std::unique_ptr<T, StaticFree> {
};
struct UniqueEvbufReadln : UniqueFreePtr<char[]> {
- // cppcheck-suppress uninitMemberVar
- UniqueEvbufReadln(evbuffer* buffer, evbuffer_eol_style eol_style)
+ // cppcheck-suppress uninitMemberVar
+ UniqueEvbufReadln(evbuffer *buffer, evbuffer_eol_style eol_style)
: UniqueFreePtr(evbuffer_readln(buffer, &length, eol_style)) {}
- size_t length;
+ size_t length;
};
using StaticEvbufFree = StaticFunction<decltype(evbuffer_free), evbuffer_free>;
diff --git a/src/fd_util.h b/src/fd_util.h
index f00c29a..57fb46e 100644
--- a/src/fd_util.h
+++ b/src/fd_util.h
@@ -21,6 +21,7 @@
#pragma once
#include <unistd.h>
+
#include <utility>
constexpr const int NullFD = -1;
@@ -30,7 +31,9 @@ struct UniqueFD {
UniqueFD() : fd_(NullFD) {}
explicit UniqueFD(int fd) : fd_(fd) {}
- ~UniqueFD() { if (fd_ != NullFD) close(fd_); }
+ ~UniqueFD() {
+ if (fd_ != NullFD) close(fd_);
+ }
UniqueFD(const UniqueFD&) = delete;
UniqueFD(UniqueFD&& f) : fd_(f.fd_) { f.fd_ = NullFD; }
@@ -54,25 +57,17 @@ struct UniqueFD {
if (old_fd != NullFD) close(old_fd);
}
- void Close() {
- Reset();
- }
+ void Close() { Reset(); }
- void Swap(UniqueFD& other) {
- std::swap(fd_, other.fd_);
- }
+ void Swap(UniqueFD& other) { std::swap(fd_, other.fd_); }
int Get() const { return fd_; }
int operator*() const { return fd_; }
explicit operator bool() const { return fd_ != NullFD; }
- bool operator==(const UniqueFD& f) const {
- return fd_ == f.fd_;
- }
+ bool operator==(const UniqueFD& f) const { return fd_ == f.fd_; }
- bool operator!=(const UniqueFD& f) const {
- return !(*this == f);
- }
+ bool operator!=(const UniqueFD& f) const { return !(*this == f); }
private:
int fd_;
diff --git a/src/geohash.cc b/src/geohash.cc
index 8bb7ca8..b78041f 100644
--- a/src/geohash.cc
+++ b/src/geohash.cc
@@ -56,13 +56,14 @@
*/
#include "geohash.h"
+
#include <math.h>
#define D_R (M_PI / 180.0)
#define R_MAJOR 6378137.0
#define R_MINOR 6356752.3142
#define RATIO (R_MINOR / R_MAJOR)
-#define ECCENT (sqrt(1.0 - (RATIO *RATIO)))
+#define ECCENT (sqrt(1.0 - (RATIO * RATIO)))
#define COM (0.5 * ECCENT)
// @brief The usual PI/180 constant
@@ -96,9 +97,8 @@ static inline double rad_deg(double ang) { return ang / D_R; }
* From: https://graphics.stanford.edu/~seander/bithacks.html#InterleaveBMN
*/
static inline uint64_t interleave64(uint32_t xlo, uint32_t ylo) {
- static const uint64_t B[] = {0x5555555555555555ULL, 0x3333333333333333ULL,
- 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
- 0x0000FFFF0000FFFFULL};
+ static const uint64_t B[] = {0x5555555555555555ULL, 0x3333333333333333ULL, 0x0F0F0F0F0F0F0F0FULL,
+ 0x00FF00FF00FF00FFULL, 0x0000FFFF0000FFFFULL};
static const unsigned int S[] = {1, 2, 4, 8, 16};
uint64_t x = xlo;
@@ -126,9 +126,8 @@ static inline uint64_t interleave64(uint32_t xlo, uint32_t ylo) {
* derived from http://stackoverflow.com/questions/4909263
*/
static inline uint64_t deinterleave64(uint64_t interleaved) {
- static const uint64_t B[] = {0x5555555555555555ULL, 0x3333333333333333ULL,
- 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
- 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL};
+ static const uint64_t B[] = {0x5555555555555555ULL, 0x3333333333333333ULL, 0x0F0F0F0F0F0F0F0FULL,
+ 0x00FF00FF00FF00FFULL, 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL};
static const unsigned int S[] = {0, 1, 2, 4, 8, 16};
uint64_t x = interleaved;
@@ -164,32 +163,26 @@ void geohashGetCoordRange(GeoHashRange *long_range, GeoHashRange *lat_range) {
lat_range->min = GEO_LAT_MIN;
}
-int geohashEncode(const GeoHashRange *long_range, const GeoHashRange *lat_range,
- double longitude, double latitude, uint8_t step,
- GeoHashBits *hash) {
+int geohashEncode(const GeoHashRange *long_range, const GeoHashRange *lat_range, double longitude, double latitude,
+ uint8_t step, GeoHashBits *hash) {
/* Check basic arguments sanity. */
- if (hash == NULL || step > 32 || step == 0 ||
- RANGEPISZERO(lat_range) || RANGEPISZERO(long_range))
- return 0;
+ if (hash == NULL || step > 32 || step == 0 || RANGEPISZERO(lat_range) || RANGEPISZERO(long_range)) return 0;
/* Return an error when trying to index outside the supported
* constraints. */
- if (longitude > GEO_LONG_MAX || longitude < GEO_LONG_MIN ||
- latitude > GEO_LAT_MAX || latitude < GEO_LAT_MIN)
+ if (longitude > GEO_LONG_MAX || longitude < GEO_LONG_MIN || latitude > GEO_LAT_MAX || latitude < GEO_LAT_MIN)
return 0;
hash->bits = 0;
hash->step = step;
- if (latitude < lat_range->min || latitude > lat_range->max ||
- longitude < long_range->min || longitude > long_range->max) {
+ if (latitude < lat_range->min || latitude > lat_range->max || longitude < long_range->min ||
+ longitude > long_range->max) {
return 0;
}
- double lat_offset =
- (latitude - lat_range->min) / (lat_range->max - lat_range->min);
- double long_offset =
- (longitude - long_range->min) / (long_range->max - long_range->min);
+ double lat_offset = (latitude - lat_range->min) / (lat_range->max - lat_range->min);
+ double long_offset = (longitude - long_range->min) / (long_range->max - long_range->min);
/* convert to fixed point based on the step size */
lat_offset *= (1ULL << step);
@@ -204,15 +197,13 @@ int geohashEncodeType(double longitude, double latitude, uint8_t step, GeoHashBi
return geohashEncode(&r[0], &r[1], longitude, latitude, step, hash);
}
-int geohashEncodeWGS84(double longitude, double latitude, uint8_t step,
- GeoHashBits *hash) {
+int geohashEncodeWGS84(double longitude, double latitude, uint8_t step, GeoHashBits *hash) {
return geohashEncodeType(longitude, latitude, step, hash);
}
-int geohashDecode(const GeoHashRange &long_range, const GeoHashRange &lat_range,
- const GeoHashBits &hash, GeoHashArea *area) {
- if (HASHISZERO(hash) || NULL == area || RANGEISZERO(lat_range) ||
- RANGEISZERO(long_range)) {
+int geohashDecode(const GeoHashRange &long_range, const GeoHashRange &lat_range, const GeoHashBits &hash,
+ GeoHashArea *area) {
+ if (HASHISZERO(hash) || NULL == area || RANGEISZERO(lat_range) || RANGEISZERO(long_range)) {
return 0;
}
@@ -229,14 +220,10 @@ int geohashDecode(const GeoHashRange &long_range, const GeoHashRange &lat_range,
/* divide by 2**step.
* Then, for 0-1 coordinate, multiply times scale and add
to the min to get the absolute coordinate. */
- area->latitude.min =
- lat_range.min + (ilato * 1.0 / (1ull << step)) * lat_scale;
- area->latitude.max =
- lat_range.min + ((ilato + 1) * 1.0 / (1ull << step)) * lat_scale;
- area->longitude.min =
- long_range.min + (ilono * 1.0 / (1ull << step)) * long_scale;
- area->longitude.max =
- long_range.min + ((ilono + 1) * 1.0 / (1ull << step)) * long_scale;
+ area->latitude.min = lat_range.min + (ilato * 1.0 / (1ull << step)) * lat_scale;
+ area->latitude.max = lat_range.min + ((ilato + 1) * 1.0 / (1ull << step)) * lat_scale;
+ area->longitude.min = long_range.min + (ilono * 1.0 / (1ull << step)) * long_scale;
+ area->longitude.max = long_range.min + ((ilono + 1) * 1.0 / (1ull << step)) * long_scale;
return 1;
}
@@ -247,9 +234,7 @@ int geohashDecodeType(const GeoHashBits &hash, GeoHashArea *area) {
return geohashDecode(r[0], r[1], hash, area);
}
-int geohashDecodeWGS84(const GeoHashBits &hash, GeoHashArea *area) {
- return geohashDecodeType(hash, area);
-}
+int geohashDecodeWGS84(const GeoHashBits &hash, GeoHashArea *area) { return geohashDecodeType(hash, area); }
int geohashDecodeAreaToLongLat(const GeoHashArea *area, double *xy) {
if (!xy) return 0;
@@ -264,18 +249,14 @@ int geohashDecodeAreaToLongLat(const GeoHashArea *area, double *xy) {
int geohashDecodeToLongLatType(const GeoHashBits &hash, double *xy) {
GeoHashArea area = {{0}};
- if (!xy || !geohashDecodeType(hash, &area))
- return 0;
+ if (!xy || !geohashDecodeType(hash, &area)) return 0;
return geohashDecodeAreaToLongLat(&area, xy);
}
-int geohashDecodeToLongLatWGS84(const GeoHashBits &hash, double *xy) {
- return geohashDecodeToLongLatType(hash, xy);
-}
+int geohashDecodeToLongLatWGS84(const GeoHashBits &hash, double *xy) { return geohashDecodeToLongLatType(hash, xy); }
static void geohash_move_x(GeoHashBits *hash, int8_t d) {
- if (d == 0)
- return;
+ if (d == 0) return;
uint64_t x = hash->bits & 0xaaaaaaaaaaaaaaaaULL;
uint64_t y = hash->bits & 0x5555555555555555ULL;
@@ -294,8 +275,7 @@ static void geohash_move_x(GeoHashBits *hash, int8_t d) {
}
static void geohash_move_y(GeoHashBits *hash, int8_t d) {
- if (d == 0)
- return;
+ if (d == 0) return;
uint64_t x = hash->bits & 0xaaaaaaaaaaaaaaaaULL;
uint64_t y = hash->bits & 0x5555555555555555ULL;
@@ -389,8 +369,7 @@ uint8_t GeoHashHelper::EstimateStepsByRadius(double range_meters, double lat) {
* Since this function is currently only used as an optimization, the
* optimization is not used for very big radiuses, however the function
* should be fixed. */
-int GeoHashHelper::BoundingBox(double longitude, double latitude, double radius_meters,
- double *bounds) {
+int GeoHashHelper::BoundingBox(double longitude, double latitude, double radius_meters, double *bounds) {
if (!bounds) return 0;
bounds[0] = longitude - rad_deg(radius_meters / EARTH_RADIUS_IN_METERS / cos(deg_rad(latitude)));
@@ -439,18 +418,10 @@ GeoHashRadius GeoHashHelper::GetAreasByRadius(double longitude, double latitude,
geohashDecode(long_range, lat_range, neighbors.east, &east);
geohashDecode(long_range, lat_range, neighbors.west, &west);
- if (GetDistance(longitude, latitude, longitude, north.latitude.max)
- < radius_meters)
- decrease_step = 1;
- if (GetDistance(longitude, latitude, longitude, south.latitude.min)
- < radius_meters)
- decrease_step = 1;
- if (GetDistance(longitude, latitude, east.longitude.max, latitude)
- < radius_meters)
- decrease_step = 1;
- if (GetDistance(longitude, latitude, west.longitude.min, latitude)
- < radius_meters)
- decrease_step = 1;
+ if (GetDistance(longitude, latitude, longitude, north.latitude.max) < radius_meters) decrease_step = 1;
+ if (GetDistance(longitude, latitude, longitude, south.latitude.min) < radius_meters) decrease_step = 1;
+ if (GetDistance(longitude, latitude, east.longitude.max, latitude) < radius_meters) decrease_step = 1;
+ if (GetDistance(longitude, latitude, west.longitude.min, latitude) < radius_meters) decrease_step = 1;
}
if (steps > 1 && decrease_step) {
@@ -489,8 +460,7 @@ GeoHashRadius GeoHashHelper::GetAreasByRadius(double longitude, double latitude,
return radius;
}
-GeoHashRadius GeoHashHelper::GetAreasByRadiusWGS84(double longitude, double latitude,
- double radius_meters) {
+GeoHashRadius GeoHashHelper::GetAreasByRadiusWGS84(double longitude, double latitude, double radius_meters) {
return GetAreasByRadius(longitude, latitude, radius_meters);
}
@@ -509,20 +479,16 @@ double GeoHashHelper::GetDistance(double lon1d, double lat1d, double lon2d, doub
lon2r = deg_rad(lon2d);
u = sin((lat2r - lat1r) / 2);
v = sin((lon2r - lon1r) / 2);
- return 2.0 * EARTH_RADIUS_IN_METERS *
- asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));
+ return 2.0 * EARTH_RADIUS_IN_METERS * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));
}
-int GeoHashHelper::GetDistanceIfInRadius(double x1, double y1,
- double x2, double y2, double radius,
- double *distance) {
+int GeoHashHelper::GetDistanceIfInRadius(double x1, double y1, double x2, double y2, double radius, double *distance) {
*distance = GetDistance(x1, y1, x2, y2);
if (*distance > radius) return 0;
return 1;
}
-int GeoHashHelper::GetDistanceIfInRadiusWGS84(double x1, double y1, double x2,
- double y2, double radius,
+int GeoHashHelper::GetDistanceIfInRadiusWGS84(double x1, double y1, double x2, double y2, double radius,
double *distance) {
return GetDistanceIfInRadius(x1, y1, x2, y2, radius, distance);
}
diff --git a/src/geohash.h b/src/geohash.h
index 3e8d00c..6ae8b80 100644
--- a/src/geohash.h
+++ b/src/geohash.h
@@ -127,15 +127,12 @@ typedef struct {
* -1:failed
*/
void geohashGetCoordRange(GeoHashRange *long_range, GeoHashRange *lat_range);
-int geohashEncode(const GeoHashRange *long_range, const GeoHashRange *lat_range,
- double longitude, double latitude, uint8_t step,
- GeoHashBits *hash);
-int geohashEncodeType(double longitude, double latitude,
- uint8_t step, GeoHashBits *hash);
-int geohashEncodeWGS84(double longitude, double latitude, uint8_t step,
- GeoHashBits *hash);
-int geohashDecode(const GeoHashRange &long_range, const GeoHashRange &lat_range,
- const GeoHashBits &hash, GeoHashArea *area);
+int geohashEncode(const GeoHashRange *long_range, const GeoHashRange *lat_range, double longitude, double latitude,
+ uint8_t step, GeoHashBits *hash);
+int geohashEncodeType(double longitude, double latitude, uint8_t step, GeoHashBits *hash);
+int geohashEncodeWGS84(double longitude, double latitude, uint8_t step, GeoHashBits *hash);
+int geohashDecode(const GeoHashRange &long_range, const GeoHashRange &lat_range, const GeoHashBits &hash,
+ GeoHashArea *area);
int geohashDecodeType(const GeoHashBits &hash, GeoHashArea *area);
int geohashDecodeAreaToLongLat(const GeoHashArea *area, double *xy);
int geohashDecodeToLongLatType(const GeoHashBits &hash, double *xy);
@@ -145,20 +142,11 @@ void geohashNeighbors(const GeoHashBits *hash, GeoHashNeighbors *neighbors);
class GeoHashHelper {
public:
static uint8_t EstimateStepsByRadius(double range_meters, double lat);
- static int BoundingBox(double longitude, double latitude, double radius_meters,
- double *bounds);
- static GeoHashRadius GetAreasByRadius(double longitude,
- double latitude, double radius_meters);
- static GeoHashRadius GetAreasByRadiusWGS84(double longitude, double latitude,
- double radius_meters);
+ static int BoundingBox(double longitude, double latitude, double radius_meters, double *bounds);
+ static GeoHashRadius GetAreasByRadius(double longitude, double latitude, double radius_meters);
+ static GeoHashRadius GetAreasByRadiusWGS84(double longitude, double latitude, double radius_meters);
static GeoHashFix52Bits Align52Bits(const GeoHashBits &hash);
- static double GetDistance(double lon1d, double lat1d,
- double lon2d, double lat2d);
- static int GetDistanceIfInRadius(double x1, double y1,
- double x2, double y2, double radius,
- double *distance);
- static int GetDistanceIfInRadiusWGS84(double x1, double y1, double x2,
- double y2, double radius,
- double *distance);
+ static double GetDistance(double lon1d, double lat1d, double lon2d, double lat2d);
+ static int GetDistanceIfInRadius(double x1, double y1, double x2, double y2, double radius, double *distance);
+ static int GetDistanceIfInRadiusWGS84(double x1, double y1, double x2, double y2, double radius, double *distance);
};
-
diff --git a/src/lock_manager.cc b/src/lock_manager.cc
index 91413d1..49fc52e 100644
--- a/src/lock_manager.cc
+++ b/src/lock_manager.cc
@@ -20,11 +20,11 @@
#include "lock_manager.h"
-#include <thread>
-#include <string>
#include <set>
+#include <string>
+#include <thread>
-LockManager::LockManager(int hash_power): hash_power_(hash_power) {
+LockManager::LockManager(int hash_power) : hash_power_(hash_power) {
hash_mask_ = (1U << hash_power) - 1;
for (unsigned i = 0; i < Size(); i++) {
mutex_pool_.emplace_back(new std::mutex());
@@ -41,17 +41,11 @@ unsigned LockManager::hash(const rocksdb::Slice &key) {
return static_cast<unsigned>(std::hash<std::string>{}(key.ToString()) & hash_mask_);
}
-unsigned LockManager::Size() {
- return (1U << hash_power_);
-}
+unsigned LockManager::Size() { return (1U << hash_power_); }
-void LockManager::Lock(const rocksdb::Slice &key) {
- mutex_pool_[hash(key)]->lock();
-}
+void LockManager::Lock(const rocksdb::Slice &key) { mutex_pool_[hash(key)]->lock(); }
-void LockManager::UnLock(const rocksdb::Slice &key) {
- mutex_pool_[hash(key)]->unlock();
-}
+void LockManager::UnLock(const rocksdb::Slice &key) { mutex_pool_[hash(key)]->unlock(); }
std::vector<std::mutex *> LockManager::MultiGet(const std::vector<std::string> &keys) {
std::vector<std::mutex *> locks;
diff --git a/src/lock_manager.h b/src/lock_manager.h
index 41908d5..9520649 100644
--- a/src/lock_manager.h
+++ b/src/lock_manager.h
@@ -40,20 +40,17 @@ class LockManager {
private:
int hash_power_;
int hash_mask_;
- std::vector<std::mutex*> mutex_pool_;
+ std::vector<std::mutex *> mutex_pool_;
unsigned hash(const rocksdb::Slice &key);
};
class LockGuard {
public:
- explicit LockGuard(LockManager *lock_mgr, rocksdb::Slice key):
- lock_mgr_(lock_mgr),
- key_(key) {
+ explicit LockGuard(LockManager *lock_mgr, rocksdb::Slice key) : lock_mgr_(lock_mgr), key_(key) {
lock_mgr->Lock(key_);
}
- ~LockGuard() {
- lock_mgr_->UnLock(key_);
- }
+ ~LockGuard() { lock_mgr_->UnLock(key_); }
+
private:
LockManager *lock_mgr_ = nullptr;
rocksdb::Slice key_;
@@ -61,8 +58,7 @@ class LockGuard {
class MultiLockGuard {
public:
- explicit MultiLockGuard(LockManager *lock_mgr, const std::vector<std::string> &keys):
- lock_mgr_(lock_mgr) {
+ explicit MultiLockGuard(LockManager *lock_mgr, const std::vector<std::string> &keys) : lock_mgr_(lock_mgr) {
locks_ = lock_mgr_->MultiGet(keys);
for (const auto &iter : locks_) {
iter->lock();
@@ -78,5 +74,5 @@ class MultiLockGuard {
private:
LockManager *lock_mgr_ = nullptr;
- std::vector<std::mutex*> locks_;
+ std::vector<std::mutex *> locks_;
};
diff --git a/src/log_collector.cc b/src/log_collector.cc
index c9d7dff..e85ced6 100644
--- a/src/log_collector.cc
+++ b/src/log_collector.cc
@@ -18,10 +18,11 @@
*
*/
-#include <algorithm>
#include "log_collector.h"
-#include "redis_reply.h"
+#include <algorithm>
+
+#include "redis_reply.h"
std::string SlowEntry::ToRedisString() {
std::string output;
@@ -82,8 +83,7 @@ void LogCollector<T>::PushEntry(T *entry) {
std::lock_guard<std::mutex> guard(mu_);
entry->id = ++id_;
entry->time = time(nullptr);
- if (max_entries_ > 0 && !entries_.empty()
- && entries_.size() >= static_cast<size_t>(max_entries_)) {
+ if (max_entries_ > 0 && !entries_.empty() && entries_.size() >= static_cast<size_t>(max_entries_)) {
delete entries_.back();
entries_.pop_back();
}
diff --git a/src/log_collector.h b/src/log_collector.h
index 053cd07..ba71f74 100644
--- a/src/log_collector.h
+++ b/src/log_collector.h
@@ -22,12 +22,12 @@
#include <time.h>
-#include <string>
-#include <list>
-#include <vector>
-#include <mutex>
#include <cstdint>
#include <functional>
+#include <list>
+#include <mutex>
+#include <string>
+#include <vector>
class SlowEntry {
public:
@@ -67,5 +67,5 @@ class LogCollector {
std::mutex mu_;
uint64_t id_ = 0;
int64_t max_entries_ = 128;
- std::list<T*> entries_;
+ std::list<T *> entries_;
};
diff --git a/src/main.cc b/src/main.cc
index 3a57158..458c4b2 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -18,33 +18,33 @@
*
*/
+#include <dlfcn.h>
+#include <fcntl.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
-#include <fcntl.h>
-#include <dlfcn.h>
#ifdef __linux__
#define _XOPEN_SOURCE 700
#else
#define _XOPEN_SOURCE
#endif
-#include <sys/un.h>
-#include <signal.h>
-#include <execinfo.h>
-#include <ucontext.h>
#include <event2/thread.h>
+#include <execinfo.h>
#include <glog/logging.h>
-#include "worker.h"
-#include "storage.h"
-#include "version.h"
+#include <signal.h>
+#include <sys/un.h>
+#include <ucontext.h>
+
#include "config.h"
+#include "fd_util.h"
#include "server.h"
+#include "storage.h"
#include "util.h"
-#include "fd_util.h"
+#include "version.h"
namespace google {
-bool Symbolize(void* pc, char* out, size_t out_size);
+bool Symbolize(void *pc, char *out, size_t out_size);
} // namespace google
std::function<void()> hup_handler;
@@ -56,9 +56,7 @@ struct Options {
Server *srv = nullptr;
-Server *GetServer() {
- return srv;
-}
+Server *GetServer() { return srv; }
extern "C" void signal_handler(int sig) {
if (hup_handler) hup_handler();
@@ -67,8 +65,8 @@ extern "C" void signal_handler(int sig) {
extern "C" void segvHandler(int sig, siginfo_t *info, void *secret) {
void *trace[100];
- LOG(ERROR) << "======= Ooops! kvrocks "<< VERSION << " @" << GIT_COMMIT
- << " got signal: " << strsignal(sig) << " (" << sig << ") =======";
+ LOG(ERROR) << "======= Ooops! kvrocks " << VERSION << " @" << GIT_COMMIT << " got signal: " << strsignal(sig) << " ("
+ << sig << ") =======";
int trace_size = backtrace(trace, sizeof(trace) / sizeof(void *));
char **messages = backtrace_symbols(trace, trace_size);
for (int i = 1; i < trace_size; ++i) {
@@ -114,7 +112,7 @@ void setupSigSegvAction() {
sigaction(SIGINT, &act, nullptr);
}
-static void usage(const char* program) {
+static void usage(const char *program) {
std::cout << program << " implements the Redis protocol based on rocksdb\n"
<< "\t-c config file\n"
<< "\t-h help\n";
@@ -126,10 +124,16 @@ static Options parseCommandLineOptions(int argc, char **argv) {
Options opts;
while ((ch = ::getopt(argc, argv, "c:hv")) != -1) {
switch (ch) {
- case 'c': opts.conf_file = optarg; break;
- case 'h': opts.show_usage = true; break;
- case 'v': exit(0);
- default: usage(argv[0]);
+ case 'c':
+ opts.conf_file = optarg;
+ break;
+ case 'h':
+ opts.show_usage = true;
+ break;
+ case 'v':
+ exit(0);
+ default:
+ usage(argv[0]);
}
}
return opts;
@@ -179,10 +183,9 @@ bool supervisedSystemd() {
struct sockaddr_un su;
memset(&su, 0, sizeof(su));
su.sun_family = AF_UNIX;
- strncpy (su.sun_path, notify_socket, sizeof(su.sun_path) -1);
+ strncpy(su.sun_path, notify_socket, sizeof(su.sun_path) - 1);
su.sun_path[sizeof(su.sun_path) - 1] = '\0';
- if (notify_socket[0] == '@')
- su.sun_path[0] = '\0';
+ if (notify_socket[0] == '@') su.sun_path[0] = '\0';
struct iovec iov;
memset(&iov, 0, sizeof(iov));
@@ -210,12 +213,12 @@ bool supervisedSystemd() {
}
bool isSupervisedMode(int mode) {
- if (mode == SUPERVISED_AUTODETECT) {
+ if (mode == SUPERVISED_AUTODETECT) {
const char *upstart_job = getenv("UPSTART_JOB");
const char *notify_socket = getenv("NOTIFY_SOCKET");
if (upstart_job) {
mode = SUPERVISED_UPSTART;
- } else if (notify_socket) {
+ } else if (notify_socket) {
mode = SUPERVISED_SYSTEMD;
}
}
@@ -228,7 +231,7 @@ bool isSupervisedMode(int mode) {
}
static Status createPidFile(const std::string &path) {
- auto fd = UniqueFD(open(path.data(), O_RDWR|O_CREAT, 0660));
+ auto fd = UniqueFD(open(path.data(), O_RDWR | O_CREAT, 0660));
if (!fd) {
return Status(Status::NotOK, strerror(errno));
}
@@ -237,9 +240,7 @@ static Status createPidFile(const std::string &path) {
return Status::OK();
}
-static void removePidFile(const std::string &path) {
- std::remove(path.data());
-}
+static void removePidFile(const std::string &path) { std::remove(path.data()); }
static void daemonize() {
pid_t pid;
@@ -261,7 +262,7 @@ static void daemonize() {
close(STDERR_FILENO);
}
-int main(int argc, char* argv[]) {
+int main(int argc, char *argv[]) {
google::InitGoogleLogging("kvrocks");
evthread_use_pthreads();
@@ -291,8 +292,8 @@ int main(int argc, char* argv[]) {
int ports[] = {config.port, config.tls_port, 0};
for (int *port = ports; *port; ++port) {
if (Util::IsPortInUse(*port)) {
- LOG(ERROR)<< "Could not create server TCP since the specified port["
- << *port << "] is already in use" << std::endl;
+ LOG(ERROR) << "Could not create server TCP since the specified port[" << *port << "] is already in use"
+ << std::endl;
exit(1);
}
}
diff --git a/src/parse_util.h b/src/parse_util.h
index c23ebb6..5457d12 100644
--- a/src/parse_util.h
+++ b/src/parse_util.h
@@ -33,7 +33,7 @@ template <typename>
struct ParseIntFunc;
template <>
-struct ParseIntFunc<short> { // NOLINT
+struct ParseIntFunc<short> { // NOLINT
constexpr static const auto value = std::strtol;
};
@@ -43,17 +43,17 @@ struct ParseIntFunc<int> {
};
template <>
-struct ParseIntFunc<long> { // NOLINT
+struct ParseIntFunc<long> { // NOLINT
constexpr static const auto value = std::strtol;
};
template <>
-struct ParseIntFunc<long long> { // NOLINT
+struct ParseIntFunc<long long> { // NOLINT
constexpr static const auto value = std::strtoll;
};
template <>
-struct ParseIntFunc<unsigned short> { // NOLINT
+struct ParseIntFunc<unsigned short> { // NOLINT
constexpr static const auto value = std::strtoul;
};
@@ -63,12 +63,12 @@ struct ParseIntFunc<unsigned> {
};
template <>
-struct ParseIntFunc<unsigned long> { // NOLINT
+struct ParseIntFunc<unsigned long> { // NOLINT
constexpr static const auto value = std::strtoul;
};
template <>
-struct ParseIntFunc<unsigned long long> { // NOLINT
+struct ParseIntFunc<unsigned long long> { // NOLINT
constexpr static const auto value = std::strtoull;
};
@@ -83,7 +83,7 @@ using ParseResultAndPos = std::tuple<T, const char *>;
// e.g. TryParseInt("100MB") -> {100, "MB"}
// if no integer can be parsed or out of type range, an error will be returned
// base can be in {0, 2, ..., 36}, refer to strto* in standard c for more details
-template <typename T = long long> // NOLINT
+template <typename T = long long> // NOLINT
StatusOr<ParseResultAndPos<T>> TryParseInt(const char *v, int base = 0) {
char *end;
@@ -99,7 +99,7 @@ StatusOr<ParseResultAndPos<T>> TryParseInt(const char *v, int base = 0) {
}
if (!std::is_same<T, decltype(res)>::value &&
- (res < std::numeric_limits<T>::min() || res > std::numeric_limits<T>::max())) {
+ (res < std::numeric_limits<T>::min() || res > std::numeric_limits<T>::max())) {
return {Status::NotOK, "out of range of integer type"};
}
@@ -109,8 +109,8 @@ StatusOr<ParseResultAndPos<T>> TryParseInt(const char *v, int base = 0) {
// ParseInt parses a string to a integer,
// not like TryParseInt, the whole string need to be parsed as an integer,
// e.g. ParseInt("100MB") -> error status
-template <typename T = long long> // NOLINT
-StatusOr<T> ParseInt(const std::string& v, int base = 0) {
+template <typename T = long long> // NOLINT
+StatusOr<T> ParseInt(const std::string &v, int base = 0) {
const char *begin = v.c_str();
auto res = TryParseInt<T>(begin, base);
@@ -128,8 +128,8 @@ using NumericRange = std::tuple<T, T>;
// this overload accepts a range {min, max},
// integer out of the range will trigger an error status
-template <typename T = long long> // NOLINT
-StatusOr<T> ParseInt(const std::string& v, NumericRange<T> range, int base = 0) {
+template <typename T = long long> // NOLINT
+StatusOr<T> ParseInt(const std::string &v, NumericRange<T> range, int base = 0) {
auto res = ParseInt<T>(v, base);
if (!res) return res;
diff --git a/src/rand.cc b/src/rand.cc
index 48eb91f..8710777 100644
--- a/src/rand.cc
+++ b/src/rand.cc
@@ -66,38 +66,44 @@
#include <stdint.h>
#define N 16
-#define MASK ((1 << (N - 1)) + (1 << (N - 1)) - 1)
-#define LOW(x) ((unsigned)(x) & MASK)
-#define HIGH(x) LOW((x) >> N)
-#define MUL(x, y, z) { int32_t l = (int64_t)(x) * (int64_t)(y); \
- (z)[0] = LOW(l); (z)[1] = HIGH(l); }
-#define CARRY(x, y) ((int32_t)(x) + (int64_t)(y) > MASK)
-#define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y)))
-#define X0 0x330E
-#define X1 0xABCD
-#define X2 0x1234
-#define A0 0xE66D
-#define A1 0xDEEC
-#define A2 0x5
-#define C 0xB
-#define SET3(x, x0, x1, x2) ((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
-#define SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
+#define MASK ((1 << (N - 1)) + (1 << (N - 1)) - 1)
+#define LOW(x) ((unsigned)(x)&MASK)
+#define HIGH(x) LOW((x) >> N)
+#define MUL(x, y, z) \
+ { \
+ int32_t l = (int64_t)(x) * (int64_t)(y); \
+ (z)[0] = LOW(l); \
+ (z)[1] = HIGH(l); \
+ }
+#define CARRY(x, y) ((int32_t)(x) + (int64_t)(y) > MASK)
+#define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y)))
+#define X0 0x330E
+#define X1 0xABCD
+#define X2 0x1234
+#define A0 0xE66D
+#define A1 0xDEEC
+#define A2 0x5
+#define C 0xB
+#define SET3(x, x0, x1, x2) ((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
+#define SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n) + 1]), LOW((y)[(n) + 2]))
#define SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
-#define REST(v) for (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \
- return (v);
-#define HI_BIT (1L << (2 * N - 1))
+#define REST(v) \
+ for (i = 0; i < 3; i++) { \
+ xsubi[i] = x[i]; \
+ x[i] = temp[i]; \
+ } \
+ return (v);
+#define HI_BIT (1L << (2 * N - 1))
static uint32_t x[3] = {X0, X1, X2}, a[3] = {A0, A1, A2}, c = C;
static void next(void);
int32_t redisLrand48() {
next();
- return (((int32_t) x[2] << (N - 1)) + (x[1] >> 1));
+ return (((int32_t)x[2] << (N - 1)) + (x[1] >> 1));
}
-void redisSrand48(int32_t seedval) {
- SEED(X0, LOW(seedval), HIGH(seedval));
-}
+void redisSrand48(int32_t seedval) { SEED(X0, LOW(seedval), HIGH(seedval)); }
static void next(void) {
uint32_t p[2], q[2], r[2], carry0, carry1;
@@ -108,8 +114,7 @@ static void next(void) {
MUL(a[0], x[1], q);
ADDEQU(p[1], q[0], carry0);
MUL(a[1], x[0], r);
- x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
- a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
+ x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] + a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
x[1] = LOW(p[1] + r[0]);
x[0] = LOW(p[0]);
}
diff --git a/src/redis_bitmap.cc b/src/redis_bitmap.cc
index 30c200c..55e44ab 100644
--- a/src/redis_bitmap.cc
+++ b/src/redis_bitmap.cc
@@ -19,41 +19,33 @@
*/
#include "redis_bitmap.h"
-#include <vector>
+
+#include <algorithm>
#include <memory>
#include <utility>
-#include <algorithm>
+#include <vector>
+#include "db_util.h"
#include "parse_util.h"
#include "redis_bitmap_string.h"
-#include "db_util.h"
namespace Redis {
const uint32_t kBitmapSegmentBits = 1024 * 8;
const uint32_t kBitmapSegmentBytes = 1024;
-const char kErrBitmapStringOutOfRange[] = "The size of the bitmap string exceeds the "
- "configuration item max-bitmap-to-string-mb";
+const char kErrBitmapStringOutOfRange[] =
+ "The size of the bitmap string exceeds the "
+ "configuration item max-bitmap-to-string-mb";
extern const uint8_t kNum2Bits[256] = {
- 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
- 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
- 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
- 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
- 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
- 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
- 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
- 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
- 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
- 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
- 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
- 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
- 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
- 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
- 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
- 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
-};
+ 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2,
+ 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3,
+ 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5,
+ 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4,
+ 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4,
+ 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6,
+ 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
rocksdb::Status Bitmap::GetMetadata(const Slice &ns_key, BitmapMetadata *metadata, std::string *raw_value) {
std::string old_metadata;
@@ -133,9 +125,7 @@ rocksdb::Status Bitmap::GetString(const Slice &user_key, const uint32_t max_btos
uint32_t frag_index, valid_size;
auto iter = DBUtil::UniqueIterator(db_, read_options);
- for (iter->Seek(prefix_key);
- iter->Valid() && iter->key().starts_with(prefix_key);
- iter->Next()) {
+ for (iter->Seek(prefix_key); iter->Valid() && iter->key().starts_with(prefix_key); iter->Next()) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
auto parse_result = ParseInt<uint32_t>(ikey.GetSubKey().ToString(), 10);
if (!parse_result) {
@@ -145,40 +135,38 @@ rocksdb::Status Bitmap::GetString(const Slice &user_key, const uint32_t max_btos
fragment = iter->value().ToString();
// To be compatible with data written before the commit d603b0e(#338)
// and avoid returning extra null char after expansion.
- valid_size = std::min({fragment.size(),
- static_cast<size_t>(kBitmapSegmentBytes),
- static_cast<size_t>(metadata.size - frag_index)});
-
- /*
- * If you setbit bit 0 1, the value is stored as 0x01 in Kvrocks but 0x80 in Redis.
- * So we need to swap bits is to keep the same return value as Redis.
- * This swap table is generated according to the following mapping definition.
- * swap_table(x) = ((x & 0x80) >> 7)| ((x & 0x40) >> 5)|\
- * ((x & 0x20) >> 3)| ((x & 0x10) >> 1)|\
- * ((x & 0x08) << 1)| ((x & 0x04) << 3)|\
- * ((x & 0x02) << 5)| ((x & 0x01) << 7);
- */
+ valid_size = std::min(
+ {fragment.size(), static_cast<size_t>(kBitmapSegmentBytes), static_cast<size_t>(metadata.size - frag_index)});
+
+ /*
+ * If you setbit bit 0 1, the value is stored as 0x01 in Kvrocks but 0x80 in Redis.
+ * So we need to swap bits is to keep the same return value as Redis.
+ * This swap table is generated according to the following mapping definition.
+ * swap_table(x) = ((x & 0x80) >> 7)| ((x & 0x40) >> 5)|\
+ * ((x & 0x20) >> 3)| ((x & 0x10) >> 1)|\
+ * ((x & 0x08) << 1)| ((x & 0x04) << 3)|\
+ * ((x & 0x02) << 5)| ((x & 0x01) << 7);
+ */
static const uint8_t swap_table[256] = {
- 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
- 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
- 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
- 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
- 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
- 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
- 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
- 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
- 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
- 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
- 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
- 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
- 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
- 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
- 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
- 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
- };
+ 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, 0x08, 0x88,
+ 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, 0x04, 0x84, 0x44, 0xC4,
+ 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC,
+ 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
+ 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A,
+ 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6,
+ 0x36, 0xB6, 0x76, 0xF6, 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE,
+ 0x7E, 0xFE, 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
+ 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, 0x05, 0x85,
+ 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, 0x0D, 0x8D, 0x4D, 0xCD,
+ 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3,
+ 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
+ 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97,
+ 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF,
+ 0x3F, 0xBF, 0x7F, 0xFF};
for (uint32_t i = 0; i < valid_size; i++) {
- if (!fragment[i]) continue;
- fragment[i] = swap_table[static_cast<uint8_t>(fragment[i])];;
+ if (!fragment[i]) continue;
+ fragment[i] = swap_table[static_cast<uint8_t>(fragment[i])];
+ ;
}
value->replace(frag_index, valid_size, fragment.data(), valid_size);
}
@@ -270,8 +258,8 @@ rocksdb::Status Bitmap::BitCount(const Slice &user_key, int64_t start, int64_t s
// Don't use multi get to prevent large range query, and take too much memory
std::string sub_key, value;
for (uint32_t i = start_index; i <= stop_index; i++) {
- InternalKey(ns_key, std::to_string(i * kBitmapSegmentBytes), metadata.version,
- storage_->IsSlotIdEncoded()).Encode(&sub_key);
+ InternalKey(ns_key, std::to_string(i * kBitmapSegmentBytes), metadata.version, storage_->IsSlotIdEncoded())
+ .Encode(&sub_key);
s = db_->Get(read_options, sub_key, &value);
if (!s.ok() && !s.IsNotFound()) return s;
if (s.IsNotFound()) continue;
@@ -285,8 +273,8 @@ rocksdb::Status Bitmap::BitCount(const Slice &user_key, int64_t start, int64_t s
return rocksdb::Status::OK();
}
-rocksdb::Status Bitmap::BitPos(const Slice &user_key, bool bit, int64_t start,
- int64_t stop, bool stop_given, int64_t *pos) {
+rocksdb::Status Bitmap::BitPos(const Slice &user_key, bool bit, int64_t start, int64_t stop, bool stop_given,
+ int64_t *pos) {
std::string ns_key, raw_value;
AppendNamespacePrefix(user_key, &ns_key);
@@ -328,8 +316,8 @@ rocksdb::Status Bitmap::BitPos(const Slice &user_key, bool bit, int64_t start,
// Don't use multi get to prevent large range query, and take too much memory
std::string sub_key, value;
for (uint32_t i = start_index; i <= stop_index; i++) {
- InternalKey(ns_key, std::to_string(i * kBitmapSegmentBytes), metadata.version,
- storage_->IsSlotIdEncoded()).Encode(&sub_key);
+ InternalKey(ns_key, std::to_string(i * kBitmapSegmentBytes), metadata.version, storage_->IsSlotIdEncoded())
+ .Encode(&sub_key);
s = db_->Get(read_options, sub_key, &value);
if (!s.ok() && !s.IsNotFound()) return s;
if (s.IsNotFound()) {
@@ -358,8 +346,8 @@ rocksdb::Status Bitmap::BitPos(const Slice &user_key, bool bit, int64_t start,
return rocksdb::Status::OK();
}
-rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name,
- const Slice &user_key, const std::vector<Slice> &op_keys, int64_t *len) {
+rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name, const Slice &user_key,
+ const std::vector<Slice> &op_keys, int64_t *len) {
std::string ns_key, raw_value, ns_op_key;
AppendNamespacePrefix(user_key, &ns_key);
LockGuard guard(storage_->GetLockManager(), ns_key);
@@ -400,7 +388,7 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name,
BitmapMetadata res_metadata;
if (num_keys == op_keys.size() || op_flag != kBitOpAnd) {
- uint64_t i, frag_numkeys = num_keys, stop_index = (max_size -1)/kBitmapSegmentBytes;
+ uint64_t i, frag_numkeys = num_keys, stop_index = (max_size - 1) / kBitmapSegmentBytes;
std::unique_ptr<unsigned char[]> frag_res(new unsigned char[kBitmapSegmentBytes]);
uint16_t frag_maxlen = 0, frag_minlen = 0;
std::string sub_key, fragment;
@@ -412,8 +400,9 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name,
read_options.snapshot = ss.GetSnapShot();
for (uint64_t frag_index = 0; frag_index <= stop_index; frag_index++) {
for (const auto &meta_pair : meta_pairs) {
- InternalKey(meta_pair.first, std::to_string(frag_index * kBitmapSegmentBytes),
- meta_pair.second.version, storage_->IsSlotIdEncoded()).Encode(&sub_key);
+ InternalKey(meta_pair.first, std::to_string(frag_index * kBitmapSegmentBytes), meta_pair.second.version,
+ storage_->IsSlotIdEncoded())
+ .Encode(&sub_key);
auto s = db_->Get(read_options, sub_key, &fragment);
if (!s.ok() && !s.IsNotFound()) {
return s;
@@ -439,67 +428,67 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name,
memset(frag_res.get(), 0, frag_maxlen);
}
- #ifndef USE_ALIGNED_ACCESS
- if (frag_minlen >= sizeof(uint64_t)*4 && frag_numkeys <= 16) {
- uint64_t *lres = reinterpret_cast<uint64_t*>(frag_res.get());
+#ifndef USE_ALIGNED_ACCESS
+ if (frag_minlen >= sizeof(uint64_t) * 4 && frag_numkeys <= 16) {
+ uint64_t *lres = reinterpret_cast<uint64_t *>(frag_res.get());
const uint64_t *lp[16];
for (i = 0; i < frag_numkeys; i++) {
- lp[i] = reinterpret_cast<const uint64_t*>(fragments[i].data());
+ lp[i] = reinterpret_cast<const uint64_t *>(fragments[i].data());
}
memcpy(frag_res.get(), fragments[0].data(), frag_minlen);
if (op_flag == kBitOpAnd) {
- while (frag_minlen >= sizeof(uint64_t)*4) {
- for (i = 1; i < frag_numkeys; i++) {
- lres[0] &= lp[i][0];
- lres[1] &= lp[i][1];
- lres[2] &= lp[i][2];
- lres[3] &= lp[i][3];
- lp[i]+=4;
- }
- lres+=4;
- j += sizeof(uint64_t)*4;
- frag_minlen -= sizeof(uint64_t)*4;
+ while (frag_minlen >= sizeof(uint64_t) * 4) {
+ for (i = 1; i < frag_numkeys; i++) {
+ lres[0] &= lp[i][0];
+ lres[1] &= lp[i][1];
+ lres[2] &= lp[i][2];
+ lres[3] &= lp[i][3];
+ lp[i] += 4;
}
+ lres += 4;
+ j += sizeof(uint64_t) * 4;
+ frag_minlen -= sizeof(uint64_t) * 4;
+ }
} else if (op_flag == kBitOpOr) {
- while (frag_minlen >= sizeof(uint64_t)*4) {
- for (i = 1; i < frag_numkeys; i++) {
- lres[0] |= lp[i][0];
- lres[1] |= lp[i][1];
- lres[2] |= lp[i][2];
- lres[3] |= lp[i][3];
- lp[i]+=4;
- }
- lres+=4;
- j += sizeof(uint64_t)*4;
- frag_minlen -= sizeof(uint64_t)*4;
+ while (frag_minlen >= sizeof(uint64_t) * 4) {
+ for (i = 1; i < frag_numkeys; i++) {
+ lres[0] |= lp[i][0];
+ lres[1] |= lp[i][1];
+ lres[2] |= lp[i][2];
+ lres[3] |= lp[i][3];
+ lp[i] += 4;
}
+ lres += 4;
+ j += sizeof(uint64_t) * 4;
+ frag_minlen -= sizeof(uint64_t) * 4;
+ }
} else if (op_flag == kBitOpXor) {
- while (frag_minlen >= sizeof(uint64_t)*4) {
- for (i = 1; i < frag_numkeys; i++) {
- lres[0] ^= lp[i][0];
- lres[1] ^= lp[i][1];
- lres[2] ^= lp[i][2];
- lres[3] ^= lp[i][3];
- lp[i]+=4;
- }
- lres+=4;
- j += sizeof(uint64_t)*4;
- frag_minlen -= sizeof(uint64_t)*4;
+ while (frag_minlen >= sizeof(uint64_t) * 4) {
+ for (i = 1; i < frag_numkeys; i++) {
+ lres[0] ^= lp[i][0];
+ lres[1] ^= lp[i][1];
+ lres[2] ^= lp[i][2];
+ lres[3] ^= lp[i][3];
+ lp[i] += 4;
}
+ lres += 4;
+ j += sizeof(uint64_t) * 4;
+ frag_minlen -= sizeof(uint64_t) * 4;
+ }
} else if (op_flag == kBitOpNot) {
- while (frag_minlen >= sizeof(uint64_t)*4) {
- lres[0] = ~lres[0];
- lres[1] = ~lres[1];
- lres[2] = ~lres[2];
- lres[3] = ~lres[3];
- lres+=4;
- j += sizeof(uint64_t)*4;
- frag_minlen -= sizeof(uint64_t)*4;
- }
+ while (frag_minlen >= sizeof(uint64_t) * 4) {
+ lres[0] = ~lres[0];
+ lres[1] = ~lres[1];
+ lres[2] = ~lres[2];
+ lres[3] = ~lres[3];
+ lres += 4;
+ j += sizeof(uint64_t) * 4;
+ frag_minlen -= sizeof(uint64_t) * 4;
+ }
}
- }
- #endif
+ }
+#endif
for (; j < frag_maxlen; j++) {
output = (fragments[0].size() <= j) ? 0 : fragments[0][j];
@@ -507,10 +496,17 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name,
for (i = 1; i < frag_numkeys; i++) {
byte = (fragments[i].size() <= j) ? 0 : fragments[i][j];
switch (op_flag) {
- case kBitOpAnd: output &= byte; break;
- case kBitOpOr: output |= byte; break;
- case kBitOpXor: output ^= byte; break;
- default: break;
+ case kBitOpAnd:
+ output &= byte;
+ break;
+ case kBitOpOr:
+ output |= byte;
+ break;
+ case kBitOpXor:
+ output ^= byte;
+ break;
+ default:
+ break;
}
}
frag_res[j] = output;
@@ -523,9 +519,10 @@ rocksdb::Status Bitmap::BitOp(BitOpFlags op_flag, const std::string &op_name,
frag_maxlen = kBitmapSegmentBytes;
}
}
- InternalKey(ns_key, std::to_string(frag_index * kBitmapSegmentBytes),
- res_metadata.version, storage_->IsSlotIdEncoded()).Encode(&sub_key);
- batch.Put(sub_key, Slice(reinterpret_cast<char*>(frag_res.get()), frag_maxlen));
+ InternalKey(ns_key, std::to_string(frag_index * kBitmapSegmentBytes), res_metadata.version,
+ storage_->IsSlotIdEncoded())
+ .Encode(&sub_key);
+ batch.Put(sub_key, Slice(reinterpret_cast<char *>(frag_res.get()), frag_maxlen));
}
frag_maxlen = 0;
diff --git a/src/redis_bitmap.h b/src/redis_bitmap.h
index a4aa594..22fcd04 100644
--- a/src/redis_bitmap.h
+++ b/src/redis_bitmap.h
@@ -41,16 +41,17 @@ namespace Redis {
class Bitmap : public Database {
public:
- Bitmap(Engine::Storage *storage, const std::string &ns): Database(storage, ns) {}
+ Bitmap(Engine::Storage *storage, const std::string &ns) : Database(storage, ns) {}
rocksdb::Status GetBit(const Slice &user_key, uint32_t offset, bool *bit);
rocksdb::Status GetString(const Slice &user_key, const uint32_t max_btos_size, std::string *value);
rocksdb::Status SetBit(const Slice &user_key, uint32_t offset, bool new_bit, bool *old_bit);
rocksdb::Status BitCount(const Slice &user_key, int64_t start, int64_t stop, uint32_t *cnt);
rocksdb::Status BitPos(const Slice &user_key, bool bit, int64_t start, int64_t stop, bool stop_given, int64_t *pos);
- rocksdb::Status BitOp(BitOpFlags op_flag, const std::string &op_name,
- const Slice &user_key, const std::vector<Slice> &op_keys, int64_t *len);
+ rocksdb::Status BitOp(BitOpFlags op_flag, const std::string &op_name, const Slice &user_key,
+ const std::vector<Slice> &op_keys, int64_t *len);
static bool GetBitFromValueAndOffset(const std::string &value, const uint32_t offset);
static bool IsEmptySegment(const Slice &segment);
+
private:
rocksdb::Status GetMetadata(const Slice &ns_key, BitmapMetadata *metadata, std::string *raw_value);
};
diff --git a/src/redis_bitmap_string.cc b/src/redis_bitmap_string.cc
index f5b0b3f..2a5284a 100644
--- a/src/redis_bitmap_string.cc
+++ b/src/redis_bitmap_string.cc
@@ -40,10 +40,7 @@ rocksdb::Status BitmapString::GetBit(const std::string &raw_value, uint32_t offs
return rocksdb::Status::OK();
}
-rocksdb::Status BitmapString::SetBit(const Slice &ns_key,
- std::string *raw_value,
- uint32_t offset,
- bool new_bit,
+rocksdb::Status BitmapString::SetBit(const Slice &ns_key, std::string *raw_value, uint32_t offset, bool new_bit,
bool *old_bit) {
auto string_value = raw_value->substr(STRING_HDR_SIZE, raw_value->size() - STRING_HDR_SIZE);
uint32_t byte_index = offset >> 3;
@@ -82,20 +79,16 @@ rocksdb::Status BitmapString::BitCount(const std::string &raw_value, int64_t sta
if (stop >= static_cast<int64_t>(strlen)) stop = strlen - 1;
/* Precondition: end >= 0 && end < strlen, so the only condition where
- * zero can be returned is: start > stop. */
+ * zero can be returned is: start > stop. */
if (start <= stop) {
int64_t bytes = stop - start + 1;
- *cnt = redisPopcount((unsigned char *) (&string_value[0] + start), bytes);
+ *cnt = redisPopcount((unsigned char *)(&string_value[0] + start), bytes);
}
return rocksdb::Status::OK();
}
-rocksdb::Status BitmapString::BitPos(const std::string &raw_value,
- bool bit,
- int64_t start,
- int64_t stop,
- bool stop_given,
- int64_t *pos) {
+rocksdb::Status BitmapString::BitPos(const std::string &raw_value, bool bit, int64_t start, int64_t stop,
+ bool stop_given, int64_t *pos) {
auto string_value = raw_value.substr(STRING_HDR_SIZE, raw_value.size() - STRING_HDR_SIZE);
auto strlen = string_value.size();
/* Convert negative indexes */
@@ -109,7 +102,7 @@ rocksdb::Status BitmapString::BitPos(const std::string &raw_value,
*pos = -1;
} else {
int64_t bytes = stop - start + 1;
- *pos = redisBitpos((unsigned char *) (&string_value[0] + start), bytes, bit);
+ *pos = redisBitpos((unsigned char *)(&string_value[0] + start), bytes, bit);
/* If we are looking for clear bits, and the user specified an exact
* range with start-end, we can't consider the right of the range as
@@ -173,16 +166,15 @@ size_t BitmapString::redisPopcount(unsigned char *p, int64_t count) {
aux6 = (aux6 & 0x33333333) + ((aux6 >> 2) & 0x33333333);
aux7 = aux7 - ((aux7 >> 1) & 0x55555555);
aux7 = (aux7 & 0x33333333) + ((aux7 >> 2) & 0x33333333);
- bits += ((((aux1 + (aux1 >> 4)) & 0x0F0F0F0F) +
- ((aux2 + (aux2 >> 4)) & 0x0F0F0F0F) +
- ((aux3 + (aux3 >> 4)) & 0x0F0F0F0F) +
- ((aux4 + (aux4 >> 4)) & 0x0F0F0F0F) +
- ((aux5 + (aux5 >> 4)) & 0x0F0F0F0F) +
- ((aux6 + (aux6 >> 4)) & 0x0F0F0F0F) +
- ((aux7 + (aux7 >> 4)) & 0x0F0F0F0F)) * 0x01010101) >> 24;
+ bits += ((((aux1 + (aux1 >> 4)) & 0x0F0F0F0F) + ((aux2 + (aux2 >> 4)) & 0x0F0F0F0F) +
+ ((aux3 + (aux3 >> 4)) & 0x0F0F0F0F) + ((aux4 + (aux4 >> 4)) & 0x0F0F0F0F) +
+ ((aux5 + (aux5 >> 4)) & 0x0F0F0F0F) + ((aux6 + (aux6 >> 4)) & 0x0F0F0F0F) +
+ ((aux7 + (aux7 >> 4)) & 0x0F0F0F0F)) *
+ 0x01010101) >>
+ 24;
}
/* Count the remaining bytes. */
- p = (unsigned char *) p4;
+ p = (unsigned char *)p4;
while (count--) bits += kNum2Bits[*p++];
return bits;
}
@@ -269,8 +261,8 @@ int64_t BitmapString::redisBitpos(unsigned char *c, int64_t count, int bit) {
* unsigned long. We don't know the size of the long so we use a
* simple trick. */
one = UINT64_MAX; /* All bits set to 1.*/
- one >>= 1; /* All bits set to 1 but the MSB. */
- one = ~one; /* All bits set to 0 but the MSB. */
+ one >>= 1; /* All bits set to 1 but the MSB. */
+ one = ~one; /* All bits set to 0 but the MSB. */
while (one) {
if (((one & word) != 0) == bit) return pos;
diff --git a/src/redis_bitmap_string.h b/src/redis_bitmap_string.h
index 4210751..01d656c 100644
--- a/src/redis_bitmap_string.h
+++ b/src/redis_bitmap_string.h
@@ -20,12 +20,12 @@
#pragma once
-#include "redis_db.h"
-#include "redis_metadata.h"
-
#include <string>
#include <vector>
+#include "redis_db.h"
+#include "redis_metadata.h"
+
namespace Redis {
class BitmapString : public Database {
@@ -34,8 +34,9 @@ class BitmapString : public Database {
rocksdb::Status GetBit(const std::string &raw_value, uint32_t offset, bool *bit);
rocksdb::Status SetBit(const Slice &ns_key, std::string *raw_value, uint32_t offset, bool new_bit, bool *old_bit);
rocksdb::Status BitCount(const std::string &raw_value, int64_t start, int64_t stop, uint32_t *cnt);
- rocksdb::Status BitPos(const std::string &raw_value, bool bit,
- int64_t start, int64_t stop, bool stop_given, int64_t *pos);
+ rocksdb::Status BitPos(const std::string &raw_value, bool bit, int64_t start, int64_t stop, bool stop_given,
+ int64_t *pos);
+
private:
size_t redisPopcount(unsigned char *p, int64_t count);
int64_t redisBitpos(unsigned char *c, int64_t count, int bit);
diff --git a/src/redis_cmd.cc b/src/redis_cmd.cc
index ad797bf..36ed9f3 100644
--- a/src/redis_cmd.cc
+++ b/src/redis_cmd.cc
@@ -20,30 +20,29 @@
#include "redis_cmd.h"
-#include <glog/logging.h>
-
#include <fcntl.h>
+#include <glog/logging.h>
#include <sys/socket.h>
+
#include <algorithm>
+#include <chrono>
#include <climits>
#include <cmath>
-#include <chrono>
-#include <vector>
#include <thread>
-#include <utility>
#include <unordered_map>
+#include <utility>
-#include "fd_util.h"
#include "cluster.h"
+#include "fd_util.h"
#include "log_collector.h"
#include "parse_util.h"
#include "redis_bitmap.h"
#include "redis_connection.h"
#include "redis_db.h"
+#include "redis_disk.h"
#include "redis_geo.h"
#include "redis_hash.h"
#include "redis_list.h"
-#include "redis_disk.h"
#include "redis_pubsub.h"
#include "redis_reply.h"
#include "redis_set.h"
@@ -81,14 +80,14 @@ enum class AuthResult {
NO_REQUIRE_PASS,
};
-AuthResult AuthenticateUser(Connection *conn, Config* config, const std::string& user_password) {
+AuthResult AuthenticateUser(Connection *conn, Config *config, const std::string &user_password) {
auto iter = config->tokens.find(user_password);
if (iter != config->tokens.end()) {
conn->SetNamespace(iter->second);
conn->BecomeUser();
return AuthResult::OK;
}
- const auto& requirepass = config->requirepass;
+ const auto &requirepass = config->requirepass;
if (!requirepass.empty() && user_password != requirepass) {
return AuthResult::INVALID_PASSWORD;
}
@@ -100,9 +99,7 @@ AuthResult AuthenticateUser(Connection *conn, Config* config, const std::string&
return AuthResult::OK;
}
-Status ParseTTL(const std::vector<std::string> &args,
- std::unordered_map<std::string, bool>* white_list,
- int *result) {
+Status ParseTTL(const std::vector<std::string> &args, std::unordered_map<std::string, bool> *white_list, int *result) {
int ttl = 0;
int64_t expire = 0;
bool last_arg = false;
@@ -112,48 +109,48 @@ Status ParseTTL(const std::vector<std::string> &args,
if (opt == "ex" && !ttl && !last_arg) {
auto parse_result = ParseInt<int>(args[++i], 10);
if (!parse_result) {
- return Status(Status::RedisParseErr, errValueNotInteger);
+ return Status(Status::RedisParseErr, errValueNotInteger);
}
ttl = *parse_result;
if (ttl <= 0) return Status(Status::RedisParseErr, errInvalidExpireTime);
- } else if (opt == "exat" && !ttl && !expire && !last_arg) {
+ } else if (opt == "exat" && !ttl && !expire && !last_arg) {
auto parse_result = ParseInt<int64_t>(args[++i], 10);
if (!parse_result) {
- return Status(Status::RedisParseErr, errValueNotInteger);
+ return Status(Status::RedisParseErr, errValueNotInteger);
}
expire = *parse_result;
if (expire <= 0) return Status(Status::RedisParseErr, errInvalidExpireTime);
- } else if (opt == "pxat" && !ttl && !expire && !last_arg) {
+ } else if (opt == "pxat" && !ttl && !expire && !last_arg) {
auto parse_result = ParseInt<uint64_t>(args[++i], 10);
if (!parse_result) {
- return Status(Status::RedisParseErr, errValueNotInteger);
+ return Status(Status::RedisParseErr, errValueNotInteger);
}
uint64_t expire_ms = *parse_result;
if (expire_ms <= 0) return Status(Status::RedisParseErr, errInvalidExpireTime);
if (expire_ms < 1000) {
- expire = 1;
+ expire = 1;
} else {
- expire = static_cast<int64_t>(expire_ms / 1000);
+ expire = static_cast<int64_t>(expire_ms / 1000);
}
- } else if (opt == "px" && !ttl && !last_arg) {
+ } else if (opt == "px" && !ttl && !last_arg) {
int64_t ttl_ms = 0;
auto parse_result = ParseInt<int64_t>(args[++i], 10);
if (!parse_result) {
- return Status(Status::RedisParseErr, errValueNotInteger);
+ return Status(Status::RedisParseErr, errValueNotInteger);
}
ttl_ms = *parse_result;
if (ttl_ms <= 0) return Status(Status::RedisParseErr, errInvalidExpireTime);
if (ttl_ms > 0 && ttl_ms < 1000) {
- ttl = 1; // round up the pttl to second
+ ttl = 1; // round up the pttl to second
} else {
- ttl = static_cast<int>(ttl_ms / 1000);
+ ttl = static_cast<int>(ttl_ms / 1000);
}
} else {
auto iter = white_list->find(opt);
if (iter != white_list->end()) {
- iter->second = true;
+ iter->second = true;
} else {
- return Status(Status::NotOK, errInvalidSyntax);
+ return Status(Status::NotOK, errInvalidSyntax);
}
}
}
@@ -171,18 +168,18 @@ class CommandAuth : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
Config *config = svr->GetConfig();
- auto& user_password = args_[1];
+ auto &user_password = args_[1];
AuthResult result = AuthenticateUser(conn, config, user_password);
switch (result) {
- case AuthResult::OK:
- *output = Redis::SimpleString("OK");
- break;
- case AuthResult::INVALID_PASSWORD:
- *output = Redis::Error("ERR invalid password");
- break;
- case AuthResult::NO_REQUIRE_PASS:
- *output = Redis::Error("ERR Client sent AUTH, but no password is set");
- break;
+ case AuthResult::OK:
+ *output = Redis::SimpleString("OK");
+ break;
+ case AuthResult::INVALID_PASSWORD:
+ *output = Redis::Error("ERR invalid password");
+ break;
+ case AuthResult::NO_REQUIRE_PASS:
+ *output = Redis::Error("ERR Client sent AUTH, but no password is set");
+ break;
}
return Status::OK();
}
@@ -220,21 +217,19 @@ class CommandNamespace : public Commander {
} else if (args_.size() == 4 && sub_command == "set") {
Status s = config->SetNamespace(args_[2], args_[3]);
*output = s.IsOK() ? Redis::SimpleString("OK") : Redis::Error(s.Msg());
- LOG(WARNING) << "Updated namespace: " << args_[2] << " with token: " << args_[3]
- << ", addr: " << conn->GetAddr() << ", result: " << s.Msg();
+ LOG(WARNING) << "Updated namespace: " << args_[2] << " with token: " << args_[3] << ", addr: " << conn->GetAddr()
+ << ", result: " << s.Msg();
} else if (args_.size() == 4 && sub_command == "add") {
Status s = config->AddNamespace(args_[2], args_[3]);
*output = s.IsOK() ? Redis::SimpleString("OK") : Redis::Error(s.Msg());
- LOG(WARNING) << "New namespace: " << args_[2] << " with token: " << args_[3]
- << ", addr: " << conn->GetAddr() << ", result: " << s.Msg();
+ LOG(WARNING) << "New namespace: " << args_[2] << " with token: " << args_[3] << ", addr: " << conn->GetAddr()
+ << ", result: " << s.Msg();
} else if (args_.size() == 3 && sub_command == "del") {
Status s = config->DelNamespace(args_[2]);
*output = s.IsOK() ? Redis::SimpleString("OK") : Redis::Error(s.Msg());
- LOG(WARNING) << "Deleted namespace: " << args_[2]
- << ", addr: " << conn->GetAddr() << ", result: " << s.Msg();
+ LOG(WARNING) << "Deleted namespace: " << args_[2] << ", addr: " << conn->GetAddr() << ", result: " << s.Msg();
} else {
- *output = Redis::Error(
- "NAMESPACE subcommand must be one of GET, SET, DEL, ADD");
+ *output = Redis::Error("NAMESPACE subcommand must be one of GET, SET, DEL, ADD");
}
return Status::OK();
}
@@ -271,8 +266,7 @@ class CommandFlushDB : public Commander {
}
Redis::Database redis(svr->storage_, conn->GetNamespace());
rocksdb::Status s = redis.FlushDB();
- LOG(WARNING) << "DB keys in namespce: " << conn->GetNamespace()
- << " was flushed, addr: " << conn->GetAddr();
+ LOG(WARNING) << "DB keys in namespce: " << conn->GetNamespace() << " was flushed, addr: " << conn->GetAddr();
if (s.ok()) {
*output = Redis::SimpleString("OK");
return Status::OK();
@@ -319,7 +313,7 @@ class CommandPing : public Commander {
}
};
-class CommandSelect: public Commander {
+class CommandSelect : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
*output = Redis::SimpleString("OK");
@@ -336,8 +330,7 @@ class CommandConfig : public Commander {
}
Config *config = svr->GetConfig();
std::string sub_command = Util::ToLower(args_[1]);
- if ((sub_command == "rewrite" && args_.size() != 2) ||
- (sub_command == "get" && args_.size() != 3) ||
+ if ((sub_command == "rewrite" && args_.size() != 2) || (sub_command == "get" && args_.size() != 3) ||
(sub_command == "set" && args_.size() != 4)) {
*output = Redis::Error(errWrongNumOfArguments);
return Status::OK();
@@ -354,7 +347,7 @@ class CommandConfig : public Commander {
} else if (args_.size() == 4 && sub_command == "set") {
Status s = config->Set(svr, args_[2], args_[3]);
if (!s.IsOK()) {
- *output = Redis::Error("CONFIG SET '"+args_[2]+"' error: "+s.Msg());
+ *output = Redis::Error("CONFIG SET '" + args_[2] + "' error: " + s.Msg());
} else {
*output = Redis::SimpleString("OK");
}
@@ -427,7 +420,7 @@ class CommandGetEx : public Commander {
std::unordered_map<std::string, bool> white_list_;
};
-class CommandStrlen: public Commander {
+class CommandStrlen : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
std::string value;
@@ -481,7 +474,7 @@ class CommandGetDel : public Commander {
}
};
-class CommandGetRange: public Commander {
+class CommandGetRange : public Commander {
public:
Status Parse(const std::vector<std::string> &args) override {
auto parse_start = ParseInt<int>(args[2], 10);
@@ -521,7 +514,7 @@ class CommandGetRange: public Commander {
int start_ = 0, stop_ = 0;
};
-class CommandSetRange: public Commander {
+class CommandSetRange : public Commander {
public:
Status Parse(const std::vector<std::string> &args) override {
auto parse_result = ParseInt<int>(args[2], 10);
@@ -563,7 +556,7 @@ class CommandMGet : public Commander {
}
};
-class CommandAppend: public Commander {
+class CommandAppend : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
int ret;
@@ -585,7 +578,9 @@ class CommandSet : public Commander {
if (white_list_["nx"] && white_list_["xx"]) {
return Status(Status::NotOK, errInvalidSyntax);
}
- if (!s.IsOK()) { return s; }
+ if (!s.IsOK()) {
+ return s;
+ }
return Commander::Parse(args);
}
Status Execute(Server *svr, Connection *conn, std::string *output) override {
@@ -684,8 +679,8 @@ class CommandMSet : public Commander {
Status Execute(Server *svr, Connection *conn, std::string *output) override {
Redis::String string_db(svr->storage_, conn->GetNamespace());
std::vector<StringPair> kvs;
- for (size_t i = 1; i < args_.size(); i+=2) {
- kvs.emplace_back(StringPair{args_[i], args_[i+1]});
+ for (size_t i = 1; i < args_.size(); i += 2) {
+ kvs.emplace_back(StringPair{args_[i], args_[i + 1]});
}
rocksdb::Status s = string_db.MSet(kvs);
if (!s.ok()) {
@@ -722,8 +717,8 @@ class CommandMSetNX : public Commander {
int ret;
std::vector<StringPair> kvs;
Redis::String string_db(svr->storage_, conn->GetNamespace());
- for (size_t i = 1; i < args_.size(); i+=2) {
- kvs.emplace_back(StringPair{args_[i], args_[i+1]});
+ for (size_t i = 1; i < args_.size(); i += 2) {
+ kvs.emplace_back(StringPair{args_[i], args_[i + 1]});
}
rocksdb::Status s = string_db.MSetNX(kvs, 0, &ret);
if (!s.ok()) {
@@ -835,7 +830,7 @@ class CommandCAS : public Commander {
Status Parse(const std::vector<std::string> &args) override {
bool last_arg;
for (size_t i = 4; i < args.size(); i++) {
- last_arg = (i == args.size()-1);
+ last_arg = (i == args.size() - 1);
std::string opt = Util::ToLower(args[i]);
if (opt == "ex") {
if (last_arg) return Status(Status::NotOK, errWrongNumOfArguments);
@@ -857,7 +852,7 @@ class CommandCAS : public Commander {
// round up the pttl to second
ttl_ = 1;
} else {
- ttl_ = static_cast<int>(ttl_ms/1000);
+ ttl_ = static_cast<int>(ttl_ms / 1000);
}
} else {
return Status(Status::NotOK, errInvalidSyntax);
@@ -936,6 +931,7 @@ class CommandGetBit : public Commander {
*output = Redis::Integer(bit ? 1 : 0);
return Status::OK();
}
+
private:
uint32_t offset_ = 0;
};
@@ -1004,7 +1000,7 @@ class CommandBitCount : public Commander {
int64_t start_ = 0, stop_ = -1;
};
-class CommandBitPos: public Commander {
+class CommandBitPos : public Commander {
public:
Status Parse(const std::vector<std::string> &args) override {
if (args.size() >= 4) {
@@ -1059,10 +1055,9 @@ class CommandBitOp : public Commander {
else if (opname == "not")
op_flag_ = kBitOpNot;
else
- return Status(Status::RedisInvalidCmd, "Unknown bit operation");
+ return Status(Status::RedisInvalidCmd, "Unknown bit operation");
if (op_flag_ == kBitOpNot && args.size() != 4) {
- return Status(Status::RedisInvalidCmd,
- "BITOP NOT must be called with a single source key.");
+ return Status(Status::RedisInvalidCmd, "BITOP NOT must be called with a single source key.");
}
return Commander::Parse(args);
}
@@ -1142,7 +1137,7 @@ class CommandPTTL : public Commander {
rocksdb::Status s = redis.TTL(args_[1], &ttl);
if (!s.ok()) return Status(Status::RedisExecErr, s.ToString());
if (ttl > 0) {
- *output = Redis::Integer(ttl*1000);
+ *output = Redis::Integer(ttl * 1000);
} else {
*output = Redis::Integer(ttl);
}
@@ -1268,10 +1263,10 @@ class CommandPExpireAt : public Commander {
if (!parse_result) {
return Status(Status::RedisParseErr, errValueNotInteger);
}
- if (*parse_result/1000 >= INT32_MAX) {
+ if (*parse_result / 1000 >= INT32_MAX) {
return Status(Status::RedisParseErr, "the expire time was overflow");
}
- timestamp_ = static_cast<int>(*parse_result/1000);
+ timestamp_ = static_cast<int>(*parse_result / 1000);
return Commander::Parse(args);
}
Status Execute(Server *svr, Connection *conn, std::string *output) override {
@@ -1563,7 +1558,7 @@ class CommandHRange : public Commander {
}
if (args.size() == 6) {
auto parse_result = ParseInt<int64_t>(args_[5], 10);
- if (!parse_result)return Status(Status::RedisParseErr, errValueNotInteger);
+ if (!parse_result) return Status(Status::RedisParseErr, errValueNotInteger);
limit_ = *parse_result;
}
return Commander::Parse(args);
@@ -1762,7 +1757,7 @@ class CommandBPop : public Commander {
rocksdb::Status TryPopFromList() {
Redis::List list_db(svr_->storage_, conn_->GetNamespace());
std::string elem;
- const std::string* last_key_ptr = nullptr;
+ const std::string *last_key_ptr = nullptr;
rocksdb::Status s;
for (const auto &key : keys_) {
last_key_ptr = &key;
@@ -1801,8 +1796,8 @@ class CommandBPop : public Commander {
self->timer_ = nullptr;
}
self->unBlockingAll();
- bufferevent_setcb(bev, Redis::Connection::OnRead, Redis::Connection::OnWrite,
- Redis::Connection::OnEvent, self->conn_);
+ bufferevent_setcb(bev, Redis::Connection::OnRead, Redis::Connection::OnWrite, Redis::Connection::OnEvent,
+ self->conn_);
bufferevent_enable(bev, EV_READ);
// We need to manually trigger the read event since we will stop processing commands
// in connection after the blocking command, so there may have some commands to be processed.
@@ -1829,8 +1824,8 @@ class CommandBPop : public Commander {
self->timer_ = nullptr;
self->unBlockingAll();
auto bev = self->conn_->GetBufferEvent();
- bufferevent_setcb(bev, Redis::Connection::OnRead, Redis::Connection::OnWrite,
- Redis::Connection::OnEvent, self->conn_);
+ bufferevent_setcb(bev, Redis::Connection::OnRead, Redis::Connection::OnWrite, Redis::Connection::OnEvent,
+ self->conn_);
bufferevent_enable(bev, EV_READ);
}
@@ -2202,13 +2197,13 @@ class CommandSPop : public Commander {
return Status(Status::RedisExecErr, s.ToString());
}
if (with_count_) {
- *output = Redis::MultiBulkString(members, false);
+ *output = Redis::MultiBulkString(members, false);
} else {
- if (members.size() > 0) {
- *output = Redis::BulkString(members.front());
- } else {
- *output = Redis::NilString();
- }
+ if (members.size() > 0) {
+ *output = Redis::BulkString(members.front());
+ } else {
+ *output = Redis::NilString();
+ }
}
return Status::OK();
}
@@ -2313,7 +2308,7 @@ class CommandSInter : public Commander {
}
};
-class CommandSDiffStore: public Commander {
+class CommandSDiffStore : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
int ret = 0;
@@ -2331,7 +2326,7 @@ class CommandSDiffStore: public Commander {
}
};
-class CommandSUnionStore: public Commander {
+class CommandSUnionStore : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
int ret = 0;
@@ -2349,7 +2344,7 @@ class CommandSUnionStore: public Commander {
}
};
-class CommandSInterStore: public Commander {
+class CommandSInterStore : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
int ret = 0;
@@ -2561,8 +2556,7 @@ class CommandZRange : public Commander {
Redis::ZSet zset_db(svr->storage_, conn->GetNamespace());
std::vector<MemberScore> memeber_scores;
uint8_t flags = !reversed_ ? 0 : ZSET_REVERSED;
- rocksdb::Status s =
- zset_db.Range(args_[1], start_, stop_, flags, &memeber_scores);
+ rocksdb::Status s = zset_db.Range(args_[1], start_, stop_, flags, &memeber_scores);
if (!s.ok()) {
return Status(Status::RedisExecErr, s.ToString());
}
@@ -2573,8 +2567,7 @@ class CommandZRange : public Commander {
}
for (const auto &ms : memeber_scores) {
output->append(Redis::BulkString(ms.member));
- if (with_scores_)
- output->append(Redis::BulkString(Util::Float2String(ms.score)));
+ if (with_scores_) output->append(Redis::BulkString(Util::Float2String(ms.score)));
}
return Status::OK();
}
@@ -2593,9 +2586,7 @@ class CommandZRevRange : public CommandZRange {
class CommandZRangeByLex : public Commander {
public:
- explicit CommandZRangeByLex(bool reversed = false) {
- spec_.reversed = reversed;
- }
+ explicit CommandZRangeByLex(bool reversed = false) { spec_.reversed = reversed; }
Status Parse(const std::vector<std::string> &args) override {
Status s;
@@ -2637,9 +2628,7 @@ class CommandZRangeByLex : public Commander {
class CommandZRangeByScore : public Commander {
public:
- explicit CommandZRangeByScore(bool reversed = false) {
- spec_.reversed = reversed;
- }
+ explicit CommandZRangeByScore(bool reversed = false) { spec_.reversed = reversed; }
Status Parse(const std::vector<std::string> &args) override {
Status s;
if (spec_.reversed) {
@@ -2650,24 +2639,24 @@ class CommandZRangeByScore : public Commander {
if (!s.IsOK()) {
return Status(Status::RedisParseErr, s.Msg());
}
- size_t i = 4;
- while (i < args.size()) {
- if (Util::ToLower(args[i]) == "withscores") {
- with_scores_ = true;
- i++;
- } else if (Util::ToLower(args[i]) == "limit" && i + 2 < args.size()) {
- auto parse_offset = ParseInt<int>(args[i + 1], 10);
- auto parse_count = ParseInt<int>(args[i + 2], 10);
- if (!parse_offset || !parse_count) {
- return Status(Status::RedisParseErr, errValueNotInteger);
- }
- spec_.offset = *parse_offset;
- spec_.count = *parse_count;
- i += 3;
- } else {
- return Status(Status::RedisParseErr, errInvalidSyntax);
+ size_t i = 4;
+ while (i < args.size()) {
+ if (Util::ToLower(args[i]) == "withscores") {
+ with_scores_ = true;
+ i++;
+ } else if (Util::ToLower(args[i]) == "limit" && i + 2 < args.size()) {
+ auto parse_offset = ParseInt<int>(args[i + 1], 10);
+ auto parse_count = ParseInt<int>(args[i + 2], 10);
+ if (!parse_offset || !parse_count) {
+ return Status(Status::RedisParseErr, errValueNotInteger);
}
+ spec_.offset = *parse_offset;
+ spec_.count = *parse_count;
+ i += 3;
+ } else {
+ return Status(Status::RedisParseErr, errInvalidSyntax);
}
+ }
return Commander::Parse(args);
}
@@ -2675,8 +2664,7 @@ class CommandZRangeByScore : public Commander {
int size;
Redis::ZSet zset_db(svr->storage_, conn->GetNamespace());
std::vector<MemberScore> memeber_scores;
- rocksdb::Status s =
- zset_db.RangeByScore(args_[1], spec_, &memeber_scores, &size);
+ rocksdb::Status s = zset_db.RangeByScore(args_[1], spec_, &memeber_scores, &size);
if (!s.ok()) {
return Status(Status::RedisExecErr, s.ToString());
}
@@ -2687,8 +2675,7 @@ class CommandZRangeByScore : public Commander {
}
for (const auto &ms : memeber_scores) {
output->append(Redis::BulkString(ms.member));
- if (with_scores_)
- output->append(Redis::BulkString(Util::Float2String(ms.score)));
+ if (with_scores_) output->append(Redis::BulkString(Util::Float2String(ms.score)));
}
return Status::OK();
}
@@ -2700,8 +2687,7 @@ class CommandZRangeByScore : public Commander {
class CommandZRank : public Commander {
public:
- explicit CommandZRank(bool reversed = false)
- : reversed_(reversed) {}
+ explicit CommandZRank(bool reversed = false) : reversed_(reversed) {}
Status Execute(Server *svr, Connection *conn, std::string *output) override {
int rank;
Redis::ZSet zset_db(svr->storage_, conn->GetNamespace());
@@ -2771,8 +2757,7 @@ class CommandZRemRangeByRank : public Commander {
int ret;
Redis::ZSet zset_db(svr->storage_, conn->GetNamespace());
- rocksdb::Status s =
- zset_db.RemoveRangeByRank(args_[1], start_, stop_, &ret);
+ rocksdb::Status s = zset_db.RemoveRangeByRank(args_[1], start_, stop_, &ret);
if (!s.ok()) {
return Status(Status::RedisExecErr, s.ToString());
}
@@ -2984,9 +2969,7 @@ class CommandGeoBase : public Commander {
return Status::OK();
}
- Status ParseLongLat(const std::string &longitude_para,
- const std::string &latitude_para,
- double *longitude,
+ Status ParseLongLat(const std::string &longitude_para, const std::string &latitude_para, double *longitude,
double *latitude) {
try {
*longitude = std::stod(longitude_para);
@@ -2994,31 +2977,30 @@ class CommandGeoBase : public Commander {
} catch (const std::exception &e) {
return Status(Status::RedisParseErr, "ERR value is not a valid float");
}
- if (*longitude < GEO_LONG_MIN || *longitude > GEO_LONG_MAX ||
- *latitude < GEO_LAT_MIN || *latitude > GEO_LAT_MAX) {
+ if (*longitude < GEO_LONG_MIN || *longitude > GEO_LONG_MAX || *latitude < GEO_LAT_MIN || *latitude > GEO_LAT_MAX) {
return Status(Status::RedisParseErr, "invalid longitude,latitude pair " + longitude_para + "," + latitude_para);
}
return Status::OK();
}
- double GetDistanceByUnit(double distance) {
- return distance / GetUnitConversion();
- }
+ double GetDistanceByUnit(double distance) { return distance / GetUnitConversion(); }
- double GetRadiusMeters(double radius) {
- return radius * GetUnitConversion();
- }
+ double GetRadiusMeters(double radius) { return radius * GetUnitConversion(); }
double GetUnitConversion() {
double conversion = 0;
switch (distance_unit_) {
- case kDistanceMeter:conversion = 1;
+ case kDistanceMeter:
+ conversion = 1;
break;
- case kDistanceKilometers:conversion = 1000;
+ case kDistanceKilometers:
+ conversion = 1000;
break;
- case kDistanceFeet:conversion = 0.3048;
+ case kDistanceFeet:
+ conversion = 0.3048;
break;
- case kDistanceMiles:conversion = 1609.34;
+ case kDistanceMiles:
+ conversion = 1609.34;
break;
}
return conversion;
@@ -3132,8 +3114,8 @@ class CommandGeoPos : public Commander {
if (iter == geo_points.end()) {
list.emplace_back(Redis::NilString());
} else {
- list.emplace_back(Redis::MultiBulkString({Util::Float2String(iter->second.longitude),
- Util::Float2String(iter->second.latitude)}));
+ list.emplace_back(Redis::MultiBulkString(
+ {Util::Float2String(iter->second.longitude), Util::Float2String(iter->second.latitude)}));
}
}
*output = Redis::Array(list);
@@ -3187,9 +3169,9 @@ class CommandGeoRadius : public CommandGeoBase {
}
count_ = *parse_result;
i += 2;
- } else if (attributes_->is_write()
- && (Util::ToLower(args_[i]) == "store" || Util::ToLower(args_[i]) == "storedist")
- && i + 1 < args_.size()) {
+ } else if (attributes_->is_write() &&
+ (Util::ToLower(args_[i]) == "store" || Util::ToLower(args_[i]) == "storedist") &&
+ i + 1 < args_.size()) {
store_key_ = args_[i + 1];
if (Util::ToLower(args_[i]) == "storedist") {
store_distance_ = true;
@@ -3216,11 +3198,8 @@ class CommandGeoRadius : public CommandGeoBase {
Status Execute(Server *svr, Connection *conn, std::string *output) override {
std::vector<GeoPoint> geo_points;
Redis::Geo geo_db(svr->storage_, conn->GetNamespace());
- rocksdb::Status s = geo_db.Radius(args_[1], longitude_, latitude_, GetRadiusMeters(radius_),
- count_,
- sort_,
- store_key_,
- store_distance_, GetUnitConversion(), &geo_points);
+ rocksdb::Status s = geo_db.Radius(args_[1], longitude_, latitude_, GetRadiusMeters(radius_), count_, sort_,
+ store_key_, store_distance_, GetUnitConversion(), &geo_points);
if (!s.ok()) {
return Status(Status::RedisExecErr, s.ToString());
}
@@ -3246,8 +3225,8 @@ class CommandGeoRadius : public CommandGeoBase {
one.emplace_back(Redis::BulkString(Util::Float2String(geo_point.score)));
}
if (with_coord_) {
- one.emplace_back(Redis::MultiBulkString({Util::Float2String(geo_point.longitude),
- Util::Float2String(geo_point.latitude)}));
+ one.emplace_back(Redis::MultiBulkString(
+ {Util::Float2String(geo_point.longitude), Util::Float2String(geo_point.latitude)}));
}
list.emplace_back(Redis::Array(one));
}
@@ -3290,10 +3269,7 @@ class CommandGeoRadiusByMember : public CommandGeoRadius {
Status Execute(Server *svr, Connection *conn, std::string *output) override {
std::vector<GeoPoint> geo_points;
Redis::Geo geo_db(svr->storage_, conn->GetNamespace());
- rocksdb::Status s = geo_db.RadiusByMember(args_[1], args_[2], GetRadiusMeters(radius_),
- count_,
- sort_,
- store_key_,
+ rocksdb::Status s = geo_db.RadiusByMember(args_[1], args_[2], GetRadiusMeters(radius_), count_, sort_, store_key_,
store_distance_, GetUnitConversion(), &geo_points);
if (!s.ok()) {
return Status(Status::RedisExecErr, s.ToString());
@@ -3414,9 +3390,7 @@ class CommandSortedintExists : public Commander {
class CommandSortedintRange : public Commander {
public:
- explicit CommandSortedintRange(bool reversed = false) {
- reversed_ = reversed;
- }
+ explicit CommandSortedintRange(bool reversed = false) { reversed_ = reversed; }
Status Parse(const std::vector<std::string> &args) override {
auto parse_offset = ParseInt<uint64_t>(args[2], 10);
@@ -3467,9 +3441,7 @@ class CommandSortedintRevRange : public CommandSortedintRange {
class CommandSortedintRangeByValue : public Commander {
public:
- explicit CommandSortedintRangeByValue(bool reversed = false) {
- spec_.reversed = reversed;
- }
+ explicit CommandSortedintRangeByValue(bool reversed = false) { spec_.reversed = reversed; }
Status Parse(const std::vector<std::string> &args) override {
Status s;
@@ -3538,8 +3510,7 @@ class CommandDisk : public Commander {
public:
Status Parse(const std::vector<std::string> &args) override {
std::string opname = Util::ToLower(args[1]);
- if (opname != "usage")
- return Status(Status::RedisInvalidCmd, "Unknown operation");
+ if (opname != "usage") return Status(Status::RedisInvalidCmd, "Unknown operation");
return Commander::Parse(args);
}
@@ -3645,7 +3616,7 @@ class CommandCompact : public Commander {
}
};
-class CommandBGSave: public Commander {
+class CommandBGSave : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
if (!conn->IsAdmin()) {
@@ -3729,8 +3700,7 @@ class CommandSubscribe : public Commander {
Status Execute(Server *svr, Connection *conn, std::string *output) override {
for (unsigned i = 1; i < args_.size(); i++) {
conn->SubscribeChannel(args_[i]);
- SubscribeCommmandReply(output, "subscribe", args_[i],
- conn->SubscriptionsCount() + conn->PSubscriptionsCount());
+ SubscribeCommmandReply(output, "subscribe", args_[i], conn->SubscriptionsCount() + conn->PSubscriptionsCount());
}
return Status::OK();
}
@@ -3740,8 +3710,8 @@ class CommandUnSubscribe : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
if (args_.size() == 1) {
- conn->UnSubscribeAll(std::bind(SubscribeCommmandReply, output, "unsubscribe",
- std::placeholders::_1, std::placeholders::_2));
+ conn->UnSubscribeAll(
+ std::bind(SubscribeCommmandReply, output, "unsubscribe", std::placeholders::_1, std::placeholders::_2));
} else {
for (unsigned i = 1; i < args_.size(); i++) {
conn->UnSubscribeChannel(args_[i]);
@@ -3758,8 +3728,7 @@ class CommandPSubscribe : public Commander {
Status Execute(Server *svr, Connection *conn, std::string *output) override {
for (unsigned i = 1; i < args_.size(); i++) {
conn->PSubscribeChannel(args_[i]);
- SubscribeCommmandReply(output, "psubscribe", args_[i],
- conn->SubscriptionsCount() + conn->PSubscriptionsCount());
+ SubscribeCommmandReply(output, "psubscribe", args_[i], conn->SubscriptionsCount() + conn->PSubscriptionsCount());
}
return Status::OK();
}
@@ -3769,8 +3738,8 @@ class CommandPUnSubscribe : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
if (args_.size() == 1) {
- conn->PUnSubscribeAll(std::bind(SubscribeCommmandReply, output, "punsubscribe",
- std::placeholders::_1, std::placeholders::_2));
+ conn->PUnSubscribeAll(
+ std::bind(SubscribeCommmandReply, output, "punsubscribe", std::placeholders::_1, std::placeholders::_2));
} else {
for (unsigned i = 1; i < args_.size(); i++) {
conn->PUnSubscribeChannel(args_[i]);
@@ -3801,8 +3770,7 @@ class CommandPubSub : public Commander {
}
return Status::OK();
}
- return Status(Status::RedisInvalidCmd,
- "ERR Unknown subcommand or wrong number of arguments");
+ return Status(Status::RedisInvalidCmd, "ERR Unknown subcommand or wrong number of arguments");
}
Status Execute(Server *srv, Connection *conn, std::string *output) override {
@@ -3825,8 +3793,7 @@ class CommandPubSub : public Commander {
return Status::OK();
}
- return Status(Status::RedisInvalidCmd,
- "ERR Unknown subcommand or wrong number of arguments");
+ return Status(Status::RedisInvalidCmd, "ERR Unknown subcommand or wrong number of arguments");
}
private:
@@ -3877,15 +3844,15 @@ class CommandSlaveOf : public Commander {
s = svr->AddMaster(host_, port_, false);
if (s.IsOK()) {
*output = Redis::SimpleString("OK");
- LOG(WARNING) << "SLAVE OF " << host_ << ":" << port_
- << " enabled (user request from '" << conn->GetAddr() << "')";
+ LOG(WARNING) << "SLAVE OF " << host_ << ":" << port_ << " enabled (user request from '" << conn->GetAddr()
+ << "')";
if (svr->GetConfig()->cluster_enabled) {
svr->slot_migrate_->SetMigrateStopFlag(true);
LOG(INFO) << "Change server role to slave, stop migration task";
}
} else {
- LOG(ERROR) << "SLAVE OF " << host_ << ":" << port_
- << " (user request from '" << conn->GetAddr() << "') encounter error: " << s.Msg();
+ LOG(ERROR) << "SLAVE OF " << host_ << ":" << port_ << " (user request from '" << conn->GetAddr()
+ << "') encounter error: " << s.Msg();
}
}
return s;
@@ -3896,7 +3863,7 @@ class CommandSlaveOf : public Commander {
uint32_t port_ = 0;
};
-class CommandStats: public Commander {
+class CommandStats : public Commander {
public:
Status Execute(Server *svr, Connection *conn, std::string *output) override {
std::string stats_json = svr->GetRocksDBStatsJson();
@@ -3929,11 +3896,10 @@ class CommandPSync : public Commander {
}
Status Execute(Server *svr, Connection *conn, std::string *output) override {
- LOG(INFO) << "Slave " << conn->GetAddr()
- << ", listening port: " << conn->GetListeningPort()
+ LOG(INFO) << "Slave " << conn->GetAddr() << ", listening port: " << conn->GetListeningPort()
<< " asks for synchronization"
<< " with next sequence: " << next_repl_seq
- << " replication id: " << (replica_replid.length() ? replica_replid : "not supported")
+ << " replication id: " << (replica_replid.length() ? replica_replid : "not supported")
<< ", and local sequence: " << svr->storage_->LatestSeq();
bool need_full_sync = false;
@@ -3974,11 +3940,9 @@ class CommandPSync : public Commander {
std::string err = "-ERR " + s.Msg() + "\r\n";
write(conn->GetFD(), err.c_str(), err.length());
conn->EnableFlag(Redis::Connection::kCloseAsync);
- LOG(WARNING) << "Failed to add salve: " << conn->GetAddr()
- << " to start increment syncing";
+ LOG(WARNING) << "Failed to add salve: " << conn->GetAddr() << " to start increment syncing";
} else {
- LOG(INFO) << "New slave: " << conn->GetAddr()
- << " was added, start increment syncing";
+ LOG(INFO) << "New slave: " << conn->GetAddr() << " was added, start increment syncing";
}
return Status::OK();
}
@@ -3989,8 +3953,7 @@ class CommandPSync : public Commander {
std::string replica_replid;
// Return OK if the seq is in the range of the current WAL
- Status checkWALBoundary(Engine::Storage *storage,
- rocksdb::SequenceNumber seq) {
+ Status checkWALBoundary(Engine::Storage *storage, rocksdb::SequenceNumber seq) {
if (seq == storage->LatestSeq() + 1) {
return Status::OK();
}
@@ -4096,7 +4059,7 @@ class CommandClient : public Commander {
Status Parse(const std::vector<std::string> &args) override {
subcommand_ = Util::ToLower(args[1]);
// subcommand: getname id kill list setname
- if ((subcommand_ == "id" || subcommand_ == "getname" || subcommand_ == "list") && args.size() == 2) {
+ if ((subcommand_ == "id" || subcommand_ == "getname" || subcommand_ == "list") && args.size() == 2) {
return Status::OK();
}
if ((subcommand_ == "setname") && args.size() == 3) {
@@ -4126,30 +4089,29 @@ class CommandClient : public Commander {
while (i < args.size()) {
bool moreargs = i < args.size();
if (!strcasecmp(args[i].c_str(), "addr") && moreargs) {
- addr_ = args[i+1];
+ addr_ = args[i + 1];
} else if (!strcasecmp(args[i].c_str(), "id") && moreargs) {
- auto parse_result = ParseInt<uint64_t>(args[i+1], 10);
+ auto parse_result = ParseInt<uint64_t>(args[i + 1], 10);
if (!parse_result) {
return Status(Status::RedisParseErr, errValueNotInteger);
}
id_ = *parse_result;
} else if (!strcasecmp(args[i].c_str(), "skipme") && moreargs) {
- if (!strcasecmp(args[i+1].c_str(), "yes")) {
+ if (!strcasecmp(args[i + 1].c_str(), "yes")) {
skipme_ = true;
- } else if (!strcasecmp(args[i+1].c_str(), "no")) {
+ } else if (!strcasecmp(args[i + 1].c_str(), "no")) {
skipme_ = false;
} else {
return Status(Status::RedisParseErr, errInvalidSyntax);
}
} else if (!strcasecmp(args[i].c_str(), "type") && moreargs) {
- if (!strcasecmp(args[i+1].c_str(), "normal")) {
+ if (!strcasecmp(args[i + 1].c_str(), "normal")) {
kill_type_ |= kTypeNormal;
- } else if (!strcasecmp(args[i+1].c_str(), "pubsub")) {
+ } else if (!strcasecmp(args[i + 1].c_str(), "pubsub")) {
kill_type_ |= kTypePubsub;
- } else if (!strcasecmp(args[i+1].c_str(), "master")) {
+ } else if (!strcasecmp(args[i + 1].c_str(), "master")) {
kill_type_ |= kTypeMaster;
- } else if (!strcasecmp(args[i+1].c_str(), "replica") ||
- !strcasecmp(args[i+1].c_str(), "slave")) {
+ } else if (!strcasecmp(args[i + 1].c_str(), "replica") || !strcasecmp(args[i + 1].c_str(), "slave")) {
kill_type_ |= kTypeSlave;
} else {
return Status(Status::RedisParseErr, errInvalidSyntax);
@@ -4161,8 +4123,7 @@ class CommandClient : public Commander {
}
return Status::OK();
}
- return Status(Status::RedisInvalidCmd,
- "Syntax error, try CLIENT LIST|KILL ip:port|GETNAME|SETNAME");
+ return Status(Status::RedisInvalidCmd, "Syntax error, try CLIENT LIST|KILL ip:port|GETNAME|SETNAME");
}
Status Execute(Server *srv, Connection *conn, std::string *output) override {
@@ -4175,7 +4136,7 @@ class CommandClient : public Commander {
return Status::OK();
} else if (subcommand_ == "getname") {
std::string name = conn->GetName();
- *output = name== ""? Redis::NilString(): Redis::BulkString(name);
+ *output = name == "" ? Redis::NilString() : Redis::BulkString(name);
return Status::OK();
} else if (subcommand_ == "id") {
*output = Redis::Integer(conn->GetID());
@@ -4194,8 +4155,7 @@ class CommandClient : public Commander {
return Status::OK();
}
- return Status(Status::RedisInvalidCmd,
- "Syntax error, try CLIENT LIST|KILL ip:port|GETNAME|SETNAME");
+ return Status(Status::RedisInvalidCmd, "Syntax error, try CLIENT LIST|KILL ip:port|GETNAME|SETNAME");
}
private:
@@ -4278,8 +4238,7 @@ class CommandCommand : public Commander {
GetAllCommandsInfo(output);
} else {
std::string sub_command = Util::ToLower(args_[1]);
- if ((sub_command == "count" && args_.size() != 2) ||
- (sub_command == "getkeys" && args_.size() < 3) ||
+ if ((sub_command == "count" && args_.size() != 2) || (sub_command == "getkeys" && args_.size() < 3) ||
(sub_command == "info" && args_.size() < 3)) {
*output = Redis::Error(errWrongNumOfArguments);
return Status::OK();
@@ -4342,21 +4301,21 @@ class CommandHello final : public Commander {
// Handling AUTH and SETNAME
for (; next_arg < args_.size(); ++next_arg) {
size_t moreargs = args_.size() - next_arg - 1;
- const std::string& opt = args_[next_arg];
+ const std::string &opt = args_[next_arg];
if (opt == "AUTH" && moreargs != 0) {
- const auto& user_password = args_[next_arg + 1];
+ const auto &user_password = args_[next_arg + 1];
auto authResult = AuthenticateUser(conn, svr->GetConfig(), user_password);
switch (authResult) {
- case AuthResult::INVALID_PASSWORD:
- return Status(Status::NotOK, "invalid password");
- case AuthResult::NO_REQUIRE_PASS:
- return Status(Status::NotOK, "Client sent AUTH, but no password is set");
- case AuthResult::OK:
- break;
+ case AuthResult::INVALID_PASSWORD:
+ return Status(Status::NotOK, "invalid password");
+ case AuthResult::NO_REQUIRE_PASS:
+ return Status(Status::NotOK, "Client sent AUTH, but no password is set");
+ case AuthResult::OK:
+ break;
}
next_arg += 1;
} else if (opt == "SETNAME" && moreargs != 0) {
- const std::string& name = args_[next_arg + 1];
+ const std::string &name = args_[next_arg + 1];
conn->SetName(name);
next_arg += 1;
} else {
@@ -4436,7 +4395,7 @@ class CommandScanBase : public Commander {
class CommandSubkeyScanBase : public CommandScanBase {
public:
- CommandSubkeyScanBase(): CommandScanBase() {}
+ CommandSubkeyScanBase() : CommandScanBase() {}
Status Parse(const std::vector<std::string> &args) override {
if (args.size() % 2 == 0) {
return Status(Status::RedisParseErr, errWrongNumOfArguments);
@@ -4621,7 +4580,7 @@ class CommandReplConf : public Commander {
if (option == "listening-port") {
auto parse_result = ParseInt<uint32_t>(value, NumericRange<int>{1, PORT_LIMIT - 1}, 10);
if (!parse_result) {
- return Status(Status::RedisParseErr, "listening-port should be number or out of range");
+ return Status(Status::RedisParseErr, "listening-port should be number or out of range");
}
port_ = *parse_result;
} else {
@@ -4644,9 +4603,7 @@ class CommandReplConf : public Commander {
class CommandFetchMeta : public Commander {
public:
- Status Parse(const std::vector<std::string> &args) override {
- return Status::OK();
- }
+ Status Parse(const std::vector<std::string> &args) override { return Status::OK(); }
Status Execute(Server *svr, Connection *conn, std::string *output) override {
int repl_fd = conn->GetFD();
@@ -4663,8 +4620,7 @@ class CommandFetchMeta : public Commander {
UniqueFD unique_fd{repl_fd};
std::string files;
- auto s = Engine::Storage::ReplDataManager::GetFullReplDataInfo(
- svr->storage_, &files);
+ auto s = Engine::Storage::ReplDataManager::GetFullReplDataInfo(svr->storage_, &files);
if (!s.IsOK()) {
const char *message = "-ERR can't create db checkpoint";
write(repl_fd, message, strlen(message));
@@ -4673,11 +4629,10 @@ class CommandFetchMeta : public Commander {
return;
}
// Send full data file info
- if (Util::SockSend(repl_fd, files+CRLF).IsOK()) {
+ if (Util::SockSend(repl_fd, files + CRLF).IsOK()) {
LOG(INFO) << "[replication] Succeed sending full data file info to " << ip;
} else {
- LOG(WARNING) << "[replication] Fail to send full data file info "
- << ip << ", error: " << strerror(errno);
+ LOG(WARNING) << "[replication] Fail to send full data file info " << ip << ", error: " << strerror(errno);
}
svr->storage_->SetCheckpointAccessTime(std::time(nullptr));
});
@@ -4713,35 +4668,29 @@ class CommandFetchFile : public Commander {
if (svr->IsStopped()) break;
uint64_t file_size = 0, max_replication_bytes = 0;
if (svr->GetConfig()->max_replication_mb > 0) {
- max_replication_bytes = (svr->GetConfig()->max_replication_mb*MiB) /
- svr->GetFetchFileThreadNum();
+ max_replication_bytes = (svr->GetConfig()->max_replication_mb * MiB) / svr->GetFetchFileThreadNum();
}
auto start = std::chrono::high_resolution_clock::now();
- auto fd = UniqueFD(Engine::Storage::ReplDataManager::OpenDataFile(svr->storage_,
- file, &file_size));
+ auto fd = UniqueFD(Engine::Storage::ReplDataManager::OpenDataFile(svr->storage_, file, &file_size));
if (!fd) break;
// Send file size and content
- if (Util::SockSend(repl_fd, std::to_string(file_size)+CRLF).IsOK() &&
+ if (Util::SockSend(repl_fd, std::to_string(file_size) + CRLF).IsOK() &&
Util::SockSendFile(repl_fd, *fd, file_size).IsOK()) {
- LOG(INFO) << "[replication] Succeed sending file " << file << " to "
- << ip;
+ LOG(INFO) << "[replication] Succeed sending file " << file << " to " << ip;
} else {
- LOG(WARNING) << "[replication] Fail to send file " << file << " to "
- << ip << ", error: " << strerror(errno);
+ LOG(WARNING) << "[replication] Fail to send file " << file << " to " << ip << ", error: " << strerror(errno);
break;
}
fd.Close();
// Sleep if the speed of sending file is more than replication speed limit
auto end = std::chrono::high_resolution_clock::now();
- uint64_t duration = std::chrono::duration_cast<std::chrono::microseconds>
- (end - start).count();
- uint64_t shortest = static_cast<uint64_t>(static_cast<double>(file_size) /
- max_replication_bytes * (1000 * 1000));
+ uint64_t duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
+ uint64_t shortest =
+ static_cast<uint64_t>(static_cast<double>(file_size) / max_replication_bytes * (1000 * 1000));
if (max_replication_bytes > 0 && duration < shortest) {
- LOG(INFO) << "[replication] Need to sleep "
- << (shortest - duration) / 1000
+ LOG(INFO) << "[replication] Need to sleep " << (shortest - duration) / 1000
<< " ms since of sending files too quickly";
usleep(shortest - duration);
}
@@ -4760,9 +4709,7 @@ class CommandFetchFile : public Commander {
class CommandDBName : public Commander {
public:
- Status Parse(const std::vector<std::string> &args) override {
- return Status::OK();
- }
+ Status Parse(const std::vector<std::string> &args) override { return Status::OK(); }
Status Execute(Server *svr, Connection *conn, std::string *output) override {
conn->Reply(svr->storage_->GetName() + CRLF);
@@ -4775,8 +4722,8 @@ class CommandCluster : public Commander {
Status Parse(const std::vector<std::string> &args) override {
subcommand_ = Util::ToLower(args[1]);
- if (args.size() == 2 && (subcommand_ == "nodes" || subcommand_ == "slots"
- || subcommand_ == "info")) return Status::OK();
+ if (args.size() == 2 && (subcommand_ == "nodes" || subcommand_ == "slots" || subcommand_ == "info"))
+ return Status::OK();
if (subcommand_ == "keyslot" && args_.size() == 3) return Status::OK();
if (subcommand_ == "import") {
if (args.size() != 4) return Status(Status::RedisParseErr, errWrongNumOfArguments);
@@ -4784,15 +4731,13 @@ class CommandCluster : public Commander {
if (!s.IsOK()) return s;
int64_t state;
- s = Util::DecimalStringToNum(args[3], &state,
- static_cast<int64_t>(kImportStart),
- static_cast<int64_t>(kImportNone));
+ s = Util::DecimalStringToNum(args[3], &state, static_cast<int64_t>(kImportStart),
+ static_cast<int64_t>(kImportNone));
if (!s.IsOK()) return Status(Status::NotOK, "Invalid import state");
state_ = static_cast<ImportStatus>(state);
return Status::OK();
}
- return Status(Status::RedisParseErr,
- "CLUSTER command, CLUSTER INFO|NODES|SLOTS|KEYSLOT");
+ return Status(Status::RedisParseErr, "CLUSTER command, CLUSTER INFO|NODES|SLOTS|KEYSLOT");
}
Status Execute(Server *svr, Connection *conn, std::string *output) override {
@@ -4815,7 +4760,7 @@ class CommandCluster : public Commander {
if (s.IsOK()) {
output->append(Redis::MultiLen(infos.size()));
for (const auto &info : infos) {
- output->append(Redis::MultiLen(info.nodes.size()+2));
+ output->append(Redis::MultiLen(info.nodes.size() + 2));
output->append(Redis::Integer(info.start));
output->append(Redis::Integer(info.end));
for (const auto &n : info.nodes) {
@@ -4847,7 +4792,7 @@ class CommandCluster : public Commander {
} else if (subcommand_ == "import") {
Status s = svr->cluster_->ImportSlot(conn, static_cast<int>(slot_), state_);
if (s.IsOK()) {
- *output = Redis::SimpleString("OK");
+ *output = Redis::SimpleString("OK");
} else {
*output = Redis::Error(s.Msg());
}
@@ -4869,8 +4814,7 @@ class CommandClusterX : public Commander {
subcommand_ = Util::ToLower(args[1]);
if (args.size() == 2 && (subcommand_ == "version")) return Status::OK();
- if (subcommand_ == "setnodeid" && args_.size() == 3 &&
- args_[2].size() == kClusterNodeIdLen) return Status::OK();
+ if (subcommand_ == "setnodeid" && args_.size() == 3 && args_[2].size() == kClusterNodeIdLen) return Status::OK();
if (subcommand_ == "migrate") {
if (args.size() != 4) return Status(Status::RedisParseErr, errWrongNumOfArguments);
auto s = Util::DecimalStringToNum(args[2], &slot_);
@@ -4897,7 +4841,7 @@ class CommandClusterX : public Commander {
if (subcommand_ == "setslot" && args_.size() == 6) {
auto parse_id = ParseInt<int>(args[2].c_str(), 10);
if (!parse_id) {
- return Status(Status::RedisParseErr, errValueNotInteger);
+ return Status(Status::RedisParseErr, errValueNotInteger);
}
slot_id_ = *parse_id;
if (!Cluster::IsValidSlot(slot_id_)) {
@@ -4911,14 +4855,13 @@ class CommandClusterX : public Commander {
}
auto parse_version = ParseInt<uint64_t>(args[5].c_str(), 10);
if (!parse_version) {
- return Status(Status::RedisParseErr, errValueNotInteger);
+ return Status(Status::RedisParseErr, errValueNotInteger);
}
set_version_ = *parse_version;
if (set_version_ < 0) return Status(Status::RedisParseErr, "Invalid version");
return Status::OK();
}
- return Status(Status::RedisParseErr,
- "CLUSTERX command, CLUSTERX VERSION|SETNODEID|SETNODES|SETSLOT|MIGRATE");
+ return Status(Status::RedisParseErr, "CLUSTERX command, CLUSTERX VERSION|SETNODEID|SETNODES|SETSLOT|MIGRATE");
}
Status Execute(Server *svr, Connection *conn, std::string *output) override {
@@ -4981,9 +4924,7 @@ class CommandClusterX : public Commander {
class CommandEval : public Commander {
public:
- Status Parse(const std::vector<std::string> &args) override {
- return Status::OK();
- }
+ Status Parse(const std::vector<std::string> &args) override { return Status::OK(); }
Status Execute(Server *svr, Connection *conn, std::string *output) override {
return Lua::evalGenericCommand(conn, args_, false, output);
@@ -4994,7 +4935,7 @@ class CommandEvalSHA : public Commander {
public:
Status Parse(const std::vector<std::string> &args) override {
if (args[1].size() != 40) {
- return Status(Status::NotOK, "NOSCRIPT No matching script. Please use EVAL");
+ return Status(Status::NotOK, "NOSCRIPT No matching script. Please use EVAL");
}
return Status::OK();
}
@@ -5015,8 +4956,7 @@ class CommandEvalSHARO : public Commander {
public:
Status Parse(const std::vector<std::string> &args) override {
if (args[1].size() != 40) {
- return Status(Status::NotOK,
- "NOSCRIPT No matching script. Please use EVAL");
+ return Status(Status::NotOK, "NOSCRIPT No matching script. Please use EVAL");
}
return Status::OK();
}
@@ -5045,7 +4985,7 @@ class CommandScript : public Commander {
svr->Propagate(Engine::kPropagateScriptCommand, args_);
*output = Redis::SimpleString("OK");
} else if (args_.size() >= 2 && subcommand_ == "exists") {
- *output = Redis::MultiLen(args_.size()-2);
+ *output = Redis::MultiLen(args_.size() - 2);
for (size_t j = 2; j < args_.size(); j++) {
if (svr->ScriptExists(args_[j]).IsOK()) {
*output += Redis::Integer(1);
@@ -5076,7 +5016,7 @@ class CommandXAdd : public Commander {
bool entry_id_found = false;
stream_name_ = args[1];
- for (size_t i = 2; i < args.size(); ) {
+ for (size_t i = 2; i < args.size();) {
auto val = entry_id_found ? args[i] : Util::ToLower(args[i]);
if (val == "nomkstream" && !entry_id_found) {
@@ -5086,17 +5026,17 @@ class CommandXAdd : public Commander {
}
if (val == "maxlen" && !entry_id_found) {
- if (i+1 >= args.size()) {
+ if (i + 1 >= args.size()) {
return Status(Status::RedisParseErr, errInvalidSyntax);
}
size_t max_len_idx;
bool eq_sign_found = false;
- if (args[i+1] == "=") {
- max_len_idx = i+2;
+ if (args[i + 1] == "=") {
+ max_len_idx = i + 2;
eq_sign_found = true;
} else {
- max_len_idx = i+1;
+ max_len_idx = i + 1;
}
if (max_len_idx >= args.size()) {
@@ -5115,17 +5055,17 @@ class CommandXAdd : public Commander {
}
if (val == "minid" && !entry_id_found) {
- if (i+1 >= args.size()) {
+ if (i + 1 >= args.size()) {
return Status(Status::RedisParseErr, errInvalidSyntax);
}
size_t min_id_idx;
bool eq_sign_found = false;
- if (args[i+1] == "=") {
- min_id_idx = i+2;
+ if (args[i + 1] == "=") {
+ min_id_idx = i + 2;
eq_sign_found = true;
} else {
- min_id_idx = i+1;
+ min_id_idx = i + 1;
}
if (min_id_idx >= args.size()) {
@@ -5347,7 +5287,7 @@ class CommandXInfo : public Commander {
} else {
output->append(Redis::BulkString("entries"));
output->append(Redis::MultiLen(info.entries.size()));
- for (const auto& e : info.entries) {
+ for (const auto &e : info.entries) {
output->append(Redis::MultiLen(2));
output->append(Redis::BulkString(e.key));
output->append(Redis::MultiBulkString(e.values));
@@ -5492,7 +5432,7 @@ class CommandXRevRange : public Commander {
count_ = *parse_result;
}
- return Status::OK();
+ return Status::OK();
}
Status Execute(Server *svr, Connection *conn, std::string *output) override {
@@ -5553,11 +5493,11 @@ class CommandXRead : public Commander {
}
if (arg == "count") {
- if (i+1 >= args.size()) {
+ if (i + 1 >= args.size()) {
return Status(Status::RedisParseErr, errInvalidSyntax);
}
with_count_ = true;
- auto parse_result = ParseInt<uint64_t>(args[i+1], 10);
+ auto parse_result = ParseInt<uint64_t>(args[i + 1], 10);
if (!parse_result) {
return Status(Status::RedisParseErr, errValueNotInteger);
}
@@ -5567,12 +5507,12 @@ class CommandXRead : public Commander {
}
if (arg == "block") {
- if (i+1 >= args.size()) {
+ if (i + 1 >= args.size()) {
return Status(Status::RedisParseErr, errInvalidSyntax);
}
block_ = true;
- auto parse_result = ParseInt<int64_t>(args[i+1], 10);
+ auto parse_result = ParseInt<int64_t>(args[i + 1], 10);
if (!parse_result) {
return Status(Status::RedisParseErr, errValueNotInteger);
}
@@ -5687,7 +5627,8 @@ class CommandXRead : public Commander {
for (size_t i = 0; i < streams_.size(); ++i) {
if (latest_marks_[i]) {
- StreamEntryID last_generated_id;;
+ StreamEntryID last_generated_id;
+ ;
auto s = stream_db->GetLastGeneratedID(streams_[i], &last_generated_id);
if (!s.ok()) {
return Status(Status::RedisExecErr, s.ToString());
@@ -5731,8 +5672,8 @@ class CommandXRead : public Commander {
}
command->unblockAll();
- bufferevent_setcb(bev, Redis::Connection::OnRead, Redis::Connection::OnWrite,
- Redis::Connection::OnEvent, command->conn_);
+ bufferevent_setcb(bev, Redis::Connection::OnRead, Redis::Connection::OnWrite, Redis::Connection::OnEvent,
+ command->conn_);
bufferevent_enable(bev, EV_READ);
Redis::Stream stream_db(command->svr_->storage_, command->conn_->GetNamespace());
@@ -5754,8 +5695,8 @@ class CommandXRead : public Commander {
if (!s.ok()) {
command->conn_->Reply(Redis::MultiLen(-1));
LOG(ERROR) << "ERR executing XRANGE for stream " << command->streams_[i] << " from "
- << command->ids_[i].ToString() << " to " << options.end.ToString()
- << " with count " << command->count_ << ": " << s.ToString();
+ << command->ids_[i].ToString() << " to " << options.end.ToString() << " with count "
+ << command->count_ << ": " << s.ToString();
}
if (result.size() > 0) {
@@ -5813,8 +5754,8 @@ class CommandXRead : public Commander {
command->unblockAll();
auto bev = command->conn_->GetBufferEvent();
- bufferevent_setcb(bev, Redis::Connection::OnRead, Redis::Connection::OnWrite,
- Redis::Connection::OnEvent, command->conn_);
+ bufferevent_setcb(bev, Redis::Connection::OnRead, Redis::Connection::OnWrite, Redis::Connection::OnEvent,
+ command->conn_);
bufferevent_enable(bev, EV_READ);
}
@@ -5831,9 +5772,7 @@ class CommandXRead : public Commander {
bool with_count_ = false;
bool block_ = false;
- void unblockAll() {
- svr_->UnblockOnStreams(streams_, conn_);
- }
+ void unblockAll() { svr_->UnblockOnStreams(streams_, conn_); }
};
class CommandXTrim : public Commander {
@@ -5927,10 +5866,11 @@ class CommandXTrim : public Commander {
StreamTrimStrategy strategy_ = StreamTrimStrategy::None;
};
-#define ADD_CMD(name, arity, description , first_key, last_key, key_step, fn) \
-{name, arity, description, 0, first_key, last_key, key_step, []() -> std::unique_ptr<Commander> { \
- return std::unique_ptr<Commander>(new fn()); \
-}}
+#define ADD_CMD(name, arity, description, first_key, last_key, key_step, fn) \
+ { \
+ name, arity, description, 0, first_key, last_key, key_step, \
+ []() -> std::unique_ptr<Commander> { return std::unique_ptr<Commander>(new fn()); } \
+ }
CommandAttributes redisCommandTable[] = {
ADD_CMD("auth", 2, "read-only ok-loading", 0, 0, 0, CommandAuth),
@@ -5956,7 +5896,7 @@ CommandAttributes redisCommandTable[] = {
ADD_CMD("command", -1, "read-only", 0, 0, 0, CommandCommand),
ADD_CMD("echo", 2, "read-only", 0, 0, 0, CommandEcho),
ADD_CMD("disk", 3, "read-only", 0, 0, 0, CommandDisk),
- ADD_CMD("hello", -1, "read-only ok-loading", 0, 0, 0, CommandHello),
+ ADD_CMD("hello", -1, "read-only ok-loading", 0, 0, 0, CommandHello),
ADD_CMD("ttl", 2, "read-only", 1, 1, 1, CommandTTL),
ADD_CMD("pttl", 2, "read-only", 1, 1, 1, CommandPTTL),
@@ -6142,17 +6082,11 @@ std::map<std::string, CommandAttributes *> commands;
// Original Command table before rename-command directive
std::map<std::string, CommandAttributes *> original_commands;
-int GetCommandNum() {
- return sizeof(redisCommandTable) / sizeof(struct CommandAttributes);
-}
+int GetCommandNum() { return sizeof(redisCommandTable) / sizeof(struct CommandAttributes); }
-std::map<std::string, CommandAttributes *> *GetCommands() {
- return &commands;
-}
+std::map<std::string, CommandAttributes *> *GetCommands() { return &commands; }
-std::map<std::string, CommandAttributes *> *GetOriginalCommands() {
- return &original_commands;
-}
+std::map<std::string, CommandAttributes *> *GetOriginalCommands() { return &original_commands; }
void PopulateCommands() {
for (int i = 0; i < GetCommandNum(); i++) {
diff --git a/src/redis_cmd.h b/src/redis_cmd.h
index 8bdddf8..857e111 100644
--- a/src/redis_cmd.h
+++ b/src/redis_cmd.h
@@ -44,29 +44,25 @@ class Connection;
struct CommandAttributes;
enum CommandFlags {
- kCmdWrite = (1ULL<<0), // "write" flag
- kCmdReadOnly = (1ULL<<1), // "read-only" flag
- kCmdReplication = (1ULL<<2), // "replication" flag
- kCmdPubSub = (1ULL<<3), // "pub-sub" flag
- kCmdScript = (1ULL<<4), // "script" flag
- kCmdLoading = (1ULL<<5), // "ok-loading" flag
- kCmdMulti = (1ULL<<6), // "multi" flag
- kCmdExclusive = (1ULL<<7), // "exclusive" flag
- kCmdNoMulti = (1ULL<<8), // "no-multi" flag
- kCmdNoScript = (1ULL<<9), // "noscript" flag
+ kCmdWrite = (1ULL << 0), // "write" flag
+ kCmdReadOnly = (1ULL << 1), // "read-only" flag
+ kCmdReplication = (1ULL << 2), // "replication" flag
+ kCmdPubSub = (1ULL << 3), // "pub-sub" flag
+ kCmdScript = (1ULL << 4), // "script" flag
+ kCmdLoading = (1ULL << 5), // "ok-loading" flag
+ kCmdMulti = (1ULL << 6), // "multi" flag
+ kCmdExclusive = (1ULL << 7), // "exclusive" flag
+ kCmdNoMulti = (1ULL << 8), // "no-multi" flag
+ kCmdNoScript = (1ULL << 9), // "noscript" flag
};
class Commander {
public:
void SetAttributes(const CommandAttributes *attributes) { attributes_ = attributes; }
- const CommandAttributes* GetAttributes() { return attributes_; }
+ const CommandAttributes *GetAttributes() { return attributes_; }
void SetArgs(const std::vector<std::string> &args) { args_ = args; }
- const std::vector<std::string>* Args() {
- return &args_;
- }
- virtual Status Parse(const std::vector<std::string> &args) {
- return Status::OK();
- }
+ const std::vector<std::string> *Args() { return &args_; }
+ virtual Status Parse(const std::vector<std::string> &args) { return Status::OK(); }
virtual Status Execute(Server *svr, Connection *conn, std::string *output) {
return Status(Status::RedisExecErr, "not implemented");
}
diff --git a/src/redis_connection.cc b/src/redis_connection.cc
index bf686b2..8b49ace 100644
--- a/src/redis_connection.cc
+++ b/src/redis_connection.cc
@@ -54,18 +54,10 @@ Connection::~Connection() {
std::string Connection::ToString() {
std::ostringstream stream;
- stream << "id=" << id_
- << " addr=" << addr_
- << " fd=" << bufferevent_getfd(bev_)
- << " name=" << name_
- << " age=" << GetAge()
- << " idle=" << GetIdleTime()
- << " flags=" << GetFlags()
- << " namespace=" << ns_
- << " qbuf=" << evbuffer_get_length(Input())
- << " obuf=" << evbuffer_get_length(Output())
- << " cmd=" << last_cmd_
- << "\n";
+ stream << "id=" << id_ << " addr=" << addr_ << " fd=" << bufferevent_getfd(bev_) << " name=" << name_
+ << " age=" << GetAge() << " idle=" << GetIdleTime() << " flags=" << GetFlags() << " namespace=" << ns_
+ << " qbuf=" << evbuffer_get_length(Input()) << " obuf=" << evbuffer_get_length(Output())
+ << " cmd=" << last_cmd_ << "\n";
return stream.str();
}
@@ -96,8 +88,7 @@ void Connection::OnRead(struct bufferevent *bev, void *ctx) {
void Connection::OnWrite(struct bufferevent *bev, void *ctx) {
auto conn = static_cast<Connection *>(ctx);
- if (conn->IsFlagEnabled(kCloseAfterReply) ||
- conn->IsFlagEnabled(kCloseAsync)) {
+ if (conn->IsFlagEnabled(kCloseAfterReply) || conn->IsFlagEnabled(kCloseAsync)) {
conn->Close();
}
}
@@ -106,23 +97,21 @@ void Connection::OnEvent(bufferevent *bev, int16_t events, void *ctx) {
auto conn = static_cast<Connection *>(ctx);
if (events & BEV_EVENT_ERROR) {
LOG(ERROR) << "[connection] Going to remove the client: " << conn->GetAddr()
- << ", while encounter error: "
- << evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())
+ << ", while encounter error: " << evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR())
#ifdef ENABLE_OPENSSL
- << ", SSL Error: " << SSLError(bufferevent_get_openssl_error(bev)) // NOLINT
+ << ", SSL Error: " << SSLError(bufferevent_get_openssl_error(bev)) // NOLINT
#endif
- ; // NOLINT
+ ; // NOLINT
conn->Close();
return;
}
if (events & BEV_EVENT_EOF) {
- DLOG(INFO) << "[connection] Going to remove the client: " << conn->GetAddr()
- << ", while closed by client";
+ DLOG(INFO) << "[connection] Going to remove the client: " << conn->GetAddr() << ", while closed by client";
conn->Close();
return;
}
if (events & BEV_EVENT_TIMEOUT) {
- DLOG(INFO) << "[connection] The client: " << conn->GetAddr() << "] reached timeout";
+ DLOG(INFO) << "[connection] The client: " << conn->GetAddr() << "] reached timeout";
bufferevent_enable(bev, EV_READ | EV_WRITE);
}
}
@@ -216,16 +205,13 @@ void Connection::UnSubscribeAll(unsubscribe_callback reply) {
owner_->svr_->UnSubscribeChannel(chan, this);
removed++;
if (reply != nullptr) {
- reply(chan, static_cast<int>(subscribe_channels_.size() -
- removed + subcribe_patterns_.size()));
+ reply(chan, static_cast<int>(subscribe_channels_.size() - removed + subcribe_patterns_.size()));
}
}
subscribe_channels_.clear();
}
-int Connection::SubscriptionsCount() {
- return static_cast<int>(subscribe_channels_.size());
-}
+int Connection::SubscriptionsCount() { return static_cast<int>(subscribe_channels_.size()); }
void Connection::PSubscribeChannel(const std::string &pattern) {
for (const auto &p : subcribe_patterns_) {
@@ -257,16 +243,13 @@ void Connection::PUnSubscribeAll(unsubscribe_callback reply) {
owner_->svr_->PUnSubscribeChannel(pattern, this);
removed++;
if (reply != nullptr) {
- reply(pattern, static_cast<int>(subcribe_patterns_.size() -
- removed + subscribe_channels_.size()));
+ reply(pattern, static_cast<int>(subcribe_patterns_.size() - removed + subscribe_channels_.size()));
}
}
subcribe_patterns_.clear();
}
-int Connection::PSubscriptionsCount() {
- return static_cast<int>(subcribe_patterns_.size());
-}
+int Connection::PSubscriptionsCount() { return static_cast<int>(subcribe_patterns_.size()); }
bool Connection::isProfilingEnabled(const std::string &cmd) {
auto config = svr_->GetConfig();
@@ -275,8 +258,7 @@ bool Connection::isProfilingEnabled(const std::string &cmd) {
config->profiling_sample_commands.find(cmd) == config->profiling_sample_commands.end()) {
return false;
}
- if (config->profiling_sample_ratio == 100 ||
- std::rand() % 100 <= config->profiling_sample_ratio) {
+ if (config->profiling_sample_ratio == 100 || std::rand() % 100 <= config->profiling_sample_ratio) {
rocksdb::SetPerfLevel(rocksdb::PerfLevel::kEnableTimeExceptForMutex);
rocksdb::get_perf_context()->Reset();
rocksdb::get_iostats_context()->Reset();
@@ -287,7 +269,7 @@ bool Connection::isProfilingEnabled(const std::string &cmd) {
void Connection::recordProfilingSampleIfNeed(const std::string &cmd, uint64_t duration) {
int threshold = svr_->GetConfig()->profiling_sample_record_threshold_ms;
- if (threshold > 0 && static_cast<int>(duration/1000) < threshold) {
+ if (threshold > 0 && static_cast<int>(duration / 1000) < threshold) {
rocksdb::SetPerfLevel(rocksdb::PerfLevel::kDisable);
return;
}
@@ -312,8 +294,7 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
auto cmd_tokens = to_process_cmds->front();
to_process_cmds->pop_front();
- if (IsFlagEnabled(Redis::Connection::kCloseAfterReply) &&
- !IsFlagEnabled(Connection::kMultiExec)) break;
+ if (IsFlagEnabled(Redis::Connection::kCloseAfterReply) && !IsFlagEnabled(Connection::kMultiExec)) break;
auto s = svr_->LookupAndCreateCommand(cmd_tokens.front(), ¤t_cmd_);
if (!s.IsOK()) {
@@ -337,7 +318,7 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
const auto attributes = current_cmd_->GetAttributes();
auto cmd_name = attributes->name;
- std::unique_ptr<RWLock::ReadLock> concurrency; // Allow concurrency
+ std::unique_ptr<RWLock::ReadLock> concurrency; // Allow concurrency
std::unique_ptr<RWLock::WriteLock> exclusivity; // Need exclusivity
// If the command need to process exclusively, we need to get 'ExclusivityGuard'
// that can guarantee other threads can't come into critical zone, such as DEBUG,
@@ -347,12 +328,9 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
if (IsFlagEnabled(Connection::kMultiExec) && attributes->name != "exec") {
// No lock guard, because 'exec' command has acquired 'WorkExclusivityGuard'
} else if (attributes->is_exclusive() ||
- (cmd_name == "config" && cmd_tokens.size() == 2 &&
- !strcasecmp(cmd_tokens[1].c_str(), "set")) ||
- (config->cluster_enabled &&
- (cmd_name == "clusterx" || cmd_name == "cluster") &&
- cmd_tokens.size() >= 2 &&
- Cluster::SubCommandIsExecExclusive(cmd_tokens[1]))) {
+ (cmd_name == "config" && cmd_tokens.size() == 2 && !strcasecmp(cmd_tokens[1].c_str(), "set")) ||
+ (config->cluster_enabled && (cmd_name == "clusterx" || cmd_name == "cluster") &&
+ cmd_tokens.size() >= 2 && Cluster::SubCommandIsExecExclusive(cmd_tokens[1]))) {
exclusivity = svr_->WorkExclusivityGuard();
// When executing lua script commands that have "exclusive" attribute,
@@ -377,8 +355,7 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
}
int arity = attributes->arity;
int tokens = static_cast<int>(cmd_tokens.size());
- if ((arity > 0 && tokens != arity)
- || (arity < 0 && tokens < -arity)) {
+ if ((arity > 0 && tokens != arity) || (arity < 0 && tokens < -arity)) {
if (IsFlagEnabled(Connection::kMultiExec)) multi_error_ = true;
Reply(Redis::Error("ERR wrong number of arguments"));
continue;
@@ -387,7 +364,7 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
s = current_cmd_->Parse(cmd_tokens);
if (!s.IsOK()) {
if (IsFlagEnabled(Connection::kMultiExec)) multi_error_ = true;
- Reply(Redis::Error("ERR "+s.Msg()));
+ Reply(Redis::Error("ERR " + s.Msg()));
continue;
}
@@ -418,11 +395,11 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
Reply(Redis::Error("READONLY You can't write against a read only slave."));
continue;
}
- if (!config->slave_serve_stale_data && svr_->IsSlave()
- && cmd_name != "info" && cmd_name != "slaveof"
- && svr_->GetReplicationState() != kReplConnected) {
- Reply(Redis::Error("MASTERDOWN Link with MASTER is down "
- "and slave-serve-stale-data is set to 'no'."));
+ if (!config->slave_serve_stale_data && svr_->IsSlave() && cmd_name != "info" && cmd_name != "slaveof" &&
+ svr_->GetReplicationState() != kReplConnected) {
+ Reply(
+ Redis::Error("MASTERDOWN Link with MASTER is down "
+ "and slave-serve-stale-data is set to 'no'."));
continue;
}
@@ -432,7 +409,7 @@ void Connection::ExecuteCommands(std::deque<CommandTokens> *to_process_cmds) {
bool is_profiling = isProfilingEnabled(cmd_name);
s = current_cmd_->Execute(svr_, this, &reply);
auto end = std::chrono::high_resolution_clock::now();
- uint64_t duration = std::chrono::duration_cast<std::chrono::microseconds>(end-start).count();
+ uint64_t duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
if (is_profiling) recordProfilingSampleIfNeed(cmd_name, duration);
svr_->SlowlogPushEntryIfNeeded(current_cmd_->Args(), duration);
svr_->stats_.IncrLatency(static_cast<uint64_t>(duration), cmd_name);
diff --git a/src/redis_connection.h b/src/redis_connection.h
index 78c0b2d..0bc024b 100644
--- a/src/redis_connection.h
+++ b/src/redis_connection.h
@@ -37,11 +37,11 @@ namespace Redis {
class Connection {
public:
enum Flag {
- kSlave = 1 << 4,
- kMonitor = 1 << 5,
+ kSlave = 1 << 4,
+ kMonitor = 1 << 5,
kCloseAfterReply = 1 << 6,
- kCloseAsync = 1 << 7,
- kMultiExec = 1 << 8,
+ kCloseAsync = 1 << 7,
+ kMultiExec = 1 << 8,
};
explicit Connection(bufferevent *bev, Worker *owner);
diff --git a/src/redis_db.cc b/src/redis_db.cc
index 898c944..6e004a2 100644
--- a/src/redis_db.cc
+++ b/src/redis_db.cc
@@ -18,15 +18,17 @@
*
*/
-#include "redis_slot.h"
#include "redis_db.h"
+
#include <ctime>
#include <map>
+
+#include "db_util.h"
+#include "parse_util.h"
+#include "redis_slot.h"
#include "rocksdb/iterator.h"
#include "server.h"
#include "util.h"
-#include "db_util.h"
-#include "parse_util.h"
namespace Redis {
@@ -49,8 +51,8 @@ rocksdb::Status Database::GetMetadata(RedisType type, const Slice &ns_key, Metad
metadata->Decode(old_metadata);
return rocksdb::Status::NotFound(kErrMsgKeyExpired);
}
- if (metadata->Type() != type
- && (metadata->size > 0 || metadata->Type() == kRedisString || metadata->Type() == kRedisStream)) {
+ if (metadata->Type() != type &&
+ (metadata->size > 0 || metadata->Type() == kRedisString || metadata->Type() == kRedisStream)) {
metadata->Decode(old_metadata);
return rocksdb::Status::InvalidArgument(kErrMsgWrongType);
}
@@ -95,13 +97,13 @@ rocksdb::Status Database::Expire(const Slice &user_key, int timestamp) {
char *buf = new char[value.size()];
memcpy(buf, value.data(), value.size());
// +1 to skip the flags
- EncodeFixed32(buf + 1, (uint32_t) timestamp);
+ EncodeFixed32(buf + 1, (uint32_t)timestamp);
rocksdb::WriteBatch batch;
WriteBatchLogData log_data(kRedisNone, {std::to_string(kRedisCmdExpire)});
batch.PutLogData(log_data.Encode());
batch.Put(metadata_cf_handle_, ns_key, Slice(buf, value.size()));
s = storage_->Write(storage_->DefaultWriteOptions(), &batch);
- delete[]buf;
+ delete[] buf;
return s;
}
@@ -159,9 +161,7 @@ rocksdb::Status Database::TTL(const Slice &user_key, int *ttl) {
return rocksdb::Status::OK();
}
-void Database::GetKeyNumStats(const std::string &prefix, KeyNumStats *stats) {
- Keys(prefix, nullptr, stats);
-}
+void Database::GetKeyNumStats(const std::string &prefix, KeyNumStats *stats) { Keys(prefix, nullptr, stats); }
void Database::Keys(std::string prefix, std::vector<std::string> *keys, KeyNumStats *stats) {
uint16_t slot_id = 0;
@@ -226,11 +226,8 @@ void Database::Keys(std::string prefix, std::vector<std::string> *keys, KeyNumSt
}
}
-rocksdb::Status Database::Scan(const std::string &cursor,
- uint64_t limit,
- const std::string &prefix,
- std::vector<std::string> *keys,
- std::string *end_cursor) {
+rocksdb::Status Database::Scan(const std::string &cursor, uint64_t limit, const std::string &prefix,
+ std::vector<std::string> *keys, std::string *end_cursor) {
end_cursor->clear();
uint64_t cnt = 0;
uint16_t slot_id = 0, slot_start = 0;
@@ -282,7 +279,7 @@ rocksdb::Status Database::Scan(const std::string &cursor,
if (!storage_->IsSlotIdEncoded() || prefix.empty()) {
if (!keys->empty()) {
- end_cursor->append(user_key);
+ end_cursor->append(user_key);
}
break;
}
@@ -454,10 +451,8 @@ void Database::AppendNamespacePrefix(const Slice &user_key, std::string *output)
ComposeNamespaceKey(namespace_, user_key, output, storage_->IsSlotIdEncoded());
}
-rocksdb::Status Database::FindKeyRangeWithPrefix(const std::string &prefix,
- const std::string &prefix_end,
- std::string *begin,
- std::string *end,
+rocksdb::Status Database::FindKeyRangeWithPrefix(const std::string &prefix, const std::string &prefix_end,
+ std::string *begin, std::string *end,
rocksdb::ColumnFamilyHandle *cf_handle) {
if (cf_handle == nullptr) {
cf_handle = metadata_cf_handle_;
@@ -520,9 +515,7 @@ rocksdb::Status Database::ClearKeysOfSlot(const rocksdb::Slice &ns, int slot) {
return rocksdb::Status::OK();
}
-rocksdb::Status Database::GetSlotKeysInfo(int slot,
- std::map<int, uint64_t> *slotskeys,
- std::vector<std::string> *keys,
+rocksdb::Status Database::GetSlotKeysInfo(int slot, std::map<int, uint64_t> *slotskeys, std::vector<std::string> *keys,
int count) {
const rocksdb::Snapshot *snapshot;
snapshot = storage_->GetDB()->GetSnapshot();
@@ -562,12 +555,8 @@ rocksdb::Status Database::GetSlotKeysInfo(int slot,
return rocksdb::Status::OK();
}
-rocksdb::Status SubKeyScanner::Scan(RedisType type,
- const Slice &user_key,
- const std::string &cursor,
- uint64_t limit,
- const std::string &subkey_prefix,
- std::vector<std::string> *keys,
+rocksdb::Status SubKeyScanner::Scan(RedisType type, const Slice &user_key, const std::string &cursor, uint64_t limit,
+ const std::string &subkey_prefix, std::vector<std::string> *keys,
std::vector<std::string> *values) {
uint64_t cnt = 0;
std::string ns_key;
@@ -616,13 +605,9 @@ rocksdb::Status SubKeyScanner::Scan(RedisType type,
return rocksdb::Status::OK();
}
-RedisType WriteBatchLogData::GetRedisType() {
- return type_;
-}
+RedisType WriteBatchLogData::GetRedisType() { return type_; }
-std::vector<std::string> *WriteBatchLogData::GetArguments() {
- return &args_;
-}
+std::vector<std::string> *WriteBatchLogData::GetArguments() { return &args_; }
std::string WriteBatchLogData::Encode() {
std::string ret = std::to_string(type_);
@@ -633,13 +618,13 @@ std::string WriteBatchLogData::Encode() {
}
Status WriteBatchLogData::Decode(const rocksdb::Slice &blob) {
- const std::string& log_data = blob.ToString();
+ const std::string &log_data = blob.ToString();
std::vector<std::string> args = Util::Split(log_data, " ");
auto parse_result = ParseInt<int>(args[0], 10);
if (!parse_result) {
return parse_result.ToStatus();
}
- type_ = static_cast<RedisType >(*parse_result);
+ type_ = static_cast<RedisType>(*parse_result);
args_ = std::vector<std::string>(args.begin() + 1, args.end());
return Status::OK();
diff --git a/src/redis_db.h b/src/redis_db.h
index 11da203..1a44449 100644
--- a/src/redis_db.h
+++ b/src/redis_db.h
@@ -20,10 +20,10 @@
#pragma once
+#include <map>
#include <string>
-#include <vector>
#include <utility>
-#include <map>
+#include <vector>
#include "redis_metadata.h"
#include "storage.h"
@@ -45,22 +45,14 @@ class Database {
rocksdb::Status FlushAll();
void GetKeyNumStats(const std::string &prefix, KeyNumStats *stats);
void Keys(std::string prefix, std::vector<std::string> *keys = nullptr, KeyNumStats *stats = nullptr);
- rocksdb::Status Scan(const std::string &cursor,
- uint64_t limit,
- const std::string &prefix,
- std::vector<std::string> *keys,
- std::string *end_cursor = nullptr);
+ rocksdb::Status Scan(const std::string &cursor, uint64_t limit, const std::string &prefix,
+ std::vector<std::string> *keys, std::string *end_cursor = nullptr);
rocksdb::Status RandomKey(const std::string &cursor, std::string *key);
void AppendNamespacePrefix(const Slice &user_key, std::string *output);
- rocksdb::Status FindKeyRangeWithPrefix(const std::string &prefix,
- const std::string &prefix_end,
- std::string *begin,
- std::string *end,
- rocksdb::ColumnFamilyHandle *cf_handle = nullptr);
+ rocksdb::Status FindKeyRangeWithPrefix(const std::string &prefix, const std::string &prefix_end, std::string *begin,
+ std::string *end, rocksdb::ColumnFamilyHandle *cf_handle = nullptr);
rocksdb::Status ClearKeysOfSlot(const rocksdb::Slice &ns, int slot);
- rocksdb::Status GetSlotKeysInfo(int slot,
- std::map<int, uint64_t> *slotskeys,
- std::vector<std::string> *keys,
+ rocksdb::Status GetSlotKeysInfo(int slot, std::map<int, uint64_t> *slotskeys, std::vector<std::string> *keys,
int count);
protected:
@@ -71,13 +63,10 @@ class Database {
class LatestSnapShot {
public:
- explicit LatestSnapShot(rocksdb::DB *db) : db_(db) {
- snapshot_ = db_->GetSnapshot();
- }
- ~LatestSnapShot() {
- db_->ReleaseSnapshot(snapshot_);
- }
+ explicit LatestSnapShot(rocksdb::DB *db) : db_(db) { snapshot_ = db_->GetSnapshot(); }
+ ~LatestSnapShot() { db_->ReleaseSnapshot(snapshot_); }
const rocksdb::Snapshot *GetSnapShot() { return snapshot_; }
+
private:
rocksdb::DB *db_ = nullptr;
const rocksdb::Snapshot *snapshot_ = nullptr;
@@ -86,14 +75,9 @@ class Database {
class SubKeyScanner : public Redis::Database {
public:
- explicit SubKeyScanner(Engine::Storage *storage, const std::string &ns)
- : Database(storage, ns) {}
- rocksdb::Status Scan(RedisType type,
- const Slice &user_key,
- const std::string &cursor,
- uint64_t limit,
- const std::string &subkey_prefix,
- std::vector<std::string> *keys,
+ explicit SubKeyScanner(Engine::Storage *storage, const std::string &ns) : Database(storage, ns) {}
+ rocksdb::Status Scan(RedisType type, const Slice &user_key, const std::string &cursor, uint64_t limit,
+ const std::string &subkey_prefix, std::vector<std::string> *keys,
std::vector<std::string> *values = nullptr);
};
@@ -101,8 +85,7 @@ class WriteBatchLogData {
public:
WriteBatchLogData() = default;
explicit WriteBatchLogData(RedisType type) : type_(type) {}
- explicit WriteBatchLogData(RedisType type, std::vector<std::string> &&args) :
- type_(type), args_(std::move(args)) {}
+ explicit WriteBatchLogData(RedisType type, std::vector<std::string> &&args) : type_(type), args_(std::move(args)) {}
RedisType GetRedisType();
std::vector<std::string> *GetArguments();
@@ -115,4 +98,3 @@ class WriteBatchLogData {
};
} // namespace Redis
-
diff --git a/src/redis_disk.cc b/src/redis_disk.cc
index 02b71d7..b4d8873 100644
--- a/src/redis_disk.cc
+++ b/src/redis_disk.cc
@@ -18,36 +18,33 @@
*
*/
-#include <vector>
-#include <memory>
-#include <utility>
+#include "redis_disk.h"
+
#include <algorithm>
+#include <memory>
#include <string>
+#include <utility>
+#include <vector>
#include "db_util.h"
+#include "redis_bitmap.h"
#include "redis_metadata.h"
#include "redis_sortedint.h"
-#include "rocksdb/status.h"
#include "redis_zset.h"
-#include "redis_bitmap.h"
-#include "redis_disk.h"
+#include "rocksdb/status.h"
#include "status.h"
namespace Redis {
rocksdb::Status Disk::GetApproximateSizes(const Metadata &metadata, const Slice &ns_key,
- rocksdb::ColumnFamilyHandle *column_family,
- uint64_t *key_size, Slice subkeyleft,
- Slice subkeyright) {
+ rocksdb::ColumnFamilyHandle *column_family, uint64_t *key_size,
+ Slice subkeyleft, Slice subkeyright) {
std::string prefix_key, next_version_prefix_key;
- InternalKey(ns_key, subkeyleft, metadata.version,
- storage_->IsSlotIdEncoded()).Encode(&prefix_key);
- InternalKey(ns_key, subkeyright, metadata.version + 1,
- storage_->IsSlotIdEncoded()).Encode(&next_version_prefix_key);
+ InternalKey(ns_key, subkeyleft, metadata.version, storage_->IsSlotIdEncoded()).Encode(&prefix_key);
+ InternalKey(ns_key, subkeyright, metadata.version + 1, storage_->IsSlotIdEncoded()).Encode(&next_version_prefix_key);
auto key_range = rocksdb::Range(prefix_key, next_version_prefix_key);
uint64_t tmp_size = 0;
- rocksdb::Status s = db_->GetApproximateSizes(option_, column_family,
- &key_range, 1, &tmp_size);
+ rocksdb::Status s = db_->GetApproximateSizes(option_, column_family, &key_range, 1, &tmp_size);
if (!s.ok()) return s;
*key_size += tmp_size;
return rocksdb::Status::OK();
@@ -88,18 +85,14 @@ rocksdb::Status Disk::GetHashSize(const Slice &ns_key, uint64_t *key_size) {
HashMetadata metadata(false);
rocksdb::Status s = Database::GetMetadata(kRedisHash, ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
- return GetApproximateSizes(metadata, ns_key,
- storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName),
- key_size);
+ return GetApproximateSizes(metadata, ns_key, storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName), key_size);
}
rocksdb::Status Disk::GetSetSize(const Slice &ns_key, uint64_t *key_size) {
SetMetadata metadata(false);
rocksdb::Status s = Database::GetMetadata(kRedisSet, ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
- return GetApproximateSizes(metadata, ns_key,
- storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName),
- key_size);
+ return GetApproximateSizes(metadata, ns_key, storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName), key_size);
}
rocksdb::Status Disk::GetListSize(const Slice &ns_key, uint64_t *key_size) {
@@ -108,9 +101,7 @@ rocksdb::Status Disk::GetListSize(const Slice &ns_key, uint64_t *key_size) {
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
std::string buf;
PutFixed64(&buf, metadata.head);
- return GetApproximateSizes(metadata, ns_key,
- storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName),
- key_size, buf);
+ return GetApproximateSizes(metadata, ns_key, storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName), key_size, buf);
}
rocksdb::Status Disk::GetZsetSize(const Slice &ns_key, uint64_t *key_size) {
@@ -119,22 +110,18 @@ rocksdb::Status Disk::GetZsetSize(const Slice &ns_key, uint64_t *key_size) {
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
std::string score_bytes;
PutDouble(&score_bytes, kMinScore);
- s = GetApproximateSizes(metadata, ns_key,
- storage_->GetCFHandle(Engine::kZSetScoreColumnFamilyName),
- key_size, score_bytes, score_bytes);
+ s = GetApproximateSizes(metadata, ns_key, storage_->GetCFHandle(Engine::kZSetScoreColumnFamilyName), key_size,
+ score_bytes, score_bytes);
if (!s.ok()) return s;
- return GetApproximateSizes(metadata, ns_key,
- storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName),
- key_size);
+ return GetApproximateSizes(metadata, ns_key, storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName), key_size);
}
rocksdb::Status Disk::GetBitmapSize(const Slice &ns_key, uint64_t *key_size) {
BitmapMetadata metadata(false);
rocksdb::Status s = Database::GetMetadata(kRedisBitmap, ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
- return GetApproximateSizes(metadata, ns_key,
- storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName),
- key_size, std::to_string(0), std::to_string(0));
+ return GetApproximateSizes(metadata, ns_key, storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName), key_size,
+ std::to_string(0), std::to_string(0));
}
rocksdb::Status Disk::GetSortedintSize(const Slice &ns_key, uint64_t *key_size) {
@@ -143,18 +130,15 @@ rocksdb::Status Disk::GetSortedintSize(const Slice &ns_key, uint64_t *key_size)
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
std::string start_buf;
PutFixed64(&start_buf, 0);
- return GetApproximateSizes(metadata, ns_key,
- storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName),
- key_size, start_buf, start_buf);
+ return GetApproximateSizes(metadata, ns_key, storage_->GetCFHandle(Engine::kSubkeyColumnFamilyName), key_size,
+ start_buf, start_buf);
}
rocksdb::Status Disk::GetStreamSize(const Slice &ns_key, uint64_t *key_size) {
StreamMetadata metadata(false);
rocksdb::Status s = Database::GetMetadata(kRedisStream, ns_key, &metadata);
if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
- return GetApproximateSizes(metadata, ns_key,
- storage_->GetCFHandle(Engine::kStreamColumnFamilyName),
- key_size);
+ return GetApproximateSizes(metadata, ns_key, storage_->GetCFHandle(Engine::kStreamColumnFamilyName), key_size);
}
} // namespace Redis
diff --git a/src/redis_disk.h b/src/redis_disk.h
index d9b9fe7..ed05c3b 100644
--- a/src/redis_disk.h
+++ b/src/redis_disk.h
@@ -21,6 +21,7 @@
#pragma once
#include <string>
+
#include "redis_db.h"
#include "redis_metadata.h"
@@ -28,14 +29,13 @@ namespace Redis {
class Disk : public Database {
public:
- explicit Disk(Engine::Storage *storage, const std::string &ns): Database(storage, ns) {
+ explicit Disk(Engine::Storage *storage, const std::string &ns) : Database(storage, ns) {
option_.include_memtabtles = true;
option_.include_files = true;
}
rocksdb::Status GetApproximateSizes(const Metadata &metadata, const Slice &ns_key,
- rocksdb::ColumnFamilyHandle *column_family,
- uint64_t *key_size, Slice subkeyleft = Slice(),
- Slice subkeyright = Slice());
+ rocksdb::ColumnFamilyHandle *column_family, uint64_t *key_size,
+ Slice subkeyleft = Slice(), Slice subkeyright = Slice());
rocksdb::Status GetStringSize(const Slice &ns_key, uint64_t *key_size);
rocksdb::Status GetHashSize(const Slice &ns_key, uint64_t *key_size);
rocksdb::Status GetSetSize(const Slice &ns_key, uint64_t *key_size);
@@ -45,6 +45,7 @@ class Disk : public Database {
rocksdb::Status GetSortedintSize(const Slice &ns_key, uint64_t *key_size);
rocksdb::Status GetStreamSize(const Slice &ns_key, uint64_t *key_size);
rocksdb::Status GetKeySize(const Slice &user_key, RedisType type, uint64_t *key_size);
+
private:
rocksdb::SizeApproximationOptions option_;
};
diff --git a/src/redis_geo.cc b/src/redis_geo.cc
index ecffbd1..293ca37 100644
--- a/src/redis_geo.cc
+++ b/src/redis_geo.cc
@@ -21,6 +21,7 @@
#include "redis_geo.h"
#include <algorithm>
+
#include "util.h"
namespace Redis {
@@ -37,10 +38,7 @@ rocksdb::Status Geo::Add(const Slice &user_key, std::vector<GeoPoint> *geo_point
return ZSet::Add(user_key, 0, &member_scores, ret);
}
-rocksdb::Status Geo::Dist(const Slice &user_key,
- const Slice &member_1,
- const Slice &member_2,
- double *dist) {
+rocksdb::Status Geo::Dist(const Slice &user_key, const Slice &member_1, const Slice &member_2, double *dist) {
std::map<std::string, GeoPoint> geo_points;
auto s = MGet(user_key, {member_1, member_2}, &geo_points);
if (!s.ok()) return s;
@@ -51,15 +49,13 @@ rocksdb::Status Geo::Dist(const Slice &user_key,
return rocksdb::Status::NotFound();
}
}
- *dist = GeoHashHelper::GetDistance(geo_points[member_1.ToString()].longitude,
- geo_points[member_1.ToString()].latitude,
- geo_points[member_2.ToString()].longitude,
- geo_points[member_2.ToString()].latitude);
+ *dist =
+ GeoHashHelper::GetDistance(geo_points[member_1.ToString()].longitude, geo_points[member_1.ToString()].latitude,
+ geo_points[member_2.ToString()].longitude, geo_points[member_2.ToString()].latitude);
return rocksdb::Status::OK();
}
-rocksdb::Status Geo::Hash(const Slice &user_key,
- const std::vector<Slice> &members,
+rocksdb::Status Geo::Hash(const Slice &user_key, const std::vector<Slice> &members,
std::vector<std::string> *geoHashes) {
std::map<std::string, GeoPoint> geo_points;
auto s = MGet(user_key, members, &geo_points);
@@ -77,22 +73,14 @@ rocksdb::Status Geo::Hash(const Slice &user_key,
return rocksdb::Status::OK();
}
-rocksdb::Status Geo::Pos(const Slice &user_key,
- const std::vector<Slice> &members,
+rocksdb::Status Geo::Pos(const Slice &user_key, const std::vector<Slice> &members,
std::map<std::string, GeoPoint> *geo_points) {
return MGet(user_key, members, geo_points);
}
-rocksdb::Status Geo::Radius(const Slice &user_key,
- double longitude,
- double latitude,
- double radius_meters,
- int count,
- DistanceSort sort,
- const std::string &store_key,
- bool store_distance,
- double unit_conversion,
- std::vector<GeoPoint> *geo_points) {
+rocksdb::Status Geo::Radius(const Slice &user_key, double longitude, double latitude, double radius_meters, int count,
+ DistanceSort sort, const std::string &store_key, bool store_distance,
+ double unit_conversion, std::vector<GeoPoint> *geo_points) {
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);
ZSetMetadata metadata(false);
@@ -139,28 +127,18 @@ rocksdb::Status Geo::Radius(const Slice &user_key,
return rocksdb::Status::OK();
}
-rocksdb::Status Geo::RadiusByMember(const Slice &user_key,
- const Slice &member,
- double radius_meters,
- int count,
- DistanceSort sort,
- const std::string &store_key,
- bool store_distance,
- double unit_conversion,
- std::vector<GeoPoint> *geo_points) {
+rocksdb::Status Geo::RadiusByMember(const Slice &user_key, const Slice &member, double radius_meters, int count,
+ DistanceSort sort, const std::string &store_key, bool store_distance,
+ double unit_conversion, std::vector<GeoPoint> *geo_points) {
GeoPoint geo_point;
auto s = Get(user_key, member, &geo_point);
if (!s.ok()) return s;
- return Radius(user_key, geo_point.longitude, geo_point.latitude, radius_meters,
- count,
- sort,
- store_key, store_distance, unit_conversion, geo_points);
+ return Radius(user_key, geo_point.longitude, geo_point.latitude, radius_meters, count, sort, store_key,
+ store_distance, unit_conversion, geo_points);
}
-rocksdb::Status Geo::Get(const Slice &user_key,
- const Slice &member,
- GeoPoint *geo_point) {
+rocksdb::Status Geo::Get(const Slice &user_key, const Slice &member, GeoPoint *geo_point) {
std::map<std::string, GeoPoint> geo_points;
auto s = MGet(user_key, {member}, &geo_points);
if (!s.ok()) return s;
@@ -173,8 +151,7 @@ rocksdb::Status Geo::Get(const Slice &user_key,
return rocksdb::Status::OK();
}
-rocksdb::Status Geo::MGet(const Slice &user_key,
- const std::vector<Slice> &members,
+rocksdb::Status Geo::MGet(const Slice &user_key, const std::vector<Slice> &members,
std::map<std::string, GeoPoint> *geo_points) {
std::map<std::string, double> member_scores;
auto s = ZSet::MGet(user_key, members, &member_scores);
@@ -197,10 +174,10 @@ rocksdb::Status Geo::MGet(const Slice &user_key,
std::string Geo::EncodeGeoHash(double longitude, double latitude) {
const std::string geoalphabet = "0123456789bcdefghjkmnpqrstuvwxyz";
/* The internal format we use for geocoding is a bit different
- * than the standard, since we use as initial latitude range
- * -85,85, while the normal geohashing algorithm uses -90,90.
- * So we have to decode our position and re-encode using the
- * standard ranges in order to output a valid geohash string. */
+ * than the standard, since we use as initial latitude range
+ * -85,85, while the normal geohashing algorithm uses -90,90.
+ * So we have to decode our position and re-encode using the
+ * standard ranges in order to output a valid geohash string. */
/* Re-encode */
GeoHashRange r[2];
@@ -228,16 +205,12 @@ std::string Geo::EncodeGeoHash(double longitude, double latitude) {
}
int Geo::decodeGeoHash(double bits, double *xy) {
- GeoHashBits hash = {(uint64_t) bits, GEO_STEP_MAX};
+ GeoHashBits hash = {(uint64_t)bits, GEO_STEP_MAX};
return geohashDecodeToLongLatWGS84(hash, xy);
}
/* Search all eight neighbors + self geohash box */
-int Geo::membersOfAllNeighbors(const Slice &user_key,
- GeoHashRadius n,
- double lon,
- double lat,
- double radius,
+int Geo::membersOfAllNeighbors(const Slice &user_key, GeoHashRadius n, double lon, double lat, double radius,
std::vector<GeoPoint> *geo_points) {
GeoHashBits neighbors[9];
unsigned int i, count = 0, last_processed = 0;
@@ -263,8 +236,7 @@ int Geo::membersOfAllNeighbors(const Slice &user_key,
* adjacent neighbors can be the same, leading to duplicated
* elements. Skip every range which is the same as the one
* processed previously. */
- if (last_processed &&
- neighbors[i].bits == neighbors[last_processed].bits &&
+ if (last_processed && neighbors[i].bits == neighbors[last_processed].bits &&
neighbors[i].step == neighbors[last_processed].step) {
continue;
}
@@ -277,12 +249,8 @@ int Geo::membersOfAllNeighbors(const Slice &user_key,
/* Obtain all members between the min/max of this geohash bounding box.
* Populate a GeoArray of GeoPoints by calling getPointsInRange().
* Return the number of points added to the array. */
-int Geo::membersOfGeoHashBox(const Slice &user_key,
- GeoHashBits hash,
- std::vector<GeoPoint> *geo_points,
- double lon,
- double lat,
- double radius) {
+int Geo::membersOfGeoHashBox(const Slice &user_key, GeoHashBits hash, std::vector<GeoPoint> *geo_points, double lon,
+ double lat, double radius) {
GeoHashFix52Bits min, max;
scoresOfGeoHashBox(hash, &min, &max);
@@ -330,12 +298,7 @@ void Geo::scoresOfGeoHashBox(GeoHashBits hash, GeoHashFix52Bits *min, GeoHashFix
* using multiple queries to the sorted set, that we later need to sort
* via qsort. Similarly we need to be able to reject points outside the search
* radius area ASAP in order to allocate and process more points than needed. */
-int Geo::getPointsInRange(const Slice &user_key,
- double min,
- double max,
- double lon,
- double lat,
- double radius,
+int Geo::getPointsInRange(const Slice &user_key, double min, double max, double lon, double lat, double radius,
std::vector<GeoPoint> *geo_points) {
/* include min in range; exclude max in range */
/* That's: min <= val < max */
@@ -360,19 +323,14 @@ int Geo::getPointsInRange(const Slice &user_key,
* only if the point is within the search area.
*
* returns true if the point is included, or false if it is outside. */
-bool Geo::appendIfWithinRadius(std::vector<GeoPoint> *geo_points,
- double lon,
- double lat,
- double radius,
- double score,
+bool Geo::appendIfWithinRadius(std::vector<GeoPoint> *geo_points, double lon, double lat, double radius, double score,
const std::string &member) {
double distance, xy[2];
if (!decodeGeoHash(score, xy)) return false; /* Can't decode. */
/* Note that geohashGetDistanceIfInRadiusWGS84() takes arguments in
* reverse order: longitude first, latitude later. */
- if (!GeoHashHelper::GetDistanceIfInRadiusWGS84(lon, lat, xy[0], xy[1],
- radius, &distance)) {
+ if (!GeoHashHelper::GetDistanceIfInRadiusWGS84(lon, lat, xy[0], xy[1], radius, &distance)) {
return false;
}
@@ -387,12 +345,8 @@ bool Geo::appendIfWithinRadius(std::vector<GeoPoint> *geo_points,
return true;
}
-bool Geo::sortGeoPointASC(const GeoPoint &gp1, const GeoPoint &gp2) {
- return gp1.dist < gp2.dist;
-}
+bool Geo::sortGeoPointASC(const GeoPoint &gp1, const GeoPoint &gp2) { return gp1.dist < gp2.dist; }
-bool Geo::sortGeoPointDESC(const GeoPoint &gp1, const GeoPoint &gp2) {
- return gp1.dist >= gp2.dist;
-}
+bool Geo::sortGeoPointDESC(const GeoPoint &gp1, const GeoPoint &gp2) { return gp1.dist >= gp2.dist; }
} // namespace Redis
diff --git a/src/redis_geo.h b/src/redis_geo.h
index 3e68509..131f9d7 100644
--- a/src/redis_geo.h
+++ b/src/redis_geo.h
@@ -20,15 +20,15 @@
#pragma once
+#include <limits>
#include <map>
#include <string>
#include <vector>
-#include <limits>
+#include "geohash.h"
#include "redis_db.h"
#include "redis_metadata.h"
#include "redis_zset.h"
-#include "geohash.h"
enum DistanceUnit {
kDistanceMeter,
@@ -56,71 +56,34 @@ namespace Redis {
class Geo : public ZSet {
public:
- explicit Geo(Engine::Storage *storage, const std::string &ns) :
- ZSet(storage, ns) {}
+ explicit Geo(Engine::Storage *storage, const std::string &ns) : ZSet(storage, ns) {}
rocksdb::Status Add(const Slice &user_key, std::vector<GeoPoint> *geo_points, int *ret);
rocksdb::Status Dist(const Slice &user_key, const Slice &member_1, const Slice &member_2, double *dist);
- rocksdb::Status Hash(const Slice &user_key,
- const std::vector<Slice> &members,
- std::vector<std::string> *geoHashes);
- rocksdb::Status Pos(const Slice &user_key,
- const std::vector<Slice> &members,
+ rocksdb::Status Hash(const Slice &user_key, const std::vector<Slice> &members, std::vector<std::string> *geoHashes);
+ rocksdb::Status Pos(const Slice &user_key, const std::vector<Slice> &members,
std::map<std::string, GeoPoint> *geo_points);
- rocksdb::Status Radius(const Slice &user_key,
- double longitude,
- double latitude,
- double radius_meters,
- int count,
- DistanceSort sort,
- const std::string &store_key,
- bool store_distance,
- double unit_conversion,
+ rocksdb::Status Radius(const Slice &user_key, double longitude, double latitude, double radius_meters, int count,
+ DistanceSort sort, const std::string &store_key, bool store_distance, double unit_conversion,
std::vector<GeoPoint> *geo_points);
- rocksdb::Status RadiusByMember(const Slice &user_key,
- const Slice &member,
- double radius_meters,
- int count,
- DistanceSort sort,
- const std::string &store_key,
- bool store_distance,
- double unit_conversion,
- std::vector<GeoPoint> *geo_points);
+ rocksdb::Status RadiusByMember(const Slice &user_key, const Slice &member, double radius_meters, int count,
+ DistanceSort sort, const std::string &store_key, bool store_distance,
+ double unit_conversion, std::vector<GeoPoint> *geo_points);
- rocksdb::Status Get(const Slice &user_key,
- const Slice &member,
- GeoPoint *geo_point);
- rocksdb::Status MGet(const Slice &user_key,
- const std::vector<Slice> &members,
+ rocksdb::Status Get(const Slice &user_key, const Slice &member, GeoPoint *geo_point);
+ rocksdb::Status MGet(const Slice &user_key, const std::vector<Slice> &members,
std::map<std::string, GeoPoint> *geo_points);
std::string EncodeGeoHash(double longitude, double latitude);
private:
int decodeGeoHash(double bits, double *xy);
- int membersOfAllNeighbors(const Slice &user_key,
- GeoHashRadius n,
- double lon,
- double lat,
- double radius,
+ int membersOfAllNeighbors(const Slice &user_key, GeoHashRadius n, double lon, double lat, double radius,
std::vector<GeoPoint> *geo_points);
- int membersOfGeoHashBox(const Slice &user_key,
- GeoHashBits hash,
- std::vector<GeoPoint> *geo_points,
- double lon,
- double lat,
- double radius);
+ int membersOfGeoHashBox(const Slice &user_key, GeoHashBits hash, std::vector<GeoPoint> *geo_points, double lon,
+ double lat, double radius);
void scoresOfGeoHashBox(GeoHashBits hash, GeoHashFix52Bits *min, GeoHashFix52Bits *max);
- int getPointsInRange(const Slice &user_key,
- double min,
- double max,
- double lon,
- double lat,
- double radius,
+ int getPointsInRange(const Slice &user_key, double min, double max, double lon, double lat, double radius,
std::vector<GeoPoint> *geo_points);
- bool appendIfWithinRadius(std::vector<GeoPoint> *geo_points,
- double lon,
- double lat,
- double radius,
- double score,
+ bool appendIfWithinRadius(std::vector<GeoPoint> *geo_points, double lon, double lat, double radius, double score,
const std::string &member);
static bool sortGeoPointASC(const GeoPoint &gp1, const GeoPoint &gp2);
diff --git a/src/redis_hash.cc b/src/redis_hash.cc
index 5405f85..86444a0 100644
--- a/src/redis_hash.cc
+++ b/src/redis_hash.cc
@@ -92,8 +92,8 @@ rocksdb::Status Hash::IncrBy(const Slice &user_key, const Slice &field, int64_t
exists = true;
}
}
- if ((increment < 0 && old_value < 0 && increment < (LLONG_MIN-old_value))
- || (increment > 0 && old_value > 0 && increment > (LLONG_MAX-old_value))) {
+ if ((increment < 0 && old_value < 0 && increment < (LLONG_MIN - old_value)) ||
+ (increment > 0 && old_value > 0 && increment > (LLONG_MAX - old_value))) {
return rocksdb::Status::InvalidArgument("increment or decrement would overflow");
}
@@ -161,9 +161,7 @@ rocksdb::Status Hash::IncrByFloat(const Slice &user_key, const Slice &field, dou
return storage_->Write(storage_->DefaultWriteOptions(), &batch);
}
-rocksdb::Status Hash::MGet(const Slice &user_key,
- const std::vector<Slice> &fields,
- std::vector<std::string> *values,
+rocksdb::Status Hash::MGet(const Slice &user_key, const std::vector<Slice> &fields, std::vector<std::string> *values,
std::vector<rocksdb::Status> *statuses) {
values->clear();
statuses->clear();
@@ -278,8 +276,8 @@ rocksdb::Status Hash::MSet(const Slice &user_key, const std::vector<FieldValue>
return storage_->Write(storage_->DefaultWriteOptions(), &batch);
}
-rocksdb::Status Hash::Range(const Slice &user_key, const Slice &start, const Slice &stop,
- int64_t limit, std::vector<FieldValue> *field_values) {
+rocksdb::Status Hash::Range(const Slice &user_key, const Slice &start, const Slice &stop, int64_t limit,
+ std::vector<FieldValue> *field_values) {
field_values->clear();
if (start.compare(stop) >= 0 || limit <= 0) {
return rocksdb::Status::OK();
@@ -334,9 +332,7 @@ rocksdb::Status Hash::GetAll(const Slice &user_key, std::vector<FieldValue> *fie
read_options.fill_cache = false;
auto iter = DBUtil::UniqueIterator(db_, read_options);
- for (iter->Seek(prefix_key);
- iter->Valid() && iter->key().starts_with(prefix_key);
- iter->Next()) {
+ for (iter->Seek(prefix_key); iter->Valid() && iter->key().starts_with(prefix_key); iter->Next()) {
FieldValue fv;
if (type == HashFetchType::kOnlyKey) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
@@ -353,11 +349,8 @@ rocksdb::Status Hash::GetAll(const Slice &user_key, std::vector<FieldValue> *fie
return rocksdb::Status::OK();
}
-rocksdb::Status Hash::Scan(const Slice &user_key,
- const std::string &cursor,
- uint64_t limit,
- const std::string &field_prefix,
- std::vector<std::string> *fields,
+rocksdb::Status Hash::Scan(const Slice &user_key, const std::string &cursor, uint64_t limit,
+ const std::string &field_prefix, std::vector<std::string> *fields,
std::vector<std::string> *values) {
return SubKeyScanner::Scan(kRedisHash, user_key, cursor, limit, field_prefix, fields, values);
}
diff --git a/src/redis_hash.h b/src/redis_hash.h
index c80ad01..7eaa81d 100644
--- a/src/redis_hash.h
+++ b/src/redis_hash.h
@@ -34,11 +34,7 @@ typedef struct FieldValue {
std::string value;
} FieldValue;
-enum class HashFetchType {
- kAll = 0,
- kOnlyKey = 1,
- kOnlyValue = 2
-};
+enum class HashFetchType { kAll = 0, kOnlyKey = 1, kOnlyValue = 2 };
namespace Redis {
class Hash : public SubKeyScanner {
@@ -52,20 +48,14 @@ class Hash : public SubKeyScanner {
rocksdb::Status IncrBy(const Slice &user_key, const Slice &field, int64_t increment, int64_t *ret);
rocksdb::Status IncrByFloat(const Slice &user_key, const Slice &field, double increment, double *ret);
rocksdb::Status MSet(const Slice &user_key, const std::vector<FieldValue> &field_values, bool nx, int *ret);
- rocksdb::Status Range(const Slice &user_key, const Slice &start, const Slice& stop,
- int64_t limit, std::vector<FieldValue> *field_values);
- rocksdb::Status MGet(const Slice &user_key,
- const std::vector<Slice> &fields,
- std::vector<std::string> *values,
+ rocksdb::Status Range(const Slice &user_key, const Slice &start, const Slice &stop, int64_t limit,
+ std::vector<FieldValue> *field_values);
+ rocksdb::Status MGet(const Slice &user_key, const std::vector<Slice> &fields, std::vector<std::string> *values,
std::vector<rocksdb::Status> *statuses);
- rocksdb::Status GetAll(const Slice &user_key,
- std::vector<FieldValue> *field_values,
+ rocksdb::Status GetAll(const Slice &user_key, std::vector<FieldValue> *field_values,
HashFetchType type = HashFetchType::kAll);
- rocksdb::Status Scan(const Slice &user_key,
- const std::string &cursor,
- uint64_t limit,
- const std::string &field_prefix,
- std::vector<std::string> *fields,
+ rocksdb::Status Scan(const Slice &user_key, const std::string &cursor, uint64_t limit,
+ const std::string &field_prefix, std::vector<std::string> *fields,
std::vector<std::string> *values = nullptr);
private:
diff --git a/src/redis_list.cc b/src/redis_list.cc
index a354f37..b0c51e6 100644
--- a/src/redis_list.cc
+++ b/src/redis_list.cc
@@ -51,10 +51,7 @@ rocksdb::Status List::PushX(const Slice &user_key, const std::vector<Slice> &ele
return push(user_key, elems, false, left, ret);
}
-rocksdb::Status List::push(const Slice &user_key,
- std::vector<Slice> elems,
- bool create_if_missing,
- bool left,
+rocksdb::Status List::push(const Slice &user_key, std::vector<Slice> elems, bool create_if_missing, bool left,
int *ret) {
*ret = 0;
std::string ns_key;
@@ -199,8 +196,7 @@ rocksdb::Status List::Rem(const Slice &user_key, int count, const Slice &elem, i
read_options.fill_cache = false;
auto iter = DBUtil::UniqueIterator(db_, read_options);
- for (iter->Seek(start_key);
- iter->Valid() && iter->key().starts_with(prefix);
+ for (iter->Seek(start_key); iter->Valid() && iter->key().starts_with(prefix);
!reversed ? iter->Next() : iter->Prev()) {
if (iter->value() == elem) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
@@ -231,8 +227,7 @@ rocksdb::Status List::Rem(const Slice &user_key, int count, const Slice &elem, i
PutFixed64(&buf, reversed ? max_to_delete_index : min_to_delete_index);
InternalKey(ns_key, buf, metadata.version, storage_->IsSlotIdEncoded()).Encode(&start_key);
size_t count = 0;
- for (iter->Seek(start_key);
- iter->Valid() && iter->key().starts_with(prefix);
+ for (iter->Seek(start_key); iter->Valid() && iter->key().starts_with(prefix);
!reversed ? iter->Next() : iter->Prev()) {
if (iter->value() != elem || count >= to_delete_indexes.size()) {
buf.clear();
@@ -290,9 +285,7 @@ rocksdb::Status List::Insert(const Slice &user_key, const Slice &pivot, const Sl
read_options.fill_cache = false;
auto iter = DBUtil::UniqueIterator(db_, read_options);
- for (iter->Seek(start_key);
- iter->Valid() && iter->key().starts_with(prefix);
- iter->Next()) {
+ for (iter->Seek(start_key); iter->Valid() && iter->key().starts_with(prefix); iter->Next()) {
if (iter->value() == pivot) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
Slice sub_key = ikey.GetSubKey();
@@ -307,10 +300,7 @@ rocksdb::Status List::Insert(const Slice &user_key, const Slice &pivot, const Sl
rocksdb::WriteBatch batch;
WriteBatchLogData log_data(kRedisList,
- {std::to_string(kRedisCmdLInsert),
- before ? "1" : "0",
- pivot.ToString(),
- elem.ToString()});
+ {std::to_string(kRedisCmdLInsert), before ? "1" : "0", pivot.ToString(), elem.ToString()});
batch.PutLogData(log_data.Encode());
std::string to_update_key;
@@ -323,9 +313,7 @@ rocksdb::Status List::Insert(const Slice &user_key, const Slice &pivot, const Sl
new_elem_index = reversed ? --pivot_index : ++pivot_index;
!reversed ? iter->Next() : iter->Prev();
}
- for (;
- iter->Valid() && iter->key().starts_with(prefix);
- !reversed ? iter->Next() : iter->Prev()) {
+ for (; iter->Valid() && iter->key().starts_with(prefix); !reversed ? iter->Next() : iter->Prev()) {
buf.clear();
PutFixed64(&buf, reversed ? --pivot_index : ++pivot_index);
InternalKey(ns_key, buf, metadata.version, storage_->IsSlotIdEncoded()).Encode(&to_update_key);
@@ -406,9 +394,7 @@ rocksdb::Status List::Range(const Slice &user_key, int start, int stop, std::vec
read_options.fill_cache = false;
auto iter = DBUtil::UniqueIterator(db_, read_options);
- for (iter->Seek(start_key);
- iter->Valid() && iter->key().starts_with(prefix);
- iter->Next()) {
+ for (iter->Seek(start_key); iter->Valid() && iter->key().starts_with(prefix); iter->Next()) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
Slice sub_key = ikey.GetSubKey();
uint64_t index;
@@ -443,8 +429,7 @@ rocksdb::Status List::Set(const Slice &user_key, int index, Slice elem) {
if (value == elem) return rocksdb::Status::OK();
rocksdb::WriteBatch batch;
- WriteBatchLogData
- log_data(kRedisList, {std::to_string(kRedisCmdLSet), std::to_string(index)});
+ WriteBatchLogData log_data(kRedisList, {std::to_string(kRedisCmdLSet), std::to_string(index)});
batch.PutLogData(log_data.Encode());
batch.Put(sub_key, elem);
return storage_->Write(storage_->DefaultWriteOptions(), &batch);
@@ -468,8 +453,8 @@ rocksdb::Status List::RPopLPush(const Slice &src, const Slice &dst, std::string
return s;
}
-rocksdb::Status List::LMove(const rocksdb::Slice &src, const rocksdb::Slice &dst,
- bool src_left, bool dst_left, std::string *elem) {
+rocksdb::Status List::LMove(const rocksdb::Slice &src, const rocksdb::Slice &dst, bool src_left, bool dst_left,
+ std::string *elem) {
if (src == dst) {
return lmoveOnSingleList(src, src_left, dst_left, elem);
}
@@ -537,8 +522,8 @@ rocksdb::Status List::lmoveOnSingleList(const rocksdb::Slice &src, bool src_left
return storage_->Write(storage_->DefaultWriteOptions(), &batch);
}
-rocksdb::Status List::lmoveOnTwoLists(const rocksdb::Slice &src, const rocksdb::Slice &dst,
- bool src_left, bool dst_left, std::string *elem) {
+rocksdb::Status List::lmoveOnTwoLists(const rocksdb::Slice &src, const rocksdb::Slice &dst, bool src_left,
+ bool dst_left, std::string *elem) {
std::string src_ns_key;
AppendNamespacePrefix(src, &src_ns_key);
std::string dst_ns_key;
@@ -622,9 +607,8 @@ rocksdb::Status List::Trim(const Slice &user_key, int start, int stop) {
if (start < 0) start = 0;
rocksdb::WriteBatch batch;
- WriteBatchLogData log_data(kRedisList,
- std::vector<std::string>{std::to_string(kRedisCmdLTrim), std::to_string(start),
- std::to_string(stop)});
+ WriteBatchLogData log_data(kRedisList, std::vector<std::string>{std::to_string(kRedisCmdLTrim), std::to_string(start),
+ std::to_string(stop)});
batch.PutLogData(log_data.Encode());
uint64_t left_index = metadata.head + start;
uint64_t right_index = metadata.head + stop + 1;
diff --git a/src/redis_list.h b/src/redis_list.h
index a180212..f8969c8 100644
--- a/src/redis_list.h
+++ b/src/redis_list.h
@@ -21,12 +21,13 @@
#pragma once
#include <stdint.h>
-#include <vector>
+
#include <string>
+#include <vector>
+#include "encoding.h"
#include "redis_db.h"
#include "redis_metadata.h"
-#include "encoding.h"
namespace Redis {
class List : public Database {
diff --git a/src/redis_metadata.cc b/src/redis_metadata.cc
index e8af1fd..f16c2df 100644
--- a/src/redis_metadata.cc
+++ b/src/redis_metadata.cc
@@ -24,21 +24,20 @@
#include <sys/time.h>
#include <atomic>
-#include <ctime>
#include <cstdlib>
+#include <ctime>
#include <vector>
#include "redis_slot.h"
-
// 52 bit for microseconds and 11 bit for counter
const int VersionCounterBits = 11;
static std::atomic<uint64_t> version_counter_ = {0};
-const char* kErrMsgWrongType = "WRONGTYPE Operation against a key holding the wrong kind of value";
-const char* kErrMsgKeyExpired = "the key was expired";
-const char* kErrMetadataTooShort = "metadata is too short";
+const char *kErrMsgWrongType = "WRONGTYPE Operation against a key holding the wrong kind of value";
+const char *kErrMsgKeyExpired = "the key was expired";
+const char *kErrMetadataTooShort = "metadata is too short";
InternalKey::InternalKey(Slice input, bool slot_id_encoded) {
slot_id_encoded_ = slot_id_encoded;
@@ -76,29 +75,21 @@ InternalKey::InternalKey(Slice ns_key, Slice sub_key, uint64_t version, bool slo
}
InternalKey::~InternalKey() {
- if (buf_ != nullptr && buf_ != prealloc_) delete []buf_;
+ if (buf_ != nullptr && buf_ != prealloc_) delete[] buf_;
}
-Slice InternalKey::GetNamespace() const {
- return namespace_;
-}
+Slice InternalKey::GetNamespace() const { return namespace_; }
-Slice InternalKey::GetKey() const {
- return key_;
-}
+Slice InternalKey::GetKey() const { return key_; }
-Slice InternalKey::GetSubKey() const {
- return sub_key_;
-}
+Slice InternalKey::GetSubKey() const { return sub_key_; }
-uint64_t InternalKey::GetVersion() const {
- return version_;
-}
+uint64_t InternalKey::GetVersion() const { return version_; }
void InternalKey::Encode(std::string *out) {
out->clear();
size_t pos = 0;
- size_t total = 1+namespace_.size()+4+key_.size()+8+sub_key_.size();
+ size_t total = 1 + namespace_.size() + 4 + key_.size() + 8 + sub_key_.size();
if (slot_id_encoded_) {
total += 2;
}
@@ -107,21 +98,21 @@ void InternalKey::Encode(std::string *out) {
} else {
buf_ = new char[total];
}
- EncodeFixed8(buf_+pos, static_cast<uint8_t>(namespace_.size()));
+ EncodeFixed8(buf_ + pos, static_cast<uint8_t>(namespace_.size()));
pos += 1;
- memcpy(buf_+pos, namespace_.data(), namespace_.size());
+ memcpy(buf_ + pos, namespace_.data(), namespace_.size());
pos += namespace_.size();
if (slot_id_encoded_) {
- EncodeFixed16(buf_+pos, slotid_);
+ EncodeFixed16(buf_ + pos, slotid_);
pos += 2;
}
- EncodeFixed32(buf_+pos, static_cast<uint32_t>(key_.size()));
+ EncodeFixed32(buf_ + pos, static_cast<uint32_t>(key_.size()));
pos += 4;
- memcpy(buf_+pos, key_.data(), key_.size());
+ memcpy(buf_ + pos, key_.data(), key_.size());
pos += key_.size();
- EncodeFixed64(buf_+pos, version_);
+ EncodeFixed64(buf_ + pos, version_);
pos += 8;
- memcpy(buf_+pos, sub_key_.data(), sub_key_.size());
+ memcpy(buf_ + pos, sub_key_.data(), sub_key_.size());
pos += sub_key_.size();
out->assign(buf_, pos);
}
@@ -146,7 +137,7 @@ void ExtractNamespaceKey(Slice ns_key, std::string *ns, std::string *key, bool s
*key = ns_key.ToString();
}
-void ComposeNamespaceKey(const Slice& ns, const Slice& key, std::string *ns_key, bool slot_id_encoded) {
+void ComposeNamespaceKey(const Slice &ns, const Slice &key, std::string *ns_key, bool slot_id_encoded) {
ns_key->clear();
PutFixed8(ns_key, static_cast<uint8_t>(ns.size()));
@@ -160,7 +151,7 @@ void ComposeNamespaceKey(const Slice& ns, const Slice& key, std::string *ns_key,
ns_key->append(key.data(), key.size());
}
-void ComposeSlotKeyPrefix(const Slice& ns, int slotid, std::string *output) {
+void ComposeSlotKeyPrefix(const Slice &ns, int slotid, std::string *output) {
output->clear();
PutFixed8(output, static_cast<uint8_t>(ns.size()));
@@ -195,7 +186,7 @@ rocksdb::Status Metadata::Decode(const std::string &bytes) {
void Metadata::Encode(std::string *dst) {
PutFixed8(dst, flags);
- PutFixed32(dst, (uint32_t) expire);
+ PutFixed32(dst, (uint32_t)expire);
if (Type() != kRedisString) {
PutFixed64(dst, version);
PutFixed32(dst, size);
@@ -214,10 +205,10 @@ void Metadata::InitVersionCounter() {
uint64_t Metadata::generateVersion() {
struct timeval now;
gettimeofday(&now, nullptr);
- uint64_t version = static_cast<uint64_t >(now.tv_sec)*1000000;
+ uint64_t version = static_cast<uint64_t>(now.tv_sec) * 1000000;
version += static_cast<uint64_t>(now.tv_usec);
uint64_t counter = version_counter_.fetch_add(1);
- return (version << VersionCounterBits) + (counter%(1 << VersionCounterBits));
+ return (version << VersionCounterBits) + (counter % (1 << VersionCounterBits));
}
bool Metadata::operator==(const Metadata &that) const {
@@ -230,9 +221,7 @@ bool Metadata::operator==(const Metadata &that) const {
return true;
}
-RedisType Metadata::Type() const {
- return static_cast<RedisType>(flags & (uint8_t)0x0f);
-}
+RedisType Metadata::Type() const { return static_cast<RedisType>(flags & (uint8_t)0x0f); }
int32_t Metadata::TTL() const {
int64_t now;
@@ -243,12 +232,14 @@ int32_t Metadata::TTL() const {
if (expire < now) {
return -2;
}
- return int32_t (expire - now);
+ return int32_t(expire - now);
}
timeval Metadata::Time() const {
auto t = version >> VersionCounterBits;
- struct timeval created_at{static_cast<uint32_t>(t / 1000000), static_cast<int32_t>(t % 1000000)};
+ struct timeval created_at {
+ static_cast<uint32_t>(t / 1000000), static_cast<int32_t>(t % 1000000)
+ };
return created_at;
}
@@ -267,7 +258,7 @@ bool Metadata::Expired() const {
}
ListMetadata::ListMetadata(bool generate_version) : Metadata(kRedisList, generate_version) {
- head = UINT64_MAX/2;
+ head = UINT64_MAX / 2;
tail = head;
}
diff --git a/src/redis_metadata.h b/src/redis_metadata.h
index 1904f7f..3c72395 100644
--- a/src/redis_metadata.h
+++ b/src/redis_metadata.h
@@ -56,13 +56,11 @@ enum RedisCommand {
kRedisCmdLMove,
};
-const std::vector<std::string> RedisTypeNames = {
- "none", "string", "hash",
- "list", "set", "zset", "bitmap", "sortedint", "stream"
-};
+const std::vector<std::string> RedisTypeNames = {"none", "string", "hash", "list", "set",
+ "zset", "bitmap", "sortedint", "stream"};
-extern const char* kErrMsgWrongType;
-extern const char* kErrMsgKeyExpired;
+extern const char *kErrMsgWrongType;
+extern const char *kErrMsgKeyExpired;
using rocksdb::Slice;
@@ -75,7 +73,7 @@ struct KeyNumStats {
void ExtractNamespaceKey(Slice ns_key, std::string *ns, std::string *key, bool slot_id_encoded);
void ComposeNamespaceKey(const Slice &ns, const Slice &key, std::string *ns_key, bool slot_id_encoded);
-void ComposeSlotKeyPrefix(const Slice& ns, int slotid, std::string *output);
+void ComposeSlotKeyPrefix(const Slice &ns, int slotid, std::string *output);
class InternalKey {
public:
@@ -126,22 +124,22 @@ class Metadata {
class HashMetadata : public Metadata {
public:
- explicit HashMetadata(bool generate_version = true) : Metadata(kRedisHash, generate_version){}
+ explicit HashMetadata(bool generate_version = true) : Metadata(kRedisHash, generate_version) {}
};
class SetMetadata : public Metadata {
public:
- explicit SetMetadata(bool generate_version = true): Metadata(kRedisSet, generate_version) {}
+ explicit SetMetadata(bool generate_version = true) : Metadata(kRedisSet, generate_version) {}
};
class ZSetMetadata : public Metadata {
public:
- explicit ZSetMetadata(bool generate_version = true): Metadata(kRedisZSet, generate_version){}
+ explicit ZSetMetadata(bool generate_version = true) : Metadata(kRedisZSet, generate_version) {}
};
class BitmapMetadata : public Metadata {
public:
- explicit BitmapMetadata(bool generate_version = true): Metadata(kRedisBitmap, generate_version){}
+ explicit BitmapMetadata(bool generate_version = true) : Metadata(kRedisBitmap, generate_version) {}
};
class SortedintMetadata : public Metadata {
@@ -154,6 +152,7 @@ class ListMetadata : public Metadata {
uint64_t head;
uint64_t tail;
explicit ListMetadata(bool generate_version = true);
+
public:
void Encode(std::string *dst) override;
rocksdb::Status Decode(const std::string &bytes) override;
@@ -169,6 +168,7 @@ class StreamMetadata : public Metadata {
uint64_t entries_added = 0;
explicit StreamMetadata(bool generate_version = true) : Metadata(kRedisStream, generate_version) {}
+
public:
void Encode(std::string *dst) override;
rocksdb::Status Decode(const std::string &bytes) override;
diff --git a/src/redis_pubsub.h b/src/redis_pubsub.h
index 2077eab..ee55c8b 100644
--- a/src/redis_pubsub.h
+++ b/src/redis_pubsub.h
@@ -29,9 +29,7 @@ namespace Redis {
class PubSub : public Database {
public:
- explicit PubSub(Engine::Storage *storage) :
- Database(storage),
- pubsub_cf_handle_(storage->GetCFHandle("pubsub")) {}
+ explicit PubSub(Engine::Storage *storage) : Database(storage), pubsub_cf_handle_(storage->GetCFHandle("pubsub")) {}
rocksdb::Status Publish(const Slice &channel, const Slice &value);
private:
diff --git a/src/redis_reply.cc b/src/redis_reply.cc
index a030319..cd6a272 100644
--- a/src/redis_reply.cc
+++ b/src/redis_reply.cc
@@ -24,9 +24,7 @@
namespace Redis {
-void Reply(evbuffer *output, const std::string &data) {
- evbuffer_add(output, data.c_str(), data.length());
-}
+void Reply(evbuffer *output, const std::string &data) { evbuffer_add(output, data.c_str(), data.length()); }
std::string SimpleString(const std::string &data) { return "+" + data + CRLF; }
@@ -34,32 +32,25 @@ std::string Error(const std::string &err) { return "-" + err + CRLF; }
std::string Integer(int64_t data) { return ":" + std::to_string(data) + CRLF; }
-std::string BulkString(const std::string &data) {
- return "$" + std::to_string(data.length()) + CRLF + data + CRLF;
-}
+std::string BulkString(const std::string &data) { return "$" + std::to_string(data.length()) + CRLF + data + CRLF; }
-std::string NilString() {
- return "$-1\r\n";
-}
+std::string NilString() { return "$-1\r\n"; }
-std::string MultiLen(int64_t len) {
- return "*"+std::to_string(len)+"\r\n";
-}
+std::string MultiLen(int64_t len) { return "*" + std::to_string(len) + "\r\n"; }
-std::string MultiBulkString(const std::vector<std::string>& values, bool output_nil_for_empty_string) {
+std::string MultiBulkString(const std::vector<std::string> &values, bool output_nil_for_empty_string) {
std::string result = "*" + std::to_string(values.size()) + CRLF;
for (size_t i = 0; i < values.size(); i++) {
if (values[i].empty() && output_nil_for_empty_string) {
result += NilString();
- } else {
+ } else {
result += BulkString(values[i]);
}
}
return result;
}
-
-std::string MultiBulkString(const std::vector<std::string>& values, const std::vector<rocksdb::Status> &statuses) {
+std::string MultiBulkString(const std::vector<std::string> &values, const std::vector<rocksdb::Status> &statuses) {
std::string result = "*" + std::to_string(values.size()) + CRLF;
for (size_t i = 0; i < values.size(); i++) {
if (i < statuses.size() && !statuses[i].ok()) {
@@ -71,13 +62,12 @@ std::string MultiBulkString(const std::vector<std::string>& values, const std::v
return result;
}
-std::string Array(const std::vector<std::string>& list) {
- size_t n = std::accumulate(
- list.begin(), list.end(), 0, [] (size_t n, const std::string &s) { return n + s.size(); });
+std::string Array(const std::vector<std::string> &list) {
+ size_t n = std::accumulate(list.begin(), list.end(), 0, [](size_t n, const std::string &s) { return n + s.size(); });
std::string result = "*" + std::to_string(list.size()) + CRLF;
std::string::size_type final_size = result.size() + n;
result.reserve(final_size);
- for (const auto& i : list) result += i;
+ for (const auto &i : list) result += i;
return result;
}
diff --git a/src/redis_reply.h b/src/redis_reply.h
index 8305c8b..ebf4fe4 100644
--- a/src/redis_reply.h
+++ b/src/redis_reply.h
@@ -36,8 +36,8 @@ std::string Integer(int64_t data);
std::string BulkString(const std::string &data);
std::string NilString();
std::string MultiLen(int64_t len);
-std::string Array(const std::vector<std::string>& list);
-std::string MultiBulkString(const std::vector<std::string>& values, bool output_nil_for_empty_string = true);
-std::string MultiBulkString(const std::vector<std::string>& values, const std::vector<rocksdb::Status> &statuses);
+std::string Array(const std::vector<std::string> &list);
+std::string MultiBulkString(const std::vector<std::string> &values, bool output_nil_for_empty_string = true);
+std::string MultiBulkString(const std::vector<std::string> &values, const std::vector<rocksdb::Status> &statuses);
std::string Command2RESP(const std::vector<std::string> &cmd_args);
} // namespace Redis
diff --git a/src/redis_request.cc b/src/redis_request.cc
index 3fd2a69..5522657 100644
--- a/src/redis_request.cc
+++ b/src/redis_request.cc
@@ -49,7 +49,7 @@ Status Request::Tokenize(evbuffer *input) {
// We don't use the `EVBUFFER_EOL_CRLF_STRICT` here since only LF is allowed in INLINE protocol.
// So we need to search LF EOL and figure out current line has CR or not.
UniqueEvbufReadln line(input, EVBUFFER_EOL_LF);
- if (line && line.length > 0 && line[line.length-1] == '\r') {
+ if (line && line.length > 0 && line[line.length - 1] == '\r') {
// remove `\r` if exists
--line.length;
isOnlyLF = false;
@@ -77,8 +77,8 @@ Status Request::Tokenize(evbuffer *input) {
return Status(Status::NotOK, "Protocol error: invalid multibulk length");
}
if (multi_bulk_len_ <= 0) {
- multi_bulk_len_ = 0;
- continue;
+ multi_bulk_len_ = 0;
+ continue;
}
state_ = BulkLen;
} else {
diff --git a/src/redis_set.cc b/src/redis_set.cc
index b789ffa..3f0d7d0 100644
--- a/src/redis_set.cc
+++ b/src/redis_set.cc
@@ -20,8 +20,8 @@
#include "redis_set.h"
-#include <map>
#include <iostream>
+#include <map>
#include <memory>
#include "db_util.h"
@@ -155,9 +155,7 @@ rocksdb::Status Set::Members(const Slice &user_key, std::vector<std::string> *me
read_options.fill_cache = false;
auto iter = DBUtil::UniqueIterator(db_, read_options);
- for (iter->Seek(prefix);
- iter->Valid() && iter->key().starts_with(prefix);
- iter->Next()) {
+ for (iter->Seek(prefix); iter->Valid() && iter->key().starts_with(prefix); iter->Next()) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
members->emplace_back(ikey.GetSubKey().ToString());
}
@@ -230,9 +228,7 @@ rocksdb::Status Set::Take(const Slice &user_key, std::vector<std::string> *membe
read_options.fill_cache = false;
auto iter = DBUtil::UniqueIterator(db_, read_options);
- for (iter->Seek(prefix);
- iter->Valid() && iter->key().starts_with(prefix);
- iter->Next()) {
+ for (iter->Seek(prefix); iter->Valid() && iter->key().starts_with(prefix); iter->Next()) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
members->emplace_back(ikey.GetSubKey().ToString());
if (pop) batch.Delete(iter->key());
@@ -263,11 +259,8 @@ rocksdb::Status Set::Move(const Slice &src, const Slice &dst, const Slice &membe
return Add(dst, members, ret);
}
-rocksdb::Status Set::Scan(const Slice &user_key,
- const std::string &cursor,
- uint64_t limit,
- const std::string &member_prefix,
- std::vector<std::string> *members) {
+rocksdb::Status Set::Scan(const Slice &user_key, const std::string &cursor, uint64_t limit,
+ const std::string &member_prefix, std::vector<std::string> *members) {
return SubKeyScanner::Scan(kRedisSet, user_key, cursor, limit, member_prefix, members);
}
diff --git a/src/redis_set.h b/src/redis_set.h
index 63a01bf..24a86ed 100644
--- a/src/redis_set.h
+++ b/src/redis_set.h
@@ -30,8 +30,7 @@ namespace Redis {
class Set : public SubKeyScanner {
public:
- explicit Set(Engine::Storage *storage, const std::string &ns)
- : SubKeyScanner(storage, ns) {}
+ explicit Set(Engine::Storage *storage, const std::string &ns) : SubKeyScanner(storage, ns) {}
rocksdb::Status Card(const Slice &user_key, int *ret);
rocksdb::Status IsMember(const Slice &user_key, const Slice &member, int *ret);
@@ -48,11 +47,8 @@ class Set : public SubKeyScanner {
rocksdb::Status DiffStore(const Slice &dst, const std::vector<Slice> &keys, int *ret);
rocksdb::Status UnionStore(const Slice &dst, const std::vector<Slice> &keys, int *ret);
rocksdb::Status InterStore(const Slice &dst, const std::vector<Slice> &keys, int *ret);
- rocksdb::Status Scan(const Slice &user_key,
- const std::string &cursor,
- uint64_t limit,
- const std::string &member_prefix,
- std::vector<std::string> *members);
+ rocksdb::Status Scan(const Slice &user_key, const std::string &cursor, uint64_t limit,
+ const std::string &member_prefix, std::vector<std::string> *members);
private:
rocksdb::Status GetMetadata(const Slice &ns_key, SetMetadata *metadata);
diff --git a/src/redis_slot.cc b/src/redis_slot.cc
index 728340f..26f520f 100644
--- a/src/redis_slot.cc
+++ b/src/redis_slot.cc
@@ -19,54 +19,41 @@
*/
#include "redis_slot.h"
+
#include <stdlib.h>
+
+#include <algorithm>
#include <cstdlib>
#include <string>
-#include <algorithm>
#include "util.h"
-static const uint16_t crc16tab[256]= {
- 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
- 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
- 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
- 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
- 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
- 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
- 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
- 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
- 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
- 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
- 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
- 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
- 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
- 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
- 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
- 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
- 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
- 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
- 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
- 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
- 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
- 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
- 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
- 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
- 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
- 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
- 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
- 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
- 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
- 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
- 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
- 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
-};
+static const uint16_t crc16tab[256] = {
+ 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad,
+ 0xe1ce, 0xf1ef, 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b, 0xa35a,
+ 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, 0xa56a, 0xb54b,
+ 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
+ 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861,
+ 0x2802, 0x3823, 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, 0x5af5, 0x4ad4, 0x7ab7, 0x6a96,
+ 0x1a71, 0x0a50, 0x3a33, 0x2a12, 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, 0x6ca6, 0x7c87,
+ 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
+ 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a,
+ 0x9f59, 0x8f78, 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, 0x1080, 0x00a1, 0x30c2, 0x20e3,
+ 0x5004, 0x4025, 0x7046, 0x6067, 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, 0x02b1, 0x1290,
+ 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
+ 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e,
+ 0xc71d, 0xd73c, 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, 0xd94c, 0xc96d, 0xf90e, 0xe92f,
+ 0x99c8, 0x89e9, 0xb98a, 0xa9ab, 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, 0xcb7d, 0xdb5c,
+ 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
+ 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83,
+ 0x1ce0, 0x0cc1, 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, 0x6e17, 0x7e36, 0x4e55, 0x5e74,
+ 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0};
uint16_t crc16(const char *buf, int len) {
- int i;
- uint16_t crc = 0;
- for (i = 0; i < len; i++)
- crc = (crc << 8) ^ crc16tab[((crc >> 8) ^ *buf++)&0x00FF];
- return crc;
+ int i;
+ uint16_t crc = 0;
+ for (i = 0; i < len; i++) crc = (crc << 8) ^ crc16tab[((crc >> 8) ^ *buf++) & 0x00FF];
+ return crc;
}
uint16_t GetSlotNumFromKey(const std::string &key) {
@@ -90,6 +77,3 @@ std::string GetTagFromKey(const std::string &key) {
return key.substr(left_pos + 1, right_pos - left_pos - 1);
}
-
-
-
diff --git a/src/redis_slot.h b/src/redis_slot.h
index 500d516..51fba30 100644
--- a/src/redis_slot.h
+++ b/src/redis_slot.h
@@ -23,7 +23,7 @@
// crc16
#define HASH_SLOTS_MASK 0x3fff
-#define HASH_SLOTS_SIZE (HASH_SLOTS_MASK + 1) // 16384
+#define HASH_SLOTS_SIZE (HASH_SLOTS_MASK + 1) // 16384
#define HASH_SLOTS_MAX_ITERATIONS 50
uint16_t crc16(const char *buf, int len);
diff --git a/src/redis_sortedint.cc b/src/redis_sortedint.cc
index f6176c3..e9dac44 100644
--- a/src/redis_sortedint.cc
+++ b/src/redis_sortedint.cc
@@ -20,9 +20,9 @@
#include "redis_sortedint.h"
-#include <map>
#include <iostream>
#include <limits>
+#include <map>
#include "db_util.h"
#include "parse_util.h"
@@ -111,12 +111,8 @@ rocksdb::Status Sortedint::Card(const Slice &user_key, int *ret) {
return rocksdb::Status::OK();
}
-rocksdb::Status Sortedint::Range(const Slice &user_key,
- uint64_t cursor_id,
- uint64_t offset,
- uint64_t limit,
- bool reversed,
- std::vector<uint64_t> *ids) {
+rocksdb::Status Sortedint::Range(const Slice &user_key, uint64_t cursor_id, uint64_t offset, uint64_t limit,
+ bool reversed, std::vector<uint64_t> *ids) {
ids->clear();
std::string ns_key;
@@ -148,21 +144,18 @@ rocksdb::Status Sortedint::Range(const Slice &user_key,
uint64_t id, pos = 0;
auto iter = DBUtil::UniqueIterator(db_, read_options);
for (!reversed ? iter->Seek(start_key) : iter->SeekForPrev(start_key);
- iter->Valid() && iter->key().starts_with(prefix);
- !reversed ? iter->Next() : iter->Prev()) {
+ iter->Valid() && iter->key().starts_with(prefix); !reversed ? iter->Next() : iter->Prev()) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
Slice sub_key = ikey.GetSubKey();
GetFixed64(&sub_key, &id);
- if ( id == cursor_id || pos++ < offset ) continue;
+ if (id == cursor_id || pos++ < offset) continue;
ids->emplace_back(id);
if (limit > 0 && ids->size() >= limit) break;
}
return rocksdb::Status::OK();
}
-rocksdb::Status Sortedint::RangeByValue(const Slice &user_key,
- SortedintRangeSpec spec,
- std::vector<uint64_t> *ids,
+rocksdb::Status Sortedint::RangeByValue(const Slice &user_key, SortedintRangeSpec spec, std::vector<uint64_t> *ids,
int *size) {
if (size) *size = 0;
if (ids) ids->clear();
@@ -198,9 +191,7 @@ rocksdb::Status Sortedint::RangeByValue(const Slice &user_key,
}
uint64_t id;
- for (;
- iter->Valid() && iter->key().starts_with(prefix_key);
- !spec.reversed ? iter->Next() : iter->Prev()) {
+ for (; iter->Valid() && iter->key().starts_with(prefix_key); !spec.reversed ? iter->Next() : iter->Prev()) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
Slice sub_key = ikey.GetSubKey();
GetFixed64(&sub_key, &id);
diff --git a/src/redis_sortedint.h b/src/redis_sortedint.h
index 736cd31..8f9d2ad 100644
--- a/src/redis_sortedint.h
+++ b/src/redis_sortedint.h
@@ -20,9 +20,9 @@
#pragma once
+#include <limits>
#include <string>
#include <vector>
-#include <limits>
#include "redis_db.h"
#include "redis_metadata.h"
@@ -51,16 +51,9 @@ class Sortedint : public Database {
rocksdb::Status MExist(const Slice &user_key, std::vector<uint64_t> ids, std::vector<int> *exists);
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);
- rocksdb::Status Range(const Slice &user_key,
- uint64_t cursor_id,
- uint64_t page,
- uint64_t limit,
- bool reversed,
+ rocksdb::Status Range(const Slice &user_key, uint64_t cursor_id, uint64_t page, uint64_t limit, bool reversed,
std::vector<uint64_t> *ids);
- rocksdb::Status RangeByValue(const Slice &user_key,
- SortedintRangeSpec spec,
- std::vector<uint64_t> *ids,
- int *size);
+ rocksdb::Status RangeByValue(const Slice &user_key, SortedintRangeSpec spec, std::vector<uint64_t> *ids, int *size);
static Status ParseRangeSpec(const std::string &min, const std::string &max, SortedintRangeSpec *spec);
private:
diff --git a/src/redis_stream.cc b/src/redis_stream.cc
index 5299bf0..b457ad4 100644
--- a/src/redis_stream.cc
+++ b/src/redis_stream.cc
@@ -28,7 +28,6 @@
#include "db_util.h"
-
namespace Redis {
rocksdb::Status Stream::GetMetadata(const Slice &stream_name, StreamMetadata *metadata) {
@@ -74,7 +73,7 @@ std::string Stream::internalKeyFromEntryID(const std::string &ns_key, const Stre
return entry_key;
}
-rocksdb::Status Stream::Add(const Slice &stream_name, const StreamAddOptions& options,
+rocksdb::Status Stream::Add(const Slice &stream_name, const StreamAddOptions &options,
const std::vector<std::string> &args, StreamEntryID *id) {
for (auto const &v : args) {
if (v.size() > INT32_MAX) {
@@ -166,24 +165,24 @@ rocksdb::Status Stream::getNextEntryID(const StreamMetadata &metadata, const Str
if (metadata.last_generated_id.ms == UINT64_MAX && metadata.last_generated_id.seq == UINT64_MAX) {
return rocksdb::Status::InvalidArgument(
- "The stream has exhausted the last possible ID, unable to add more items");
+ "The stream has exhausted the last possible ID, unable to add more items");
}
if (!first_entry) {
if (metadata.last_generated_id.ms > options.entry_id.ms) {
return rocksdb::Status::InvalidArgument(
- "The ID specified in XADD is equal or smaller than the target stream top item");
+ "The ID specified in XADD is equal or smaller than the target stream top item");
}
if (metadata.last_generated_id.ms == options.entry_id.ms) {
if (!options.entry_id.any_seq_number && metadata.last_generated_id.seq >= options.entry_id.seq) {
return rocksdb::Status::InvalidArgument(
- "The ID specified in XADD is equal or smaller than the target stream top item");
+ "The ID specified in XADD is equal or smaller than the target stream top item");
}
if (options.entry_id.any_seq_number && metadata.last_generated_id.seq == UINT64_MAX) {
return rocksdb::Status::InvalidArgument(
- "Elements are too large to be stored"); // Redis responds with exactly this message
+ "Elements are too large to be stored"); // Redis responds with exactly this message
}
}
@@ -210,8 +209,8 @@ rocksdb::Status Stream::getNextEntryID(const StreamMetadata &metadata, const Str
}
}
-rocksdb::Status Stream::DeleteEntries(const rocksdb::Slice &stream_name,
- const std::vector<StreamEntryID> &ids, uint64_t *ret) {
+rocksdb::Status Stream::DeleteEntries(const rocksdb::Slice &stream_name, const std::vector<StreamEntryID> &ids,
+ uint64_t *ret) {
*ret = 0;
std::string ns_key;
@@ -244,7 +243,7 @@ rocksdb::Status Stream::DeleteEntries(const rocksdb::Slice &stream_name,
auto iter = DBUtil::UniqueIterator(db_, read_options, stream_cf_handle_);
- for (const auto& id : ids) {
+ for (const auto &id : ids) {
std::string entry_key = internalKeyFromEntryID(ns_key, metadata, id);
std::string value;
s = db_->Get(read_options, stream_cf_handle_, entry_key, &value);
@@ -339,8 +338,7 @@ rocksdb::Status Stream::range(const std::string &ns_key, const StreamMetadata &m
return rocksdb::Status::OK();
}
- if ((!options.reverse && options.end < options.start)
- || (options.reverse && options.start < options.end)) {
+ if ((!options.reverse && options.end < options.start) || (options.reverse && options.start < options.end)) {
return rocksdb::Status::OK();
}
@@ -364,8 +362,7 @@ rocksdb::Status Stream::range(const std::string &ns_key, const StreamMetadata &m
iter->SeekForPrev(start_key);
}
- for (;
- iter->Valid() && (options.reverse ? iter->key().ToString() >= end_key : iter->key().ToString() <= end_key);
+ for (; iter->Valid() && (options.reverse ? iter->key().ToString() >= end_key : iter->key().ToString() <= end_key);
options.reverse ? iter->Prev() : iter->Next()) {
if (options.exclude_start && iter->key().ToString() == start_key) {
continue;
@@ -397,8 +394,7 @@ rocksdb::Status Stream::getEntryRawValue(const std::string &ns_key, const Stream
return db_->Get(rocksdb::ReadOptions(), stream_cf_handle_, entry_key, value);
}
-rocksdb::Status Stream::GetStreamInfo(const rocksdb::Slice &stream_name, bool full,
- uint64_t count, StreamInfo *info) {
+rocksdb::Status Stream::GetStreamInfo(const rocksdb::Slice &stream_name, bool full, uint64_t count, StreamInfo *info) {
std::string ns_key;
AppendNamespacePrefix(stream_name, &ns_key);
@@ -533,8 +529,8 @@ rocksdb::Status Stream::Trim(const rocksdb::Slice &stream_name, const StreamTrim
return rocksdb::Status::OK();
}
-uint64_t Stream::trim(const std::string &ns_key, const StreamTrimOptions &options,
- StreamMetadata *metadata, rocksdb::WriteBatch *batch) {
+uint64_t Stream::trim(const std::string &ns_key, const StreamTrimOptions &options, StreamMetadata *metadata,
+ rocksdb::WriteBatch *batch) {
if (metadata->size == 0) {
return 0;
}
@@ -607,5 +603,4 @@ uint64_t Stream::trim(const std::string &ns_key, const StreamTrimOptions &option
return ret;
}
-
} // namespace Redis
diff --git a/src/redis_stream.h b/src/redis_stream.h
index c83d498..e777adb 100644
--- a/src/redis_stream.h
+++ b/src/redis_stream.h
@@ -34,16 +34,14 @@ namespace Redis {
class Stream : public SubKeyScanner {
public:
- explicit Stream(Engine::Storage *storage, const std::string &ns) :
- SubKeyScanner(storage, ns),
- stream_cf_handle_(storage->GetCFHandle("stream")) {}
- rocksdb::Status Add(const Slice &stream_name, const StreamAddOptions &options,
- const std::vector<std::string> &values, StreamEntryID *id);
+ explicit Stream(Engine::Storage *storage, const std::string &ns)
+ : SubKeyScanner(storage, ns), stream_cf_handle_(storage->GetCFHandle("stream")) {}
+ rocksdb::Status Add(const Slice &stream_name, const StreamAddOptions &options, const std::vector<std::string> &values,
+ StreamEntryID *id);
rocksdb::Status DeleteEntries(const Slice &stream_name, const std::vector<StreamEntryID> &ids, uint64_t *ret);
rocksdb::Status Len(const Slice &stream_name, uint64_t *ret);
rocksdb::Status GetStreamInfo(const Slice &stream_name, bool full, uint64_t count, StreamInfo *info);
- rocksdb::Status Range(const Slice &stream_name, const StreamRangeOptions &options,
- std::vector<StreamEntry> *entries);
+ rocksdb::Status Range(const Slice &stream_name, const StreamRangeOptions &options, std::vector<StreamEntry> *entries);
rocksdb::Status Trim(const Slice &stream_name, const StreamTrimOptions &options, uint64_t *ret);
rocksdb::Status GetMetadata(const Slice &stream_name, StreamMetadata *metadata);
rocksdb::Status GetLastGeneratedID(const Slice &stream_name, StreamEntryID *id);
@@ -51,17 +49,17 @@ class Stream : public SubKeyScanner {
private:
rocksdb::ColumnFamilyHandle *stream_cf_handle_;
- rocksdb::Status range(const std::string &ns_key, const StreamMetadata &metadata,
- const StreamRangeOptions &options, std::vector<StreamEntry> *entries) const;
- rocksdb::Status getEntryRawValue(const std::string &ns_key, const StreamMetadata &metadata,
- const StreamEntryID &id, std::string *value) const;
+ rocksdb::Status range(const std::string &ns_key, const StreamMetadata &metadata, const StreamRangeOptions &options,
+ std::vector<StreamEntry> *entries) const;
+ rocksdb::Status getEntryRawValue(const std::string &ns_key, const StreamMetadata &metadata, const StreamEntryID &id,
+ std::string *value) const;
StreamEntryID entryIDFromInternalKey(const rocksdb::Slice &key) const;
std::string internalKeyFromEntryID(const std::string &ns_key, const StreamMetadata &metadata,
const StreamEntryID &id) const;
- rocksdb::Status getNextEntryID(const StreamMetadata &metadata, const StreamAddOptions &options,
- bool first_entry, StreamEntryID *next_entry_id) const;
- uint64_t trim(const std::string &ns_key, const StreamTrimOptions &options,
- StreamMetadata *metadata, rocksdb::WriteBatch *batch);
+ rocksdb::Status getNextEntryID(const StreamMetadata &metadata, const StreamAddOptions &options, bool first_entry,
+ StreamEntryID *next_entry_id) const;
+ uint64_t trim(const std::string &ns_key, const StreamTrimOptions &options, StreamMetadata *metadata,
+ rocksdb::WriteBatch *batch);
};
} // namespace Redis
diff --git a/src/redis_stream_base.cc b/src/redis_stream_base.cc
index 3e033ce..c05c470 100644
--- a/src/redis_stream_base.cc
+++ b/src/redis_stream_base.cc
@@ -19,15 +19,16 @@
*/
#include "redis_stream_base.h"
+
#include "encoding.h"
-#include "util.h"
#include "parse_util.h"
+#include "util.h"
namespace Redis {
-const char* kErrLastEntryIdReached = "last possible entry id reached";
-const char* kErrInvalidEntryIdSpecified = "Invalid stream ID specified as stream command argument";
-const char* kErrDecodingStreamEntryValueFailure = "failed to decode stream entry value";
+const char *kErrLastEntryIdReached = "last possible entry id reached";
+const char *kErrInvalidEntryIdSpecified = "Invalid stream ID specified as stream command argument";
+const char *kErrDecodingStreamEntryValueFailure = "failed to decode stream entry value";
rocksdb::Status IncrementStreamEntryID(StreamEntryID *id) {
if (id->seq == UINT64_MAX) {
@@ -67,14 +68,14 @@ Status ParseStreamEntryID(const std::string &input, StreamEntryID *id) {
auto parse_ms = ParseInt<uint64_t>(ms_str, 10);
auto parse_seq = ParseInt<uint64_t>(seq_str, 10);
if (!parse_ms || !parse_seq) {
- return Status(Status::RedisParseErr, kErrInvalidEntryIdSpecified);
+ return Status(Status::RedisParseErr, kErrInvalidEntryIdSpecified);
}
id->ms = *parse_ms;
id->seq = *parse_seq;
} else {
auto parse_input = ParseInt<uint64_t>(input, 10);
if (!parse_input) {
- return Status(Status::RedisParseErr, kErrInvalidEntryIdSpecified);
+ return Status(Status::RedisParseErr, kErrInvalidEntryIdSpecified);
}
id->ms = *parse_input;
id->seq = 0;
@@ -111,13 +112,10 @@ Status ParseNewStreamEntryID(const std::string &input, NewStreamEntryID *id) {
id->seq = 0;
}
-
return Status::OK();
}
-Status ParseRangeStart(const std::string &input, StreamEntryID *id) {
- return ParseStreamEntryID(input, id);
-}
+Status ParseRangeStart(const std::string &input, StreamEntryID *id) { return ParseStreamEntryID(input, id); }
Status ParseRangeEnd(const std::string &input, StreamEntryID *id) {
auto pos = input.find("-");
@@ -127,14 +125,14 @@ Status ParseRangeEnd(const std::string &input, StreamEntryID *id) {
auto parse_ms = ParseInt<uint64_t>(ms_str, 10);
auto parse_seq = ParseInt<uint64_t>(seq_str, 10);
if (!parse_ms || !parse_seq) {
- return Status(Status::RedisParseErr, kErrInvalidEntryIdSpecified);
+ return Status(Status::RedisParseErr, kErrInvalidEntryIdSpecified);
}
id->ms = *parse_ms;
id->seq = *parse_seq;
} else {
auto parse_input = ParseInt<uint64_t>(input, 10);
if (!parse_input) {
- return Status(Status::RedisParseErr, kErrInvalidEntryIdSpecified);
+ return Status(Status::RedisParseErr, kErrInvalidEntryIdSpecified);
}
id->ms = *parse_input;
id->seq = UINT64_MAX;
diff --git a/src/redis_stream_base.h b/src/redis_stream_base.h
index 44232b0..bc0207e 100644
--- a/src/redis_stream_base.h
+++ b/src/redis_stream_base.h
@@ -44,7 +44,10 @@ struct StreamEntryID {
StreamEntryID() {}
StreamEntryID(uint64_t ms, uint64_t seq) : ms(ms), seq(seq) {}
- void Clear() { ms = 0; seq = 0; }
+ void Clear() {
+ ms = 0;
+ seq = 0;
+ }
bool IsMaximum() const { return ms == UINT64_MAX && seq == UINT64_MAX; }
bool IsMinimum() const { return ms == 0 && seq == 0; }
@@ -55,25 +58,15 @@ struct StreamEntryID {
return false;
}
- bool operator>=(const StreamEntryID &rhs) const {
- return !(*this < rhs);
- }
+ bool operator>=(const StreamEntryID &rhs) const { return !(*this < rhs); }
- bool operator>(const StreamEntryID &rhs) const {
- return rhs < *this;
- }
+ bool operator>(const StreamEntryID &rhs) const { return rhs < *this; }
- bool operator<=(const StreamEntryID &rhs) const {
- return !(rhs < *this);
- }
+ bool operator<=(const StreamEntryID &rhs) const { return !(rhs < *this); }
- bool operator==(const StreamEntryID &rhs) const {
- return ms == rhs.ms && seq == rhs.seq;
- }
+ bool operator==(const StreamEntryID &rhs) const { return ms == rhs.ms && seq == rhs.seq; }
- std::string ToString() const {
- return std::to_string(ms) + "-" + std::to_string(seq);
- }
+ std::string ToString() const { return std::to_string(ms) + "-" + std::to_string(seq); }
static StreamEntryID Minimum() { return StreamEntryID{0, 0}; }
static StreamEntryID Maximum() { return StreamEntryID{UINT64_MAX, UINT64_MAX}; }
@@ -135,7 +128,7 @@ struct StreamReadResult {
std::vector<StreamEntry> entries;
StreamReadResult(std::string name, std::vector<StreamEntry> result)
- : name(std::move(name)), entries(std::move(result)) {}
+ : name(std::move(name)), entries(std::move(result)) {}
};
rocksdb::Status IncrementStreamEntryID(StreamEntryID *id);
@@ -147,5 +140,4 @@ Status ParseRangeEnd(const std::string &input, StreamEntryID *id);
std::string EncodeStreamEntryValue(const std::vector<std::string> &args);
Status DecodeRawStreamEntryValue(const std::string &value, std::vector<std::string> *result);
-
} // namespace Redis
diff --git a/src/redis_string.cc b/src/redis_string.cc
index c982b86..ae1aa4c 100644
--- a/src/redis_string.cc
+++ b/src/redis_string.cc
@@ -19,17 +19,18 @@
*/
#include "redis_string.h"
-#include <utility>
-#include <string>
-#include <limits>
+
#include <cmath>
+#include <limits>
+#include <string>
+#include <utility>
+
#include "parse_util.h"
namespace Redis {
-std::vector<rocksdb::Status> String::getRawValues(
- const std::vector<Slice> &keys,
- std::vector<std::string> *raw_values) {
+std::vector<rocksdb::Status> String::getRawValues(const std::vector<Slice> &keys,
+ std::vector<std::string> *raw_values) {
raw_values->clear();
rocksdb::ReadOptions read_options;
@@ -38,8 +39,7 @@ std::vector<rocksdb::Status> String::getRawValues(
raw_values->resize(keys.size());
std::vector<rocksdb::Status> statuses(keys.size());
std::vector<rocksdb::PinnableSlice> pin_values(keys.size());
- db_->MultiGet(read_options, metadata_cf_handle_, keys.size(),
- keys.data(), pin_values.data(), statuses.data(), false);
+ db_->MultiGet(read_options, metadata_cf_handle_, keys.size(), keys.data(), pin_values.data(), statuses.data(), false);
for (size_t i = 0; i < keys.size(); i++) {
if (!statuses[i].ok()) continue;
(*raw_values)[i].assign(pin_values[i].data(), pin_values[i].size());
@@ -86,7 +86,7 @@ rocksdb::Status String::getValue(const std::string &ns_key, std::string *value)
std::string raw_value;
auto s = getRawValue(ns_key, &raw_value);
if (!s.ok()) return s;
- *value = raw_value.substr(STRING_HDR_SIZE, raw_value.size()-STRING_HDR_SIZE);
+ *value = raw_value.substr(STRING_HDR_SIZE, raw_value.size() - STRING_HDR_SIZE);
return rocksdb::Status::OK();
}
@@ -94,7 +94,7 @@ std::vector<rocksdb::Status> String::getValues(const std::vector<Slice> &ns_keys
auto statuses = getRawValues(ns_keys, values);
for (size_t i = 0; i < ns_keys.size(); i++) {
if (!statuses[i].ok()) continue;
- (*values)[i] = (*values)[i].substr(STRING_HDR_SIZE, (*values)[i].size()-STRING_HDR_SIZE);
+ (*values)[i] = (*values)[i].substr(STRING_HDR_SIZE, (*values)[i].size() - STRING_HDR_SIZE);
}
return statuses;
}
@@ -121,7 +121,7 @@ rocksdb::Status String::Append(const std::string &user_key, const std::string &v
metadata.Encode(&raw_value);
}
raw_value.append(value);
- *ret = static_cast<int>(raw_value.size()-STRING_HDR_SIZE);
+ *ret = static_cast<int>(raw_value.size() - STRING_HDR_SIZE);
return updateRawValue(ns_key, raw_value);
}
@@ -151,31 +151,31 @@ rocksdb::Status String::Get(const std::string &user_key, std::string *value) {
}
rocksdb::Status String::GetEx(const std::string &user_key, std::string *value, int ttl) {
- uint32_t expire = 0;
- if (ttl > 0) {
- int64_t now;
- rocksdb::Env::Default()->GetCurrentTime(&now);
- expire = uint32_t(now) + ttl;
- }
- std::string ns_key;
- AppendNamespacePrefix(user_key, &ns_key);
+ uint32_t expire = 0;
+ if (ttl > 0) {
+ int64_t now;
+ rocksdb::Env::Default()->GetCurrentTime(&now);
+ expire = uint32_t(now) + ttl;
+ }
+ std::string ns_key;
+ AppendNamespacePrefix(user_key, &ns_key);
- LockGuard guard(storage_->GetLockManager(), ns_key);
- rocksdb::Status s = getValue(ns_key, value);
- if (!s.ok() && s.IsNotFound()) return s;
+ LockGuard guard(storage_->GetLockManager(), ns_key);
+ rocksdb::Status s = getValue(ns_key, value);
+ if (!s.ok() && s.IsNotFound()) return s;
- std::string raw_data;
- Metadata metadata(kRedisString, false);
- metadata.expire = expire;
- metadata.Encode(&raw_data);
- raw_data.append(value->data(), value->size());
- rocksdb::WriteBatch batch;
- WriteBatchLogData log_data(kRedisString);
- batch.PutLogData(log_data.Encode());
- batch.Put(metadata_cf_handle_, ns_key, raw_data);
- s = storage_->Write(storage_->DefaultWriteOptions(), &batch);
- if (!s.ok()) return s;
- return rocksdb::Status::OK();
+ std::string raw_data;
+ Metadata metadata(kRedisString, false);
+ metadata.expire = expire;
+ metadata.Encode(&raw_data);
+ raw_data.append(value->data(), value->size());
+ rocksdb::WriteBatch batch;
+ WriteBatchLogData log_data(kRedisString);
+ batch.PutLogData(log_data.Encode());
+ batch.Put(metadata_cf_handle_, ns_key, raw_data);
+ s = storage_->Write(storage_->DefaultWriteOptions(), &batch);
+ if (!s.ok()) return s;
+ return rocksdb::Status::OK();
}
rocksdb::Status String::GetSet(const std::string &user_key, const std::string &new_value, std::string *old_value) {
@@ -194,7 +194,7 @@ rocksdb::Status String::GetSet(const std::string &user_key, const std::string &n
// prev status was used to tell whether old value was empty or not
return !write_status.ok() ? write_status : s;
}
-rocksdb::Status String::GetDel(const std::string &user_key, std::string *value) {
+rocksdb::Status String::GetDel(const std::string &user_key, std::string *value) {
std::string ns_key;
AppendNamespacePrefix(user_key, &ns_key);
@@ -273,7 +273,7 @@ rocksdb::Status String::SetRange(const std::string &user_key, int offset, const
offset += STRING_HDR_SIZE;
if (offset > size) {
// padding the value with zero byte while offset is longer than value size
- int paddings = offset-size;
+ int paddings = offset - size;
raw_value.append(paddings, '\0');
}
if (offset + static_cast<int>(value.size()) >= size) {
@@ -281,10 +281,10 @@ rocksdb::Status String::SetRange(const std::string &user_key, int offset, const
raw_value.append(value);
} else {
for (size_t i = 0; i < value.size(); i++) {
- raw_value[offset+i] = value[i];
+ raw_value[offset + i] = value[i];
}
}
- *ret = static_cast<int>(raw_value.size()-STRING_HDR_SIZE);
+ *ret = static_cast<int>(raw_value.size() - STRING_HDR_SIZE);
return updateRawValue(ns_key, raw_value);
}
@@ -301,7 +301,7 @@ rocksdb::Status String::IncrBy(const std::string &user_key, int64_t increment, i
metadata.Encode(&raw_value);
}
- value = raw_value.substr(STRING_HDR_SIZE, raw_value.size()-STRING_HDR_SIZE);
+ value = raw_value.substr(STRING_HDR_SIZE, raw_value.size() - STRING_HDR_SIZE);
int64_t n = 0;
if (!value.empty()) {
auto parse_result = ParseInt<int64_t>(value, 10);
@@ -313,8 +313,8 @@ rocksdb::Status String::IncrBy(const std::string &user_key, int64_t increment, i
}
n = *parse_result;
}
- if ((increment < 0 && n <= 0 && increment < (LLONG_MIN-n))
- || (increment > 0 && n >= 0 && increment > (LLONG_MAX-n))) {
+ if ((increment < 0 && n <= 0 && increment < (LLONG_MIN - n)) ||
+ (increment > 0 && n >= 0 && increment > (LLONG_MAX - n))) {
return rocksdb::Status::InvalidArgument("increment or decrement would overflow");
}
n += increment;
@@ -337,13 +337,13 @@ rocksdb::Status String::IncrByFloat(const std::string &user_key, double incremen
Metadata metadata(kRedisString, false);
metadata.Encode(&raw_value);
}
- value = raw_value.substr(STRING_HDR_SIZE, raw_value.size()-STRING_HDR_SIZE);
+ value = raw_value.substr(STRING_HDR_SIZE, raw_value.size() - STRING_HDR_SIZE);
double n = 0;
std::size_t idx;
if (!value.empty()) {
try {
n = std::stod(value, &idx);
- } catch(std::exception &e) {
+ } catch (std::exception &e) {
return rocksdb::Status::InvalidArgument("value is not an float");
}
if (isspace(value[0]) || idx != value.size()) {
@@ -438,8 +438,8 @@ rocksdb::Status String::MSetNX(const std::vector<StringPair> &pairs, int ttl, in
// 1 if the operation is successful
// -1 if the user_key does not exist
// 0 if the operation fails
-rocksdb::Status String::CAS(const std::string &user_key, const std::string &old_value,
- const std::string &new_value, int ttl, int *ret) {
+rocksdb::Status String::CAS(const std::string &user_key, const std::string &old_value, const std::string &new_value,
+ int ttl, int *ret) {
*ret = 0;
std::string ns_key, current_value;
@@ -501,8 +501,7 @@ rocksdb::Status String::CAD(const std::string &user_key, const std::string &valu
if (value == current_value) {
auto delete_status = storage_->Delete(storage_->DefaultWriteOptions(),
- storage_->GetCFHandle(Engine::kMetadataColumnFamilyName),
- ns_key);
+ storage_->GetCFHandle(Engine::kMetadataColumnFamilyName), ns_key);
if (!delete_status.ok()) {
return s;
}
diff --git a/src/redis_string.h b/src/redis_string.h
index 7af77e2..90bd575 100644
--- a/src/redis_string.h
+++ b/src/redis_string.h
@@ -20,8 +20,8 @@
#pragma once
-#include <vector>
#include <string>
+#include <vector>
#include "redis_db.h"
#include "redis_metadata.h"
@@ -53,8 +53,8 @@ class String : public Database {
std::vector<rocksdb::Status> MGet(const std::vector<Slice> &keys, std::vector<std::string> *values);
rocksdb::Status MSet(const std::vector<StringPair> &pairs, int ttl = 0);
rocksdb::Status MSetNX(const std::vector<StringPair> &pairs, int ttl, int *ret);
- rocksdb::Status CAS(const std::string &user_key, const std::string &old_value,
- const std::string &new_value, int ttl, int *ret);
+ rocksdb::Status CAS(const std::string &user_key, const std::string &old_value, const std::string &new_value, int ttl,
+ int *ret);
rocksdb::Status CAD(const std::string &user_key, const std::string &value, int *ret);
private:
diff --git a/src/redis_zset.cc b/src/redis_zset.cc
index a1fa0e3..1fa2853 100644
--- a/src/redis_zset.cc
+++ b/src/redis_zset.cc
@@ -21,14 +21,15 @@
#include "redis_zset.h"
#include <math.h>
-#include <map>
-#include <limits>
+
#include <cmath>
+#include <limits>
+#include <map>
#include <memory>
#include <set>
-#include "util.h"
#include "db_util.h"
+#include "util.h"
namespace Redis {
@@ -53,9 +54,8 @@ rocksdb::Status ZSet::Add(const Slice &user_key, uint8_t flags, std::vector<Memb
batch.PutLogData(log_data.Encode());
std::string member_key;
std::set<std::string> added_member_keys;
- for (int i = static_cast<int>(mscores->size()-1); i >= 0; i--) {
- InternalKey(ns_key, (*mscores)[i].member, metadata.version,
- storage_->IsSlotIdEncoded()).Encode(&member_key);
+ for (int i = static_cast<int>(mscores->size() - 1); i >= 0; i--) {
+ InternalKey(ns_key, (*mscores)[i].member, metadata.version, storage_->IsSlotIdEncoded()).Encode(&member_key);
// Fix the corner case that adds the same member which may add the score
// column family many times and cause problems in the ZRANGE command.
@@ -124,7 +124,7 @@ rocksdb::Status ZSet::Card(const Slice &user_key, int *ret) {
ZSetMetadata metadata(false);
rocksdb::Status s = GetMetadata(ns_key, &metadata);
- if (!s.ok()) return s.IsNotFound()? rocksdb::Status::OK():s;
+ if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
*ret = metadata.size;
return rocksdb::Status::OK();
}
@@ -153,8 +153,8 @@ rocksdb::Status ZSet::Pop(const Slice &user_key, int count, bool min, std::vecto
LockGuard guard(storage_->GetLockManager(), ns_key);
ZSetMetadata metadata(false);
rocksdb::Status s = GetMetadata(ns_key, &metadata);
- if (!s.ok()) return s.IsNotFound()? rocksdb::Status::OK():s;
- if (count <=0) return rocksdb::Status::OK();
+ if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
+ if (count <= 0) return rocksdb::Status::OK();
if (count > static_cast<int>(metadata.size)) count = metadata.size;
std::string score_bytes;
@@ -184,9 +184,7 @@ rocksdb::Status ZSet::Pop(const Slice &user_key, int count, bool min, std::vecto
if (!min && (!iter->Valid() || !iter->key().starts_with(prefix_key))) {
iter->SeekForPrev(start_key);
}
- for (;
- iter->Valid() && iter->key().starts_with(prefix_key);
- min ? iter->Next() : iter->Prev()) {
+ for (; iter->Valid() && iter->key().starts_with(prefix_key); min ? iter->Next() : iter->Prev()) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
Slice score_key = ikey.GetSubKey();
GetDouble(&score_key, &score);
@@ -207,8 +205,8 @@ rocksdb::Status ZSet::Pop(const Slice &user_key, int count, bool min, std::vecto
return storage_->Write(storage_->DefaultWriteOptions(), &batch);
}
-rocksdb::Status ZSet::Range(const Slice &user_key, int start, int stop, uint8_t flags, std::vector<MemberScore>
-*mscores) {
+rocksdb::Status ZSet::Range(const Slice &user_key, int start, int stop, uint8_t flags,
+ std::vector<MemberScore> *mscores) {
mscores->clear();
std::string ns_key;
@@ -221,7 +219,7 @@ rocksdb::Status ZSet::Range(const Slice &user_key, int start, int stop, uint8_t
if (removed) lock_guard = Util::MakeUnique<LockGuard>(storage_->GetLockManager(), ns_key);
ZSetMetadata metadata(false);
rocksdb::Status s = GetMetadata(ns_key, &metadata);
- if (!s.ok()) return s.IsNotFound()? rocksdb::Status::OK():s;
+ if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
if (start < 0) start += metadata.size;
if (stop < 0) stop += metadata.size;
if (start < 0) start = 0;
@@ -256,9 +254,7 @@ rocksdb::Status ZSet::Range(const Slice &user_key, int start, int stop, uint8_t
iter->SeekForPrev(start_key);
}
- for (;
- iter->Valid() && iter->key().starts_with(prefix_key);
- !reversed ? iter->Next() : iter->Prev()) {
+ for (; iter->Valid() && iter->key().starts_with(prefix_key); !reversed ? iter->Next() : iter->Prev()) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
Slice score_key = ikey.GetSubKey();
GetDouble(&score_key, &score);
@@ -285,10 +281,8 @@ rocksdb::Status ZSet::Range(const Slice &user_key, int start, int stop, uint8_t
return rocksdb::Status::OK();
}
-rocksdb::Status ZSet::RangeByScore(const Slice &user_key,
- ZRangeSpec spec,
- std::vector<MemberScore> *mscores,
- int *size) {
+rocksdb::Status ZSet::RangeByScore(const Slice &user_key, ZRangeSpec spec, std::vector<MemberScore> *mscores,
+ int *size) {
if (size) *size = 0;
if (mscores) mscores->clear();
@@ -299,7 +293,7 @@ rocksdb::Status ZSet::RangeByScore(const Slice &user_key,
if (spec.removed) lock_guard = Util::MakeUnique<LockGuard>(storage_->GetLockManager(), ns_key);
ZSetMetadata metadata(false);
rocksdb::Status s = GetMetadata(ns_key, &metadata);
- if (!s.ok()) return s.IsNotFound()? rocksdb::Status::OK():s;
+ if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
// let's get familiar with score first:
// a. score of zset's member is represented by double and it takes up 8 bytes in rocksdb
@@ -333,9 +327,9 @@ rocksdb::Status ZSet::RangeByScore(const Slice &user_key,
int64_t i64 = 0;
double max_next_score = 0;
if (spec.reversed && !spec.maxex) {
- memcpy(&i64, &spec.max, sizeof(spec.max));
- i64 = i64 >= 0 ? i64 + 1 : i64 - 1;
- memcpy(&max_next_score, &i64, sizeof(i64));
+ memcpy(&i64, &spec.max, sizeof(spec.max));
+ i64 = i64 >= 0 ? i64 + 1 : i64 - 1;
+ memcpy(&max_next_score, &i64, sizeof(i64));
}
std::string start_score_bytes;
@@ -368,9 +362,7 @@ rocksdb::Status ZSet::RangeByScore(const Slice &user_key,
}
}
- for (;
- iter->Valid() && iter->key().starts_with(prefix_key);
- !spec.reversed ? iter->Next() : iter->Prev()) {
+ for (; iter->Valid() && iter->key().starts_with(prefix_key); !spec.reversed ? iter->Next() : iter->Prev()) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
Slice score_key = ikey.GetSubKey();
double score;
@@ -405,14 +397,12 @@ rocksdb::Status ZSet::RangeByScore(const Slice &user_key,
return rocksdb::Status::OK();
}
-rocksdb::Status ZSet::RangeByLex(const Slice &user_key,
- ZRangeLexSpec spec,
- std::vector<std::string> *members,
+rocksdb::Status ZSet::RangeByLex(const Slice &user_key, ZRangeLexSpec spec, std::vector<std::string> *members,
int *size) {
if (size) *size = 0;
if (members) members->clear();
if (spec.offset > -1 && spec.count == 0) {
- return rocksdb::Status::OK();
+ return rocksdb::Status::OK();
}
std::string ns_key;
@@ -420,8 +410,7 @@ rocksdb::Status ZSet::RangeByLex(const Slice &user_key,
std::unique_ptr<LockGuard> lock_guard;
if (spec.removed) {
- lock_guard = Util::MakeUnique<LockGuard>(
- storage_->GetLockManager(), ns_key);
+ lock_guard = Util::MakeUnique<LockGuard>(storage_->GetLockManager(), ns_key);
}
ZSetMetadata metadata(false);
rocksdb::Status s = GetMetadata(ns_key, &metadata);
@@ -458,21 +447,19 @@ rocksdb::Status ZSet::RangeByLex(const Slice &user_key,
}
}
- for (;
- iter->Valid() && iter->key().starts_with(prefix_key);
- (!spec.reversed ? iter->Next() : iter->Prev())) {
+ for (; iter->Valid() && iter->key().starts_with(prefix_key); (!spec.reversed ? iter->Next() : iter->Prev())) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
Slice member = ikey.GetSubKey();
if (spec.reversed) {
- if (member.ToString() < spec.min || (spec.minex && member == spec.min)) {
- break;
- }
- if ((spec.maxex && member == spec.max) || (!spec.max_infinite && member.ToString() > spec.max)) {
- continue;
- }
+ if (member.ToString() < spec.min || (spec.minex && member == spec.min)) {
+ break;
+ }
+ if ((spec.maxex && member == spec.max) || (!spec.max_infinite && member.ToString() > spec.max)) {
+ continue;
+ }
} else {
- if (spec.minex && member == spec.min) continue; // the min member was exclusive
- if ((spec.maxex && member == spec.max) || (!spec.max_infinite && member.ToString() > spec.max)) break;
+ if (spec.minex && member == spec.min) continue; // the min member was exclusive
+ if ((spec.maxex && member == spec.max) || (!spec.max_infinite && member.ToString() > spec.max)) break;
}
if (spec.offset >= 0 && pos++ < spec.offset) continue;
if (spec.removed) {
@@ -526,7 +513,7 @@ rocksdb::Status ZSet::Remove(const Slice &user_key, const std::vector<Slice> &me
LockGuard guard(storage_->GetLockManager(), ns_key);
ZSetMetadata metadata(false);
rocksdb::Status s = GetMetadata(ns_key, &metadata);
- if (!s.ok()) return s.IsNotFound()? rocksdb::Status::OK():s;
+ if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
rocksdb::WriteBatch batch;
WriteBatchLogData log_data(kRedisZSet);
@@ -580,7 +567,7 @@ rocksdb::Status ZSet::Rank(const Slice &user_key, const Slice &member, bool reve
AppendNamespacePrefix(user_key, &ns_key);
ZSetMetadata metadata(false);
rocksdb::Status s = GetMetadata(ns_key, &metadata);
- if (!s.ok()) return s.IsNotFound()? rocksdb::Status::OK():s;
+ if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
rocksdb::ReadOptions read_options;
LatestSnapShot ss(db_);
@@ -588,7 +575,7 @@ rocksdb::Status ZSet::Rank(const Slice &user_key, const Slice &member, bool reve
std::string score_bytes, member_key;
InternalKey(ns_key, member, metadata.version, storage_->IsSlotIdEncoded()).Encode(&member_key);
s = db_->Get(read_options, member_key, &score_bytes);
- if (!s.ok()) return s.IsNotFound()? rocksdb::Status::OK():s;
+ if (!s.ok()) return s.IsNotFound() ? rocksdb::Status::OK() : s;
double target_score = DecodeDouble(score_bytes.data());
std::string start_score_bytes, start_key, prefix_key, next_verison_prefix_key;
@@ -611,9 +598,7 @@ rocksdb::Status ZSet::Rank(const Slice &user_key, const Slice &member, bool reve
if (reversed && (!iter->Valid() || !iter->key().starts_with(prefix_key))) {
iter->SeekForPrev(start_key);
}
- for (;
- iter->Valid() && iter->key().starts_with(prefix_key);
- !reversed ? iter->Next() : iter->Prev()) {
+ for (; iter->Valid() && iter->key().starts_with(prefix_key); !reversed ? iter->Next() : iter->Prev()) {
InternalKey ikey(iter->key(), storage_->IsSlotIdEncoded());
Slice score_key = ikey.GetSubKey();
double score;
@@ -651,10 +636,8 @@ rocksdb::Status ZSet::Overwrite(const Slice &user_key, const std::vector<MemberS
return storage_->Write(storage_->DefaultWriteOptions(), &batch);
}
-rocksdb::Status ZSet::InterStore(const Slice &dst,
- const std::vector<KeyWeight> &keys_weights,
- AggregateMethod aggregate_method,
- int *size) {
+rocksdb::Status ZSet::InterStore(const Slice &dst, const std::vector<KeyWeight> &keys_weights,
+ AggregateMethod aggregate_method, int *size) {
if (size) *size = 0;
std::map<std::string, double> dst_zset;
@@ -711,10 +694,8 @@ rocksdb::Status ZSet::InterStore(const Slice &dst,
return rocksdb::Status::OK();
}
-rocksdb::Status ZSet::UnionStore(const Slice &dst,
- const std::vector<KeyWeight> &keys_weights,
- AggregateMethod aggregate_method,
- int *size) {
+rocksdb::Status ZSet::UnionStore(const Slice &dst, const std::vector<KeyWeight> &keys_weights,
+ AggregateMethod aggregate_method, int *size) {
if (size) *size = 0;
std::map<std::string, double> dst_zset;
@@ -734,8 +715,7 @@ rocksdb::Status ZSet::UnionStore(const Slice &dst,
switch (aggregate_method) {
case kAggregateSum:
dst_zset[ms.member] += score;
- if (std::isnan(dst_zset[ms.member]))
- dst_zset[ms.member] = 0;
+ if (std::isnan(dst_zset[ms.member])) dst_zset[ms.member] = 0;
break;
case kAggregateMin:
if (dst_zset[ms.member] > score) {
@@ -767,7 +747,7 @@ Status ZSet::ParseRangeSpec(const std::string &min, const std::string &max, ZRan
const char *sptr = nullptr;
char *eptr = nullptr;
- if (min == "+inf" || max == "-inf") {
+ if (min == "+inf" || max == "-inf") {
return Status(Status::NotOK, "min > max");
}
@@ -834,11 +814,8 @@ Status ZSet::ParseRangeLexSpec(const std::string &min, const std::string &max, Z
return Status::OK();
}
-rocksdb::Status ZSet::Scan(const Slice &user_key,
- const std::string &cursor,
- uint64_t limit,
- const std::string &member_prefix,
- std::vector<std::string> *members,
+rocksdb::Status ZSet::Scan(const Slice &user_key, const std::string &cursor, uint64_t limit,
+ const std::string &member_prefix, std::vector<std::string> *members,
std::vector<double> *scores) {
if (scores != nullptr) {
std::vector<std::string> values;
@@ -854,8 +831,7 @@ rocksdb::Status ZSet::Scan(const Slice &user_key,
return SubKeyScanner::Scan(kRedisZSet, user_key, cursor, limit, member_prefix, members);
}
-rocksdb::Status ZSet::MGet(const Slice &user_key,
- const std::vector<Slice> &members,
+rocksdb::Status ZSet::MGet(const Slice &user_key, const std::vector<Slice> &members,
std::map<std::string, double> *mscores) {
mscores->clear();
diff --git a/src/redis_zset.h b/src/redis_zset.h
index c34ab9e..ec581fd 100644
--- a/src/redis_zset.h
+++ b/src/redis_zset.h
@@ -20,24 +20,20 @@
#pragma once
+#include <limits>
#include <map>
#include <string>
#include <vector>
-#include <limits>
#include "redis_db.h"
#include "redis_metadata.h"
-enum AggregateMethod {
- kAggregateSum,
- kAggregateMin,
- kAggregateMax
-};
+enum AggregateMethod { kAggregateSum, kAggregateMin, kAggregateMax };
-const double kMinScore = (std::numeric_limits<float>::is_iec559 ?
- -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::lowest());
-const double kMaxScore = (std::numeric_limits<float>::is_iec559 ?
- std::numeric_limits<double>::infinity() : std::numeric_limits<double>::max());
+const double kMinScore = (std::numeric_limits<float>::is_iec559 ? -std::numeric_limits<double>::infinity()
+ : std::numeric_limits<double>::lowest());
+const double kMaxScore = (std::numeric_limits<float>::is_iec559 ? std::numeric_limits<double>::infinity()
+ : std::numeric_limits<double>::max());
typedef struct ZRangeSpec {
double min, max;
@@ -48,7 +44,8 @@ typedef struct ZRangeSpec {
min = kMinScore;
max = kMaxScore;
minex = maxex = false;
- offset = -1; count = -1;
+ offset = -1;
+ count = -1;
removed = reversed = false;
}
} ZRangeSpec;
@@ -80,18 +77,17 @@ typedef struct {
} MemberScore;
#define ZSET_INCR 1
-#define ZSET_NX (1<<1)
-#define ZSET_XX (1<<2)
-#define ZSET_REVERSED (1<<3)
-#define ZSET_REMOVED 1<<4
+#define ZSET_NX (1 << 1)
+#define ZSET_XX (1 << 2)
+#define ZSET_REVERSED (1 << 3)
+#define ZSET_REMOVED 1 << 4
namespace Redis {
class ZSet : public SubKeyScanner {
public:
- explicit ZSet(Engine::Storage *storage, const std::string &ns) :
- SubKeyScanner(storage, ns),
- score_cf_handle_(storage->GetCFHandle("zset_score")) {}
+ explicit ZSet(Engine::Storage *storage, const std::string &ns)
+ : SubKeyScanner(storage, ns), score_cf_handle_(storage->GetCFHandle("zset_score")) {}
rocksdb::Status Add(const Slice &user_key, uint8_t flags, std::vector<MemberScore> *mscores, int *ret);
rocksdb::Status Card(const Slice &user_key, int *ret);
rocksdb::Status Count(const Slice &user_key, const ZRangeSpec &spec, int *ret);
@@ -108,23 +104,15 @@ class ZSet : public SubKeyScanner {
rocksdb::Status Score(const Slice &user_key, const Slice &member, double *score);
static Status ParseRangeSpec(const std::string &min, const std::string &max, ZRangeSpec *spec);
static Status ParseRangeLexSpec(const std::string &min, const std::string &max, ZRangeLexSpec *spec);
- rocksdb::Status Scan(const Slice &user_key,
- const std::string &cursor,
- uint64_t limit,
- const std::string &member_prefix,
- std::vector<std::string> *members,
+ rocksdb::Status Scan(const Slice &user_key, const std::string &cursor, uint64_t limit,
+ const std::string &member_prefix, std::vector<std::string> *members,
std::vector<double> *scores = nullptr);
rocksdb::Status Overwrite(const Slice &user_key, const std::vector<MemberScore> &mscores);
- rocksdb::Status InterStore(const Slice &dst,
- const std::vector<KeyWeight> &keys_weights,
- AggregateMethod aggregate_method,
- int *size);
- rocksdb::Status UnionStore(const Slice &dst,
- const std::vector<KeyWeight> &keys_weights,
- AggregateMethod aggregate_method,
- int *size);
- rocksdb::Status MGet(const Slice &user_key,
- const std::vector<Slice> &members,
+ rocksdb::Status InterStore(const Slice &dst, const std::vector<KeyWeight> &keys_weights,
+ AggregateMethod aggregate_method, int *size);
+ rocksdb::Status UnionStore(const Slice &dst, const std::vector<KeyWeight> &keys_weights,
+ AggregateMethod aggregate_method, int *size);
+ rocksdb::Status MGet(const Slice &user_key, const std::vector<Slice> &members,
std::map<std::string, double> *mscores);
rocksdb::Status GetMetadata(const Slice &ns_key, ZSetMetadata *metadata);
diff --git a/src/replication.cc b/src/replication.cc
index b0700b2..9293a65 100644
--- a/src/replication.cc
+++ b/src/replication.cc
@@ -75,8 +75,7 @@ void FeedSlaveThread::checkLivenessIfNeed() {
const auto ping_command = Redis::BulkString("ping");
auto s = Util::SockSend(conn_->GetFD(), ping_command);
if (!s.IsOK()) {
- LOG(ERROR) << "Ping slave[" << conn_->GetAddr() << "] err: " << s.Msg()
- << ", would stop the thread";
+ LOG(ERROR) << "Ping slave[" << conn_->GetAddr() << "] err: " << s.Msg() << ", would stop the thread";
Stop();
}
}
@@ -92,8 +91,8 @@ void FeedSlaveThread::loop() {
while (!IsStopped()) {
if (!iter_ || !iter_->Valid()) {
if (iter_) LOG(INFO) << "WAL was rotated, would reopen again";
- if (!srv_->storage_->WALHasNewData(next_repl_seq_)
- || !srv_->storage_->GetWALIter(next_repl_seq_, &iter_).IsOK()) {
+ if (!srv_->storage_->WALHasNewData(next_repl_seq_) ||
+ !srv_->storage_->GetWALIter(next_repl_seq_, &iter_).IsOK()) {
iter_ = nullptr;
usleep(yield_microseconds);
checkLivenessIfNeed();
@@ -119,15 +118,13 @@ void FeedSlaveThread::loop() {
// 3. To avoid master don't send replication stream to slave since of packing
// batches strategy, we still send batches if current batch sequence is less
// kMaxDelayUpdates than latest sequence.
- if (is_first_repl_batch ||
- batches_bulk.size() >= kMaxDelayBytes ||
- updates_in_batches >= kMaxDelayUpdates ||
+ if (is_first_repl_batch || batches_bulk.size() >= kMaxDelayBytes || updates_in_batches >= kMaxDelayUpdates ||
srv_->storage_->LatestSeq() - batch.sequence <= kMaxDelayUpdates) {
// Send entire bulk which contain multiple batches
auto s = Util::SockSend(conn_->GetFD(), batches_bulk);
if (!s.IsOK()) {
- LOG(ERROR) << "Write error while sending batch to slave: " << s.Msg()
- << ". batches: 0x" << Util::StringToHex(batches_bulk);
+ LOG(ERROR) << "Write error while sending batch to slave: " << s.Msg() << ". batches: 0x"
+ << Util::StringToHex(batches_bulk);
Stop();
return;
}
@@ -150,8 +147,7 @@ void send_string(bufferevent *bev, const std::string &data) {
evbuffer_add(output, data.c_str(), data.length());
}
-void ReplicationThread::CallbacksStateMachine::ConnEventCB(
- bufferevent *bev, int16_t events, void *state_machine_ptr) {
+void ReplicationThread::CallbacksStateMachine::ConnEventCB(bufferevent *bev, int16_t events, void *state_machine_ptr) {
if (events & BEV_EVENT_CONNECTED) {
// call write_cb when connected
bufferevent_data_cb write_cb;
@@ -170,21 +166,20 @@ void ReplicationThread::CallbacksStateMachine::ConnEventCB(
}
}
-void ReplicationThread::CallbacksStateMachine::SetReadCB(
- bufferevent *bev, bufferevent_data_cb cb, void *state_machine_ptr) {
+void ReplicationThread::CallbacksStateMachine::SetReadCB(bufferevent *bev, bufferevent_data_cb cb,
+ void *state_machine_ptr) {
bufferevent_enable(bev, EV_READ);
bufferevent_setcb(bev, cb, nullptr, ConnEventCB, state_machine_ptr);
}
-void ReplicationThread::CallbacksStateMachine::SetWriteCB(
- bufferevent *bev, bufferevent_data_cb cb, void *state_machine_ptr) {
+void ReplicationThread::CallbacksStateMachine::SetWriteCB(bufferevent *bev, bufferevent_data_cb cb,
+ void *state_machine_ptr) {
bufferevent_enable(bev, EV_WRITE);
bufferevent_setcb(bev, nullptr, cb, ConnEventCB, state_machine_ptr);
}
ReplicationThread::CallbacksStateMachine::CallbacksStateMachine(
- ReplicationThread *repl,
- ReplicationThread::CallbacksStateMachine::CallbackList &&handlers)
+ ReplicationThread *repl, ReplicationThread::CallbacksStateMachine::CallbackList &&handlers)
: repl_(repl), handlers_(std::move(handlers)) {
// Note: It may cause data races to use 'masterauth' directly.
// It is acceptable because password change is a low frequency operation.
@@ -194,8 +189,7 @@ ReplicationThread::CallbacksStateMachine::CallbacksStateMachine(
}
}
-void ReplicationThread::CallbacksStateMachine::EvCallback(bufferevent *bev,
- void *ctx) {
+void ReplicationThread::CallbacksStateMachine::EvCallback(bufferevent *bev, void *ctx) {
auto self = static_cast<CallbacksStateMachine *>(ctx);
LOOP_LABEL:
assert(self->handler_idx_ <= self->handlers_.size());
@@ -279,49 +273,29 @@ void ReplicationThread::CallbacksStateMachine::Stop() {
}
}
-ReplicationThread::ReplicationThread(std::string host, uint32_t port,
- Server *srv)
+ReplicationThread::ReplicationThread(std::string host, uint32_t port, Server *srv)
: host_(std::move(host)),
port_(port),
srv_(srv),
storage_(srv->storage_),
repl_state_(kReplConnecting),
- psync_steps_(this,
- CallbacksStateMachine::CallbackList{
- CallbacksStateMachine::CallbackType{
- CallbacksStateMachine::WRITE, "dbname write", checkDBNameWriteCB
- },
- CallbacksStateMachine::CallbackType{
- CallbacksStateMachine::READ, "dbname read", checkDBNameReadCB
- },
- CallbacksStateMachine::CallbackType{
- CallbacksStateMachine::WRITE, "replconf write", replConfWriteCB
- },
- CallbacksStateMachine::CallbackType{
- CallbacksStateMachine::READ, "replconf read", replConfReadCB
- },
- CallbacksStateMachine::CallbackType{
- CallbacksStateMachine::WRITE, "psync write", tryPSyncWriteCB
- },
- CallbacksStateMachine::CallbackType{
- CallbacksStateMachine::READ, "psync read", tryPSyncReadCB
- },
- CallbacksStateMachine::CallbackType{
- CallbacksStateMachine::READ, "batch loop", incrementBatchLoopCB
- }
- }),
- fullsync_steps_(this,
- CallbacksStateMachine::CallbackList{
- CallbacksStateMachine::CallbackType{
- CallbacksStateMachine::WRITE, "fullsync write", fullSyncWriteCB
- },
- CallbacksStateMachine::CallbackType{
- CallbacksStateMachine::READ, "fullsync read", fullSyncReadCB}
- }) {
-}
-
-Status ReplicationThread::Start(std::function<void()> &&pre_fullsync_cb,
- std::function<void()> &&post_fullsync_cb) {
+ psync_steps_(
+ this,
+ CallbacksStateMachine::CallbackList{
+ CallbacksStateMachine::CallbackType{CallbacksStateMachine::WRITE, "dbname write", checkDBNameWriteCB},
+ CallbacksStateMachine::CallbackType{CallbacksStateMachine::READ, "dbname read", checkDBNameReadCB},
+ CallbacksStateMachine::CallbackType{CallbacksStateMachine::WRITE, "replconf write", replConfWriteCB},
+ CallbacksStateMachine::CallbackType{CallbacksStateMachine::READ, "replconf read", replConfReadCB},
+ CallbacksStateMachine::CallbackType{CallbacksStateMachine::WRITE, "psync write", tryPSyncWriteCB},
+ CallbacksStateMachine::CallbackType{CallbacksStateMachine::READ, "psync read", tryPSyncReadCB},
+ CallbacksStateMachine::CallbackType{CallbacksStateMachine::READ, "batch loop", incrementBatchLoopCB}}),
+ fullsync_steps_(
+ this,
+ CallbacksStateMachine::CallbackList{
+ CallbacksStateMachine::CallbackType{CallbacksStateMachine::WRITE, "fullsync write", fullSyncWriteCB},
+ CallbacksStateMachine::CallbackType{CallbacksStateMachine::READ, "fullsync read", fullSyncReadCB}}) {}
+
+Status ReplicationThread::Start(std::function<void()> &&pre_fullsync_cb, std::function<void()> &&post_fullsync_cb) {
pre_fullsync_cb_ = std::move(pre_fullsync_cb);
post_fullsync_cb_ = std::move(post_fullsync_cb);
@@ -382,8 +356,7 @@ void ReplicationThread::run() {
event_base_free(base_);
}
-ReplicationThread::CBState ReplicationThread::authWriteCB(bufferevent *bev,
- void *ctx) {
+ReplicationThread::CBState ReplicationThread::authWriteCB(bufferevent *bev, void *ctx) {
auto self = static_cast<ReplicationThread *>(ctx);
send_string(bev, Redis::MultiBulkString({"AUTH", self->srv_->GetConfig()->masterauth}));
LOG(INFO) << "[replication] Auth request was sent, waiting for response";
@@ -391,8 +364,7 @@ ReplicationThread::CBState ReplicationThread::authWriteCB(bufferevent *bev,
return CBState::NEXT;
}
-ReplicationThread::CBState ReplicationThread::authReadCB(bufferevent *bev,
- void *ctx) {
+ReplicationThread::CBState ReplicationThread::authReadCB(bufferevent *bev, void *ctx) {
auto input = bufferevent_get_input(bev);
UniqueEvbufReadln line(input, EVBUFFER_EOL_CRLF_STRICT);
if (!line) return CBState::AGAIN;
@@ -405,8 +377,7 @@ ReplicationThread::CBState ReplicationThread::authReadCB(bufferevent *bev,
return CBState::NEXT;
}
-ReplicationThread::CBState ReplicationThread::checkDBNameWriteCB(
- bufferevent *bev, void *ctx) {
+ReplicationThread::CBState ReplicationThread::checkDBNameWriteCB(bufferevent *bev, void *ctx) {
send_string(bev, Redis::MultiBulkString({"_db_name"}));
auto self = static_cast<ReplicationThread *>(ctx);
self->repl_state_ = kReplCheckDBName;
@@ -414,8 +385,7 @@ ReplicationThread::CBState ReplicationThread::checkDBNameWriteCB(
return CBState::NEXT;
}
-ReplicationThread::CBState ReplicationThread::checkDBNameReadCB(
- bufferevent *bev, void *ctx) {
+ReplicationThread::CBState ReplicationThread::checkDBNameReadCB(bufferevent *bev, void *ctx) {
auto input = bufferevent_get_input(bev);
UniqueEvbufReadln line(input, EVBUFFER_EOL_CRLF_STRICT);
if (!line) return CBState::AGAIN;
@@ -439,8 +409,7 @@ ReplicationThread::CBState ReplicationThread::checkDBNameReadCB(
return CBState::RESTART;
}
-ReplicationThread::CBState ReplicationThread::replConfWriteCB(
- bufferevent *bev, void *ctx) {
+ReplicationThread::CBState ReplicationThread::replConfWriteCB(bufferevent *bev, void *ctx) {
auto self = static_cast<ReplicationThread *>(ctx);
send_string(bev,
Redis::MultiBulkString({"replconf", "listening-port", std::to_string(self->srv_->GetConfig()->port)}));
@@ -449,8 +418,7 @@ ReplicationThread::CBState ReplicationThread::replConfWriteCB(
return CBState::NEXT;
}
-ReplicationThread::CBState ReplicationThread::replConfReadCB(
- bufferevent *bev, void *ctx) {
+ReplicationThread::CBState ReplicationThread::replConfReadCB(bufferevent *bev, void *ctx) {
auto input = bufferevent_get_input(bev);
UniqueEvbufReadln line(input, EVBUFFER_EOL_CRLF_STRICT);
if (!line) return CBState::AGAIN;
@@ -469,8 +437,7 @@ ReplicationThread::CBState ReplicationThread::replConfReadCB(
}
}
-ReplicationThread::CBState ReplicationThread::tryPSyncWriteCB(
- bufferevent *bev, void *ctx) {
+ReplicationThread::CBState ReplicationThread::tryPSyncWriteCB(bufferevent *bev, void *ctx) {
auto self = static_cast<ReplicationThread *>(ctx);
auto cur_seq = self->storage_->LatestSeq();
auto next_seq = cur_seq + 1;
@@ -493,23 +460,21 @@ ReplicationThread::CBState ReplicationThread::tryPSyncWriteCB(
// To adapt to old master using old PSYNC, i.e. only use next sequence id.
// Also use old PSYNC if replica can't find replication id from WAL and DB.
- if (!self->srv_->GetConfig()->use_rsid_psync ||
- self->next_try_old_psync_ || replid.length() != kReplIdLength) {
+ if (!self->srv_->GetConfig()->use_rsid_psync || self->next_try_old_psync_ || replid.length() != kReplIdLength) {
self->next_try_old_psync_ = false; // Reset next_try_old_psync_
send_string(bev, Redis::MultiBulkString({"PSYNC", std::to_string(next_seq)}));
LOG(INFO) << "[replication] Try to use psync, next seq: " << next_seq;
} else {
// NEW PSYNC "Unique Replication Sequence ID": replication id and sequence id
send_string(bev, Redis::MultiBulkString({"PSYNC", replid, std::to_string(next_seq)}));
- LOG(INFO) << "[replication] Try to use new psync, current unique replication sequence id: "
- << replid << ":" << cur_seq;
+ LOG(INFO) << "[replication] Try to use new psync, current unique replication sequence id: " << replid << ":"
+ << cur_seq;
}
self->repl_state_ = kReplSendPSync;
return CBState::NEXT;
}
-ReplicationThread::CBState ReplicationThread::tryPSyncReadCB(bufferevent *bev,
- void *ctx) {
+ReplicationThread::CBState ReplicationThread::tryPSyncReadCB(bufferevent *bev, void *ctx) {
auto self = static_cast<ReplicationThread *>(ctx);
auto input = bufferevent_get_input(bev);
UniqueEvbufReadln line(input, EVBUFFER_EOL_CRLF_STRICT);
@@ -523,7 +488,7 @@ ReplicationThread::CBState ReplicationThread::tryPSyncReadCB(bufferevent *bev,
if (line[0] == '-' && isWrongPsyncNum(line.get())) {
self->next_try_old_psync_ = true;
LOG(WARNING) << "The old version master, can't handle new PSYNC, "
- << "try old PSYNC again";
+ << "try old PSYNC again";
// Retry previous state, i.e. send PSYNC again
return CBState::PREV;
}
@@ -532,8 +497,7 @@ ReplicationThread::CBState ReplicationThread::tryPSyncReadCB(bufferevent *bev,
// PSYNC isn't OK, we should use FullSync
// Switch to fullsync state machine
self->fullsync_steps_.Start();
- LOG(INFO) << "[replication] Failed to psync, error: " << line.get()
- << ", switch to fullsync";
+ LOG(INFO) << "[replication] Failed to psync, error: " << line.get() << ", switch to fullsync";
return CBState::QUIT;
} else {
// PSYNC is OK, use IncrementBatchLoop
@@ -542,8 +506,7 @@ ReplicationThread::CBState ReplicationThread::tryPSyncReadCB(bufferevent *bev,
}
}
-ReplicationThread::CBState ReplicationThread::incrementBatchLoopCB(
- bufferevent *bev, void *ctx) {
+ReplicationThread::CBState ReplicationThread::incrementBatchLoopCB(bufferevent *bev, void *ctx) {
char *bulk_data = nullptr;
auto self = static_cast<ReplicationThread *>(ctx);
self->repl_state_ = kReplConnected;
@@ -564,7 +527,7 @@ ReplicationThread::CBState ReplicationThread::incrementBatchLoopCB(
}
case Incr_batch_data:
// Read bulk data (batch data)
- if (self->incr_bulk_len_+2 <= evbuffer_get_length(input)) { // We got enough data
+ if (self->incr_bulk_len_ + 2 <= evbuffer_get_length(input)) { // We got enough data
bulk_data = reinterpret_cast<char *>(evbuffer_pullup(input, self->incr_bulk_len_ + 2));
std::string bulk_string = std::string(bulk_data, self->incr_bulk_len_);
// master would send the ping heartbeat packet to check whether the slave was alive or not,
@@ -588,8 +551,7 @@ ReplicationThread::CBState ReplicationThread::incrementBatchLoopCB(
}
}
-ReplicationThread::CBState ReplicationThread::fullSyncWriteCB(
- bufferevent *bev, void *ctx) {
+ReplicationThread::CBState ReplicationThread::fullSyncWriteCB(bufferevent *bev, void *ctx) {
send_string(bev, Redis::MultiBulkString({"_fetch_meta"}));
auto self = static_cast<ReplicationThread *>(ctx);
self->repl_state_ = kReplFetchMeta;
@@ -597,8 +559,7 @@ ReplicationThread::CBState ReplicationThread::fullSyncWriteCB(
return CBState::NEXT;
}
-ReplicationThread::CBState ReplicationThread::fullSyncReadCB(bufferevent *bev,
- void *ctx) {
+ReplicationThread::CBState ReplicationThread::fullSyncReadCB(bufferevent *bev, void *ctx) {
auto self = static_cast<ReplicationThread *>(ctx);
auto input = bufferevent_get_input(bev);
switch (self->fullsync_state_) {
@@ -614,8 +575,8 @@ ReplicationThread::CBState ReplicationThread::fullSyncReadCB(bufferevent *bev,
LOG(ERROR) << "[replication] Failed to fetch meta id: " << line.get();
return CBState::RESTART;
}
- self->fullsync_meta_id_ = static_cast<rocksdb::BackupID>(
- line.length > 0 ? std::strtoul(line.get(), nullptr, 10) : 0);
+ self->fullsync_meta_id_ =
+ static_cast<rocksdb::BackupID>(line.length > 0 ? std::strtoul(line.get(), nullptr, 10) : 0);
if (self->fullsync_meta_id_ == 0) {
LOG(ERROR) << "[replication] Invalid meta id received";
return CBState::RESTART;
@@ -646,8 +607,7 @@ ReplicationThread::CBState ReplicationThread::fullSyncReadCB(bufferevent *bev,
if (evbuffer_get_length(input) < self->fullsync_filesize_) {
return CBState::AGAIN;
}
- meta = Engine::Storage::ReplDataManager::ParseMetaAndSave(
- self->storage_, self->fullsync_meta_id_, input);
+ meta = Engine::Storage::ReplDataManager::ParseMetaAndSave(self->storage_, self->fullsync_meta_id_, input);
target_dir = self->srv_->GetConfig()->backup_sync_dir;
} else {
// Master using new version
@@ -668,16 +628,14 @@ ReplicationThread::CBState ReplicationThread::fullSyncReadCB(bufferevent *bev,
// file doesn't have number.
auto iter = std::find(need_files.begin(), need_files.end(), "CURRENT");
if (iter != need_files.end()) need_files.erase(iter);
- auto s = Engine::Storage::ReplDataManager::CleanInvalidFiles(
- self->storage_, target_dir, need_files);
+ auto s = Engine::Storage::ReplDataManager::CleanInvalidFiles(self->storage_, target_dir, need_files);
if (!s.IsOK()) {
LOG(WARNING) << "[replication] Failed to clean up invalid files of the old checkpoint,"
<< " error: " << s.Msg();
LOG(WARNING) << "[replication] Try to clean all checkpoint files";
auto s = rocksdb::DestroyDB(target_dir, rocksdb::Options());
if (!s.ok()) {
- LOG(WARNING) << "[replication] Failed to clean all checkpoint files, error: "
- << s.ToString();
+ LOG(WARNING) << "[replication] Failed to clean all checkpoint files, error: " << s.ToString();
}
}
}
@@ -729,7 +687,7 @@ ReplicationThread::CBState ReplicationThread::fullSyncReadCB(bufferevent *bev,
}
Status ReplicationThread::parallelFetchFile(const std::string &dir,
- const std::vector<std::pair<std::string, uint32_t>> &files) {
+ const std::vector<std::pair<std::string, uint32_t>> &files) {
size_t concurrency = 1;
if (files.size() > 20) {
// Use 4 threads to download files in parallel
@@ -739,8 +697,8 @@ Status ReplicationThread::parallelFetchFile(const std::string &dir,
std::atomic<uint32_t> skip_cnt = {0};
std::vector<std::future<Status>> results;
for (size_t tid = 0; tid < concurrency; ++tid) {
- results.push_back(std::async(
- std::launch::async, [this, dir, &files, tid, concurrency, &fetch_cnt, &skip_cnt]() -> Status {
+ results.push_back(
+ std::async(std::launch::async, [this, dir, &files, tid, concurrency, &fetch_cnt, &skip_cnt]() -> Status {
if (this->stop_flag_) {
return Status(Status::NotOK, "replication thread was stopped");
}
@@ -767,23 +725,24 @@ Status ReplicationThread::parallelFetchFile(const std::string &dir,
skip_cnt.fetch_add(1);
uint32_t cur_skip_cnt = skip_cnt.load();
uint32_t cur_fetch_cnt = fetch_cnt.load();
- LOG(INFO) << "[skip] "<< f_name << " " << f_crc
- << ", skip count: " << cur_skip_cnt << ", fetch count: " << cur_fetch_cnt
- << ", progress: " << cur_skip_cnt+cur_fetch_cnt<< "/" << files.size();
+ LOG(INFO) << "[skip] " << f_name << " " << f_crc << ", skip count: " << cur_skip_cnt
+ << ", fetch count: " << cur_fetch_cnt << ", progress: " << cur_skip_cnt + cur_fetch_cnt << "/"
+ << files.size();
continue;
}
fetch_files.push_back(f_name);
crcs.push_back(f_crc);
}
unsigned files_count = files.size();
- fetch_file_callback fn = [&fetch_cnt, &skip_cnt, files_count]
- (const std::string fetch_file, const uint32_t fetch_crc) {
+ fetch_file_callback fn = [&fetch_cnt, &skip_cnt, files_count](const std::string fetch_file,
+ const uint32_t fetch_crc) {
fetch_cnt.fetch_add(1);
uint32_t cur_skip_cnt = skip_cnt.load();
uint32_t cur_fetch_cnt = fetch_cnt.load();
- LOG(INFO) << "[fetch] " << "Fetched " << fetch_file << ", crc32: " << fetch_crc
- << ", skip count: " << cur_skip_cnt << ", fetch count: " << cur_fetch_cnt
- << ", progress: " << cur_skip_cnt+cur_fetch_cnt << "/" << files_count;
+ LOG(INFO) << "[fetch] "
+ << "Fetched " << fetch_file << ", crc32: " << fetch_crc << ", skip count: " << cur_skip_cnt
+ << ", fetch count: " << cur_fetch_cnt << ", progress: " << cur_skip_cnt + cur_fetch_cnt << "/"
+ << files_count;
};
// For master using old version, it only supports to fetch a single file by one
// command, so we need to fetch all files by multiple command interactions.
@@ -816,10 +775,10 @@ Status ReplicationThread::sendAuth(int sock_fd) {
UniqueEvbuf evbuf;
const auto auth_command = Redis::MultiBulkString({"AUTH", auth});
auto s = Util::SockSend(sock_fd, auth_command);
- if (!s.IsOK()) return Status(Status::NotOK, "send auth command err:"+s.Msg());
+ if (!s.IsOK()) return Status(Status::NotOK, "send auth command err:" + s.Msg());
while (true) {
if (evbuffer_read(evbuf.get(), sock_fd, -1) <= 0) {
- return Status(Status::NotOK, std::string("read auth response err: ")+strerror(errno));
+ return Status(Status::NotOK, std::string("read auth response err: ") + strerror(errno));
}
UniqueEvbufReadln line(evbuf.get(), EVBUFFER_EOL_CRLF_STRICT);
if (!line) continue;
@@ -832,9 +791,8 @@ Status ReplicationThread::sendAuth(int sock_fd) {
return Status::OK();
}
-Status ReplicationThread::fetchFile(int sock_fd, evbuffer *evbuf,
- const std::string &dir, std::string file,
- uint32_t crc, fetch_file_callback fn) {
+Status ReplicationThread::fetchFile(int sock_fd, evbuffer *evbuf, const std::string &dir, std::string file,
+ uint32_t crc, fetch_file_callback fn) {
size_t file_size;
// Read file size line
@@ -842,7 +800,7 @@ Status ReplicationThread::fetchFile(int sock_fd, evbuffer *evbuf,
UniqueEvbufReadln line(evbuf, EVBUFFER_EOL_CRLF_STRICT);
if (!line) {
if (evbuffer_read(evbuf, sock_fd, -1) <= 0) {
- return Status(Status::NotOK, std::string("read size: ")+strerror(errno));
+ return Status(Status::NotOK, std::string("read size: ") + strerror(errno));
}
continue;
}
@@ -862,10 +820,10 @@ Status ReplicationThread::fetchFile(int sock_fd, evbuffer *evbuf,
size_t remain = file_size;
uint32_t tmp_crc = 0;
- char data[16*1024];
+ char data[16 * 1024];
while (remain != 0) {
if (evbuffer_get_length(evbuf) > 0) {
- auto data_len = evbuffer_remove(evbuf, data, remain > 16*1024 ? 16*1024 : remain);
+ auto data_len = evbuffer_remove(evbuf, data, remain > 16 * 1024 ? 16 * 1024 : remain);
if (data_len == 0) continue;
if (data_len < 0) {
return Status(Status::NotOK, "read sst file data error");
@@ -875,7 +833,7 @@ Status ReplicationThread::fetchFile(int sock_fd, evbuffer *evbuf,
remain -= data_len;
} else {
if (evbuffer_read(evbuf, sock_fd, -1) <= 0) {
- return Status(Status::NotOK, std::string("read sst file: ")+strerror(errno));
+ return Status(Status::NotOK, std::string("read sst file: ") + strerror(errno));
}
}
}
@@ -894,9 +852,8 @@ Status ReplicationThread::fetchFile(int sock_fd, evbuffer *evbuf,
return Status::OK();
}
-Status ReplicationThread::fetchFiles(int sock_fd, const std::string &dir,
- const std::vector<std::string> &files, const std::vector<uint32_t> &crcs,
- fetch_file_callback fn) {
+Status ReplicationThread::fetchFiles(int sock_fd, const std::string &dir, const std::vector<std::string> &files,
+ const std::vector<uint32_t> &crcs, fetch_file_callback fn) {
std::string files_str;
for (auto file : files) {
files_str += file;
@@ -906,7 +863,7 @@ Status ReplicationThread::fetchFiles(int sock_fd, const std::string &dir,
const auto fetch_command = Redis::MultiBulkString({"_fetch_file", files_str});
auto s = Util::SockSend(sock_fd, fetch_command);
- if (!s.IsOK()) return Status(Status::NotOK, "send fetch file command: "+s.Msg());
+ if (!s.IsOK()) return Status(Status::NotOK, "send fetch file command: " + s.Msg());
UniqueEvbuf evbuf;
for (unsigned i = 0; i < files.size(); i++) {
diff --git a/src/replication.h b/src/replication.h
index 3e60713..4ae484c 100644
--- a/src/replication.h
+++ b/src/replication.h
@@ -67,7 +67,7 @@ class FeedSlaveThread {
void Join();
bool IsStopped() { return stop_; }
Redis::Connection *GetConn() { return conn_.get(); }
- rocksdb::SequenceNumber GetCurrentReplSeq() { return next_repl_seq_ == 0 ? 0 : next_repl_seq_-1; }
+ rocksdb::SequenceNumber GetCurrentReplSeq() { return next_repl_seq_ == 0 ? 0 : next_repl_seq_ - 1; }
private:
uint64_t interval = 0;
@@ -78,7 +78,7 @@ class FeedSlaveThread {
std::thread t_;
std::unique_ptr<rocksdb::TransactionLogIterator> iter_ = nullptr;
const size_t kMaxDelayUpdates = 16;
- const size_t kMaxDelayBytes = 16 * 1024;
+ const size_t kMaxDelayBytes = 16 * 1024;
void loop();
void checkLivenessIfNeed();
@@ -86,10 +86,8 @@ class FeedSlaveThread {
class ReplicationThread {
public:
- explicit ReplicationThread(std::string host, uint32_t port,
- Server *srv);
- Status Start(std::function<void()> &&pre_fullsync_cb,
- std::function<void()> &&post_fullsync_cb);
+ explicit ReplicationThread(std::string host, uint32_t port, Server *srv);
+ Status Start(std::function<void()> &&pre_fullsync_cb, std::function<void()> &&post_fullsync_cb);
void Stop();
ReplState State() { return repl_state_; }
time_t LastIOTime() { return last_io_time_; }
@@ -118,12 +116,9 @@ class ReplicationThread {
void Start();
void Stop();
static void EvCallback(bufferevent *bev, void *ctx);
- static void ConnEventCB(bufferevent *bev, int16_t events,
- void *state_machine_ptr);
- static void SetReadCB(bufferevent *bev, bufferevent_data_cb cb,
- void *state_machine_ptr);
- static void SetWriteCB(bufferevent *bev, bufferevent_data_cb cb,
- void *state_machine_ptr);
+ static void ConnEventCB(bufferevent *bev, int16_t events, void *state_machine_ptr);
+ static void SetReadCB(bufferevent *bev, bufferevent_data_cb cb, void *state_machine_ptr);
+ static void SetWriteCB(bufferevent *bev, bufferevent_data_cb cb, void *state_machine_ptr);
private:
bufferevent *bev_ = nullptr;
@@ -131,12 +126,8 @@ class ReplicationThread {
CallbackList handlers_;
CallbackList::size_type handler_idx_ = 0;
- EventType getHandlerEventType(CallbackList::size_type idx) {
- return std::get<0>(handlers_[idx]);
- }
- std::string getHandlerName(CallbackList::size_type idx) {
- return std::get<1>(handlers_[idx]);
- }
+ EventType getHandlerEventType(CallbackList::size_type idx) { return std::get<0>(handlers_[idx]); }
+ std::string getHandlerName(CallbackList::size_type idx) { return std::get<1>(handlers_[idx]); }
std::function<State(bufferevent *, void *)> getHandlerFunc(CallbackList::size_type idx) {
return std::get<2>(handlers_[idx]);
}
@@ -193,14 +184,11 @@ class ReplicationThread {
// Synchronized-Blocking ops
Status sendAuth(int sock_fd);
- Status fetchFile(int sock_fd, evbuffer *evbuf, const std::string &dir,
- const std::string file, uint32_t crc, fetch_file_callback fn);
- Status fetchFiles(int sock_fd, const std::string &dir,
- const std::vector<std::string> &files,
- const std::vector<uint32_t> &crcs,
- fetch_file_callback fn);
- Status parallelFetchFile(const std::string &dir,
- const std::vector<std::pair<std::string, uint32_t>> &files);
+ Status fetchFile(int sock_fd, evbuffer *evbuf, const std::string &dir, const std::string file, uint32_t crc,
+ fetch_file_callback fn);
+ Status fetchFiles(int sock_fd, const std::string &dir, const std::vector<std::string> &files,
+ const std::vector<uint32_t> &crcs, fetch_file_callback fn);
+ Status parallelFetchFile(const std::string &dir, const std::vector<std::pair<std::string, uint32_t>> &files);
static bool isRestoringError(const char *err);
static bool isWrongPsyncNum(const char *err);
@@ -214,13 +202,12 @@ class ReplicationThread {
*/
class WriteBatchHandler : public rocksdb::WriteBatch::Handler {
public:
- rocksdb::Status PutCF(uint32_t column_family_id, const rocksdb::Slice &key,
- const rocksdb::Slice &value) override;
+ rocksdb::Status PutCF(uint32_t column_family_id, const rocksdb::Slice &key, const rocksdb::Slice &value) override;
rocksdb::Status DeleteCF(uint32_t column_family_id, const rocksdb::Slice &key) override {
return rocksdb::Status::OK();
}
- rocksdb::Status DeleteRangeCF(uint32_t column_family_id,
- const rocksdb::Slice& begin_key, const rocksdb::Slice& end_key) override {
+ rocksdb::Status DeleteRangeCF(uint32_t column_family_id, const rocksdb::Slice &begin_key,
+ const rocksdb::Slice &end_key) override {
return rocksdb::Status::OK();
}
WriteBatchType Type() { return type_; }
diff --git a/src/rocksdb_crc32c.h b/src/rocksdb_crc32c.h
index c4d3c7c..68342d6 100644
--- a/src/rocksdb_crc32c.h
+++ b/src/rocksdb_crc32c.h
@@ -32,6 +32,7 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
+
#include <string>
namespace rocksdb {
@@ -45,9 +46,7 @@ extern std::string IsFastCrc32Supported();
extern uint32_t Extend(uint32_t init_crc, const char* data, size_t n);
// Return the crc32c of data[0,n-1]
-inline uint32_t Value(const char* data, size_t n) {
- return Extend(0, data, n);
-}
+inline uint32_t Value(const char* data, size_t n) { return Extend(0, data, n); }
static const uint32_t kMaskDelta = 0xa282ead8ul;
diff --git a/src/rw_lock.h b/src/rw_lock.h
index a9edc94..b869580 100644
--- a/src/rw_lock.h
+++ b/src/rw_lock.h
@@ -24,8 +24,8 @@
// A implementation of RAII write-first read-write lock using C++11.
#pragma once
-#include <mutex>
#include <condition_variable>
+#include <mutex>
namespace RWLock {
@@ -37,9 +37,7 @@ class ReadWriteLock {
void LockWrite() {
std::unique_lock<std::mutex> guard(lock_);
++write_count_;
- write_condition_.wait(guard, [=] {
- return read_count_ == 0 && !is_writing_;
- });
+ write_condition_.wait(guard, [=] { return read_count_ == 0 && !is_writing_; });
is_writing_ = true;
}
@@ -47,24 +45,22 @@ class ReadWriteLock {
std::lock_guard<std::mutex> guard(lock_);
is_writing_ = false;
if (--write_count_ == 0) {
- read_condition_.notify_all();
+ read_condition_.notify_all();
} else {
- write_condition_.notify_one();
+ write_condition_.notify_one();
}
}
void LockRead() {
std::unique_lock<std::mutex> guard(lock_);
- read_condition_.wait(guard, [=] {
- return write_count_ == 0;
- });
+ read_condition_.wait(guard, [=] { return write_count_ == 0; });
++read_count_;
}
void UnLockRead() {
std::lock_guard<std::mutex> guard(lock_);
if (--read_count_ == 0 && write_count_ > 0) {
- write_condition_.notify_one();
+ write_condition_.notify_one();
}
}
@@ -79,13 +75,9 @@ class ReadWriteLock {
class WriteLock {
public:
- explicit WriteLock(ReadWriteLock& wlock) : wlock_(wlock) {
- wlock_.LockWrite();
- }
+ explicit WriteLock(ReadWriteLock& wlock) : wlock_(wlock) { wlock_.LockWrite(); }
- virtual ~WriteLock() {
- wlock_.UnLockWrite();
- }
+ virtual ~WriteLock() { wlock_.UnLockWrite(); }
private:
ReadWriteLock& wlock_;
@@ -93,13 +85,9 @@ class WriteLock {
class ReadLock {
public:
- explicit ReadLock(ReadWriteLock& rlock) : rlock_(rlock) {
- rlock_.LockRead();
- }
+ explicit ReadLock(ReadWriteLock& rlock) : rlock_(rlock) { rlock_.LockRead(); }
- virtual ~ReadLock() {
- rlock_.UnLockRead();
- }
+ virtual ~ReadLock() { rlock_.UnLockRead(); }
private:
ReadWriteLock& rlock_;
diff --git a/src/scope_exit.h b/src/scope_exit.h
index dfa0b7a..93e0125 100644
--- a/src/scope_exit.h
+++ b/src/scope_exit.h
@@ -35,13 +35,9 @@ struct ScopeExit {
if (enabled_) f_();
}
- void Enable() {
- enabled_ = false;
- }
+ void Enable() { enabled_ = false; }
- void Disable() {
- enabled_ = true;
- }
+ void Disable() { enabled_ = true; }
bool enabled_;
F f_;
diff --git a/src/scripting.cc b/src/scripting.cc
index 652f606..e09dcd1 100644
--- a/src/scripting.cc
+++ b/src/scripting.cc
@@ -53,19 +53,20 @@
#include "scripting.h"
#include <math.h>
+
#include <string>
-#include "util.h"
-#include "sha1.h"
-#include "server.h"
-#include "redis_connection.h"
-#include "redis_cmd.h"
#include "rand.h"
+#include "redis_cmd.h"
+#include "redis_connection.h"
+#include "server.h"
+#include "sha1.h"
+#include "util.h"
/* The maximum number of characters needed to represent a long double
* as a string (long double has a huge range).
* This should be the size of the buffer given to doule to string */
-#define MAX_LONG_DOUBLE_CHARS 5*1024
+#define MAX_LONG_DOUBLE_CHARS 5 * 1024
enum {
LL_DEBUG = 0,
@@ -75,478 +76,468 @@ enum {
};
namespace Lua {
- lua_State *CreateState(bool read_only) {
- lua_State *lua = lua_open();
- loadLibraries(lua);
- removeUnsupportedFunctions(lua);
- loadFuncs(lua, read_only);
- enableGlobalsProtection(lua);
- return lua;
- }
+lua_State *CreateState(bool read_only) {
+ lua_State *lua = lua_open();
+ loadLibraries(lua);
+ removeUnsupportedFunctions(lua);
+ loadFuncs(lua, read_only);
+ enableGlobalsProtection(lua);
+ return lua;
+}
- void DestroyState(lua_State *lua) {
- lua_gc(lua, LUA_GCCOLLECT, 0);
- lua_close(lua);
- }
+void DestroyState(lua_State *lua) {
+ lua_gc(lua, LUA_GCCOLLECT, 0);
+ lua_close(lua);
+}
- void loadFuncs(lua_State *lua, bool read_only) {
- lua_newtable(lua);
+void loadFuncs(lua_State *lua, bool read_only) {
+ lua_newtable(lua);
- /* redis.call */
- lua_pushstring(lua, "call");
- lua_pushcfunction(lua, redisCallCommand);
- lua_settable(lua, -3);
+ /* redis.call */
+ lua_pushstring(lua, "call");
+ lua_pushcfunction(lua, redisCallCommand);
+ lua_settable(lua, -3);
- /* redis.pcall */
- lua_pushstring(lua, "pcall");
- lua_pushcfunction(lua, redisPCallCommand);
- lua_settable(lua, -3);
+ /* redis.pcall */
+ lua_pushstring(lua, "pcall");
+ lua_pushcfunction(lua, redisPCallCommand);
+ lua_settable(lua, -3);
- /* redis.log and log levels. */
- lua_pushstring(lua, "log");
- lua_pushcfunction(lua, redisLogCommand);
- lua_settable(lua, -3);
+ /* redis.log and log levels. */
+ lua_pushstring(lua, "log");
+ lua_pushcfunction(lua, redisLogCommand);
+ lua_settable(lua, -3);
- lua_pushstring(lua, "LOG_DEBUG");
- lua_pushnumber(lua, LL_DEBUG);
- lua_settable(lua, -3);
+ lua_pushstring(lua, "LOG_DEBUG");
+ lua_pushnumber(lua, LL_DEBUG);
+ lua_settable(lua, -3);
- lua_pushstring(lua, "LOG_VERBOSE");
- lua_pushnumber(lua, LL_VERBOSE);
- lua_settable(lua, -3);
+ lua_pushstring(lua, "LOG_VERBOSE");
+ lua_pushnumber(lua, LL_VERBOSE);
+ lua_settable(lua, -3);
- lua_pushstring(lua, "LOG_NOTICE");
- lua_pushnumber(lua, LL_NOTICE);
- lua_settable(lua, -3);
+ lua_pushstring(lua, "LOG_NOTICE");
+ lua_pushnumber(lua, LL_NOTICE);
+ lua_settable(lua, -3);
- lua_pushstring(lua, "LOG_WARNING");
- lua_pushnumber(lua, LL_WARNING);
- lua_settable(lua, -3);
+ lua_pushstring(lua, "LOG_WARNING");
+ lua_pushnumber(lua, LL_WARNING);
+ lua_settable(lua, -3);
- /* redis.sha1hex */
- lua_pushstring(lua, "sha1hex");
- lua_pushcfunction(lua, redisSha1hexCommand);
- lua_settable(lua, -3);
+ /* redis.sha1hex */
+ lua_pushstring(lua, "sha1hex");
+ lua_pushcfunction(lua, redisSha1hexCommand);
+ lua_settable(lua, -3);
- /* redis.error_reply and redis.status_reply */
- lua_pushstring(lua, "error_reply");
- lua_pushcfunction(lua, redisErrorReplyCommand);
- lua_settable(lua, -3);
- lua_pushstring(lua, "status_reply");
- lua_pushcfunction(lua, redisStatusReplyCommand);
- lua_settable(lua, -3);
+ /* redis.error_reply and redis.status_reply */
+ lua_pushstring(lua, "error_reply");
+ lua_pushcfunction(lua, redisErrorReplyCommand);
+ lua_settable(lua, -3);
+ lua_pushstring(lua, "status_reply");
+ lua_pushcfunction(lua, redisStatusReplyCommand);
+ lua_settable(lua, -3);
- if (read_only) {
- /* redis.read_only */
- lua_pushstring(lua, "read_only");
- lua_pushboolean(lua, 1);
- lua_settable(lua, -3);
- }
+ if (read_only) {
+ /* redis.read_only */
+ lua_pushstring(lua, "read_only");
+ lua_pushboolean(lua, 1);
+ lua_settable(lua, -3);
+ }
- lua_setglobal(lua, "redis");
+ lua_setglobal(lua, "redis");
- /* Replace math.random and math.randomseed with our implementations. */
- lua_getglobal(lua, "math");
+ /* Replace math.random and math.randomseed with our implementations. */
+ lua_getglobal(lua, "math");
- lua_pushstring(lua, "random");
- lua_pushcfunction(lua, redisMathRandom);
- lua_settable(lua, -3);
+ lua_pushstring(lua, "random");
+ lua_pushcfunction(lua, redisMathRandom);
+ lua_settable(lua, -3);
- lua_pushstring(lua, "randomseed");
- lua_pushcfunction(lua, redisMathRandomSeed);
- lua_settable(lua, -3);
+ lua_pushstring(lua, "randomseed");
+ lua_pushcfunction(lua, redisMathRandomSeed);
+ lua_settable(lua, -3);
- lua_setglobal(lua, "math");
-
- /* Add a helper function we use for pcall error reporting.
- * Note that when the error is in the C function we want to report the
- * information about the caller, that's what makes sense from the point
- * of view of the user debugging a script. */
- {
- const char *err_func = "local dbg = debug\n"
- "function __redis__err__handler(err)\n"
- " local i = dbg.getinfo(2,'nSl')\n"
- " if i and i.what == 'C' then\n"
- " i = dbg.getinfo(3,'nSl')\n"
- " end\n"
- " if i then\n"
- " return i.source .. ':' .. i.currentline .. ': ' .. err\n"
- " else\n"
- " return err\n"
- " end\n"
- "end\n";
- luaL_loadbuffer(lua, err_func, strlen(err_func), "@err_handler_def");
- lua_pcall(lua, 0, 0, 0);
- }
- {
- const char *compare_func = "function __redis__compare_helper(a,b)\n"
- " if a == false then a = '' end\n"
- " if b == false then b = '' end\n"
- " return a<b\n"
- "end\n";
- luaL_loadbuffer(lua, compare_func, strlen(compare_func), "@cmp_func_def");
- lua_pcall(lua, 0, 0, 0);
- }
+ lua_setglobal(lua, "math");
+
+ /* Add a helper function we use for pcall error reporting.
+ * Note that when the error is in the C function we want to report the
+ * information about the caller, that's what makes sense from the point
+ * of view of the user debugging a script. */
+ {
+ const char *err_func =
+ "local dbg = debug\n"
+ "function __redis__err__handler(err)\n"
+ " local i = dbg.getinfo(2,'nSl')\n"
+ " if i and i.what == 'C' then\n"
+ " i = dbg.getinfo(3,'nSl')\n"
+ " end\n"
+ " if i then\n"
+ " return i.source .. ':' .. i.currentline .. ': ' .. err\n"
+ " else\n"
+ " return err\n"
+ " end\n"
+ "end\n";
+ luaL_loadbuffer(lua, err_func, strlen(err_func), "@err_handler_def");
+ lua_pcall(lua, 0, 0, 0);
}
+ {
+ const char *compare_func =
+ "function __redis__compare_helper(a,b)\n"
+ " if a == false then a = '' end\n"
+ " if b == false then b = '' end\n"
+ " return a<b\n"
+ "end\n";
+ luaL_loadbuffer(lua, compare_func, strlen(compare_func), "@cmp_func_def");
+ lua_pcall(lua, 0, 0, 0);
+ }
+}
+int redisLogCommand(lua_State *lua) {
+ int j, level, argc = lua_gettop(lua);
- int redisLogCommand(lua_State *lua) {
- int j, level, argc = lua_gettop(lua);
-
- if (argc < 2) {
- lua_pushstring(lua, "redis.log() requires two arguments or more.");
- return lua_error(lua);
- }
- if (!lua_isnumber(lua, -argc)) {
- lua_pushstring(lua, "First argument must be a number (log level).");
- return lua_error(lua);
- }
- level = lua_tonumber(lua, -argc);
- if (level < LL_DEBUG || level > LL_WARNING) {
- lua_pushstring(lua, "Invalid debug level.");
- return lua_error(lua);
- }
- if (level < GetServer()->GetConfig()->loglevel) {
- return 0;
- }
+ if (argc < 2) {
+ lua_pushstring(lua, "redis.log() requires two arguments or more.");
+ return lua_error(lua);
+ }
+ if (!lua_isnumber(lua, -argc)) {
+ lua_pushstring(lua, "First argument must be a number (log level).");
+ return lua_error(lua);
+ }
+ level = lua_tonumber(lua, -argc);
+ if (level < LL_DEBUG || level > LL_WARNING) {
+ lua_pushstring(lua, "Invalid debug level.");
+ return lua_error(lua);
+ }
+ if (level < GetServer()->GetConfig()->loglevel) {
+ return 0;
+ }
- std::string log_message;
- for (j = 1; j < argc; j++) {
- size_t len;
- const char *s;
- s = lua_tolstring(lua, (-argc)+j, &len);
- if (s) {
- if (j != 1) {
- log_message += " "+std::string(s, len);
- } else {
- log_message = std::string(s, len);
- }
+ std::string log_message;
+ for (j = 1; j < argc; j++) {
+ size_t len;
+ const char *s;
+ s = lua_tolstring(lua, (-argc) + j, &len);
+ if (s) {
+ if (j != 1) {
+ log_message += " " + std::string(s, len);
+ } else {
+ log_message = std::string(s, len);
}
}
+ }
- // The min log level was INFO, DEBUG would never take effect
- switch (level) {
- case LL_VERBOSE: // also regard VERBOSE as INFO here since no VERBOSE level
- case LL_NOTICE:
- LOG(INFO) << "[Lua] " << log_message;
- break;
- case LL_WARNING:
- LOG(WARNING) << "[Lua] " << log_message;
- break;
- }
- return 0;
+ // The min log level was INFO, DEBUG would never take effect
+ switch (level) {
+ case LL_VERBOSE: // also regard VERBOSE as INFO here since no VERBOSE level
+ case LL_NOTICE:
+ LOG(INFO) << "[Lua] " << log_message;
+ break;
+ case LL_WARNING:
+ LOG(WARNING) << "[Lua] " << log_message;
+ break;
}
+ return 0;
+}
- Status evalGenericCommand(Redis::Connection *conn,
- const std::vector<std::string> &args,
- bool evalsha,
- std::string *output,
- bool read_only) {
- int64_t numkeys = 0;
- char funcname[43];
- Server *srv = conn->GetServer();
- lua_State *lua = srv->Lua();
- if (read_only) {
- // Use the worker's private Lua VM when entering the read-only mode
- lua = conn->Owner()->Lua();
- }
+Status evalGenericCommand(Redis::Connection *conn, const std::vector<std::string> &args, bool evalsha,
+ std::string *output, bool read_only) {
+ int64_t numkeys = 0;
+ char funcname[43];
+ Server *srv = conn->GetServer();
+ lua_State *lua = srv->Lua();
+ if (read_only) {
+ // Use the worker's private Lua VM when entering the read-only mode
+ lua = conn->Owner()->Lua();
+ }
- auto s = Util::DecimalStringToNum(args[2], &numkeys);
- if (!s.IsOK()) {
- return s;
- }
- if (numkeys > int64_t(args.size()-3)) {
- return Status(Status::NotOK, "Number of keys can't be greater than number of args");
- } else if (numkeys < -1) {
- return Status(Status::NotOK, "Number of keys can't be negative");
- }
+ auto s = Util::DecimalStringToNum(args[2], &numkeys);
+ if (!s.IsOK()) {
+ return s;
+ }
+ if (numkeys > int64_t(args.size() - 3)) {
+ return Status(Status::NotOK, "Number of keys can't be greater than number of args");
+ } else if (numkeys < -1) {
+ return Status(Status::NotOK, "Number of keys can't be negative");
+ }
- /* We obtain the script SHA1, then check if this function is already
- * defined into the Lua state */
- funcname[0] = 'f';
- funcname[1] = '_';
- if (!evalsha) {
- SHA1Hex(funcname+2, args[1].c_str(), args[1].size());
- } else {
- for (int j = 0; j < 40; j++) {
- std::string sha = args[1];
- funcname[j+2] = (sha[j] >= 'A' && sha[j] <= 'Z') ? sha[j]+('a'-'A') : sha[j];
- }
- funcname[42] = '\0';
+ /* We obtain the script SHA1, then check if this function is already
+ * defined into the Lua state */
+ funcname[0] = 'f';
+ funcname[1] = '_';
+ if (!evalsha) {
+ SHA1Hex(funcname + 2, args[1].c_str(), args[1].size());
+ } else {
+ for (int j = 0; j < 40; j++) {
+ std::string sha = args[1];
+ funcname[j + 2] = (sha[j] >= 'A' && sha[j] <= 'Z') ? sha[j] + ('a' - 'A') : sha[j];
}
+ funcname[42] = '\0';
+ }
- /* Push the pcall error handler function on the stack. */
- lua_getglobal(lua, "__redis__err__handler");
+ /* Push the pcall error handler function on the stack. */
+ lua_getglobal(lua, "__redis__err__handler");
- /* Try to lookup the Lua function */
- lua_getglobal(lua, funcname);
- if (lua_isnil(lua, -1)) {
- lua_pop(lua, 1); /* remove the nil from the stack */
- std::string body;
- if (evalsha) {
- auto s = srv->ScriptGet(funcname+2, &body);
- if (!s.IsOK()) {
- lua_pop(lua, 1); /* remove the error handler from the stack. */
- return Status(Status::NotOK, "NOSCRIPT No matching script. Please use EVAL");
- }
- } else {
- body = args[1];
- }
- std::string sha;
- s = createFunction(srv, body, &sha, lua);
+ /* Try to lookup the Lua function */
+ lua_getglobal(lua, funcname);
+ if (lua_isnil(lua, -1)) {
+ lua_pop(lua, 1); /* remove the nil from the stack */
+ std::string body;
+ if (evalsha) {
+ auto s = srv->ScriptGet(funcname + 2, &body);
if (!s.IsOK()) {
lua_pop(lua, 1); /* remove the error handler from the stack. */
- return s;
+ return Status(Status::NotOK, "NOSCRIPT No matching script. Please use EVAL");
}
- /* Now the following is guaranteed to return non nil */
- lua_getglobal(lua, funcname);
- }
-
- /* Populate the argv and keys table accordingly to the arguments that
- * EVAL received. */
- setGlobalArray(lua, "KEYS", std::vector<std::string>(args.begin()+3, args.begin()+3+numkeys));
- setGlobalArray(lua, "ARGV", std::vector<std::string>(args.begin()+3+numkeys, args.end()));
- int err = lua_pcall(lua, 0, 1, -2);
- if (err) {
- std::string msg = std::string("ERR running script (call to ") + funcname + "): "+ lua_tostring(lua, -1);
- *output = Redis::Error(msg);
- lua_pop(lua, 2);
} else {
- *output = replyToRedisReply(lua);
- lua_pop(lua, 1);
+ body = args[1];
}
-
- /* Call the Lua garbage collector from time to time to avoid a
- * full cycle performed by Lua, which adds too latency.
- *
- * The call is performed every LUA_GC_CYCLE_PERIOD executed commands
- * (and for LUA_GC_CYCLE_PERIOD collection steps) because calling it
- * for every command uses too much CPU. */
- #define LUA_GC_CYCLE_PERIOD 50
- {
- static int64_t gc_count = 0;
-
- gc_count++;
- if (gc_count == LUA_GC_CYCLE_PERIOD) {
- lua_gc(lua, LUA_GCSTEP, LUA_GC_CYCLE_PERIOD);
- gc_count = 0;
- }
+ std::string sha;
+ s = createFunction(srv, body, &sha, lua);
+ if (!s.IsOK()) {
+ lua_pop(lua, 1); /* remove the error handler from the stack. */
+ return s;
}
- return Status::OK();
+ /* Now the following is guaranteed to return non nil */
+ lua_getglobal(lua, funcname);
}
- int redisCallCommand(lua_State *lua) {
- return redisGenericCommand(lua, 1);
+ /* Populate the argv and keys table accordingly to the arguments that
+ * EVAL received. */
+ setGlobalArray(lua, "KEYS", std::vector<std::string>(args.begin() + 3, args.begin() + 3 + numkeys));
+ setGlobalArray(lua, "ARGV", std::vector<std::string>(args.begin() + 3 + numkeys, args.end()));
+ int err = lua_pcall(lua, 0, 1, -2);
+ if (err) {
+ std::string msg = std::string("ERR running script (call to ") + funcname + "): " + lua_tostring(lua, -1);
+ *output = Redis::Error(msg);
+ lua_pop(lua, 2);
+ } else {
+ *output = replyToRedisReply(lua);
+ lua_pop(lua, 1);
}
- int redisPCallCommand(lua_State *lua) {
- return redisGenericCommand(lua, 0);
+/* Call the Lua garbage collector from time to time to avoid a
+ * full cycle performed by Lua, which adds too latency.
+ *
+ * The call is performed every LUA_GC_CYCLE_PERIOD executed commands
+ * (and for LUA_GC_CYCLE_PERIOD collection steps) because calling it
+ * for every command uses too much CPU. */
+#define LUA_GC_CYCLE_PERIOD 50
+ {
+ static int64_t gc_count = 0;
+
+ gc_count++;
+ if (gc_count == LUA_GC_CYCLE_PERIOD) {
+ lua_gc(lua, LUA_GCSTEP, LUA_GC_CYCLE_PERIOD);
+ gc_count = 0;
+ }
}
- int redisGenericCommand(lua_State *lua, int raise_error) {
- int j, argc = lua_gettop(lua);
- std::vector<std::string> args;
- lua_getglobal(lua, "redis");
- lua_getfield(lua, -1, "read_only");
- int read_only = lua_toboolean(lua, -1);
- lua_pop(lua, 2);
+ return Status::OK();
+}
- if (argc == 0) {
- pushError(lua, "Please specify at least one argument for redis.call()");
- return raise_error ? raiseError(lua) : 1;
- }
- for (j = 0; j < argc; j++) {
- char dbuf[64];
- if (lua_type(lua, j+1) == LUA_TNUMBER) {
- lua_Number num = lua_tonumber(lua, j+1);
- snprintf(dbuf, sizeof(dbuf), "%.17g", static_cast<double>(num));
- args.emplace_back(dbuf);
- } else {
- const char *obj_s;
- size_t obj_len;
- obj_s = lua_tolstring(lua, j+1, &obj_len);
- if (obj_s == nullptr) break; /* no a string */
- args.emplace_back(std::string(obj_s, obj_len));
- }
- }
- if (j != argc) {
- pushError(lua, "Lua redis() command arguments must be strings or integers");
- return raise_error ? raiseError(lua) : 1;
- }
+int redisCallCommand(lua_State *lua) { return redisGenericCommand(lua, 1); }
- auto commands = Redis::GetCommands();
- auto cmd_iter = commands->find(Util::ToLower(args[0]));
- if (cmd_iter == commands->end()) {
- pushError(lua, "Unknown Redis command called from Lua script");
- return raise_error ? raiseError(lua) : 1;
- }
- auto redisCmd = cmd_iter->second;
- if (read_only && redisCmd->is_write()) {
- pushError(lua, "Write commands are not allowed from read-only scripts");
- return raise_error ? raiseError(lua) : 1;
- }
- auto cmd = redisCmd->factory();
- cmd->SetAttributes(redisCmd);
- cmd->SetArgs(args);
- int arity = cmd->GetAttributes()->arity;
- if (((arity > 0 && argc != arity) || (arity < 0 && argc < -arity))) {
- pushError(lua, "Wrong number of args calling Redis command From Lua script");
- return raise_error ? raiseError(lua) : 1;
- }
- auto attributes = cmd->GetAttributes();
- if (attributes->flags & Redis::kCmdNoScript) {
- pushError(lua, "This Redis command is not allowed from scripts");
- return raise_error ? raiseError(lua) : 1;
- }
+int redisPCallCommand(lua_State *lua) { return redisGenericCommand(lua, 0); }
+int redisGenericCommand(lua_State *lua, int raise_error) {
+ int j, argc = lua_gettop(lua);
+ std::vector<std::string> args;
+ lua_getglobal(lua, "redis");
+ lua_getfield(lua, -1, "read_only");
+ int read_only = lua_toboolean(lua, -1);
+ lua_pop(lua, 2);
- std::string output, cmd_name = Util::ToLower(args[0]);
- Server *srv = GetServer();
- Config *config = srv->GetConfig();
- Redis::Connection *conn = srv->GetCurrentConnection();
- if (config->cluster_enabled) {
- auto s = srv->cluster_->CanExecByMySelf(attributes, args, conn);
- if (!s.IsOK()) {
- pushError(lua, s.Msg().c_str());
- return raise_error ? raiseError(lua) : 1;
- }
- }
- if (config->slave_readonly && srv->IsSlave() && attributes->is_write()) {
- pushError(lua, "READONLY You can't write against a read only slave.");
- return raise_error ? raiseError(lua) : 1;
- }
- if (!config->slave_serve_stale_data && srv->IsSlave()
- && cmd_name != "info" && cmd_name != "slaveof"
- && srv->GetReplicationState() != kReplConnected) {
- pushError(lua, "MASTERDOWN Link with MASTER is down "
- "and slave-serve-stale-data is set to 'no'.");
- return raise_error ? raiseError(lua) : 1;
- }
- auto s = cmd->Parse(args);
- if (!s.IsOK()) {
- pushError(lua, s.Msg().data());
- return raise_error ? raiseError(lua) : 1;
+ if (argc == 0) {
+ pushError(lua, "Please specify at least one argument for redis.call()");
+ return raise_error ? raiseError(lua) : 1;
+ }
+ for (j = 0; j < argc; j++) {
+ char dbuf[64];
+ if (lua_type(lua, j + 1) == LUA_TNUMBER) {
+ lua_Number num = lua_tonumber(lua, j + 1);
+ snprintf(dbuf, sizeof(dbuf), "%.17g", static_cast<double>(num));
+ args.emplace_back(dbuf);
+ } else {
+ const char *obj_s;
+ size_t obj_len;
+ obj_s = lua_tolstring(lua, j + 1, &obj_len);
+ if (obj_s == nullptr) break; /* no a string */
+ args.emplace_back(std::string(obj_s, obj_len));
}
- srv->stats_.IncrCalls(cmd_name);
- auto start = std::chrono::high_resolution_clock::now();
- bool is_profiling = conn->isProfilingEnabled(cmd_name);
- auto end = std::chrono::high_resolution_clock::now();
- s = cmd->Execute(GetServer(), srv->GetCurrentConnection(), &output);
- uint64_t duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
- if (is_profiling) conn->recordProfilingSampleIfNeed(cmd_name, duration);
- srv->SlowlogPushEntryIfNeeded(&args, duration);
- srv->stats_.IncrLatency(static_cast<uint64_t>(duration), cmd_name);
- srv->FeedMonitorConns(conn, args);
+ }
+ if (j != argc) {
+ pushError(lua, "Lua redis() command arguments must be strings or integers");
+ return raise_error ? raiseError(lua) : 1;
+ }
+
+ auto commands = Redis::GetCommands();
+ auto cmd_iter = commands->find(Util::ToLower(args[0]));
+ if (cmd_iter == commands->end()) {
+ pushError(lua, "Unknown Redis command called from Lua script");
+ return raise_error ? raiseError(lua) : 1;
+ }
+ auto redisCmd = cmd_iter->second;
+ if (read_only && redisCmd->is_write()) {
+ pushError(lua, "Write commands are not allowed from read-only scripts");
+ return raise_error ? raiseError(lua) : 1;
+ }
+ auto cmd = redisCmd->factory();
+ cmd->SetAttributes(redisCmd);
+ cmd->SetArgs(args);
+ int arity = cmd->GetAttributes()->arity;
+ if (((arity > 0 && argc != arity) || (arity < 0 && argc < -arity))) {
+ pushError(lua, "Wrong number of args calling Redis command From Lua script");
+ return raise_error ? raiseError(lua) : 1;
+ }
+ auto attributes = cmd->GetAttributes();
+ if (attributes->flags & Redis::kCmdNoScript) {
+ pushError(lua, "This Redis command is not allowed from scripts");
+ return raise_error ? raiseError(lua) : 1;
+ }
+
+ std::string output, cmd_name = Util::ToLower(args[0]);
+ Server *srv = GetServer();
+ Config *config = srv->GetConfig();
+ Redis::Connection *conn = srv->GetCurrentConnection();
+ if (config->cluster_enabled) {
+ auto s = srv->cluster_->CanExecByMySelf(attributes, args, conn);
if (!s.IsOK()) {
- pushError(lua, s.Msg().data());
+ pushError(lua, s.Msg().c_str());
return raise_error ? raiseError(lua) : 1;
}
- redisProtocolToLuaType(lua, output.data());
- return 1;
}
-
- void removeUnsupportedFunctions(lua_State *lua) {
- lua_pushnil(lua);
- lua_setglobal(lua, "loadfile");
- lua_pushnil(lua);
- lua_setglobal(lua, "dofile");
- }
-
- void enableGlobalsProtection(lua_State *lua) {
- const char *s[32];
- int j = 0;
-
- s[j++] = "local dbg=debug\n";
- s[j++] = "local mt = {}\n";
- s[j++] = "setmetatable(_G, mt)\n";
- s[j++] = "mt.__newindex = function (t, n, v)\n";
- s[j++] = " if dbg.getinfo(2) then\n";
- s[j++] = " local w = dbg.getinfo(2, \"S\").what\n";
- s[j++] = " if w ~= \"main\" and w ~= \"C\" then\n";
- s[j++] = " error(\"Script attempted to create global variable '\"..tostring(n)..\"'\", 2)\n";
- s[j++] = " end\n";
- s[j++] = " end\n";
- s[j++] = " rawset(t, n, v)\n";
- s[j++] = "end\n";
- s[j++] = "mt.__index = function (t, n)\n";
- s[j++] = " if dbg.getinfo(2) and dbg.getinfo(2, \"S\").what ~= \"C\" then\n";
- s[j++] = " error(\"Script attempted to access nonexistent global variable '\"..tostring(n)..\"'\", 2)\n";
- s[j++] = " end\n";
- s[j++] = " return rawget(t, n)\n";
- s[j++] = "end\n";
- s[j++] = "debug = nil\n";
- s[j++] = nullptr;
-
- std::string code;
- for (j = 0; s[j] != nullptr; j++) code += s[j];
- luaL_loadbuffer(lua, code.c_str(), code.size(), "@enable_strict_lua");
- lua_pcall(lua, 0, 0, 0);
+ if (config->slave_readonly && srv->IsSlave() && attributes->is_write()) {
+ pushError(lua, "READONLY You can't write against a read only slave.");
+ return raise_error ? raiseError(lua) : 1;
}
+ if (!config->slave_serve_stale_data && srv->IsSlave() && cmd_name != "info" && cmd_name != "slaveof" &&
+ srv->GetReplicationState() != kReplConnected) {
+ pushError(lua,
+ "MASTERDOWN Link with MASTER is down "
+ "and slave-serve-stale-data is set to 'no'.");
+ return raise_error ? raiseError(lua) : 1;
+ }
+ auto s = cmd->Parse(args);
+ if (!s.IsOK()) {
+ pushError(lua, s.Msg().data());
+ return raise_error ? raiseError(lua) : 1;
+ }
+ srv->stats_.IncrCalls(cmd_name);
+ auto start = std::chrono::high_resolution_clock::now();
+ bool is_profiling = conn->isProfilingEnabled(cmd_name);
+ auto end = std::chrono::high_resolution_clock::now();
+ s = cmd->Execute(GetServer(), srv->GetCurrentConnection(), &output);
+ uint64_t duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
+ if (is_profiling) conn->recordProfilingSampleIfNeed(cmd_name, duration);
+ srv->SlowlogPushEntryIfNeeded(&args, duration);
+ srv->stats_.IncrLatency(static_cast<uint64_t>(duration), cmd_name);
+ srv->FeedMonitorConns(conn, args);
+ if (!s.IsOK()) {
+ pushError(lua, s.Msg().data());
+ return raise_error ? raiseError(lua) : 1;
+ }
+ redisProtocolToLuaType(lua, output.data());
+ return 1;
+}
- void loadLibraries(lua_State *lua) {
- auto loadLib = [] (lua_State *lua, const char *libname, lua_CFunction func) {
- lua_pushcfunction(lua, func);
- lua_pushstring(lua, libname);
- lua_call(lua, 1, 0);
- };
- loadLib(lua, "", luaopen_base);
- loadLib(lua, LUA_TABLIBNAME, luaopen_table);
- loadLib(lua, LUA_STRLIBNAME, luaopen_string);
- loadLib(lua, LUA_MATHLIBNAME, luaopen_math);
- loadLib(lua, LUA_DBLIBNAME, luaopen_debug);
- loadLib(lua, "cjson", luaopen_cjson);
- loadLib(lua, "struct", luaopen_struct);
- loadLib(lua, "cmsgpack", luaopen_cmsgpack);
- loadLib(lua, "bit", luaopen_bit);
- }
-
- /* Returns a table with a single field 'field' set to the string value
- * passed as argument. This helper function is handy when returning
- * a Redis Protocol error or status reply from Lua:
- *
- * return redis.error_reply("ERR Some Error")
- * return redis.status_reply("ERR Some Error")
... 6542 lines suppressed ...