env.cc 3.1 KB
Newer Older
1 2 3 4 5
//  Copyright (c) 2013, Facebook, Inc.  All rights reserved.
//  This source code is licensed under the BSD-style license found in the
//  LICENSE file in the root directory of this source tree. An additional grant
//  of patent rights can be found in the PATENTS file in the same directory.
//
J
jorlow@chromium.org 已提交
6 7 8 9
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

10 11
#include "rocksdb/env.h"
#include "rocksdb/options.h"
J
jorlow@chromium.org 已提交
12

13
namespace rocksdb {
J
jorlow@chromium.org 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26

Env::~Env() {
}

SequentialFile::~SequentialFile() {
}

RandomAccessFile::~RandomAccessFile() {
}

WritableFile::~WritableFile() {
}

27 28 29
Logger::~Logger() {
}

J
jorlow@chromium.org 已提交
30 31 32
FileLock::~FileLock() {
}

33
void Log(Logger* info_log, const char* format, ...) {
34 35 36 37 38 39 40 41 42 43
  if (info_log) {
    va_list ap;
    va_start(ap, format);
    info_log->Logv(format, ap);
    va_end(ap);
  }
}

void Log(const shared_ptr<Logger>& info_log, const char* format, ...) {
  if (info_log) {
44 45 46 47 48
    va_list ap;
    va_start(ap, format);
    info_log->Logv(format, ap);
    va_end(ap);
  }
J
jorlow@chromium.org 已提交
49 50
}

51 52 53
static Status DoWriteStringToFile(Env* env, const Slice& data,
                                  const std::string& fname,
                                  bool should_sync) {
54
  unique_ptr<WritableFile> file;
H
Haobo Xu 已提交
55
  EnvOptions soptions;
56
  Status s = env->NewWritableFile(fname, &file, soptions);
J
jorlow@chromium.org 已提交
57 58 59 60
  if (!s.ok()) {
    return s;
  }
  s = file->Append(data);
61 62 63
  if (s.ok() && should_sync) {
    s = file->Sync();
  }
J
jorlow@chromium.org 已提交
64 65 66 67 68 69
  if (!s.ok()) {
    env->DeleteFile(fname);
  }
  return s;
}

70 71 72 73 74 75 76 77 78 79
Status WriteStringToFile(Env* env, const Slice& data,
                         const std::string& fname) {
  return DoWriteStringToFile(env, data, fname, false);
}

Status WriteStringToFileSync(Env* env, const Slice& data,
                             const std::string& fname) {
  return DoWriteStringToFile(env, data, fname, true);
}

J
jorlow@chromium.org 已提交
80
Status ReadFileToString(Env* env, const std::string& fname, std::string* data) {
H
Haobo Xu 已提交
81
  EnvOptions soptions;
J
jorlow@chromium.org 已提交
82
  data->clear();
83
  unique_ptr<SequentialFile> file;
84
  Status s = env->NewSequentialFile(fname, &file, soptions);
J
jorlow@chromium.org 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  if (!s.ok()) {
    return s;
  }
  static const int kBufferSize = 8192;
  char* space = new char[kBufferSize];
  while (true) {
    Slice fragment;
    s = file->Read(kBufferSize, &fragment, space);
    if (!s.ok()) {
      break;
    }
    data->append(fragment.data(), fragment.size());
    if (fragment.empty()) {
      break;
    }
  }
  delete[] space;
  return s;
}

EnvWrapper::~EnvWrapper() {
}

H
Haobo Xu 已提交
108 109 110 111 112 113 114
namespace {  // anonymous namespace

void AssignEnvOptions(EnvOptions* env_options, const Options& options) {
  env_options->use_os_buffer = options.allow_os_buffer;
  env_options->use_mmap_reads = options.allow_mmap_reads;
  env_options->use_mmap_writes = options.allow_mmap_writes;
  env_options->set_fd_cloexec = options.is_fd_close_on_exec;
H
Haobo Xu 已提交
115
  env_options->bytes_per_sync = options.bytes_per_sync;
H
Haobo Xu 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129
}

}

EnvOptions::EnvOptions(const Options& options) {
  AssignEnvOptions(this, options);
}

EnvOptions::EnvOptions() {
  Options options;
  AssignEnvOptions(this, options);
}


130
}  // namespace rocksdb