Coverage Report

Created: 2026-06-18 19:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/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.07M
static uint64_t PackSequenceAndType(uint64_t seq, ValueType t) {
17
2.07M
  assert(seq <= kMaxSequenceNumber);
  Branch (17:3): [True: 2.02M, False: 42.4k]
18
2.07M
  assert(t <= kValueTypeForSeek);
  Branch (18:3): [True: 2.00M, False: 21.7k]
19
2.00M
  return (seq << 8) | t;
20
2.02M
}
21
22
46.5k
void AppendInternalKey(std::string* result, const ParsedInternalKey& key) {
23
46.5k
  result->append(key.user_key.data(), key.user_key.size());
24
46.5k
  PutFixed64(result, PackSequenceAndType(key.sequence, key.type));
25
46.5k
}
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
27.3M
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
27.3M
  int r = user_comparator_->Compare(ExtractUserKey(akey), ExtractUserKey(bkey));
54
27.3M
  if (r == 0) {
  Branch (54:7): [True: 24.0M, False: 3.35M]
55
24.0M
    const uint64_t anum = DecodeFixed64(akey.data() + akey.size() - 8);
56
24.0M
    const uint64_t bnum = DecodeFixed64(bkey.data() + bkey.size() - 8);
57
24.0M
    if (anum > bnum) {
  Branch (57:9): [True: 2.55M, False: 21.4M]
58
2.55M
      r = -1;
59
21.4M
    } else if (anum < bnum) {
  Branch (59:16): [True: 21.4M, False: 18.4E]
60
21.4M
      r = +1;
61
21.4M
    }
62
24.0M
  }
63
27.3M
  return r;
64
27.3M
}
65
66
void InternalKeyComparator::FindShortestSeparator(std::string* start,
67
18.7k
                                                  const Slice& limit) const {
68
  // Attempt to shorten the user portion of the key
69
18.7k
  Slice user_start = ExtractUserKey(*start);
70
18.7k
  Slice user_limit = ExtractUserKey(limit);
71
18.7k
  std::string tmp(user_start.data(), user_start.size());
72
18.7k
  user_comparator_->FindShortestSeparator(&tmp, user_limit);
73
18.7k
  if (tmp.size() < user_start.size() &&
  Branch (73:7): [True: 1.77k, False: 16.9k]
  Branch (73:7): [True: 1.77k, False: 16.9k]
74
18.7k
      user_comparator_->Compare(user_start, tmp) < 0) {
  Branch (74:7): [True: 1.77k, 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
1.77k
    PutFixed64(&tmp,
78
1.77k
               PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));
79
1.77k
    assert(this->Compare(*start, tmp) < 0);
  Branch (79:5): [True: 1.77k, False: 0]
80
1.77k
    assert(this->Compare(tmp, limit) < 0);
  Branch (80:5): [True: 1.77k, False: 0]
81
1.77k
    start->swap(tmp);
82
1.77k
  }
83
18.7k
}
84
85
1.11k
void InternalKeyComparator::FindShortSuccessor(std::string* key) const {
86
1.11k
  Slice user_key = ExtractUserKey(*key);
87
1.11k
  std::string tmp(user_key.data(), user_key.size());
88
1.11k
  user_comparator_->FindShortSuccessor(&tmp);
89
1.11k
  if (tmp.size() < user_key.size() &&
  Branch (89:7): [True: 733, False: 377]
  Branch (89:7): [True: 733, False: 377]
90
1.11k
      user_comparator_->Compare(user_key, tmp) < 0) {
  Branch (90:7): [True: 733, 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
733
    PutFixed64(&tmp,
94
733
               PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));
95
733
    assert(this->Compare(*key, tmp) < 0);
  Branch (95:5): [True: 733, False: 0]
96
733
    key->swap(tmp);
97
733
  }
98
1.11k
}
99
100
2.22k
const char* InternalFilterPolicy::Name() const { return user_policy_->Name(); }
101
102
void InternalFilterPolicy::CreateFilter(const Slice* keys, int n,
103
19.8k
                                        std::string* dst) const {
104
  // We rely on the fact that the code in table.cc does not mind us
105
  // adjusting keys[].
106
19.8k
  Slice* mkey = const_cast<Slice*>(keys);
107
1.73M
  for (int i = 0; i < n; i++) {
  Branch (107:19): [True: 1.71M, False: 19.8k]
108
1.71M
    mkey[i] = ExtractUserKey(keys[i]);
109
    // TODO(sanjay): Suppress dups?
110
1.71M
  }
111
19.8k
  user_policy_->CreateFilter(keys, n, dst);
112
19.8k
}
113
114
0
bool InternalFilterPolicy::KeyMayMatch(const Slice& key, const Slice& f) const {
115
0
  return user_policy_->KeyMayMatch(ExtractUserKey(key), f);
116
0
}
117
118
2.04M
LookupKey::LookupKey(const Slice& user_key, SequenceNumber s) {
119
2.04M
  size_t usize = user_key.size();
120
2.04M
  size_t needed = usize + 13;  // A conservative estimate
121
2.04M
  char* dst;
122
2.04M
  if (needed <= sizeof(space_)) {
  Branch (122:7): [True: 2.04M, False: 6.81k]
123
2.04M
    dst = space_;
124
2.04M
  } else {
125
6.81k
    dst = new char[needed];
126
6.81k
  }
127
2.04M
  start_ = dst;
128
2.04M
  dst = EncodeVarint32(dst, usize + 8);
129
2.04M
  kstart_ = dst;
130
2.04M
  memcpy(dst, user_key.data(), usize);
131
2.04M
  dst += usize;
132
2.04M
  EncodeFixed64(dst, PackSequenceAndType(s, kValueTypeForSeek));
133
2.04M
  dst += 8;
134
2.04M
  end_ = dst;
135
2.04M
}
136
137
}  // namespace leveldb