summaryrefslogtreecommitdiff
path: root/storage/mroonga/vendor/groonga/lib/grn_egn.hpp
blob: 46511afd6caf87a0e068dcd4fabf25aae43732ad (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* -*- c-basic-offset: 2 -*- */
/*
  Copyright(C) 2015 Brazil

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License version 2.1 as published by the Free Software Foundation.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef GRN_EGN_HPP
#define GRN_EGN_HPP

#include <cstring>
#include <vector>

#include "grn_egn.h"

namespace grn {
namespace egn {

// Constant values.

typedef grn_egn_operator_type OperatorType;
typedef grn_egn_data_type DataType;

typedef grn_egn_expression_node_type ExpressionNodeType;
typedef grn_egn_expression_type ExpressionType;

// Built-in data types.

typedef grn_egn_id ID;
typedef grn_egn_score Score;
typedef grn_egn_record Record;

//typedef grn_egn_bool Bool;
//typedef grn_egn_int Int;
//typedef grn_egn_float Float;
//typedef grn_egn_time Time;
//typedef grn_egn_text Text;
//typedef grn_egn_geo_point GeoPoint;

//inline bool operator==(const Text &lhs, const Text &rhs) {
//  if (lhs.size != rhs.size) {
//    return false;
//  }
//  return std::memcmp(lhs.ptr, rhs.ptr, lhs.size) == 0;
//}
//inline bool operator!=(const Text &lhs, const Text &rhs) {
//  if (lhs.size != rhs.size) {
//    return true;
//  }
//  return std::memcmp(lhs.ptr, rhs.ptr, lhs.size) != 0;
//}
//inline bool operator<(const Text &lhs, const Text &rhs) {
//  size_t min_size = (lhs.size < rhs.size) ? lhs.size : rhs.size;
//  int result = std::memcmp(lhs.ptr, rhs.ptr, min_size);
//  if (result == 0) {
//    return lhs.size < rhs.size;
//  }
//  return result < 0;
//}
//inline bool operator<=(const Text &lhs, const Text &rhs) {
//  size_t min_size = (lhs.size < rhs.size) ? lhs.size : rhs.size;
//  int result = std::memcmp(lhs.ptr, rhs.ptr, min_size);
//  if (result == 0) {
//    return lhs.size <= rhs.size;
//  }
//  return result <= 0;
//}
//inline bool operator>(const Text &lhs, const Text &rhs) {
//  return rhs < lhs;
//}
//inline bool operator>=(const Text &lhs, const Text &rhs) {
//  return rhs <= lhs;
//}

//inline bool operator==(const GeoPoint &lhs, const GeoPoint &rhs) {
//  return (lhs.latitude == rhs.latitude) && (lhs.longitude == rhs.longitude);
//}
//inline bool operator!=(const GeoPoint &lhs, const GeoPoint &rhs) {
//  return (lhs.latitude != rhs.latitude) || (lhs.longitude != rhs.longitude);
//}

struct Bool {
  typedef grn_egn_bool Raw;
  Raw raw;

  static DataType data_type() {
    return GRN_DB_BOOL;
  }

  Bool() : raw() {}
  Bool(const Bool &value) : raw(value.raw) {}
  explicit Bool(Raw value) : raw(value) {}
  ~Bool() {}
};

inline bool operator!(Bool value) { return !value.raw; }
inline bool operator==(Bool lhs, Bool rhs) { return lhs.raw == rhs.raw; }
inline bool operator!=(Bool lhs, Bool rhs) { return lhs.raw != rhs.raw; }

struct Int {
  typedef grn_egn_int Raw;
  Raw raw;

  static DataType data_type() {
    return GRN_DB_INT64;
  }

  Int() : raw() {}
  Int(const Int &value) : raw(value.raw) {}
  explicit Int(Raw value) : raw(value) {}
  ~Int() {}
};

inline bool operator==(Int lhs, Int rhs) { return lhs.raw == rhs.raw; }
inline bool operator!=(Int lhs, Int rhs) { return lhs.raw != rhs.raw; }
inline bool operator<(Int lhs, Int rhs) { return lhs.raw < rhs.raw; }
inline bool operator<=(Int lhs, Int rhs) { return lhs.raw <= rhs.raw; }
inline bool operator>(Int lhs, Int rhs) { return lhs.raw > rhs.raw; }
inline bool operator>=(Int lhs, Int rhs) { return lhs.raw >= rhs.raw; }

struct Float {
  typedef grn_egn_float Raw;
  Raw raw;

  static DataType data_type() {
    return GRN_DB_FLOAT;
  }

  Float() : raw() {}
  Float(const Float &value) : raw(value.raw) {}
  explicit Float(Raw value) : raw(value) {}
  ~Float() {}
};

inline bool operator==(Float lhs, Float rhs) { return lhs.raw == rhs.raw; }
inline bool operator!=(Float lhs, Float rhs) { return lhs.raw != rhs.raw; }
inline bool operator<(Float lhs, Float rhs) { return lhs.raw < rhs.raw; }
inline bool operator<=(Float lhs, Float rhs) { return lhs.raw <= rhs.raw; }
inline bool operator>(Float lhs, Float rhs) { return lhs.raw > rhs.raw; }
inline bool operator>=(Float lhs, Float rhs) { return lhs.raw >= rhs.raw; }

struct Time {
  typedef grn_egn_time Raw;
  Raw raw;

  static DataType data_type() {
    return GRN_DB_TIME;
  }

  Time() : raw() {}
  Time(const Time &value) : raw(value.raw) {}
  explicit Time(Raw value) : raw(value) {}
  ~Time() {}
};

inline bool operator==(Time lhs, Time rhs) { return lhs.raw == rhs.raw; }
inline bool operator!=(Time lhs, Time rhs) { return lhs.raw != rhs.raw; }
inline bool operator<(Time lhs, Time rhs) { return lhs.raw < rhs.raw; }
inline bool operator<=(Time lhs, Time rhs) { return lhs.raw <= rhs.raw; }
inline bool operator>(Time lhs, Time rhs) { return lhs.raw > rhs.raw; }
inline bool operator>=(Time lhs, Time rhs) { return lhs.raw >= rhs.raw; }

struct Text {
  typedef grn_egn_text Raw;
  Raw raw;

  static DataType data_type() {
    return GRN_DB_TEXT;
  }

  Text() : raw() {}
  Text(const Text &value) : raw(value.raw) {}
  explicit Text(const Raw &value) : raw(value) {}
  Text(const char *ptr, size_t size) : raw((Raw){ptr, size}) {}
  ~Text() {}
};

inline bool operator==(const Text &lhs, const Text &rhs) {
  if (lhs.raw.size != rhs.raw.size) {
    return false;
  }
  return std::memcmp(lhs.raw.ptr, rhs.raw.ptr, lhs.raw.size) == 0;
}
inline bool operator!=(const Text &lhs, const Text &rhs) {
  if (lhs.raw.size != rhs.raw.size) {
    return true;
  }
  return std::memcmp(lhs.raw.ptr, rhs.raw.ptr, lhs.raw.size) != 0;
}
inline bool operator<(const Text &lhs, const Text &rhs) {
  size_t min_size = (lhs.raw.size < rhs.raw.size) ?
                    lhs.raw.size : rhs.raw.size;
  int result = std::memcmp(lhs.raw.ptr, rhs.raw.ptr, min_size);
  if (result == 0) {
    return lhs.raw.size < rhs.raw.size;
  }
  return result < 0;
}
inline bool operator<=(const Text &lhs, const Text &rhs) {
  size_t min_size = (lhs.raw.size < rhs.raw.size) ?
                    lhs.raw.size : rhs.raw.size;
  int result = std::memcmp(lhs.raw.ptr, rhs.raw.ptr, min_size);
  if (result == 0) {
    return lhs.raw.size <= rhs.raw.size;
  }
  return result <= 0;
}
inline bool operator>(const Text &lhs, const Text &rhs) { return rhs < lhs; }
inline bool operator>=(const Text &lhs, const Text &rhs) { return rhs <= lhs; }

struct GeoPoint {
  typedef grn_egn_geo_point Raw;
  Raw raw;

  static DataType data_type() {
    return GRN_DB_WGS84_GEO_POINT;
  }

  GeoPoint() : raw() {}
  GeoPoint(const GeoPoint &value) : raw(value.raw) {}
  explicit GeoPoint(Raw value) : raw(value) {}
  GeoPoint(int latitude, int longitude) : raw((Raw){ latitude, longitude }) {}
  ~GeoPoint() {}
};

inline bool operator==(GeoPoint lhs, GeoPoint rhs) {
  return (lhs.raw.latitude == rhs.raw.latitude) &&
         (lhs.raw.longitude == rhs.raw.longitude);
}
inline bool operator!=(GeoPoint lhs, GeoPoint rhs) {
  return (lhs.raw.latitude != rhs.raw.latitude) ||
         (lhs.raw.longitude != rhs.raw.longitude);
}

// Cursor is a base class which provides an interface for sequential access to
// records.
class Cursor {
 public:
  Cursor() {}
  virtual ~Cursor() {}

  // FIXME: Give me options.
  static grn_rc open_table_cursor(grn_ctx *ctx, grn_obj *table,
                                  Cursor **cursor);

  virtual grn_rc read(Record *records, size_t size, size_t *count);
};

// ExpressionNode is an element of Expression.
class ExpressionNode;

// Expression is a class which represents an expression.
class Expression {
 public:
  Expression(grn_ctx *ctx, grn_obj *table);
  ~Expression();

  static grn_rc open(grn_ctx *ctx, grn_obj *table, Expression **expression);
  static grn_rc parse(grn_ctx *ctx, grn_obj *table,
                      const char *query, size_t query_size,
                      Expression **expression);

  ExpressionType type() const {
    return type_;
  }
  DataType data_type() const {
    return data_type_;
  }

  grn_rc push_object(grn_obj *obj);
  grn_rc push_operator(grn_operator operator_type);

  grn_rc filter(Record *input, size_t input_size,
                Record *output, size_t *output_size);
  grn_rc adjust(Record *records, size_t num_records);

  template <typename T>
  grn_rc evaluate(const Record *records, size_t num_records, T *results);

 private:
  grn_ctx *ctx_;
  grn_obj *table_;
  ExpressionType type_;
  DataType data_type_;
  std::vector<ExpressionNode *> stack_;

  // Disable copy and assignment.
  Expression(const Expression &);
  Expression &operator=(const Expression &);

  ExpressionNode *root() const;

  void update_types();

  grn_rc push_bulk_object(grn_obj *obj);
  grn_rc push_column_object(grn_obj *obj);

  grn_rc create_unary_node(OperatorType operator_type,
    ExpressionNode *arg, ExpressionNode **node);
  grn_rc create_binary_node(OperatorType operator_type,
    ExpressionNode *arg1, ExpressionNode *arg2, ExpressionNode **node);
};

}  // namespace egn
}  // namespace grn

#endif  // GRN_EGN_HPP