-
Notifications
You must be signed in to change notification settings - Fork 1
/
queryevents.cpp
122 lines (108 loc) · 3.39 KB
/
queryevents.cpp
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
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
#include "hiredis.h"
using namespace std;
void ProcessSubReplyArray(redisContext *c, redisReply *reply){
for (int i=0;i < (int)reply->elements;i++){
int type = reply->element[i]->type;
if (type == REDIS_REPLY_STRING || type == REDIS_REPLY_STATUS || type == REDIS_REPLY_ERROR){
cout << reply->element[i]->str << " ";
} else if (type == REDIS_REPLY_INTEGER){
cout << reply->element[i]->integer << " ";
} else if (type == REDIS_REPLY_NIL){
cout << " NIL ";
}
}
cout << endl;
}
void ProcessReply(redisContext *c, redisReply *reply, int &count){
cout << " Reply:" << endl;
if (reply && reply->type == REDIS_REPLY_ARRAY){
for (int i=0;i < reply->elements;i++){
int type = reply->element[i]->type;
if (type == REDIS_REPLY_ARRAY){
cout << " (" << i+1 << ") ";
ProcessSubReplyArray(c, reply->element[i]);
} else if (type == REDIS_REPLY_STRING || type == REDIS_REPLY_STATUS){
cout << reply->element[i]->str << endl;
} else if (type == REDIS_REPLY_INTEGER){
cout << reply->element[i]->integer << endl;
} else if (type == REDIS_REPLY_ERROR){
cout << reply->element[i]->str << endl;
} else if (type == REDIS_REPLY_NIL){
cout << "NIL" << endl;
} else {
cout << "ERROR" << endl;
}
count++;
}
return;
} else if (reply && reply->type == REDIS_REPLY_STRING){
cout << reply->str << endl;
} else if (reply && reply->type == REDIS_REPLY_INTEGER){
cout << reply->integer << endl;
} else if (reply && reply->type == REDIS_REPLY_STATUS){
cout << reply->str << endl;
} else if (reply && reply->type == REDIS_REPLY_ERROR){
cout << reply->str << endl;
} else if (reply && reply->type == REDIS_REPLY_NIL){
cout << "NIL" << endl;
} else {
cout << "ERROR" << endl;
}
return;
}
int Query(redisContext *c, const string &key,
const double x1, const double x2,
const double y1, const double y2,
const string &startdatetime, const string &enddatetime,
int n, ...){
char cmd[256];
snprintf(cmd, 256, "reventis.query %s %f %f %f %f %s %s",
key.c_str(), x1, x2, y1, y2,
startdatetime.c_str(), enddatetime.c_str());
va_list ap;
va_start(ap, n);
for (int i=0;i<n;i++){
string catidstr = to_string(va_arg(ap, int)) + " ";
strncat(cmd, catidstr.c_str(), catidstr.size());
}
va_end(ap);
cout << "send => " << cmd << endl << endl;
redisReply *reply = (redisReply*)redisCommand(c, cmd);
int count = 0;
ProcessReply(c, reply, count);
cout << " (" << count << " results)" << endl << endl;
freeReplyObject(reply);
return count;
}
int main(int argc, char **argv){
if (argc < 10){
cout << "not enough args" << endl;
cout << "./queryevents key longitude1 longitude2 latitude1 latitude2 start-datetime end-datetime" << endl;
exit(0);
}
redisContext *c = redisConnect("localhost", 6379);
if (c == NULL || c->err){
if (c){
cout << "Error: " << c->errstr << endl;
} else {
cout << "Unable to allocate redis context" << endl;
}
exit(EXIT_FAILURE);
}
const string key = argv[1];
const double x1 = atof(argv[2]);
const double x2 = atof(argv[3]);
const double y1 = atof(argv[4]);
const double y2 = atof(argv[5]);
const string startdatetime = argv[6];
const string enddatetime = argv[7];
int n = Query(c, key, x1, x2, y1, y2, startdatetime, enddatetime, 0);
cout << "items found: " << n << endl;
cout << "done." << endl;
redisFree(c);
return 0;
}