mdc.c 3.01 KB
Newer Older
Hardy Simpson's avatar
Hardy Simpson committed
1
/*
Hardy Simpson's avatar
Hardy Simpson committed
2
 * This file is part of the zlog Library.
Hardy Simpson's avatar
Hardy Simpson committed
3
 *
Hardy Simpson's avatar
Hardy Simpson committed
4
 * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
Hardy Simpson's avatar
Hardy Simpson committed
5
 *
6
 * Licensed under the LGPL v2.1, see the file COPYING in base directory.
Hardy Simpson's avatar
Hardy Simpson committed
7 8
 */

Hardy Simpson's avatar
Hardy Simpson committed
9
#include <stdlib.h>
Hardy Simpson's avatar
Hardy Simpson committed
10
#include <stdio.h>
Hardy Simpson's avatar
Hardy Simpson committed
11 12 13 14 15
#include <errno.h>

#include "mdc.h"
#include "zc_defs.h"

16 17 18 19 20 21
void zlog_mdc_profile(zlog_mdc_t *a_mdc, int flag)
{
	zc_hashtable_entry_t *a_entry;
	zlog_mdc_kv_t *a_mdc_kv;

	zc_assert(a_mdc,);
22
	zc_profile(flag, "---mdc[%p]---", a_mdc);
23 24 25 26 27 28 29 30 31

	zc_hashtable_foreach(a_mdc->tab, a_entry) {
		a_mdc_kv = a_entry->value;
		zc_profile(flag, "----mdc_kv[%p][%s]-[%s]----",
				a_mdc_kv,
				a_mdc_kv->key, a_mdc_kv->value);
	}
	return;
}
Hardy Simpson's avatar
Hardy Simpson committed
32
/*******************************************************************************/
33 34 35
void zlog_mdc_del(zlog_mdc_t * a_mdc)
{
	zc_assert(a_mdc,);
Hardy Simpson's avatar
Hardy Simpson committed
36
	if (a_mdc->tab) zc_hashtable_del(a_mdc->tab);
37 38 39 40 41 42 43 44 45 46
	free(a_mdc);
	zc_debug("zlog_mdc_del[%p]", a_mdc);
	return;
}

static void zlog_mdc_kv_del(zlog_mdc_kv_t * a_mdc_kv)
{
	free(a_mdc_kv);
	zc_debug("zlog_mdc_kv_del[%p]", a_mdc_kv);
}
Hardy Simpson's avatar
Hardy Simpson committed
47

Hardy Simpson's avatar
Hardy Simpson committed
48
static zlog_mdc_kv_t *zlog_mdc_kv_new(const char *key, const char *value)
Hardy Simpson's avatar
Hardy Simpson committed
49
{
Hardy Simpson's avatar
Hardy Simpson committed
50
	zlog_mdc_kv_t *a_mdc_kv;
Hardy Simpson's avatar
Hardy Simpson committed
51

Hardy Simpson's avatar
Hardy Simpson committed
52
	a_mdc_kv = calloc(1, sizeof(zlog_mdc_kv_t));
Hardy Simpson's avatar
Hardy Simpson committed
53 54 55 56 57
	if (!a_mdc_kv) {
		zc_error("calloc fail, errno[%d]", errno);
		return NULL;
	}

Hardy Simpson's avatar
Hardy Simpson committed
58 59
	snprintf(a_mdc_kv->key, sizeof(a_mdc_kv->key), "%s", key);
	a_mdc_kv->value_len = snprintf(a_mdc_kv->value, sizeof(a_mdc_kv->value), "%s", value);
Hardy Simpson's avatar
Hardy Simpson committed
60 61 62
	return a_mdc_kv;
}

Hardy Simpson's avatar
Hardy Simpson committed
63
zlog_mdc_t *zlog_mdc_new(void)
Hardy Simpson's avatar
Hardy Simpson committed
64
{
Hardy Simpson's avatar
Hardy Simpson committed
65
	zlog_mdc_t *a_mdc;
Hardy Simpson's avatar
Hardy Simpson committed
66

Hardy Simpson's avatar
Hardy Simpson committed
67
	a_mdc = calloc(1, sizeof(zlog_mdc_t));
Hardy Simpson's avatar
Hardy Simpson committed
68 69 70 71 72 73
	if (!a_mdc) {
		zc_error("calloc fail, errno[%d]", errno);
		return NULL;
	}

	a_mdc->tab = zc_hashtable_new(20,
Hardy Simpson's avatar
Hardy Simpson committed
74 75 76
			      zc_hashtable_str_hash,
			      zc_hashtable_str_equal, NULL,
			      (zc_hashtable_del_fn) zlog_mdc_kv_del);
Hardy Simpson's avatar
Hardy Simpson committed
77 78
	if (!a_mdc->tab) {
		zc_error("zc_hashtable_new fail");
Hardy Simpson's avatar
Hardy Simpson committed
79
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
80 81
	}

82
	//zlog_mdc_profile(a_mdc, ZC_DEBUG);
Hardy Simpson's avatar
Hardy Simpson committed
83 84 85 86
	return a_mdc;
err:
	zlog_mdc_del(a_mdc);
	return NULL;
Hardy Simpson's avatar
Hardy Simpson committed
87 88
}

89
/*******************************************************************************/
Hardy Simpson's avatar
Hardy Simpson committed
90
int zlog_mdc_put(zlog_mdc_t * a_mdc, const char *key, const char *value)
Hardy Simpson's avatar
Hardy Simpson committed
91
{
Hardy Simpson's avatar
Hardy Simpson committed
92
	zlog_mdc_kv_t *a_mdc_kv;
Hardy Simpson's avatar
Hardy Simpson committed
93

Hardy Simpson's avatar
Hardy Simpson committed
94
	a_mdc_kv = zlog_mdc_kv_new(key, value);
Hardy Simpson's avatar
Hardy Simpson committed
95
	if (!a_mdc_kv) {
Hardy Simpson's avatar
Hardy Simpson committed
96
		zc_error("zlog_mdc_kv_new failed");
Hardy Simpson's avatar
Hardy Simpson committed
97 98 99
		return -1;
	}

Hardy Simpson's avatar
Hardy Simpson committed
100
	if (zc_hashtable_put(a_mdc->tab, a_mdc_kv->key, a_mdc_kv)) {
Hardy Simpson's avatar
Hardy Simpson committed
101
		zc_error("zc_hashtable_put fail");
Hardy Simpson's avatar
Hardy Simpson committed
102
		zlog_mdc_kv_del(a_mdc_kv);
Hardy Simpson's avatar
Hardy Simpson committed
103 104 105 106 107 108
		return -1;
	}

	return 0;
}

109 110 111 112 113 114
void zlog_mdc_clean(zlog_mdc_t * a_mdc)
{
	zc_hashtable_clean(a_mdc->tab);
	return;
}

Hardy Simpson's avatar
Hardy Simpson committed
115
char *zlog_mdc_get(zlog_mdc_t * a_mdc, const char *key)
Hardy Simpson's avatar
Hardy Simpson committed
116
{
Hardy Simpson's avatar
Hardy Simpson committed
117
	zlog_mdc_kv_t *a_mdc_kv;
Hardy Simpson's avatar
Hardy Simpson committed
118 119 120 121 122 123 124 125 126 127

	a_mdc_kv = zc_hashtable_get(a_mdc->tab, key);
	if (!a_mdc_kv) {
		zc_error("zc_hashtable_get fail");
		return NULL;
	} else {
		return a_mdc_kv->value;
	}
}

Hardy Simpson's avatar
Hardy Simpson committed
128
zlog_mdc_kv_t *zlog_mdc_get_kv(zlog_mdc_t * a_mdc, const char *key)
Hardy Simpson's avatar
Hardy Simpson committed
129
{
Hardy Simpson's avatar
Hardy Simpson committed
130
	zlog_mdc_kv_t *a_mdc_kv;
Hardy Simpson's avatar
Hardy Simpson committed
131 132 133 134 135 136 137 138 139 140

	a_mdc_kv = zc_hashtable_get(a_mdc->tab, key);
	if (!a_mdc_kv) {
		zc_error("zc_hashtable_get fail");
		return NULL;
	} else {
		return a_mdc_kv;
	}
}

Hardy Simpson's avatar
Hardy Simpson committed
141
void zlog_mdc_remove(zlog_mdc_t * a_mdc, const char *key)
Hardy Simpson's avatar
Hardy Simpson committed
142 143 144 145
{
	zc_hashtable_remove(a_mdc->tab, key);
	return;
}