-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathlive.cpp
More file actions
146 lines (125 loc) · 4.61 KB
/
Copy pathlive.cpp
File metadata and controls
146 lines (125 loc) · 4.61 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
#include "databento/live.hpp"
#include <chrono>
#include <utility> // move
#include "databento/constants.hpp" // kApiKeyLength
#include "databento/detail/buffer.hpp" // kDefaultBufSize
#include "databento/exceptions.hpp" // InvalidArgumentError, LiveApiError
#include "databento/live_blocking.hpp" // LiveBlocking
#include "databento/live_threaded.hpp" // LiveThreaded
#include "databento/log.hpp"
using databento::LiveBuilder;
LiveBuilder::LiveBuilder() : buffer_size_{detail::Buffer::kDefaultBufSize} {}
LiveBuilder& LiveBuilder::SetKeyFromEnv() {
char const* env_key = std::getenv("DATABENTO_API_KEY");
if (env_key == nullptr) {
throw Exception{"Expected environment variable DATABENTO_API_KEY to be set"};
}
return this->SetKey(env_key);
}
LiveBuilder& LiveBuilder::SetKey(std::string key) {
if (key.length() != kApiKeyLength) {
throw InvalidArgumentError{
"LiveBuilder::SetKey", "key",
"Must contain " + std::to_string(kApiKeyLength) + " characters"};
}
key_ = std::move(key);
return *this;
}
LiveBuilder& LiveBuilder::SetDataset(std::string dataset) {
dataset_ = std::move(dataset);
return *this;
}
LiveBuilder& LiveBuilder::SetDataset(Dataset dataset) {
dataset_ = ToString(dataset);
return *this;
}
LiveBuilder& LiveBuilder::SetSendTsOut(bool send_ts_out) {
send_ts_out_ = send_ts_out;
return *this;
}
LiveBuilder& LiveBuilder::SetUpgradePolicy(VersionUpgradePolicy upgrade_policy) {
upgrade_policy_ = upgrade_policy;
return *this;
}
LiveBuilder& LiveBuilder::SetLogReceiver(databento::ILogReceiver* log_receiver) {
log_receiver_ = log_receiver;
return *this;
}
LiveBuilder& LiveBuilder::SetHeartbeatInterval(
std::chrono::seconds heartbeat_interval) {
heartbeat_interval_ = heartbeat_interval;
return *this;
}
LiveBuilder& LiveBuilder::SetCompression(Compression compression) {
compression_ = compression;
return *this;
}
LiveBuilder& LiveBuilder::SetSlowReaderBehavior(
SlowReaderBehavior slow_reader_behavior) {
slow_reader_behavior_ = slow_reader_behavior;
return *this;
}
LiveBuilder& LiveBuilder::SetAddress(std::string gateway, std::uint16_t port) {
gateway_ = std::move(gateway);
port_ = port;
return *this;
}
LiveBuilder& LiveBuilder::SetBufferSize(std::size_t size) {
buffer_size_ = size;
return *this;
}
LiveBuilder& LiveBuilder::ExtendUserAgent(std::string extension) {
user_agent_ext_ = std::move(extension);
return *this;
}
LiveBuilder& LiveBuilder::SetTimeoutConf(TimeoutConf timeout_conf) {
timeout_conf_ = timeout_conf;
return *this;
}
databento::LiveBlocking LiveBuilder::BuildBlocking() {
Validate();
if (gateway_.empty()) {
return databento::LiveBlocking{log_receiver_, key_,
dataset_, send_ts_out_,
upgrade_policy_, heartbeat_interval_,
buffer_size_, user_agent_ext_,
compression_, slow_reader_behavior_,
timeout_conf_};
}
return databento::LiveBlocking{log_receiver_, key_,
dataset_, gateway_,
port_, send_ts_out_,
upgrade_policy_, heartbeat_interval_,
buffer_size_, user_agent_ext_,
compression_, slow_reader_behavior_,
timeout_conf_};
}
databento::LiveThreaded LiveBuilder::BuildThreaded() {
Validate();
if (gateway_.empty()) {
return databento::LiveThreaded{log_receiver_, key_,
dataset_, send_ts_out_,
upgrade_policy_, heartbeat_interval_,
buffer_size_, user_agent_ext_,
compression_, slow_reader_behavior_,
timeout_conf_};
}
return databento::LiveThreaded{log_receiver_, key_,
dataset_, gateway_,
port_, send_ts_out_,
upgrade_policy_, heartbeat_interval_,
buffer_size_, user_agent_ext_,
compression_, slow_reader_behavior_,
timeout_conf_};
}
void LiveBuilder::Validate() {
if (key_.empty()) {
throw Exception{"'key' is unset"};
}
if (dataset_.empty()) {
throw Exception{"'dataset' is unset"};
}
if (log_receiver_ == nullptr) {
log_receiver_ = databento::ILogReceiver::Default();
}
}