作者: 谢瑞阳 10225101483 徐翔宇 10225101535
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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