-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbtrdb_util.h
More file actions
149 lines (129 loc) · 4.39 KB
/
btrdb_util.h
File metadata and controls
149 lines (129 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef BTRDB_UTIL_H_
#define BTRDB_UTIL_H_
#include <cstdlib>
#include <functional>
#include <mutex>
#include <condition_variable>
#include <vector>
#include <grpc++/grpc++.h>
#include "btrdb.grpc.pb.h"
namespace btrdb {
/* Some useful constants. */
const constexpr std::size_t UUID_NUM_BYTES = 16;
/* Structures for BTrDB data. */
struct RawPoint {
std::int64_t time;
double value;
};
struct StatisticalPoint {
std::int64_t time;
double min;
double mean;
double max;
std::uint64_t count;
};
struct ChangedRange {
std::int64_t start;
std::int64_t end;
};
/* Interface to keep track of data for pending async requests. */
class AsyncRequest {
public:
virtual ~AsyncRequest() {}
virtual bool process_batch() = 0;
virtual void end_request() = 0;
};
/* Status type. */
class Status {
public:
Status();
Status(const grpc::Status& grpcstatus);
Status(std::uint32_t code, std::string message);
Status(const grpcinterface::Status& btrdbstatus);
bool isError() const;
std::uint32_t code() const;
std::string message() const;
template <typename ResponseType>
static Status fromResponse(grpc::Status& grpcstatus, ResponseType& response) {
if (!grpcstatus.ok()) {
return Status(grpcstatus);
} else if (response.has_stat()) {
const grpcinterface::Status& btrdbstatus = response.stat();
if (btrdbstatus.code() != 0) {
return Status(btrdbstatus);
}
return Status(response.stat());
}
return Status();
}
/* Some useful constants. */
static const Status Disconnected;
static const Status ClusterDegraded;
static const Status WrongArgs;
static const Status NoSuchStream;
private:
enum Type {
StatusOK,
GRPCError,
CodedError
};
Type type_;
std::uint32_t code_;
std::string message_;
};
/* Some useful functions. */
std::vector<std::string> split_string(const std::string& str, char delimiter);
template <typename V, typename... ExtraArgs>
Status async_to_sync(std::function<Status(std::function<void(bool, Status, V, ExtraArgs...)>)> async_fn, std::function<void(bool, Status, V, ExtraArgs...)> worker) {
bool done = false;
std::mutex condvar_mutex;
std::condition_variable condvar;
Status status;
auto callback = [&](bool finished, Status stat, V data, ExtraArgs... extra_args) {
if (stat.isError()) {
status = stat;
}
worker(finished, stat, data, extra_args...);
if (finished) {
std::lock_guard<std::mutex> lock(condvar_mutex);
done = true;
condvar.notify_one();
}
};
status = async_fn(std::move(callback));
if (status.isError()) {
return status;
}
{
std::unique_lock<std::mutex> lock(condvar_mutex);
while (!done) {
condvar.wait(lock);
}
}
return status;
}
template <typename V>
std::function<void(bool, Status, std::vector<V>&)> collect_worker(std::vector<V>* result) {
return [=](bool finished, Status stat, std::vector<V>& data) {
(void) finished;
auto num_points = data.size();
result->reserve(result->size() + num_points);
for (typename std::vector<V>::size_type i = 0; i != num_points; i++) {
result->push_back(std::move(data[i]));
}
};
}
template <typename V>
std::function<void(bool, Status, std::vector<V>&, std::uint64_t)> collect_vernum_worker(std::vector<V>* result, std::uint64_t* version_ptr) {
return [=](bool finished, Status stat, std::vector<V>& data, std::uint64_t version) {
(void) finished;
auto num_points = data.size();
result->reserve(result->size() + num_points);
for (typename std::vector<V>::size_type i = 0; i != num_points; i++) {
result->push_back(std::move(data[i]));
}
*version_ptr = version;
};
}
}
#endif // BTRDB_UTIL_H_