LCOV - code coverage report
Current view: top level - src/leveldb/db - table_cache.cc (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 91.8 % 73 67
Test Date: 2026-02-04 05:05:50 Functions: 100.0 % 8 8
Branches: 50.0 % 76 38

             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                 :        4142 : static void DeleteEntry(const Slice& key, void* value) {
      20                 :        4142 :   TableAndFile* tf = reinterpret_cast<TableAndFile*>(value);
      21         [ +  - ]:        4142 :   delete tf->table;
      22         [ +  - ]:        4142 :   delete tf->file;
      23         [ +  - ]:        4142 :   delete tf;
      24                 :        4142 : }
      25                 :             : 
      26                 :        6992 : static void UnrefEntry(void* arg1, void* arg2) {
      27                 :        6992 :   Cache* cache = reinterpret_cast<Cache*>(arg1);
      28                 :        6992 :   Cache::Handle* h = reinterpret_cast<Cache::Handle*>(arg2);
      29                 :        6992 :   cache->Release(h);
      30                 :        6992 : }
      31                 :             : 
      32                 :        2854 : TableCache::TableCache(const std::string& dbname, const Options& options,
      33                 :        2854 :                        int entries)
      34                 :        2854 :     : env_(options.env),
      35         [ -  + ]:        2854 :       dbname_(dbname),
      36                 :        2854 :       options_(options),
      37         [ +  - ]:        2854 :       cache_(NewLRUCache(entries)) {}
      38                 :             : 
      39         [ +  - ]:        2853 : TableCache::~TableCache() { delete cache_; }
      40                 :             : 
      41                 :     2223820 : Status TableCache::FindTable(uint64_t file_number, uint64_t file_size,
      42                 :             :                              Cache::Handle** handle) {
      43                 :     2223820 :   Status s;
      44                 :     2223820 :   char buf[sizeof(file_number)];
      45                 :     2223820 :   EncodeFixed64(buf, file_number);
      46         [ +  - ]:     2223820 :   Slice key(buf, sizeof(buf));
      47         [ +  - ]:     2223820 :   *handle = cache_->Lookup(key);
      48         [ +  + ]:     2223820 :   if (*handle == nullptr) {
      49         [ +  - ]:        4145 :     std::string fname = TableFileName(dbname_, file_number);
      50                 :        4145 :     RandomAccessFile* file = nullptr;
      51                 :        4145 :     Table* table = nullptr;
      52   [ +  -  -  + ]:        4145 :     s = env_->NewRandomAccessFile(fname, &file);
      53         [ -  + ]:        4145 :     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                 :             :       }
      58                 :           0 :     }
      59         [ +  - ]:        4145 :     if (s.ok()) {
      60   [ +  -  -  + ]:        4145 :       s = Table::Open(options_, file, file_size, &table);
      61                 :             :     }
      62                 :             : 
      63         [ +  + ]:        4145 :     if (!s.ok()) {
      64         [ -  + ]:           1 :       assert(table == nullptr);
      65         [ +  - ]:           1 :       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                 :             :     } else {
      69         [ +  - ]:        4144 :       TableAndFile* tf = new TableAndFile;
      70                 :        4144 :       tf->file = file;
      71                 :        4144 :       tf->table = table;
      72         [ +  - ]:        4144 :       *handle = cache_->Insert(key, tf, 1, &DeleteEntry);
      73                 :             :     }
      74                 :        4145 :   }
      75                 :     2223820 :   return s;
      76                 :           0 : }
      77                 :             : 
      78                 :        6993 : Iterator* TableCache::NewIterator(const ReadOptions& options,
      79                 :             :                                   uint64_t file_number, uint64_t file_size,
      80                 :             :                                   Table** tableptr) {
      81         [ +  + ]:        6993 :   if (tableptr != nullptr) {
      82                 :         164 :     *tableptr = nullptr;
      83                 :             :   }
      84                 :             : 
      85                 :        6993 :   Cache::Handle* handle = nullptr;
      86                 :        6993 :   Status s = FindTable(file_number, file_size, &handle);
      87         [ +  + ]:        6993 :   if (!s.ok()) {
      88         [ +  - ]:           1 :     return NewErrorIterator(s);
      89                 :             :   }
      90                 :             : 
      91         [ +  - ]:        6992 :   Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
      92         [ +  - ]:        6992 :   Iterator* result = table->NewIterator(options);
      93         [ +  - ]:        6992 :   result->RegisterCleanup(&UnrefEntry, cache_, handle);
      94         [ +  + ]:        6992 :   if (tableptr != nullptr) {
      95                 :         164 :     *tableptr = table;
      96                 :             :   }
      97                 :             :   return result;
      98                 :        6993 : }
      99                 :             : 
     100                 :     2216827 : 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                 :             :                                              const Slice&)) {
     104                 :     2216827 :   Cache::Handle* handle = nullptr;
     105                 :     2216827 :   Status s = FindTable(file_number, file_size, &handle);
     106         [ +  - ]:     2216827 :   if (s.ok()) {
     107         [ +  - ]:     2216827 :     Table* t = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
     108   [ +  -  -  + ]:     2216827 :     s = t->InternalGet(options, k, arg, handle_result);
     109         [ +  - ]:     2216827 :     cache_->Release(handle);
     110                 :             :   }
     111                 :     2216827 :   return s;
     112                 :           0 : }
     113                 :             : 
     114                 :        1092 : void TableCache::Evict(uint64_t file_number) {
     115                 :        1092 :   char buf[sizeof(file_number)];
     116                 :        1092 :   EncodeFixed64(buf, file_number);
     117                 :        1092 :   cache_->Erase(Slice(buf, sizeof(buf)));
     118                 :        1092 : }
     119                 :             : 
     120                 :             : }  // namespace leveldb
        

Generated by: LCOV version 2.0-1