lookup_key.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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) 2011 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. #pragma once
  10. #include <string>
  11. #include <utility>
  12. #include "rocksdb/db.h"
  13. #include "rocksdb/slice.h"
  14. #include "rocksdb/types.h"
  15. namespace ROCKSDB_NAMESPACE {
  16. // A helper class useful for DBImpl::Get()
  17. class LookupKey {
  18. public:
  19. // Initialize *this for looking up user_key at a snapshot with
  20. // the specified sequence number.
  21. LookupKey(const Slice& _user_key, SequenceNumber sequence,
  22. const Slice* ts = nullptr);
  23. ~LookupKey();
  24. // Return a key suitable for lookup in a MemTable.
  25. Slice memtable_key() const {
  26. return Slice(start_, static_cast<size_t>(end_ - start_));
  27. }
  28. // Return an internal key (suitable for passing to an internal iterator)
  29. Slice internal_key() const {
  30. return Slice(kstart_, static_cast<size_t>(end_ - kstart_));
  31. }
  32. // Return the user key
  33. Slice user_key() const {
  34. return Slice(kstart_, static_cast<size_t>(end_ - kstart_ - 8));
  35. }
  36. private:
  37. // We construct a char array of the form:
  38. // klength varint32 <-- start_
  39. // userkey char[klength] <-- kstart_
  40. // tag uint64
  41. // <-- end_
  42. // The array is a suitable MemTable key.
  43. // The suffix starting with "userkey" can be used as an InternalKey.
  44. const char* start_;
  45. const char* kstart_;
  46. const char* end_;
  47. char space_[200]; // Avoid allocation for short keys
  48. // No copying allowed
  49. LookupKey(const LookupKey&);
  50. void operator=(const LookupKey&);
  51. };
  52. inline LookupKey::~LookupKey() {
  53. if (start_ != space_) delete[] start_;
  54. }
  55. } // namespace ROCKSDB_NAMESPACE