Coverage Report

Created: 2026-07-14 18:13

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