YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/docdb/doc_write_batch_cache.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#include "yb/docdb/doc_write_batch_cache.h"
15
16
#include <vector>
17
#include <algorithm>
18
#include <sstream>
19
20
#include "yb/docdb/doc_key.h"
21
#include "yb/docdb/docdb-internal.h"
22
#include "yb/docdb/primitive_value.h"
23
#include "yb/util/bytes_formatter.h"
24
25
using std::back_inserter;
26
using std::copy;
27
using std::endl;
28
using std::ostringstream;
29
using std::pair;
30
using std::sort;
31
using std::string;
32
using std::tuple;
33
using std::vector;
34
35
using yb::FormatBytesAsStr;
36
37
namespace yb {
38
namespace docdb {
39
40
21.3M
void DocWriteBatchCache::Put(const KeyBytes& key_bytes, const DocWriteBatchCache::Entry& entry) {
41
21.3M
    DOCDB_DEBUG_LOG(
42
21.3M
      "Writing to DocWriteBatchCache: encoded_key_prefix=$0, gen_ht=$1, value_type=$2",
43
21.3M
      BestEffortDocDBKeyToStr(key_bytes),
44
21.3M
      entry.doc_hybrid_time.ToString(),
45
21.3M
      ToString(entry.value_type));
46
47
21.3M
  prefix_to_gen_ht_[key_bytes.data()] = entry;
48
21.3M
}
49
50
boost::optional<DocWriteBatchCache::Entry> DocWriteBatchCache::Get(
51
118k
    const KeyBytes& encoded_key_prefix) {
52
118k
  auto iter = prefix_to_gen_ht_.find(encoded_key_prefix.data());
53
#ifdef DOCDB_DEBUG
54
  if (iter == prefix_to_gen_ht_.end()) {
55
    DOCDB_DEBUG_LOG("DocWriteBatchCache contained no entry for $0",
56
                    BestEffortDocDBKeyToStr(encoded_key_prefix));
57
  } else {
58
    DOCDB_DEBUG_LOG("DocWriteBatchCache entry found for key $0: $1",
59
                    BestEffortDocDBKeyToStr(encoded_key_prefix), EntryToStr(iter->second));
60
  }
61
#endif
62
66.6k
  return iter == prefix_to_gen_ht_.end() ? boost::optional<Entry>() : iter->second;
63
118k
}
64
65
0
string DocWriteBatchCache::ToDebugString() {
66
0
  vector<pair<KeyBuffer, Entry>> sorted_contents;
67
0
  copy(prefix_to_gen_ht_.begin(), prefix_to_gen_ht_.end(), back_inserter(sorted_contents));
68
0
  sort(sorted_contents.begin(), sorted_contents.end());
69
0
  ostringstream ss;
70
0
  ss << "DocWriteBatchCache[" << endl;
71
0
  for (const auto& kv : sorted_contents) {
72
0
    ss << "  " << BestEffortDocDBKeyToStr(kv.first.AsSlice()) << " -> "
73
0
       << EntryToStr(kv.second) << endl;
74
0
  }
75
0
  ss << "]";
76
0
  return ss.str();
77
0
}
78
79
0
string DocWriteBatchCache::EntryToStr(const Entry& entry) {
80
0
  ostringstream ss;
81
0
  ss << "("
82
0
     << entry.doc_hybrid_time.ToString() << ", "
83
0
     << entry.value_type << ", "
84
0
     << ((entry.user_timestamp == Value::kInvalidUserTimestamp) ?
85
0
        string("InvalidUserTimestamp") :
86
0
        std::to_string(entry.user_timestamp)) << ", "
87
0
     << entry.found_exact_key_prefix
88
0
     << ")";
89
0
  return ss.str();
90
0
}
91
92
0
void DocWriteBatchCache::Clear() {
93
0
  prefix_to_gen_ht_.clear();
94
0
}
95
96
}  // namespace docdb
97
}  // namespace yb