slice.cc 1.8 KB
Newer Older
1 2 3 4 5
//  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.
//
T
Tyler Harter 已提交
6 7 8 9
// 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.

10 11
#include "rocksdb/slice_transform.h"
#include "rocksdb/slice.h"
T
Tyler Harter 已提交
12

13
namespace rocksdb {
T
Tyler Harter 已提交
14 15 16 17 18 19

namespace {

class FixedPrefixTransform : public SliceTransform {
 private:
  size_t prefix_len_;
20
  std::string name_;
T
Tyler Harter 已提交
21 22

 public:
23 24 25
  explicit FixedPrefixTransform(size_t prefix_len)
      : prefix_len_(prefix_len),
        name_("rocksdb.FixedPrefix." + std::to_string(prefix_len_)) {}
T
Tyler Harter 已提交
26

27
  virtual const char* Name() const { return name_.c_str(); }
T
Tyler Harter 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41

  virtual Slice Transform(const Slice& src) const {
    assert(InDomain(src));
    return Slice(src.data(), prefix_len_);
  }

  virtual bool InDomain(const Slice& src) const {
    return (src.size() >= prefix_len_);
  }

  virtual bool InRange(const Slice& dst) const {
    return (dst.size() == prefix_len_);
  }
};
J
Jim Paton 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

class NoopTransform : public SliceTransform {
 public:
  explicit NoopTransform() { }

  virtual const char* Name() const {
    return "rocksdb.Noop";
  }

  virtual Slice Transform(const Slice& src) const {
    return src;
  }

  virtual bool InDomain(const Slice& src) const {
    return true;
  }

  virtual bool InRange(const Slice& dst) const {
    return true;
  }
};

T
Tyler Harter 已提交
64 65 66 67 68 69
}

const SliceTransform* NewFixedPrefixTransform(size_t prefix_len) {
  return new FixedPrefixTransform(prefix_len);
}

J
Jim Paton 已提交
70 71 72 73
const SliceTransform* NewNoopTransform() {
  return new NoopTransform;
}

74
}  // namespace rocksdb