lookup_key.h 2.1 KB

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