小组成员:谢瑞阳、徐翔宇
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.

127 line
3.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. Status TableCache::FindTable(uint64_t file_number, uint64_t file_size,
  37. Cache::Handle** handle) {
  38. Status s;
  39. char buf[sizeof(file_number)];
  40. EncodeFixed64(buf, file_number);
  41. Slice key(buf, sizeof(buf));
  42. *handle = cache_->Lookup(key);
  43. if (*handle == NULL) {
  44. std::string fname = TableFileName(dbname_, file_number);
  45. RandomAccessFile* file = NULL;
  46. Table* table = NULL;
  47. s = env_->NewRandomAccessFile(fname, &file);
  48. if (!s.ok()) {
  49. std::string old_fname = SSTTableFileName(dbname_, file_number);
  50. if (env_->NewRandomAccessFile(old_fname, &file).ok()) {
  51. s = Status::OK();
  52. }
  53. }
  54. if (s.ok()) {
  55. s = Table::Open(*options_, file, file_size, &table);
  56. }
  57. if (!s.ok()) {
  58. assert(table == NULL);
  59. delete file;
  60. // We do not cache error results so that if the error is transient,
  61. // or somebody repairs the file, we recover automatically.
  62. } else {
  63. TableAndFile* tf = new TableAndFile;
  64. tf->file = file;
  65. tf->table = table;
  66. *handle = cache_->Insert(key, tf, 1, &DeleteEntry);
  67. }
  68. }
  69. return s;
  70. }
  71. Iterator* TableCache::NewIterator(const ReadOptions& options,
  72. uint64_t file_number,
  73. uint64_t file_size,
  74. Table** tableptr) {
  75. if (tableptr != NULL) {
  76. *tableptr = NULL;
  77. }
  78. Cache::Handle* handle = NULL;
  79. Status s = FindTable(file_number, file_size, &handle);
  80. if (!s.ok()) {
  81. return NewErrorIterator(s);
  82. }
  83. Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
  84. Iterator* result = table->NewIterator(options);
  85. result->RegisterCleanup(&UnrefEntry, cache_, handle);
  86. if (tableptr != NULL) {
  87. *tableptr = table;
  88. }
  89. return result;
  90. }
  91. Status TableCache::Get(const ReadOptions& options,
  92. uint64_t file_number,
  93. uint64_t file_size,
  94. const Slice& k,
  95. void* arg,
  96. void (*saver)(void*, const Slice&, const Slice&)) {
  97. Cache::Handle* handle = NULL;
  98. Status s = FindTable(file_number, file_size, &handle);
  99. if (s.ok()) {
  100. Table* t = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
  101. s = t->InternalGet(options, k, arg, saver);
  102. cache_->Release(handle);
  103. }
  104. return s;
  105. }
  106. void TableCache::Evict(uint64_t file_number) {
  107. char buf[sizeof(file_number)];
  108. EncodeFixed64(buf, file_number);
  109. cache_->Erase(Slice(buf, sizeof(buf)));
  110. }
  111. } // namespace leveldb