You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
14 KiB

  1. /* -*- mode: C; c-basic-offset: 4 -*- */
  2. #ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved."
  3. /* LICENSE: This file is licensed under the GPL or from Tokutek. */
  4. /* Make a db.h that will be link-time compatible with Sleepycat's Berkeley DB. */
  5. #include <db.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <assert.h>
  9. #include <string.h>
  10. #define VISIBLE "__attribute__((__visibility__(\"default\")))"
  11. void print_dbtype(void) {
  12. /* DBTYPE is mentioned by db_open.html */
  13. printf("typedef enum {\n");
  14. printf(" DB_BTREE=%d,\n", DB_BTREE);
  15. printf(" DB_UNKNOWN=%d\n", DB_UNKNOWN);
  16. printf("} DBTYPE;\n");
  17. }
  18. #if 0
  19. void print_db_notices (void) {
  20. printf("typedef enum { /* This appears to be a mysql-specific addition to the api. */ \n");
  21. printf(" DB_NOTICE_LOGFILE_CHANGED=%d\n", DB_NOTICE_LOGFILE_CHANGED);
  22. printf("} db_notices;\n");
  23. }
  24. #endif
  25. #define dodefine(name) printf("#define %s %d\n", #name, name)
  26. enum {
  27. TOKUDB_OUT_OF_LOCKS = -100000,
  28. TOKUDB_SUCCEEDED_EARLY = -100001
  29. };
  30. void print_defines (void) {
  31. printf("#ifndef _TOKUDB_WRAP_H\n");
  32. dodefine(DB_VERB_DEADLOCK);
  33. dodefine(DB_VERB_RECOVERY);
  34. dodefine(DB_VERB_REPLICATION);
  35. dodefine(DB_VERB_WAITSFOR);
  36. dodefine(DB_DBT_MALLOC);
  37. dodefine(DB_DBT_REALLOC);
  38. dodefine(DB_DBT_USERMEM);
  39. dodefine(DB_DBT_DUPOK);
  40. dodefine(DB_ARCH_ABS);
  41. dodefine(DB_ARCH_LOG);
  42. dodefine(DB_CREATE);
  43. dodefine(DB_CXX_NO_EXCEPTIONS);
  44. dodefine(DB_EXCL);
  45. dodefine(DB_PRIVATE);
  46. dodefine(DB_RDONLY);
  47. dodefine(DB_RECOVER);
  48. dodefine(DB_THREAD);
  49. dodefine(DB_TXN_NOSYNC);
  50. dodefine(DB_LOCK_DEFAULT);
  51. dodefine(DB_LOCK_OLDEST);
  52. dodefine(DB_LOCK_RANDOM);
  53. dodefine(DB_DUP);
  54. dodefine(DB_DUPSORT);
  55. dodefine(DB_KEYFIRST);
  56. dodefine(DB_KEYLAST);
  57. dodefine(DB_NODUPDATA);
  58. dodefine(DB_NOOVERWRITE);
  59. printf("#define DB_YESOVERWRITE 254\n"); // tokudb
  60. dodefine(DB_OPFLAGS_MASK);
  61. dodefine(DB_AUTO_COMMIT);
  62. dodefine(DB_INIT_LOCK);
  63. dodefine(DB_INIT_LOG);
  64. dodefine(DB_INIT_MPOOL);
  65. dodefine(DB_INIT_TXN);
  66. dodefine(DB_USE_ENVIRON);
  67. dodefine(DB_USE_ENVIRON_ROOT);
  68. dodefine(DB_KEYEMPTY);
  69. dodefine(DB_KEYEXIST);
  70. dodefine(DB_LOCK_DEADLOCK);
  71. dodefine(DB_LOCK_NOTGRANTED);
  72. dodefine(DB_NOTFOUND);
  73. dodefine(DB_SECONDARY_BAD);
  74. dodefine(DB_DONOTINDEX);
  75. #ifdef DB_BUFFER_SMALL
  76. dodefine(DB_BUFFER_SMALL);
  77. #endif
  78. printf("#define DB_BADFORMAT -30500\n"); // private tokudb
  79. printf("#define DB_DELETE_ANY %d\n", 1<<16); // private tokudb
  80. dodefine(DB_FIRST);
  81. dodefine(DB_GET_BOTH);
  82. dodefine(DB_GET_BOTH_RANGE);
  83. dodefine(DB_LAST);
  84. dodefine(DB_CURRENT);
  85. dodefine(DB_NEXT);
  86. dodefine(DB_NEXT_DUP);
  87. dodefine(DB_NEXT_NODUP);
  88. dodefine(DB_PREV);
  89. #ifdef DB_PREV_DUP
  90. dodefine(DB_PREV_DUP);
  91. #endif
  92. dodefine(DB_PREV_NODUP);
  93. dodefine(DB_SET);
  94. dodefine(DB_SET_RANGE);
  95. printf("#define DB_CURRENT_BINDING 253\n"); // private tokudb
  96. dodefine(DB_RMW);
  97. printf("#define DB_PRELOCKED 0x00800000\n"); // private tokudb
  98. printf("#define DB_PRELOCKED_WRITE 0x00400000\n"); // private tokudb
  99. dodefine(DB_DBT_APPMALLOC);
  100. #ifdef DB_DBT_MULTIPLE
  101. dodefine(DB_DBT_MULTIPLE);
  102. #endif
  103. // flags for the env->set_flags function
  104. #if DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3
  105. dodefine(DB_LOG_AUTOREMOVE);
  106. #endif
  107. dodefine(DB_TXN_WRITE_NOSYNC);
  108. dodefine(DB_TXN_NOWAIT);
  109. dodefine(DB_TXN_SYNC);
  110. printf("#endif\n");
  111. /* TOKUDB specific error codes*/
  112. printf("/* TOKUDB specific error codes */\n");
  113. dodefine(TOKUDB_OUT_OF_LOCKS);
  114. dodefine(TOKUDB_SUCCEEDED_EARLY);
  115. }
  116. //#define DECL_LIMIT 100
  117. struct fieldinfo {
  118. char *decl;
  119. unsigned int off;
  120. unsigned int size;
  121. };
  122. #if USE_MAJOR==4 && USE_MINOR==1
  123. #include "sample_offsets_32_4_1.h"
  124. #include "sample_offsets_64_4_1.h"
  125. #elif USE_MAJOR==4 && USE_MINOR==3
  126. #include "sample_offsets_32_4_3.h"
  127. #include "sample_offsets_64_4_3.h"
  128. #elif USE_MAJOR==4 && USE_MINOR==4
  129. #include "sample_offsets_32_4_4.h"
  130. #include "sample_offsets_64_4_4.h"
  131. #elif USE_MAJOR==4 && USE_MINOR==5
  132. #include "sample_offsets_32_4_5.h"
  133. #include "sample_offsets_64_4_5.h"
  134. #elif USE_MAJOR==4 && USE_MINOR==6
  135. #include "sample_offsets_32_4_6.h"
  136. #include "sample_offsets_64_4_6.h"
  137. #else
  138. #error
  139. #endif
  140. void print_struct (const char *structname, int need_internal, struct fieldinfo *fields32, struct fieldinfo *fields64, unsigned int N, const char *extra_decls[]) {
  141. unsigned int i;
  142. unsigned int current_32 = 0;
  143. unsigned int current_64 = 0;
  144. int dummy_counter=0;
  145. int did_toku_internal=0;
  146. // int total32 = fields32[N-1].size;
  147. // int total64 = fields32[N-1].size;
  148. printf("struct __toku_%s {\n", structname);
  149. for (i=0; i<N-1; i++) {
  150. unsigned int this_32 = fields32[i].off;
  151. unsigned int this_64 = fields64[i].off;
  152. //fprintf(stderr, "this32=%d current32=%d this64=%d current64=%d\n", this_32, current_32, this_64, current_64);
  153. if (this_32 > current_32 || this_64 > current_64) {
  154. unsigned int diff32 = this_32-current_32;
  155. unsigned int diff64 = this_64-current_64;
  156. assert(this_32 > current_32 && this_64 > current_64);
  157. if (diff32!=diff64) {
  158. unsigned int diff = diff64-diff32;
  159. unsigned int n_dummys = diff/4;
  160. if (need_internal && !did_toku_internal) {
  161. printf(" struct __toku_%s_internal *i;\n", structname);
  162. n_dummys--;
  163. did_toku_internal=1;
  164. }
  165. while (n_dummys>0 && extra_decls && *extra_decls) {
  166. printf(" %s;\n", *extra_decls);
  167. extra_decls++;
  168. n_dummys--;
  169. }
  170. if (n_dummys>0) {
  171. printf(" void* __toku_dummy%d[%d];\n", dummy_counter++, n_dummys);
  172. }
  173. diff64-=diff*2;
  174. diff32-=diff;
  175. }
  176. assert(diff32==diff64);
  177. if (diff32>0) {
  178. printf(" char __toku_dummy%d[%d];\n", dummy_counter++, diff32);
  179. }
  180. current_32 = this_32;
  181. current_64 = this_64;
  182. }
  183. if (this_32<current_32 || this_64<current_64) {
  184. printf("Whoops this_32=%d this_64=%d\n", this_32, this_64);
  185. }
  186. if (i+1<N) {
  187. assert(strcmp(fields32[i].decl, fields64[i].decl)==0);
  188. 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);
  189. } else {
  190. assert(fields32[i].decl==0);
  191. assert(fields64[i].decl==0);
  192. }
  193. current_32 += fields32[i].size;
  194. current_64 += fields64[i].size;
  195. }
  196. if (extra_decls) assert(NULL==*extra_decls); // make sure that the extra decls all got used up.
  197. {
  198. unsigned int this_32 = fields32[N-1].off;
  199. unsigned int this_64 = fields64[N-1].off;
  200. unsigned int diff32 = this_32-current_32;
  201. unsigned int diff64 = this_64-current_64;
  202. if (diff32>0 && diff32<diff64) {
  203. unsigned int diff = diff64-diff32;
  204. printf(" void* __toku_dummy%d[%d]; /* Padding at the end */ \n", dummy_counter++, diff/4);
  205. diff64-=diff*2;
  206. diff32-=diff;
  207. }
  208. if (diff32>0) {
  209. printf(" char __toku_dummy%d[%d]; /* Padding at the end */ \n", dummy_counter++, diff32);
  210. diff64-=diff32;
  211. diff32=0;
  212. }
  213. if (diff64>0) printf(" /* %d more bytes of alignment in the 64-bit case. */\n", diff64);
  214. assert(diff64<8); /* there could be a few left from alignment. */
  215. }
  216. printf("};\n");
  217. assert(did_toku_internal || !need_internal);
  218. }
  219. int main (int argc __attribute__((__unused__)), char *argv[] __attribute__((__unused__))) {
  220. printf("#ifndef _DB_H\n");
  221. printf("#define _DB_H\n");
  222. printf("/* This code generated by make_db_h. Copyright (c) 2007, 2008 Tokutek */\n");
  223. printf("#ident \"Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved.\"\n");
  224. printf("#include <sys/types.h>\n");
  225. printf("/*stdio is needed for the FILE* in db->verify*/\n");
  226. printf("#include <stdio.h>\n");
  227. //printf("#include <inttypes.h>\n");
  228. printf("#if defined(__cplusplus)\nextern \"C\" {\n#endif\n");
  229. assert(DB_VERSION_MAJOR==DB_VERSION_MAJOR_32);
  230. assert(DB_VERSION_MINOR==DB_VERSION_MINOR_32);
  231. printf("#define TOKUDB 1\n");
  232. dodefine(DB_VERSION_MAJOR);
  233. dodefine(DB_VERSION_MINOR);
  234. dodefine(DB_VERSION_PATCH);
  235. printf("#ifndef _TOKUDB_WRAP_H\n");
  236. printf("#define DB_VERSION_STRING \"Tokutek: TokuDB %d.%d.%d\"\n", DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH);
  237. printf("#else\n");
  238. printf("#define DB_VERSION_STRING_ydb \"Tokutek: TokuDB (wrapped bdb)\"\n");
  239. printf("#endif\n");
  240. if (0) {
  241. printf("#ifndef __BIT_TYPES_DEFINED__\n");
  242. printf("/* Define some int types if not provided by the system. BIND does this, so we do it too. */\n");
  243. printf("typedef unsigned int u_int32_t;\n");
  244. printf("#endif\n");
  245. }
  246. printf("typedef struct __toku_db_btree_stat DB_BTREE_STAT;\n");
  247. printf("typedef struct __toku_db_env DB_ENV;\n");
  248. printf("typedef struct __toku_db_key_range DB_KEY_RANGE;\n");
  249. printf("typedef struct __toku_db_lsn DB_LSN;\n");
  250. printf("typedef struct __toku_db DB;\n");
  251. printf("typedef struct __toku_db_txn DB_TXN;\n");
  252. printf("typedef struct __toku_db_txn_active DB_TXN_ACTIVE;\n");
  253. printf("typedef struct __toku_db_txn_stat DB_TXN_STAT;\n");
  254. printf("typedef struct __toku_dbc DBC;\n");
  255. printf("typedef struct __toku_dbt DBT;\n");
  256. printf("typedef u_int32_t db_recno_t;\n");
  257. print_dbtype();
  258. // print_db_notices();
  259. print_defines();
  260. 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");
  261. printf("#ifdef _TOKUDB_WRAP_H\n#undef txn_begin\n#endif\n");
  262. assert(sizeof(db_btree_stat_fields32)==sizeof(db_btree_stat_fields64));
  263. 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);
  264. assert(sizeof(db_env_fields32)==sizeof(db_env_fields64));
  265. print_struct("db_env", 1, db_env_fields32, db_env_fields64, sizeof(db_env_fields32)/sizeof(db_env_fields32[0]), 0);
  266. assert(sizeof(db_key_range_fields32)==sizeof(db_key_range_fields64));
  267. 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);
  268. assert(sizeof(db_lsn_fields32)==sizeof(db_lsn_fields64));
  269. print_struct("db_lsn", 0, db_lsn_fields32, db_lsn_fields64, sizeof(db_lsn_fields32)/sizeof(db_lsn_fields32[0]), 0);
  270. assert(sizeof(db_fields32)==sizeof(db_fields64));
  271. {
  272. 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)",
  273. "int (*pre_acquire_read_lock)(DB*, DB_TXN*, const DBT*, const DBT*, const DBT*, const DBT*)",
  274. "int (*pre_acquire_table_lock)(DB*, DB_TXN*)",
  275. "const DBT* (*dbt_pos_infty)(void) /* Return the special DBT that refers to positive infinity in the lock table.*/",
  276. "const DBT* (*dbt_neg_infty)(void)/* Return the special DBT that refers to negative infinity in the lock table.*/",
  277. NULL};
  278. print_struct("db", 1, db_fields32, db_fields64, sizeof(db_fields32)/sizeof(db_fields32[0]), extra);
  279. }
  280. assert(sizeof(db_txn_active_fields32)==sizeof(db_txn_active_fields64));
  281. 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);
  282. assert(sizeof(db_txn_fields32)==sizeof(db_txn_fields64));
  283. print_struct("db_txn", 1, db_txn_fields32, db_txn_fields64, sizeof(db_txn_fields32)/sizeof(db_txn_fields32[0]), 0);
  284. assert(sizeof(db_txn_stat_fields32)==sizeof(db_txn_stat_fields64));
  285. 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);
  286. {
  287. const char *extra[]={"int (*c_getf_next)(DBC *, u_int32_t, void(*)(DBT const *, DBT const *, void *), void *)",
  288. "int (*c_getf_next_dup)(DBC *, u_int32_t, void(*)(DBT const *, DBT const *, void *), void *)",
  289. "int (*c_getf_next_no_dup)(DBC *, u_int32_t, void(*)(DBT const *, DBT const *, void *), void *)",
  290. "int (*c_getf_prev)(DBC *, u_int32_t, void(*)(DBT const *, DBT const *, void *), void *)",
  291. "int (*c_getf_prev_dup)(DBC *, u_int32_t, void(*)(DBT const *, DBT const *, void *), void *)",
  292. "int (*c_getf_prev_no_dup)(DBC *, u_int32_t, void(*)(DBT const *, DBT const *, void *), void *)",
  293. "int (*c_getf_current)(DBC *, u_int32_t, void(*)(DBT const *, DBT const *, void *), void *)",
  294. "int (*c_getf_first)(DBC *, u_int32_t, void(*)(DBT const *, DBT const *, void *), void *)",
  295. "int (*c_getf_last)(DBC *, u_int32_t, void(*)(DBT const *, DBT const *, void *), void *)",
  296. "int (*c_getf_heavi)(DBC *, u_int32_t, "
  297. "void(*f)(DBT const *, DBT const *, void *, int), void *extra_f, "
  298. "int (*h)(const DBT *key, const DBT *value, void *extra_h), void *extra_h, int direction)",
  299. NULL};
  300. assert(sizeof(dbc_fields32)==sizeof(dbc_fields64));
  301. print_struct("dbc", 1, dbc_fields32, dbc_fields64, sizeof(dbc_fields32)/sizeof(dbc_fields32[0]), extra);
  302. }
  303. assert(sizeof(dbt_fields32)==sizeof(dbt_fields64));
  304. print_struct("dbt", 0, dbt_fields32, dbt_fields64, sizeof(dbt_fields32)/sizeof(dbt_fields32[0]), 0);
  305. printf("#ifdef _TOKUDB_WRAP_H\n#define txn_begin txn_begin_tokudb\n#endif\n");
  306. printf("int db_env_create(DB_ENV **, u_int32_t) %s;\n", VISIBLE);
  307. printf("int db_create(DB **, DB_ENV *, u_int32_t) %s;\n", VISIBLE);
  308. printf("char *db_strerror(int) %s;\n", VISIBLE);
  309. printf("const char *db_version(int*,int *,int *) %s;\n", VISIBLE);
  310. printf("int log_compare (const DB_LSN*, const DB_LSN *) %s;\n", VISIBLE);
  311. printf("int db_env_set_func_fsync (int (*)(int)) %s;\n", VISIBLE);
  312. printf("int toku_set_trace_file (char *fname) %s;\n", VISIBLE);
  313. printf("int toku_close_trace_file (void) %s;\n", VISIBLE);
  314. printf("#if defined(__cplusplus)\n}\n#endif\n");
  315. printf("#endif\n");
  316. return 0;
  317. }