leveldb
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | Friends | List of all members
leveldb::Table Class Reference

#include <table.h>

Collaboration diagram for leveldb::Table:
Collaboration graph
[legend]

Classes

struct  Rep
 

Public Member Functions

 ~Table ()
 
IteratorNewIterator (const ReadOptions &) const
 
uint64_t ApproximateOffsetOf (const Slice &key) const
 

Static Public Member Functions

static Status Open (const Options &options, RandomAccessFile *file, uint64_t file_size, Table **table)
 

Private Member Functions

 Table (Rep *rep)
 
Status InternalGet (const ReadOptions &, const Slice &key, void *arg, void(*handle_result)(void *arg, const Slice &k, const Slice &v))
 
void ReadMeta (const Footer &footer)
 
void ReadFilter (const Slice &filter_handle_value)
 
 Table (const Table &)
 
void operator= (const Table &)
 

Static Private Member Functions

static IteratorBlockReader (void *, const ReadOptions &, const Slice &)
 

Private Attributes

Reprep_
 

Friends

class TableCache
 

Detailed Description

Definition at line 24 of file table.h.

Constructor & Destructor Documentation

§ ~Table()

leveldb::Table::~Table ( )

Definition at line 143 of file table.cc.

143  {
144  delete rep_;
145 }
Rep * rep_
Definition: table.h:59

§ Table() [1/2]

leveldb::Table::Table ( Rep rep)
inlineexplicitprivate

Definition at line 62 of file table.h.

62 { rep_ = rep; }
Rep * rep_
Definition: table.h:59
Here is the call graph for this function:
Here is the caller graph for this function:

§ Table() [2/2]

leveldb::Table::Table ( const Table )
private

Member Function Documentation

§ ApproximateOffsetOf()

uint64_t leveldb::Table::ApproximateOffsetOf ( const Slice key) const

Definition at line 258 of file table.cc.

258  {
259  Iterator* index_iter =
261  index_iter->Seek(key);
262  uint64_t result;
263  if (index_iter->Valid()) {
264  BlockHandle handle;
265  Slice input = index_iter->value();
266  Status s = handle.DecodeFrom(&input);
267  if (s.ok()) {
268  result = handle.offset();
269  } else {
270  // Strange: we can't decode the block handle in the index block.
271  // We'll just return the offset of the metaindex block, which is
272  // close to the whole file size for this case.
273  result = rep_->metaindex_handle.offset();
274  }
275  } else {
276  // key is past the last key in the file. Approximate the offset
277  // by returning the offset of the metaindex block (which is
278  // right near the end of the file).
279  result = rep_->metaindex_handle.offset();
280  }
281  delete index_iter;
282  return result;
283 }
Rep * rep_
Definition: table.h:59
BlockHandle metaindex_handle
Definition: table.cc:34
virtual void Seek(const Slice &target)=0
Iterator * NewIterator(const Comparator *comparator)
Definition: block.cc:256
Options options
Definition: table.cc:27
Block * index_block
Definition: table.cc:35
const Comparator * comparator
Definition: options.h:41
uint64_t offset() const
Definition: format.h:27
Here is the call graph for this function:
Here is the caller graph for this function:

§ BlockReader()

Iterator * leveldb::Table::BlockReader ( void *  arg,
const ReadOptions options,
const Slice index_value 
)
staticprivate

Definition at line 164 of file table.cc.

166  {
167  Table* table = reinterpret_cast<Table*>(arg);
168  Cache* block_cache = table->rep_->options.block_cache;
169  Block* block = NULL;
170  Cache::Handle* cache_handle = NULL;
171 
172  BlockHandle handle;
173  Slice input = index_value;
174  Status s = handle.DecodeFrom(&input);
175  // We intentionally allow extra stuff in index_value so that we
176  // can add more features in the future.
177 
178  if (s.ok()) {
179  BlockContents contents;
180  if (block_cache != NULL) {
181  char cache_key_buffer[16];
182  EncodeFixed64(cache_key_buffer, table->rep_->cache_id);
183  EncodeFixed64(cache_key_buffer+8, handle.offset());
184  Slice key(cache_key_buffer, sizeof(cache_key_buffer));
185  cache_handle = block_cache->Lookup(key);
186  if (cache_handle != NULL) {
187  block = reinterpret_cast<Block*>(block_cache->Value(cache_handle));
188  } else {
189  s = ReadBlock(table->rep_->file, options, handle, &contents);
190  if (s.ok()) {
191  block = new Block(contents);
192  if (contents.cachable && options.fill_cache) {
193  cache_handle = block_cache->Insert(
194  key, block, block->size(), &DeleteCachedBlock);
195  }
196  }
197  }
198  } else {
199  s = ReadBlock(table->rep_->file, options, handle, &contents);
200  if (s.ok()) {
201  block = new Block(contents);
202  }
203  }
204  }
205 
206  Iterator* iter;
207  if (block != NULL) {
208  iter = block->NewIterator(table->rep_->options.comparator);
209  if (cache_handle == NULL) {
210  iter->RegisterCleanup(&DeleteBlock, block, NULL);
211  } else {
212  iter->RegisterCleanup(&ReleaseBlock, block_cache, cache_handle);
213  }
214  } else {
215  iter = NewErrorIterator(s);
216  }
217  return iter;
218 }
Table(Rep *rep)
Definition: table.h:62
void EncodeFixed64(char *buf, uint64_t value)
Definition: coding.cc:20
static void ReleaseBlock(void *arg, void *h)
Definition: table.cc:156
Iterator * NewErrorIterator(const Status &status)
Definition: iterator.cc:63
static void DeleteCachedBlock(const Slice &key, void *value)
Definition: table.cc:151
static void DeleteBlock(void *arg, void *ignored)
Definition: table.cc:147
Status ReadBlock(RandomAccessFile *file, const ReadOptions &options, const BlockHandle &handle, BlockContents *result)
Definition: format.cc:65
Here is the call graph for this function:
Here is the caller graph for this function:

§ InternalGet()

Status leveldb::Table::InternalGet ( const ReadOptions ,
const Slice key,
void *  arg,
void(*)(void *arg, const Slice &k, const Slice &v)  handle_result 
)
private

Definition at line 226 of file table.cc.

228  {
229  Status s;
230  Iterator* iiter = rep_->index_block->NewIterator(rep_->options.comparator);
231  iiter->Seek(k);
232  if (iiter->Valid()) {
233  Slice handle_value = iiter->value();
234  FilterBlockReader* filter = rep_->filter;
235  BlockHandle handle;
236  if (filter != NULL &&
237  handle.DecodeFrom(&handle_value).ok() &&
238  !filter->KeyMayMatch(handle.offset(), k)) {
239  // Not found
240  } else {
241  Iterator* block_iter = BlockReader(this, options, iiter->value());
242  block_iter->Seek(k);
243  if (block_iter->Valid()) {
244  (*saver)(arg, block_iter->key(), block_iter->value());
245  }
246  s = block_iter->status();
247  delete block_iter;
248  }
249  }
250  if (s.ok()) {
251  s = iiter->status();
252  }
253  delete iiter;
254  return s;
255 }
Rep * rep_
Definition: table.h:59
virtual void Seek(const Slice &target)=0
Iterator * NewIterator(const Comparator *comparator)
Definition: block.cc:256
static Iterator * BlockReader(void *, const ReadOptions &, const Slice &)
Definition: table.cc:164
Options options
Definition: table.cc:27
Block * index_block
Definition: table.cc:35
const Comparator * comparator
Definition: options.h:41
FilterBlockReader * filter
Definition: table.cc:31
Here is the call graph for this function:
Here is the caller graph for this function:

§ NewIterator()

Iterator * leveldb::Table::NewIterator ( const ReadOptions options) const

Definition at line 220 of file table.cc.

220  {
221  return NewTwoLevelIterator(
223  &Table::BlockReader, const_cast<Table*>(this), options);
224 }
Table(Rep *rep)
Definition: table.h:62
Rep * rep_
Definition: table.h:59
Iterator * NewIterator(const Comparator *comparator)
Definition: block.cc:256
static Iterator * BlockReader(void *, const ReadOptions &, const Slice &)
Definition: table.cc:164
Options options
Definition: table.cc:27
Block * index_block
Definition: table.cc:35
Iterator * NewTwoLevelIterator(Iterator *index_iter, BlockFunction block_function, void *arg, const ReadOptions &options)
const Comparator * comparator
Definition: options.h:41
Here is the call graph for this function:
Here is the caller graph for this function:

§ Open()

Status leveldb::Table::Open ( const Options options,
RandomAccessFile file,
uint64_t  file_size,
Table **  table 
)
static

Definition at line 38 of file table.cc.

41  {
42  *table = NULL;
43  if (size < Footer::kEncodedLength) {
44  return Status::Corruption("file is too short to be an sstable");
45  }
46 
47  char footer_space[Footer::kEncodedLength];
48  Slice footer_input;
49  Status s = file->Read(size - Footer::kEncodedLength, Footer::kEncodedLength,
50  &footer_input, footer_space);
51  if (!s.ok()) return s;
52 
53  Footer footer;
54  s = footer.DecodeFrom(&footer_input);
55  if (!s.ok()) return s;
56 
57  // Read the index block
58  BlockContents contents;
59  Block* index_block = NULL;
60  if (s.ok()) {
61  ReadOptions opt;
62  if (options.paranoid_checks) {
63  opt.verify_checksums = true;
64  }
65  s = ReadBlock(file, opt, footer.index_handle(), &contents);
66  if (s.ok()) {
67  index_block = new Block(contents);
68  }
69  }
70 
71  if (s.ok()) {
72  // We've successfully read the footer and the index block: we're
73  // ready to serve requests.
74  Rep* rep = new Table::Rep;
75  rep->options = options;
76  rep->file = file;
77  rep->metaindex_handle = footer.metaindex_handle();
78  rep->index_block = index_block;
79  rep->cache_id = (options.block_cache ? options.block_cache->NewId() : 0);
80  rep->filter_data = NULL;
81  rep->filter = NULL;
82  *table = new Table(rep);
83  (*table)->ReadMeta(footer);
84  } else {
85  delete index_block;
86  }
87 
88  return s;
89 }
Table(Rep *rep)
Definition: table.h:62
static Status Corruption(const Slice &msg, const Slice &msg2=Slice())
Definition: status.h:38
Status ReadBlock(RandomAccessFile *file, const ReadOptions &options, const BlockHandle &handle, BlockContents *result)
Definition: format.cc:65
Here is the call graph for this function:
Here is the caller graph for this function:

§ operator=()

void leveldb::Table::operator= ( const Table )
private

§ ReadFilter()

void leveldb::Table::ReadFilter ( const Slice filter_handle_value)
private

Definition at line 120 of file table.cc.

120  {
121  Slice v = filter_handle_value;
122  BlockHandle filter_handle;
123  if (!filter_handle.DecodeFrom(&v).ok()) {
124  return;
125  }
126 
127  // We might want to unify with ReadBlock() if we start
128  // requiring checksum verification in Table::Open.
129  ReadOptions opt;
131  opt.verify_checksums = true;
132  }
133  BlockContents block;
134  if (!ReadBlock(rep_->file, opt, filter_handle, &block).ok()) {
135  return;
136  }
137  if (block.heap_allocated) {
138  rep_->filter_data = block.data.data(); // Will need to delete later
139  }
140  rep_->filter = new FilterBlockReader(rep_->options.filter_policy, block.data);
141 }
Rep * rep_
Definition: table.h:59
Options options
Definition: table.cc:27
const FilterPolicy * filter_policy
Definition: options.h:154
bool paranoid_checks
Definition: options.h:57
bool ok() const
Definition: status.h:52
FilterBlockReader * filter
Definition: table.cc:31
RandomAccessFile * file
Definition: table.cc:29
const char * filter_data
Definition: table.cc:32
Status ReadBlock(RandomAccessFile *file, const ReadOptions &options, const BlockHandle &handle, BlockContents *result)
Definition: format.cc:65
Here is the call graph for this function:
Here is the caller graph for this function:

§ ReadMeta()

void leveldb::Table::ReadMeta ( const Footer footer)
private

Definition at line 91 of file table.cc.

91  {
92  if (rep_->options.filter_policy == NULL) {
93  return; // Do not need any metadata
94  }
95 
96  // TODO(sanjay): Skip this if footer.metaindex_handle() size indicates
97  // it is an empty block.
98  ReadOptions opt;
100  opt.verify_checksums = true;
101  }
102  BlockContents contents;
103  if (!ReadBlock(rep_->file, opt, footer.metaindex_handle(), &contents).ok()) {
104  // Do not propagate errors since meta info is not needed for operation
105  return;
106  }
107  Block* meta = new Block(contents);
108 
109  Iterator* iter = meta->NewIterator(BytewiseComparator());
110  std::string key = "filter.";
111  key.append(rep_->options.filter_policy->Name());
112  iter->Seek(key);
113  if (iter->Valid() && iter->key() == Slice(key)) {
114  ReadFilter(iter->value());
115  }
116  delete iter;
117  delete meta;
118 }
Rep * rep_
Definition: table.h:59
virtual const char * Name() const =0
Options options
Definition: table.cc:27
const Comparator * BytewiseComparator()
Definition: comparator.cc:76
bool paranoid_checks
Definition: options.h:57
const FilterPolicy * filter_policy
Definition: options.h:154
void ReadFilter(const Slice &filter_handle_value)
Definition: table.cc:120
RandomAccessFile * file
Definition: table.cc:29
Status ReadBlock(RandomAccessFile *file, const ReadOptions &options, const BlockHandle &handle, BlockContents *result)
Definition: format.cc:65
Here is the call graph for this function:

Friends And Related Function Documentation

§ TableCache

friend class TableCache
friend

Definition at line 68 of file table.h.

Member Data Documentation

§ rep_

Rep* leveldb::Table::rep_
private

Definition at line 59 of file table.h.


The documentation for this class was generated from the following files: