forked from citusdata/mongo_fdw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_fdw.h
132 lines (105 loc) · 3.56 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
/*-------------------------------------------------------------------------
*
* mongo_fdw.h
*
* Type and function declarations for MongoDB foreign data wrapper.
*
* Copyright (c) 2012, Citus Data, Inc.
*
* $Id$
*
*-------------------------------------------------------------------------
*/
#ifndef MONGO_FDW_H
#define MONGO_FDW_H
#include "bson.h"
#include "mongo.h"
#include "fmgr.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_foreign_table.h"
#include "utils/datetime.h"
#include "nodes/pg_list.h"
#include "nodes/relation.h"
#include "utils/timestamp.h"
/* 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"
/* 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 */
static const uint32 ValidOptionCount = 4;
static const MongoValidOption ValidOptionArray[] =
{
/* foreign server options */
{ OPTION_NAME_ADDRESS, ForeignServerRelationId },
{ OPTION_NAME_PORT, ForeignServerRelationId },
/* foreign table options */
{ OPTION_NAME_DATABASE, ForeignTableRelationId },
{ OPTION_NAME_COLLECTION, ForeignTableRelationId }
};
/*
* 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 *addressName;
int32 portNumber;
char *databaseName;
char *collectionName;
} MongoFdwOptions;
/*
* MongoFdwExecState keeps foreign data wrapper specific execution state that we
* create and hold onto when executing the query.
*/
typedef struct MongoFdwExecState
{
struct HTAB *columnMappingHash;
mongo *mongoConnection;
mongo_cursor *mongoCursor;
bson *queryDocument;
} MongoFdwExecState;
/*
* 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;
/* Function declarations related to creating the mongo query */
extern List * ApplicableOpExpressionList(RelOptInfo *baserel);
extern bson * QueryDocument(Oid relationId, List *opExpressionList);
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 */