10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

84 lignes
2.0 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. s = builder->Finish();
  38. if (s.ok()) {
  39. meta->file_size = builder->FileSize();
  40. assert(meta->file_size > 0);
  41. }
  42. delete builder;
  43. // Finish and check for file errors
  44. if (s.ok()) {
  45. s = file->Sync();
  46. }
  47. if (s.ok()) {
  48. s = file->Close();
  49. }
  50. delete file;
  51. file = nullptr;
  52. if (s.ok()) {
  53. // Verify that the table is usable
  54. Iterator* it = table_cache->NewIterator(ReadOptions(),
  55. meta->number,
  56. meta->file_size);
  57. s = it->status();
  58. delete it;
  59. }
  60. }
  61. // Check for input iterator errors
  62. if (!iter->status().ok()) {
  63. s = iter->status();
  64. }
  65. if (s.ok() && meta->file_size > 0) {
  66. // Keep it
  67. } else {
  68. env->DeleteFile(fname);
  69. }
  70. return s;
  71. }
  72. } // namespace leveldb