summaryrefslogtreecommitdiffstats
path: root/ctdb/common/hash_count.c
blob: f84501663c191f6f6097fa15739cd3a59ea77afc (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
/*
   Using hash table for counting events

   Copyright (C) Amitay Isaacs  2017

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"
#include "system/filesys.h"
#include "system/time.h"

#include <tdb.h>

#include "lib/util/time.h"

#include "common/db_hash.h"
#include "common/hash_count.h"

struct hash_count_value {
	struct timeval update_time;
	uint64_t counter;
};

struct hash_count_context {
	struct db_hash_context *dh;
	struct timeval update_interval;
	hash_count_update_handler_fn handler;
	void *private_data;
};

/*
 * Initialise hash count map
 */
int hash_count_init(TALLOC_CTX *mem_ctx, struct timeval update_interval,
		    hash_count_update_handler_fn handler, void *private_data,
		    struct hash_count_context **result)
{
	struct hash_count_context *hcount;
	int ret;

	if (handler == NULL) {
		return EINVAL;
	}

	hcount = talloc_zero(mem_ctx, struct hash_count_context);
	if (hcount == NULL) {
		return ENOMEM;
	}

	ret = db_hash_init(hcount, "hash_count_db", 8192, DB_HASH_COMPLEX,
			    &hcount->dh);
	if (ret != 0) {
		talloc_free(hcount);
		return ret;
	}

	hcount->update_interval = update_interval;
	hcount->handler = handler;
	hcount->private_data = private_data;

	*result = hcount;
	return 0;
}

static int hash_count_fetch_parser(uint8_t *keybuf, size_t keylen,
				   uint8_t *databuf, size_t datalen,
				   void *private_data)
{
	struct hash_count_value *value =
		(struct hash_count_value *)private_data;

	if (datalen != sizeof(struct hash_count_value)) {
		return EIO;
	}

	*value = *(struct hash_count_value *)databuf;
	return 0;
}

static int hash_count_fetch(struct hash_count_context *hcount, TDB_DATA key,
			    struct hash_count_value *value)
{
	return db_hash_fetch(hcount->dh, key.dptr, key.dsize,
			     hash_count_fetch_parser, value);
}

static int hash_count_insert(struct hash_count_context *hcount, TDB_DATA key,
			     struct hash_count_value *value)
{
	return db_hash_insert(hcount->dh, key.dptr, key.dsize,
			      (uint8_t *)value,
			      sizeof(struct hash_count_value));
}

static int hash_count_update(struct hash_count_context *hcount, TDB_DATA key,
			     struct hash_count_value *value)
{
	return db_hash_add(hcount->dh, key.dptr, key.dsize,
			   (uint8_t *)value, sizeof(struct hash_count_value));
}

int hash_count_increment(struct hash_count_context *hcount, TDB_DATA key)
{
	struct hash_count_value value;
	struct timeval current_time = timeval_current();
	int ret;

	if (hcount == NULL) {
		return EINVAL;
	}

	ret = hash_count_fetch(hcount, key, &value);
	if (ret == 0) {
		struct timeval tmp_t;

		tmp_t = timeval_sum(&value.update_time,
				    &hcount->update_interval);
		if (timeval_compare(&current_time, &tmp_t) < 0) {
			value.counter += 1;
		} else {
			value.update_time = current_time;
			value.counter = 1;
		}

		hcount->handler(key, value.counter, hcount->private_data);
		ret = hash_count_update(hcount, key, &value);

	} else if (ret == ENOENT) {
		value.update_time = current_time;
		value.counter = 1;

		hcount->handler(key, value.counter, hcount->private_data);
		ret = hash_count_insert(hcount, key, &value);
	}

	return ret;
}

static struct timeval timeval_subtract(const struct timeval *tv1,
				       const struct timeval *tv2)
{
	struct timeval tv = *tv1;
	const unsigned int million = 1000000;

	if (tv.tv_sec > 1) {
		tv.tv_sec -= 1;
		tv.tv_usec += million;
	} else {
		return tv;
	}

	tv.tv_sec -= tv2->tv_sec;
	tv.tv_usec -= tv2->tv_usec;

	tv.tv_sec += tv.tv_usec / million;
	tv.tv_usec = tv.tv_usec % million;

	return tv;
}

struct hash_count_expire_state {
	struct db_hash_context *dh;
	struct timeval last_time;
	int count;
};

static int hash_count_expire_parser(uint8_t *keybuf, size_t keylen,
				    uint8_t *databuf, size_t datalen,
				    void *private_data)
{
	struct hash_count_expire_state *state =
		(struct hash_count_expire_state *)private_data;
	struct hash_count_value *value;
	int ret = 0;

	if (datalen != sizeof(struct hash_count_value)) {
		return EIO;
	}

	value = (struct hash_count_value *)databuf;
	if (timeval_compare(&value->update_time, &state->last_time) < 0) {
		ret = db_hash_delete(state->dh, keybuf, keylen);
		if (ret == 0) {
			state->count += 1;
		}
	}

	return ret;
}

void hash_count_expire(struct hash_count_context *hcount, int *delete_count)
{
	struct timeval current_time = timeval_current();
	struct hash_count_expire_state state;

	state.dh = hcount->dh;
	state.last_time = timeval_subtract(&current_time,
					   &hcount->update_interval);
	state.count = 0;

	(void) db_hash_traverse_update(hcount->dh, hash_count_expire_parser,
				       &state, NULL);

	if (delete_count != NULL) {
		*delete_count = state.count;
	}
}