Skip to content

Commit 82956c6

Browse files
committed
Clickhouse - add common
1 parent 14ae576 commit 82956c6

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @file
3+
* @author Michal Sedlak <[email protected]>
4+
* @brief Common functionality and general utilities
5+
* @date 2024
6+
*
7+
* Copyright(c) 2024 CESNET z.s.p.o.
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
11+
#include "common.h"
12+
13+
#include <cctype>
14+
#include <iostream>
15+
16+
std::string to_lower(std::string str)
17+
{
18+
for (auto &c : str) {
19+
c = std::tolower(c);
20+
}
21+
return str;
22+
}
23+
24+
void debug_print_block(const clickhouse::Block& block) {
25+
std::cout << "================================================================================\n";
26+
for (size_t i = 0; i < block.GetColumnCount(); i++) {
27+
std::cout << block.GetColumnName(i) << ":"
28+
<< block[i]->GetType().GetName()
29+
<< (i < block.GetColumnCount() - 1 ? '\t' : '\n');
30+
}
31+
std::cout << "--------------------------------------------------------------------------------\n";
32+
33+
for (size_t i = 0; i < block.GetRowCount(); i++) {
34+
for (size_t j = 0; j < block.GetColumnCount(); j++) {
35+
switch (block[j]->GetType().GetCode()) {
36+
case clickhouse::Type::Int8:
37+
std::cout << +block[j]->As<clickhouse::ColumnInt8>()->At(i);
38+
break;
39+
case clickhouse::Type::Int16:
40+
std::cout << block[j]->As<clickhouse::ColumnInt16>()->At(i);
41+
break;
42+
case clickhouse::Type::Int32:
43+
std::cout << block[j]->As<clickhouse::ColumnInt32>()->At(i);
44+
break;
45+
case clickhouse::Type::Int64:
46+
std::cout << block[j]->As<clickhouse::ColumnInt64>()->At(i);
47+
break;
48+
case clickhouse::Type::Int128:
49+
std::cout << block[j]->As<clickhouse::ColumnInt128>()->At(i);
50+
break;
51+
case clickhouse::Type::UInt8:
52+
std::cout << +block[j]->As<clickhouse::ColumnUInt8>()->At(i);
53+
break;
54+
case clickhouse::Type::UInt16:
55+
std::cout << block[j]->As<clickhouse::ColumnUInt16>()->At(i);
56+
break;
57+
case clickhouse::Type::UInt32:
58+
std::cout << block[j]->As<clickhouse::ColumnUInt32>()->At(i);
59+
break;
60+
case clickhouse::Type::UInt64:
61+
std::cout << block[j]->As<clickhouse::ColumnUInt64>()->At(i);
62+
break;
63+
case clickhouse::Type::String:
64+
std::cout << block[j]->As<clickhouse::ColumnString>()->At(i);
65+
break;
66+
default:
67+
std::cout << "-";
68+
break;
69+
}
70+
std::cout << (j < block.GetColumnCount() - 1 ? '\t' : '\n');
71+
}
72+
}
73+
std::cout << "================================================================================\n\n";
74+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @file
3+
* @author Michal Sedlak <[email protected]>
4+
* @brief Common functionality and general utilities
5+
* @date 2024
6+
*
7+
* Copyright(c) 2024 CESNET z.s.p.o.
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
#pragma once
11+
12+
#include "clickhouse.h"
13+
14+
#include <fmt/core.h>
15+
#include <ipfixcol2.h>
16+
17+
#include <stdexcept>
18+
19+
/**
20+
* @class Nonmoveable
21+
* @brief Classes that will inherit from this class will be immovable
22+
*/
23+
struct Nonmoveable {
24+
Nonmoveable() = default;
25+
Nonmoveable(Nonmoveable&&) = delete;
26+
Nonmoveable& operator=(Nonmoveable&&) = delete;
27+
};
28+
29+
/**
30+
* @class Noncopyable
31+
* @brief Classes that will inherit from this class will not be able to be copied
32+
*/
33+
struct Noncopyable {
34+
Noncopyable() = default;
35+
Noncopyable(const Noncopyable&) = delete;
36+
Noncopyable& operator=(const Noncopyable&) = delete;
37+
};
38+
39+
/**
40+
* @class Error
41+
* @brief An exception type that will be thrown by the plugin
42+
*/
43+
class Error : public std::runtime_error {
44+
public:
45+
template <typename ...Args>
46+
Error(Args&& ...args) : std::runtime_error(fmt::format(args...)) {}
47+
};
48+
49+
50+
51+
class Logger {
52+
public:
53+
Logger(const ipx_ctx_t *ctx) : m_ctx(ctx) {}
54+
template <typename ...Args> void info (const char *fmt, Args... args) { IPX_CTX_INFO (m_ctx, fmt, args..., 0); }
55+
template <typename ...Args> void warning(const char *fmt, Args... args) { IPX_CTX_WARNING(m_ctx, fmt, args..., 0); }
56+
template <typename ...Args> void error (const char *fmt, Args... args) { IPX_CTX_ERROR (m_ctx, fmt, args..., 0); }
57+
template <typename ...Args> void debug (const char *fmt, Args... args) { IPX_CTX_DEBUG (m_ctx, fmt, args..., 0); }
58+
59+
private:
60+
const ipx_ctx_t *m_ctx;
61+
};
62+
63+
64+
/**
65+
* @brief Convert string to lowercase
66+
*
67+
* @param str The string
68+
*/
69+
std::string to_lower(std::string s);
70+
71+
/**
72+
* @brief Debug function for printing contents of a block
73+
*
74+
* @param block The block
75+
*/
76+
void debug_print_block(const clickhouse::Block& block);

0 commit comments

Comments
 (0)