1#pragma once
2
3#include <Core/Types.h>
4#include <Common/CurrentMetrics.h>
5#include <Common/Exception.h>
6
7#include <memory>
8#include <utility>
9#include <boost/noncopyable.hpp>
10
11
12namespace CurrentMetrics
13{
14extern const Metric DiskSpaceReservedForMerge;
15}
16
17namespace DB
18{
19class IDiskDirectoryIterator;
20using DiskDirectoryIteratorPtr = std::unique_ptr<IDiskDirectoryIterator>;
21
22class IReservation;
23using ReservationPtr = std::unique_ptr<IReservation>;
24
25class ReadBuffer;
26class WriteBuffer;
27
28/**
29 * Provide interface for reservation.
30 */
31class Space : public std::enable_shared_from_this<Space>
32{
33public:
34 /// Return the name of the space object.
35 virtual const String & getName() const = 0;
36
37 /// Reserve the specified number of bytes.
38 virtual ReservationPtr reserve(UInt64 bytes) = 0;
39
40 virtual ~Space() = default;
41};
42
43using SpacePtr = std::shared_ptr<Space>;
44
45/**
46 * A unit of storage persisting data and metadata.
47 * Abstract underlying storage technology.
48 * Responsible for:
49 * - file management;
50 * - space accounting and reservation.
51 */
52class IDisk : public Space
53{
54public:
55 /// Root path for all files stored on the disk.
56 /// It's not required to be a local filesystem path.
57 virtual const String & getPath() const = 0;
58
59 /// Total available space on the disk.
60 virtual UInt64 getTotalSpace() const = 0;
61
62 /// Space currently available on the disk.
63 virtual UInt64 getAvailableSpace() const = 0;
64
65 /// Space available for reservation (available space minus reserved space).
66 virtual UInt64 getUnreservedSpace() const = 0;
67
68 /// Amount of bytes which should be kept free on the disk.
69 virtual UInt64 getKeepingFreeSpace() const { return 0; }
70
71 /// Return `true` if the specified file exists.
72 virtual bool exists(const String & path) const = 0;
73
74 /// Return `true` if the specified file exists and it's a regular file (not a directory or special file type).
75 virtual bool isFile(const String & path) const = 0;
76
77 /// Return `true` if the specified file exists and it's a directory.
78 virtual bool isDirectory(const String & path) const = 0;
79
80 /// Create directory.
81 virtual void createDirectory(const String & path) = 0;
82
83 /// Create directory and all parent directories if necessary.
84 virtual void createDirectories(const String & path) = 0;
85
86 /// Return iterator to the contents of the specified directory.
87 virtual DiskDirectoryIteratorPtr iterateDirectory(const String & path) = 0;
88
89 /// Move the file from `from_path` to `to_path`.
90 virtual void moveFile(const String & from_path, const String & to_path) = 0;
91
92 /// Copy the file from `from_path` to `to_path`.
93 virtual void copyFile(const String & from_path, const String & to_path) = 0;
94
95 /// Open the file for read and return ReadBuffer object.
96 virtual std::unique_ptr<ReadBuffer> readFile(const String & path) const = 0;
97
98 /// Open the file for write and return WriteBuffer object.
99 virtual std::unique_ptr<WriteBuffer> writeFile(const String & path) = 0;
100};
101
102using DiskPtr = std::shared_ptr<IDisk>;
103using Disks = std::vector<DiskPtr>;
104
105/**
106 * Iterator of directory contents on particular disk.
107 */
108class IDiskDirectoryIterator
109{
110public:
111 /// Iterate to the next file.
112 virtual void next() = 0;
113
114 /// Return `true` if the iterator points to a valid element.
115 virtual bool isValid() const = 0;
116
117 /// Name of the file that the iterator currently points to.
118 virtual String name() const = 0;
119
120 virtual ~IDiskDirectoryIterator() = default;
121};
122
123/**
124 * Information about reserved size on particular disk.
125 */
126class IReservation
127{
128public:
129 /// Get reservation size.
130 virtual UInt64 getSize() const = 0;
131
132 /// Get disk where reservation take place.
133 virtual DiskPtr getDisk() const = 0;
134
135 /// Changes amount of reserved space.
136 virtual void update(UInt64 new_size) = 0;
137
138 /// Unreserves reserved space.
139 virtual ~IReservation() = default;
140};
141
142/// Return full path to a file on disk.
143inline String fullPath(const DiskPtr & disk, const String & path)
144{
145 return disk->getPath() + path;
146}
147
148}
149