make_db_h.c 36.5 KB
Newer Older
1
/* -*- mode: C; c-basic-offset: 4 -*- */
2
#ident "Copyright (c) 2007, 2008 Tokutek Inc.  All rights reserved."
3 4
/* LICENSE:  This file is licensed under the GPL or from Tokutek. */

5 6 7 8 9
/* Make a db.h that will be link-time compatible with Sleepycat's Berkeley DB. */

#include <db.h>
#include <stdio.h>
#include <stdlib.h>
10 11
#include <assert.h>
#include <string.h>
12
#include <stdint.h>
13

14
#define VISIBLE "__attribute__((__visibility__(\"default\")))"
15

16
static void print_dbtype(void) {
17 18
    /* DBTYPE is mentioned by db_open.html */
    printf("typedef enum {\n");
Yoni Fogel's avatar
Yoni Fogel committed
19 20
    printf(" DB_BTREE=%d,\n", DB_BTREE);
    printf(" DB_UNKNOWN=%d\n", DB_UNKNOWN);
21 22
    printf("} DBTYPE;\n");
}
Bradley C. Kuszmaul's avatar
Fixup  
Bradley C. Kuszmaul committed
23
#if 0
24 25 26 27 28
void print_db_notices (void) {
    printf("typedef enum { /* This appears to be a mysql-specific addition to the api. */ \n");
    printf(" DB_NOTICE_LOGFILE_CHANGED=%d\n", DB_NOTICE_LOGFILE_CHANGED);
    printf("} db_notices;\n");
}
Bradley C. Kuszmaul's avatar
Fixup  
Bradley C. Kuszmaul committed
29
#endif
30 31

#define dodefine(name) printf("#define %s %d\n", #name, name)
32 33 34 35 36 37 38 39 40 41 42 43 44
#define dodefine_track(flags, name) do {assert((flags & name) != name); \
                                        flags |= (name);                \
                                        printf("#define %s %d\n", #name, name);} while (0)
#define dodefine_from_track(flags, name) do {   \
    uint32_t which;                             \
    uint32_t bit;                               \
    for (which = 0; which < 32; which++) {      \
        bit = 1U << which;                      \
        if (!(flags & bit)) break;              \
    }                                           \
    assert(which < 32);                         \
    printf("#define %s %d\n", #name, bit);      \
} while (0)
45

Yoni Fogel's avatar
Yoni Fogel committed
46 47
#define dodefine_track_enum(flags, name) do {assert(name>=0 && name<256); \
                                             assert(!(flags[name])); \
48
                                             flags[name] = 1;        \
Yoni Fogel's avatar
Yoni Fogel committed
49
                                             printf("#define %s %d\n", #name, name);} while (0)
50 51 52 53 54 55 56 57 58 59 60 61
#define dodefine_from_track_enum(flags, name) do {   \
    uint32_t which;                             \
    /* don't use 0 */                           \
    for (which = 1; which < 256; which++) {     \
        if (!(flags[which])) break;             \
    }                                           \
    assert(which < 256);                        \
    flags[which] = 1;                           \
    printf("#define %s %d\n", #name, which);    \
} while (0)


Zardosht Kasheff's avatar
Zardosht Kasheff committed
62
enum {
63 64 65 66 67 68
	TOKUDB_OUT_OF_LOCKS         = -100000,
        TOKUDB_SUCCEEDED_EARLY      = -100001,
        TOKUDB_FOUND_BUT_REJECTED   = -100002,
        TOKUDB_USER_CALLBACK_ERROR  = -100003,
        TOKUDB_DICTIONARY_TOO_OLD   = -100004,
        TOKUDB_DICTIONARY_TOO_NEW   = -100005,
69 70
        TOKUDB_DICTIONARY_NO_HEADER = -100006,
        TOKUDB_CANCELED             = -100007,
Zardosht Kasheff's avatar
Zardosht Kasheff committed
71 72
};

73
static void print_defines (void) {
74
    printf("#ifndef _TOKUDB_WRAP_H\n");
75 76 77 78 79 80 81 82 83
    dodefine(DB_VERB_DEADLOCK);
    dodefine(DB_VERB_RECOVERY);
    dodefine(DB_VERB_REPLICATION);
    dodefine(DB_VERB_WAITSFOR);

    dodefine(DB_ARCH_ABS);
    dodefine(DB_ARCH_LOG);

    dodefine(DB_CREATE);
84
    dodefine(DB_CXX_NO_EXCEPTIONS);
85
    dodefine(DB_EXCL);
86 87 88
    dodefine(DB_PRIVATE);
    dodefine(DB_RDONLY);
    dodefine(DB_RECOVER);
89
    dodefine(DB_RUNRECOVERY);
90 91 92 93 94 95 96 97
    dodefine(DB_THREAD);
    dodefine(DB_TXN_NOSYNC);

    dodefine(DB_LOCK_DEFAULT);
    dodefine(DB_LOCK_OLDEST);
    dodefine(DB_LOCK_RANDOM);

    dodefine(DB_DUP);
Bradley C. Kuszmaul's avatar
up  
Bradley C. Kuszmaul committed
98
    dodefine(DB_DUPSORT);
99

100 101
    dodefine(DB_KEYFIRST);
    dodefine(DB_KEYLAST);
102 103 104 105 106 107 108
    {
        static uint8_t insert_flags[256];
        dodefine_track_enum(insert_flags, DB_NOOVERWRITE);
        dodefine_track_enum(insert_flags, DB_NODUPDATA);
        dodefine_from_track_enum(insert_flags, DB_YESOVERWRITE);
        dodefine_from_track_enum(insert_flags, DB_NOOVERWRITE_NO_ERROR);
    }
109
    dodefine(DB_OPFLAGS_MASK);
110

Yoni Fogel's avatar
Yoni Fogel committed
111 112
    dodefine(DB_AUTO_COMMIT);

113 114 115
    dodefine(DB_INIT_LOCK);
    dodefine(DB_INIT_LOG);
    dodefine(DB_INIT_MPOOL);
116
    printf("#define DB_CLOSE_DONT_TRIM_LOG 1048576\n"); // tokudb
117
    dodefine(DB_INIT_TXN);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
118

119
    //dodefine(DB_KEYEMPTY);      /// KEYEMPTY is no longer used.  We just use DB_NOTFOUND
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
120 121
    dodefine(DB_KEYEXIST);
    dodefine(DB_LOCK_DEADLOCK);
Yoni Fogel's avatar
Yoni Fogel committed
122
    dodefine(DB_LOCK_NOTGRANTED);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
123
    dodefine(DB_NOTFOUND);
Yoni Fogel's avatar
{{{  
Yoni Fogel committed
124
    dodefine(DB_SECONDARY_BAD);
125
    dodefine(DB_DONOTINDEX);
126
#ifdef DB_BUFFER_SMALL
127
    dodefine(DB_BUFFER_SMALL);
128
#endif
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
129
    printf("#define DB_BADFORMAT -30500\n"); // private tokudb
130
    printf("#define DB_DELETE_ANY %d\n", 1<<16); // private tokudb
131
    printf("#define DB_TRUNCATE_WITHCURSORS %d\n", 1<<17); // private tokudb
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
132 133 134

    dodefine(DB_FIRST);
    dodefine(DB_GET_BOTH);
135
    dodefine(DB_GET_BOTH_RANGE);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
136
    dodefine(DB_LAST);
Yoni Fogel's avatar
{{{  
Yoni Fogel committed
137
    dodefine(DB_CURRENT);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
138 139
    dodefine(DB_NEXT);
    dodefine(DB_NEXT_DUP);
140
    dodefine(DB_NEXT_NODUP);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
141
    dodefine(DB_PREV);
142
#if defined(DB_PREV_DUP)
143 144 145
    dodefine(DB_PREV_DUP);
#endif
    dodefine(DB_PREV_NODUP);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
146 147
    dodefine(DB_SET);
    dodefine(DB_SET_RANGE);
148
    printf("#define DB_CURRENT_BINDING 253\n"); // private tokudb
149 150
    printf("#define DB_SET_RANGE_REVERSE 252\n"); // private tokudb
    printf("#define DB_GET_BOTH_RANGE_REVERSE 251\n"); // private tokudb
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
151
    dodefine(DB_RMW);
152
    printf("#define DB_PRELOCKED 0x00800000\n"); // private tokudb
Yoni Fogel's avatar
Yoni Fogel committed
153
    printf("#define DB_PRELOCKED_WRITE 0x00400000\n"); // private tokudb
154

155 156 157 158 159 160
    {
        //dbt flags
        uint32_t dbt_flags = 0;
        dodefine_track(dbt_flags, DB_DBT_APPMALLOC);
        dodefine_track(dbt_flags, DB_DBT_DUPOK);
        dodefine_track(dbt_flags, DB_DBT_MALLOC);
161
#ifdef DB_DBT_MULTIPLE
162
        dodefine_track(dbt_flags, DB_DBT_MULTIPLE);
163
#endif
164 165 166
        dodefine_track(dbt_flags, DB_DBT_REALLOC);
        dodefine_track(dbt_flags, DB_DBT_USERMEM);
    }
167 168 169 170 171 172

    // flags for the env->set_flags function
#if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3
    dodefine(DB_LOG_AUTOREMOVE);
#endif

173 174 175 176 177 178 179 180
    {
    //Txn begin/commit flags
        uint32_t txn_flags = 0;
        dodefine_track(txn_flags, DB_TXN_WRITE_NOSYNC);
        dodefine_track(txn_flags, DB_TXN_NOWAIT);
        dodefine_track(txn_flags, DB_TXN_SYNC);
#ifdef DB_READ_UNCOMMITTED
        dodefine_track(txn_flags, DB_READ_UNCOMMITTED);
181 182 183
#endif
#ifdef DB_READ_COMMITTED
        dodefine_track(txn_flags, DB_READ_COMMITTED);
184 185 186
#endif
        dodefine_from_track(txn_flags, DB_INHERIT_ISOLATION);
    }
Zardosht Kasheff's avatar
Zardosht Kasheff committed
187
    
188
    printf("#endif\n");
Zardosht Kasheff's avatar
Zardosht Kasheff committed
189 190 191 192
    
    /* TOKUDB specific error codes*/
    printf("/* TOKUDB specific error codes */\n");
    dodefine(TOKUDB_OUT_OF_LOCKS);
Yoni Fogel's avatar
Yoni Fogel committed
193
    dodefine(TOKUDB_SUCCEEDED_EARLY);
194 195
    dodefine(TOKUDB_FOUND_BUT_REJECTED);
    dodefine(TOKUDB_USER_CALLBACK_ERROR);
196
    dodefine(TOKUDB_DICTIONARY_TOO_OLD);
197 198
    dodefine(TOKUDB_DICTIONARY_TOO_NEW);
    dodefine(TOKUDB_DICTIONARY_NO_HEADER);
199
    dodefine(TOKUDB_CANCELED);
200 201 202 203

    /* LOADER flags */
    printf("/* LOADER flags */\n");
    printf("#define LOADER_USE_PUTS 1\n"); // minimize space usage
204
}
205

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
206
//#define DECL_LIMIT 100
207
struct fieldinfo {
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
208
    char *decl;
209 210
    unsigned int off;
    unsigned int size;
211
};
212

213 214 215 216 217 218
#if USE_MAJOR==4 && USE_MINOR==1
#include "sample_offsets_32_4_1.h"
#include "sample_offsets_64_4_1.h"
#elif USE_MAJOR==4 && USE_MINOR==3
#include "sample_offsets_32_4_3.h"
#include "sample_offsets_64_4_3.h"
Rich Prohaska's avatar
Rich Prohaska committed
219 220 221
#elif USE_MAJOR==4 && USE_MINOR==4
#include "sample_offsets_32_4_4.h"
#include "sample_offsets_64_4_4.h"
Rich Prohaska's avatar
Rich Prohaska committed
222 223 224
#elif USE_MAJOR==4 && USE_MINOR==5
#include "sample_offsets_32_4_5.h"
#include "sample_offsets_64_4_5.h"
Rich Prohaska's avatar
Rich Prohaska committed
225 226 227
#elif USE_MAJOR==4 && USE_MINOR==6
#include "sample_offsets_32_4_6.h"
#include "sample_offsets_64_4_6.h"
228 229 230
#else
#error
#endif
231

232 233
enum need_internal_type { NO_INTERNAL=0, INTERNAL_NAMED=1, INTERNAL_AT_END=2};

234
static void print_struct (const char *structname, enum need_internal_type need_internal, struct fieldinfo *fields32, struct fieldinfo *fields64, unsigned int N, const char *extra_decls[]) {
235 236 237
    unsigned int i;
    unsigned int current_32 = 0;
    unsigned int current_64 = 0;
238
    int dummy_counter=0;
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
239
    int did_toku_internal=0;
Bradley C. Kuszmaul's avatar
txn  
Bradley C. Kuszmaul committed
240 241
//    int total32 = fields32[N-1].size;
//    int total64 = fields32[N-1].size;
242
    assert(need_internal==NO_INTERNAL || need_internal==INTERNAL_NAMED || need_internal==INTERNAL_AT_END);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
243
    printf("struct __toku_%s {\n", structname);
244
    for (i=0; i<N-1; i++) {
245 246
	unsigned int this_32 = fields32[i].off;
	unsigned int this_64 = fields64[i].off;
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
247
	//fprintf(stderr, "this32=%d current32=%d this64=%d current64=%d\n", this_32, current_32, this_64, current_64);
248 249 250 251 252 253
	if (this_32 > current_32 || this_64 > current_64) {
	    unsigned int diff32 = this_32-current_32;
	    unsigned int diff64 = this_64-current_64;
	    assert(this_32 > current_32 && this_64 > current_64);
	    if (diff32!=diff64) {
		unsigned int diff = diff64-diff32;
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
254
		unsigned int n_dummys = diff/4;
255
		if (need_internal==INTERNAL_NAMED && !did_toku_internal) {
256 257 258 259 260 261 262 263 264
		    if (TDB_NATIVE &&
			(strcmp(structname, "dbc")==0 ||
			 strcmp(structname, "db_txn")==0)) {
			printf("  struct __toku_%s_internal ii;\n", structname);
			printf("#define %s_struct_i(x) (&(x)->ii)\n", structname);
		    } else {
			printf("  struct __toku_%s_internal *i;\n", structname);
			printf("#define %s_struct_i(x) ((x)->i)\n", structname);
		    }
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
265 266 267
		    n_dummys--;
		    did_toku_internal=1;
		}
268 269 270 271 272
		while (n_dummys>0 && extra_decls && *extra_decls) {
		    printf("  %s;\n", *extra_decls);
		    extra_decls++;
		    n_dummys--;
		}
273
		if (n_dummys>0) {
274 275 276
		    if (!TDB_NATIVE)
			printf("  void* __toku_dummy%d[%d];\n", dummy_counter, n_dummys);
		    dummy_counter++;
277
		}
278 279 280 281 282 283
		diff64-=diff*2;
		diff32-=diff;
		
	    }
	    assert(diff32==diff64);
	    if (diff32>0) {
284 285 286
		if (!TDB_NATIVE)
		    printf("  char __toku_dummy%d[%d];\n", dummy_counter, diff32);
		dummy_counter++;
287 288 289
	    }
	    current_32 = this_32;
	    current_64 = this_64;
290
	}
291
	if (this_32<current_32 || this_64<current_64) {
Bradley C. Kuszmaul's avatar
32  
Bradley C. Kuszmaul committed
292 293 294 295
	    printf("Whoops this_32=%d this_64=%d\n", this_32, this_64);
	}
	if (i+1<N) {
	    assert(strcmp(fields32[i].decl, fields64[i].decl)==0);
296 297 298 299
	    printf("  %s;", fields32[i].decl);
	    if (!TDB_NATIVE)
		printf(" /* 32-bit offset=%d size=%d, 64=bit offset=%d size=%d */", fields32[i].off, fields32[i].size, fields64[i].off, fields64[i].size);
	    printf("\n");
Bradley C. Kuszmaul's avatar
32  
Bradley C. Kuszmaul committed
300 301 302
	} else {
	    assert(fields32[i].decl==0);
	    assert(fields64[i].decl==0);
303
	}
304 305
	current_32 += fields32[i].size;
	current_64 += fields64[i].size;
306
    }
307
    if (extra_decls) assert(NULL==*extra_decls); // make sure that the extra decls all got used up.
308 309 310 311 312 313 314
    {
	unsigned int this_32 = fields32[N-1].off;
	unsigned int this_64 = fields64[N-1].off;
	unsigned int diff32  = this_32-current_32;
	unsigned int diff64  = this_64-current_64;
	if (diff32>0 && diff32<diff64) {
	    unsigned int diff = diff64-diff32;
315 316 317
	    if (!TDB_NATIVE)
		printf("  void* __toku_dummy%d[%d]; /* Padding at the end */ \n", dummy_counter, diff/4);
	    dummy_counter++;
318 319 320 321
	    diff64-=diff*2;
	    diff32-=diff;
	}
	if (diff32>0) {
322 323 324
	    if (!TDB_NATIVE)
		printf("  char __toku_dummy%d[%d];  /* Padding at the end */ \n", dummy_counter, diff32);
	    dummy_counter++;
325 326 327
	    diff64-=diff32;
	    diff32=0;
	}
328 329 330
	if (diff64>0)
	    if (!TDB_NATIVE)
		printf("  /* %d more bytes of alignment in the 64-bit case. */\n", diff64);
331 332
	assert(diff64<8); /* there could be a few left from alignment. */ 
    }
333 334 335 336 337
    if (need_internal==INTERNAL_AT_END) {
	printf("  char iic[0] __attribute__((aligned(__BIGGEST_ALIGNMENT__)));\n");
	printf("#define %s_struct_i(x) ((struct __toku_%s_internal *)(&(x)->iic))\n", structname, structname);
	did_toku_internal = 1;
    }
338
    printf("};\n");
339
    assert(did_toku_internal || !need_internal);
340
}
341

342
int main (int argc __attribute__((__unused__)), char *const argv[] __attribute__((__unused__))) {
343 344
    printf("#ifndef _DB_H\n");
    printf("#define _DB_H\n");
345 346
    printf("/* This code generated by make_db_h.   Copyright (c) 2007, 2008 Tokutek */\n");
    printf("#ident \"Copyright (c) 2007, 2008 Tokutek Inc.  All rights reserved.\"\n");
347
    printf("#include <sys/types.h>\n");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
348 349
    printf("/*stdio is needed for the FILE* in db->verify*/\n");
    printf("#include <stdio.h>\n");
350
    printf("#include <stdint.h>\n");
351
    //printf("#include <inttypes.h>\n");
352
    printf("#if defined(__cplusplus)\nextern \"C\" {\n#endif\n");
353

354 355
    assert(DB_VERSION_MAJOR==DB_VERSION_MAJOR_32);
    assert(DB_VERSION_MINOR==DB_VERSION_MINOR_32);
356
    printf("#define TOKUDB 1\n");
357
    printf("#define TOKUDB_NATIVE_H %d\n", TDB_NATIVE);
358 359 360
    dodefine(DB_VERSION_MAJOR);
    dodefine(DB_VERSION_MINOR);
    dodefine(DB_VERSION_PATCH);
361
    printf("#ifndef _TOKUDB_WRAP_H\n");
362
    printf("#define DB_VERSION_STRING \"Tokutek: TokuDB %d.%d.%d\"\n", DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH);
363
    printf("#else\n");
364
    printf("#define DB_VERSION_STRING_ydb \"Tokutek: TokuDB (wrapped bdb)\"\n");
365
    printf("#endif\n");
366

367 368 369 370 371 372 373
    if (0) {
	printf("#ifndef __BIT_TYPES_DEFINED__\n");
	printf("/* Define some int types if not provided by the system.  BIND does this, so we do it too. */\n");
	printf("typedef unsigned int u_int32_t;\n");
	printf("#endif\n");
    }

374 375 376 377 378 379
    //Typedef toku_off_t
    printf("#ifndef TOKU_OFF_T_DEFINED\n"
           "#define TOKU_OFF_T_DEFINED\n"
           "typedef int64_t toku_off_t;\n"
           "#endif\n");

380
    //printf("typedef struct __toku_db_btree_stat DB_BTREE_STAT;\n");
381
    printf("typedef struct __toku_db_env DB_ENV;\n");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
382 383
    printf("typedef struct __toku_db_key_range DB_KEY_RANGE;\n");
    printf("typedef struct __toku_db_lsn DB_LSN;\n");
384
    printf("typedef struct __toku_db DB;\n");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
385
    printf("typedef struct __toku_db_txn DB_TXN;\n");
386
    printf("typedef struct __toku_db_txn_active DB_TXN_ACTIVE;\n");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
387 388 389
    printf("typedef struct __toku_db_txn_stat DB_TXN_STAT;\n");
    printf("typedef struct __toku_dbc DBC;\n");
    printf("typedef struct __toku_dbt DBT;\n");
390
    printf("typedef u_int32_t db_recno_t;\n");
391 392 393
    printf("typedef int(*YDB_CALLBACK_FUNCTION)(DBT const*, DBT const*, void*);\n");
    printf("typedef int(*YDB_HEAVISIDE_CALLBACK_FUNCTION)(DBT const *key, DBT const *value, void *extra_f, int r_h);\n");
    printf("typedef int(*YDB_HEAVISIDE_FUNCTION)(const DBT *key, const DBT *value, void *extra_h);\n");
394

395 396
    printf("#include <tdb-internal.h>\n");
    
397 398
    printf("#ifndef __BIGGEST_ALIGNMENT__\n  #define __BIGGEST_ALIGNMENT__ 16\n#endif\n");

399
    //stat64
400 401 402 403 404 405
    printf("typedef struct __toku_db_btree_stat64 {\n");
    printf("  u_int64_t bt_nkeys; /* how many unique keys (guaranteed only to be an estimate, even when flattened)          */\n");
    printf("  u_int64_t bt_ndata; /* how many key-value pairs (an estimate, but exact when flattened)                       */\n");
    printf("  u_int64_t bt_dsize; /* how big are the keys+values (not counting the lengths) (an estimate, unless flattened) */\n");
    printf("  u_int64_t bt_fsize; /* how big is the underlying file                                                         */\n");
    printf("} DB_BTREE_STAT64;\n");
406

407 408 409 410 411
    //bulk loader
    printf("typedef struct __toku_loader DB_LOADER;\n");
    printf("struct __toku_loader_internal;\n");
    printf("struct __toku_loader {\n");
    printf("  struct __toku_loader_internal *i;\n");
412
    printf("  int (*set_error_callback)(DB_LOADER *loader, void (*error_cb)(DB *db, int i, int err, DBT *key, DBT *val, void *error_extra), void *error_extra); /* set the error callback */\n");
413
    printf("  int (*set_poll_function)(DB_LOADER *loader, int (*poll_func)(void *extra, float progress), void *poll_extra);             /* set the polling function */\n");
414 415 416
    printf("  int (*put)(DB_LOADER *loader, DBT *key, DBT* val);                                                      /* give a row to the loader */\n");
    printf("  int (*close)(DB_LOADER *loader);                                                                        /* finish loading, free memory */\n");
    printf("  int (*abort)(DB_LOADER *loader);                                                                        /* abort loading, free memory */\n");
417 418
    printf("};\n");

419 420
    //engine status info
    printf("typedef struct __toku_engine_status {\n");
421
    printf("  char             now[26];                 /* time of engine status query (i.e. now)  */ \n");
422 423 424 425 426 427 428 429 430 431 432 433 434
    printf("  u_int64_t        ydb_lock_ctr;            /* how many times has ydb lock been taken/released */ \n");
    printf("  u_int64_t        max_possible_sleep;      /* max possible sleep time for ydb lock scheduling (constant) */ \n");
    printf("  u_int64_t        processor_freq_mhz;      /* clock frequency in MHz */ \n");
    printf("  u_int64_t        max_requested_sleep;     /* max sleep time requested, can be larger than max possible */ \n");
    printf("  u_int64_t        times_max_sleep_used;    /* number of times the max_possible_sleep was used to sleep */ \n");
    printf("  u_int64_t        total_sleepers;          /* total number of times a client slept for ydb lock scheduling */ \n");
    printf("  u_int64_t        total_sleep_time;        /* total time spent sleeping for ydb lock scheduling */ \n");
    printf("  u_int64_t        max_waiters;             /* max number of simultaneous client threads kept waiting for ydb lock  */ \n");
    printf("  u_int64_t        total_waiters;           /* total number of times a client thread waited for ydb lock  */ \n");
    printf("  u_int64_t        total_clients;           /* total number of separate client threads that use ydb lock  */ \n");
    printf("  u_int64_t        time_ydb_lock_held_unavailable;  /* number of times a thread migrated and theld is unavailable */ \n");
    printf("  u_int64_t        max_time_ydb_lock_held;  /* max time a client thread held the ydb lock  */ \n");
    printf("  u_int64_t        total_time_ydb_lock_held;/* total time client threads held the ydb lock  */ \n");
435
    printf("  u_int32_t        checkpoint_period;       /* delay between automatic checkpoints  */ \n");
436
    printf("  u_int32_t        checkpoint_footprint;    /* state of checkpoint procedure        */ \n");
437 438 439
    printf("  char             checkpoint_time_begin[26]; /* time of last checkpoint begin      */ \n");
    printf("  char             checkpoint_time_begin_complete[26]; /* time of last complete checkpoint begin      */ \n");
    printf("  char             checkpoint_time_end[26]; /* time of last checkpoint end      */ \n");
440 441
    printf("  u_int64_t        cachetable_lock_taken;   /* how many times has cachetable lock been taken */ \n");
    printf("  u_int64_t        cachetable_lock_released;/* how many times has cachetable lock been released */ \n");
442 443
    printf("  u_int64_t        cachetable_hit;          /* how many cache hits   */ \n");
    printf("  u_int64_t        cachetable_miss;         /* how many cache misses */ \n");
444 445
    printf("  u_int64_t        cachetable_misstime;     /* how many usec spent waiting for disk read because of cache miss */ \n");
    printf("  u_int64_t        cachetable_waittime;     /* how many usec spent waiting for another thread to release cache line */ \n");
446 447
    printf("  u_int64_t        cachetable_wait_reading; /* how many times get_and_pin waits for a node to be read */ \n");
    printf("  u_int64_t        cachetable_wait_writing; /* how many times get_and_pin waits for a node to be written */ \n");
448
    printf("  u_int64_t        cachetable_wait_checkpoint; /* how many times get_and_pin waits for a node to be written for a checkpoint*/ \n");
449 450
    printf("  u_int64_t        puts;                    /* how many times has a newly created node been put into the cachetable */ \n");
    printf("  u_int64_t        prefetches;              /* how many times has a block been prefetched into the cachetable */ \n");
451 452
    printf("  u_int64_t        maybe_get_and_pins;      /* how many times has maybe_get_and_pin(_clean) been called */ \n");
    printf("  u_int64_t        maybe_get_and_pin_hits;  /* how many times has get_and_pin(_clean) returned with a node */ \n");
453 454 455
    printf("  int64_t          cachetable_size_current; /* sum of the sizes of the nodes represented in the cachetable */ \n");
    printf("  int64_t          cachetable_size_limit;   /* the limit to the sum of the node sizes */ \n");
    printf("  int64_t          cachetable_size_writing; /* the sum of the sizes of the nodes being written */ \n");
456
    printf("  int64_t          get_and_pin_footprint;   /* state of get_and_pin procedure */ \n");
457
    printf("  u_int32_t        range_locks_max;         /* max total number of range locks */ \n");
458
    printf("  u_int32_t        range_locks_max_per_index;  /* max range locks per dictionary */ \n");
459
    printf("  u_int32_t        range_locks_curr;        /* total range locks currently in use */ \n");
460 461
    printf("  u_int32_t        range_lock_escalation_successes;       /* number of times range locks escalation succeeded */ \n");
    printf("  u_int32_t        range_lock_escalation_failures;        /* number of times range locks escalation failed */ \n");
462 463 464 465 466 467
    printf("  u_int64_t        inserts;                 /* ydb row insert operations            */ \n");
    printf("  u_int64_t        deletes;                 /* ydb row delete operations            */ \n");
    printf("  u_int64_t        commits;                 /* ydb txn commit operations            */ \n");
    printf("  u_int64_t        aborts;                  /* ydb txn abort operations             */ \n");
    printf("  u_int64_t        point_queries;           /* ydb point queries                    */ \n");
    printf("  u_int64_t        sequential_queries;      /* ydb sequential queries               */ \n");
468 469
    printf("  u_int64_t        fsync_count;             /* number of times fsync performed      */ \n");
    printf("  u_int64_t        fsync_time;              /* total time required to fsync         */ \n");
470 471 472 473 474 475
    printf("  u_int64_t        logger_ilock_ctr;        /* how many times has logger input lock been taken or released  */ \n");
    printf("  u_int64_t        logger_olock_ctr;        /* how many times has logger output condition lock been taken or released  */ \n");
    printf("  u_int64_t        logger_swap_ctr;         /* how many times have logger buffers been swapped  */ \n");
    printf("  char             enospc_most_recent[26];  /* time of most recent ENOSPC error return from disk write  */ \n");
    printf("  u_int64_t        enospc_threads_blocked;  /* how many threads are currently blocked by ENOSPC */ \n");
    printf("  u_int64_t        enospc_total;            /* how many times has ENOSPC been returned by disk write */ \n");
476 477
    printf("  u_int64_t        enospc_seal_ctr;         /* how many times has ENOSPC been returned to user (red zone) */ \n");
    printf("  u_int64_t        enospc_seal_state;       /* state of ydb-level seal (0 = green, 1 = yellow, 2 = red) */ \n");
478 479
    printf("} ENGINE_STATUS;\n");

480
    print_dbtype();
Bradley C. Kuszmaul's avatar
Fixup  
Bradley C. Kuszmaul committed
481
//    print_db_notices();
482
    print_defines();
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
483

484 485
    printf("/* in wrap mode, top-level function txn_begin is renamed, but the field isn't renamed, so we have to hack it here.*/\n");
    printf("#ifdef _TOKUDB_WRAP_H\n#undef txn_begin\n#endif\n");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
486
    assert(sizeof(db_btree_stat_fields32)==sizeof(db_btree_stat_fields64));
487 488
    // Don't produce db_btree_stat records.
    //print_struct("db_btree_stat", 0, db_btree_stat_fields32, db_btree_stat_fields64, sizeof(db_btree_stat_fields32)/sizeof(db_btree_stat_fields32[0]), 0);
Bradley C. Kuszmaul's avatar
32  
Bradley C. Kuszmaul committed
489
    assert(sizeof(db_env_fields32)==sizeof(db_env_fields64));
490 491
    {
	const char *extra[]={
492 493
                             "int (*checkpointing_set_period)             (DB_ENV*, u_int32_t) /* Change the delay between automatic checkpoints.  0 means disabled. */",
                             "int (*checkpointing_get_period)             (DB_ENV*, u_int32_t*) /* Retrieve the delay between automatic checkpoints.  0 means disabled. */",
494 495 496 497 498 499
                             "int (*checkpointing_postpone)               (DB_ENV*) /* Use for 'rename table' or any other operation that must be disjoint from a checkpoint */",
                             "int (*checkpointing_resume)                 (DB_ENV*) /* Alert tokudb 'postpone' is no longer necessary */",
                             "int (*checkpointing_begin_atomic_operation) (DB_ENV*) /* Begin a set of operations (that must be atomic as far as checkpoints are concerned). i.e. inserting into every index in one table */",
                             "int (*checkpointing_end_atomic_operation)   (DB_ENV*) /* End   a set of operations (that must be atomic as far as checkpoints are concerned). */",
                             "int (*set_default_bt_compare)  (DB_ENV*,int (*bt_compare) (DB *, const DBT *, const DBT *)) /* Set default (key) comparison function for all DBs in this environment.  Required for RECOVERY since you cannot open the DBs manually. */",
                             "int (*set_default_dup_compare) (DB_ENV*,int (*bt_compare) (DB *, const DBT *, const DBT *)) /* Set default (val) comparison function for all DBs in this environment.  Required for RECOVERY since you cannot open the DBs manually. */",
500
			     "int (*get_engine_status)                    (DB_ENV*, ENGINE_STATUS*) /* Fill in status struct */",
501
			     "int (*get_engine_status_text)               (DB_ENV*, char*, int)     /* Fill in status text */",
502
			     "int (*get_iname)                            (DB_ENV* env, DBT* dname_dbt, DBT* iname_dbt) /* FOR TEST ONLY: lookup existing iname */",
503
                             "int (*create_loader)                        (DB_ENV *env, DB_TXN *txn, DB_LOADER **blp, DB *src_db, int N, DB *dbs[/*N*/], uint32_t db_flags[/*N*/], uint32_t dbt_flags[/*N*/], uint32_t loader_flags)",
504 505 506 507 508 509 510 511
                             "int (*put_multiple)                         (DB_ENV *env, DB *src_db, DB_TXN *txn,\n"
                             "                                             const DBT *key, const DBT *val,\n"
                             "                                             uint32_t num_dbs, DB **db_array, DBT *keys, DBT *vals, uint32_t *flags_array,\n"
                             "                                             void *extra) /* Insert into multiple dbs */",
                             "int (*set_generate_row_callback_for_put)    (DB_ENV *env, \n"
                             "                                             int (*generate_row_for_put)(DB *dest_db, DB *src_db,\n"
                             "                                                                         DBT *dest_key, DBT *dest_val,\n"
                             "                                                                         const DBT *src_key, const DBT *src_val,\n"
512
                             "                                                                         void *extra))",
513 514 515 516 517 518 519 520
                             "int (*del_multiple)                         (DB_ENV *env, DB *src_db, DB_TXN *txn,\n"
                             "                                             const DBT *key, const DBT *val,\n"
                             "                                             uint32_t num_dbs, DB **db_array, DBT *keys, uint32_t *flags_array,\n"
                             "                                             void *extra) /* Insert into multiple dbs */",
                             "int (*set_generate_row_callback_for_del)    (DB_ENV *env, \n"
                             "                                             int (*generate_row_for_del)(DB *dest_db, DB *src_db,\n"
                             "                                                                         DBT *dest_key,\n"
                             "                                                                         const DBT *src_key, const DBT *src_val,\n"
521
                             "                                                                         void *extra))",
522 523
                             "int (*get_redzone)                          (DB_ENV *env, int *redzone) /* get the redzone limit */",
                             "int (*set_redzone)                          (DB_ENV *env, int redzone) /* set the redzone limit in percent of total space */",
524 525 526
			     NULL};
        print_struct("db_env", 1, db_env_fields32, db_env_fields64, sizeof(db_env_fields32)/sizeof(db_env_fields32[0]), extra);
    }
Bradley C. Kuszmaul's avatar
32  
Bradley C. Kuszmaul committed
527

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
528
    assert(sizeof(db_key_range_fields32)==sizeof(db_key_range_fields64));
529
    print_struct("db_key_range", 0, db_key_range_fields32, db_key_range_fields64, sizeof(db_key_range_fields32)/sizeof(db_key_range_fields32[0]), 0);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
530 531

    assert(sizeof(db_lsn_fields32)==sizeof(db_lsn_fields64));
532 533 534 535
    {
	//const char *extra[] = {"u_int64_t lsn", NULL};
	print_struct("db_lsn", 0, db_lsn_fields32, db_lsn_fields64, sizeof(db_lsn_fields32)/sizeof(db_lsn_fields32[0]), 0);
    }
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
536

537 538 539
    assert(sizeof(dbt_fields32)==sizeof(dbt_fields64));
    print_struct("dbt", 0, dbt_fields32, dbt_fields64, sizeof(dbt_fields32)/sizeof(dbt_fields32[0]), 0);

540 541 542 543
    printf("typedef int (*toku_dbt_upgradef)(DB*,\n");
    printf("                                 u_int32_t old_version, const DBT *old_descriptor, const DBT *old_key, const DBT *old_val,\n");
    printf("                                 u_int32_t new_version, const DBT *new_descriptor, const DBT *new_key, const DBT *new_val);\n");

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
544
    assert(sizeof(db_fields32)==sizeof(db_fields64));
545
    {
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
        //file fragmentation info
        //a block is just a contiguous region in a file.
        printf("//One header is included in 'data'\n");
        printf("//One header is included in 'additional for checkpoint'\n");
        printf("typedef struct __toku_db_fragmentation {\n");
        printf("  uint64_t file_size_bytes;               //Total file size in bytes\n");
        printf("  uint64_t data_bytes;                    //Compressed User Data in bytes\n");
        printf("  uint64_t data_blocks;                   //Number of blocks of compressed User Data\n");
        printf("  uint64_t checkpoint_bytes_additional;   //Additional bytes used for checkpoint system\n");
        printf("  uint64_t checkpoint_blocks_additional;  //Additional blocks used for checkpoint system \n");
        printf("  uint64_t unused_bytes;                  //Unused space in file\n");
        printf("  uint64_t unused_blocks;                 //Number of contiguous regions of unused space\n");
        printf("  uint64_t largest_unused_block;          //Size of largest contiguous unused space\n");
        printf("} *TOKU_DB_FRAGMENTATION, TOKU_DB_FRAGMENTATION_S;\n");

561
	const char *extra[]={"int (*key_range64)(DB*, DB_TXN *, DBT *, u_int64_t *less, u_int64_t *equal, u_int64_t *greater, int *is_exact)",
562
			     "int (*stat64)(DB *, DB_TXN *, DB_BTREE_STAT64 *)",
563
			     "int (*pre_acquire_read_lock)(DB*, DB_TXN*, const DBT*, const DBT*, const DBT*, const DBT*)",
Yoni Fogel's avatar
Yoni Fogel committed
564
			     "int (*pre_acquire_table_lock)(DB*, DB_TXN*)",
565 566
			     "const DBT* (*dbt_pos_infty)(void) /* Return the special DBT that refers to positive infinity in the lock table.*/",
			     "const DBT* (*dbt_neg_infty)(void)/* Return the special DBT that refers to negative infinity in the lock table.*/",
Yoni Fogel's avatar
Yoni Fogel committed
567
                             "int (*delboth) (DB*, DB_TXN*, DBT*, DBT*, u_int32_t) /* Delete the key/value pair. */",
568
                             "int (*row_size_supported) (DB*, u_int32_t) /* Test whether a row size is supported. */",
569
                             "const DBT *descriptor /* saved row/dictionary descriptor for aiding in comparisons */",
570
                             "int (*set_descriptor) (DB*, u_int32_t version, const DBT* descriptor, toku_dbt_upgradef dbt_userformat_upgrade) /* set row/dictionary descriptor for a db.  Available only while db is open */",
571 572
			     "int (*getf_set)(DB*, DB_TXN*, u_int32_t, DBT*, YDB_CALLBACK_FUNCTION, void*) /* same as DBC->c_getf_set without a persistent cursor) */",
			     "int (*getf_get_both)(DB*, DB_TXN*, u_int32_t, DBT*, DBT*, YDB_CALLBACK_FUNCTION, void*) /* same as DBC->c_getf_get_both without a persistent cursor) */",
573
                             "int (*flatten)(DB*, DB_TXN*) /* Flatten a dictionary, similar to (but faster than) a table scan */",
574
                             "int (*get_fragmentation)(DB*,TOKU_DB_FRAGMENTATION)",
575 576 577
			     NULL};
	print_struct("db", 1, db_fields32, db_fields64, sizeof(db_fields32)/sizeof(db_fields32[0]), extra);
    }
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
578

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
579
    assert(sizeof(db_txn_active_fields32)==sizeof(db_txn_active_fields64));
580
    print_struct("db_txn_active", 0, db_txn_active_fields32, db_txn_active_fields64, sizeof(db_txn_active_fields32)/sizeof(db_txn_active_fields32[0]), 0);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
581
    assert(sizeof(db_txn_fields32)==sizeof(db_txn_fields64));
582
    {
583 584 585 586 587 588 589 590 591
        //txn progress info
        printf("typedef struct __toku_txn_progress {\n");
        printf("  uint64_t entries_total;\n");
        printf("  uint64_t entries_processed;\n");
        printf("  uint8_t  is_commit;\n");
        printf("  uint8_t  stalled_on_checkpoint;\n");
        printf("} *TOKU_TXN_PROGRESS, TOKU_TXN_PROGRESS_S;\n");
        printf("typedef void(*TXN_PROGRESS_POLL_FUNCTION)(TOKU_TXN_PROGRESS, void*);\n");

592
	printf("struct txn_stat {\n  u_int64_t rollback_raw_count;\n};\n");
593 594 595
	const char *extra[] = {
            "int (*txn_stat)(DB_TXN *, struct txn_stat **)", 
            "struct { void *next, *prev; } open_txns",
596 597
            "int (*commit_with_progress)(DB_TXN*, uint32_t, TXN_PROGRESS_POLL_FUNCTION, void*)",
            "int (*abort_with_progress)(DB_TXN*, TXN_PROGRESS_POLL_FUNCTION, void*)",
598 599
            NULL,
        };
600
	print_struct("db_txn", INTERNAL_AT_END, db_txn_fields32, db_txn_fields64, sizeof(db_txn_fields32)/sizeof(db_txn_fields32[0]), extra);
601
    }
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
602

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
603
    assert(sizeof(db_txn_stat_fields32)==sizeof(db_txn_stat_fields64));
604
    print_struct("db_txn_stat", 0, db_txn_stat_fields32, db_txn_stat_fields64, sizeof(db_txn_stat_fields32)/sizeof(db_txn_stat_fields32[0]), 0);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
605

606
    {
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
	const char *extra[]={
			     "int (*c_getf_first)(DBC *, u_int32_t, YDB_CALLBACK_FUNCTION, void *)",
			     "int (*c_getf_last)(DBC *, u_int32_t, YDB_CALLBACK_FUNCTION, void *)",
                             "int (*c_getf_next)(DBC *, u_int32_t, YDB_CALLBACK_FUNCTION, void *)",
			     "int (*c_getf_next_dup)(DBC *, u_int32_t, YDB_CALLBACK_FUNCTION, void *)",
			     "int (*c_getf_next_nodup)(DBC *, u_int32_t, YDB_CALLBACK_FUNCTION, void *)",
			     "int (*c_getf_prev)(DBC *, u_int32_t, YDB_CALLBACK_FUNCTION, void *)",
			     "int (*c_getf_prev_dup)(DBC *, u_int32_t, YDB_CALLBACK_FUNCTION, void *)",
			     "int (*c_getf_prev_nodup)(DBC *, u_int32_t, YDB_CALLBACK_FUNCTION, void *)",
			     "int (*c_getf_current)(DBC *, u_int32_t, YDB_CALLBACK_FUNCTION, void *)",
			     "int (*c_getf_current_binding)(DBC *, u_int32_t, YDB_CALLBACK_FUNCTION, void *)",
                             "int (*c_getf_heaviside)(DBC *, u_int32_t, "
                                 "YDB_HEAVISIDE_CALLBACK_FUNCTION f, void *extra_f, "
                                 "YDB_HEAVISIDE_FUNCTION h, void *extra_h, int direction)",
			     "int (*c_getf_set)(DBC *, u_int32_t, DBT *, YDB_CALLBACK_FUNCTION, void *)",
			     "int (*c_getf_set_range)(DBC *, u_int32_t, DBT *, YDB_CALLBACK_FUNCTION, void *)",
623
			     "int (*c_getf_set_range_reverse)(DBC *, u_int32_t, DBT *, YDB_CALLBACK_FUNCTION, void *)",
624 625
			     "int (*c_getf_get_both)(DBC *, u_int32_t, DBT *, DBT *, YDB_CALLBACK_FUNCTION, void *)",
			     "int (*c_getf_get_both_range)(DBC *, u_int32_t, DBT *, DBT *, YDB_CALLBACK_FUNCTION, void *)",
626
			     "int (*c_getf_get_both_range_reverse)(DBC *, u_int32_t, DBT *, DBT *, YDB_CALLBACK_FUNCTION, void *)",
627 628
			     NULL};
	assert(sizeof(dbc_fields32)==sizeof(dbc_fields64));
629
	print_struct("dbc", INTERNAL_AT_END, dbc_fields32, dbc_fields64, sizeof(dbc_fields32)/sizeof(dbc_fields32[0]), extra);
630
    }
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
631

632
    printf("#ifdef _TOKUDB_WRAP_H\n#define txn_begin txn_begin_tokudb\n#endif\n");
633 634 635 636 637
    printf("int db_env_create(DB_ENV **, u_int32_t) %s;\n", VISIBLE);
    printf("int db_create(DB **, DB_ENV *, u_int32_t) %s;\n", VISIBLE);
    printf("char *db_strerror(int) %s;\n", VISIBLE);
    printf("const char *db_version(int*,int *,int *) %s;\n", VISIBLE);
    printf("int log_compare (const DB_LSN*, const DB_LSN *) %s;\n", VISIBLE);
638
    printf("int db_env_set_func_fsync (int (*)(int)) %s;\n", VISIBLE);
639 640
    printf("int toku_set_trace_file (char *fname) %s;\n", VISIBLE);
    printf("int toku_close_trace_file (void) %s;\n", VISIBLE);
641
    printf("int db_env_set_func_free (void (*)(void*)) %s;\n", VISIBLE);
642
    printf("int db_env_set_func_malloc (void *(*)(size_t)) %s;\n", VISIBLE);
643
    printf("int db_env_set_func_pwrite (ssize_t (*)(int, const void *, size_t, toku_off_t)) %s;\n", VISIBLE);
644
    printf("int db_env_set_func_write (ssize_t (*)(int, const void *, size_t)) %s;\n", VISIBLE);
645
    printf("int db_env_set_func_realloc (void *(*)(void*, size_t)) %s;\n", VISIBLE);
646
    printf("void db_env_set_func_loader_fwrite (size_t (*fwrite_fun)(const void*,size_t,size_t,FILE*)) %s;\n", VISIBLE);
647
    printf("void db_env_set_checkpoint_callback (void (*)(void*), void*) %s;\n", VISIBLE);
648
    printf("void db_env_set_checkpoint_callback2 (void (*)(void*), void*) %s;\n", VISIBLE);
649 650
    printf("void db_env_set_recover_callback (void (*)(void*), void*) %s;\n", VISIBLE);
    printf("void db_env_set_recover_callback2 (void (*)(void*), void*) %s;\n", VISIBLE);
651
    printf("#if defined(__cplusplus)\n}\n#endif\n");
652 653 654
    printf("#endif\n");
    return 0;
}
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
655