2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 23:59:46 +02:00
|
|
|
//
|
2013-08-13 23:04:56 +02:00
|
|
|
// Copyright (c) 2012 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2015-07-02 01:13:49 +02:00
|
|
|
#include <algorithm>
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/slice_transform.h"
|
|
|
|
#include "rocksdb/slice.h"
|
2014-11-25 05:44:49 +01:00
|
|
|
#include "util/string_util.h"
|
2015-07-08 01:58:20 +02:00
|
|
|
#include <stdio.h>
|
2013-08-13 23:04:56 +02:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2013-08-13 23:04:56 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class FixedPrefixTransform : public SliceTransform {
|
|
|
|
private:
|
|
|
|
size_t prefix_len_;
|
2014-05-13 23:42:31 +02:00
|
|
|
std::string name_;
|
2013-08-13 23:04:56 +02:00
|
|
|
|
|
|
|
public:
|
2014-05-13 23:42:31 +02:00
|
|
|
explicit FixedPrefixTransform(size_t prefix_len)
|
|
|
|
: prefix_len_(prefix_len),
|
2015-10-03 00:35:32 +02:00
|
|
|
// Note that if any part of the name format changes, it will require
|
|
|
|
// changes on options_helper in order to make RocksDBOptionsParser work
|
|
|
|
// for the new change.
|
|
|
|
// TODO(yhchiang): move serialization / deserializaion code inside
|
|
|
|
// the class implementation itself.
|
2014-11-25 05:44:49 +01:00
|
|
|
name_("rocksdb.FixedPrefix." + ToString(prefix_len_)) {}
|
2013-08-13 23:04:56 +02:00
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual const char* Name() const override { return name_.c_str(); }
|
2013-08-13 23:04:56 +02:00
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual Slice Transform(const Slice& src) const override {
|
2013-08-13 23:04:56 +02:00
|
|
|
assert(InDomain(src));
|
|
|
|
return Slice(src.data(), prefix_len_);
|
|
|
|
}
|
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual bool InDomain(const Slice& src) const override {
|
2013-08-13 23:04:56 +02:00
|
|
|
return (src.size() >= prefix_len_);
|
|
|
|
}
|
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual bool InRange(const Slice& dst) const override {
|
2013-08-13 23:04:56 +02:00
|
|
|
return (dst.size() == prefix_len_);
|
|
|
|
}
|
2015-01-21 20:09:56 +01:00
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual bool SameResultWhenAppended(const Slice& prefix) const override {
|
2015-01-21 20:09:56 +01:00
|
|
|
return InDomain(prefix);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CappedPrefixTransform : public SliceTransform {
|
|
|
|
private:
|
|
|
|
size_t cap_len_;
|
|
|
|
std::string name_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit CappedPrefixTransform(size_t cap_len)
|
|
|
|
: cap_len_(cap_len),
|
2015-10-03 00:35:32 +02:00
|
|
|
// Note that if any part of the name format changes, it will require
|
|
|
|
// changes on options_helper in order to make RocksDBOptionsParser work
|
|
|
|
// for the new change.
|
|
|
|
// TODO(yhchiang): move serialization / deserializaion code inside
|
|
|
|
// the class implementation itself.
|
2015-01-21 20:09:56 +01:00
|
|
|
name_("rocksdb.CappedPrefix." + ToString(cap_len_)) {}
|
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual const char* Name() const override { return name_.c_str(); }
|
2015-01-21 20:09:56 +01:00
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual Slice Transform(const Slice& src) const override {
|
2015-01-21 20:09:56 +01:00
|
|
|
assert(InDomain(src));
|
|
|
|
return Slice(src.data(), std::min(cap_len_, src.size()));
|
|
|
|
}
|
|
|
|
|
2017-07-22 03:13:59 +02:00
|
|
|
virtual bool InDomain(const Slice& src) const override { return true; }
|
2015-01-21 20:09:56 +01:00
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual bool InRange(const Slice& dst) const override {
|
2015-01-21 20:09:56 +01:00
|
|
|
return (dst.size() <= cap_len_);
|
|
|
|
}
|
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual bool SameResultWhenAppended(const Slice& prefix) const override {
|
2015-01-21 20:09:56 +01:00
|
|
|
return prefix.size() >= cap_len_;
|
|
|
|
}
|
2013-08-13 23:04:56 +02:00
|
|
|
};
|
2013-08-23 08:10:02 +02:00
|
|
|
|
|
|
|
class NoopTransform : public SliceTransform {
|
|
|
|
public:
|
|
|
|
explicit NoopTransform() { }
|
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual const char* Name() const override { return "rocksdb.Noop"; }
|
2013-08-23 08:10:02 +02:00
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual Slice Transform(const Slice& src) const override { return src; }
|
2013-08-23 08:10:02 +02:00
|
|
|
|
2017-07-22 03:13:59 +02:00
|
|
|
virtual bool InDomain(const Slice& src) const override { return true; }
|
2013-08-23 08:10:02 +02:00
|
|
|
|
2017-07-22 03:13:59 +02:00
|
|
|
virtual bool InRange(const Slice& dst) const override { return true; }
|
2015-01-21 20:09:56 +01:00
|
|
|
|
2017-07-22 03:13:59 +02:00
|
|
|
virtual bool SameResultWhenAppended(const Slice& prefix) const override {
|
2015-01-21 20:09:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
2013-08-23 08:10:02 +02:00
|
|
|
};
|
|
|
|
|
2013-08-13 23:04:56 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 06:25:12 +02:00
|
|
|
// 2 small internal utility functions, for efficient hex conversions
|
|
|
|
// and no need for snprintf, toupper etc...
|
|
|
|
// Originally from wdt/util/EncryptionUtils.cpp - for ToString(true)/DecodeHex:
|
|
|
|
char toHex(unsigned char v) {
|
|
|
|
if (v <= 9) {
|
|
|
|
return '0' + v;
|
|
|
|
}
|
|
|
|
return 'A' + v - 10;
|
|
|
|
}
|
|
|
|
// most of the code is for validation/error check
|
|
|
|
int fromHex(char c) {
|
|
|
|
// toupper:
|
|
|
|
if (c >= 'a' && c <= 'f') {
|
|
|
|
c -= ('a' - 'A'); // aka 0x20
|
|
|
|
}
|
|
|
|
// validation
|
|
|
|
if (c < '0' || (c > '9' && (c < 'A' || c > 'F'))) {
|
|
|
|
return -1; // invalid not 0-9A-F hex char
|
|
|
|
}
|
|
|
|
if (c <= '9') {
|
|
|
|
return c - '0';
|
|
|
|
}
|
|
|
|
return c - 'A' + 10;
|
|
|
|
}
|
2015-07-08 01:58:20 +02:00
|
|
|
|
2016-06-10 00:30:19 +02:00
|
|
|
Slice::Slice(const SliceParts& parts, std::string* buf) {
|
|
|
|
size_t length = 0;
|
|
|
|
for (int i = 0; i < parts.num_parts; ++i) {
|
|
|
|
length += parts.parts[i].size();
|
|
|
|
}
|
|
|
|
buf->reserve(length);
|
|
|
|
|
|
|
|
for (int i = 0; i < parts.num_parts; ++i) {
|
|
|
|
buf->append(parts.parts[i].data(), parts.parts[i].size());
|
|
|
|
}
|
|
|
|
data_ = buf->data();
|
|
|
|
size_ = buf->size();
|
|
|
|
}
|
|
|
|
|
2015-07-08 01:58:20 +02:00
|
|
|
// Return a string that contains the copy of the referenced data.
|
|
|
|
std::string Slice::ToString(bool hex) const {
|
2015-07-13 21:11:05 +02:00
|
|
|
std::string result; // RVO/NRVO/move
|
2015-07-08 01:58:20 +02:00
|
|
|
if (hex) {
|
2016-03-30 06:25:12 +02:00
|
|
|
result.reserve(2 * size_);
|
|
|
|
for (size_t i = 0; i < size_; ++i) {
|
|
|
|
unsigned char c = data_[i];
|
|
|
|
result.push_back(toHex(c >> 4));
|
|
|
|
result.push_back(toHex(c & 0xf));
|
2015-07-08 01:58:20 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
result.assign(data_, size_);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 01:09:09 +02:00
|
|
|
// Originally from rocksdb/utilities/ldb_cmd.h
|
2016-03-30 06:25:12 +02:00
|
|
|
bool Slice::DecodeHex(std::string* result) const {
|
|
|
|
std::string::size_type len = size_;
|
|
|
|
if (len % 2) {
|
|
|
|
// Hex string must be even number of hex digits to get complete bytes back
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!result) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
result->clear();
|
|
|
|
result->reserve(len / 2);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len;) {
|
|
|
|
int h1 = fromHex(data_[i++]);
|
|
|
|
if (h1 < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int h2 = fromHex(data_[i++]);
|
|
|
|
if (h2 < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
result->push_back((h1 << 4) | h2);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-13 23:04:56 +02:00
|
|
|
const SliceTransform* NewFixedPrefixTransform(size_t prefix_len) {
|
|
|
|
return new FixedPrefixTransform(prefix_len);
|
|
|
|
}
|
|
|
|
|
2015-01-21 20:09:56 +01:00
|
|
|
const SliceTransform* NewCappedPrefixTransform(size_t cap_len) {
|
|
|
|
return new CappedPrefixTransform(cap_len);
|
|
|
|
}
|
|
|
|
|
2013-08-23 08:10:02 +02:00
|
|
|
const SliceTransform* NewNoopTransform() {
|
|
|
|
return new NoopTransform;
|
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|