小组成员:谢瑞阳、徐翔宇
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.5 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 "include/env.h"
  7. #include "include/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. Table** tableptr) {
  39. if (tableptr != NULL) {
  40. *tableptr = NULL;
  41. }
  42. char buf[sizeof(file_number)];
  43. EncodeFixed64(buf, file_number);
  44. Slice key(buf, sizeof(buf));
  45. Cache::Handle* handle = cache_->Lookup(key);
  46. if (handle == NULL) {
  47. std::string fname = TableFileName(dbname_, file_number);
  48. RandomAccessFile* file = NULL;
  49. Table* table = NULL;
  50. Status s = env_->NewRandomAccessFile(fname, &file);
  51. if (s.ok()) {
  52. s = Table::Open(*options_, file, &table);
  53. }
  54. if (!s.ok()) {
  55. assert(table == NULL);
  56. delete file;
  57. // We do not cache error results so that if the error is transient,
  58. // or somebody repairs the file, we recover automatically.
  59. return NewErrorIterator(s);
  60. }
  61. TableAndFile* tf = new TableAndFile;
  62. tf->file = file;
  63. tf->table = table;
  64. handle = cache_->Insert(key, tf, 1, &DeleteEntry);
  65. }
  66. Table* table = reinterpret_cast<TableAndFile*>(cache_->Value(handle))->table;
  67. Iterator* result = table->NewIterator(options);
  68. result->RegisterCleanup(&UnrefEntry, cache_, handle);
  69. if (tableptr != NULL) {
  70. *tableptr = table;
  71. }
  72. return result;
  73. }
  74. void TableCache::Evict(uint64_t file_number) {
  75. char buf[sizeof(file_number)];
  76. EncodeFixed64(buf, file_number);
  77. cache_->Erase(Slice(buf, sizeof(buf)));
  78. }
  79. }