作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

88 linhas
2.1 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. Status s;
  20. meta->file_size = 0;
  21. iter->SeekToFirst();
  22. std::string fname = TableFileName(dbname, meta->number);
  23. if (iter->Valid()) {
  24. WritableFile* file;
  25. s = env->NewWritableFile(fname, &file);
  26. if (!s.ok()) {
  27. return s;
  28. }
  29. TableBuilder* builder = new TableBuilder(options, file);
  30. meta->smallest.DecodeFrom(iter->key());
  31. for (; iter->Valid(); iter->Next()) {
  32. Slice key = iter->key();
  33. meta->largest.DecodeFrom(key);
  34. builder->Add(key, iter->value());
  35. }
  36. // Finish and check for builder errors
  37. if (s.ok()) {
  38. s = builder->Finish();
  39. if (s.ok()) {
  40. meta->file_size = builder->FileSize();
  41. assert(meta->file_size > 0);
  42. }
  43. } else {
  44. builder->Abandon();
  45. }
  46. delete builder;
  47. // Finish and check for file errors
  48. if (s.ok()) {
  49. s = file->Sync();
  50. }
  51. if (s.ok()) {
  52. s = file->Close();
  53. }
  54. delete file;
  55. file = NULL;
  56. if (s.ok()) {
  57. // Verify that the table is usable
  58. Iterator* it = table_cache->NewIterator(ReadOptions(),
  59. meta->number,
  60. meta->file_size);
  61. s = it->status();
  62. delete it;
  63. }
  64. }
  65. // Check for input iterator errors
  66. if (!iter->status().ok()) {
  67. s = iter->status();
  68. }
  69. if (s.ok() && meta->file_size > 0) {
  70. // Keep it
  71. } else {
  72. env->DeleteFile(fname);
  73. }
  74. return s;
  75. }
  76. } // namespace leveldb