forked from EnterpriseDB/mongo_fdw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_fdw.h
323 lines (285 loc) · 10.1 KB
/
mongo_fdw.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
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
319
320
321
322
323
/*-------------------------------------------------------------------------
*
* mongo_fdw.h
* Foreign-data wrapper for remote MongoDB servers
*
* Portions Copyright (c) 2012-2014, PostgreSQL Global Development Group
*
* Portions Copyright (c) 2004-2014, EnterpriseDB Corporation.
*
* Portions Copyright (c) 2012–2014 Citus Data, Inc.
*
* IDENTIFICATION
* mongo_fdw.h
*
*-------------------------------------------------------------------------
*/
#ifndef MONGO_FDW_H
#define MONGO_FDW_H
#include "config.h"
#include "mongo_wrapper.h"
#include "bson.h"
#ifdef META_DRIVER
#include "mongoc.h"
#else
#include "mongo.h"
#endif
#include "fmgr.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_foreign_table.h"
#include "utils/datetime.h"
#include "nodes/pg_list.h"
#if PG_VERSION_NUM < 120000
#include "nodes/relation.h"
#endif
#include "utils/timestamp.h"
#include "access/reloptions.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#include "commands/vacuum.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
#include "nodes/makefuncs.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/plancat.h"
#include "optimizer/planmain.h"
#include "optimizer/restrictinfo.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/hsearch.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/memutils.h"
#include "catalog/pg_user_mapping.h"
#ifdef META_DRIVER
#define BSON bson_t
#define BSON_TYPE bson_type_t
#define BSON_ITERATOR bson_iter_t
#define MONGO_CONN mongoc_client_t
#define MONGO_CURSOR mongoc_cursor_t
#define BSON_TYPE_DOCUMENT BSON_TYPE_DOCUMENT
#define BSON_TYPE_NULL BSON_TYPE_NULL
#define BSON_TYPE_ARRAY BSON_TYPE_ARRAY
#define BSON_TYPE_INT32 BSON_TYPE_INT32
#define BSON_TYPE_INT64 BSON_TYPE_INT64
#define BSON_TYPE_DOUBLE BSON_TYPE_DOUBLE
#define BSON_TYPE_BINDATA BSON_TYPE_BINARY
#define BSON_TYPE_BOOL BSON_TYPE_BOOL
#define BSON_TYPE_UTF8 BSON_TYPE_UTF8
#define BSON_TYPE_OID BSON_TYPE_OID
#define BSON_TYPE_DATE_TIME BSON_TYPE_DATE_TIME
#define BSON_TYPE_SYMBOL BSON_TYPE_SYMBOL
#define BSON_TYPE_UNDEFINED BSON_TYPE_UNDEFINED
#define BSON_TYPE_REGEX BSON_TYPE_REGEX
#define BSON_TYPE_CODE BSON_TYPE_CODE
#define BSON_TYPE_CODEWSCOPE BSON_TYPE_CODEWSCOPE
#define BSON_TYPE_TIMESTAMP BSON_TYPE_TIMESTAMP
#define PREF_READ_PRIMARY_NAME "readPrimary"
#define PREF_READ_SECONDARY_NAME "readSecondary"
#define PREF_READ_PRIMARY_PREFERRED_NAME "readPrimaryPreferred"
#define PREF_READ_SECONDARY_PREFERRED_NAME "readSecondaryPreferred"
#define PREF_READ_NEAREST_NAME "readNearest"
#define BSON_ITER_BOOL bson_iter_bool
#define BSON_ITER_DOUBLE bson_iter_double
#define BSON_ITER_INT32 bson_iter_int32
#define BSON_ITER_INT64 bson_iter_int64
#define BSON_ITER_OID bson_iter_oid
#define BSON_ITER_UTF8 bson_iter_utf8
#define BSON_ITER_REGEX bson_iter_regex
#define BSON_ITER_DATE_TIME bson_iter_date_time
#define BSON_ITER_CODE bson_iter_code
#define BSON_ITER_VALUE bson_iter_value
#define BSON_ITER_KEY bson_iter_key
#define BSON_ITER_NEXT bson_iter_next
#define BSON_ITER_TYPE bson_iter_type
#define BSON_ITER_BINARY bson_iter_binary
#else
#define BSON bson
#define BSON_TYPE bson_type
#define BSON_ITERATOR bson_iterator
#define MONGO_CONN mongo
#define MONGO_CURSOR mongo_cursor
#define BSON_TYPE_DOCUMENT BSON_OBJECT
#define BSON_TYPE_NULL BSON_NULL
#define BSON_TYPE_ARRAY BSON_ARRAY
#define BSON_TYPE_INT32 BSON_INT
#define BSON_TYPE_INT64 BSON_LONG
#define BSON_TYPE_DOUBLE BSON_DOUBLE
#define BSON_TYPE_BINDATA BSON_BINDATA
#define BSON_TYPE_BOOL BSON_BOOL
#define BSON_TYPE_UTF8 BSON_STRING
#define BSON_TYPE_OID BSON_OID
#define BSON_TYPE_DATE_TIME BSON_DATE
#define BSON_TYPE_SYMBOL BSON_SYMBOL
#define BSON_TYPE_UNDEFINED BSON_UNDEFINED
#define BSON_TYPE_REGEX BSON_REGEX
#define BSON_TYPE_CODE BSON_CODE
#define BSON_TYPE_CODEWSCOPE BSON_CODEWSCOPE
#define BSON_TYPE_TIMESTAMP BSON_TIMESTAMP
#define BSON_ITER_BOOL bson_iterator_bool
#define BSON_ITER_DOUBLE bson_iterator_double
#define BSON_ITER_INT32 bson_iterator_int
#define BSON_ITER_INT64 bson_iterator_long
#define BSON_ITER_OID bson_iterator_oid
#define BSON_ITER_UTF8 bson_iterator_string
#define BSON_ITER_REGEX bson_iterator_regex
#define BSON_ITER_DATE_TIME bson_iterator_date
#define BSON_ITER_CODE bson_iterator_code
#define BSON_ITER_VALUE bson_iterator_value
#define BSON_ITER_KEY bson_iterator_key
#define BSON_ITER_NEXT bson_iterator_next
#define BSON_ITER_TYPE bson_iterator_type
#define BSON_ITER_BINARY bson_iterator_bin_data
#endif
/* Defines for valid option names */
#define OPTION_NAME_ADDRESS "address"
#define OPTION_NAME_PORT "port"
#define OPTION_NAME_DATABASE "database"
#define OPTION_NAME_COLLECTION "collection"
#define OPTION_NAME_USERNAME "username"
#define OPTION_NAME_PASSWORD "password"
#ifdef META_DRIVER
#define OPTION_NAME_READ_PREFERENCE "read_preference"
#define OPTION_NAME_AUTHENTICATION_DATABASE "authentication_database"
#define OPTION_NAME_REPLICA_SET "replica_set"
#define OPTION_NAME_SSL "ssl"
#define OPTION_NAME_PEM_FILE "pem_file"
#define OPTION_NAME_PEM_PWD "pem_pwd"
#define OPTION_NAME_CA_FILE "ca_file"
#define OPTION_NAME_CA_DIR "ca_dir"
#define OPTION_NAME_CRL_FILE "crl_file"
#define OPTION_NAME_WEAK_CERT "weak_cert_validation"
#endif
/* Default values for option parameters */
#define DEFAULT_IP_ADDRESS "127.0.0.1"
#define DEFAULT_PORT_NUMBER 27017
#define DEFAULT_DATABASE_NAME "test"
/* Defines for sending queries and converting types */
#define EQUALITY_OPERATOR_NAME "="
#define INITIAL_ARRAY_CAPACITY 8
#define MONGO_TUPLE_COST_MULTIPLIER 5
#define MONGO_CONNECTION_COST_MULTIPLIER 5
#define POSTGRES_TO_UNIX_EPOCH_DAYS (POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE)
#define POSTGRES_TO_UNIX_EPOCH_USECS (POSTGRES_TO_UNIX_EPOCH_DAYS * USECS_PER_DAY)
/*
* MongoValidOption keeps an option name and a context. When an option is passed
* into mongo_fdw objects (server and foreign table), we compare this option's
* name and context against those of valid options.
*/
typedef struct MongoValidOption
{
const char *optionName;
Oid optionContextId;
} MongoValidOption;
/* Array of options that are valid for mongo_fdw */
#ifdef META_DRIVER
static const uint32 ValidOptionCount = 16;
#else
static const uint32 ValidOptionCount = 6;
#endif
static const MongoValidOption ValidOptionArray[] =
{
/* foreign server options */
{ OPTION_NAME_ADDRESS, ForeignServerRelationId },
{ OPTION_NAME_PORT, ForeignServerRelationId },
#ifdef META_DRIVER
{ OPTION_NAME_READ_PREFERENCE, ForeignServerRelationId },
{ OPTION_NAME_AUTHENTICATION_DATABASE, ForeignServerRelationId },
{ OPTION_NAME_REPLICA_SET, ForeignServerRelationId },
{ OPTION_NAME_SSL, ForeignServerRelationId },
{ OPTION_NAME_PEM_FILE, ForeignServerRelationId },
{ OPTION_NAME_PEM_PWD, ForeignServerRelationId },
{ OPTION_NAME_CA_FILE, ForeignServerRelationId },
{ OPTION_NAME_CA_DIR, ForeignServerRelationId },
{ OPTION_NAME_CRL_FILE, ForeignServerRelationId },
{ OPTION_NAME_WEAK_CERT, ForeignServerRelationId },
#endif
/* foreign table options */
{ OPTION_NAME_DATABASE, ForeignTableRelationId },
{ OPTION_NAME_COLLECTION, ForeignTableRelationId },
/* User mapping options */
{ OPTION_NAME_USERNAME, UserMappingRelationId },
{ OPTION_NAME_PASSWORD, UserMappingRelationId }
};
/*
* MongoFdwOptions holds the option values to be used when connecting to Mongo.
* To resolve these values, we first check foreign table's options, and if not
* present, we then fall back to the default values specified above.
*/
typedef struct MongoFdwOptions
{
char *svr_address;
int32 svr_port;
char *svr_database;
char *collectionName;
char *svr_username;
char *svr_password;
#ifdef META_DRIVER
char *readPreference;
char *authenticationDatabase;
char *replicaSet;
bool ssl;
char *pem_file;
char *pem_pwd;
char *ca_file;
char *ca_dir;
char *crl_file;
bool weak_cert_validation;
#endif
} MongoFdwOptions;
/*
* MongoFdwExecState keeps foreign data wrapper specific execution state that we
* create and hold onto when executing the query.
*/
/*
* Execution state of a foreign insert/update/delete operation.
*/
typedef struct MongoFdwModifyState
{
Relation rel; /* relcache entry for the foreign table */
List *target_attrs; /* list of target attribute numbers */
/* info about parameters for prepared statement */
int p_nums; /* number of parameters to transmit */
FmgrInfo *p_flinfo; /* output conversion functions for them */
struct HTAB *columnMappingHash;
MONGO_CONN *mongoConnection; /* MongoDB connection */
MONGO_CURSOR *mongoCursor; /* MongoDB cursor */
BSON *queryDocument; /* Bson Document */
MongoFdwOptions *options;
/* working memory context */
MemoryContext temp_cxt; /* context for per-tuple temporary data */
} MongoFdwModifyState;
/*
* ColumnMapping reprents a hash table entry that maps a column name to column
* related information. We construct these hash table entries to speed up the
* conversion from BSON documents to PostgreSQL tuples; and each hash entry maps
* the column name to the column's tuple index and its type-related information.
*/
typedef struct ColumnMapping
{
char columnName[NAMEDATALEN];
uint32 columnIndex;
Oid columnTypeId;
int32 columnTypeMod;
Oid columnArrayTypeId;
} ColumnMapping;
/* options.c */
extern MongoFdwOptions * mongo_get_options(Oid foreignTableId);
extern void mongo_free_options(MongoFdwOptions *options);
extern StringInfo mongo_option_names_string(Oid currentContextId);
/* connection.c */
MONGO_CONN* mongo_get_connection(ForeignServer *server, UserMapping *user, MongoFdwOptions *opt);
extern void mongo_cleanup_connection(void);
extern void mongo_release_connection(MONGO_CONN* conn);
/* Function declarations related to creating the mongo query */
extern List * ApplicableOpExpressionList(RelOptInfo *baserel);
extern BSON * QueryDocument(Oid relationId, List *opExpressionList,
ForeignScanState *scanStateNode);
extern List * ColumnList(RelOptInfo *baserel);
/* Function declarations for foreign data wrapper */
extern Datum mongo_fdw_handler(PG_FUNCTION_ARGS);
extern Datum mongo_fdw_validator(PG_FUNCTION_ARGS);
#endif /* MONGO_FDW_H */