作者: 谢瑞阳 10225101483 徐翔宇 10225101535
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

100 行
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 "leveldb/env.h"
  5. namespace leveldb {
  6. Env::~Env() {
  7. }
  8. Status Env::NewAppendableFile(const std::string& fname, WritableFile** result) {
  9. return Status::NotSupported("NewAppendableFile", fname);
  10. }
  11. SequentialFile::~SequentialFile() {
  12. }
  13. RandomAccessFile::~RandomAccessFile() {
  14. }
  15. WritableFile::~WritableFile() {
  16. }
  17. Logger::~Logger() {
  18. }
  19. FileLock::~FileLock() {
  20. }
  21. void Log(Logger* info_log, const char* format, ...) {
  22. if (info_log != nullptr) {
  23. va_list ap;
  24. va_start(ap, format);
  25. info_log->Logv(format, ap);
  26. va_end(ap);
  27. }
  28. }
  29. static Status DoWriteStringToFile(Env* env, const Slice& data,
  30. const std::string& fname,
  31. bool should_sync) {
  32. WritableFile* file;
  33. Status s = env->NewWritableFile(fname, &file);
  34. if (!s.ok()) {
  35. return s;
  36. }
  37. s = file->Append(data);
  38. if (s.ok() && should_sync) {
  39. s = file->Sync();
  40. }
  41. if (s.ok()) {
  42. s = file->Close();
  43. }
  44. delete file; // Will auto-close if we did not close above
  45. if (!s.ok()) {
  46. env->DeleteFile(fname);
  47. }
  48. return s;
  49. }
  50. Status WriteStringToFile(Env* env, const Slice& data,
  51. const std::string& fname) {
  52. return DoWriteStringToFile(env, data, fname, false);
  53. }
  54. Status WriteStringToFileSync(Env* env, const Slice& data,
  55. const std::string& fname) {
  56. return DoWriteStringToFile(env, data, fname, true);
  57. }
  58. Status ReadFileToString(Env* env, const std::string& fname, std::string* data) {
  59. data->clear();
  60. SequentialFile* file;
  61. Status s = env->NewSequentialFile(fname, &file);
  62. if (!s.ok()) {
  63. return s;
  64. }
  65. static const int kBufferSize = 8192;
  66. char* space = new char[kBufferSize];
  67. while (true) {
  68. Slice fragment;
  69. s = file->Read(kBufferSize, &fragment, space);
  70. if (!s.ok()) {
  71. break;
  72. }
  73. data->append(fragment.data(), fragment.size());
  74. if (fragment.empty()) {
  75. break;
  76. }
  77. }
  78. delete[] space;
  79. delete file;
  80. return s;
  81. }
  82. EnvWrapper::~EnvWrapper() {
  83. }
  84. } // namespace leveldb