make_db_h.c 10.3 KB
Newer Older
1 2 3 4
/* -*- mode: C; c-basic-offset: 4 -*- */
#ident "Copyright (c) 2007 Tokutek Inc.  All rights reserved."
/* 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 13 14 15 16


void print_dbtype(void) {
    /* DBTYPE is mentioned by db_open.html */
    printf("typedef enum {\n");
Yoni Fogel's avatar
Yoni Fogel committed
17 18
    printf(" DB_BTREE=%d,\n", DB_BTREE);
    printf(" DB_UNKNOWN=%d\n", DB_UNKNOWN);
19 20
    printf("} DBTYPE;\n");
}
Bradley C. Kuszmaul's avatar
Fixup  
Bradley C. Kuszmaul committed
21
#if 0
22 23 24 25 26
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
27
#endif
28 29 30 31

#define dodefine(name) printf("#define %s %d\n", #name, name)

void print_defines (void) {
32
    printf("#ifndef _TOKUDB_WRAP_H\n");
33 34 35 36 37 38 39 40 41 42 43 44 45 46
    dodefine(DB_VERB_DEADLOCK);
    dodefine(DB_VERB_RECOVERY);
    dodefine(DB_VERB_REPLICATION);
    dodefine(DB_VERB_WAITSFOR);

    dodefine(DB_DBT_MALLOC);
    dodefine(DB_DBT_REALLOC);
    dodefine(DB_DBT_USERMEM);
    dodefine(DB_DBT_DUPOK);

    dodefine(DB_ARCH_ABS);
    dodefine(DB_ARCH_LOG);

    dodefine(DB_CREATE);
47
    dodefine(DB_EXCL);
48 49 50 51 52 53 54 55 56 57 58
    dodefine(DB_PRIVATE);
    dodefine(DB_RDONLY);
    dodefine(DB_RECOVER);
    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
59
    dodefine(DB_DUPSORT);
60

61 62
    dodefine(DB_KEYFIRST);
    dodefine(DB_KEYLAST);
63
    dodefine(DB_NODUPDATA);
64
    dodefine(DB_NOOVERWRITE);
65 66
    printf("#define DB_YESOVERWRITE 254\n"); // tokudb
    dodefine(DB_OPFLAGS_MASK);
67 68 69 70 71

    dodefine(DB_INIT_LOCK);
    dodefine(DB_INIT_LOG);
    dodefine(DB_INIT_MPOOL);
    dodefine(DB_INIT_TXN);
72 73
    dodefine(DB_USE_ENVIRON);
    dodefine(DB_USE_ENVIRON_ROOT);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
74 75 76 77 78

    dodefine(DB_KEYEMPTY);
    dodefine(DB_KEYEXIST);
    dodefine(DB_LOCK_DEADLOCK);
    dodefine(DB_NOTFOUND);
Yoni Fogel's avatar
{{{  
Yoni Fogel committed
79
    dodefine(DB_SECONDARY_BAD);
80
    dodefine(DB_DONOTINDEX);
81
#ifdef DB_BUFFER_SMALL
82
    dodefine(DB_BUFFER_SMALL);
83
#endif
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
84
    printf("#define DB_BADFORMAT -30500\n"); // private tokudb
85
    printf("#define DB_DELETE_ANY %d\n", 1<<16); // private tokudb
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
86 87 88 89

    dodefine(DB_FIRST);
    dodefine(DB_GET_BOTH);
    dodefine(DB_LAST);
Yoni Fogel's avatar
{{{  
Yoni Fogel committed
90
    dodefine(DB_CURRENT);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
91 92 93 94 95 96
    dodefine(DB_NEXT);
    dodefine(DB_NEXT_DUP);
    dodefine(DB_PREV);
    dodefine(DB_SET);
    dodefine(DB_SET_RANGE);
    dodefine(DB_RMW);
97 98 99 100 101

    dodefine(DB_DBT_APPMALLOC);
#ifdef DB_DBT_MULTIPLE
    dodefine(DB_DBT_MULTIPLE);
#endif
102
    printf("#endif\n");
103
}
104

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
105
//#define DECL_LIMIT 100
106
struct fieldinfo {
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
107
    char *decl;
108 109
    unsigned int off;
    unsigned int size;
110
};
111

112 113 114 115 116 117
#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
118 119 120
#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
121 122 123
#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
124 125 126
#elif USE_MAJOR==4 && USE_MINOR==6
#include "sample_offsets_32_4_6.h"
#include "sample_offsets_64_4_6.h"
127 128 129
#else
#error
#endif
130

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
131
void print_struct (const char *structname, int need_internal, struct fieldinfo *fields32, struct fieldinfo *fields64, unsigned int N) {
132 133 134
    unsigned int i;
    unsigned int current_32 = 0;
    unsigned int current_64 = 0;
135
    int dummy_counter=0;
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
136
    int did_toku_internal=0;
Bradley C. Kuszmaul's avatar
txn  
Bradley C. Kuszmaul committed
137 138
//    int total32 = fields32[N-1].size;
//    int total64 = fields32[N-1].size;
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
139
    printf("struct __toku_%s {\n", structname);
140
    for (i=0; i<N-1; i++) {
141 142
	unsigned int this_32 = fields32[i].off;
	unsigned int this_64 = fields64[i].off;
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
143
	//fprintf(stderr, "this32=%d current32=%d this64=%d current64=%d\n", this_32, current_32, this_64, current_64);
144 145 146 147 148 149
	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
150 151
		unsigned int n_dummys = diff/4;
		if (need_internal && !did_toku_internal) {
Bradley C. Kuszmaul's avatar
dbc  
Bradley C. Kuszmaul committed
152
		    printf("  struct __toku_%s_internal *i;\n", structname);
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
153 154 155
		    n_dummys--;
		    did_toku_internal=1;
		}
156
		if (n_dummys>0) printf("  void* __toku_dummy%d[%d];\n", dummy_counter++, n_dummys);
157 158 159 160 161 162
		diff64-=diff*2;
		diff32-=diff;
		
	    }
	    assert(diff32==diff64);
	    if (diff32>0) {
163
		printf("  char __toku_dummy%d[%d];\n", dummy_counter++, diff32);
164 165 166
	    }
	    current_32 = this_32;
	    current_64 = this_64;
167
	}
168
	if (this_32<current_32 || this_64<current_64) {
Bradley C. Kuszmaul's avatar
32  
Bradley C. Kuszmaul committed
169 170 171 172 173 174 175 176
	    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);
	    printf("  %s; /* 32-bit offset=%d size=%d, 64=bit offset=%d size=%d */\n", fields32[i].decl, fields32[i].off, fields32[i].size, fields64[i].off, fields64[i].size);
	} else {
	    assert(fields32[i].decl==0);
	    assert(fields64[i].decl==0);
177
	}
178 179
	current_32 += fields32[i].size;
	current_64 += fields64[i].size;
180
    }
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    {
	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;
	    printf("  void* __toku_dummy%d[%d]; /* Padding at the end */ \n", dummy_counter++, diff/4);
	    diff64-=diff*2;
	    diff32-=diff;
	}
	if (diff32>0) {
	    printf("  char __toku_dummy%d[%d];  /* Padding at the end */ \n", dummy_counter++, diff32);
	    diff64-=diff32;
	    diff32=0;
	}
	if (diff64>0) printf("  /* %d more bytes of alignment in the 64-bit case. */\n", diff64);
	assert(diff64<8); /* there could be a few left from alignment. */ 
    }
200
    printf("};\n");
201
    assert(did_toku_internal || !need_internal);
202
}
203

204 205 206 207
int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) {
    printf("#ifndef _DB_H\n");
    printf("#define _DB_H\n");
    printf("/* This code generated by make_db_h.   Copyright (c) 2007 Tokutek */\n");
208
    printf("#ident \"Copyright (c) 2007 Tokutek Inc.  All rights reserved.\"\n");
209
    printf("#include <sys/types.h>\n");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
210 211
    printf("/*stdio is needed for the FILE* in db->verify*/\n");
    printf("#include <stdio.h>\n");
212
    //printf("#include <inttypes.h>\n");
213
    printf("#if defined(__cplusplus)\nextern \"C\" {\n#endif\n");
214

215 216
    assert(DB_VERSION_MAJOR==DB_VERSION_MAJOR_32);
    assert(DB_VERSION_MINOR==DB_VERSION_MINOR_32);
217
    printf("#define TOKUDB 1\n");
218 219 220
    dodefine(DB_VERSION_MAJOR);
    dodefine(DB_VERSION_MINOR);
    dodefine(DB_VERSION_PATCH);
221
    printf("#ifndef _TOKUDB_WRAP_H\n");
222
    printf("#define DB_VERSION_STRING \"Tokutek: TokuDB %d.%d.%d\"\n", DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH);
223
    printf("#else\n");
224
    printf("#define DB_VERSION_STRING_ydb \"Tokutek: TokuDB (wrapped bdb)\"\n");
225 226 227 228 229 230 231 232 233
    printf("#endif\n");
    
    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");
    }

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
234
    printf("typedef struct __toku_db_btree_stat DB_BTREE_STAT;\n");
235
    printf("typedef struct __toku_db_env DB_ENV;\n");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
236 237
    printf("typedef struct __toku_db_key_range DB_KEY_RANGE;\n");
    printf("typedef struct __toku_db_lsn DB_LSN;\n");
238
    printf("typedef struct __toku_db DB;\n");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
239
    printf("typedef struct __toku_db_txn DB_TXN;\n");
240
    printf("typedef struct __toku_db_txn_active DB_TXN_ACTIVE;\n");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
241 242 243
    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");
244
    print_dbtype();
Bradley C. Kuszmaul's avatar
Fixup  
Bradley C. Kuszmaul committed
245
//    print_db_notices();
246
    print_defines();
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
247

248 249
    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
250 251
    assert(sizeof(db_btree_stat_fields32)==sizeof(db_btree_stat_fields64));
    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]));
Bradley C. Kuszmaul's avatar
32  
Bradley C. Kuszmaul committed
252 253 254
    assert(sizeof(db_env_fields32)==sizeof(db_env_fields64));
    print_struct("db_env", 1, db_env_fields32, db_env_fields64, sizeof(db_env_fields32)/sizeof(db_env_fields32[0]));

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
255 256 257 258 259 260
    assert(sizeof(db_key_range_fields32)==sizeof(db_key_range_fields64));
    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]));

    assert(sizeof(db_lsn_fields32)==sizeof(db_lsn_fields64));
    print_struct("db_lsn", 0, db_lsn_fields32, db_lsn_fields64, sizeof(db_lsn_fields32)/sizeof(db_lsn_fields32[0]));

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
261 262 263
    assert(sizeof(db_fields32)==sizeof(db_fields64));
    print_struct("db", 1, db_fields32, db_fields64, sizeof(db_fields32)/sizeof(db_fields32[0]));

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
264 265
    assert(sizeof(db_txn_active_fields32)==sizeof(db_txn_active_fields64));
    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]));
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
266 267 268
    assert(sizeof(db_txn_fields32)==sizeof(db_txn_fields64));
    print_struct("db_txn", 1, db_txn_fields32, db_txn_fields64, sizeof(db_txn_fields32)/sizeof(db_txn_fields32[0]));

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
269 270 271
    assert(sizeof(db_txn_stat_fields32)==sizeof(db_txn_stat_fields64));
    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]));

Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
272 273 274 275 276
    assert(sizeof(dbc_fields32)==sizeof(dbc_fields64));
    print_struct("dbc", 1, dbc_fields32, dbc_fields64, sizeof(dbc_fields32)/sizeof(dbc_fields32[0]));

    assert(sizeof(dbt_fields32)==sizeof(dbt_fields64));
    print_struct("dbt", 0, dbt_fields32, dbt_fields64, sizeof(dbt_fields32)/sizeof(dbt_fields32[0]));
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
277

278
    printf("#ifdef _TOKUDB_WRAP_H\n#define txn_begin txn_begin_tokudb\n#endif\n");
279 280
    printf("int db_env_create(DB_ENV **, u_int32_t);\n");
    printf("int db_create(DB **, DB_ENV *, u_int32_t);\n");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
281
    printf("char *db_strerror(int);\n");
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
282
    printf("const char *db_version(int*,int *,int *);\n");
Bradley C. Kuszmaul's avatar
Up  
Bradley C. Kuszmaul committed
283
    printf("int log_compare (const DB_LSN*, const DB_LSN *);\n");
284
    printf("#if defined(__cplusplus)\n}\n#endif\n");
285 286 287
    printf("#endif\n");
    return 0;
}
Bradley C. Kuszmaul's avatar
Bradley C. Kuszmaul committed
288