LCOV - code coverage report
Current view: top level - src/leveldb/db - table_cache.cc (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 67 77 87.0 %
Date: 2022-04-21 14:51:19 Functions: 8 8 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 11 18 61.1 %

           Branch data     Line data    Source code
#       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                 :       1923 : static void DeleteEntry(const Slice& key, void* value) {
#      20                 :       1923 :   TableAndFile* tf = reinterpret_cast<TableAndFile*>(value);
#      21                 :       1923 :   delete tf->table;
#      22                 :       1923 :   delete tf->file;
#      23                 :       1923 :   delete tf;
#      24                 :       1923 : }
#      25                 :            : 
#      26                 :       3253 : static void UnrefEntry(void* arg1, void* arg2) {
#      27                 :       3253 :   Cache* cache = reinterpret_cast<Cache*>(arg1);
#      28                 :       3253 :   Cache::Handle* h = reinterpret_cast<Cache::Handle*>(arg2);
#      29                 :       3253 :   cache->Release(h);
#      30                 :       3253 : }
#      31                 :            : 
#      32                 :            : TableCache::TableCache(const std::string& dbname, const Options& options,
#      33                 :            :                        int entries)
#      34                 :            :     : env_(options.env),
#      35                 :            :       dbname_(dbname),
#      36                 :            :       options_(options),
#      37                 :       2193 :       cache_(NewLRUCache(entries)) {}
#      38                 :            : 
#      39                 :       2193 : TableCache::~TableCache() { delete cache_; }
#      40                 :            : 
#      41                 :            : Status TableCache::FindTable(uint64_t file_number, uint64_t file_size,
#      42                 :    3386212 :                              Cache::Handle** handle) {
#      43                 :    3386212 :   Status s;
#      44                 :    3386212 :   char buf[sizeof(file_number)];
#      45                 :    3386212 :   EncodeFixed64(buf, file_number);
#      46                 :    3386212 :   Slice key(buf, sizeof(buf));
#      47                 :    3386212 :   *handle = cache_->Lookup(key);
#      48         [ +  + ]:    3386212 :   if (*handle == nullptr) {
#      49                 :       1923 :     std::string fname = TableFileName(dbname_, file_number);
#      50                 :       1923 :     RandomAccessFile* file = nullptr;
#      51                 :       1923 :     Table* table = nullptr;
#      52                 :       1923 :     s = env_->NewRandomAccessFile(fname, &file);
#      53         [ -  + ]:       1923 :     if (!s.ok()) {
#      54                 :          0 :       std::string old_fname = SSTTableFileName(dbname_, file_number);
#      55         [ #  # ]:          0 :       if (env_->NewRandomAccessFile(old_fname, &file).ok()) {
#      56                 :          0 :         s = Status::OK();
#      57                 :          0 :       }
#      58                 :          0 :     }
#      59         [ +  - ]:       1923 :     if (s.ok()) {
#      60                 :       1923 :       s = Table::Open(options_, file, file_size, &table);
#      61                 :       1923 :     }
#      62                 :            : 
#      63         [ -  + ]:       1923 :     if (!s.ok()) {
#      64                 :          0 :       assert(table == nullptr);
#      65                 :          0 :       delete file;
#      66                 :            :       // We do not cache error results so that if the error is transient,
#      67                 :            :       // or somebody repairs the file, we recover automatically.
#      68                 :       1923 :     } else {
#      69                 :       1923 :       TableAndFile* tf = new TableAndFile;
#      70                 :       1923 :       tf->file = file;
#      71                 :       1923 :       tf->table = table;
#      72                 :       1923 :       *handle = cache_->Insert(key, tf, 1, &DeleteEntry);
#      73                 :       1923 :     }
#      74                 :       1923 :   }
#      75                 :          0 :   return s;
#      76                 :    3386212 : }
#      77                 :            : 
#      78                 :            : Iterator* TableCache::NewIterator(const ReadOptions& options,
#      79                 :            :                                   uint64_t file_number, uint64_t file_size,
#      80                 :       3253 :                                   Table** tableptr) {
#      81         [ +  + ]:       3253 :   if (tableptr != nullptr) {
#      82                 :         16 :     *tableptr = nullptr;
#      83                 :         16 :   }
#      84                 :            : 
#      85                 :       3253 :   Cache::Handle* handle = nullptr;
#      86                 :       3253 :   Status s = FindTable(file_number, file_size, &handle);
#      87         [ -  + ]:       3253 :   if (!s.ok()) {
#      88                 :          0 :     return NewErrorIterator(s);
#      89                 :          0 :   }
#      90                 :            : 
#      91                 :       3253 :   Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
#      92                 :       3253 :   Iterator* result = table->NewIterator(options);
#      93                 :       3253 :   result->RegisterCleanup(&UnrefEntry, cache_, handle);
#      94         [ +  + ]:       3253 :   if (tableptr != nullptr) {
#      95                 :         16 :     *tableptr = table;
#      96                 :         16 :   }
#      97                 :       3253 :   return result;
#      98                 :       3253 : }
#      99                 :            : 
#     100                 :            : Status TableCache::Get(const ReadOptions& options, uint64_t file_number,
#     101                 :            :                        uint64_t file_size, const Slice& k, void* arg,
#     102                 :            :                        void (*handle_result)(void*, const Slice&,
#     103                 :    3382960 :                                              const Slice&)) {
#     104                 :    3382960 :   Cache::Handle* handle = nullptr;
#     105                 :    3382960 :   Status s = FindTable(file_number, file_size, &handle);
#     106         [ +  - ]:    3382960 :   if (s.ok()) {
#     107                 :    3382960 :     Table* t = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
#     108                 :    3382960 :     s = t->InternalGet(options, k, arg, handle_result);
#     109                 :    3382960 :     cache_->Release(handle);
#     110                 :    3382960 :   }
#     111                 :    3382960 :   return s;
#     112                 :    3382960 : }
#     113                 :            : 
#     114                 :        403 : void TableCache::Evict(uint64_t file_number) {
#     115                 :        403 :   char buf[sizeof(file_number)];
#     116                 :        403 :   EncodeFixed64(buf, file_number);
#     117                 :        403 :   cache_->Erase(Slice(buf, sizeof(buf)));
#     118                 :        403 : }
#     119                 :            : 
#     120                 :            : }  // namespace leveldb

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a