小组成员: 曹可心-10223903406 朴祉燕-10224602413
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

84 rindas
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 = NULL;
  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