| 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
| Branch | True | False | Total |
| delete tf->table; |
1 |
0 |
1 |
|
| 22 |
1 |
▶ | delete tf->file; |
Branch: 1 branch
| Branch | True | False | Total |
| delete tf->file; |
1 |
0 |
1 |
|
| 23 |
1 |
▶ | delete tf; |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| cache_(NewLRUCache(entries)) { |
4 |
0 |
4 |
|
| 39 |
4 |
| } |
| 40 |
|
| |
| 41 |
8 |
| TableCache::~TableCache() { |
| 42 |
4 |
▶ | delete cache_; |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| Cache::Handle* handle = cache_->Lookup(key); |
2 |
0 |
2 |
|
| 57 |
2 |
▶ | if (handle == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (handle == NULL) { |
1 |
1 |
2 |
|
| 58 |
1 |
▶ | std::string fname = TableFileName(dbname_, file_number); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| Status s = env_->NewRandomAccessFile(fname, &file); |
1 |
0 |
1 |
|
| 62 |
1 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
1 |
0 |
1 |
|
| 63 |
1 |
▶ | s = Table::Open(*options_, file, file_size, &table); |
Branch: 2 branches
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (!s.ok()) { |
0 |
1 |
1 |
|
| 67 |
0 |
▶ | assert(table == NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(table == NULL); |
0 |
0 |
0 |
|
| 68 |
0 |
▶ | delete file; |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| return NewErrorIterator(s); |
0 |
0 |
0 |
|
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
1 |
▶ | TableAndFile* tf = new TableAndFile; |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| handle = cache_->Insert(key, tf, 1, &DeleteEntry); |
1 |
0 |
1 |
|
| 78 |
1 |
▶ | } |
Branch: 2 branches
| Branch | True | False | Total |
| } |
1 |
0 |
1 |
| } |
1 |
0 |
1 |
|
| 79 |
|
| |
| 80 |
2 |
▶ | Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table; |
Branch: 1 branch
| Branch | True | False | Total |
| Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table; |
2 |
0 |
2 |
|
| 81 |
2 |
▶ | Iterator* result = table->NewIterator(options); |
Branch: 1 branch
| Branch | True | False | Total |
| Iterator* result = table->NewIterator(options); |
2 |
0 |
2 |
|
| 82 |
2 |
▶ | result->RegisterCleanup(&UnrefEntry, cache_, handle); |
Branch: 1 branch
| Branch | True | False | Total |
| result->RegisterCleanup(&UnrefEntry, cache_, handle); |
2 |
0 |
2 |
|
| 83 |
2 |
▶ | if (tableptr != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| EncodeFixed64(buf, file_number); |
0 |
0 |
0 |
|
| 92 |
0 |
▶ | cache_->Erase(Slice(buf, sizeof(buf))); |
Branch: 1 branch
| Branch | True | False | Total |
| cache_->Erase(Slice(buf, sizeof(buf))); |
0 |
0 |
0 |
|
| 93 |
0 |
| } |
| 94 |
|
| |
| 95 |
|
| } |