This repository has been archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
vdb.cpp
157 lines (132 loc) · 4.7 KB
/
vdb.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
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
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
// libircbot irc::bot::
#include "ircbot/bot.h"
using namespace irc::bot;
// SPQF
#include "log.h"
#include "vote.h"
#include "votes.h"
#include "vdb.h"
Vdb::Vdb(const std::string &dir):
Adb(dir)
{
}
const std::vector<std::string> Vdb::operators
{{
"", // empty operator checks if key exists (and is non-empty)
"=", // equality operator aliased to ==
"==", // case insensitive string equality
"!=", // case insensitive string inequality
"<", // integer less than
">", // integer greater than
"<=", // integer less than or equals
">=", // integer greater than or equals
}};
Vdb::Results Vdb::query(const Terms &terms,
const size_t &limit,
const bool &descending)
{
Results ret;
// TODO: Until stldb reverse iterators and propert sorting are fixed hack this for now...
query(terms,0,cbegin(),cend(),ret);
ret.sort();
if(descending)
ret.reverse();
if(limit)
ret.resize(std::min(limit,ret.size()));
return ret;
}
bool Vdb::match(const Adoc &doc,
const Term &term)
try
{
const auto &key(std::get<0>(term));
const auto &op(std::get<1>(term));
const auto &val(std::get<2>(term));
switch(hash(op))
{
case hash("="):
case hash("=="): return boost::iequals(doc[key],val);
case hash("!="): return !boost::iequals(doc[key],val);
case hash("<="): return doc.get<int64_t>(key) <= lex_cast<int64_t>(val);
case hash(">="): return doc.get<int64_t>(key) >= lex_cast<int64_t>(val);
case hash("<"): return doc.get<int64_t>(key) < lex_cast<int64_t>(val);
case hash(">"): return doc.get<int64_t>(key) > lex_cast<int64_t>(val);
case hash(""): return doc.has(key);
default: throw Assertive("Unrecognized query term operator");
};
}
catch(const boost::bad_lexical_cast &e)
{
throw Exception("This comparison operator requires a number.");
}
catch(const boost::property_tree::ptree_bad_path &e)
{
throw Exception("A key is not recognized: ") << e.what();
}
catch(const boost::property_tree::ptree_bad_data &e)
{
throw Exception("A key can not be compared to the argument: ") << e.what();
}
catch(const boost::property_tree::ptree_error &e)
{
throw Assertive(e.what());
}
std::string Vdb::get_type(const id_t &id)
{
return get_value(id,"type");
}
std::string Vdb::get_value(const id_t &id,
const std::string &key)
{
return Adb::get(std::nothrow,lex_cast(id))[key];
}
std::unique_ptr<Vote> Vdb::get(const id_t &id)
try
{
switch(hash(get_type(id)))
{
case hash("config"): return std::make_unique<vote::Config>(id,*this);
case hash("mode"): return std::make_unique<vote::Mode>(id,*this);
case hash("appeal"): return std::make_unique<vote::Appeal>(id,*this);
case hash("trial"): return std::make_unique<vote::Trial>(id,*this);
case hash("kick"): return std::make_unique<vote::Kick>(id,*this);
case hash("invite"): return std::make_unique<vote::Invite>(id,*this);
case hash("topic"): return std::make_unique<vote::Topic>(id,*this);
case hash("opine"): return std::make_unique<vote::Opine>(id,*this);
case hash("quote"): return std::make_unique<vote::Quote>(id,*this);
case hash("exempt"): return std::make_unique<vote::Exempt>(id,*this);
case hash("unexempt"): return std::make_unique<vote::UnExempt>(id,*this);
case hash("invex"): return std::make_unique<vote::Invex>(id,*this);
case hash("uninvex"): return std::make_unique<vote::UnInvex>(id,*this);
case hash("op"): return std::make_unique<vote::Op>(id,*this);
case hash("deop"): return std::make_unique<vote::DeOp>(id,*this);
case hash("ban"): return std::make_unique<vote::Ban>(id,*this);
case hash("unban"): return std::make_unique<vote::UnBan>(id,*this);
case hash("quiet"): return std::make_unique<vote::Quiet>(id,*this);
case hash("unquiet"): return std::make_unique<vote::UnQuiet>(id,*this);
case hash("voice"): return std::make_unique<vote::Voice>(id,*this);
case hash("devoice"): return std::make_unique<vote::DeVoice>(id,*this);
case hash("flags"): return std::make_unique<vote::Flags>(id,*this);
case hash("civis"): return std::make_unique<vote::Civis>(id,*this);
case hash("censure"): return std::make_unique<vote::Censure>(id,*this);
case hash("import"): return std::make_unique<vote::Import>(id,*this);
default: return std::make_unique<Vote>("",id,*this);
}
}
catch(const Exception &e)
{
if(exists(id))
throw;
throw Exception("Could not find a vote by that ID.");
}
bool Vdb::exists(const id_t &id)
const
{
return Adb::exists(lex_cast(id));
}