Coverage Report

Created: 2025-12-03 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/leveldb/db/dbformat.cc
Line
Count
Source
1
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
5
#include "db/dbformat.h"
6
7
#include <cstdio>
8
#include <sstream>
9
10
#include "port/port.h"
11
#include "util/coding.h"
12
13
namespace leveldb {
14
15
440k
static uint64_t PackSequenceAndType(uint64_t seq, ValueType t) {
16
440k
  assert(seq <= kMaxSequenceNumber);
17
440k
  assert(t <= kValueTypeForSeek);
18
440k
  return (seq << 8) | t;
19
440k
}
20
21
331k
void AppendInternalKey(std::string* result, const ParsedInternalKey& key) {
22
331k
  result->append(key.user_key.data(), key.user_key.size());
23
331k
  PutFixed64(result, PackSequenceAndType(key.sequence, key.type));
24
331k
}
25
26
138k
std::string ParsedInternalKey::DebugString() const {
27
138k
  std::ostringstream ss;
28
138k
  ss << '\'' << EscapeString(user_key.ToString()) << "' @ " << sequence << " : "
29
138k
     << static_cast<int>(type);
30
138k
  return ss.str();
31
138k
}
32
33
138k
std::string InternalKey::DebugString() const {
34
138k
  ParsedInternalKey parsed;
35
138k
  if (ParseInternalKey(rep_, &parsed)) {
36
138k
    return parsed.DebugString();
37
138k
  }
38
0
  std::ostringstream ss;
39
0
  ss << "(bad)" << EscapeString(rep_);
40
0
  return ss.str();
41
138k
}
42
43
0
const char* InternalKeyComparator::Name() const {
44
0
  return "leveldb.InternalKeyComparator";
45
0
}
46
47
44.6M
int InternalKeyComparator::Compare(const Slice& akey, const Slice& bkey) const {
48
  // Order by:
49
  //    increasing user key (according to user-supplied comparator)
50
  //    decreasing sequence number
51
  //    decreasing type (though sequence# should be enough to disambiguate)
52
44.6M
  int r = user_comparator_->Compare(ExtractUserKey(akey), ExtractUserKey(bkey));
53
44.6M
  if (r == 0) {
54
28.3M
    const uint64_t anum = DecodeFixed64(akey.data() + akey.size() - 8);
55
28.3M
    const uint64_t bnum = DecodeFixed64(bkey.data() + bkey.size() - 8);
56
28.3M
    if (anum > bnum) {
57
246k
      r = -1;
58
28.0M
    } else if (anum < bnum) {
59
28.0M
      r = +1;
60
28.0M
    }
61
28.3M
  }
62
44.6M
  return r;
63
44.6M
}
64
65
void InternalKeyComparator::FindShortestSeparator(std::string* start,
66
28.1k
                                                  const Slice& limit) const {
67
  // Attempt to shorten the user portion of the key
68
28.1k
  Slice user_start = ExtractUserKey(*start);
69
28.1k
  Slice user_limit = ExtractUserKey(limit);
70
28.1k
  std::string tmp(user_start.data(), user_start.size());
71
28.1k
  user_comparator_->FindShortestSeparator(&tmp, user_limit);
72
28.1k
  if (tmp.size() < user_start.size() &&
73
17.6k
      user_comparator_->Compare(user_start, tmp) < 0) {
74
    // User key has become shorter physically, but larger logically.
75
    // Tack on the earliest possible number to the shortened user key.
76
17.6k
    PutFixed64(&tmp,
77
17.6k
               PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));
78
17.6k
    assert(this->Compare(*start, tmp) < 0);
79
17.6k
    assert(this->Compare(tmp, limit) < 0);
80
17.6k
    start->swap(tmp);
81
17.6k
  }
82
28.1k
}
83
84
75.4k
void InternalKeyComparator::FindShortSuccessor(std::string* key) const {
85
75.4k
  Slice user_key = ExtractUserKey(*key);
86
75.4k
  std::string tmp(user_key.data(), user_key.size());
87
75.4k
  user_comparator_->FindShortSuccessor(&tmp);
88
75.4k
  if (tmp.size() < user_key.size() &&
89
40.9k
      user_comparator_->Compare(user_key, tmp) < 0) {
90
    // User key has become shorter physically, but larger logically.
91
    // Tack on the earliest possible number to the shortened user key.
92
40.9k
    PutFixed64(&tmp,
93
40.9k
               PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));
94
40.9k
    assert(this->Compare(*key, tmp) < 0);
95
40.9k
    key->swap(tmp);
96
40.9k
  }
97
75.4k
}
98
99
0
const char* InternalFilterPolicy::Name() const { return user_policy_->Name(); }
100
101
void InternalFilterPolicy::CreateFilter(const Slice* keys, int n,
102
0
                                        std::string* dst) const {
103
  // We rely on the fact that the code in table.cc does not mind us
104
  // adjusting keys[].
105
0
  Slice* mkey = const_cast<Slice*>(keys);
106
0
  for (int i = 0; i < n; i++) {
107
0
    mkey[i] = ExtractUserKey(keys[i]);
108
    // TODO(sanjay): Suppress dups?
109
0
  }
110
0
  user_policy_->CreateFilter(keys, n, dst);
111
0
}
112
113
0
bool InternalFilterPolicy::KeyMayMatch(const Slice& key, const Slice& f) const {
114
0
  return user_policy_->KeyMayMatch(ExtractUserKey(key), f);
115
0
}
116
117
50.6k
LookupKey::LookupKey(const Slice& user_key, SequenceNumber s) {
118
50.6k
  size_t usize = user_key.size();
119
50.6k
  size_t needed = usize + 13;  // A conservative estimate
120
50.6k
  char* dst;
121
50.6k
  if (needed <= sizeof(space_)) {
122
44.8k
    dst = space_;
123
44.8k
  } else {
124
5.86k
    dst = new char[needed];
125
5.86k
  }
126
50.6k
  start_ = dst;
127
50.6k
  dst = EncodeVarint32(dst, usize + 8);
128
50.6k
  kstart_ = dst;
129
50.6k
  std::memcpy(dst, user_key.data(), usize);
130
50.6k
  dst += usize;
131
50.6k
  EncodeFixed64(dst, PackSequenceAndType(s, kValueTypeForSeek));
132
50.6k
  dst += 8;
133
50.6k
  end_ = dst;
134
50.6k
}
135
136
}  // namespace leveldb