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
|
/*
* 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.h"
#include "log.h"
#include "run_queue.h"
#include "storage_sqlite.h"
#include <stddef.h>
typedef void (*storage_settings_destroy_t)(const struct storage_settings *);
typedef int (*storage_create_t)(struct storage *, const struct storage_settings *);
typedef void (*storage_destroy_t)(struct storage *);
typedef int (*storage_run_create_t)(struct storage *, const char *repo_url, const char *rev);
typedef int (*storage_run_finished_t)(struct storage *, int repo_id, int ec);
typedef int (*storage_get_run_queue_t)(struct storage *, struct run_queue *);
struct storage_api {
storage_settings_destroy_t destroy_settings;
storage_create_t create;
storage_destroy_t destroy;
storage_run_create_t run_create;
storage_run_finished_t run_finished;
storage_get_run_queue_t get_run_queue;
};
static const struct storage_api apis[] = {
{
storage_sqlite_settings_destroy,
storage_sqlite_create,
storage_sqlite_destroy,
storage_sqlite_run_create,
storage_sqlite_run_finished,
storage_sqlite_get_run_queue,
},
};
static size_t numof_apis(void)
{
return sizeof(apis) / sizeof(apis[0]);
}
static const struct storage_api *get_api(enum storage_type type)
{
if (type < 0)
goto invalid_type;
if ((size_t)type > numof_apis())
goto invalid_type;
return &apis[type];
invalid_type:
log_err("Unsupported storage type: %d\n", type);
return NULL;
}
void storage_settings_destroy(const struct storage_settings *settings)
{
const struct storage_api *api = get_api(settings->type);
if (!api)
return;
api->destroy_settings(settings);
}
int storage_create(struct storage *storage, const struct storage_settings *settings)
{
int ret = 0;
const struct storage_api *api = get_api(settings->type);
if (!api)
return -1;
ret = api->create(storage, settings);
if (ret < 0)
return ret;
storage->type = settings->type;
return ret;
}
void storage_destroy(struct storage *storage)
{
const struct storage_api *api = get_api(storage->type);
if (!api)
return;
api->destroy(storage);
}
int storage_run_create(struct storage *storage, const char *repo_url, const char *rev)
{
const struct storage_api *api = get_api(storage->type);
if (!api)
return -1;
return api->run_create(storage, repo_url, rev);
}
int storage_run_finished(struct storage *storage, int run_id, int ec)
{
const struct storage_api *api = get_api(storage->type);
if (!api)
return -1;
return api->run_finished(storage, run_id, ec);
}
int storage_get_run_queue(struct storage *storage, struct run_queue *queue)
{
const struct storage_api *api = get_api(storage->type);
if (!api)
return -1;
return api->get_run_queue(storage, queue);
}
|