-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathifx_conncache.c
217 lines (184 loc) · 5.09 KB
/
ifx_conncache.c
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
/*-------------------------------------------------------------------------
*
* ifx_conncache.c
* foreign-data wrapper for IBM INFORMIX databases
*
* Copyright (c) 2012, credativ GmbH
*
* IDENTIFICATION
* informix_fdw/ifx_conncache.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "utils/memutils.h"
#include "ifx_conncache.h"
/*
* Expected number of cached connections.
*/
#define IFX_CONNCACHE_SIZE 16
/*
* Name of the connection hash table
*/
#define IFX_CONNCACHE_HASHTABLE "IFX_CONN_CACHE"
/*
* Flags for dynahash hashtable, version dependent
*/
#if PG_VERSION_NUM >= 140000
#define IFX_FDW_CONNCACHE_HASH_FLAGS HASH_ELEM | HASH_CONTEXT | HASH_STRINGS
#else
#define IFX_FDW_CONNCACHE_HASH_FLAGS HASH_ELEM | HASH_CONTEXT
#endif
static void ifxConnCache_init(void);
bool IfxCacheIsInitialized;
InformixCache ifxCache;
void InformixCacheInit()
{
if (!IfxCacheIsInitialized)
{
ifxConnCache_init();
IfxCacheIsInitialized = true;
}
}
/*
* Initialize INFORMIX connection cache. Each connection
* to an INFORMIX server is explicitely named and cached
* within a backend-local list.
*/
static void ifxConnCache_init()
{
HASHCTL hash_ctl;
MemoryContext old_ctxt;
memset(&hash_ctl, 0, sizeof(hash_ctl));
hash_ctl.keysize = IFX_CONNAME_LEN;
hash_ctl.entrysize = sizeof(IfxCachedConnection);
/*
* We need to allocate within the backend's
* memory context, otherwise we will loose all allocated
* objects when the transaction ends.
*/
hash_ctl.hcxt = TopMemoryContext;
old_ctxt = MemoryContextSwitchTo(TopMemoryContext);
ifxCache.connections = hash_create(IFX_CONNCACHE_HASHTABLE,
IFX_CONNCACHE_SIZE,
&hash_ctl,
IFX_FDW_CONNCACHE_HASH_FLAGS);
/*
* Back to old context.
*/
MemoryContextSwitchTo(old_ctxt);
}
/*
* Add a new INFORMIX connection to the connection cache.
*/
IfxCachedConnection *
ifxConnCache_add(Oid foreignTableOid, IfxConnectionInfo *coninfo, bool *found)
{
IfxCachedConnection *item;
Assert(IfxCacheIsInitialized);
/*
* Lookup the connection name. If it is *not*
* already registered, create a new cached entry, otherwise
* return the cached item.
*/
item = hash_search(ifxCache.connections, (void *) coninfo->conname,
HASH_ENTER, found);
/*
* Connection already cached?
*/
if (!*found)
{
MemoryContext old_cxt;
/*
* Make sure, cached connection information is
* allocated within the memory context actually
* used by the connection cache.
*/
old_cxt = MemoryContextSwitchTo(TopMemoryContext);
item->establishedByOid = foreignTableOid;
item->con.servername = pstrdup(coninfo->servername);
item->con.informixdir = pstrdup(coninfo->informixdir);
item->con.username = pstrdup(coninfo->username);
item->con.database = pstrdup(coninfo->database);
/* This is a no-op. When looking up the connection handle, those
* settings might not yet be initialized properly. Copy it anyways to
* make sure we make it consistent with the connection handle. The caller
* should set those settings which can vary across cache retrieval
* itself.
*/
item->con.tx_enabled = coninfo->tx_enabled;
item->con.db_ansi = coninfo->db_ansi;
item->con.tx_in_progress = 0;
/*
* Init statistics.
*/
item->con.tx_num_commit = 0;
item->con.tx_num_rollback = 0;
/* can be NULL */
if (coninfo->db_locale != NULL)
item->con.db_locale = pstrdup(coninfo->db_locale);
else
item->con.db_locale = NULL;
if (coninfo->client_locale != NULL)
item->con.client_locale = pstrdup(coninfo->client_locale);
else
item->con.client_locale = NULL;
/* also initialize usage counter */
item->con.usage = 1;
MemoryContextSwitchTo(old_cxt);
}
else
{
/*
* NOTE:
*
* If coninfo was specified with IFX_PLAN_SCAN (meaning a new scan on a
* foreign table was initiated), we need to increase the usage counter to ensure
* a new refid for all identifier used by this scan is generated.
*/
if ( (coninfo->scan_mode == IFX_PLAN_SCAN)
|| (coninfo->scan_mode == IFX_IMPORT_SCHEMA) )
{
item->con.usage++;
}
}
return item;
}
/*
* Check for an existing Informix connection
*/
IfxCachedConnection *
ifxConnCache_exists(char *conname, bool *found)
{
IfxCachedConnection *item;
/*
* Lookup the connection name, return true
* in case the identifier was found in the connection
* cache.
*/
item = hash_search(ifxCache.connections, (void *) conname,
HASH_FIND, found);
return ((found) ? item : NULL);
}
/*
* Remove an existing connection handle from the cache.
* If the requested connection doesn't exist yet, NULL
* is returned.
*/
IfxCachedConnection *
ifxConnCache_rm(char *conname, bool *found)
{
IfxCachedConnection *item;
Assert(IfxCacheIsInitialized);
/*
* Lookup the connection name. If found, the entry is
* removed from the cache, but returned to the caller.
*/
item = hash_search(ifxCache.connections, (void *) conname,
HASH_REMOVE, found);
/*
* If something found, return it, otherwise
* NULL is returned.
*/
return item;
}