db/table_cache.cc

filter: Statementstmt 83.0% 44 / 53 Branchbran 42.8% 24 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 62.3% 68 / 109 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« table/table_builder.cc ↑ Summary util/status.cc »
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/table_cache.h"
6  
7 #include "db/filename.h"
8 #include "leveldb/env.h"
9 #include "leveldb/table.h"
10 #include "util/coding.h"
11  
12 namespace leveldb {
13  
14 struct TableAndFile {
15 RandomAccessFile* file;
16 Table* table;
17 };
18  
19 1 static void DeleteEntry(const Slice& key, void* value) {
20 1 TableAndFile* tf = reinterpret_cast<TableAndFile*>(value);
21 1 ▶ delete tf->table;
Branch: 1 branch
BranchTrueFalseTotal
delete tf->table; 1 0 1
22 1 ▶ delete tf->file;
Branch: 1 branch
BranchTrueFalseTotal
delete tf->file; 1 0 1
23 1 ▶ delete tf;
Branch: 1 branch
BranchTrueFalseTotal
delete tf; 1 0 1
24 1 }
25  
26 2 static void UnrefEntry(void* arg1, void* arg2) {
27 2 Cache* cache = reinterpret_cast<Cache*>(arg1);
28 2 Cache::Handle* h = reinterpret_cast<Cache::Handle*>(arg2);
29 2 cache->Release(h);
30 2 }
31  
32 4 TableCache::TableCache(const std::string& dbname,
33 const Options* options,
34 4 int entries)
35 4 : env_(options->env),
36 4 dbname_(dbname),
37 4 options_(options),
38 4 ▶ cache_(NewLRUCache(entries)) {
Branch: 1 branch
BranchTrueFalseTotal
cache_(NewLRUCache(entries)) { 4 0 4
39 4 }
40  
41 8 TableCache::~TableCache() {
42 4 ▶ delete cache_;
Branch: 1 branch
BranchTrueFalseTotal
delete cache_; 4 0 4
43 4 }
44  
45 2 Iterator* TableCache::NewIterator(const ReadOptions& options,
46 uint64_t file_number,
47 uint64_t file_size,
48 Table** tableptr) {
49 2 ▶ if (tableptr != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tableptr != NULL) { 0 2 2
50 0 *tableptr = NULL;
51 }
52  
53 char buf[sizeof(file_number)];
54 2 ▶ EncodeFixed64(buf, file_number);
Branch: 1 branch
BranchTrueFalseTotal
EncodeFixed64(buf, file_number); 2 0 2
55 2 Slice key(buf, sizeof(buf));
56 2 ▶ Cache::Handle* handle = cache_->Lookup(key);
Branch: 1 branch
BranchTrueFalseTotal
Cache::Handle* handle = cache_->Lookup(key); 2 0 2
57 2 ▶ if (handle == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (handle == NULL) { 1 1 2
58 1 ▶ std::string fname = TableFileName(dbname_, file_number);
Branch: 1 branch
BranchTrueFalseTotal
std::string fname = TableFileName(dbname_, file_number); 1 0 1
59 1 RandomAccessFile* file = NULL;
60 1 Table* table = NULL;
61 1 ▶ Status s = env_->NewRandomAccessFile(fname, &file);
Branch: 1 branch
BranchTrueFalseTotal
Status s = env_->NewRandomAccessFile(fname, &file); 1 0 1
62 1 ▶ if (s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (s.ok()) { 1 0 1
63 1 ▶ s = Table::Open(*options_, file, file_size, &table);
Branch: 2 branches
BranchTrueFalseTotal
s = Table::Open(*options_, file, file_size, &table); 1 0 1
s = Table::Open(*options_, file, file_size, &table); 1 0 1
64 }
65  
66 1 ▶ if (!s.ok()) {
Branch: 1 branch
BranchTrueFalseTotal
if (!s.ok()) { 0 1 1
67 0 ▶ assert(table == NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(table == NULL); 0 0 0
68 0 ▶ delete file;
Branch: 1 branch
BranchTrueFalseTotal
delete file; 0 0 0
69 // We do not cache error results so that if the error is transient,
70 // or somebody repairs the file, we recover automatically.
71 0 ▶ return NewErrorIterator(s);
Branch: 1 branch
BranchTrueFalseTotal
return NewErrorIterator(s); 0 0 0
72 }
73  
74 1 ▶ TableAndFile* tf = new TableAndFile;
Branch: 1 branch
BranchTrueFalseTotal
TableAndFile* tf = new TableAndFile; 1 0 1
75 1 tf->file = file;
76 1 tf->table = table;
77 1 ▶ handle = cache_->Insert(key, tf, 1, &DeleteEntry);
Branch: 1 branch
BranchTrueFalseTotal
handle = cache_->Insert(key, tf, 1, &DeleteEntry); 1 0 1
78 1 ▶ }
Branch: 2 branches
BranchTrueFalseTotal
} 1 0 1
} 1 0 1
79  
80 2 ▶ Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
Branch: 1 branch
BranchTrueFalseTotal
Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table; 2 0 2
81 2 ▶ Iterator* result = table->NewIterator(options);
Branch: 1 branch
BranchTrueFalseTotal
Iterator* result = table->NewIterator(options); 2 0 2
82 2 ▶ result->RegisterCleanup(&UnrefEntry, cache_, handle);
Branch: 1 branch
BranchTrueFalseTotal
result->RegisterCleanup(&UnrefEntry, cache_, handle); 2 0 2
83 2 ▶ if (tableptr != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tableptr != NULL) { 0 2 2
84 0 *tableptr = table;
85 }
86 2 return result;
87 }
88  
89 0 void TableCache::Evict(uint64_t file_number) {
90 char buf[sizeof(file_number)];
91 0 ▶ EncodeFixed64(buf, file_number);
Branch: 1 branch
BranchTrueFalseTotal
EncodeFixed64(buf, file_number); 0 0 0
92 0 ▶ cache_->Erase(Slice(buf, sizeof(buf)));
Branch: 1 branch
BranchTrueFalseTotal
cache_->Erase(Slice(buf, sizeof(buf))); 0 0 0
93 0 }
94  
95 }
« table/table_builder.cc ↑ Summary util/status.cc »