]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
31f18b77 FG |
4 | #ifndef CEPH_RGW_ES_QUERY_H |
5 | #define CEPH_RGW_ES_QUERY_H | |
6 | ||
7 | #include "rgw_string.h" | |
8 | ||
9 | class ESQueryStack { | |
10 | list<string> l; | |
11 | list<string>::iterator iter; | |
12 | ||
13 | public: | |
11fdf7f2 | 14 | explicit ESQueryStack(list<string>& src) { |
31f18b77 FG |
15 | assign(src); |
16 | } | |
17 | ||
18 | ESQueryStack() {} | |
19 | ||
20 | void assign(list<string>& src) { | |
21 | l.swap(src); | |
22 | iter = l.begin(); | |
23 | } | |
24 | ||
25 | bool peek(string *dest) { | |
26 | if (done()) { | |
27 | return false; | |
28 | } | |
29 | *dest = *iter; | |
30 | return true; | |
31 | } | |
32 | ||
33 | bool pop(string *dest) { | |
34 | bool valid = peek(dest); | |
35 | if (!valid) { | |
36 | return false; | |
37 | } | |
38 | ++iter; | |
39 | return true; | |
40 | } | |
41 | ||
42 | bool done() { | |
43 | return (iter == l.end()); | |
44 | } | |
45 | }; | |
46 | ||
47 | class ESInfixQueryParser { | |
48 | string query; | |
49 | int size; | |
50 | const char *str; | |
51 | int pos{0}; | |
52 | list<string> args; | |
53 | ||
54 | void skip_whitespace(const char *str, int size, int& pos); | |
55 | bool get_next_token(bool (*filter)(char)); | |
56 | ||
57 | bool parse_condition(); | |
58 | bool parse_and_or(); | |
59 | bool parse_specific_char(const char *pchar); | |
60 | bool parse_open_bracket(); | |
61 | bool parse_close_bracket(); | |
62 | ||
63 | public: | |
11fdf7f2 | 64 | explicit ESInfixQueryParser(const string& _query) : query(_query), size(query.size()), str(query.c_str()) {} |
31f18b77 FG |
65 | bool parse(list<string> *result); |
66 | }; | |
67 | ||
68 | class ESQueryNode; | |
69 | ||
70 | struct ESEntityTypeMap { | |
71 | enum EntityType { | |
72 | ES_ENTITY_NONE = 0, | |
73 | ES_ENTITY_STR = 1, | |
74 | ES_ENTITY_INT = 2, | |
75 | ES_ENTITY_DATE = 3, | |
76 | }; | |
77 | ||
78 | map<string, EntityType> m; | |
79 | ||
11fdf7f2 | 80 | explicit ESEntityTypeMap(map<string, EntityType>& _m) : m(_m) {} |
31f18b77 FG |
81 | |
82 | bool find(const string& entity, EntityType *ptype) { | |
83 | auto i = m.find(entity); | |
84 | if (i != m.end()) { | |
85 | *ptype = i->second; | |
86 | return true; | |
87 | } | |
88 | ||
89 | *ptype = ES_ENTITY_NONE; | |
90 | return false; | |
91 | } | |
92 | }; | |
93 | ||
94 | class ESQueryCompiler { | |
95 | ESInfixQueryParser parser; | |
96 | ESQueryStack stack; | |
97 | ESQueryNode *query_root{nullptr}; | |
98 | ||
99 | string custom_prefix; | |
100 | ||
101 | bool convert(list<string>& infix, string *perr); | |
102 | ||
103 | list<pair<string, string> > eq_conds; | |
104 | ||
105 | ESEntityTypeMap *generic_type_map{nullptr}; | |
106 | ESEntityTypeMap *custom_type_map{nullptr}; | |
107 | ||
224ce89b WB |
108 | map<string, string, ltstr_nocase> *field_aliases = nullptr; |
109 | set<string> *restricted_fields = nullptr; | |
31f18b77 FG |
110 | |
111 | public: | |
112 | ESQueryCompiler(const string& query, list<pair<string, string> > *prepend_eq_conds, const string& _custom_prefix) : parser(query), custom_prefix(_custom_prefix) { | |
113 | if (prepend_eq_conds) { | |
114 | eq_conds = std::move(*prepend_eq_conds); | |
115 | } | |
116 | } | |
117 | ~ESQueryCompiler(); | |
118 | ||
119 | bool compile(string *perr); | |
120 | void dump(Formatter *f) const; | |
121 | ||
122 | void set_generic_type_map(ESEntityTypeMap *entity_map) { | |
123 | generic_type_map = entity_map; | |
124 | } | |
125 | ||
126 | ESEntityTypeMap *get_generic_type_map() { | |
127 | return generic_type_map; | |
128 | } | |
129 | const string& get_custom_prefix() { return custom_prefix; } | |
130 | ||
131 | void set_custom_type_map(ESEntityTypeMap *entity_map) { | |
132 | custom_type_map = entity_map; | |
133 | } | |
134 | ||
135 | ESEntityTypeMap *get_custom_type_map() { | |
136 | return custom_type_map; | |
137 | } | |
138 | ||
139 | void set_field_aliases(map<string, string, ltstr_nocase> *fa) { | |
140 | field_aliases = fa; | |
141 | } | |
142 | ||
143 | string unalias_field(const string& field) { | |
144 | if (!field_aliases) { | |
145 | return field; | |
146 | } | |
147 | auto i = field_aliases->find(field); | |
148 | if (i == field_aliases->end()) { | |
149 | return field; | |
150 | } | |
151 | ||
152 | return i->second; | |
153 | } | |
154 | ||
155 | void set_restricted_fields(set<string> *rf) { | |
156 | restricted_fields = rf; | |
157 | } | |
158 | ||
159 | bool is_restricted(const string& f) { | |
160 | return (restricted_fields && restricted_fields->find(f) != restricted_fields->end()); | |
161 | } | |
162 | }; | |
163 | ||
164 | ||
165 | #endif |