作者: 谢瑞阳 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.

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