作者: 韩晨旭 10225101440 李畅 10225102463
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.6 KiB

  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. #include "db/table_cache.h"
  5. #include "db/filename.h"
  6. #include "leveldb/env.h"
  7. #include "leveldb/table.h"
  8. #include "util/coding.h"
  9. namespace leveldb {
  10. struct TableAndFile {
  11. RandomAccessFile* file;
  12. Table* table;
  13. };
  14. static void DeleteEntry(const Slice& key, void* value) {
  15. TableAndFile* tf = reinterpret_cast<TableAndFile*>(value);
  16. delete tf->table;
  17. delete tf->file;
  18. delete tf;
  19. }
  20. static void UnrefEntry(void* arg1, void* arg2) {
  21. Cache* cache = reinterpret_cast<Cache*>(arg1);
  22. Cache::Handle* h = reinterpret_cast<Cache::Handle*>(arg2);
  23. cache->Release(h);
  24. }
  25. TableCache::TableCache(const std::string& dbname,
  26. const Options* options,
  27. int entries)
  28. : env_(options->env),
  29. dbname_(dbname),
  30. options_(options),
  31. cache_(NewLRUCache(entries)) {
  32. }
  33. TableCache::~TableCache() {
  34. delete cache_;
  35. }
  36. Iterator* TableCache::NewIterator(const ReadOptions& options,
  37. uint64_t file_number,
  38. uint64_t file_size,
  39. Table** tableptr) {
  40. if (tableptr != NULL) {
  41. *tableptr = NULL;
  42. }
  43. char buf[sizeof(file_number)];
  44. EncodeFixed64(buf, file_number);
  45. Slice key(buf, sizeof(buf));
  46. Cache::Handle* handle = cache_->Lookup(key);
  47. if (handle == NULL) {
  48. std::string fname = TableFileName(dbname_, file_number);
  49. RandomAccessFile* file = NULL;
  50. Table* table = NULL;
  51. Status s = env_->NewRandomAccessFile(fname, &file);
  52. if (s.ok()) {
  53. s = Table::Open(*options_, file, file_size, &table);
  54. }
  55. if (!s.ok()) {
  56. assert(table == NULL);
  57. delete file;
  58. // We do not cache error results so that if the error is transient,
  59. // or somebody repairs the file, we recover automatically.
  60. return NewErrorIterator(s);
  61. }
  62. TableAndFile* tf = new TableAndFile;
  63. tf->file = file;
  64. tf->table = table;
  65. handle = cache_->Insert(key, tf, 1, &DeleteEntry);
  66. }
  67. Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
  68. Iterator* result = table->NewIterator(options);
  69. result->RegisterCleanup(&UnrefEntry, cache_, handle);
  70. if (tableptr != NULL) {
  71. *tableptr = table;
  72. }
  73. return result;
  74. }
  75. void TableCache::Evict(uint64_t file_number) {
  76. char buf[sizeof(file_number)];
  77. EncodeFixed64(buf, file_number);
  78. cache_->Erase(Slice(buf, sizeof(buf)));
  79. }
  80. } // namespace leveldb