File: types.h

package info (click to toggle)
php-mongo 1.5.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,040 kB
  • ctags: 2,802
  • sloc: ansic: 17,632; xml: 2,195; php: 1,630; pascal: 330; makefile: 52; sh: 39
file content (304 lines) | stat: -rw-r--r-- 11,297 bytes parent folder | download
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
/**
 *  Copyright 2009-2014 MongoDB, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
#ifndef __MCON_TYPES_H__
#define __MCON_TYPES_H__

#include <stdarg.h>

/* Windows compatibility */
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#ifndef strcasecmp
# define strcasecmp(a,b) stricmp((a), (b))
#endif
#ifndef snprintf
# define snprintf _snprintf
#endif
#ifndef va_copy
# define va_copy(d,s) ((void)((d) = (s)))
#endif
#else
# include <stdint.h>
# include <sys/types.h>
# include <netinet/in.h>
# include <netinet/tcp.h>
# include <fcntl.h>
# include <netdb.h>
# include <sys/un.h>
# include <sys/socket.h>
# include <unistd.h>
# include <sys/time.h>
#endif

#define MONGO_CON_TYPE_STANDALONE 1
#define MONGO_CON_TYPE_MULTIPLE   2
#define MONGO_CON_TYPE_REPLSET    3

/* Bitfield used for fetching connections */
#define MONGO_CON_FLAG_READ            0x01
#define MONGO_CON_FLAG_WRITE           0x02
#define MONGO_CON_FLAG_DONT_CONNECT    0x04
#define MONGO_CON_FLAG_DONT_FILTER     0x08

/* These constants are a bit field - however, each connection will only have
 * one type. The reason why it's a bit field is because of filtering during
 * read preference scanning (see read_preference.c).
 *
 * SECONDARY needs to have a larger constant value than PRIMARY for the read
 * preference sorting algorithm to work. */
#define MONGO_NODE_INVALID        0x00
#define MONGO_NODE_STANDALONE     0x01
#define MONGO_NODE_PRIMARY        0x02
#define MONGO_NODE_SECONDARY      0x04
#define MONGO_NODE_ARBITER        0x08
#define MONGO_NODE_MONGOS         0x10

/* Constants for health states as returned by replSetGetStatus
 * From: http://docs.mongodb.org/manual/reference/replica-status/#statuses */
#define MONGO_STATE_STARTING1     0x00
#define MONGO_STATE_PRIMARY       0x01
#define MONGO_STATE_SECONDARY     0x02
#define MONGO_STATE_RECOVERING    0x03
#define MONGO_STATE_FATAL_ERROR   0x04
#define MONGO_STATE_STARTING2     0x05
#define MONGO_STATE_UNKNOWN       0x06
#define MONGO_STATE_ARBITER       0x07
#define MONGO_STATE_DOWN          0x08
#define MONGO_STATE_ROLLBACK      0x09
#define MONGO_STATE_REMOVED       0x0a

/* Constants for the logging framework */

/* Levels */
#define MLOG_WARN    1
#define MLOG_INFO    2
#define MLOG_FINE    4

/* Modules */
#define MLOG_RS      1
#define MLOG_CON     2
#define MLOG_IO      4
#define MLOG_SERVER  8
#define MLOG_PARSE  16

#define MLOG_NONE    0
#define MLOG_ALL    31 /* Must be the bit sum of all above */

/* Defaults */
#define MONGO_DEFAULT_MAX_DOCUMENT_SIZE (16 * 1024 * 1024)
#define MONGO_DEFAULT_MAX_MESSAGE_SIZE  (32 * 1024 * 1024)


/* FIXME: This should be dynamic. Although mongod doesn't allow more then 12
 * replicaset members, there is nothing preventing us from connecting to 20
 * mongos' */
#define MAX_SERVERS_LIMIT   16

/* To track why we are closing a connection */
#define MONGO_CLOSE_SHUTDOWN 1 /* In our shutdown procedures */
#define MONGO_CLOSE_BROKEN 2   /* The connection is unusable */

/* Enable/Disable SSL */
#define MONGO_SSL_DISABLE 0
#define MONGO_SSL_ENABLE 1
#define MONGO_SSL_PREFER 2

typedef int (mongo_cleanup_t)(void *callback_data);

typedef struct _mongo_connection_deregister_callback
{
	void                                         *callback_data;
	mongo_cleanup_t                              *mongo_cleanup_cb;
	struct _mongo_connection_deregister_callback *next;
} mongo_connection_deregister_callback;

/* Stores all the information about the connection. The hash is a group of
 * parameters to identify a unique connection. */
typedef struct _mongo_connection
{
	time_t last_ping;        /* The timestamp when ping was called last */
	int    ping_ms;
	int    connected;        /* Whether the connection is connected. Used for establishing which timeout to use */
	int    last_ismaster;    /* The timestamp when ismaster/get_server_flags was called last */
	int    last_replcheck;   /* The timestamp when ismaster/replicaset test was called last */
	int    last_reqid;
	void  *socket;           /* void* so we can support different "socket" backends */
	int    connection_type;  /* MONGO_NODE_: PRIMARY, SECONDARY, ARBITER, MONGOS */
	struct {
		int32_t major;
		int32_t minor;
		int32_t mini;
		int32_t build;
	} version;
	int    min_wire_version; /* Minimum wire version supported by mongo[d|s] */
	int    max_wire_version; /* Maximum wire version supported by mongo[d|s] */
	int    max_bson_size;    /* Maximum size of each document. Store per connection, as it can actually differ. */
	int    max_message_size; /* Maximum size of each data packet. Store per connection, as it can actually differ. */
	int    max_write_batch_size; /* Maximum operations in a batch */
	int    tag_count;
	char **tags;
	char  *hash;             /* Duplicate of the hash that the manager knows this connection as */
	mongo_connection_deregister_callback *cleanup_list;
} mongo_connection;

/* MongoDB pre-1.8; Spec says default to 4 MB */
#define MONGO_CONNECTION_DEFAULT_MAX_BSON_SIZE 4194304
/* MongoDB pre-2.4; Spec says default to 2 * the maxBsonSize */
#define MONGO_CONNECTION_DEFAULT_MAX_MESSAGE_SIZE 2 * MONGO_CONNECTION_DEFAULT_MAX_BSON_SIZE
/* Default wire versions for MongoDB pre-2.6 */
#define MONGO_CONNECTION_DEFAULT_MIN_WIRE_VERSION 0
#define MONGO_CONNECTION_DEFAULT_MAX_WIRE_VERSION 0
/* Default max operations in a batch .. for 2.6 api */
#define MONGO_CONNECTION_DEFAULT_MAX_WRITE_BATCH_SIZE 1000
/* Default connection timeout setting (same as default_socket_timeout INI default) */
#define MONGO_CONNECTION_DEFAULT_CONNECT_TIMEOUT 60000L
/* Default socket timeout setting.. Not cool. But legacy */
#define MONGO_CONNECTION_DEFAULT_SOCKET_TIMEOUT 30000L

typedef struct _mongo_connection_blacklist
{
	time_t last_ping;
} mongo_connection_blacklist;

typedef struct _mongo_con_manager_item
{
	char                           *hash;
	void                           *data;
	struct _mongo_con_manager_item *next;
} mongo_con_manager_item;

typedef void (mongo_log_callback_t)(int module, int level, void *context, char *format, va_list arg);

#define MONGO_MANAGER_DEFAULT_PING_INTERVAL     5
#define MONGO_MANAGER_DEFAULT_PING_INTERVAL_S   "5"
#define MONGO_MANAGER_DEFAULT_MASTER_INTERVAL   15
#define MONGO_MANAGER_DEFAULT_MASTER_INTERVAL_S "15"

typedef struct _mongo_read_preference_tagset
{
	int    tag_count;
	char **tags;
} mongo_read_preference_tagset;

typedef struct _mongo_read_preference
{
	int                            type;         /* MONGO_RP_* */
	int                            tagset_count; /* The number of tag sets in this RP */
	mongo_read_preference_tagset **tagsets;
} mongo_read_preference;

#define MONGO_AUTH_MECHANISM_MONGODB_CR   1
#define MONGO_AUTH_MECHANISM_GSSAPI       2
#define MONGO_AUTH_MECHANISM_PLAIN        3
#define MONGO_AUTH_MECHANISM_MONGODB_X509 4

typedef struct _mongo_server_def
{
	char *host;
	int   port;
	char *repl_set_name;
	char *db;
	char *authdb;
	char *username;
	char *password;
	int   mechanism;
} mongo_server_def;

/* NOTE: when making changes, update mongo_parse_init, mongo_servers_copy and mongo_servers_dtor */
typedef struct _mongo_server_options
{
	int   con_type;         /* One of MONGO_CON_TYPE_STANDALONE, MONGO_CON_TYPE_MULTIPLE or MONGO_CON_TYPE_REPLSET */
	char *repl_set_name;
	int   connectTimeoutMS; /* How many milliseconds to wait for when connecting to nodes */
	int   socketTimeoutMS;  /* How many milliseconds to wait for when reading/writing data to nodes */
	int   secondaryAcceptableLatencyMS; /* Latency cutoff point for RP_NEAREST and RP_SECONDARY_PREFERRED */
	int   default_w;        /* The number specifies the number of replica nodes */
	char *default_wstring;  /* If the value for "w" is a string, then it means a getLastError error-mode */
	int   default_wtimeout; /* How many milliseconds to wait for replication to "w" nodes */
	int   default_fsync;    /* 1/0 send fsync=1 by default or not */
	int   default_journal;  /* 1/0 send j=1 by default or not */
	int   ssl;              /* If we should be using SSL */
	char *gssapiServiceName;/* Service Principal Name (Kerberos) */
	void *ctx;              /* Arbitrary implementation dependent options (MongoDB-PHP uses this for stream context) */
} mongo_server_options;

typedef struct _mongo_servers
{
	int                   count;
	mongo_server_def     *server[MAX_SERVERS_LIMIT]; /* TODO: Make this dynamic */

	/* flags and options */
	mongo_server_options  options;
	mongo_read_preference read_pref;
} mongo_servers;

struct _mongo_con_manager;
typedef struct _mongo_con_manager
{
	mongo_con_manager_item *connections;
	mongo_con_manager_item *blacklist;

	/* context and callback function that is used to send logging information
	 * through */
	void                   *log_context;
	mongo_log_callback_t   *log_function;

	/* ping/ismaster will not be called more often than the amount of seconds
	 * that is configured with ping_interval/ismaster_interval. The ismaster
	 * interval is also used for the get_server_flags function. */
	long                    ping_interval;      /* default:  5 seconds */
	long                    ismaster_interval;  /* default: 15 seconds */

	/* IO callbacks, either using the 'native mcon' or external hooks (i.e. PHP Streams) */
	void* (*connect)     (struct _mongo_con_manager *manager, mongo_server_def *server, mongo_server_options *options, char **error_message);
	int   (*recv_header) (mongo_connection *con, mongo_server_options *options, int timeout, void *data, int size, char **error_message);
	int   (*recv_data)   (mongo_connection *con, mongo_server_options *options, int timeout, void *data, int size, char **error_message);
	int   (*send)        (mongo_connection *con, mongo_server_options *options, void *data, int size, char **error_message);
	void  (*close)       (mongo_connection *con, int why);
	void  (*forget)      (struct _mongo_con_manager *manager, mongo_connection *con);
	int   (*authenticate)(struct _mongo_con_manager *manager, mongo_connection *con, mongo_server_options *options, mongo_server_def *server_def, char **error_message);

	/* Check if a wire version supported */
	int (*supports_wire_version) (int min_wire_version, int max_wire_version, char **error_message);
} mongo_con_manager;

typedef void (mongo_con_manager_item_destroy_t)(mongo_con_manager *manager, void *item, int why);

typedef struct _mcon_collection
{
	int count;
	int space;
	int data_size;
	void **data;
} mcon_collection;

typedef void (mcon_collection_callback_t)(mongo_con_manager *manager, void *elem);

#endif

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: fdm=marker
 * vim: noet sw=4 ts=4
 */