2016-05-20 01:40:54 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// 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).
|
2016-05-20 01:40:54 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
#pragma once
|
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
#include <stdint.h>
|
2021-01-06 19:48:24 +01:00
|
|
|
#include <windows.h>
|
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
#include "rocksdb/file_system.h"
|
2017-05-06 01:09:24 +02:00
|
|
|
#include "rocksdb/status.h"
|
2016-05-20 01:40:54 +02:00
|
|
|
#include "util/aligned_buffer.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2016-05-20 01:40:54 +02:00
|
|
|
namespace port {
|
|
|
|
|
|
|
|
std::string GetWindowsErrSz(DWORD err);
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
inline IOStatus IOErrorFromWindowsError(const std::string& context, DWORD err) {
|
2016-12-22 21:51:29 +01:00
|
|
|
return ((err == ERROR_HANDLE_DISK_FULL) || (err == ERROR_DISK_FULL))
|
2021-01-06 19:48:24 +01:00
|
|
|
? IOStatus::NoSpace(context, GetWindowsErrSz(err))
|
2019-03-27 00:41:31 +01:00
|
|
|
: ((err == ERROR_FILE_NOT_FOUND) || (err == ERROR_PATH_NOT_FOUND))
|
2021-01-06 19:48:24 +01:00
|
|
|
? IOStatus::PathNotFound(context, GetWindowsErrSz(err))
|
|
|
|
: IOStatus::IOError(context, GetWindowsErrSz(err));
|
2016-05-20 01:40:54 +02:00
|
|
|
}
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
inline IOStatus IOErrorFromLastWindowsError(const std::string& context) {
|
2016-05-20 01:40:54 +02:00
|
|
|
return IOErrorFromWindowsError(context, GetLastError());
|
|
|
|
}
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
inline IOStatus IOError(const std::string& context, int err_number) {
|
2016-12-22 21:51:29 +01:00
|
|
|
return (err_number == ENOSPC)
|
2021-01-06 19:48:24 +01:00
|
|
|
? IOStatus::NoSpace(context, strerror(err_number))
|
2019-03-27 00:41:31 +01:00
|
|
|
: (err_number == ENOENT)
|
2021-01-06 19:48:24 +01:00
|
|
|
? IOStatus::PathNotFound(context, strerror(err_number))
|
|
|
|
: IOStatus::IOError(context, strerror(err_number));
|
2016-05-20 01:40:54 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 22:38:36 +02:00
|
|
|
class WinFileData;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus pwrite(const WinFileData* file_data, const Slice& data,
|
|
|
|
uint64_t offset, size_t& bytes_written);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus pread(const WinFileData* file_data, char* src, size_t num_bytes,
|
|
|
|
uint64_t offset, size_t& bytes_read);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus fallocate(const std::string& filename, HANDLE hFile, uint64_t to_size);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus ftruncate(const std::string& filename, HANDLE hFile, uint64_t toSize);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
size_t GetUniqueIdFromFile(HANDLE hFile, char* id, size_t max_size);
|
|
|
|
|
2016-10-14 01:36:34 +02:00
|
|
|
class WinFileData {
|
2016-12-22 21:51:29 +01:00
|
|
|
protected:
|
2016-10-14 01:36:34 +02:00
|
|
|
const std::string filename_;
|
2016-05-20 01:40:54 +02:00
|
|
|
HANDLE hFile_;
|
2018-10-05 05:44:43 +02:00
|
|
|
// If true, the I/O issued would be direct I/O which the buffer
|
2016-10-14 01:36:34 +02:00
|
|
|
// will need to be aligned (not sure there is a guarantee that the buffer
|
|
|
|
// passed in is aligned).
|
2016-12-22 21:51:29 +01:00
|
|
|
const bool use_direct_io_;
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
public:
|
2016-10-14 01:36:34 +02:00
|
|
|
// We want this class be usable both for inheritance (prive
|
|
|
|
// or protected) and for containment so __ctor and __dtor public
|
2017-05-06 01:09:24 +02:00
|
|
|
WinFileData(const std::string& filename, HANDLE hFile, bool direct_io)
|
|
|
|
: filename_(filename), hFile_(hFile), use_direct_io_(direct_io) {}
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
virtual ~WinFileData() { this->CloseFile(); }
|
2016-10-14 01:36:34 +02:00
|
|
|
|
|
|
|
bool CloseFile() {
|
|
|
|
bool result = true;
|
|
|
|
|
|
|
|
if (hFile_ != NULL && hFile_ != INVALID_HANDLE_VALUE) {
|
|
|
|
result = ::CloseHandle(hFile_);
|
|
|
|
assert(result);
|
|
|
|
hFile_ = NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& GetName() const { return filename_; }
|
|
|
|
|
|
|
|
HANDLE GetFileHandle() const { return hFile_; }
|
|
|
|
|
2017-01-13 21:01:08 +01:00
|
|
|
bool use_direct_io() const { return use_direct_io_; }
|
2016-10-14 01:36:34 +02:00
|
|
|
|
|
|
|
WinFileData(const WinFileData&) = delete;
|
|
|
|
WinFileData& operator=(const WinFileData&) = delete;
|
|
|
|
};
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
class WinSequentialFile : protected WinFileData, public FSSequentialFile {
|
2017-01-15 22:11:04 +01:00
|
|
|
// Override for behavior change when creating a custom env
|
2021-01-06 19:48:24 +01:00
|
|
|
virtual IOStatus PositionedReadInternal(char* src, size_t numBytes,
|
|
|
|
uint64_t offset,
|
|
|
|
size_t& bytes_read) const;
|
2017-01-15 22:11:04 +01:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
public:
|
2017-01-15 22:11:04 +01:00
|
|
|
WinSequentialFile(const std::string& fname, HANDLE f,
|
2021-01-06 19:48:24 +01:00
|
|
|
const FileOptions& options);
|
2017-01-15 22:11:04 +01:00
|
|
|
|
|
|
|
~WinSequentialFile();
|
|
|
|
|
|
|
|
WinSequentialFile(const WinSequentialFile&) = delete;
|
|
|
|
WinSequentialFile& operator=(const WinSequentialFile&) = delete;
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Read(size_t n, const IOOptions& options, Slice* result,
|
|
|
|
char* scratch, IODebugContext* dbg) override;
|
|
|
|
IOStatus PositionedRead(uint64_t offset, size_t n, const IOOptions& options,
|
|
|
|
Slice* result, char* scratch,
|
|
|
|
IODebugContext* dbg) override;
|
2017-01-15 22:11:04 +01:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Skip(uint64_t n) override;
|
2017-01-15 22:11:04 +01:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus InvalidateCache(size_t offset, size_t length) override;
|
2017-01-15 22:11:04 +01:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
virtual bool use_direct_io() const override {
|
|
|
|
return WinFileData::use_direct_io();
|
|
|
|
}
|
2017-01-15 22:11:04 +01:00
|
|
|
};
|
|
|
|
|
2016-10-14 01:36:34 +02:00
|
|
|
// mmap() based random-access
|
2021-01-06 19:48:24 +01:00
|
|
|
class WinMmapReadableFile : private WinFileData, public FSRandomAccessFile {
|
2016-05-20 01:40:54 +02:00
|
|
|
HANDLE hMap_;
|
|
|
|
|
|
|
|
const void* mapped_region_;
|
|
|
|
const size_t length_;
|
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
public:
|
2016-05-20 01:40:54 +02:00
|
|
|
// mapped_region_[0,length-1] contains the mmapped contents of the file.
|
|
|
|
WinMmapReadableFile(const std::string& fileName, HANDLE hFile, HANDLE hMap,
|
2016-12-22 21:51:29 +01:00
|
|
|
const void* mapped_region, size_t length);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
~WinMmapReadableFile();
|
|
|
|
|
2016-10-14 01:36:34 +02:00
|
|
|
WinMmapReadableFile(const WinMmapReadableFile&) = delete;
|
|
|
|
WinMmapReadableFile& operator=(const WinMmapReadableFile&) = delete;
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Read(uint64_t offset, size_t n, const IOOptions& options,
|
|
|
|
Slice* result, char* scratch,
|
|
|
|
IODebugContext* dbg) const override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
virtual IOStatus InvalidateCache(size_t offset, size_t length) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
virtual size_t GetUniqueId(char* id, size_t max_size) const override;
|
|
|
|
};
|
|
|
|
|
|
|
|
// We preallocate and use memcpy to append new
|
|
|
|
// data to the file. This is safe since we either properly close the
|
|
|
|
// file before reading from it, or for log files, the reading code
|
|
|
|
// knows enough to skip zero suffixes.
|
2021-01-06 19:48:24 +01:00
|
|
|
class WinMmapFile : private WinFileData, public FSWritableFile {
|
2016-12-22 21:51:29 +01:00
|
|
|
private:
|
2016-05-20 01:40:54 +02:00
|
|
|
HANDLE hMap_;
|
|
|
|
|
|
|
|
const size_t page_size_; // We flush the mapping view in page_size
|
|
|
|
// increments. We may decide if this is a memory
|
|
|
|
// page size or SSD page size
|
|
|
|
const size_t
|
2016-12-22 21:51:29 +01:00
|
|
|
allocation_granularity_; // View must start at such a granularity
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
size_t reserved_size_; // Preallocated size
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
size_t mapping_size_; // The max size of the mapping object
|
2016-05-20 01:40:54 +02:00
|
|
|
// we want to guess the final file size to minimize the remapping
|
2016-12-22 21:51:29 +01:00
|
|
|
size_t view_size_; // How much memory to map into a view at a time
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
char* mapped_begin_; // Must begin at the file offset that is aligned with
|
|
|
|
// allocation_granularity_
|
|
|
|
char* mapped_end_;
|
|
|
|
char* dst_; // Where to write next (in range [mapped_begin_,mapped_end_])
|
|
|
|
char* last_sync_; // Where have we synced up to
|
|
|
|
|
|
|
|
uint64_t file_offset_; // Offset of mapped_begin_ in file
|
|
|
|
|
|
|
|
// Do we have unsynced writes?
|
|
|
|
bool pending_sync_;
|
|
|
|
|
|
|
|
// Can only truncate or reserve to a sector size aligned if
|
|
|
|
// used on files that are opened with Unbuffered I/O
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus TruncateFile(uint64_t toSize);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus UnmapCurrentRegion();
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus MapNewRegion(const IOOptions& options, IODebugContext* dbg);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
virtual IOStatus PreallocateInternal(uint64_t spaceToReserve);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
public:
|
2016-05-20 01:40:54 +02:00
|
|
|
WinMmapFile(const std::string& fname, HANDLE hFile, size_t page_size,
|
2021-01-06 19:48:24 +01:00
|
|
|
size_t allocation_granularity, const FileOptions& options);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
~WinMmapFile();
|
|
|
|
|
2016-10-14 01:36:34 +02:00
|
|
|
WinMmapFile(const WinMmapFile&) = delete;
|
|
|
|
WinMmapFile& operator=(const WinMmapFile&) = delete;
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Append(const Slice& data, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override;
|
|
|
|
IOStatus Append(const Slice& data, const IOOptions& opts,
|
|
|
|
const DataVerificationInfo& /* verification_info */,
|
|
|
|
IODebugContext* dbg) override {
|
|
|
|
return Append(data, opts, dbg);
|
|
|
|
}
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
// Means Close() will properly take care of truncate
|
|
|
|
// and it does not need any additional information
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Truncate(uint64_t size, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Close(const IOOptions& options, IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Flush(const IOOptions& options, IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
// Flush only data
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Sync(const IOOptions& options, IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
/**
|
2021-01-06 19:48:24 +01:00
|
|
|
* Flush data as well as metadata to stable storage.
|
|
|
|
*/
|
|
|
|
IOStatus Fsync(const IOOptions& options, IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
/**
|
2021-01-06 19:48:24 +01:00
|
|
|
* Get the size of valid data in the file. This will not match the
|
|
|
|
* size that is returned from the filesystem because we use mmap
|
|
|
|
* to extend file by map_size every time.
|
|
|
|
*/
|
|
|
|
uint64_t GetFileSize(const IOOptions& options, IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus InvalidateCache(size_t offset, size_t length) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Allocate(uint64_t offset, uint64_t len, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
virtual size_t GetUniqueId(char* id, size_t max_size) const override;
|
|
|
|
};
|
|
|
|
|
2016-10-14 01:36:34 +02:00
|
|
|
class WinRandomAccessImpl {
|
2016-12-22 21:51:29 +01:00
|
|
|
protected:
|
2016-10-14 01:36:34 +02:00
|
|
|
WinFileData* file_base_;
|
2021-01-06 19:48:24 +01:00
|
|
|
size_t alignment_;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2016-10-14 01:36:34 +02:00
|
|
|
// Override for behavior change when creating a custom env
|
2021-01-06 19:48:24 +01:00
|
|
|
virtual IOStatus PositionedReadInternal(char* src, size_t numBytes,
|
|
|
|
uint64_t offset,
|
|
|
|
size_t& bytes_read) const;
|
2016-12-22 21:51:29 +01:00
|
|
|
|
2016-10-14 01:36:34 +02:00
|
|
|
WinRandomAccessImpl(WinFileData* file_base, size_t alignment,
|
2021-01-06 19:48:24 +01:00
|
|
|
const FileOptions& options);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2016-10-14 01:36:34 +02:00
|
|
|
virtual ~WinRandomAccessImpl() {}
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus ReadImpl(uint64_t offset, size_t n, Slice* result,
|
|
|
|
char* scratch) const;
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2017-04-27 21:19:55 +02:00
|
|
|
size_t GetAlignment() const { return alignment_; }
|
2017-01-15 22:11:04 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
WinRandomAccessImpl(const WinRandomAccessImpl&) = delete;
|
|
|
|
WinRandomAccessImpl& operator=(const WinRandomAccessImpl&) = delete;
|
2016-10-14 01:36:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// pread() based random-access
|
2016-12-22 21:51:29 +01:00
|
|
|
class WinRandomAccessFile
|
|
|
|
: private WinFileData,
|
|
|
|
protected WinRandomAccessImpl, // Want to be able to override
|
|
|
|
// PositionedReadInternal
|
2021-01-06 19:48:24 +01:00
|
|
|
public FSRandomAccessFile {
|
2016-12-22 21:51:29 +01:00
|
|
|
public:
|
2016-05-20 01:40:54 +02:00
|
|
|
WinRandomAccessFile(const std::string& fname, HANDLE hFile, size_t alignment,
|
2021-01-06 19:48:24 +01:00
|
|
|
const FileOptions& options);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
~WinRandomAccessFile();
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Read(uint64_t offset, size_t n, const IOOptions& options,
|
|
|
|
Slice* result, char* scratch,
|
|
|
|
IODebugContext* dbg) const override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2017-01-15 22:11:04 +01:00
|
|
|
virtual size_t GetUniqueId(char* id, size_t max_size) const override;
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
virtual bool use_direct_io() const override {
|
|
|
|
return WinFileData::use_direct_io();
|
|
|
|
}
|
2017-01-15 22:11:04 +01:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus InvalidateCache(size_t offset, size_t length) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2017-01-15 22:11:04 +01:00
|
|
|
virtual size_t GetRequiredBufferAlignment() const override;
|
2016-05-20 01:40:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// This is a sequential write class. It has been mimicked (as others) after
|
|
|
|
// the original Posix class. We add support for unbuffered I/O on windows as
|
|
|
|
// well
|
|
|
|
// we utilize the original buffer as an alignment buffer to write directly to
|
|
|
|
// file with no buffering.
|
|
|
|
// No buffering requires that the provided buffer is aligned to the physical
|
|
|
|
// sector size (SSD page size) and
|
|
|
|
// that all SetFilePointer() operations to occur with such an alignment.
|
|
|
|
// We thus always write in sector/page size increments to the drive and leave
|
|
|
|
// the tail for the next write OR for Close() at which point we pad with zeros.
|
|
|
|
// No padding is required for
|
|
|
|
// buffered access.
|
2016-10-14 01:36:34 +02:00
|
|
|
class WinWritableImpl {
|
2016-12-22 21:51:29 +01:00
|
|
|
protected:
|
|
|
|
WinFileData* file_data_;
|
|
|
|
const uint64_t alignment_;
|
2021-01-06 19:48:24 +01:00
|
|
|
uint64_t
|
|
|
|
next_write_offset_; // Needed because Windows does not support O_APPEND
|
2016-12-22 21:51:29 +01:00
|
|
|
uint64_t reservedsize_; // how far we have reserved space
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
virtual IOStatus PreallocateInternal(uint64_t spaceToReserve);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2016-10-14 01:36:34 +02:00
|
|
|
WinWritableImpl(WinFileData* file_data, size_t alignment);
|
|
|
|
|
|
|
|
~WinWritableImpl() {}
|
|
|
|
|
|
|
|
uint64_t GetAlignement() const { return alignment_; }
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus AppendImpl(const Slice& data);
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
// Requires that the data is aligned as specified by
|
|
|
|
// GetRequiredBufferAlignment()
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus PositionedAppendImpl(const Slice& data, uint64_t offset);
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus TruncateImpl(uint64_t size);
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus CloseImpl();
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus SyncImpl(const IOOptions& options, IODebugContext* dbg);
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2017-06-20 19:16:24 +02:00
|
|
|
uint64_t GetFileNextWriteOffset() {
|
2016-10-14 01:36:34 +02:00
|
|
|
// Double accounting now here with WritableFileWriter
|
|
|
|
// and this size will be wrong when unbuffered access is used
|
2016-12-22 21:51:29 +01:00
|
|
|
// but tests implement their own writable files and do not use
|
|
|
|
// WritableFileWrapper
|
2016-10-14 01:36:34 +02:00
|
|
|
// so we need to squeeze a square peg through
|
|
|
|
// a round hole here.
|
2017-06-20 19:16:24 +02:00
|
|
|
return next_write_offset_;
|
2016-10-14 01:36:34 +02:00
|
|
|
}
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus AllocateImpl(uint64_t offset, uint64_t len);
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
public:
|
2016-10-14 01:36:34 +02:00
|
|
|
WinWritableImpl(const WinWritableImpl&) = delete;
|
|
|
|
WinWritableImpl& operator=(const WinWritableImpl&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
class WinWritableFile : private WinFileData,
|
2016-12-22 21:51:29 +01:00
|
|
|
protected WinWritableImpl,
|
2021-01-06 19:48:24 +01:00
|
|
|
public FSWritableFile {
|
2016-12-22 21:51:29 +01:00
|
|
|
public:
|
2016-05-20 01:40:54 +02:00
|
|
|
WinWritableFile(const std::string& fname, HANDLE hFile, size_t alignment,
|
2021-01-06 19:48:24 +01:00
|
|
|
size_t capacity, const FileOptions& options);
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
~WinWritableFile();
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Append(const Slice& data, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override;
|
|
|
|
IOStatus Append(const Slice& data, const IOOptions& opts,
|
|
|
|
const DataVerificationInfo& /* verification_info */,
|
|
|
|
IODebugContext* dbg) override {
|
|
|
|
return Append(data, opts, dbg);
|
|
|
|
}
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
// Requires that the data is aligned as specified by
|
|
|
|
// GetRequiredBufferAlignment()
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus PositionedAppend(const Slice& data, uint64_t offset,
|
|
|
|
const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override;
|
|
|
|
IOStatus PositionedAppend(const Slice& data, uint64_t offset,
|
|
|
|
const IOOptions& opts,
|
|
|
|
const DataVerificationInfo& /* verification_info */,
|
|
|
|
IODebugContext* dbg) override {
|
|
|
|
return PositionedAppend(data, offset, opts, dbg);
|
|
|
|
}
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
// Need to implement this so the file is truncated correctly
|
|
|
|
// when buffered and unbuffered mode
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Truncate(uint64_t size, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Close(const IOOptions& options, IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
// write out the cached data to the OS cache
|
|
|
|
// This is now taken care of the WritableFileWriter
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Flush(const IOOptions& options, IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Sync(const IOOptions& options, IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Fsync(const IOOptions& options, IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2017-12-22 03:37:27 +01:00
|
|
|
virtual bool IsSyncThreadSafe() const override;
|
|
|
|
|
2017-01-15 22:11:04 +01:00
|
|
|
// Indicates if the class makes use of direct I/O
|
|
|
|
// Use PositionedAppend
|
|
|
|
virtual bool use_direct_io() const override;
|
|
|
|
|
|
|
|
virtual size_t GetRequiredBufferAlignment() const override;
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
uint64_t GetFileSize(const IOOptions& options, IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Allocate(uint64_t offset, uint64_t len, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override;
|
2016-05-20 01:40:54 +02:00
|
|
|
|
|
|
|
virtual size_t GetUniqueId(char* id, size_t max_size) const override;
|
|
|
|
};
|
|
|
|
|
2016-10-14 01:36:34 +02:00
|
|
|
class WinRandomRWFile : private WinFileData,
|
2016-12-22 21:51:29 +01:00
|
|
|
protected WinRandomAccessImpl,
|
|
|
|
protected WinWritableImpl,
|
2021-01-06 19:48:24 +01:00
|
|
|
public FSRandomRWFile {
|
2016-12-22 21:51:29 +01:00
|
|
|
public:
|
2016-10-14 01:36:34 +02:00
|
|
|
WinRandomRWFile(const std::string& fname, HANDLE hFile, size_t alignment,
|
2021-01-06 19:48:24 +01:00
|
|
|
const FileOptions& options);
|
2016-10-14 01:36:34 +02:00
|
|
|
|
|
|
|
~WinRandomRWFile() {}
|
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
// Indicates if the class makes use of direct I/O
|
2016-10-14 01:36:34 +02:00
|
|
|
// If false you must pass aligned buffer to Write()
|
2017-01-13 21:01:08 +01:00
|
|
|
virtual bool use_direct_io() const override;
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
// Use the returned alignment value to allocate aligned
|
2017-01-13 21:01:08 +01:00
|
|
|
// buffer for Write() when use_direct_io() returns true
|
2016-10-14 01:36:34 +02:00
|
|
|
virtual size_t GetRequiredBufferAlignment() const override;
|
|
|
|
|
|
|
|
// Write bytes in `data` at offset `offset`, Returns Status::OK() on success.
|
2017-01-13 21:01:08 +01:00
|
|
|
// Pass aligned buffer when use_direct_io() returns true.
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Write(uint64_t offset, const Slice& data, const IOOptions& options,
|
|
|
|
IODebugContext* dbg) override;
|
2016-10-14 01:36:34 +02:00
|
|
|
|
|
|
|
// Read up to `n` bytes starting from offset `offset` and store them in
|
|
|
|
// result, provided `scratch` size should be at least `n`.
|
|
|
|
// Returns Status::OK() on success.
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Read(uint64_t offset, size_t n, const IOOptions& options,
|
|
|
|
Slice* result, char* scratch,
|
|
|
|
IODebugContext* dbg) const override;
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Flush(const IOOptions& options, IODebugContext* dbg) override;
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Sync(const IOOptions& options, IODebugContext* dbg) override;
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Fsync(const IOOptions& options, IODebugContext* dbg) override {
|
|
|
|
return Sync(options, dbg);
|
|
|
|
}
|
2016-10-14 01:36:34 +02:00
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
IOStatus Close(const IOOptions& options, IODebugContext* dbg) override;
|
2016-10-14 01:36:34 +02:00
|
|
|
};
|
|
|
|
|
2018-05-25 00:05:00 +02:00
|
|
|
class WinMemoryMappedBuffer : public MemoryMappedFileBuffer {
|
2021-01-06 19:48:24 +01:00
|
|
|
private:
|
|
|
|
HANDLE file_handle_;
|
|
|
|
HANDLE map_handle_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
WinMemoryMappedBuffer(HANDLE file_handle, HANDLE map_handle, void* base,
|
|
|
|
size_t size)
|
|
|
|
: MemoryMappedFileBuffer(base, size),
|
|
|
|
file_handle_(file_handle),
|
|
|
|
map_handle_(map_handle) {}
|
2018-05-25 00:05:00 +02:00
|
|
|
~WinMemoryMappedBuffer() override;
|
|
|
|
};
|
|
|
|
|
2021-01-06 19:48:24 +01:00
|
|
|
class WinDirectory : public FSDirectory {
|
2018-03-06 20:47:42 +01:00
|
|
|
HANDLE handle_;
|
2021-01-06 19:48:24 +01:00
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
public:
|
2019-03-27 00:41:31 +01:00
|
|
|
explicit WinDirectory(HANDLE h) noexcept : handle_(h) {
|
2018-03-06 20:47:42 +01:00
|
|
|
assert(handle_ != INVALID_HANDLE_VALUE);
|
|
|
|
}
|
2021-01-06 19:48:24 +01:00
|
|
|
~WinDirectory() { ::CloseHandle(handle_); }
|
|
|
|
IOStatus Fsync(const IOOptions& options, IODebugContext* dbg) override;
|
2018-03-06 20:47:42 +01:00
|
|
|
|
|
|
|
size_t GetUniqueId(char* id, size_t max_size) const override;
|
2016-05-20 01:40:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class WinFileLock : public FileLock {
|
2016-12-22 21:51:29 +01:00
|
|
|
public:
|
2016-05-20 01:40:54 +02:00
|
|
|
explicit WinFileLock(HANDLE hFile) : hFile_(hFile) {
|
|
|
|
assert(hFile != NULL);
|
|
|
|
assert(hFile != INVALID_HANDLE_VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
~WinFileLock();
|
|
|
|
|
2016-12-22 21:51:29 +01:00
|
|
|
private:
|
2016-05-20 01:40:54 +02:00
|
|
|
HANDLE hFile_;
|
|
|
|
};
|
2021-01-06 19:48:24 +01:00
|
|
|
} // namespace port
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|