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#ifndef STORAGE_LEVELDB_INCLUDE_TABLE_H_ 6#define STORAGE_LEVELDB_INCLUDE_TABLE_H_ 7 8#include <stdint.h> 9#include "leveldb/iterator.h" 10 11namespace leveldb { 12 13class Block; 14class BlockHandle; 15class Footer; 16struct Options; 17class RandomAccessFile; 18struct ReadOptions; 19class TableCache; 20 21// A Table is a sorted map from strings to strings. Tables are 22// immutable and persistent. A Table may be safely accessed from 23// multiple threads without external synchronization. 24class Table { 25 public: 26 // Attempt to open the table that is stored in bytes [0..file_size) 27 // of "file", and read the metadata entries necessary to allow 28 // retrieving data from the table. 29 // 30 // If successful, returns ok and sets "*table" to the newly opened 31 // table. The client should delete "*table" when no longer needed. 32 // If there was an error while initializing the table, sets "*table" 33 // to NULL and returns a non-ok status. Does not take ownership of 34 // "*source", but the client must ensure that "source" remains live 35 // for the duration of the returned table's lifetime. 36 // 37 // *file must remain live while this Table is in use. 38 static Status Open(const Options& options, 39 RandomAccessFile* file, 40 uint64_t file_size, 41 Table** table); 42 43 ~Table(); 44 45 // Returns a new iterator over the table contents. 46 // The result of NewIterator() is initially invalid (caller must 47 // call one of the Seek methods on the iterator before using it). 48 Iterator* NewIterator(const ReadOptions&) const; 49 50 // Given a key, return an approximate byte offset in the file where 51 // the data for that key begins (or would begin if the key were 52 // present in the file). The returned value is in terms of file 53 // bytes, and so includes effects like compression of the underlying data. 54 // E.g., the approximate offset of the last key in the table will 55 // be close to the file length. 56 uint64_t ApproximateOffsetOf(const Slice& key) const; 57 58 private: 59 struct Rep; 60 Rep* rep_; 61 62 explicit Table(Rep* rep) { rep_ = rep; } 63 static Iterator* BlockReader(void*, const ReadOptions&, const Slice&); 64 65 // Calls (*handle_result)(arg, ...) with the entry found after a call 66 // to Seek(key). May not make such a call if filter policy says 67 // that key is not present. 68 friend class TableCache; 69 Status InternalGet( 70 const ReadOptions&, const Slice& key, 71 void* arg, 72 void (*handle_result)(void* arg, const Slice& k, const Slice& v)); 73 74 75 void ReadMeta(const Footer& footer); 76 void ReadFilter(const Slice& filter_handle_value); 77 78 // No copying allowed 79 Table(const Table&); 80 void operator=(const Table&); 81}; 82 83} // namespace leveldb 84 85#endif // STORAGE_LEVELDB_INCLUDE_TABLE_H_ 86