slice.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #include "rocksdb/slice.h"
  10. #include <algorithm>
  11. #include <cstdio>
  12. #include "rocksdb/convenience.h"
  13. #include "rocksdb/slice_transform.h"
  14. #include "rocksdb/utilities/object_registry.h"
  15. #include "rocksdb/utilities/options_type.h"
  16. #include "util/string_util.h"
  17. namespace ROCKSDB_NAMESPACE {
  18. namespace {
  19. class FixedPrefixTransform : public SliceTransform {
  20. private:
  21. size_t prefix_len_;
  22. std::string id_;
  23. public:
  24. explicit FixedPrefixTransform(size_t prefix_len) : prefix_len_(prefix_len) {
  25. id_ = std::string(kClassName()) + "." + std::to_string(prefix_len_);
  26. }
  27. static const char* kClassName() { return "rocksdb.FixedPrefix"; }
  28. static const char* kNickName() { return "fixed"; }
  29. const char* Name() const override { return kClassName(); }
  30. const char* NickName() const override { return kNickName(); }
  31. bool IsInstanceOf(const std::string& name) const override {
  32. if (name == id_) {
  33. return true;
  34. } else if (StartsWith(name, kNickName())) {
  35. std::string alt_id =
  36. std::string(kNickName()) + ":" + std::to_string(prefix_len_);
  37. if (name == alt_id) {
  38. return true;
  39. }
  40. }
  41. return SliceTransform::IsInstanceOf(name);
  42. }
  43. std::string GetId() const override { return id_; }
  44. Slice Transform(const Slice& src) const override {
  45. assert(InDomain(src));
  46. return Slice(src.data(), prefix_len_);
  47. }
  48. bool InDomain(const Slice& src) const override {
  49. return (src.size() >= prefix_len_);
  50. }
  51. bool InRange(const Slice& dst) const override {
  52. return (dst.size() == prefix_len_);
  53. }
  54. bool FullLengthEnabled(size_t* len) const override {
  55. *len = prefix_len_;
  56. return true;
  57. }
  58. bool SameResultWhenAppended(const Slice& prefix) const override {
  59. return InDomain(prefix);
  60. }
  61. };
  62. class CappedPrefixTransform : public SliceTransform {
  63. private:
  64. size_t cap_len_;
  65. std::string id_;
  66. public:
  67. explicit CappedPrefixTransform(size_t cap_len) : cap_len_(cap_len) {
  68. id_ = std::string(kClassName()) + "." + std::to_string(cap_len_);
  69. }
  70. static const char* kClassName() { return "rocksdb.CappedPrefix"; }
  71. static const char* kNickName() { return "capped"; }
  72. const char* Name() const override { return kClassName(); }
  73. const char* NickName() const override { return kNickName(); }
  74. std::string GetId() const override { return id_; }
  75. bool IsInstanceOf(const std::string& name) const override {
  76. if (name == id_) {
  77. return true;
  78. } else if (StartsWith(name, kNickName())) {
  79. std::string alt_id =
  80. std::string(kNickName()) + ":" + std::to_string(cap_len_);
  81. if (name == alt_id) {
  82. return true;
  83. }
  84. }
  85. return SliceTransform::IsInstanceOf(name);
  86. }
  87. Slice Transform(const Slice& src) const override {
  88. assert(InDomain(src));
  89. return Slice(src.data(), std::min(cap_len_, src.size()));
  90. }
  91. bool InDomain(const Slice& /*src*/) const override { return true; }
  92. bool InRange(const Slice& dst) const override {
  93. return (dst.size() <= cap_len_);
  94. }
  95. bool FullLengthEnabled(size_t* len) const override {
  96. *len = cap_len_;
  97. return true;
  98. }
  99. bool SameResultWhenAppended(const Slice& prefix) const override {
  100. return prefix.size() >= cap_len_;
  101. }
  102. };
  103. class NoopTransform : public SliceTransform {
  104. public:
  105. explicit NoopTransform() = default;
  106. static const char* kClassName() { return "rocksdb.Noop"; }
  107. const char* Name() const override { return kClassName(); }
  108. Slice Transform(const Slice& src) const override { return src; }
  109. bool InDomain(const Slice& /*src*/) const override { return true; }
  110. bool InRange(const Slice& /*dst*/) const override { return true; }
  111. bool SameResultWhenAppended(const Slice& /*prefix*/) const override {
  112. return false;
  113. }
  114. };
  115. } // end namespace
  116. const SliceTransform* NewFixedPrefixTransform(size_t prefix_len) {
  117. return new FixedPrefixTransform(prefix_len);
  118. }
  119. const SliceTransform* NewCappedPrefixTransform(size_t cap_len) {
  120. return new CappedPrefixTransform(cap_len);
  121. }
  122. const SliceTransform* NewNoopTransform() { return new NoopTransform; }
  123. static int RegisterBuiltinSliceTransform(ObjectLibrary& library,
  124. const std::string& /*arg*/) {
  125. // For the builtin transforms, the format is typically
  126. // [Name].[0-9]+ or [NickName]:[0-9]+
  127. library.AddFactory<const SliceTransform>(
  128. NoopTransform::kClassName(),
  129. [](const std::string& /*uri*/,
  130. std::unique_ptr<const SliceTransform>* guard,
  131. std::string* /*errmsg*/) {
  132. guard->reset(NewNoopTransform());
  133. return guard->get();
  134. });
  135. library.AddFactory<const SliceTransform>(
  136. ObjectLibrary::PatternEntry(FixedPrefixTransform::kNickName(), false)
  137. .AddNumber(":"),
  138. [](const std::string& uri, std::unique_ptr<const SliceTransform>* guard,
  139. std::string* /*errmsg*/) {
  140. auto colon = uri.find(':');
  141. auto len = ParseSizeT(uri.substr(colon + 1));
  142. guard->reset(NewFixedPrefixTransform(len));
  143. return guard->get();
  144. });
  145. library.AddFactory<const SliceTransform>(
  146. ObjectLibrary::PatternEntry(FixedPrefixTransform::kClassName(), false)
  147. .AddNumber("."),
  148. [](const std::string& uri, std::unique_ptr<const SliceTransform>* guard,
  149. std::string* /*errmsg*/) {
  150. auto len = ParseSizeT(
  151. uri.substr(strlen(FixedPrefixTransform::kClassName()) + 1));
  152. guard->reset(NewFixedPrefixTransform(len));
  153. return guard->get();
  154. });
  155. library.AddFactory<const SliceTransform>(
  156. ObjectLibrary::PatternEntry(CappedPrefixTransform::kNickName(), false)
  157. .AddNumber(":"),
  158. [](const std::string& uri, std::unique_ptr<const SliceTransform>* guard,
  159. std::string* /*errmsg*/) {
  160. auto colon = uri.find(':');
  161. auto len = ParseSizeT(uri.substr(colon + 1));
  162. guard->reset(NewCappedPrefixTransform(len));
  163. return guard->get();
  164. });
  165. library.AddFactory<const SliceTransform>(
  166. ObjectLibrary::PatternEntry(CappedPrefixTransform::kClassName(), false)
  167. .AddNumber("."),
  168. [](const std::string& uri, std::unique_ptr<const SliceTransform>* guard,
  169. std::string* /*errmsg*/) {
  170. auto len = ParseSizeT(
  171. uri.substr(strlen(CappedPrefixTransform::kClassName()) + 1));
  172. guard->reset(NewCappedPrefixTransform(len));
  173. return guard->get();
  174. });
  175. size_t num_types;
  176. return static_cast<int>(library.GetFactoryCount(&num_types));
  177. }
  178. Status SliceTransform::CreateFromString(
  179. const ConfigOptions& config_options, const std::string& value,
  180. std::shared_ptr<const SliceTransform>* result) {
  181. static std::once_flag once;
  182. std::call_once(once, [&]() {
  183. RegisterBuiltinSliceTransform(*(ObjectLibrary::Default().get()), "");
  184. });
  185. std::string id;
  186. std::unordered_map<std::string, std::string> opt_map;
  187. Status status = Customizable::GetOptionsMap(config_options, result->get(),
  188. value, &id, &opt_map);
  189. if (!status.ok()) { // GetOptionsMap failed
  190. return status;
  191. } else if (id.empty() && opt_map.empty()) {
  192. result->reset();
  193. } else {
  194. status = config_options.registry->NewSharedObject(id, result);
  195. if (config_options.ignore_unsupported_options && status.IsNotSupported()) {
  196. return Status::OK();
  197. } else if (status.ok()) {
  198. SliceTransform* transform = const_cast<SliceTransform*>(result->get());
  199. status =
  200. Customizable::ConfigureNewObject(config_options, transform, opt_map);
  201. }
  202. }
  203. return status;
  204. }
  205. std::string SliceTransform::AsString() const {
  206. if (HasRegisteredOptions()) {
  207. ConfigOptions opts;
  208. opts.delimiter = ";";
  209. return ToString(opts);
  210. }
  211. return GetId();
  212. }
  213. // 2 small internal utility functions, for efficient hex conversions
  214. // and no need for snprintf, toupper etc...
  215. // Originally from wdt/util/EncryptionUtils.cpp - for
  216. // std::to_string(true)/DecodeHex:
  217. char toHex(unsigned char v) {
  218. if (v <= 9) {
  219. return '0' + v;
  220. }
  221. return 'A' + v - 10;
  222. }
  223. // most of the code is for validation/error check
  224. int fromHex(char c) {
  225. // toupper:
  226. if (c >= 'a' && c <= 'f') {
  227. c -= ('a' - 'A'); // aka 0x20
  228. }
  229. // validation
  230. if (c < '0' || (c > '9' && (c < 'A' || c > 'F'))) {
  231. return -1; // invalid not 0-9A-F hex char
  232. }
  233. if (c <= '9') {
  234. return c - '0';
  235. }
  236. return c - 'A' + 10;
  237. }
  238. Slice::Slice(const SliceParts& parts, std::string* buf) {
  239. size_t length = 0;
  240. for (int i = 0; i < parts.num_parts; ++i) {
  241. length += parts.parts[i].size();
  242. }
  243. buf->reserve(length);
  244. for (int i = 0; i < parts.num_parts; ++i) {
  245. buf->append(parts.parts[i].data(), parts.parts[i].size());
  246. }
  247. data_ = buf->data();
  248. size_ = buf->size();
  249. }
  250. // Return a string that contains the copy of the referenced data.
  251. std::string Slice::ToString(bool hex) const {
  252. std::string result; // RVO/NRVO/move
  253. if (hex) {
  254. result.reserve(2 * size_);
  255. for (size_t i = 0; i < size_; ++i) {
  256. unsigned char c = data_[i];
  257. result.push_back(toHex(c >> 4));
  258. result.push_back(toHex(c & 0xf));
  259. }
  260. return result;
  261. } else {
  262. result.assign(data_, size_);
  263. return result;
  264. }
  265. }
  266. // Originally from rocksdb/utilities/ldb_cmd.h
  267. bool Slice::DecodeHex(std::string* result) const {
  268. std::string::size_type len = size_;
  269. if (len % 2) {
  270. // Hex string must be even number of hex digits to get complete bytes back
  271. return false;
  272. }
  273. if (!result) {
  274. return false;
  275. }
  276. result->clear();
  277. result->reserve(len / 2);
  278. for (size_t i = 0; i < len;) {
  279. int h1 = fromHex(data_[i++]);
  280. if (h1 < 0) {
  281. return false;
  282. }
  283. int h2 = fromHex(data_[i++]);
  284. if (h2 < 0) {
  285. return false;
  286. }
  287. result->push_back(static_cast<char>((h1 << 4) | h2));
  288. }
  289. return true;
  290. }
  291. PinnableSlice::PinnableSlice(PinnableSlice&& other) {
  292. *this = std::move(other);
  293. }
  294. PinnableSlice& PinnableSlice::operator=(PinnableSlice&& other) {
  295. if (this != &other) {
  296. Cleanable::Reset();
  297. Cleanable::operator=(std::move(other));
  298. size_ = other.size_;
  299. pinned_ = other.pinned_;
  300. if (pinned_) {
  301. data_ = other.data_;
  302. // When it's pinned, buf should no longer be of use.
  303. } else {
  304. if (other.buf_ == &other.self_space_) {
  305. self_space_ = std::move(other.self_space_);
  306. buf_ = &self_space_;
  307. data_ = buf_->data();
  308. } else {
  309. buf_ = other.buf_;
  310. data_ = other.data_;
  311. }
  312. }
  313. other.self_space_.clear();
  314. other.buf_ = &other.self_space_;
  315. other.pinned_ = false;
  316. other.PinSelf();
  317. }
  318. return *this;
  319. }
  320. } // namespace ROCKSDB_NAMESPACE