table.h 2.9 KB

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