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