1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5 //
6 // Copyright (c) 2012 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
9
10 #include <algorithm>
11 #include "rocksdb/slice_transform.h"
12 #include "rocksdb/slice.h"
13 #include "util/string_util.h"
14 #include <stdio.h>
15
16 namespace rocksdb {
17
18 namespace {
19
20 class FixedPrefixTransform : public SliceTransform {
21 private:
22 size_t prefix_len_;
23 std::string name_;
24
25 public:
FixedPrefixTransform(size_t prefix_len)26 explicit FixedPrefixTransform(size_t prefix_len)
27 : prefix_len_(prefix_len),
28 // Note that if any part of the name format changes, it will require
29 // changes on options_helper in order to make RocksDBOptionsParser work
30 // for the new change.
31 // TODO(yhchiang): move serialization / deserializaion code inside
32 // the class implementation itself.
33 name_("rocksdb.FixedPrefix." + ToString(prefix_len_)) {}
34
Name() const35 const char* Name() const override { return name_.c_str(); }
36
Transform(const Slice & src) const37 Slice Transform(const Slice& src) const override {
38 assert(InDomain(src));
39 return Slice(src.data(), prefix_len_);
40 }
41
InDomain(const Slice & src) const42 bool InDomain(const Slice& src) const override {
43 return (src.size() >= prefix_len_);
44 }
45
InRange(const Slice & dst) const46 bool InRange(const Slice& dst) const override {
47 return (dst.size() == prefix_len_);
48 }
49
FullLengthEnabled(size_t * len) const50 bool FullLengthEnabled(size_t* len) const override {
51 *len = prefix_len_;
52 return true;
53 }
54
SameResultWhenAppended(const Slice & prefix) const55 bool SameResultWhenAppended(const Slice& prefix) const override {
56 return InDomain(prefix);
57 }
58 };
59
60 class CappedPrefixTransform : public SliceTransform {
61 private:
62 size_t cap_len_;
63 std::string name_;
64
65 public:
CappedPrefixTransform(size_t cap_len)66 explicit CappedPrefixTransform(size_t cap_len)
67 : cap_len_(cap_len),
68 // Note that if any part of the name format changes, it will require
69 // changes on options_helper in order to make RocksDBOptionsParser work
70 // for the new change.
71 // TODO(yhchiang): move serialization / deserializaion code inside
72 // the class implementation itself.
73 name_("rocksdb.CappedPrefix." + ToString(cap_len_)) {}
74
Name() const75 const char* Name() const override { return name_.c_str(); }
76
Transform(const Slice & src) const77 Slice Transform(const Slice& src) const override {
78 assert(InDomain(src));
79 return Slice(src.data(), std::min(cap_len_, src.size()));
80 }
81
InDomain(const Slice &) const82 bool InDomain(const Slice& /*src*/) const override { return true; }
83
InRange(const Slice & dst) const84 bool InRange(const Slice& dst) const override {
85 return (dst.size() <= cap_len_);
86 }
87
FullLengthEnabled(size_t * len) const88 bool FullLengthEnabled(size_t* len) const override {
89 *len = cap_len_;
90 return true;
91 }
92
SameResultWhenAppended(const Slice & prefix) const93 bool SameResultWhenAppended(const Slice& prefix) const override {
94 return prefix.size() >= cap_len_;
95 }
96 };
97
98 class NoopTransform : public SliceTransform {
99 public:
NoopTransform()100 explicit NoopTransform() { }
101
Name() const102 const char* Name() const override { return "rocksdb.Noop"; }
103
Transform(const Slice & src) const104 Slice Transform(const Slice& src) const override { return src; }
105
InDomain(const Slice &) const106 bool InDomain(const Slice& /*src*/) const override { return true; }
107
InRange(const Slice &) const108 bool InRange(const Slice& /*dst*/) const override { return true; }
109
SameResultWhenAppended(const Slice &) const110 bool SameResultWhenAppended(const Slice& /*prefix*/) const override {
111 return false;
112 }
113 };
114
115 }
116
117 // 2 small internal utility functions, for efficient hex conversions
118 // and no need for snprintf, toupper etc...
119 // Originally from wdt/util/EncryptionUtils.cpp - for ToString(true)/DecodeHex:
toHex(unsigned char v)120 char toHex(unsigned char v) {
121 if (v <= 9) {
122 return '0' + v;
123 }
124 return 'A' + v - 10;
125 }
126 // most of the code is for validation/error check
fromHex(char c)127 int fromHex(char c) {
128 // toupper:
129 if (c >= 'a' && c <= 'f') {
130 c -= ('a' - 'A'); // aka 0x20
131 }
132 // validation
133 if (c < '0' || (c > '9' && (c < 'A' || c > 'F'))) {
134 return -1; // invalid not 0-9A-F hex char
135 }
136 if (c <= '9') {
137 return c - '0';
138 }
139 return c - 'A' + 10;
140 }
141
Slice(const SliceParts & parts,std::string * buf)142 Slice::Slice(const SliceParts& parts, std::string* buf) {
143 size_t length = 0;
144 for (int i = 0; i < parts.num_parts; ++i) {
145 length += parts.parts[i].size();
146 }
147 buf->reserve(length);
148
149 for (int i = 0; i < parts.num_parts; ++i) {
150 buf->append(parts.parts[i].data(), parts.parts[i].size());
151 }
152 data_ = buf->data();
153 size_ = buf->size();
154 }
155
156 // Return a string that contains the copy of the referenced data.
ToString(bool hex) const157 std::string Slice::ToString(bool hex) const {
158 std::string result; // RVO/NRVO/move
159 if (hex) {
160 result.reserve(2 * size_);
161 for (size_t i = 0; i < size_; ++i) {
162 unsigned char c = data_[i];
163 result.push_back(toHex(c >> 4));
164 result.push_back(toHex(c & 0xf));
165 }
166 return result;
167 } else {
168 result.assign(data_, size_);
169 return result;
170 }
171 }
172
173 // Originally from rocksdb/utilities/ldb_cmd.h
DecodeHex(std::string * result) const174 bool Slice::DecodeHex(std::string* result) const {
175 std::string::size_type len = size_;
176 if (len % 2) {
177 // Hex string must be even number of hex digits to get complete bytes back
178 return false;
179 }
180 if (!result) {
181 return false;
182 }
183 result->clear();
184 result->reserve(len / 2);
185
186 for (size_t i = 0; i < len;) {
187 int h1 = fromHex(data_[i++]);
188 if (h1 < 0) {
189 return false;
190 }
191 int h2 = fromHex(data_[i++]);
192 if (h2 < 0) {
193 return false;
194 }
195 result->push_back(static_cast<char>((h1 << 4) | h2));
196 }
197 return true;
198 }
199
NewFixedPrefixTransform(size_t prefix_len)200 const SliceTransform* NewFixedPrefixTransform(size_t prefix_len) {
201 return new FixedPrefixTransform(prefix_len);
202 }
203
NewCappedPrefixTransform(size_t cap_len)204 const SliceTransform* NewCappedPrefixTransform(size_t cap_len) {
205 return new CappedPrefixTransform(cap_len);
206 }
207
NewNoopTransform()208 const SliceTransform* NewNoopTransform() {
209 return new NoopTransform;
210 }
211
212 } // namespace rocksdb
213