src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 / 203
0.0%
Branches:
0 / 614
0.0%
Functions:
0 / 9
0.0%

storage/qdrant_client.cpp
Line Branch Exec Source
1 #include "qdrant_client.hpp"
2 #include <format>
3 #include <nlohmann/json.hpp>
4 #include <string>
5 #include <vector>
6
7 using nlohmann::json_abi_v3_11_2::json;
8
9 /**
10 * Initializes a QdrantClient instance with the provided UI and networking utilities.
11 * Constructs the base URL from the QDRANT_URL environment variable, defaulting to the local Qdrant service.
12 * Performs an immediate health check of the Qdrant service upon construction.
13 *
14 * @param ui Pointer to a UserInterface instance for status message output.
15 * @param curl Pointer to a CurlUtils instance for HTTP communication with Qdrant.
16 * @param system_utils Pointer to a SustemUtils instance for `system()` call status handling.
17 *
18 * @note Constructor calls check_qdrant() as a side effect, which may produce console or UI output.
19 */
20 ✗ QdrantClient::QdrantClient(std::shared_ptr<UserInterface> ui, std::shared_ptr<CurlUtils> curl,
21 ✗ std::shared_ptr<SystemUtils> system_utils)
22 ✗ : ui(ui), curl(curl), system_utils(system_utils) {
23
24
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ this->setup();
25
26
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ sleep(1);
27
28
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ const std::string& base_url = std::getenv("QDRANT_URL") ? std::getenv("QDRANT_URL") : "http://localhost:6333";
29 this->urls = {
30 collections_url : std::format("{}/collections", base_url),
31 ✗ };
32
33
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ this->check_qdrant();
34
35
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
36
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ UserInterface::print_message_noninteractive("\n");
37 }
38
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ }
39
40 /**
41 * Cleans up Docker resources associated with the Qdrant container and network.
42 * Stops and removes the qdrant-rag-app container and destroys the app-rag-net Docker network.
43 * Suppresses errors during cleanup to ensure all resources are removed even if individual commands fail.
44 *
45 * @note This destructor is called automatically when the QdrantClient instance is destroyed.
46 */
47 ✗ QdrantClient::~QdrantClient() {
48 int status;
49 ✗ std::string message{};
50
51 ✗ message = "🛈 Qdrant docker container cleanup...\n";
52
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (this->ui->backend_running() == false) {
53 ✗ UserInterface::print_message_noninteractive(message);
54 } else {
55 ✗ this->ui->print_message(message);
56 }
57
58 ✗ status = system("docker stop qdrant-rag-app >/dev/null 2>&1 || true");
59 ✗ this->system_utils->catch_error(status);
60
61 ✗ status = system("docker rm qdrant-rag-app >/dev/null 2>&1 || true");
62 ✗ this->system_utils->catch_error(status);
63
64 ✗ status = system("docker network rm app-rag-net >/dev/null 2>&1 || true");
65 ✗ this->system_utils->catch_error(status);
66
67 ✗ message = "Done.\n";
68
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (this->ui->backend_running() == false) {
69 ✗ UserInterface::print_success_noninteractive(message);
70 } else {
71 ✗ this->ui->print_success(message);
72 }
73 ✗ }
74
75 /**
76 * Retrieves a URL value by property name as a const reference to a std::string.
77 *
78 * @param property_name: Name of the URL property to look up; the only supported value is "collections_url".
79 * @return: Const reference to the matching URL string stored in the urls member, valid for as long as this QdrantClient
80 * instance is alive.
81 * @throws std::runtime_error: If property_name is not a supported URL property.
82 *
83 * Compares property_name against "collections_url" and returns a reference to the corresponding field of urls on a
84 * match.
85 *
86 * Note: The returned reference refers into urls, so it must not outlive the QdrantClient instance. Because this class
87 * exposes only one URL property, any other name throws, which makes the lookup a single comparison rather than a loop
88 * or map search.
89 */
90 ✗ const std::string& QdrantClient::get_url_value(const std::string& property_name) const {
91
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ if (property_name == "collections_url") {
92 ✗ return this->urls.collections_url;
93 }
94
95
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ throw std::runtime_error("Property not supported: " + property_name);
96 }
97
98 /**
99 * Initializes and starts the Qdrant Docker container with necessary configuration.
100 * Pulls the Qdrant image, validates the QDRANT_API_KEY and QDRANT_API_KEY_READ_ONLY environment variables,
101 * creates a persistent data directory, establishes a Docker network, and launches the container with API key
102 * authentication. If a Qdrant container is already running, this method returns early without restarting it.
103 *
104 * @throws Exits with status code 1 if required environment variables are missing or container startup fails.
105 *
106 * @note This method should be called before check_qdrant() to ensure the service is available.
107 * @note Does not wait for the container to be fully operational; health verification is delegated to check_qdrant().
108 */
109 ✗ void QdrantClient::setup() {
110 int status;
111 ✗ std::string message{};
112
113
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ status = system("docker pull qdrant/qdrant");
114
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->system_utils->catch_error(status);
115
116
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ status = system("docker ps | grep -q qdrant");
117
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (status == 0) {
118
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = "Qdrant is already running. Skipping docker run.\n";
119
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
120
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_success_noninteractive(message);
121 } else {
122
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_success(message);
123 }
124 ✗ return;
125 }
126
127
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = "🛈 Starting Qdrant container...\n";
128
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
129
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(message);
130 } else {
131
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_message(message);
132 }
133
134 ✗ const char* qdrant_key = std::getenv("QDRANT_API_KEY");
135 ✗ const char* qdrant_key_readonly = std::getenv("QDRANT_API_KEY_READ_ONLY");
136
137
0/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ if (!qdrant_key || std::string(qdrant_key).empty()) {
138
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = "QDRANT_API_KEY not set.\n";
139
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
140
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
141 } else {
142
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_error(message);
143 }
144
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->end_session(EXIT_FAILURE);
145 }
146
147
0/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ if (!qdrant_key_readonly || std::string(qdrant_key_readonly).empty()) {
148
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = "QDRANT_API_KEY_READ_ONLY not set.\n";
149
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
150
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
151 } else {
152
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_error(message);
153 }
154
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->end_session(EXIT_FAILURE);
155 }
156
157
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ const std::string data_dir = std::string(std::getenv("HOME")) + "/qdrant";
158
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ std::string mkdir_cmd = std::string("mkdir -p ") + data_dir;
159
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ status = system(mkdir_cmd.c_str());
160
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->system_utils->catch_error(status);
161
162
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ status = system("docker network create app-rag-net 2>/dev/null || true");
163
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->system_utils->catch_error(status);
164
165 std::string docker_run_cmd =
166 std::format("docker run -d --name qdrant-rag-app --restart always --network app-rag-net "
167 "-p 6333:6333 -p 6334:6334 -v {}:/qdrant/storage "
168 "-e QDRANT_API_KEY='{}' -e QDRANT_API_KEY_READ_ONLY='{}' qdrant/qdrant:latest",
169
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ data_dir, qdrant_key, qdrant_key_readonly);
170
171
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (system(docker_run_cmd.c_str()) != 0) {
172
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = "Failed to start Qdrant container.\n";
173
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
174
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
175 } else {
176
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_error(message);
177 }
178
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->end_session(EXIT_FAILURE);
179 }
180
181
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ message = std::format("🛈 Qdrant container started with data dir {}\n", data_dir.c_str());
182
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
183
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(message);
184 } else {
185
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_message(message);
186 }
187
188
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = "🛈 Qdrant dashboard is available at http://localhost:6333/dashboard\n";
189
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
190
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(message);
191 } else {
192
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_message(message);
193 }
194
0/2
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ }
195
196 /**
197 * Checks the health status of the Qdrant service using the configured API endpoint.
198 * Retrieves the API key from the QDRANT_API_KEY environment variable for authentication.
199 * Outputs a status message (success, error, or warning) to the UI or console based on the HTTP response code.
200 */
201 ✗ void QdrantClient::check_qdrant() {
202 ✗ std::string message{};
203
204 ✗ const char* qdrant_key = std::getenv("QDRANT_API_KEY");
205
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ std::map<std::string, std::string> headers{{"api-key", qdrant_key}};
206
207
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ long status = this->curl->wait_for_service(this->urls.collections_url, headers);
208
209
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (status == 200) {
210
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = "Qdrant is healthy.\n";
211
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
212
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_success_noninteractive(message);
213 } else {
214
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_success(message);
215 }
216
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (status == -1) {
217
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = "Failed to connect to Qdrant.\n";
218
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
219
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
220 } else {
221
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_error(message);
222 }
223 } else {
224
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = std::format("⚠️ Qdrant returned {}.\n", status);
225
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
226
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(message);
227 } else {
228
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_message(message);
229 }
230 }
231
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ };
232
233 /**
234 * @brief Checks if a collection exists in Qdrant.
235 * @param collection_name The name of the collection to check.
236 * @return true if the collection exists, false otherwise.
237 */
238 ✗ bool QdrantClient::collection_exists(const std::string& collection_name) {
239 ✗ std::string message{};
240
241
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::string url = std::format("{}/{}", this->urls.collections_url, collection_name);
242
243 ✗ const char* qdrant_key = std::getenv("QDRANT_API_KEY");
244
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ std::map<std::string, std::string> headers{{"api-key", qdrant_key}};
245
246 try {
247
0/6
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ this->curl->fetch_web_content(url, std::nullopt, "GET", headers, false);
248
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = std::format("Collection '{}' exists", collection_name);
249
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
250
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_success_noninteractive(message);
251 } else {
252
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_success(message);
253 }
254 ✗ return true;
255 ✗ } catch (...) {
256
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = std::format("⚠️ Collection '{}' does not exist", collection_name);
257
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
258
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(message);
259 } else {
260
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_message(message);
261 }
262 ✗ return false;
263
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ }
264
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ }
265
266 /**
267 * @brief Deletes a collection from Qdrant.
268 * @param collection_name The name of the collection to delete.
269 */
270 ✗ void QdrantClient::delete_collection(const std::string& collection_name) {
271
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::string url = std::format("{}/{}", this->urls.collections_url, collection_name);
272
273 ✗ const char* qdrant_key = std::getenv("QDRANT_API_KEY");
274
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ std::map<std::string, std::string> headers{{"api-key", qdrant_key}};
275
276 try {
277
0/6
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ this->curl->fetch_web_content(url, std::nullopt, "DELETE", headers);
278
279
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::string message = std::format("Deleted collection '{}'\n", collection_name);
280
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
281
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_success_noninteractive(message);
282 } else {
283
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_success(message);
284 }
285
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ } catch (const HttpException& e) {
286 std::string message =
287
0/2
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ std::format("[{}] Failed to delete collection '{}': {}\n", e.get_status_code(), collection_name, e.what());
288
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
289
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
290 } else {
291
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_error(message);
292 }
293 ✗ throw;
294 ✗ }
295
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ }
296
297 /**
298 * @brief Creates a new collection in Qdrant.
299 * @param collection_name The name of the collection to create.
300 * @param vector_size The dimensionality of the vectors (e.g., 384 for embeddinggemma:latest).
301 */
302 ✗ void QdrantClient::create_collection(const std::string& collection_name, size_t vector_size) {
303
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ json config = {{"vectors", {{"size", vector_size}, {"distance", "Cosine"}}}};
304
305 ✗ std::string url = std::format("{}/{}", this->urls.collections_url, collection_name);
306
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::string post_data = config.dump();
307
308 ✗ const char* qdrant_key = std::getenv("QDRANT_API_KEY");
309
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ std::map<std::string, std::string> headers{{"api-key", qdrant_key}};
310
311 try {
312
0/8
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ this->curl->fetch_web_content(url, post_data, "PUT", headers);
313
314 std::string message =
315
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::format("Created collection '{}' with vector size {}\n", collection_name, vector_size);
316
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
317
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_success_noninteractive(message);
318 } else {
319
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_success(message);
320 }
321
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ } catch (const HttpException& e) {
322 std::string message =
323
0/2
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ std::format("[{}] Failed to create collection '{}': {}\n", e.get_status_code(), collection_name, e.what());
324
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
325
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
326 } else {
327
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_error(message);
328 }
329 ✗ throw;
330 ✗ }
331
0/62
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 53 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
✗ Branch 57 not taken.
✗ Branch 58 not taken.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
✗ Branch 61 not taken.
✗ Branch 62 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✗ Branch 66 not taken.
✗ Branch 67 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
✗ Branch 71 not taken.
✗ Branch 72 not taken.
✗ Branch 74 not taken.
✗ Branch 75 not taken.
✗ Branch 76 not taken.
✗ Branch 77 not taken.
✗ Branch 79 not taken.
✗ Branch 80 not taken.
✗ Branch 81 not taken.
✗ Branch 82 not taken.
✗ }
332
333 /**
334 * Stores text documents and their corresponding embeddings into the Qdrant collection.
335 * Constructs points from the provided texts and embeddings, then sends them to the Qdrant service via PUT request.
336 * Authentication is performed using the QDRANT_API_KEY environment variable.
337 *
338 * @param texts A vector of text strings to be stored as payloads in Qdrant points.
339 * @param embeddings A vector of embedding vectors (floats) corresponding to each text string.
340 *
341 * @note The size of texts and embeddings vectors should match; behavior is undefined if they differ.
342 */
343 ✗ void QdrantClient::store(const std::vector<std::string>& texts, const std::vector<std::vector<float>>& embeddings) {
344 ✗ std::string message{};
345
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ const std::string collection_name = "rag-app";
346
347
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ if (texts.empty() || embeddings.empty()) {
348
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ message = "Cannot store: texts or embeddings are empty\n";
349
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
350
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
351 } else {
352
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_error(message);
353 }
354 ✗ return;
355 }
356
357
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (texts.size() != embeddings.size()) {
358
0/2
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ message = std::format("Mismatch: {} texts but {} embeddings\n", texts.size(), embeddings.size());
359
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
360
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_error_noninteractive(message);
361 } else {
362
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_error(message);
363 }
364 ✗ return;
365 }
366
367 ✗ size_t vector_size = embeddings[0].size();
368
369 #ifdef DEBUG
370
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
371
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(
372
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::format("\t[DEBUG] Checking collection '{}'\n", collection_name));
373 }
374 #endif
375
376
0/4
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ if (this->collection_exists(collection_name)) {
377 #ifdef DEBUG
378
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
379
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(
380
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::format("\t[DEBUG] Collection '{}' exists, deleting...\n", collection_name));
381 }
382 #endif
383
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ this->delete_collection(collection_name);
384 }
385
386 #ifdef DEBUG
387
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
388
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(
389
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::format("\t[DEBUG] Creating collection '{}' with vector size {}\n", collection_name, vector_size));
390 }
391 #endif
392
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ this->create_collection(collection_name, vector_size);
393
394
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ json points = json::array();
395
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ for (size_t i = 0; i < texts.size(); i += 1) {
396
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ points.push_back({{"id", i}, {"vector", embeddings[i]}, {"payload", {{"text", texts[i]}}}});
397 }
398
399
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ json request_payload = {{"points", points}};
400
401 ✗ std::string url = std::format("{}/{}/points", this->urls.collections_url, collection_name);
402
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ std::string post_data = request_payload.dump();
403
404 ✗ const char* qdrant_key = std::getenv("QDRANT_API_KEY");
405
0/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ std::map<std::string, std::string> headers{{"api-key", qdrant_key}};
406
407 #ifdef DEBUG
408
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
409
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_message_noninteractive(
410
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ std::format("\t[DEBUG] Storing {} embeddings to URL: {}\n", texts.size(), url));
411 }
412 #endif
413
0/8
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ std::string response = this->curl->fetch_web_content(url, post_data, "PUT", headers);
414
415
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ message = std::format("Stored {} embeddings in Qdrant.\nResponse: {}\n", texts.size(), response);
416
0/4
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ if (this->ui->backend_running() == false) {
417
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ UserInterface::print_success_noninteractive(message);
418 } else {
419
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ this->ui->print_success(message);
420 }
421
0/100
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 53 not taken.
✗ Branch 54 not taken.
✗ Branch 56 not taken.
✗ Branch 57 not taken.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
✗ Branch 61 not taken.
✗ Branch 62 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✗ Branch 67 not taken.
✗ Branch 68 not taken.
✗ Branch 76 not taken.
✗ Branch 77 not taken.
✗ Branch 79 not taken.
✗ Branch 80 not taken.
✗ Branch 81 not taken.
✗ Branch 82 not taken.
✗ Branch 83 not taken.
✗ Branch 84 not taken.
✗ Branch 85 not taken.
✗ Branch 86 not taken.
✗ Branch 87 not taken.
✗ Branch 88 not taken.
✗ Branch 89 not taken.
✗ Branch 90 not taken.
✗ Branch 92 not taken.
✗ Branch 93 not taken.
✗ Branch 94 not taken.
✗ Branch 95 not taken.
✗ Branch 97 not taken.
✗ Branch 98 not taken.
✗ Branch 99 not taken.
✗ Branch 100 not taken.
✗ Branch 102 not taken.
✗ Branch 103 not taken.
✗ Branch 104 not taken.
✗ Branch 105 not taken.
✗ Branch 106 not taken.
✗ Branch 107 not taken.
✗ Branch 109 not taken.
✗ Branch 110 not taken.
✗ Branch 111 not taken.
✗ Branch 112 not taken.
✗ Branch 113 not taken.
✗ Branch 114 not taken.
✗ Branch 116 not taken.
✗ Branch 117 not taken.
✗ Branch 118 not taken.
✗ Branch 119 not taken.
✗ Branch 121 not taken.
✗ Branch 122 not taken.
✗ Branch 123 not taken.
✗ Branch 124 not taken.
✗ Branch 125 not taken.
✗ Branch 126 not taken.
✗ Branch 128 not taken.
✗ Branch 129 not taken.
✗ Branch 130 not taken.
✗ Branch 131 not taken.
✗ Branch 133 not taken.
✗ Branch 134 not taken.
✗ Branch 135 not taken.
✗ Branch 136 not taken.
✗ }
422