2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
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"
|
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),
|
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),
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
2015-02-26 20:28:41 +01: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
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual bool InDomain(const Slice& src) const override { return true; }
|
2013-08-23 08:10:02 +02:00
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual bool InRange(const Slice& dst) const override { return true; }
|
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 false;
|
|
|
|
}
|
2013-08-23 08:10:02 +02:00
|
|
|
};
|
|
|
|
|
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
|