Leveldb源码分析--11

来源:互联网 发布:java 多张图片合成pdf 编辑:程序博客网 时间:2024/05/29 13:31

7 TableCache

这章的内容比较简单,篇幅也不长。

7.1 TableCache简介

TableCache缓存的是Table对象,每个DB一个,它内部使用一个LRUCache缓存所有的table对象,实际上其内容是文件编号{file number, TableAndFile*}。TableAndFile是一个拥有2个变量的结构体:RandomAccessFile*和Table*;

TableCache类的主要成员变量有:

[cpp] view plaincopy
  1. Env* const env_; // 用来操作文件  
  2. const std::string dbname_; // db名  
  3. Cache* cache_;  // LRUCache  

三个函数接口,其中的参数@file_number是文件编号,@file_size是文件大小:

[cpp] view plaincopy
  1. void Evict(uint64_tfile_number);  
  2. // 该函数用以清除指定文件所有cache的entry,函数实现很简单,就是根据file number清除cache对象。  
  3. EncodeFixed64(buf,file_number); cache_->Erase(Slice(buf, sizeof(buf)));  
  4. Iterator* NewIterator(constReadOptions& options, uint64_t file_number,  
  5.                       uint64_t file_size, Table**tableptr = NULL);  
  6. //该函数为指定的file返回一个iterator(对应的文件长度必须是"file_size"字节). 如果tableptr不是NULL,那么*tableptr保存的是底层的Table指针。返回的*tableptr是cache拥有的,不能被删除,生命周期同返回的iterator  
  7. Status Get(constReadOptions& options,  
  8.            uint64_t file_number,uint64_t file_size,  
  9.            const Slice& k,void* arg,  
  10.            void(*handle_result)(void*, const Slice&, const Slice&));  
  11. // 这是一个查找函数,如果在指定文件中seek 到internal key "k" 找到一个entry,就调用 (*handle_result)(arg,found_key, found_value).  

7.2 TableCache::Get()

先来看看Get接口,只有几行代码:

[cpp] view plaincopy
  1. Cache::Handle* handle = NULL;  
  2. Status s =FindTable(file_number, file_size, &handle);  
  3. if (s.ok()) {  
  4.   Table* t =reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;  
  5.   s = t->InternalGet(options,k, arg, saver);  
  6.   cache_->Release(handle);  
  7. }  
  8. return s;  
首先根据file_number找到Table的cache对象,如果找到了就调用Table::InternalGet,对查找结果的处理在调用者传入的saver回调函数中。

Cache在Lookup找到cache对象后,如果不再使用需要调用Release减引用计数。这个见Cache的接口说明。

7.3 TableCache遍历

函数NewIterator(),返回一个可以遍历Table对象的Iterator指针,函数逻辑:

S1 初始化tableptr,调用FindTable,返回cache对象

[cpp] view plaincopy
  1. if (tableptr != NULL) *tableptr =NULL;  
  2. Cache::Handle* handle = NULL;  
  3. Status s =FindTable(file_number, file_size, &handle);  
  4. if (!s.ok()) returnNewErrorIterator(s);  

S2 从cache对象中取出Table对象指针,调用其NewIterator返回Iterator对象,并为Iterator注册一个cleanup函数。

[cpp] view plaincopy
  1. Table* table =reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;  
  2. Iterator* result =table->NewIterator(options);  
  3. result->RegisterCleanup(&UnrefEntry, cache_, handle);  
  4. if (tableptr != NULL) *tableptr= table;  
  5. return result;  

7.4 TableCache::FindTable()

前面的遍历和Get函数都依赖于FindTable这个私有函数完成对cache的查找,下面就来看看该函数的逻辑。函数声明为:

Status FindTable(uint64_t file_number, uint64_t file_size,Cache::Handle** handle)

函数流程为:

S1 首先根据file number从cache中查找table,找到就直接返回成功。

[cpp] view plaincopy
  1. char buf[sizeof(file_number)];  
  2. EncodeFixed64(buf, file_number);  
  3. Slice key(buf, sizeof(buf));  
  4. *handle = cache_->Lookup(key);  

S2 如果没有找到,说明table不在cache中,则根据file number和db name打开一个RadomAccessFile。Table文件格式为:<db name>.<filenumber(%6u)>.sst。如果文件打开成功,则调用Table::Open读取sstable文件。

[cpp] view plaincopy
  1. std::string fname =TableFileName(dbname_, file_number);  
  2. RandomAccessFile* file = NULL;  
  3. Table* table = NULL;  
  4. s =env_->NewRandomAccessFile(fname, &file);  
  5. if (s.ok()) s =Table::Open(*options_, file, file_size, &table);  

S3 如果Table::Open成功则,插入到Cache中。

[cpp] view plaincopy
  1. TableAndFile* tf = newTableAndFile(table, file);  
  2. *handle = cache_->Insert(key,tf, 1, &DeleteEntry);  

如果失败,则删除file,直接返回失败,失败的结果是不会cache的。

7.5 辅助函数

有点啰嗦,不过还是写一下吧。其中一个是为LRUCache注册的删除函数DeleteEntry。

[cpp] view plaincopy
  1. static void DeleteEntry(const Slice& key, void* value) {  
  2.   TableAndFile* tf =reinterpret_cast<TableAndFile*>(value);  
  3.   delete tf->table;  
  4.   delete tf->file;  
  5.   delete tf;  
  6. }  

另外一个是为Iterator注册的清除函数UnrefEntry。

[cpp] view plaincopy
  1. static void UnrefEntry(void* arg1, void* arg2) {  
  2.   Cache* cache =reinterpret_cast<Cache*>(arg1);  
  3.   Cache::Handle* h =reinterpret_cast<Cache::Handle*>(arg2);  
0 0
原创粉丝点击