fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
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.
111 lines
3.1 KiB
111 lines
3.1 KiB
9 years ago
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
||
8 years ago
|
// This source code is licensed under both the GPLv2 (found in the
|
||
|
// COPYING file in the root directory) and Apache 2.0 License
|
||
|
// (found in the LICENSE.Apache file in the root directory).
|
||
10 years ago
|
//
|
||
6 years ago
|
#include "file/file_util.h"
|
||
9 years ago
|
|
||
10 years ago
|
#include <string>
|
||
|
#include <algorithm>
|
||
9 years ago
|
|
||
6 years ago
|
#include "file/sst_file_manager_impl.h"
|
||
10 years ago
|
#include "rocksdb/env.h"
|
||
10 years ago
|
#include "util/file_reader_writer.h"
|
||
10 years ago
|
|
||
|
namespace rocksdb {
|
||
|
|
||
|
// Utility function to copy a file up to a specified length
|
||
|
Status CopyFile(Env* env, const std::string& source,
|
||
8 years ago
|
const std::string& destination, uint64_t size, bool use_fsync) {
|
||
10 years ago
|
const EnvOptions soptions;
|
||
|
Status s;
|
||
6 years ago
|
std::unique_ptr<SequentialFileReader> src_reader;
|
||
|
std::unique_ptr<WritableFileWriter> dest_writer;
|
||
10 years ago
|
|
||
|
{
|
||
6 years ago
|
std::unique_ptr<SequentialFile> srcfile;
|
||
7 years ago
|
s = env->NewSequentialFile(source, &srcfile, soptions);
|
||
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
6 years ago
|
std::unique_ptr<WritableFile> destfile;
|
||
10 years ago
|
s = env->NewWritableFile(destination, &destfile, soptions);
|
||
7 years ago
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
10 years ago
|
|
||
7 years ago
|
if (size == 0) {
|
||
|
// default argument means copy everything
|
||
10 years ago
|
s = env->GetFileSize(source, &size);
|
||
7 years ago
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
10 years ago
|
}
|
||
7 years ago
|
src_reader.reset(new SequentialFileReader(std::move(srcfile), source));
|
||
6 years ago
|
dest_writer.reset(
|
||
|
new WritableFileWriter(std::move(destfile), destination, soptions));
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
char buffer[4096];
|
||
|
Slice slice;
|
||
|
while (size > 0) {
|
||
9 years ago
|
size_t bytes_to_read = std::min(sizeof(buffer), static_cast<size_t>(size));
|
||
7 years ago
|
s = src_reader->Read(bytes_to_read, &slice, buffer);
|
||
|
if (!s.ok()) {
|
||
|
return s;
|
||
10 years ago
|
}
|
||
7 years ago
|
if (slice.size() == 0) {
|
||
|
return Status::Corruption("file too small");
|
||
10 years ago
|
}
|
||
7 years ago
|
s = dest_writer->Append(slice);
|
||
10 years ago
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
|
size -= slice.size();
|
||
|
}
|
||
7 years ago
|
return dest_writer->Sync(use_fsync);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
// Utility function to create a file with the provided contents
|
||
|
Status CreateFile(Env* env, const std::string& destination,
|
||
6 years ago
|
const std::string& contents, bool use_fsync) {
|
||
9 years ago
|
const EnvOptions soptions;
|
||
|
Status s;
|
||
6 years ago
|
std::unique_ptr<WritableFileWriter> dest_writer;
|
||
9 years ago
|
|
||
6 years ago
|
std::unique_ptr<WritableFile> destfile;
|
||
9 years ago
|
s = env->NewWritableFile(destination, &destfile, soptions);
|
||
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
6 years ago
|
dest_writer.reset(
|
||
|
new WritableFileWriter(std::move(destfile), destination, soptions));
|
||
6 years ago
|
s = dest_writer->Append(Slice(contents));
|
||
|
if (!s.ok()) {
|
||
|
return s;
|
||
|
}
|
||
|
return dest_writer->Sync(use_fsync);
|
||
9 years ago
|
}
|
||
|
|
||
6 years ago
|
Status DeleteDBFile(const ImmutableDBOptions* db_options,
|
||
|
const std::string& fname, const std::string& dir_to_sync,
|
||
|
const bool force_bg) {
|
||
8 years ago
|
#ifndef ROCKSDB_LITE
|
||
6 years ago
|
SstFileManagerImpl* sfm =
|
||
9 years ago
|
static_cast<SstFileManagerImpl*>(db_options->sst_file_manager.get());
|
||
7 years ago
|
if (sfm) {
|
||
6 years ago
|
return sfm->ScheduleFileDeletion(fname, dir_to_sync, force_bg);
|
||
9 years ago
|
} else {
|
||
9 years ago
|
return db_options->env->DeleteFile(fname);
|
||
9 years ago
|
}
|
||
8 years ago
|
#else
|
||
7 years ago
|
(void)dir_to_sync;
|
||
6 years ago
|
(void)force_bg;
|
||
8 years ago
|
// SstFileManager is not supported in ROCKSDB_LITE
|
||
6 years ago
|
// Delete file immediately
|
||
8 years ago
|
return db_options->env->DeleteFile(fname);
|
||
|
#endif
|
||
9 years ago
|
}
|
||
|
|
||
10 years ago
|
} // namespace rocksdb
|