aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/storage_sqlite.c
blob: 8973fd9e33e03adeb202f7bfdd5933b3db168fec (plain) (blame)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 * Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
 * This file is part of the "cimple" project.
 * For details, see https://github.com/egor-tensin/cimple.
 * Distributed under the MIT License.
 */

#include "storage_sqlite.h"
#include "log.h"
#include "process.h"
#include "run_queue.h"
#include "sql/sqlite_sql.h"
#include "sqlite.h"
#include "storage.h"

#include <sqlite3.h>

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct storage_sqlite_settings {
	char *path;
};

int storage_sqlite_settings_create(struct storage_settings *settings, const char *path)
{
	struct storage_sqlite_settings *sqlite = malloc(sizeof(struct storage_sqlite_settings));
	if (!sqlite) {
		log_errno("malloc");
		return -1;
	}

	sqlite->path = strdup(path);
	if (!sqlite->path) {
		log_errno("strdup");
		goto free;
	}

	settings->type = STORAGE_TYPE_SQLITE;
	settings->sqlite = sqlite;
	return 0;

free:
	free(sqlite);

	return -1;
}

void storage_sqlite_settings_destroy(const struct storage_settings *settings)
{
	free(settings->sqlite->path);
	free(settings->sqlite);
}

enum run_status {
	RUN_STATUS_CREATED = 1,
	RUN_STATUS_FINISHED = 2,
};

struct prepared_stmt {
	pthread_mutex_t mtx;
	sqlite3_stmt *impl;
};

static int prepared_stmt_init(struct prepared_stmt *stmt, sqlite3 *db, const char *sql)
{
	int ret = 0;

	ret = pthread_mutex_init(&stmt->mtx, NULL);
	if (ret) {
		pthread_errno(ret, "pthread_mutex_init");
		return ret;
	}

	ret = sqlite_prepare(db, sql, &stmt->impl);
	if (ret < 0)
		goto destroy_mtx;

	return ret;

destroy_mtx:
	pthread_errno_if(pthread_mutex_destroy(&stmt->mtx), "pthread_mutex_destroy");

	return ret;
}

static void prepared_stmt_destroy(struct prepared_stmt *stmt)
{
	pthread_errno_if(pthread_mutex_destroy(&stmt->mtx), "pthread_mutex_destroy");
	sqlite_finalize(stmt->impl);
}

static int prepared_stmt_lock(struct prepared_stmt *stmt)
{
	int ret = pthread_mutex_lock(&stmt->mtx);
	if (ret) {
		pthread_errno(ret, "pthread_mutex_unlock");
		return ret;
	}
	return ret;
}

static void prepared_stmt_unlock(struct prepared_stmt *stmt)
{
	pthread_errno_if(pthread_mutex_unlock(&stmt->mtx), "pthread_mutex_unlock");
}

struct storage_sqlite {
	sqlite3 *db;

	struct prepared_stmt stmt_repo_find;
	struct prepared_stmt stmt_repo_insert;
	struct prepared_stmt stmt_run_insert;
	struct prepared_stmt stmt_run_finished;
};

static int storage_sqlite_upgrade_to(struct storage_sqlite *storage, size_t version)
{
	static const char *const fmt = "%s PRAGMA user_version = %zu;";

	const char *script = sqlite_schemas[version];
	int ret = 0;

	ret = snprintf(NULL, 0, fmt, script, version + 1);
	size_t nb = (size_t)ret + 1;
	ret = 0;

	char *full_script = malloc(nb);
	if (!full_script) {
		log_errno("malloc");
		return -1;
	}
	snprintf(full_script, nb, fmt, script, version + 1);

	ret = sqlite_exec_as_transaction(storage->db, full_script);
	goto free;

free:
	free(full_script);

	return ret;
}

static int storage_sqlite_upgrade_from_to(struct storage_sqlite *storage, size_t from, size_t to)
{
	int ret = 0;

	for (size_t i = from; i < to; ++i) {
		log("Upgrading SQLite database from version %zu to version %zu\n", i, i + 1);
		ret = storage_sqlite_upgrade_to(storage, i);
		if (ret < 0) {
			log_err("Failed to upgrade to version %zu\n", i + 1);
			return ret;
		}
	}

	return ret;
}

static int storage_sqlite_upgrade(struct storage_sqlite *storage)
{
	unsigned int current_version = 0;
	int ret = 0;

	ret = sqlite_get_user_version(storage->db, &current_version);
	if (ret < 0)
		return ret;
	log("SQLite database version: %u\n", current_version);

	size_t newest_version = sizeof(sqlite_schemas) / sizeof(sqlite_schemas[0]);
	log("Newest database version: %zu\n", newest_version);

	if (current_version > newest_version) {
		log_err("Unknown database version: %u\n", current_version);
		return -1;
	}

	if (current_version == newest_version) {
		log("SQLite database already at the newest version\n");
		return 0;
	}

	return storage_sqlite_upgrade_from_to(storage, current_version, newest_version);
}

static int storage_sqlite_setup(struct storage_sqlite *storage)
{
	int ret = 0;

	ret = sqlite_set_foreign_keys(storage->db);
	if (ret < 0)
		return ret;

	ret = storage_sqlite_upgrade(storage);
	if (ret < 0)
		return ret;

	return ret;
}

static int storage_sqlite_prepare_statements(struct storage_sqlite *storage)
{
	static const char *const fmt_repo_find = "SELECT id FROM cimple_repos WHERE url = ?;";
	static const char *const fmt_repo_insert =
	    "INSERT INTO cimple_repos(url) VALUES (?) ON CONFLICT(url) DO NOTHING;";
	static const char *const fmt_run_insert =
	    "INSERT INTO cimple_runs(status, exit_code, output, repo_id, repo_rev) VALUES (?, -1, x'', ?, ?) RETURNING id;";
	static const char *const fmt_run_finished =
	    "UPDATE cimple_runs SET status = ?, exit_code = ?, output = ? WHERE id = ?;";

	int ret = 0;

	ret = prepared_stmt_init(&storage->stmt_repo_find, storage->db, fmt_repo_find);
	if (ret < 0)
		return ret;
	ret = prepared_stmt_init(&storage->stmt_repo_insert, storage->db, fmt_repo_insert);
	if (ret < 0)
		goto finalize_repo_find;
	ret = prepared_stmt_init(&storage->stmt_run_insert, storage->db, fmt_run_insert);
	if (ret < 0)
		goto finalize_repo_insert;
	ret = prepared_stmt_init(&storage->stmt_run_finished, storage->db, fmt_run_finished);
	if (ret < 0)
		goto finalize_run_insert;

	return ret;

finalize_run_insert:
	prepared_stmt_destroy(&storage->stmt_run_insert);
finalize_repo_insert:
	prepared_stmt_destroy(&storage->stmt_repo_insert);
finalize_repo_find:
	prepared_stmt_destroy(&storage->stmt_repo_find);

	return ret;
}

static void storage_sqlite_finalize_statements(struct storage_sqlite *storage)
{
	prepared_stmt_destroy(&storage->stmt_run_finished);
	prepared_stmt_destroy(&storage->stmt_run_insert);
	prepared_stmt_destroy(&storage->stmt_repo_insert);
	prepared_stmt_destroy(&storage->stmt_repo_find);
}

int storage_sqlite_create(struct storage *storage, const struct storage_settings *settings)
{
	int ret = 0;

	log("Using SQLite database at %s\n", settings->sqlite->path);

	struct storage_sqlite *sqlite = malloc(sizeof(struct storage_sqlite));
	if (!sqlite) {
		log_errno("malloc");
		return -1;
	}

	ret = sqlite_init();
	if (ret < 0)
		goto free;
	ret = sqlite_open_rw(settings->sqlite->path, &sqlite->db);
	if (ret < 0)
		goto destroy;
	ret = storage_sqlite_setup(sqlite);
	if (ret < 0)
		goto close;
	ret = storage_sqlite_prepare_statements(sqlite);
	if (ret < 0)
		goto close;

	storage->sqlite = sqlite;
	return ret;

close:
	sqlite_close(storage->sqlite->db);
destroy:
	sqlite_destroy();
free:
	free(sqlite);

	return ret;
}

void storage_sqlite_destroy(struct storage *storage)
{
	storage_sqlite_finalize_statements(storage->sqlite);
	sqlite_close(storage->sqlite->db);
	sqlite_destroy();
	free(storage->sqlite);
}

static int storage_sqlite_find_repo(struct storage_sqlite *storage, const char *url)
{
	struct prepared_stmt *stmt = &storage->stmt_repo_find;
	int ret = 0;

	ret = prepared_stmt_lock(stmt);
	if (ret < 0)
		return ret;
	ret = sqlite_bind_text(stmt->impl, 1, url);
	if (ret < 0)
		goto reset;
	ret = sqlite_step(stmt->impl);
	if (ret < 0)
		goto reset;

	if (!ret)
		goto reset;

	ret = sqlite_column_int(stmt->impl, 0);
	goto reset;

reset:
	sqlite_reset(stmt->impl);
	prepared_stmt_unlock(stmt);

	return ret;
}

static int storage_sqlite_insert_repo(struct storage_sqlite *storage, const char *url)
{
	struct prepared_stmt *stmt = &storage->stmt_repo_insert;
	int ret = 0;

	ret = prepared_stmt_lock(stmt);
	if (ret < 0)
		return ret;
	ret = sqlite_bind_text(stmt->impl, 1, url);
	if (ret < 0)
		goto reset;
	ret = sqlite_step(stmt->impl);
	if (ret < 0)
		goto reset;

reset:
	sqlite_reset(stmt->impl);
	prepared_stmt_unlock(stmt);

	if (ret < 0)
		return ret;

	return storage_sqlite_find_repo(storage, url);
}

static int storage_sqlite_insert_run(struct storage_sqlite *storage, int repo_id, const char *rev)
{
	struct prepared_stmt *stmt = &storage->stmt_run_insert;
	int ret = 0;

	ret = prepared_stmt_lock(stmt);
	if (ret < 0)
		return ret;
	ret = sqlite_bind_int(stmt->impl, 1, RUN_STATUS_CREATED);
	if (ret < 0)
		goto reset;
	ret = sqlite_bind_int(stmt->impl, 2, repo_id);
	if (ret < 0)
		goto reset;
	ret = sqlite_bind_text(stmt->impl, 3, rev);
	if (ret < 0)
		goto reset;
	ret = sqlite_step(stmt->impl);
	if (ret < 0)
		goto reset;

	if (!ret) {
		ret = -1;
		log_err("Failed to insert a run\n");
		goto reset;
	}

	ret = sqlite_column_int(stmt->impl, 0);
	goto reset;

reset:
	sqlite_reset(stmt->impl);
	prepared_stmt_unlock(stmt);

	return ret;
}

int storage_sqlite_run_create(struct storage *storage, const char *repo_url, const char *rev)
{
	int ret = 0;

	ret = storage_sqlite_insert_repo(storage->sqlite, repo_url);
	if (ret < 0)
		return ret;

	ret = storage_sqlite_insert_run(storage->sqlite, ret, rev);
	if (ret < 0)
		return ret;

	return ret;
}

int storage_sqlite_run_finished(struct storage *storage, int run_id,
                                const struct proc_output *output)
{
	struct prepared_stmt *stmt = &storage->sqlite->stmt_run_finished;
	int ret = 0;

	ret = prepared_stmt_lock(stmt);
	if (ret < 0)
		return ret;
	ret = sqlite_bind_int(stmt->impl, 1, RUN_STATUS_FINISHED);
	if (ret < 0)
		goto reset;
	ret = sqlite_bind_int(stmt->impl, 2, output->ec);
	if (ret < 0)
		goto reset;
	ret = sqlite_bind_blob(stmt->impl, 3, output->data, output->data_size);
	if (ret < 0)
		goto reset;
	ret = sqlite_bind_int(stmt->impl, 4, run_id);
	if (ret < 0)
		goto reset;
	ret = sqlite_step(stmt->impl);
	if (ret < 0)
		goto reset;

reset:
	sqlite_reset(stmt->impl);
	prepared_stmt_unlock(stmt);

	return ret;
}

static int storage_sqlite_row_to_run(struct sqlite3_stmt *stmt, struct run **run)
{
	int ret = 0;

	int id = sqlite_column_int(stmt, 0);

	char *url = NULL;
	ret = sqlite_column_text(stmt, 1, &url);
	if (ret < 0)
		return ret;

	char *rev = NULL;
	ret = sqlite_column_text(stmt, 2, &rev);
	if (ret < 0)
		goto free_url;

	ret = run_create(run, id, url, rev);
	if (ret < 0)
		goto free_rev;

	log("Adding a run %d for repository %s to the queue\n", id, url);

free_rev:
	free(rev);

free_url:
	free(url);

	return ret;
}

int storage_sqlite_get_run_queue(struct storage *storage, struct run_queue *queue)
{
	static const char *const fmt =
	    "SELECT id, repo_url, repo_rev FROM cimple_runs_view WHERE status = ?;";

	sqlite3_stmt *stmt;
	int ret = 0;

	ret = sqlite_prepare(storage->sqlite->db, fmt, &stmt);
	if (ret < 0)
		return ret;
	ret = sqlite_bind_int(stmt, 1, RUN_STATUS_CREATED);
	if (ret < 0)
		goto finalize;

	run_queue_create(queue);

	while (1) {
		ret = sqlite_step(stmt);
		if (!ret)
			break;
		if (ret < 0)
			goto run_queue_destroy;

		struct run *run = NULL;

		ret = storage_sqlite_row_to_run(stmt, &run);
		if (ret < 0)
			goto run_queue_destroy;

		run_queue_add_last(queue, run);
	}

	goto finalize;

run_queue_destroy:
	run_queue_destroy(queue);

finalize:
	sqlite_finalize(stmt);

	return ret;
}