summaryrefslogtreecommitdiff
path: root/src/t_hyperloglog.c
blob: 667b2be3cd89e15d8ace39a3af86f00d2f262ca2 (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
#include "swarmkv_common.h"
#include "swarmkv_utils.h"
#include "swarmkv_store.h"
#include "swarmkv_error.h"
#include "st_hyperloglog.h"

#include <stdlib.h>
#include <assert.h>
enum cmd_exec_result pfinit_command(struct swarmkv_module *mod_store, const struct swarmkv_cmd *cmd, struct swarmkv_reply **reply)
{
	/* PFINIT key precison [TIME window-milliseconds] */
	struct sobj *obj = NULL;
	const sds key = cmd->argv[1];

	long long precision = 0, time_window_ms = 0;

	int ret = 0;
	precision = strtod(cmd->argv[2], NULL);

	if (precision < HLL_MIN_PRECISION || precision > HLL_MAX_PRECISION)
	{
		*reply = swarmkv_reply_new_error(error_arg_not_valid_integer, cmd->argv[2]);
		return FINISHED;
	}
	if (cmd->argc == 5)
	{
		if (strncasecmp(cmd->argv[3], "TIME", 4) != 0)
		{
			*reply = swarmkv_reply_new_error(error_arg_string_should_be, cmd->argv[4], "TIME");
			return FINISHED;
		}
		ret = str2integer(cmd->argv[4], &time_window_ms);
		if (ret < 0)
		{
			*reply = swarmkv_reply_new_error(error_arg_not_valid_integer, cmd->argv[5]);
			return FINISHED;
		}
	}
	obj = store_lookup(mod_store, key);
	if (!obj)
	{
		return NEED_KEY_ROUTE;
	}
	struct timeval now;
	gettimeofday(&now, NULL);

	if (obj->type == OBJ_TYPE_UNDEFINED)
	{
		assert(obj->raw == NULL);
		obj->hll = ST_hyperloglog_new(precision, time_window_ms, now);
		obj->type = OBJ_TYPE_HYPERLOGLOG;
		*reply = swarmkv_reply_new_status("OK");
	}
	else
	{
		*reply = swarmkv_reply_new_array(0);
	}
	return FINISHED;
}
enum cmd_exec_result pfadd_command(struct swarmkv_module *mod_store, const struct swarmkv_cmd *cmd, struct swarmkv_reply **reply)
{
	/*PFADD key item [item ...]*/
	struct sobj *obj = NULL;
	const sds key = cmd->argv[1];

	obj = store_lookup(mod_store, key);
	if (!obj)
	{
		return NEED_KEY_ROUTE;
	}
	struct timeval now;
	gettimeofday(&now, NULL);

	if (obj->type == OBJ_TYPE_UNDEFINED)
	{
		return handle_undefined_object(obj, reply);
	}
	else if (obj->type != OBJ_TYPE_HYPERLOGLOG)
	{
		*reply = swarmkv_reply_new_error(error_wrong_type);
		return FINISHED;
	}
	int at_least_one_register_altered = 0;
	for (int i = 0; i < cmd->argc - 2; i++)
	{
		int ret = 0;
		ret = ST_hyperloglog_add(obj->hll, cmd->argv[i + 2], sdslen(cmd->argv[i + 2]), now);
		if (ret)
		{
			at_least_one_register_altered = 1;
		}
	}
	store_mark_object_as_modified(mod_store, obj);
	*reply = swarmkv_reply_new_integer(at_least_one_register_altered);
	return FINISHED;
}
enum cmd_exec_result pfcount_command(struct swarmkv_module *mod_store, const struct swarmkv_cmd *cmd, struct swarmkv_reply **reply)
{
	/*PFCOUNT key*/
	struct sobj *obj = NULL;
	const sds key = cmd->argv[1];
	obj = store_lookup(mod_store, key);
	if (!obj)
	{
		return NEED_KEY_ROUTE;
	}
	struct timeval now;
	gettimeofday(&now, NULL);
	if (obj->type == OBJ_TYPE_UNDEFINED)
	{
		return handle_undefined_object(obj, reply);
	}
	else if (obj->type != OBJ_TYPE_HYPERLOGLOG)
	{
		*reply = swarmkv_reply_new_error(error_wrong_type);
		return FINISHED;
	}

	long long count = 0;
	count = ST_hyperloglog_count(obj->hll, now);
	*reply = swarmkv_reply_new_integer(count);
	return FINISHED;
}
enum cmd_exec_result pfinfo_command(struct swarmkv_module *mod_store, const struct swarmkv_cmd *cmd, struct swarmkv_reply **reply)
{
	/*PFINFO key*/
	struct sobj *obj = NULL;
	const sds key = cmd->argv[1];
	obj = store_lookup(mod_store, key);
	if (!obj)
	{
		return NEED_KEY_ROUTE;
	}
	struct timeval now;
	gettimeofday(&now, NULL);
	if (obj->type == OBJ_TYPE_UNDEFINED)
	{
		return handle_undefined_object(obj, reply);
	}
	else if (obj->type != OBJ_TYPE_HYPERLOGLOG)
	{
		*reply = swarmkv_reply_new_error(error_wrong_type);
		return FINISHED;
	}
	struct ST_hyperloglog_info info;
	ST_hyperloglog_info(obj->hll, &info);
	int i = 0;
	*reply = swarmkv_reply_new_array(6);
	(*reply)->elements[i++] = swarmkv_reply_new_string_fmt("Error");
	(*reply)->elements[i++] = swarmkv_reply_new_double(info.error);
	(*reply)->elements[i++] = swarmkv_reply_new_string_fmt("Precision");
	(*reply)->elements[i++] = swarmkv_reply_new_integer(info.precision);
	(*reply)->elements[i++] = swarmkv_reply_new_string_fmt("TimeWindowMs");
	(*reply)->elements[i++] = swarmkv_reply_new_integer(info.time_window_ms);
	assert(i == 6);
	return FINISHED;
}