|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include "redis_timeseries.h" |
| 22 | + |
| 23 | +namespace redis { |
| 24 | + |
| 25 | +void TSDownStreamMeta::Encode(std::string *dst) const { |
| 26 | + PutFixed8(dst, static_cast<uint8_t>(aggregator)); |
| 27 | + PutFixed64(dst, bucket_duration); |
| 28 | + PutFixed64(dst, alignment); |
| 29 | + PutFixed64(dst, latest_bucket_idx); |
| 30 | + PutFixed8(dst, static_cast<uint8_t>(u64_auxs.size())); |
| 31 | + PutFixed8(dst, static_cast<uint8_t>(f64_auxs.size())); |
| 32 | + for (const auto &aux : u64_auxs) { |
| 33 | + PutFixed64(dst, aux); |
| 34 | + } |
| 35 | + for (const auto &aux : f64_auxs) { |
| 36 | + PutDouble(dst, aux); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +rocksdb::Status TSDownStreamMeta::Decode(Slice *input) { |
| 41 | + if (input->size() < sizeof(uint8_t) * 3 + sizeof(uint64_t) * 3) { |
| 42 | + return rocksdb::Status::InvalidArgument("TSDownStreamMeta size is too short"); |
| 43 | + } |
| 44 | + |
| 45 | + GetFixed8(input, reinterpret_cast<uint8_t *>(&aggregator)); |
| 46 | + GetFixed64(input, &bucket_duration); |
| 47 | + GetFixed64(input, &alignment); |
| 48 | + GetFixed64(input, &latest_bucket_idx); |
| 49 | + uint8_t u64_auxs_size = 0; |
| 50 | + GetFixed8(input, &u64_auxs_size); |
| 51 | + uint8_t f64_auxs_size = 0; |
| 52 | + GetFixed8(input, &f64_auxs_size); |
| 53 | + |
| 54 | + // Strict checking to prevent accidental overwrites |
| 55 | + if (input->size() != sizeof(uint64_t) * u64_auxs_size + sizeof(double) * f64_auxs_size) { |
| 56 | + return rocksdb::Status::InvalidArgument("Invalid auxinfo size"); |
| 57 | + } |
| 58 | + |
| 59 | + for (uint8_t i = 0; i < u64_auxs_size; i++) { |
| 60 | + uint64_t aux = 0; |
| 61 | + GetFixed64(input, &aux); |
| 62 | + u64_auxs.push_back(aux); |
| 63 | + } |
| 64 | + for (uint8_t i = 0; i < f64_auxs_size; i++) { |
| 65 | + double aux = 0; |
| 66 | + GetDouble(input, &aux); |
| 67 | + f64_auxs.push_back(aux); |
| 68 | + } |
| 69 | + |
| 70 | + return rocksdb::Status::OK(); |
| 71 | +} |
| 72 | + |
| 73 | +std::string TSRevLabelKey::Encode() const { |
| 74 | + std::string encoded; |
| 75 | + size_t total = 1 + ns.size() + 1 + 4 + label_key.size() + 4 + label_value.size() + user_key.size(); |
| 76 | + |
| 77 | + encoded.resize(total); |
| 78 | + auto buf = encoded.data(); |
| 79 | + buf = EncodeFixed8(buf, static_cast<uint8_t>(ns.size())); |
| 80 | + buf = EncodeBuffer(buf, ns); |
| 81 | + buf = EncodeFixed8(buf, static_cast<uint8_t>(IndexKeyType::TS_LABEL)); |
| 82 | + buf = EncodeFixed32(buf, static_cast<uint32_t>(label_key.size())); |
| 83 | + buf = EncodeBuffer(buf, label_key); |
| 84 | + buf = EncodeFixed32(buf, static_cast<uint32_t>(label_value.size())); |
| 85 | + buf = EncodeBuffer(buf, label_value); |
| 86 | + EncodeBuffer(buf, user_key); |
| 87 | + |
| 88 | + return encoded; |
| 89 | +} |
| 90 | + |
| 91 | +std::string TimeSeries::internalKeyFromChunkID(const std::string &ns_key, const TimeSeriesMetadata &metadata, |
| 92 | + uint64_t id) const { |
| 93 | + std::string sub_key; |
| 94 | + PutFixed8(&sub_key, static_cast<uint8_t>(TSSubkeyType::CHUNK)); |
| 95 | + PutFixed64(&sub_key, id); |
| 96 | + |
| 97 | + return InternalKey(ns_key, sub_key, metadata.version, storage_->IsSlotIdEncoded()).Encode(); |
| 98 | +} |
| 99 | + |
| 100 | +std::string TimeSeries::internalKeyFromLabelKey(const std::string &ns_key, const TimeSeriesMetadata &metadata, |
| 101 | + Slice label_key) const { |
| 102 | + std::string sub_key; |
| 103 | + sub_key.resize(1 + label_key.size()); |
| 104 | + auto buf = sub_key.data(); |
| 105 | + buf = EncodeFixed8(buf, static_cast<uint8_t>(TSSubkeyType::LABEL)); |
| 106 | + EncodeBuffer(buf, label_key); |
| 107 | + |
| 108 | + return InternalKey(ns_key, sub_key, metadata.version, storage_->IsSlotIdEncoded()).Encode(); |
| 109 | +} |
| 110 | + |
| 111 | +std::string TimeSeries::internalKeyFromDownstreamKey(const std::string &ns_key, const TimeSeriesMetadata &metadata, |
| 112 | + Slice downstream_key) const { |
| 113 | + std::string sub_key; |
| 114 | + sub_key.resize(1 + downstream_key.size()); |
| 115 | + auto buf = sub_key.data(); |
| 116 | + buf = EncodeFixed8(buf, static_cast<uint8_t>(TSSubkeyType::DOWNSTREAM)); |
| 117 | + EncodeBuffer(buf, downstream_key); |
| 118 | + |
| 119 | + return InternalKey(ns_key, sub_key, metadata.version, storage_->IsSlotIdEncoded()).Encode(); |
| 120 | +} |
| 121 | + |
| 122 | +} // namespace redis |
0 commit comments