1179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Use of this source code is governed by a BSD-style license that can be
3179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// found in the LICENSE file. See the AUTHORS file for names of contributors.
4179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
5179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#ifndef STORAGE_LEVELDB_INCLUDE_TABLE_H_
6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#define STORAGE_LEVELDB_INCLUDE_TABLE_H_
7179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
8179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include <stdint.h>
91ca60b12c68a71aac695b15e329b2a76a63cbb0ajorlow@chromium.org#include "leveldb/iterator.h"
10179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
11179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace leveldb {
12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass Block;
14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass BlockHandle;
1599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.comclass Footer;
16179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstruct Options;
17179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass RandomAccessFile;
18179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstruct ReadOptions;
1999a7585544fc162a5f8dd39a6add00776a981efesanjay@google.comclass TableCache;
20179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
21179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// A Table is a sorted map from strings to strings.  Tables are
22a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// immutable and persistent.  A Table may be safely accessed from
23a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// multiple threads without external synchronization.
24179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass Table {
25179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org public:
26f85ede82f8c27a00c3120f67fbab89b2a89fe987jorlow@chromium.org  // Attempt to open the table that is stored in bytes [0..file_size)
27f85ede82f8c27a00c3120f67fbab89b2a89fe987jorlow@chromium.org  // of "file", and read the metadata entries necessary to allow
28f85ede82f8c27a00c3120f67fbab89b2a89fe987jorlow@chromium.org  // retrieving data from the table.
29179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //
30179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // If successful, returns ok and sets "*table" to the newly opened
31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // table.  The client should delete "*table" when no longer needed.
32179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // If there was an error while initializing the table, sets "*table"
33179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // to NULL and returns a non-ok status.  Does not take ownership of
34179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // "*source", but the client must ensure that "source" remains live
35179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // for the duration of the returned table's lifetime.
36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  //
37179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // *file must remain live while this Table is in use.
38179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  static Status Open(const Options& options,
39179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org                     RandomAccessFile* file,
40f85ede82f8c27a00c3120f67fbab89b2a89fe987jorlow@chromium.org                     uint64_t file_size,
41179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org                     Table** table);
42179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
43179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  ~Table();
44179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
45179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Returns a new iterator over the table contents.
46179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // The result of NewIterator() is initially invalid (caller must
47179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // call one of the Seek methods on the iterator before using it).
48179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  Iterator* NewIterator(const ReadOptions&) const;
49179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
50179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Given a key, return an approximate byte offset in the file where
51179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // the data for that key begins (or would begin if the key were
52179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // present in the file).  The returned value is in terms of file
53179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // bytes, and so includes effects like compression of the underlying data.
54179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // E.g., the approximate offset of the last key in the table will
55179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // be close to the file length.
56179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  uint64_t ApproximateOffsetOf(const Slice& key) const;
57179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
58179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org private:
59179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  struct Rep;
60179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  Rep* rep_;
61179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
62179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  explicit Table(Rep* rep) { rep_ = rep; }
63179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  static Iterator* BlockReader(void*, const ReadOptions&, const Slice&);
64179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
6599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  // Calls (*handle_result)(arg, ...) with the entry found after a call
6699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  // to Seek(key).  May not make such a call if filter policy says
6799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  // that key is not present.
6899a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  friend class TableCache;
6999a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  Status InternalGet(
7099a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      const ReadOptions&, const Slice& key,
7199a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      void* arg,
7299a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com      void (*handle_result)(void* arg, const Slice& k, const Slice& v));
7399a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
7499a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
7599a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  void ReadMeta(const Footer& footer);
7699a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com  void ReadFilter(const Slice& filter_handle_value);
7799a7585544fc162a5f8dd39a6add00776a981efesanjay@google.com
78179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // No copying allowed
79179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  Table(const Table&);
80179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void operator=(const Table&);
81179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org};
82179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
8345b9940be332834440bd5299419f396e38085ebehans@chromium.org}  // namespace leveldb
84179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
85179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#endif  // STORAGE_LEVELDB_INCLUDE_TABLE_H_
86