Skip to content

Commit af8d225

Browse files
committed
Minor enhance ts code style
1 parent ce1ca5f commit af8d225

File tree

5 files changed

+39
-44
lines changed

5 files changed

+39
-44
lines changed

src/commands/cmd_timeseries.cc

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ class CommandTSCreateBase : public KeywordCommandBase {
308308

309309
class CommandTSCreate : public CommandTSCreateBase {
310310
public:
311-
CommandTSCreate() { registerDefaultHandlers(); }
311+
CommandTSCreate() { CommandTSCreateBase::registerDefaultHandlers(); }
312312
Status Parse(const std::vector<std::string> &args) override {
313313
if (args.size() < 2) {
314314
return {Status::RedisParseErr, errWrongNumOfArguments};
@@ -379,7 +379,7 @@ class CommandTSInfo : public Commander {
379379

380380
class CommandTSAdd : public CommandTSCreateBase {
381381
public:
382-
CommandTSAdd() { registerDefaultHandlers(); }
382+
CommandTSAdd() { CommandTSAdd::registerDefaultHandlers(); }
383383
Status Parse(const std::vector<std::string> &args) override {
384384
if (args.size() < 4) {
385385
return {Status::RedisParseErr, errWrongNumOfArguments};
@@ -577,30 +577,24 @@ class CommandTSRangeBase : virtual public CommandTSAggregatorBase {
577577
Status Parse(const std::vector<std::string> &args) override {
578578
TSOptionsParser parser(std::next(args.begin(), static_cast<std::ptrdiff_t>(skip_num_)), args.end());
579579
// Parse start timestamp
580-
auto start_ts = parser.TakeInt<uint64_t>();
581-
if (!start_ts.IsOK()) {
582-
auto start_ts_str = parser.TakeStr();
583-
if (!start_ts_str.IsOK() || start_ts_str.GetValue() != "-") {
584-
return {Status::RedisParseErr, "wrong fromTimestamp"};
585-
}
586-
// "-" means use default start timestamp: 0
587-
} else {
588-
is_start_explicit_set_ = true;
589-
option_.start_ts = start_ts.GetValue();
590-
}
591580

592-
// Parse end timestamp
593-
auto end_ts = parser.TakeInt<uint64_t>();
594-
if (!end_ts.IsOK()) {
595-
auto end_ts_str = parser.TakeStr();
596-
if (!end_ts_str.IsOK() || end_ts_str.GetValue() != "+") {
597-
return {Status::RedisParseErr, "wrong toTimestamp"};
581+
auto parse_ts = [](TSOptionsParser &parser, bool *is_set_flag, uint64_t *start_ts,
582+
std::string_view error_msg) -> Status {
583+
auto ts = parser.TakeInt<uint64_t>();
584+
if (!ts.IsOK()) {
585+
auto ts_str = parser.TakeStr();
586+
if (!ts_str.IsOK() || ts_str.GetValue() != "-") {
587+
return {Status::RedisParseErr, std::string(error_msg)};
588+
}
589+
// "-" means use default start timestamp: 0
590+
} else {
591+
*is_set_flag = true;
592+
*start_ts = ts.GetValue();
598593
}
599-
// "+" means use default end timestamp: MAX_TIMESTAMP
600-
} else {
601-
is_end_explicit_set_ = true;
602-
option_.end_ts = end_ts.GetValue();
603-
}
594+
return Status::OK();
595+
};
596+
GET_OR_RET(parse_ts(parser, &is_start_explicit_set_, &option_.start_ts, "wrong fromTimestamp"));
597+
GET_OR_RET(parse_ts(parser, &is_end_explicit_set_, &option_.end_ts, "wrong toTimestamp"));
604598
KeywordCommandBase::setSkipNum(skip_num_ + 2);
605599
auto s = KeywordCommandBase::Parse(args);
606600
if (!s.IsOK()) return s;
@@ -727,7 +721,7 @@ class CommandTSRangeBase : virtual public CommandTSAggregatorBase {
727721

728722
class CommandTSRange : public CommandTSRangeBase {
729723
public:
730-
CommandTSRange() : CommandTSRangeBase(2) { registerDefaultHandlers(); }
724+
CommandTSRange() : CommandTSRangeBase(2) { CommandTSRangeBase::registerDefaultHandlers(); }
731725
Status Parse(const std::vector<std::string> &args) override {
732726
if (args.size() < 4) {
733727
return {Status::RedisParseErr, "wrong number of arguments for 'ts.range' command"};
@@ -753,7 +747,7 @@ class CommandTSRange : public CommandTSRangeBase {
753747

754748
class CommandTSCreateRule : public CommandTSAggregatorBase {
755749
public:
756-
explicit CommandTSCreateRule() { registerDefaultHandlers(); }
750+
explicit CommandTSCreateRule() { CommandTSAggregatorBase::registerDefaultHandlers(); }
757751
Status Parse(const std::vector<std::string> &args) override {
758752
if (args.size() < 6) {
759753
return {Status::NotOK, "wrong number of arguments for 'TS.CREATERULE' command"};
@@ -780,7 +774,7 @@ class CommandTSCreateRule : public CommandTSAggregatorBase {
780774

781775
class CommandTSGet : public CommandTSAggregatorBase {
782776
public:
783-
CommandTSGet() { registerDefaultHandlers(); }
777+
CommandTSGet() { CommandTSGet::registerDefaultHandlers(); }
784778
Status Parse(const std::vector<std::string> &args) override {
785779
if (args.size() < 2) {
786780
return {Status::RedisParseErr, "wrong number of arguments for 'ts.get' command"};
@@ -838,6 +832,7 @@ class CommandTSMGetBase : virtual public CommandTSAggregatorBase {
838832
}
839833
return Status::OK();
840834
}
835+
841836
Status handleFilterExpr(TSOptionsParser &parser, TSMGetOption::FilterOption &filter_option) {
842837
auto filter_parser = TSMQueryFilterParser(filter_option);
843838
while (parser.Good()) {
@@ -857,7 +852,7 @@ class CommandTSMGetBase : virtual public CommandTSAggregatorBase {
857852

858853
class CommandTSMGet : public CommandTSMGetBase {
859854
public:
860-
CommandTSMGet() { registerDefaultHandlers(); }
855+
CommandTSMGet() { CommandTSMGet::registerDefaultHandlers(); }
861856
Status Parse(const std::vector<std::string> &args) override {
862857
if (args.size() < 3) {
863858
return {Status::RedisParseErr, "wrong number of arguments for 'ts.mget' command"};
@@ -902,7 +897,7 @@ class CommandTSMGet : public CommandTSMGetBase {
902897

903898
class CommandTSMRange : public CommandTSRangeBase, public CommandTSMGetBase {
904899
public:
905-
CommandTSMRange() : CommandTSRangeBase(1) { registerDefaultHandlers(); }
900+
CommandTSMRange() : CommandTSRangeBase(1) { CommandTSMRange::registerDefaultHandlers(); }
906901
Status Parse(const std::vector<std::string> &args) override {
907902
if (args.size() < 5) {
908903
return {Status::RedisParseErr, errTSMRangeArgsNum};
@@ -1001,7 +996,7 @@ class CommandTSMRange : public CommandTSRangeBase, public CommandTSMGetBase {
1001996

1002997
class CommandTSIncrByDecrBy : public CommandTSCreateBase {
1003998
public:
1004-
CommandTSIncrByDecrBy() { registerDefaultHandlers(); }
999+
CommandTSIncrByDecrBy() { CommandTSIncrByDecrBy::registerDefaultHandlers(); }
10051000
Status Parse(const std::vector<std::string> &args) override {
10061001
CommandParser parser(args, 2);
10071002
auto value_parse = parser.TakeFloat<double>();

src/types/redis_timeseries.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ std::vector<TSSample> GroupSamplesAndReduce(const std::vector<std::vector<TSSamp
211211

212212
while (!min_heap.empty()) {
213213
// Get the top element from the min-heap
214-
SamplePtr top = min_heap.top();
214+
SamplePtr top = std::move(min_heap.top());
215215
min_heap.pop();
216216

217217
// Check if the timestamp is the same as the current group
@@ -843,15 +843,15 @@ rocksdb::Status TimeSeries::upsertCommon(engine::Context &ctx, const Slice &ns_k
843843
rocksdb::Status TimeSeries::upsertCommonInBatch(engine::Context &ctx, const Slice &ns_key, TimeSeriesMetadata &metadata,
844844
SampleBatch &sample_batch,
845845
ObserverOrUniquePtr<rocksdb::WriteBatchBase> &batch,
846-
std::vector<std::string> *new_chunks) {
846+
std::vector<std::string> *new_chunks) const {
847847
auto all_batch_slice = sample_batch.AsSlice();
848848

849849
if (all_batch_slice.GetSampleSpan().empty() && new_chunks != nullptr) {
850850
new_chunks->clear();
851851
return rocksdb::Status::OK();
852852
}
853853

854-
// In the emun `TSSubkeyType`, `LABEL` is the next of `CHUNK`
854+
// In the enum `TSSubkeyType`, `LABEL` is the next of `CHUNK`
855855
std::string chunk_upper_bound = internalKeyFromLabelKey(ns_key, metadata, "");
856856
std::string end_key = internalKeyFromChunkID(ns_key, metadata, TSSample::MAX_TIMESTAMP);
857857
std::string prefix = end_key.substr(0, end_key.size() - sizeof(uint64_t));
@@ -980,7 +980,7 @@ rocksdb::Status TimeSeries::rangeCommon(engine::Context &ctx, const Slice &ns_ke
980980
return rocksdb::Status::OK();
981981
}
982982

983-
// In the emun `TSSubkeyType`, `LABEL` is the next of `CHUNK`
983+
// In the enum `TSSubkeyType`, `LABEL` is the next of `CHUNK`
984984
std::string chunk_upper_bound = internalKeyFromLabelKey(ns_key, metadata, "");
985985
std::string end_key = internalKeyFromChunkID(ns_key, metadata, TSSample::MAX_TIMESTAMP);
986986
std::string prefix = end_key.substr(0, end_key.size() - sizeof(uint64_t));
@@ -1293,7 +1293,7 @@ rocksdb::Status TimeSeries::upsertDownStream(engine::Context &ctx, const Slice &
12931293

12941294
rocksdb::Status TimeSeries::getCommon(engine::Context &ctx, const Slice &ns_key, const TimeSeriesMetadata &metadata,
12951295
bool is_return_latest, std::vector<TSSample> *res) {
1296-
// In the emun `TSSubkeyType`, `LABEL` is the next of `CHUNK`
1296+
// In the enum `TSSubkeyType`, `LABEL` is the next of `CHUNK`
12971297
std::string chunk_upper_bound = internalKeyFromLabelKey(ns_key, metadata, "");
12981298
std::string end_key = internalKeyFromChunkID(ns_key, metadata, TSSample::MAX_TIMESTAMP);
12991299
std::string prefix = end_key.substr(0, end_key.size() - sizeof(uint64_t));
@@ -1335,7 +1335,7 @@ rocksdb::Status TimeSeries::delRangeCommonInBatch(engine::Context &ctx, const Sl
13351335
if (from > to || (from == to && !inclusive_to)) {
13361336
return rocksdb::Status::OK();
13371337
}
1338-
// In the emun `TSSubkeyType`, `LABEL` is the next of `CHUNK`
1338+
// In the enum `TSSubkeyType`, `LABEL` is the next of `CHUNK`
13391339
std::string start_key = internalKeyFromChunkID(ns_key, metadata, from);
13401340
std::string prefix = start_key.substr(0, start_key.size() - sizeof(uint64_t));
13411341
std::string end_key;

src/types/redis_timeseries.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ struct TSRangeOption {
168168
std::set<uint64_t> filter_by_ts;
169169
std::optional<std::pair<double, double>> filter_by_value;
170170

171-
// Used for comapction
171+
// Used for compaction
172172
TSAggregator aggregator;
173173
bool is_return_latest = false;
174174
bool is_return_empty = false;
@@ -287,7 +287,7 @@ class TimeSeries : public SubKeyScanner {
287287
SampleBatch &sample_batch, std::vector<std::string> *new_chunks = nullptr);
288288
rocksdb::Status upsertCommonInBatch(engine::Context &ctx, const Slice &ns_key, TimeSeriesMetadata &metadata,
289289
SampleBatch &sample_batch, ObserverOrUniquePtr<rocksdb::WriteBatchBase> &batch,
290-
std::vector<std::string> *new_chunks = nullptr);
290+
std::vector<std::string> *new_chunks = nullptr) const;
291291
rocksdb::Status rangeCommon(engine::Context &ctx, const Slice &ns_key, const TimeSeriesMetadata &metadata,
292292
const TSRangeOption &option, std::vector<TSSample> *res, bool apply_retention = true);
293293
rocksdb::Status upsertDownStream(engine::Context &ctx, const Slice &ns_key, const TimeSeriesMetadata &metadata,

src/types/timeseries.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void TSChunk::SampleBatch::sortAndOrganize() {
9696
}
9797
}
9898

99-
SampleBatchSlice TSChunk::SampleBatchSlice::SliceByCount(uint64_t first, int count, uint64_t* last_ts) {
99+
SampleBatchSlice TSChunk::SampleBatchSlice::SliceByCount(uint64_t first, int count, uint64_t* last_ts) const {
100100
if (sample_span_.empty()) {
101101
return {};
102102
}
@@ -129,7 +129,7 @@ SampleBatchSlice TSChunk::SampleBatchSlice::SliceByCount(uint64_t first, int cou
129129
return createSampleSlice(start_idx, end_idx);
130130
}
131131

132-
SampleBatchSlice TSChunk::SampleBatchSlice::SliceByTimestamps(uint64_t first, uint64_t last, bool contain_last) {
132+
SampleBatchSlice TSChunk::SampleBatchSlice::SliceByTimestamps(uint64_t first, uint64_t last, bool contain_last) const {
133133
if (sample_span_.empty()) {
134134
return {};
135135
}
@@ -158,7 +158,7 @@ SampleBatchSlice TSChunk::SampleBatchSlice::createSampleSlice(size_t start_idx,
158158
nonstd::span<AddResult>(&add_result_span_[start_idx], end_idx - start_idx), policy_};
159159
}
160160

161-
SampleBatchSlice TSChunk::SampleBatch::AsSlice() { return {samples_, add_results_, policy_}; }
161+
SampleBatchSlice TSChunk::SampleBatch::AsSlice() const { return {samples_, add_results_, policy_}; }
162162

163163
std::vector<TSChunk::AddResult> TSChunk::SampleBatch::GetFinalResults() const {
164164
std::vector<AddResult> res;

src/types/timeseries.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ class TSChunk {
9696

9797
// Slice samples by count. Returns count valid samples starting from first timestamp
9898
// e.g., samples: {100,200,300}, first=200, count=2 -> {200,300}
99-
SampleBatchSlice SliceByCount(uint64_t first, int count, uint64_t* last_ts = nullptr);
99+
SampleBatchSlice SliceByCount(uint64_t first, int count, uint64_t* last_ts = nullptr) const;
100100

101101
// Slice samples by timestamp range [first, last)
102102
// e.g., samples: {10,20,30,40}, first=20, last=40 -> {20,30}
103-
SampleBatchSlice SliceByTimestamps(uint64_t first, uint64_t last, bool contain_last = false);
103+
SampleBatchSlice SliceByTimestamps(uint64_t first, uint64_t last, bool contain_last = false) const;
104104

105105
uint64_t GetFirstTimestamp() const;
106106
uint64_t GetLastTimestamp() const;
@@ -136,7 +136,7 @@ class TSChunk {
136136
// e.g., retention=3600, last_ts=5000 -> samples before 5000-3600 are expired
137137
void Expire(uint64_t last_ts, uint64_t retention);
138138

139-
SampleBatchSlice AsSlice();
139+
SampleBatchSlice AsSlice() const;
140140

141141
// Return add results by samples' order
142142
std::vector<AddResult> GetFinalResults() const;

0 commit comments

Comments
 (0)