Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
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 be3// found in the LICENSE file. See the AUTHORS file for names of contributors.5#include "db/dbformat.h"7#include <stdio.h>9#include <sstream>11#include "port/port.h"12#include "util/coding.h"14namespace leveldb {16static uint64_t PackSequenceAndType(uint64_t seq, ValueType t) {17assert(seq <= kMaxSequenceNumber);18assert(t <= kValueTypeForSeek);19return (seq << 8) | t;20}22void AppendInternalKey(std::string* result, const ParsedInternalKey& key) {23result->append(key.user_key.data(), key.user_key.size());24PutFixed64(result, PackSequenceAndType(key.sequence, key.type));25}27std::string ParsedInternalKey::DebugString() const {28std::ostringstream ss;29ss << '\'' << EscapeString(user_key.ToString()) << "' @ " << sequence << " : "30<< static_cast<int>(type);31return ss.str();32}34std::string InternalKey::DebugString() const {35ParsedInternalKey parsed;36if (ParseInternalKey(rep_, &parsed)) {37return parsed.DebugString();38}39std::ostringstream ss;40ss << "(bad)" << EscapeString(rep_);41return ss.str();42}44const char* InternalKeyComparator::Name() const {45return "leveldb.InternalKeyComparator";46}48int InternalKeyComparator::Compare(const Slice& akey, const Slice& bkey) const {49// Order by:50// increasing user key (according to user-supplied comparator)51// decreasing sequence number52// decreasing type (though sequence# should be enough to disambiguate)53int r = user_comparator_->Compare(ExtractUserKey(akey), ExtractUserKey(bkey));54if (r == 0) {55const uint64_t anum = DecodeFixed64(akey.data() + akey.size() - 8);56const uint64_t bnum = DecodeFixed64(bkey.data() + bkey.size() - 8);57if (anum > bnum) {58r = -1;59} else if (anum < bnum) {60r = +1;61}62}63return r;64}66void InternalKeyComparator::FindShortestSeparator(std::string* start,67const Slice& limit) const {68// Attempt to shorten the user portion of the key69Slice user_start = ExtractUserKey(*start);70Slice user_limit = ExtractUserKey(limit);71std::string tmp(user_start.data(), user_start.size());72user_comparator_->FindShortestSeparator(&tmp, user_limit);73if (tmp.size() < user_start.size() &&74user_comparator_->Compare(user_start, tmp) < 0) {75// User key has become shorter physically, but larger logically.76// Tack on the earliest possible number to the shortened user key.77PutFixed64(&tmp,78PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));79assert(this->Compare(*start, tmp) < 0);80assert(this->Compare(tmp, limit) < 0);81start->swap(tmp);82}83}85void InternalKeyComparator::FindShortSuccessor(std::string* key) const {86Slice user_key = ExtractUserKey(*key);87std::string tmp(user_key.data(), user_key.size());88user_comparator_->FindShortSuccessor(&tmp);89if (tmp.size() < user_key.size() &&90user_comparator_->Compare(user_key, tmp) < 0) {91// User key has become shorter physically, but larger logically.92// Tack on the earliest possible number to the shortened user key.93PutFixed64(&tmp,94PackSequenceAndType(kMaxSequenceNumber, kValueTypeForSeek));95assert(this->Compare(*key, tmp) < 0);96key->swap(tmp);97}98}100const char* InternalFilterPolicy::Name() const { return user_policy_->Name(); }102void InternalFilterPolicy::CreateFilter(const Slice* keys, int n,103std::string* dst) const {104// We rely on the fact that the code in table.cc does not mind us105// adjusting keys[].106Slice* mkey = const_cast<Slice*>(keys);107for (int i = 0; i < n; i++) {108mkey[i] = ExtractUserKey(keys[i]);109// TODO(sanjay): Suppress dups?110}111user_policy_->CreateFilter(keys, n, dst);112}114bool InternalFilterPolicy::KeyMayMatch(const Slice& key, const Slice& f) const {115return user_policy_->KeyMayMatch(ExtractUserKey(key), f);116}118LookupKey::LookupKey(const Slice& user_key, SequenceNumber s) {119size_t usize = user_key.size();120size_t needed = usize + 13; // A conservative estimate121char* dst;122if (needed <= sizeof(space_)) {123dst = space_;124} else {125dst = new char[needed];126}127start_ = dst;128dst = EncodeVarint32(dst, usize + 8);129kstart_ = dst;130memcpy(dst, user_key.data(), usize);131dst += usize;132EncodeFixed64(dst, PackSequenceAndType(s, kValueTypeForSeek));133dst += 8;134end_ = dst;135}137} // namespace leveldb
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.