1#include <Functions/IFunctionImpl.h>
2#include <Functions/FunctionFactory.h>
3#include <DataTypes/DataTypesNumber.h>
4#include <Interpreters/Context.h>
5#include <filesystem>
6#include <Poco/Util/AbstractConfiguration.h>
7
8namespace DB
9{
10
11struct FilesystemAvailable
12{
13 static constexpr auto name = "filesystemAvailable";
14 static std::uintmax_t get(std::filesystem::space_info & spaceinfo) { return spaceinfo.available; }
15};
16
17struct FilesystemFree
18{
19 static constexpr auto name = "filesystemFree";
20 static std::uintmax_t get(std::filesystem::space_info & spaceinfo) { return spaceinfo.free; }
21};
22
23struct FilesystemCapacity
24{
25 static constexpr auto name = "filesystemCapacity";
26 static std::uintmax_t get(std::filesystem::space_info & spaceinfo) { return spaceinfo.capacity; }
27};
28
29template <typename Impl>
30class FilesystemImpl : public IFunction
31{
32public:
33 static constexpr auto name = Impl::name;
34
35 static FunctionPtr create(const Context & context)
36 {
37 return std::make_shared<FilesystemImpl<Impl>>(std::filesystem::space(context.getConfigRef().getString("path")));
38 }
39
40 explicit FilesystemImpl(std::filesystem::space_info spaceinfo_) : spaceinfo(spaceinfo_) { }
41
42 String getName() const override { return name; }
43 size_t getNumberOfArguments() const override { return 0; }
44 bool isDeterministic() const override { return false; }
45
46 DataTypePtr getReturnTypeImpl(const DataTypes & /*arguments*/) const override
47 {
48 return std::make_shared<DataTypeUInt64>();
49 }
50
51 void executeImpl(Block & block, const ColumnNumbers &, size_t result, size_t input_rows_count) override
52 {
53 block.getByPosition(result).column = DataTypeUInt64().createColumnConst(input_rows_count, static_cast<UInt64>(Impl::get(spaceinfo)));
54 }
55
56private:
57 std::filesystem::space_info spaceinfo;
58};
59
60
61void registerFunctionFilesystem(FunctionFactory & factory)
62{
63 factory.registerFunction<FilesystemImpl<FilesystemAvailable>>();
64 factory.registerFunction<FilesystemImpl<FilesystemCapacity>>();
65 factory.registerFunction<FilesystemImpl<FilesystemFree>>();
66}
67
68}
69