-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.h
100 lines (78 loc) · 2.86 KB
/
query.h
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
/*
Copyright (C) 2013 Sridhar Valaguru <sridharnitt@gmail.com>
This file is part of eXTendDB.
eXTendDB is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
eXTendDB 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with eXTendDB. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __QUERY_H__
#define __QUERY_H__
#include "bson.h"
#include "stringutil.h"
#define BsonToBStr(bsonVal,binaryStr)\
(binaryStr)->data = (bsonVal)->data;\
(binaryStr)->len = bson_size((bsonVal));
//(binaryStr)->len = bson_size((bsonVal));
#define BStrToBson(binaryStr,bsonVal)\
bson_init_finished_data((bsonVal),(char*)(binaryStr)->data);
#define BSON_TYPE_LAST (BSON_LONG+1)
typedef struct BSONElem {
bson_type type;
//`uint8_t buf[32];
void* value;
uint32_t len;
uint8_t freeVal;
bson_iterator* itr;
} BSONElem;
typedef void (*BSONElemConverterFn)(BSONElem* const from,BSONElem* const to);
typedef int (*BSONElemCompareFn)(BSONElem* const val1,
BSONElem* const val2,int* cmpOut);
typedef int (*BSONElemToBStr)(const BSONElem* const from,bson* out);
#define DEFINE_CMP(fnName) int fnName(BSONElem* const val1, BSONElem* const val2,int* cmpOut)
#define DEFINE_TOBSTR(fnName) int fnName(const BSONElem* elem,bson* out)
typedef struct BSONElemOps{
//BSONElemConverter converter;
// Compare any 2 types of BSONElements used in querying
BSONElemCompareFn compare;
// Convert BSONElem to binary data used for indexing
BSONElemToBStr datatobstr;
} BSONElemOps;
void InitQueryModule(void);
int QueryMatch(BinaryStr* query,BinaryStr* obj);
int QueryMatchBson(bson* query,bson* obj);
extern BSONElemOps elemOps[BSON_TYPE_LAST];
static int
ConvertToBStr(BSONElem* val,bson* out) {
assert(val);
assert(out);
bson_init(out);
if (bson_append_element(out,"",val->itr)) {
return ENOMEM;
}
bson_finish(out);
return 0;
//return elemOps[val->type].datatobstr(val,out);
}
typedef uint8_t (*QueryOpFn)(BSONElem* val, BSONElem* qVal, int* err);
typedef struct QueryOpDesc {
char opName[64];
QueryOpFn fn;
} QueryOpDesc;
int RegisterQueryOp(const char* op, QueryOpFn fn);
int RegisterBSONCmp(bson_type type,BSONElemCompareFn fn);
typedef struct BSONElemItr {
BSONElem* val;
bson_iterator it;
uint8_t more;
uint8_t useItr;
} BSONElemItr;
void BSONElemItrInit(BSONElemItr * it,BSONElem* val);
uint8_t BSONElemItrNext(BSONElemItr* it,BSONElem * outVal);
#endif