2020-01-10 21:42:26 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-01-18 19:15:21 +00:00
|
|
|
#include <memory>
|
2020-01-30 19:29:30 +00:00
|
|
|
#include <mutex>
|
2020-01-10 21:42:26 +00:00
|
|
|
#include <unordered_map>
|
2020-01-30 19:29:30 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <Disks/IDisk.h>
|
|
|
|
#include <IO/WriteBufferFromString.h>
|
2020-01-10 21:42:26 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-01-30 19:29:30 +00:00
|
|
|
class DiskMemory;
|
2020-01-18 19:15:21 +00:00
|
|
|
class ReadBuffer;
|
|
|
|
class WriteBuffer;
|
|
|
|
|
2020-01-30 19:29:30 +00:00
|
|
|
// This class is responsible to update files metadata after buffer is finalized.
|
|
|
|
class WriteIndirectBuffer : public WriteBufferFromOwnString
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WriteIndirectBuffer(DiskMemory * disk_, String path_, WriteMode mode_) : disk(disk_), path(std::move(path_)), mode(mode_) {}
|
|
|
|
|
|
|
|
~WriteIndirectBuffer() override;
|
|
|
|
|
|
|
|
void finalize() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
DiskMemory * disk;
|
|
|
|
String path;
|
|
|
|
WriteMode mode;
|
|
|
|
};
|
2020-01-18 19:15:21 +00:00
|
|
|
|
|
|
|
/** Implementation of Disk intended only for testing purposes.
|
|
|
|
* All filesystem objects are stored in memory and lost on server restart.
|
|
|
|
*
|
|
|
|
* NOTE Work in progress. Currently the interface is not viable enough to support MergeTree or even StripeLog tables.
|
|
|
|
* Please delete this interface if it will not be finished after 2020-06-18.
|
|
|
|
*/
|
2020-01-10 21:42:26 +00:00
|
|
|
class DiskMemory : public IDisk
|
|
|
|
{
|
|
|
|
public:
|
2020-01-30 19:29:30 +00:00
|
|
|
DiskMemory(const String & name_) : name(name_), disk_path("memory://" + name_ + '/') {}
|
2020-01-10 21:42:26 +00:00
|
|
|
|
|
|
|
const String & getName() const override { return name; }
|
|
|
|
|
|
|
|
const String & getPath() const override { return disk_path; }
|
|
|
|
|
|
|
|
ReservationPtr reserve(UInt64 bytes) override;
|
|
|
|
|
|
|
|
UInt64 getTotalSpace() const override;
|
|
|
|
|
|
|
|
UInt64 getAvailableSpace() const override;
|
|
|
|
|
|
|
|
UInt64 getUnreservedSpace() const override;
|
|
|
|
|
|
|
|
bool exists(const String & path) const override;
|
|
|
|
|
|
|
|
bool isFile(const String & path) const override;
|
|
|
|
|
|
|
|
bool isDirectory(const String & path) const override;
|
|
|
|
|
|
|
|
size_t getFileSize(const String & path) const override;
|
|
|
|
|
|
|
|
void createDirectory(const String & path) override;
|
|
|
|
|
|
|
|
void createDirectories(const String & path) override;
|
|
|
|
|
|
|
|
void clearDirectory(const String & path) override;
|
|
|
|
|
|
|
|
void moveDirectory(const String & from_path, const String & to_path) override;
|
|
|
|
|
|
|
|
DiskDirectoryIteratorPtr iterateDirectory(const String & path) override;
|
|
|
|
|
|
|
|
void moveFile(const String & from_path, const String & to_path) override;
|
|
|
|
|
|
|
|
void replaceFile(const String & from_path, const String & to_path) override;
|
|
|
|
|
|
|
|
void copyFile(const String & from_path, const String & to_path) override;
|
|
|
|
|
2020-01-30 19:29:30 +00:00
|
|
|
std::unique_ptr<SeekableReadBuffer> readFile(const String & path, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE) const override;
|
2020-01-10 21:42:26 +00:00
|
|
|
|
2020-01-30 19:29:30 +00:00
|
|
|
std::unique_ptr<WriteBuffer>
|
|
|
|
writeFile(const String & path, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE, WriteMode mode = WriteMode::Rewrite) override;
|
2020-01-10 21:42:26 +00:00
|
|
|
|
2020-01-18 23:18:23 +00:00
|
|
|
void remove(const String & path) override;
|
|
|
|
|
|
|
|
void removeRecursive(const String & path) override;
|
2020-01-15 14:15:21 +00:00
|
|
|
|
2020-01-10 21:42:26 +00:00
|
|
|
private:
|
|
|
|
void createDirectoriesImpl(const String & path);
|
|
|
|
void replaceFileImpl(const String & from_path, const String & to_path);
|
|
|
|
|
|
|
|
private:
|
2020-01-30 19:29:30 +00:00
|
|
|
friend class WriteIndirectBuffer;
|
|
|
|
|
2020-01-10 21:42:26 +00:00
|
|
|
enum class FileType
|
|
|
|
{
|
|
|
|
File,
|
|
|
|
Directory
|
|
|
|
};
|
|
|
|
|
|
|
|
struct FileData
|
|
|
|
{
|
|
|
|
FileType type;
|
|
|
|
String data;
|
|
|
|
|
2020-01-30 19:29:30 +00:00
|
|
|
FileData(FileType type_, String data_) : type(type_), data(std::move(data_)) {}
|
|
|
|
explicit FileData(FileType type_) : type(type_), data("") {}
|
2020-01-10 21:42:26 +00:00
|
|
|
};
|
|
|
|
using Files = std::unordered_map<String, FileData>; /// file path -> file data
|
|
|
|
|
|
|
|
const String name;
|
|
|
|
const String disk_path;
|
|
|
|
Files files;
|
|
|
|
mutable std::mutex mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|