作者: 韩晨旭 10225101440 李畅 10225102463
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

90 Zeilen
2.2 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/builder.h"
  5. #include "db/filename.h"
  6. #include "db/dbformat.h"
  7. #include "db/table_cache.h"
  8. #include "db/version_edit.h"
  9. #include "leveldb/db.h"
  10. #include "leveldb/env.h"
  11. #include "leveldb/iterator.h"
  12. namespace leveldb {
  13. Status BuildTable(const std::string& dbname,
  14. Env* env,
  15. const Options& options,
  16. TableCache* table_cache,
  17. Iterator* iter,
  18. FileMetaData* meta,
  19. VersionEdit* edit) {
  20. Status s;
  21. meta->file_size = 0;
  22. iter->SeekToFirst();
  23. std::string fname = TableFileName(dbname, meta->number);
  24. if (iter->Valid()) {
  25. WritableFile* file;
  26. s = env->NewWritableFile(fname, &file);
  27. if (!s.ok()) {
  28. return s;
  29. }
  30. TableBuilder* builder = new TableBuilder(options, file);
  31. meta->smallest.DecodeFrom(iter->key());
  32. for (; iter->Valid(); iter->Next()) {
  33. Slice key = iter->key();
  34. meta->largest.DecodeFrom(key);
  35. builder->Add(key, iter->value());
  36. }
  37. // Finish and check for builder errors
  38. if (s.ok()) {
  39. s = builder->Finish();
  40. if (s.ok()) {
  41. meta->file_size = builder->FileSize();
  42. assert(meta->file_size > 0);
  43. }
  44. } else {
  45. builder->Abandon();
  46. }
  47. delete builder;
  48. // Finish and check for file errors
  49. if (s.ok()) {
  50. s = file->Sync();
  51. }
  52. if (s.ok()) {
  53. s = file->Close();
  54. }
  55. delete file;
  56. file = NULL;
  57. if (s.ok()) {
  58. // Verify that the table is usable
  59. Iterator* it = table_cache->NewIterator(ReadOptions(),
  60. meta->number,
  61. meta->file_size);
  62. s = it->status();
  63. delete it;
  64. }
  65. }
  66. // Check for input iterator errors
  67. if (!iter->status().ok()) {
  68. s = iter->status();
  69. }
  70. if (s.ok() && meta->file_size > 0) {
  71. edit->AddFile(0, meta->number, meta->file_size,
  72. meta->smallest, meta->largest);
  73. } else {
  74. env->DeleteFile(fname);
  75. }
  76. return s;
  77. }
  78. }