summaryrefslogtreecommitdiffstats
path: root/server/memdb.c
blob: 501d0efd03782852075307847687f51352b07bf1 (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
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
#include "conf.h"
#include "config.h"
#include "array.h"
#include "log.h"
#include "md5.h"
#include "memdb.h"
#include "thread.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

RCSID("$Id: memdb.c,v 1.27 2001/10/04 23:57:10 shmit Exp $");

#define HASHSIZE 1024

extern time_t cdb_mtime;

enum _cachetype_t { C_DEL, C_UPD, C_TRYVAL, C_EXPIRE };
typedef enum _cachetype_t cachetype_t;

struct _mement_t {
	char *key;
	int keylen;
	array_t *vals;
	time_t *upd_time;
	cachetype_t type;
	void *typeval;
};
typedef struct _mement_t mement_t;

struct _cachent_t {
	mement_t ent;
	struct _cachent_t *next;
};
typedef struct _cachent_t cachent_t;

struct _counter_t {
	float onemin, fivemin, fifteenmin;
};
typedef struct _counter_t counter_t;

extern short db_fieldcount;

static cachent_t **memdb = NULL;
static int memdbsize;
static rw_mutex_t *memdb_lk = NULL;

static int query_sec, add_sec, del_sec, upd_sec;
static counter_t queryrate;
static counter_t addrate;
static counter_t delrate;
static counter_t updrate;

static unsigned int
hashkey(const char *key, int keylen)
{
	char md5hash[32];
	unsigned int rc;

	memset(md5hash, 0, sizeof(md5hash));
	md5_calc(md5hash, key, keylen, sizeof(md5hash));
	memcpy(&rc, md5hash, sizeof(rc));
	return (rc % HASHSIZE);
}

static void
ent_delete(mement_t *ent)
{
	if (ent == NULL)
		return;

	if (ent->key != NULL)
		free(ent->key);
	if (ent->vals != NULL)
		array_delete(ent->vals);
	if (ent->upd_time != NULL)
		free(ent->upd_time);
	if (ent->typeval != NULL)
		free(ent->typeval);
}

int
memdb_new()
{
	cachent_t **newdb;
	int i;
	int rc;

	log_info("Initialising memdb interface.");

	if (memdb_lk == NULL) {
		memdb_lk = malloc(sizeof(rw_mutex_t));
		if (memdb_lk == NULL) {
			log_err("Couldn't allocate memdb mutex: %s.",
				strerror(errno));
			return -1;
		}
		rc = rw_mutex_new(memdb_lk);
		if (rc) {
			log_err("Couldn't initialise memdb mutex: %s.",
				strerror(rc));
			return -1;
		}
	}

	newdb = malloc(HASHSIZE * sizeof(cachent_t *));
	if (newdb == NULL) {
		log_err("Couldn't allocate new memdb: %s.", strerror(errno));
		return -1;
	}

	for (i = 0; i < HASHSIZE; i++)
		newdb[i] = NULL;

	rw_mutex_write_lock(memdb_lk);
	if (memdb != NULL)
		memdb_delete(memdb);
	memdb = newdb;
	rw_mutex_unlock(memdb_lk);

	query_sec = add_sec = del_sec = upd_sec = 0;
	queryrate.onemin = queryrate.fivemin = queryrate.fifteenmin = 0;
	addrate.onemin = addrate.fivemin = addrate.fifteenmin = 0;
	delrate.onemin = delrate.fivemin = delrate.fifteenmin = 0;
	updrate.onemin = updrate.fivemin = updrate.fifteenmin = 0;
	memdbsize = 0;

	log_info("memdb interface initialised.");
	return 0;
}

void
memdb_delete()
{
	int i;

	if (memdb == NULL)
		return;

	for (i = 0; i < HASHSIZE; i++) {
		cachent_t *p;

		p = memdb[i];
		while (p != NULL) {
			cachent_t *q;

			q = p->next;
			ent_delete(&p->ent);
			free(p);
			p = q;
		}
	}
	free(memdb);
	memdb = NULL;
}

int
memdb_add(const char *key, int keylen, array_t *vals)
{
	cachent_t *ent;
	int hkey;
	time_t curtime;

	ent = malloc(sizeof(cachent_t));
	if (ent == NULL) {
		log_err("Couldn't create memdb entry for `%s': %s.",
			key, strerror(errno));
		return -1;
	}
	ent->next = NULL;
	ent->ent.key = NULL;
	ent->ent.type = C_UPD;
	ent->ent.typeval = NULL;
	ent->ent.upd_time = NULL;

	ent->ent.key = malloc((keylen + 1) * sizeof(char));
	if (ent->ent.key == NULL) {
		log_err("Couldn't create entry key for `%s': %s.",
			key, strerror(errno));
		ent_delete(&ent->ent);
		return -1;
	}
	memcpy(ent->ent.key, key, keylen);
	ent->ent.keylen = keylen;

	ent->ent.vals = array_new();
	if (ent->ent.vals == NULL) {
		ent_delete(&ent->ent);
		return -1;
	}
	if (array_dup(ent->ent.vals, vals) == -1) {
		ent_delete(&ent->ent);
		return -1;
	}

	ent->ent.upd_time = malloc(sizeof(time_t));
	if (ent->ent.upd_time == NULL) {
		ent_delete(&ent->ent);
		return -1;
	}
	curtime = time(NULL);
	memcpy(ent->ent.upd_time, &curtime, sizeof(time_t));

	/* Cache non-results for a minute. */
	if (vals->nitems == 0) {
		time_t expire;

		ent->ent.type = C_EXPIRE;
		ent->ent.typeval = malloc(sizeof(time_t));
		expire = time(NULL);
		expire += config.null_cache_timeout;
		memcpy(ent->ent.typeval, &expire, sizeof(time_t));
	}

	hkey = hashkey(key, keylen);
	rw_mutex_write_lock(memdb_lk);
	if (memdb[hkey] == NULL) {
		memdb[hkey] = ent;
		memdbsize++;
	} else {
		cachent_t *p, *q;

		p = memdb[hkey];
		q = NULL;
		while (p->next != NULL &&
		       !(keylen == p->ent.keylen &&
		       memcmp(p->ent.key, key, p->ent.keylen) == 0)) {
			q = p;
			p = p->next;
		}
		if (keylen == p->ent.keylen &&
		    memcmp(p->ent.key, key, keylen) == 0) {
			/* Already in memdb. */

			log_info("(add) %s already exists.", key);

			switch (p->ent.type) {
			case C_EXPIRE:
			case C_DEL:
				/*
				 * Deletable entries. Trash 'em and
				 * add 'em.
				 */

				if (q == NULL)
					memdb[hkey] = ent;
				else
					q->next = ent;
				ent->next = p->next;

				ent_delete(&p->ent);
				free(p);
				break;
			default:
				/* XXX: Do an update instead of an add. */
			}
		} else {
			p->next = ent;
			memdbsize++;
		}
	}

	add_sec++;
	rw_mutex_unlock(memdb_lk);
	return 0;
}

int
memdb_del(const char *key, int keylen)
{
	cachent_t *p;
	int hkey;

	hkey = hashkey(key, keylen);

	rw_mutex_read_lock(memdb_lk);
	p = memdb[hkey];
	while (p != NULL &&
	       (keylen != p->ent.keylen || memcmp(p->ent.key, key, keylen)))
		p = p->next;

	if (p == NULL) {
		array_t *aa;

		/*
		 * Entry not found. We have to add it ourselves.
		 */
		rw_mutex_unlock(memdb_lk);

		aa = array_new();
		if (aa == NULL)
			return -1;
		if (memdb_add(key, keylen, aa) == -1)
			return -1;

		/*
		 * The entry has been added - now go back and mark it
		 * for deletion.
		 */
		return memdb_del(key, keylen);
	}

	rw_mutex_write_lock(memdb_lk);
	if (p->ent.typeval != NULL) {
		free(p->ent.typeval);
		p->ent.typeval = NULL;
	}

	p->ent.type = C_DEL;
	if (p->ent.vals != NULL)
		array_delete(p->ent.vals);

	del_sec++;
	rw_mutex_unlock(memdb_lk);

	return 0;
}

int
memdb_get(const char *key, int keylen, array_t *vals)
{
	cachent_t *p, *q;
	int hkey;

	hkey = hashkey(key, keylen);
	rw_mutex_read_lock(memdb_lk);
	p = memdb[hkey];
	q = NULL;
	while (p != NULL &&
	       (keylen != p->ent.keylen || memcmp(p->ent.key, key, keylen))) {
		q = p;
		p = p->next;
	}

	if (p == NULL) {
		rw_mutex_unlock(memdb_lk);
		return -1;
	}

	switch (p->ent.type) {
	case C_DEL:
		rw_mutex_unlock(memdb_lk);
		query_sec++;
		return 0;
	case C_EXPIRE:
		if (p->ent.typeval != NULL) {
			time_t now, expire;

			now = time(NULL);
			expire = *((time_t *)p->ent.typeval);
			if (now >= expire) {
				ent_delete(&p->ent);
				if (q != NULL)
					q->next = p->next;
				else
					memdb[hkey] = p->next;
				free(p);
				rw_mutex_unlock(memdb_lk);
				log_info("DEBUG: (get) %s has expired", key);
				return -1;
			}
		}
		break;
	default:
		if (p->ent.upd_time != NULL) {
			time_t upd_time;

			upd_time = *p->ent.upd_time;
			if (cdb_mtime > upd_time) {
				ent_delete(&p->ent);
				if (q != NULL)
					q->next = p->next;
				else
					memdb[hkey] = p->next;
				free(p);
				rw_mutex_unlock(memdb_lk);
				log_info("DEBUG: (get) %s is out of date", key);
				return -1;
			}
		}
		break;
	}

	if (array_dup(vals, p->ent.vals) == -1) {
		rw_mutex_unlock(memdb_lk);
		return -1;
	}

	rw_mutex_unlock(memdb_lk);
	query_sec++;
	return 0;
}

int
memdb_upd(const char *key, int keylen, array_t *vals)
{
	cachent_t *p;
	int hkey;
	time_t curtime;

	/* First make sure that we have enough values. */
	if (vals->nitems != db_fieldcount) {
		log_err("Update tried with wrong value count.");
		return -1;
	}

	hkey = hashkey(key, keylen);
	rw_mutex_read_lock(memdb_lk);
	p = memdb[hkey];
	while (p != NULL &&
	       (keylen != p->ent.keylen || memcmp(p->ent.key, key, keylen)))
		p = p->next;

	if (p == NULL) {
		/* Entry not found. Add it ourselves. */
		rw_mutex_unlock(memdb_lk);
		return memdb_add(key, keylen, vals);
	}
	rw_mutex_unlock(memdb_lk);

	/* Entry found, lets replace it with our copy. */

	rw_mutex_write_lock(memdb_lk);
	p->ent.type = C_UPD;
	p->ent.typeval = NULL;
	curtime = time(NULL);
	memcpy(p->ent.upd_time, &curtime, sizeof(time_t));
	array_delete(p->ent.vals);

	p->ent.vals = array_new();
	if (p->ent.vals == NULL) {
		rw_mutex_unlock(memdb_lk);
		return -1;
	}

	if (array_dup(p->ent.vals, vals) == -1) {
		rw_mutex_unlock(memdb_lk);
		return -1;
	}

	upd_sec++;
	rw_mutex_unlock(memdb_lk);

	return 0;
}

int
memdb_stats(array_t *statarr)
{
	char buffer[512];

	/* Convert q/m, q/5m, and q/15m into q/s. */
	snprintf(buffer, sizeof(buffer), "MEMDB get: %.2f, %.2f, %.2f",
		 queryrate.onemin, queryrate.fivemin, queryrate.fifteenmin);
	if (array_add(statarr, strlen(buffer), buffer, ARRTERM) == -1)
		return -1;

	snprintf(buffer, sizeof(buffer), "MEMDB add: %.2f, %.2f, %.2f",
		 addrate.onemin, addrate.fivemin, addrate.fifteenmin);
	if (array_add(statarr, strlen(buffer), buffer, ARRTERM) == -1)
		return -1;

	snprintf(buffer, sizeof(buffer), "MEMDB del: %.2f, %.2f, %.2f",
		 delrate.onemin, delrate.fivemin, delrate.fifteenmin);
	if (array_add(statarr, strlen(buffer), buffer, ARRTERM) == -1)
		return -1;

	snprintf(buffer, sizeof(buffer), "MEMDB upd: %.2f, %.2f, %.2f",
		 updrate.onemin, updrate.fivemin, updrate.fifteenmin);
	if (array_add(statarr, strlen(buffer), buffer, ARRTERM) == -1)
		return -1;

	snprintf(buffer, sizeof(buffer), "MEMDB total entries: %d", memdbsize);
	return array_add(statarr, strlen(buffer), buffer, ARRTERM);
}

void
memdb_collate()
{
	queryrate.onemin = ((queryrate.onemin * 59) + query_sec) / 60;
	queryrate.fivemin = ((queryrate.fivemin * 299) + query_sec) / 300;
	queryrate.fifteenmin = ((queryrate.fifteenmin * 899) + query_sec) / 900;
	query_sec = 0;

	addrate.onemin = ((addrate.onemin * 59) + add_sec) / 60;
	addrate.fivemin = ((addrate.fivemin * 299) + add_sec) / 300;
	addrate.fifteenmin = ((addrate.fifteenmin * 899) + add_sec) / 900;
	add_sec = 0;

	delrate.onemin = ((delrate.onemin * 59) + del_sec) / 60;
	delrate.fivemin = ((delrate.fivemin * 299) + del_sec) / 300;
	delrate.fifteenmin = ((delrate.fifteenmin * 899) + del_sec) / 900;
	del_sec = 0;

	updrate.onemin = ((updrate.onemin * 59) + upd_sec) / 60;
	updrate.fivemin = ((updrate.fivemin * 299) + upd_sec) / 300;
	updrate.fifteenmin = ((updrate.fifteenmin * 899) + upd_sec) / 900;
	upd_sec = 0;
}