ClickHouse/dbms/src/Disks/IDisk.h

164 lines
4.7 KiB
C++
Raw Normal View History

2019-11-27 09:39:44 +00:00
#pragma once
#include <Core/Defines.h>
#include <Core/Types.h>
2019-11-27 09:39:44 +00:00
#include <Common/CurrentMetrics.h>
#include <Common/Exception.h>
#include <memory>
#include <utility>
#include <boost/noncopyable.hpp>
namespace CurrentMetrics
{
extern const Metric DiskSpaceReservedForMerge;
}
namespace DB
{
2019-12-03 13:37:40 +00:00
class IDiskDirectoryIterator;
using DiskDirectoryIteratorPtr = std::unique_ptr<IDiskDirectoryIterator>;
2019-11-27 09:39:44 +00:00
class IReservation;
using ReservationPtr = std::unique_ptr<IReservation>;
class ReadBuffer;
class WriteBuffer;
/**
2019-11-27 15:49:15 +00:00
* Provide interface for reservation.
2019-11-27 09:39:44 +00:00
*/
class Space : public std::enable_shared_from_this<Space>
{
public:
2019-12-03 13:37:40 +00:00
/// Return the name of the space object.
2019-11-27 09:39:44 +00:00
virtual const String & getName() const = 0;
2019-12-03 13:37:40 +00:00
/// Reserve the specified number of bytes.
virtual ReservationPtr reserve(UInt64 bytes) = 0;
2019-11-27 09:39:44 +00:00
virtual ~Space() = default;
};
2019-12-03 13:37:40 +00:00
using SpacePtr = std::shared_ptr<Space>;
2019-11-27 09:39:44 +00:00
/**
* A unit of storage persisting data and metadata.
* Abstract underlying storage technology.
* Responsible for:
* - file management;
* - space accounting and reservation.
*/
2019-11-27 09:39:44 +00:00
class IDisk : public Space
{
public:
2019-12-03 13:37:40 +00:00
/// Root path for all files stored on the disk.
/// It's not required to be a local filesystem path.
2019-11-27 09:39:44 +00:00
virtual const String & getPath() const = 0;
2019-12-03 13:37:40 +00:00
/// Total available space on the disk.
2019-11-27 09:39:44 +00:00
virtual UInt64 getTotalSpace() const = 0;
2019-12-03 13:37:40 +00:00
/// Space currently available on the disk.
2019-11-27 09:39:44 +00:00
virtual UInt64 getAvailableSpace() const = 0;
2019-12-03 13:37:40 +00:00
/// Space available for reservation (available space minus reserved space).
2019-11-27 09:39:44 +00:00
virtual UInt64 getUnreservedSpace() const = 0;
2019-12-03 13:37:40 +00:00
/// Amount of bytes which should be kept free on the disk.
2019-11-27 09:39:44 +00:00
virtual UInt64 getKeepingFreeSpace() const { return 0; }
2019-12-03 13:37:40 +00:00
/// Return `true` if the specified file exists.
virtual bool exists(const String & path) const = 0;
2019-11-27 09:39:44 +00:00
2019-12-03 13:37:40 +00:00
/// Return `true` if the specified file exists and it's a regular file (not a directory or special file type).
virtual bool isFile(const String & path) const = 0;
2019-11-27 09:39:44 +00:00
2019-12-03 13:37:40 +00:00
/// Return `true` if the specified file exists and it's a directory.
virtual bool isDirectory(const String & path) const = 0;
2019-11-27 09:39:44 +00:00
/// Return size of the specified file.
virtual size_t getFileSize(const String & path) const = 0;
2019-12-03 13:37:40 +00:00
/// Create directory.
virtual void createDirectory(const String & path) = 0;
2019-11-27 09:39:44 +00:00
2019-12-03 13:37:40 +00:00
/// Create directory and all parent directories if necessary.
virtual void createDirectories(const String & path) = 0;
2019-11-27 09:39:44 +00:00
/// Remove all files from the directory.
virtual void clearDirectory(const String & path) = 0;
/// Move directory from `from_path` to `to_path`.
virtual void moveDirectory(const String & from_path, const String & to_path) = 0;
2019-12-03 13:37:40 +00:00
/// Return iterator to the contents of the specified directory.
virtual DiskDirectoryIteratorPtr iterateDirectory(const String & path) = 0;
2019-11-27 09:39:44 +00:00
/// Return `true` if the specified directory is empty.
bool isDirectoryEmpty(const String & path);
2019-12-03 13:37:40 +00:00
/// Move the file from `from_path` to `to_path`.
virtual void moveFile(const String & from_path, const String & to_path) = 0;
2019-11-27 09:39:44 +00:00
2019-12-03 13:37:40 +00:00
/// Copy the file from `from_path` to `to_path`.
virtual void copyFile(const String & from_path, const String & to_path) = 0;
2019-11-27 09:39:44 +00:00
2019-12-03 13:37:40 +00:00
/// Open the file for read and return ReadBuffer object.
virtual std::unique_ptr<ReadBuffer> read(const String & path, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE) const = 0;
2019-11-27 09:39:44 +00:00
2019-12-03 13:37:40 +00:00
/// Open the file for write and return WriteBuffer object.
virtual std::unique_ptr<WriteBuffer> write(const String & path, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE) = 0;
/// Open the file for write in append mode and return WriteBuffer object.
virtual std::unique_ptr<WriteBuffer> append(const String & path, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE) = 0;
2019-11-27 09:39:44 +00:00
};
2019-12-03 13:37:40 +00:00
using DiskPtr = std::shared_ptr<IDisk>;
using Disks = std::vector<DiskPtr>;
/**
* Iterator of directory contents on particular disk.
*/
class IDiskDirectoryIterator
2019-11-27 09:39:44 +00:00
{
public:
2019-12-03 13:37:40 +00:00
/// Iterate to the next file.
2019-11-27 09:39:44 +00:00
virtual void next() = 0;
2019-12-03 13:37:40 +00:00
/// Return `true` if the iterator points to a valid element.
2019-11-27 09:39:44 +00:00
virtual bool isValid() const = 0;
2019-12-03 13:37:40 +00:00
/// Name of the file that the iterator currently points to.
virtual String name() const = 0;
2019-11-27 09:39:44 +00:00
2019-12-03 13:37:40 +00:00
virtual ~IDiskDirectoryIterator() = default;
2019-11-27 09:39:44 +00:00
};
/**
2019-11-27 15:49:15 +00:00
* Information about reserved size on particular disk.
2019-11-27 09:39:44 +00:00
*/
class IReservation
{
public:
/// Get reservation size.
2019-12-03 13:37:40 +00:00
virtual UInt64 getSize() const = 0;
2019-11-27 09:39:44 +00:00
/// Get disk where reservation take place.
2019-12-03 13:37:40 +00:00
virtual DiskPtr getDisk() const = 0;
2019-11-27 09:39:44 +00:00
/// Changes amount of reserved space.
virtual void update(UInt64 new_size) = 0;
/// Unreserves reserved space.
virtual ~IReservation() = default;
};
2019-12-03 13:37:40 +00:00
/// Return full path to a file on disk.
inline String fullPath(const DiskPtr & disk, const String & path)
{
return disk->getPath() + path;
}
2019-11-27 09:39:44 +00:00
}