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// Thread-safe (provides internal synchronization)
6
7#ifndef STORAGE_LEVELDB_DB_TABLE_CACHE_H_
8#define STORAGE_LEVELDB_DB_TABLE_CACHE_H_
9
10#include <cstdint>
11#include <string>
12
13#include "db/dbformat.h"
14#include "leveldb/cache.h"
15#include "leveldb/table.h"
16#include "port/port.h"
17
18namespace leveldb {
19
20class Env;
21
22class TableCache {
23 public:
24 TableCache(const std::string& dbname, const Options& options, int entries);
25
26 TableCache(const TableCache&) = delete;
27 TableCache& operator=(const TableCache&) = delete;
28
29 ~TableCache();
30
31 // Return an iterator for the specified file number (the corresponding
32 // file length must be exactly "file_size" bytes). If "tableptr" is
33 // non-null, also sets "*tableptr" to point to the Table object
34 // underlying the returned iterator, or to nullptr if no Table object
35 // underlies the returned iterator. The returned "*tableptr" object is owned
36 // by the cache and should not be deleted, and is valid for as long as the
37 // returned iterator is live.
38 Iterator* NewIterator(const ReadOptions& options, uint64_t file_number,
39 uint64_t file_size, Table** tableptr = nullptr);
40
41 // If a seek to internal key "k" in specified file finds an entry,
42 // call (*handle_result)(arg, found_key, found_value).
43 Status Get(const ReadOptions& options, uint64_t file_number,
44 uint64_t file_size, const Slice& k, void* arg,
45 void (*handle_result)(void*, const Slice&, const Slice&));
46
47 // Evict any entry for the specified file number
48 void Evict(uint64_t file_number);
49
50 private:
51 Status FindTable(uint64_t file_number, uint64_t file_size, Cache::Handle**);
52
53 Env* const env_;
54 const std::string dbname_;
55 const Options& options_;
56 Cache* cache_;
57};
58
59} // namespace leveldb
60
61#endif // STORAGE_LEVELDB_DB_TABLE_CACHE_H_
62