summaryrefslogtreecommitdiffstats
path: root/ll-store-binding/src/ll-store-binding.c
blob: fd655522a0a7724d799a1a48c846fde0c04151f8 (plain)
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
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <json-c/json.h>
#include <lmdb.h>

#define AFB_BINDING_VERSION 2
#include <afb/afb-binding.h>

#define DB_FILE "/home/agl/projects/ll-store-binding/build/src/ll-store-binding.lmdb"

MDB_env* dbenv;

/// @brief Get a string from a json object.
/// @param[in] obj Json object from wich the string is queried.
/// @param[in] name Name of the string to get.
/// @return 
const char* get_json_string(struct json_object* obj, const char* name)
{
	if (!obj || !name || !strlen(name)) return NULL;
	
	struct json_object* item = NULL;
	if (!json_object_object_get_ex(obj, name, &item) || !item) return NULL;

	return json_object_get_string(item);
}

char* make_key(const char* username, const char* appname, const char* tagname)
{
	size_t sz_username = username ? strlen(username) : 0;
	size_t sz_appname = appname ? strlen(appname) : 0;
	size_t sz_tagname = tagname ? strlen(tagname) : 0;
	size_t sz_total = sz_username + sz_appname + sz_tagname + 3;

	char* result = (char*)malloc(sz_total);
	memset(result, sz_total, 0);

	strcpy(result, username);
	result[sz_username] = '.';
	
	strcpy(result + sz_username + 1, appname);
	result[sz_username + 1 + sz_appname] = '.';

	strcpy(result + sz_username + 1 + sz_appname + 1, tagname);
	
	return result;
}

static void verb_get(struct afb_req req)
{
	int r;
	struct json_object* args = afb_req_json(req);

	AFB_INFO("args:\n%s\n", json_object_to_json_string_ext(args, JSON_C_TO_STRING_PRETTY));

	const char* username = get_json_string(args, "username");
	const char* appname = get_json_string(args, "appname");
	const char* tagname = get_json_string(args, "tagname");

	if (!username || !appname || !tagname)
	{
		AFB_ERROR("[store] username, appname and tagname must be provided!");
		afb_req_fail(req, "username, appname and tagname must be provided!", NULL);
		return;
	}

	char* keyname = make_key(username, appname, tagname);

	MDB_txn* txn;
	r = mdb_txn_begin(dbenv, NULL, 0, &txn);
	if (r)
	{
		free(keyname);
		AFB_ERROR("Failed to begin a transaction!");
		afb_req_fail(req, "Failed to begin a transaction!", NULL);
		return;
	}

	MDB_dbi dbi;
	r = mdb_dbi_open(txn, NULL, 0, &dbi);
	if (r)
	{
		free(keyname);
		mdb_txn_abort(txn);
		AFB_ERROR("Failed to open the database!");
		afb_req_fail(req, "Failed to open the database!", NULL);
		return;
	}

	MDB_val k;
	MDB_val v;
	k.mv_size = strlen(keyname) + 1;
	k.mv_data = keyname;

	if(mdb_get(txn, dbi, &k, &v))
	{
		free(keyname);
		mdb_txn_abort(txn);
		mdb_dbi_close(dbenv, dbi);
		AFB_ERROR("Failed to get the data!");
		afb_req_fail(req, "Failed to get the data!", NULL);
		return;
	}

	char* value = strndup(v.mv_data, v.mv_size + 1);
	if(mdb_txn_commit(txn))
	{
		free(keyname);
		free(value);
		mdb_dbi_close(dbenv, dbi);
		AFB_ERROR("Failed to commit the transaction!");
		return;
	}
		
	json_object* result = json_object_new_object();
	json_object_object_add(result, "key", json_object_new_string(keyname));
	json_object_object_add(result, "value", json_object_new_string(value));
	afb_req_success(req, result, NULL);

	free(value);
	free(keyname);
	return;
}

static void verb_set(struct afb_req req)
{
	int r;
	struct json_object* args = afb_req_json(req);

	AFB_INFO("args:\n%s\n", json_object_to_json_string_ext(args, JSON_C_TO_STRING_PRETTY));

	const char* username = get_json_string(args, "username");
	const char* appname = get_json_string(args, "appname");
	const char* tagname = get_json_string(args, "tagname");
	const char* value = get_json_string(args, "value");

	if (!username || !appname || !tagname)
	{
		AFB_ERROR("[store] username, appname and tagname must be provided!");
		afb_req_fail(req, "username, appname and tagname must be provided!", NULL);
		return;
	}

	char* keyname = make_key(username, appname, tagname);

	MDB_txn* txn;
	r = mdb_txn_begin(dbenv, NULL, 0, &txn);
	if (r)
	{
		free(keyname);
		AFB_ERROR("Failed to begin a transaction!");
		afb_req_fail(req, "Failed to begin a transaction!", NULL);
		return;
	}

	MDB_dbi dbi;
	r = mdb_dbi_open(txn, NULL, 0, &dbi);
	if (r)
	{
		free(keyname);
		mdb_txn_abort(txn);
		AFB_ERROR("Failed to open the database!");
		afb_req_fail(req, "Failed to open the database!", NULL);
		return;
	}

	MDB_val k;
	MDB_val v;
	k.mv_size = strlen(keyname) + 1;
	k.mv_data = keyname;
	v.mv_size = value ? strlen(value) + 1 : 0;
	v.mv_data = value;

	if(mdb_put(txn, dbi, &k, &v, 0))
	{
		free(keyname);
		mdb_txn_abort(txn);
		mdb_dbi_close(dbenv, dbi);
		AFB_ERROR("Failed to get the data!");
		afb_req_fail(req, "Failed to get the data!", NULL);
		return;
	}

	if(mdb_txn_commit(txn))
	{
		free(keyname);
		mdb_dbi_close(dbenv, dbi);
		AFB_ERROR("Failed to commit the transaction!");
		return;
	}
		
	json_object* result = json_object_new_object();
	json_object_object_add(result, "key", json_object_new_string(keyname));
	json_object_object_add(result, "value", json_object_new_string(value));
	afb_req_success(req, result, NULL);

	free(keyname);
	return;
}

int ll_store_preinit()
{
	int r = mdb_env_create(&dbenv);
	if (r)
	{
		AFB_INFO("Failed to create MDB environment!");
		dbenv = NULL;
		return r;
	}

	r = mdb_env_open(dbenv, "/home/agl/ll-store-binding.lmdb", MDB_NOSUBDIR, 0644);
	if (r)
	{
		mdb_env_close(dbenv);
		dbenv = NULL;
		AFB_INFO("Failed to open MDB environment!");
		return r;
	}

/*
	MDB_txn* txn;
	r = mdb_txn_begin(dbenv, NULL, 0, &txn);
	if (r)
	{
		mdb_env_close(dbenv);
		dbenv = NULL;
		AFB_INFO("Failed to begin a transaction!");
		return r;
	}

	MDB_dbi dbi;
	r = mdb_dbi_open(txn, NULL, 0, &dbi);
	if (r)
	{
		mdb_env_close(dbenv);
		AFB_INFO("Failed to open the database!");
		dbenv = NULL;
	}
*/
	return 0;
}

static const afb_verb_v2 _ll_store_binding_verbs[]= {
	{
		.verb = "get",
		.callback = verb_get,
		.auth = NULL,
		.info = NULL,
		.session = AFB_SESSION_NONE_V2
	},
	{
		.verb = "set",
		.callback = verb_set,
		.auth = NULL,
		.info = NULL,
		.session = AFB_SESSION_NONE_V2
	},
	{ .verb=NULL}
};

const struct afb_binding_v2 afbBindingV2 = {
		.api = "ll-store",
		.specification = NULL,
		.verbs = _ll_store_binding_verbs,
		.preinit = ll_store_preinit,
		.init = NULL,
		.onevent = NULL,
		.noconcurrency = 0
};