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