summaryrefslogtreecommitdiff
path: root/src/include/api.h
blob: 22b7c93df4d1a5c4c29410d868a931c629cc95e2 (plain)
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
324
/*-
 * Copyright (c) 2008-2012 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

/*
 * WT_PROCESS --
 *	Per-process information for the library.
 */
struct __wt_process {
	WT_SPINLOCK spinlock;		/* Per-process spinlock */

					/* Locked: connection queue */
	TAILQ_HEAD(__wt_connection_impl_qh, __wt_connection_impl) connqh;
};

/*******************************************
 * Implementation of WT_SESSION
 *******************************************/
/*
 * WT_BTREE_SESSION --
 *      Per-session cache of btree handles to avoid synchronization when
 *      opening cursors.
 */
struct __wt_btree_session {
	WT_BTREE *btree;

	TAILQ_ENTRY(__wt_btree_session) q;
};

/*
 * WT_HAZARD --
 *	A hazard reference.
 */
struct __wt_hazard {
	WT_PAGE *page;			/* Page address */
#ifdef HAVE_DIAGNOSTIC
	const char *file;		/* File/line where hazard acquired */
	int	    line;
#endif
};

typedef	enum {
	WT_SERIAL_NONE=0,		/* No request */
	WT_SERIAL_FUNC=1,		/* Function, then return */
	WT_SERIAL_EVICT=2,		/* Function, then schedule evict */
} wq_state_t;

/* Get the connection implementation for a session */
#define	S2C(session) ((WT_CONNECTION_IMPL *)(session)->iface.connection)

/*
 * WT_SESSION_IMPL --
 *	Implementation of WT_SESSION.
 */
struct __wt_session_impl {
	WT_SESSION iface;

	WT_CONDVAR *cond;		/* Condition variable */

	const char *name;		/* Name */

	WT_EVENT_HANDLER *event_handler;/* Application's event handlers */

	WT_BTREE *btree;		/* Current file */
	TAILQ_HEAD(__btrees, __wt_btree_session) btrees;

	WT_CURSOR *cursor;		/* Current cursor */
					/* Cursors closed with the session */
	TAILQ_HEAD(__cursors, __wt_cursor) cursors;

	WT_BTREE *metafile;		/* Metadata file */
	void	*meta_track;		/* Metadata operation tracking */
	void	*meta_track_next;	/* Current position */
	void	*meta_track_sub;	/* Child transaction / save point */
	size_t	 meta_track_alloc;	/* Currently allocated */
#define	WT_META_TRACKING(session)	(session->meta_track_next != NULL)

	TAILQ_HEAD(__tables, __wt_table) tables;

	WT_ITEM	logrec_buf;		/* Buffer for log records */
	WT_ITEM	logprint_buf;		/* Buffer for debug log records */

	WT_ITEM	**scratch;		/* Temporary memory for any function */
	u_int	scratch_alloc;		/* Currently allocated */

					/* Serialized operation state */
	void	*wq_args;		/* Operation arguments */
	int	wq_sleeping;		/* Thread is blocked */
	int	wq_ret;			/* Return value */

	WT_HAZARD *hazard;		/* Hazard reference array */

	void	*reconcile;		/* Reconciliation information */

	WT_REF **excl;			/* Eviction exclusive list */
	u_int	 excl_next;		/* Next empty slot */
	size_t	 excl_allocated;	/* Bytes allocated */

#define	WT_SYNC			1	/* Sync the file */
#define	WT_SYNC_DISCARD		2	/* Sync the file, discard pages */
#define	WT_SYNC_DISCARD_NOWRITE	3	/* Discard the file */
	int syncop;			/* File operation */

	uint32_t flags;
};

/*******************************************
 * Implementation of WT_CONNECTION
 *******************************************/
/*
 * WT_NAMED_COLLATOR --
 *	A collator list entry
 */
struct __wt_named_collator {
	const char *name;		/* Name of collator */
	WT_COLLATOR *collator;	        /* User supplied object */
	TAILQ_ENTRY(__wt_named_collator) q;	/* Linked list of collators */
};

/*
 * WT_NAMED_COMPRESSOR --
 *	A compressor list entry
 */
struct __wt_named_compressor {
	const char *name;		/* Name of compressor */
	WT_COMPRESSOR *compressor;	/* User supplied callbacks */
	TAILQ_ENTRY(__wt_named_compressor) q;	/* Linked list of compressors */
};

/*
 * WT_NAMED_DATA_SOURCE --
 *	A data source list entry
 */
struct __wt_named_data_source {
	const char *prefix;		/* Name of compressor */
	WT_DATA_SOURCE *dsrc;		/* User supplied callbacks */
	TAILQ_ENTRY(__wt_named_data_source) q;	/* Linked list of compressors */
};

/*
 * Allocate some additional slots for internal sessions.  There is a default
 * session for each connection, plus a session for the eviction thread.
 */
#define	WT_NUM_INTERNAL_SESSIONS	2

/*
 * WT_CONNECTION_IMPL --
 *	Implementation of WT_CONNECTION
 */
struct __wt_connection_impl {
	WT_CONNECTION iface;

	/* For operations without an application-supplied session */
	WT_SESSION_IMPL *default_session;
	WT_SESSION_IMPL  dummy_session;

	WT_SPINLOCK fh_lock;		/* File handle queue spinlock */
	WT_SPINLOCK serial_lock;	/* Serial function call spinlock */
	WT_SPINLOCK spinlock;		/* General purpose spinlock */

					/* Connection queue */
	TAILQ_ENTRY(__wt_connection_impl) q;

	const char *home;		/* Database home */
	int is_new;			/* Connection created database */

	WT_FH *lock_fh;			/* Lock file handle */

	pthread_t cache_evict_tid;	/* Cache eviction server thread ID */
	pthread_t cache_read_tid;	/* Cache read server thread ID */

					/* Locked: btree list */
	TAILQ_HEAD(__wt_btree_qh, __wt_btree) btqh;
					/* Locked: file list */
	TAILQ_HEAD(__wt_fh_qh, __wt_fh) fhqh;

					/* Locked: library list */
	TAILQ_HEAD(__wt_dlh_qh, __wt_dlh) dlhqh;

	u_int btqcnt;			/* Locked: btree count */
	u_int next_file_id;		/* Locked: file ID counter */

	/*
	 * WiredTiger allocates space for 50 simultaneous sessions (threads of
	 * control) by default.  Growing the number of threads dynamically is
	 * possible, but tricky since server threads are walking the array
	 * without locking it.
	 *
	 * There's an array of WT_SESSION_IMPL pointers that reference the
	 * allocated array; we do it that way because we want an easy way for
	 * the server thread code to avoid walking the entire array when only a
	 * few threads are running.
	 */
	WT_SESSION_IMPL	**sessions;		/* Session reference */
	void		 *session_array;	/* Session array */
	uint32_t	  session_cnt;		/* Session count */

	/*
	 * WiredTiger allocates space for 15 hazard references in each thread of
	 * control, by default.  There's no code path that requires more than 15
	 * pages at a time (and if we find one, the right change is to increase
	 * the default).
	 *
	 * The hazard array is separate from the WT_SESSION_IMPL array because
	 * we need to easily copy and search it when evicting pages from memory.
	 */
	WT_HAZARD *hazard;		/* Hazard references array */
	uint32_t   hazard_size;
	uint32_t   session_size;

	WT_CACHE  *cache;		/* Page cache */
	uint64_t   cache_size;

	WT_CONNECTION_STATS *stats;	/* Connection statistics */

	WT_FH	   *log_fh;		/* Logging file handle */

					/* Locked: collator list */
	TAILQ_HEAD(__wt_coll_qh, __wt_named_collator) collqh;

					/* Locked: compressor list */
	TAILQ_HEAD(__wt_comp_qh, __wt_named_compressor) compqh;

					/* Locked: data source list */
	TAILQ_HEAD(__wt_dsrc_qh, __wt_named_data_source) dsrcqh;

	FILE *msgfile;
	void (*msgcall)(const WT_CONNECTION_IMPL *, const char *);

	/* If non-zero, all buffers used for I/O will be aligned to this. */
	size_t buffer_alignment;

	uint32_t direct_io;
	uint32_t verbose;

	uint32_t flags;
};

/* Standard entry points to the API: declares/initializes local variables. */
#define	API_CONF_DEFAULTS(h, n, cfg)					\
	{ __wt_confdfl_##h##_##n, (cfg), NULL }

#define	API_SESSION_INIT(s, h, n, cur, bt)				\
	WT_BTREE *__oldbtree = (s)->btree;				\
	const char *__oldname = (s)->name;				\
	(s)->cursor = (cur);						\
	(s)->btree = (bt);						\
	(s)->name = #h "." #n;

#define	API_CALL_NOCONF(s, h, n, cur, bt) do {				\
	API_SESSION_INIT(s, h, n, cur, bt);

#define	API_CALL(s, h, n, cur, bt, cfg, cfgvar) do {			\
	const char *cfgvar[] = API_CONF_DEFAULTS(h, n, cfg);		\
	API_SESSION_INIT(s, h, n, cur, bt);				\
	WT_ERR(((cfg) != NULL) ?					\
	    __wt_config_check((s), __wt_confchk_##h##_##n, (cfg)) : 0)

#define	API_END(s)							\
	if ((s) != NULL) {						\
		(s)->btree = __oldbtree;				\
		(s)->name = __oldname;					\
	}								\
} while (0)

/*
 * If a session or connection method is about to return WT_NOTFOUND (some
 * underlying object was not found), map it to ENOENT, only cursor methods
 * return WT_NOTFOUND.
 */
#define	API_END_NOTFOUND_MAP(s, ret)					\
	API_END(s);							\
	return ((ret) == WT_NOTFOUND ? ENOENT : (ret))

#define	SESSION_API_CALL(s, n, cfg, cfgvar)				\
	API_CALL(s, session, n, NULL, NULL, cfg, cfgvar);

#define	CONNECTION_API_CALL(conn, s, n, cfg, cfgvar)			\
	s = conn->default_session;					\
	API_CALL(s, connection, n, NULL, NULL, cfg, cfgvar);		\

#define	CURSOR_API_CALL(cur, s, n, bt)					\
	(s) = (WT_SESSION_IMPL *)(cur)->session;			\
	API_CALL_NOCONF(s, cursor, n, (cur), bt);			\

/*******************************************
 * Global variables.
 *******************************************/
extern WT_EVENT_HANDLER *__wt_event_handler_default;
extern WT_EVENT_HANDLER *__wt_event_handler_verbose;
extern WT_PROCESS __wt_process;

/*
 * DO NOT EDIT: automatically built by dist/api_flags.py.
 * API flags section: BEGIN
 */
#define	WT_CONN_NOSYNC					0x00000002
#define	WT_DIRECTIO_DATA				0x00000002
#define	WT_DIRECTIO_LOG					0x00000001
#define	WT_PAGE_FREE_IGNORE_DISK			0x00000001
#define	WT_REC_SINGLE					0x00000001
#define	WT_SERVER_RUN					0x00000001
#define	WT_SESSION_INTERNAL				0x00000002
#define	WT_SESSION_SALVAGE_QUIET_ERR			0x00000001
#define	WT_VERB_block					0x00001000
#define	WT_VERB_evict					0x00000800
#define	WT_VERB_evictserver				0x00000400
#define	WT_VERB_fileops					0x00000200
#define	WT_VERB_hazard					0x00000100
#define	WT_VERB_mutex					0x00000080
#define	WT_VERB_read					0x00000040
#define	WT_VERB_readserver				0x00000020
#define	WT_VERB_reconcile				0x00000010
#define	WT_VERB_salvage					0x00000008
#define	WT_VERB_snapshot				0x00000004
#define	WT_VERB_verify					0x00000002
#define	WT_VERB_write					0x00000001
/*
 * API flags section: END
 * DO NOT EDIT: automatically built by dist/api_flags.py.
 */