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.

2415 lines
80 KiB

  1. /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
  3. #ident "$Id$"
  4. #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
  5. #ident "Copyright (c) 2007-2012 Tokutek Inc. All rights reserved."
  6. // Purpose of this file is to handle all modifications and queries to the database
  7. // at the level of leafentry.
  8. //
  9. // ule = Unpacked Leaf Entry
  10. //
  11. // This design unpacks the leafentry into a convenient format, performs all work
  12. // on the unpacked form, then repacks the leafentry into its compact format.
  13. //
  14. // See design documentation for nested transactions at
  15. // TokuWiki/Imp/TransactionsOverview.
  16. #include <toku_portability.h>
  17. #include "fttypes.h"
  18. #include "ft-internal.h"
  19. #include <util/omt.h>
  20. #include "leafentry.h"
  21. #include "xids.h"
  22. #include "ft_msg.h"
  23. #include "ule.h"
  24. #include "txn_manager.h"
  25. #include "ule-internal.h"
  26. #include <util/partitioned_counter.h>
  27. #define ULE_DEBUG 0
  28. static uint32_t ule_get_innermost_numbytes(ULE ule);
  29. ///////////////////////////////////////////////////////////////////////////////////
  30. // Engine status
  31. //
  32. // Status is intended for display to humans to help understand system behavior.
  33. // It does not need to be perfectly thread-safe.
  34. static LE_STATUS_S le_status;
  35. #define STATUS_INIT(k,c,t,l,inc) TOKUDB_STATUS_INIT(le_status, k, c, t, "le: " l, inc)
  36. static void
  37. status_init(void) {
  38. // Note, this function initializes the keyname, type, and legend fields.
  39. // Value fields are initialized to zero by compiler.
  40. STATUS_INIT(LE_MAX_COMMITTED_XR, nullptr, UINT64, "max committed xr", TOKU_ENGINE_STATUS);
  41. STATUS_INIT(LE_MAX_PROVISIONAL_XR, nullptr, UINT64, "max provisional xr", TOKU_ENGINE_STATUS);
  42. STATUS_INIT(LE_EXPANDED, nullptr, UINT64, "expanded", TOKU_ENGINE_STATUS);
  43. STATUS_INIT(LE_MAX_MEMSIZE, nullptr, UINT64, "max memsize", TOKU_ENGINE_STATUS);
  44. le_status.initialized = true;
  45. }
  46. #undef STATUS_INIT
  47. void
  48. toku_le_get_status(LE_STATUS statp) {
  49. if (!le_status.initialized)
  50. status_init();
  51. *statp = le_status;
  52. }
  53. #define STATUS_VALUE(x) le_status.status[x].value.num
  54. ///////////////////////////////////////////////////////////////////////////////////
  55. // Accessor functions used by outside world (e.g. indexer)
  56. //
  57. ULEHANDLE
  58. toku_ule_create(LEAFENTRY le) {
  59. ULE XMALLOC(ule_p);
  60. le_unpack(ule_p, le);
  61. return (ULEHANDLE) ule_p;
  62. }
  63. void toku_ule_free(ULEHANDLE ule_p) {
  64. ule_cleanup((ULE) ule_p);
  65. toku_free(ule_p);
  66. }
  67. ///////////////////////////////////////////////////////////////////////////////////
  68. //
  69. // Question: Can any software outside this file modify or read a leafentry?
  70. // If so, is it worthwhile to put it all here?
  71. //
  72. // There are two entries, one each for modification and query:
  73. // toku_le_apply_msg() performs all inserts/deletes/aborts
  74. //
  75. //
  76. //
  77. //
  78. //This is what we use to initialize Xuxrs[0] in a new unpacked leafentry.
  79. const UXR_S committed_delete = {
  80. .type = XR_DELETE,
  81. .vallen = 0,
  82. .valp = NULL,
  83. .xid = 0
  84. }; // static allocation of uxr with type set to committed delete and xid = 0
  85. #define INSERT_LENGTH(len) ((1U << 31) | len)
  86. #define DELETE_LENGTH(len) (0)
  87. #define GET_LENGTH(len) (len & ((1U << 31)-1))
  88. #define IS_INSERT(len) (len & (1U << 31))
  89. #define IS_VALID_LEN(len) (len < (1U<<31))
  90. // Local functions:
  91. static void msg_init_empty_ule(ULE ule, FT_MSG msg);
  92. static void msg_modify_ule(ULE ule, FT_MSG msg);
  93. static void ule_init_empty_ule(ULE ule, uint32_t keylen, void * keyp);
  94. static void ule_do_implicit_promotions(ULE ule, XIDS xids);
  95. static void ule_try_promote_provisional_outermost(ULE ule, TXNID oldest_possible_live_xid);
  96. static void ule_promote_provisional_innermost_to_index(ULE ule, uint32_t index);
  97. static void ule_promote_provisional_innermost_to_committed(ULE ule);
  98. static void ule_apply_insert(ULE ule, XIDS xids, uint32_t vallen, void * valp);
  99. static void ule_apply_delete(ULE ule, XIDS xids);
  100. static void ule_prepare_for_new_uxr(ULE ule, XIDS xids);
  101. static void ule_apply_abort(ULE ule, XIDS xids);
  102. static void ule_apply_broadcast_commit_all (ULE ule);
  103. static void ule_apply_commit(ULE ule, XIDS xids);
  104. static void ule_push_insert_uxr(ULE ule, bool is_committed, TXNID xid, uint32_t vallen, void * valp);
  105. static void ule_push_delete_uxr(ULE ule, bool is_committed, TXNID xid);
  106. static void ule_push_placeholder_uxr(ULE ule, TXNID xid);
  107. static UXR ule_get_innermost_uxr(ULE ule);
  108. static UXR ule_get_first_empty_uxr(ULE ule);
  109. static void ule_remove_innermost_uxr(ULE ule);
  110. static TXNID ule_get_innermost_xid(ULE ule);
  111. static TXNID ule_get_xid(ULE ule, uint32_t index);
  112. static void ule_remove_innermost_placeholders(ULE ule);
  113. static void ule_add_placeholders(ULE ule, XIDS xids);
  114. static void ule_optimize(ULE ule, XIDS xids);
  115. static inline bool uxr_type_is_insert(uint8_t type);
  116. static inline bool uxr_type_is_delete(uint8_t type);
  117. static inline bool uxr_type_is_placeholder(uint8_t type);
  118. static inline size_t uxr_pack_txnid(UXR uxr, uint8_t *p);
  119. static inline size_t uxr_pack_type_and_length(UXR uxr, uint8_t *p);
  120. static inline size_t uxr_pack_length_and_bit(UXR uxr, uint8_t *p);
  121. static inline size_t uxr_pack_data(UXR uxr, uint8_t *p);
  122. static inline size_t uxr_unpack_txnid(UXR uxr, uint8_t *p);
  123. static inline size_t uxr_unpack_type_and_length(UXR uxr, uint8_t *p);
  124. static inline size_t uxr_unpack_length_and_bit(UXR uxr, uint8_t *p);
  125. static inline size_t uxr_unpack_data(UXR uxr, uint8_t *p);
  126. static void *
  127. le_malloc(OMT *omtp, struct mempool *mp, size_t size, void **maybe_free)
  128. {
  129. void * rval;
  130. if (omtp)
  131. rval = mempool_malloc_from_omt(omtp, mp, size, maybe_free);
  132. else
  133. rval = toku_xmalloc(size);
  134. resource_assert(rval);
  135. return rval;
  136. }
  137. /////////////////////////////////////////////////////////////////////
  138. // Garbage collection related functions
  139. //
  140. static TXNID
  141. get_next_older_txnid(TXNID xc, const xid_omt_t &omt) {
  142. int r;
  143. TXNID xid;
  144. r = omt.find<TXNID, toku_find_xid_by_xid>(xc, -1, &xid, nullptr);
  145. if (r==0) {
  146. invariant(xid < xc); //sanity check
  147. }
  148. else {
  149. invariant(r==DB_NOTFOUND);
  150. xid = TXNID_NONE;
  151. }
  152. return xid;
  153. }
  154. //
  155. // This function returns true if live transaction TL1 is allowed to read a value committed by
  156. // transaction xc, false otherwise.
  157. //
  158. static bool
  159. xid_reads_committed_xid(TXNID tl1, TXNID xc, const xid_omt_t &snapshot_txnids, const rx_omt_t &referenced_xids) {
  160. bool rval;
  161. if (tl1 < xc) rval = false; //cannot read a newer txn
  162. else {
  163. TXNID x = toku_get_youngest_live_list_txnid_for(xc, snapshot_txnids, referenced_xids);
  164. if (x == TXNID_NONE) rval = true; //Not in ANY live list, tl1 can read it.
  165. else rval = tl1 > x; //Newer than the 'newest one that has it in live list'
  166. // we know tl1 > xc
  167. // we know x > xc
  168. // if tl1 == x, then we do not read, because tl1 is in xc's live list
  169. // if x is older than tl1, that means that xc < x < tl1
  170. // and if xc is in x's live list, it CANNOT be in tl1's live list
  171. }
  172. return rval;
  173. }
  174. //
  175. // This function does some simple garbage collection given a TXNID known
  176. // to be the oldest referenced xid, that is, the oldest xid in any live list.
  177. // We find the youngest entry in the stack with an xid less
  178. // than oldest_referenced_xid. All elements below this entry are garbage,
  179. // so we get rid of them.
  180. //
  181. static void
  182. ule_simple_garbage_collection(ULE ule, TXNID oldest_referenced_xid, GC_INFO gc_info) {
  183. uint32_t curr_index = 0;
  184. uint32_t num_entries;
  185. if (ule->num_cuxrs == 1) {
  186. goto done;
  187. }
  188. if (gc_info.mvcc_needed) {
  189. // starting at the top of the committed stack, find the first
  190. // uxr with a txnid that is less than oldest_referenced_xid
  191. for (uint32_t i = 0; i < ule->num_cuxrs; i++) {
  192. curr_index = ule->num_cuxrs - i - 1;
  193. if (ule->uxrs[curr_index].xid < oldest_referenced_xid) {
  194. break;
  195. }
  196. }
  197. }
  198. else {
  199. // if mvcc is not needed, we can need the top committed
  200. // value and nothing else
  201. curr_index = ule->num_cuxrs - 1;
  202. }
  203. // curr_index is now set to the youngest uxr older than oldest_referenced_xid
  204. if (curr_index == 0) {
  205. goto done;
  206. }
  207. // now get rid of the entries below curr_index
  208. num_entries = ule->num_cuxrs + ule->num_puxrs - curr_index;
  209. memmove(&ule->uxrs[0], &ule->uxrs[curr_index], num_entries * sizeof(ule->uxrs[0]));
  210. ule->uxrs[0].xid = TXNID_NONE; //New 'bottom of stack' loses its TXNID
  211. ule->num_cuxrs -= curr_index;
  212. done:;
  213. }
  214. static void
  215. ule_garbage_collect(ULE ule, const xid_omt_t &snapshot_xids, const rx_omt_t &referenced_xids, const xid_omt_t &live_root_txns) {
  216. if (ule->num_cuxrs == 1) goto done;
  217. // will fail if too many num_cuxrs
  218. bool necessary_static[MAX_TRANSACTION_RECORDS];
  219. bool *necessary;
  220. necessary = necessary_static;
  221. if (ule->num_cuxrs >= MAX_TRANSACTION_RECORDS) {
  222. XMALLOC_N(ule->num_cuxrs, necessary);
  223. }
  224. memset(necessary, 0, sizeof(necessary[0])*ule->num_cuxrs);
  225. uint32_t curr_committed_entry;
  226. curr_committed_entry = ule->num_cuxrs - 1;
  227. while (true) {
  228. // mark the curr_committed_entry as necessary
  229. necessary[curr_committed_entry] = true;
  230. if (curr_committed_entry == 0) break; //nothing left
  231. // find the youngest live transaction that reads something
  232. // below curr_committed_entry, if it exists
  233. TXNID tl1;
  234. TXNID xc = ule->uxrs[curr_committed_entry].xid;
  235. //
  236. // If we find that the committed transaction is in the live list,
  237. // then xc is really in the process of being committed. It has not
  238. // been fully committed. As a result, our assumption that transactions
  239. // newer than what is currently in these OMTs will read the top of the stack
  240. // is not necessarily accurate. Transactions may read what is just below xc.
  241. // As a result, we must mark what is just below xc as necessary and move on.
  242. // This issue was found while testing flusher threads, and was fixed for #3979
  243. //
  244. bool is_xc_live = toku_is_txn_in_live_root_txn_list(live_root_txns, xc);
  245. if (is_xc_live) {
  246. curr_committed_entry--;
  247. continue;
  248. }
  249. tl1 = toku_get_youngest_live_list_txnid_for(xc, snapshot_xids, referenced_xids);
  250. if (tl1 == xc) {
  251. // if tl1 == xc, that means xc should be live and show up in
  252. // live_root_txns, which we check above. So, if we get
  253. // here, something is wrong.
  254. assert(false);
  255. }
  256. if (tl1 == TXNID_NONE) {
  257. // set tl1 to youngest live transaction older than ule->uxrs[curr_committed_entry]->xid
  258. tl1 = get_next_older_txnid(xc, snapshot_xids);
  259. if (tl1 == TXNID_NONE) {
  260. //Remainder is garbage, we're done
  261. break;
  262. }
  263. }
  264. if (garbage_collection_debug)
  265. {
  266. int r = snapshot_xids.find_zero<TXNID, toku_find_xid_by_xid>(tl1, nullptr, nullptr);
  267. invariant(r==0); //make sure that the txn you are claiming is live is actually live
  268. }
  269. //
  270. // tl1 should now be set
  271. //
  272. curr_committed_entry--;
  273. while (curr_committed_entry > 0) {
  274. xc = ule->uxrs[curr_committed_entry].xid;
  275. if (xid_reads_committed_xid(tl1, xc, snapshot_xids, referenced_xids)) {
  276. break;
  277. }
  278. curr_committed_entry--;
  279. }
  280. }
  281. uint32_t first_free;
  282. first_free = 0;
  283. uint32_t i;
  284. for (i = 0; i < ule->num_cuxrs; i++) {
  285. //Shift values to 'delete' garbage values.
  286. if (necessary[i]) {
  287. ule->uxrs[first_free] = ule->uxrs[i];
  288. first_free++;
  289. }
  290. }
  291. uint32_t saved;
  292. saved = first_free;
  293. invariant(saved <= ule->num_cuxrs);
  294. invariant(saved >= 1);
  295. ule->uxrs[0].xid = TXNID_NONE; //New 'bottom of stack' loses its TXNID
  296. if (first_free != ule->num_cuxrs) {
  297. //Shift provisional values
  298. memmove(&ule->uxrs[first_free], &ule->uxrs[ule->num_cuxrs], ule->num_puxrs * sizeof(ule->uxrs[0]));
  299. }
  300. ule->num_cuxrs = saved;
  301. if (necessary != necessary_static) {
  302. toku_free(necessary);
  303. }
  304. done:;
  305. }
  306. /////////////////////////////////////////////////////////////////////////////////
  307. // This is the big enchilada. (Bring Tums.) Note that this level of abstraction
  308. // has no knowledge of the inner structure of either leafentry or msg. It makes
  309. // calls into the next lower layer (msg_xxx) which handles messages.
  310. //
  311. // NOTE: This is the only function (at least in this body of code) that modifies
  312. // a leafentry.
  313. //
  314. // Return 0 on success.
  315. // If the leafentry is destroyed it sets *new_leafentry_p to NULL.
  316. // Otehrwise the new_leafentry_p points at the new leaf entry.
  317. // As of October 2011, this function always returns 0.
  318. void
  319. toku_le_apply_msg(FT_MSG msg, // message to apply to leafentry
  320. LEAFENTRY old_leafentry, // NULL if there was no stored data.
  321. TXNID oldest_referenced_xid,
  322. GC_INFO gc_info,
  323. size_t *new_leafentry_memorysize,
  324. LEAFENTRY *new_leafentry_p,
  325. OMT *omtp,
  326. struct mempool *mp,
  327. void **maybe_free,
  328. int64_t * numbytes_delta_p) { // change in total size of key and val, not including any overhead
  329. ULE_S ule;
  330. int64_t oldnumbytes = 0;
  331. int64_t newnumbytes = 0;
  332. if (old_leafentry == NULL) {
  333. msg_init_empty_ule(&ule, msg);
  334. } else {
  335. le_unpack(&ule, old_leafentry); // otherwise unpack leafentry
  336. oldnumbytes = ule_get_innermost_numbytes(&ule);
  337. }
  338. msg_modify_ule(&ule, msg); // modify unpacked leafentry
  339. ule_simple_garbage_collection(&ule, oldest_referenced_xid, gc_info);
  340. int rval = le_pack(&ule, // create packed leafentry
  341. new_leafentry_memorysize,
  342. new_leafentry_p,
  343. omtp,
  344. mp,
  345. maybe_free);
  346. invariant_zero(rval);
  347. if (new_leafentry_p) {
  348. newnumbytes = ule_get_innermost_numbytes(&ule);
  349. }
  350. *numbytes_delta_p = newnumbytes - oldnumbytes;
  351. ule_cleanup(&ule);
  352. }
  353. bool toku_le_worth_running_garbage_collection(LEAFENTRY le, TXNID oldest_referenced_xid_known) {
  354. // Effect: Quickly determines if it's worth trying to run garbage collection on a leafentry
  355. // Return: True if it makes sense to try garbage collection, false otherwise.
  356. // Rationale: Garbage collection is likely to clean up under two circumstances:
  357. // 1.) There are multiple committed entries. Some may never be read by new txns.
  358. // 2.) There is only one committed entry, but the outermost provisional entry
  359. // is older than the oldest known referenced xid, so it must have commited.
  360. // Therefor we can promote it to committed and get rid of the old commited entry.
  361. if (le->type != LE_MVCC) {
  362. return false;
  363. }
  364. if (le->u.mvcc.num_cxrs > 1) {
  365. return true;
  366. } else {
  367. paranoid_invariant(le->u.mvcc.num_cxrs == 1);
  368. }
  369. return le->u.mvcc.num_pxrs > 0 && le_outermost_uncommitted_xid(le) < oldest_referenced_xid_known;
  370. }
  371. // Garbage collect one leaf entry, using the given OMT's.
  372. // Parameters:
  373. // -- old_leaf_entry : the leaf we intend to clean up through garbage
  374. // collecting.
  375. // -- new_leaf_entry (OUTPUT) : a pointer to the leaf entry after
  376. // garbage collection.
  377. // -- new_leaf_entry_memory_size : after this call, our leaf entry
  378. // should be empty or smaller. This number represents that and is
  379. // used in a previous call to truncate the existing size.
  380. // -- omt : the memory where our leaf entry resides.
  381. // -- mp : our memory pool.
  382. // -- maybe_free (OUTPUT) : in a previous call, we may be able to free
  383. // the memory completely, if we removed the leaf entry.
  384. // -- snapshot_xids : we use these in memory transaction ids to
  385. // determine what to garbage collect.
  386. // -- referenced_xids : list of in memory active transactions.
  387. // NOTE: it is not a good idea to garbage collect a leaf
  388. // entry with only one committed value.
  389. void
  390. toku_le_garbage_collect(LEAFENTRY old_leaf_entry,
  391. LEAFENTRY *new_leaf_entry,
  392. size_t *new_leaf_entry_memory_size,
  393. OMT *omtp,
  394. struct mempool *mp,
  395. void **maybe_free,
  396. const xid_omt_t &snapshot_xids,
  397. const rx_omt_t &referenced_xids,
  398. const xid_omt_t &live_root_txns,
  399. TXNID oldest_referenced_xid_known,
  400. int64_t * numbytes_delta_p) {
  401. ULE_S ule;
  402. int64_t oldnumbytes = 0;
  403. int64_t newnumbytes = 0;
  404. le_unpack(&ule, old_leaf_entry);
  405. oldnumbytes = ule_get_innermost_numbytes(&ule);
  406. // Before running garbage collection, try to promote the outermost provisional
  407. // entries to committed if its xid is older than the oldest possible live xid.
  408. //
  409. // The oldest known refeferenced xid is a lower bound on the oldest possible
  410. // live xid, so we use that. It's usually close enough to get rid of most
  411. // garbage in leafentries.
  412. TXNID oldest_possible_live_xid = oldest_referenced_xid_known;
  413. ule_try_promote_provisional_outermost(&ule, oldest_possible_live_xid);
  414. ule_garbage_collect(&ule, snapshot_xids, referenced_xids, live_root_txns);
  415. int r = le_pack(&ule,
  416. new_leaf_entry_memory_size,
  417. new_leaf_entry,
  418. omtp,
  419. mp,
  420. maybe_free);
  421. assert(r == 0);
  422. if (new_leaf_entry) {
  423. newnumbytes = ule_get_innermost_numbytes(&ule);
  424. }
  425. *numbytes_delta_p = newnumbytes - oldnumbytes;
  426. ule_cleanup(&ule);
  427. }
  428. /////////////////////////////////////////////////////////////////////////////////
  429. // This layer of abstraction (msg_xxx)
  430. // knows the accessors of msg, but not of leafentry or unpacked leaf entry.
  431. // It makes calls into the lower layer (le_xxx) which handles leafentries.
  432. // Purpose is to init the ule with given key and no transaction records
  433. //
  434. static void
  435. msg_init_empty_ule(ULE ule, FT_MSG msg) {
  436. uint32_t keylen = ft_msg_get_keylen(msg);
  437. void *keyp = ft_msg_get_key(msg);
  438. ule_init_empty_ule(ule, keylen, keyp);
  439. }
  440. static void
  441. update_ule_key(ULE ule, FT_MSG msg) {
  442. ule->keylen = ft_msg_get_keylen(msg);
  443. ule->keyp = ft_msg_get_key(msg);
  444. }
  445. // Purpose is to modify the unpacked leafentry in our private workspace.
  446. //
  447. static void
  448. msg_modify_ule(ULE ule, FT_MSG msg) {
  449. XIDS xids = ft_msg_get_xids(msg);
  450. invariant(xids_get_num_xids(xids) < MAX_TRANSACTION_RECORDS);
  451. enum ft_msg_type type = ft_msg_get_type(msg);
  452. if (type != FT_OPTIMIZE && type != FT_OPTIMIZE_FOR_UPGRADE) {
  453. ule_do_implicit_promotions(ule, xids);
  454. }
  455. switch (type) {
  456. case FT_INSERT_NO_OVERWRITE: {
  457. UXR old_innermost_uxr = ule_get_innermost_uxr(ule);
  458. //If something exists, quit (no overwrite).
  459. if (uxr_is_insert(old_innermost_uxr)) break;
  460. //else it is just an insert, so
  461. //fall through to FT_INSERT on purpose.
  462. }
  463. case FT_INSERT: {
  464. // even though the keys of the ule and the msg should techinically
  465. // be the same as far as comparison goes, tickets #4618 and #4631
  466. // show why this is necessary. We need to update the key with the exact
  467. // bytes of the message
  468. update_ule_key(ule, msg);
  469. uint32_t vallen = ft_msg_get_vallen(msg);
  470. invariant(IS_VALID_LEN(vallen));
  471. void * valp = ft_msg_get_val(msg);
  472. ule_apply_insert(ule, xids, vallen, valp);
  473. break;
  474. }
  475. case FT_DELETE_ANY:
  476. ule_apply_delete(ule, xids);
  477. break;
  478. case FT_ABORT_ANY:
  479. case FT_ABORT_BROADCAST_TXN:
  480. ule_apply_abort(ule, xids);
  481. break;
  482. case FT_COMMIT_BROADCAST_ALL:
  483. ule_apply_broadcast_commit_all(ule);
  484. break;
  485. case FT_COMMIT_ANY:
  486. case FT_COMMIT_BROADCAST_TXN:
  487. ule_apply_commit(ule, xids);
  488. break;
  489. case FT_OPTIMIZE:
  490. case FT_OPTIMIZE_FOR_UPGRADE:
  491. ule_optimize(ule, xids);
  492. break;
  493. case FT_UPDATE:
  494. case FT_UPDATE_BROADCAST_ALL:
  495. assert(false); // These messages don't get this far. Instead they get translated (in setval_fun in do_update) into FT_INSERT messages.
  496. break;
  497. default:
  498. assert(false /* illegal FT_MSG.type */);
  499. break;
  500. }
  501. }
  502. void
  503. test_msg_modify_ule(ULE ule, FT_MSG msg){
  504. msg_modify_ule(ule,msg);
  505. }
  506. static void ule_optimize(ULE ule, XIDS xids) {
  507. if (ule->num_puxrs) {
  508. TXNID uncommitted = ule->uxrs[ule->num_cuxrs].xid; // outermost uncommitted
  509. TXNID oldest_living_xid = TXNID_NONE;
  510. uint32_t num_xids = xids_get_num_xids(xids);
  511. if (num_xids > 0) {
  512. invariant(num_xids==1);
  513. oldest_living_xid = xids_get_xid(xids, 0);
  514. }
  515. if (oldest_living_xid == TXNID_NONE || uncommitted < oldest_living_xid) {
  516. ule_promote_provisional_innermost_to_committed(ule);
  517. }
  518. }
  519. }
  520. /////////////////////////////////////////////////////////////////////////////////
  521. // This layer of abstraction (le_xxx) understands the structure of the leafentry
  522. // and of the unpacked leafentry. It is the only layer that understands the
  523. // structure of leafentry. It has no knowledge of any other data structures.
  524. //
  525. //
  526. // required for every le_unpack that is done
  527. //
  528. void
  529. ule_cleanup(ULE ule) {
  530. invariant(ule->uxrs);
  531. if (ule->uxrs != ule->uxrs_static) {
  532. toku_free(ule->uxrs);
  533. ule->uxrs = NULL;
  534. }
  535. }
  536. // populate an unpacked leafentry using pointers into the given leafentry.
  537. // thus, the memory referenced by 'le' must live as long as the ULE.
  538. void
  539. le_unpack(ULE ule, LEAFENTRY le) {
  540. //Read the keylen
  541. ule->keylen = toku_dtoh32(le->keylen);
  542. uint8_t type = le->type;
  543. uint8_t *p;
  544. uint32_t i;
  545. switch (type) {
  546. case LE_CLEAN: {
  547. ule->uxrs = ule->uxrs_static; //Static version is always enough.
  548. ule->num_cuxrs = 1;
  549. ule->num_puxrs = 0;
  550. ule->keyp = le->u.clean.key_val;
  551. UXR uxr = ule->uxrs;
  552. uxr->type = XR_INSERT;
  553. uxr->vallen = toku_dtoh32(le->u.clean.vallen);
  554. uxr->valp = le->u.clean.key_val + ule->keylen;
  555. uxr->xid = TXNID_NONE;
  556. //Set p to immediately after leafentry
  557. p = le->u.clean.key_val + ule->keylen + uxr->vallen;
  558. break;
  559. }
  560. case LE_MVCC:
  561. ule->num_cuxrs = toku_dtoh32(le->u.mvcc.num_cxrs);
  562. invariant(ule->num_cuxrs);
  563. ule->num_puxrs = le->u.mvcc.num_pxrs;
  564. //Dynamic memory
  565. if (ule->num_cuxrs < MAX_TRANSACTION_RECORDS) {
  566. ule->uxrs = ule->uxrs_static;
  567. }
  568. else {
  569. XMALLOC_N(ule->num_cuxrs + 1 + MAX_TRANSACTION_RECORDS, ule->uxrs);
  570. }
  571. ule->keyp = le->u.mvcc.key_xrs;
  572. p = le->u.mvcc.key_xrs + ule->keylen;
  573. //unpack interesting TXNIDs inner to outer.
  574. if (ule->num_puxrs!=0) {
  575. UXR outermost = ule->uxrs + ule->num_cuxrs;
  576. p += uxr_unpack_txnid(outermost, p);
  577. }
  578. //unpack other TXNIDS (not for ule->uxrs[0])
  579. ule->uxrs[0].xid = TXNID_NONE; //0 for super-root is implicit
  580. for (i = 0; i < ule->num_cuxrs - 1; i++) {
  581. p += uxr_unpack_txnid(ule->uxrs + ule->num_cuxrs - 1 - i, p);
  582. }
  583. //unpack interesting lengths inner to outer.
  584. if (ule->num_puxrs!=0) {
  585. UXR innermost = ule->uxrs + ule->num_cuxrs + ule->num_puxrs - 1;
  586. p += uxr_unpack_length_and_bit(innermost, p);
  587. }
  588. for (i = 0; i < ule->num_cuxrs; i++) {
  589. p += uxr_unpack_length_and_bit(ule->uxrs + ule->num_cuxrs - 1 - i, p);
  590. }
  591. //unpack interesting values inner to outer
  592. if (ule->num_puxrs!=0) {
  593. UXR innermost = ule->uxrs + ule->num_cuxrs + ule->num_puxrs - 1;
  594. p += uxr_unpack_data(innermost, p);
  595. }
  596. for (i = 0; i < ule->num_cuxrs; i++) {
  597. p += uxr_unpack_data(ule->uxrs + ule->num_cuxrs - 1 - i, p);
  598. }
  599. //unpack provisional xrs outer to inner
  600. if (ule->num_puxrs > 1) {
  601. {
  602. //unpack length, bit, data for outermost uncommitted
  603. UXR outermost = ule->uxrs + ule->num_cuxrs;
  604. p += uxr_unpack_type_and_length(outermost, p);
  605. p += uxr_unpack_data(outermost, p);
  606. }
  607. //unpack txnid, length, bit, data for non-outermost, non-innermost
  608. for (i = ule->num_cuxrs + 1; i < ule->num_cuxrs + ule->num_puxrs - 1; i++) {
  609. UXR uxr = ule->uxrs + i;
  610. p += uxr_unpack_txnid(uxr, p);
  611. p += uxr_unpack_type_and_length(uxr, p);
  612. p += uxr_unpack_data(uxr, p);
  613. }
  614. {
  615. //Just unpack txnid for innermost
  616. UXR innermost = ule->uxrs + ule->num_cuxrs + ule->num_puxrs - 1;
  617. p += uxr_unpack_txnid(innermost, p);
  618. }
  619. }
  620. break;
  621. default:
  622. invariant(false);
  623. }
  624. #if ULE_DEBUG
  625. size_t memsize = le_memsize_from_ule(ule);
  626. assert(p == ((uint8_t*)le) + memsize);
  627. #endif
  628. }
  629. static inline size_t
  630. uxr_pack_txnid(UXR uxr, uint8_t *p) {
  631. *(TXNID*)p = toku_htod64(uxr->xid);
  632. return sizeof(TXNID);
  633. }
  634. static inline size_t
  635. uxr_pack_type_and_length(UXR uxr, uint8_t *p) {
  636. size_t rval = 1;
  637. *p = uxr->type;
  638. if (uxr_is_insert(uxr)) {
  639. *(uint32_t*)(p+1) = toku_htod32(uxr->vallen);
  640. rval += sizeof(uint32_t);
  641. }
  642. return rval;
  643. }
  644. static inline size_t
  645. uxr_pack_length_and_bit(UXR uxr, uint8_t *p) {
  646. uint32_t length_and_bit;
  647. if (uxr_is_insert(uxr)) {
  648. length_and_bit = INSERT_LENGTH(uxr->vallen);
  649. }
  650. else {
  651. length_and_bit = DELETE_LENGTH(uxr->vallen);
  652. }
  653. *(uint32_t*)p = toku_htod32(length_and_bit);
  654. return sizeof(uint32_t);
  655. }
  656. static inline size_t
  657. uxr_pack_data(UXR uxr, uint8_t *p) {
  658. if (uxr_is_insert(uxr)) {
  659. memcpy(p, uxr->valp, uxr->vallen);
  660. return uxr->vallen;
  661. }
  662. return 0;
  663. }
  664. static inline size_t
  665. uxr_unpack_txnid(UXR uxr, uint8_t *p) {
  666. uxr->xid = toku_dtoh64(*(TXNID*)p);
  667. return sizeof(TXNID);
  668. }
  669. static inline size_t
  670. uxr_unpack_type_and_length(UXR uxr, uint8_t *p) {
  671. size_t rval = 1;
  672. uxr->type = *p;
  673. if (uxr_is_insert(uxr)) {
  674. uxr->vallen = toku_dtoh32(*(uint32_t*)(p+1));
  675. rval += sizeof(uint32_t);
  676. }
  677. return rval;
  678. }
  679. static inline size_t
  680. uxr_unpack_length_and_bit(UXR uxr, uint8_t *p) {
  681. uint32_t length_and_bit = toku_dtoh32(*(uint32_t*)p);
  682. if (IS_INSERT(length_and_bit)) {
  683. uxr->type = XR_INSERT;
  684. uxr->vallen = GET_LENGTH(length_and_bit);
  685. }
  686. else {
  687. uxr->type = XR_DELETE;
  688. uxr->vallen = 0;
  689. }
  690. return sizeof(uint32_t);
  691. }
  692. static inline size_t
  693. uxr_unpack_data(UXR uxr, uint8_t *p) {
  694. if (uxr_is_insert(uxr)) {
  695. uxr->valp = p;
  696. return uxr->vallen;
  697. }
  698. return 0;
  699. }
  700. // executed too often to be worth making threadsafe
  701. static inline void
  702. update_le_status(ULE ule, size_t memsize) {
  703. if (ule->num_cuxrs > STATUS_VALUE(LE_MAX_COMMITTED_XR))
  704. STATUS_VALUE(LE_MAX_COMMITTED_XR) = ule->num_cuxrs;
  705. if (ule->num_puxrs > STATUS_VALUE(LE_MAX_PROVISIONAL_XR))
  706. STATUS_VALUE(LE_MAX_PROVISIONAL_XR) = ule->num_puxrs;
  707. if (ule->num_cuxrs > MAX_TRANSACTION_RECORDS)
  708. STATUS_VALUE(LE_EXPANDED)++;
  709. if (memsize > STATUS_VALUE(LE_MAX_MEMSIZE))
  710. STATUS_VALUE(LE_MAX_MEMSIZE) = memsize;
  711. }
  712. // Purpose is to return a newly allocated leaf entry in packed format, or
  713. // return null if leaf entry should be destroyed (if no transaction records
  714. // are for inserts).
  715. // Transaction records in packed le are stored inner to outer (first xr is innermost),
  716. // with some information extracted out of the transaction records into the header.
  717. // Transaction records in ule are stored outer to inner (uxr[0] is outermost).
  718. int
  719. le_pack(ULE ule, // data to be packed into new leafentry
  720. size_t *new_leafentry_memorysize,
  721. LEAFENTRY * const new_leafentry_p, // this is what this function creates
  722. OMT* omtp,
  723. struct mempool *mp,
  724. void **maybe_free)
  725. {
  726. invariant(ule->num_cuxrs > 0);
  727. invariant(ule->uxrs[0].xid == TXNID_NONE);
  728. int rval;
  729. size_t memsize = 0;
  730. {
  731. // The unpacked leafentry may contain no inserts anywhere on its stack.
  732. // If so, then there IS no leafentry to pack, we should return NULL
  733. // So, first we check the stack to see if there is any insert. If not,
  734. // Then we can return NULL and exit the function, otherwise, we goto
  735. // found_insert, and proceed with packing the leafentry
  736. uint32_t i;
  737. for (i = 0; i < ule->num_cuxrs + ule->num_puxrs; i++) {
  738. if (uxr_is_insert(&ule->uxrs[i])) {
  739. goto found_insert;
  740. }
  741. }
  742. *new_leafentry_p = NULL;
  743. rval = 0;
  744. goto cleanup;
  745. }
  746. found_insert:;
  747. memsize = le_memsize_from_ule(ule);
  748. LEAFENTRY new_leafentry;
  749. CAST_FROM_VOIDP(new_leafentry, le_malloc(omtp, mp, memsize, maybe_free));
  750. //Universal data
  751. new_leafentry->keylen = toku_htod32(ule->keylen);
  752. //p always points to first unused byte after leafentry we are packing
  753. uint8_t *p;
  754. invariant(ule->num_cuxrs>0);
  755. //Type specific data
  756. if (ule->num_cuxrs == 1 && ule->num_puxrs == 0) {
  757. //Pack a 'clean leafentry' (no uncommitted transactions, only one committed value)
  758. new_leafentry->type = LE_CLEAN;
  759. uint32_t vallen = ule->uxrs[0].vallen;
  760. //Store vallen
  761. new_leafentry->u.clean.vallen = toku_htod32(vallen);
  762. //Store actual key
  763. memcpy(new_leafentry->u.clean.key_val, ule->keyp, ule->keylen);
  764. //Store actual val immediately after actual key
  765. memcpy(new_leafentry->u.clean.key_val + ule->keylen, ule->uxrs[0].valp, vallen);
  766. //Set p to after leafentry
  767. p = new_leafentry->u.clean.key_val + ule->keylen + vallen;
  768. }
  769. else {
  770. uint32_t i;
  771. //Pack an 'mvcc leafentry'
  772. new_leafentry->type = LE_MVCC;
  773. new_leafentry->u.mvcc.num_cxrs = toku_htod32(ule->num_cuxrs);
  774. // invariant makes cast that follows ok, although not sure if
  775. // check should be "< MAX_TRANSACTION_RECORDS" or
  776. // "< MAX_TRANSACTION_RECORDS - 1"
  777. invariant(ule->num_puxrs < MAX_TRANSACTION_RECORDS);
  778. new_leafentry->u.mvcc.num_pxrs = (uint8_t)ule->num_puxrs;
  779. //Store actual key.
  780. memcpy(new_leafentry->u.mvcc.key_xrs, ule->keyp, ule->keylen);
  781. p = new_leafentry->u.mvcc.key_xrs + ule->keylen;
  782. //pack interesting TXNIDs inner to outer.
  783. if (ule->num_puxrs!=0) {
  784. UXR outermost = ule->uxrs + ule->num_cuxrs;
  785. p += uxr_pack_txnid(outermost, p);
  786. }
  787. //pack other TXNIDS (not for ule->uxrs[0])
  788. for (i = 0; i < ule->num_cuxrs - 1; i++) {
  789. p += uxr_pack_txnid(ule->uxrs + ule->num_cuxrs - 1 - i, p);
  790. }
  791. //pack interesting lengths inner to outer.
  792. if (ule->num_puxrs!=0) {
  793. UXR innermost = ule->uxrs + ule->num_cuxrs + ule->num_puxrs - 1;
  794. p += uxr_pack_length_and_bit(innermost, p);
  795. }
  796. for (i = 0; i < ule->num_cuxrs; i++) {
  797. p += uxr_pack_length_and_bit(ule->uxrs + ule->num_cuxrs - 1 - i, p);
  798. }
  799. //pack interesting values inner to outer
  800. if (ule->num_puxrs!=0) {
  801. UXR innermost = ule->uxrs + ule->num_cuxrs + ule->num_puxrs - 1;
  802. p += uxr_pack_data(innermost, p);
  803. }
  804. for (i = 0; i < ule->num_cuxrs; i++) {
  805. p += uxr_pack_data(ule->uxrs + ule->num_cuxrs - 1 - i, p);
  806. }
  807. //pack provisional xrs outer to inner
  808. if (ule->num_puxrs > 1) {
  809. {
  810. //pack length, bit, data for outermost uncommitted
  811. UXR outermost = ule->uxrs + ule->num_cuxrs;
  812. p += uxr_pack_type_and_length(outermost, p);
  813. p += uxr_pack_data(outermost, p);
  814. }
  815. //pack txnid, length, bit, data for non-outermost, non-innermost
  816. for (i = ule->num_cuxrs + 1; i < ule->num_cuxrs + ule->num_puxrs - 1; i++) {
  817. UXR uxr = ule->uxrs + i;
  818. p += uxr_pack_txnid(uxr, p);
  819. p += uxr_pack_type_and_length(uxr, p);
  820. p += uxr_pack_data(uxr, p);
  821. }
  822. {
  823. //Just pack txnid for innermost
  824. UXR innermost = ule->uxrs + ule->num_cuxrs + ule->num_puxrs - 1;
  825. p += uxr_pack_txnid(innermost, p);
  826. }
  827. }
  828. }
  829. //p points to first unused byte after packed leafentry
  830. size_t bytes_written;
  831. bytes_written = (size_t)p - (size_t)new_leafentry;
  832. invariant(bytes_written == memsize);
  833. #if ULE_DEBUG
  834. if (omt) { //Disable recursive debugging.
  835. size_t memsize_verify = leafentry_memsize(new_leafentry);
  836. invariant(memsize_verify == memsize);
  837. ULE_S ule_tmp;
  838. le_unpack(&ule_tmp, new_leafentry);
  839. memsize_verify = le_memsize_from_ule(&ule_tmp);
  840. invariant(memsize_verify == memsize);
  841. //Debugging code inside le_unpack will repack and verify it is the same.
  842. LEAFENTRY le_copy;
  843. int r_tmp = le_pack(&ule_tmp, &memsize_verify, &memsize_verify,
  844. &le_copy);
  845. invariant(r_tmp==0);
  846. invariant(memsize_verify == memsize);
  847. invariant(memcmp(new_leafentry, le_copy, memsize)==0);
  848. toku_free(le_copy);
  849. ule_cleanup(&ule_tmp);
  850. }
  851. #endif
  852. *new_leafentry_p = (LEAFENTRY)new_leafentry;
  853. *new_leafentry_memorysize = memsize;
  854. rval = 0;
  855. cleanup:
  856. update_le_status(ule, memsize);
  857. return rval;
  858. }
  859. //////////////////////////////////////////////////////////////////////////////////
  860. // Following functions provide convenient access to a packed leafentry.
  861. //Requires:
  862. // Leafentry that ule represents should not be destroyed (is not just all deletes)
  863. size_t
  864. le_memsize_from_ule (ULE ule) {
  865. invariant(ule->num_cuxrs);
  866. size_t rval;
  867. if (ule->num_cuxrs == 1 && ule->num_puxrs == 0) {
  868. UXR committed = ule->uxrs;
  869. invariant(uxr_is_insert(committed));
  870. rval = 1 //type
  871. +4 //keylen
  872. +4 //vallen
  873. +ule->keylen //actual key
  874. +committed->vallen; //actual val
  875. }
  876. else {
  877. rval = 1 //type
  878. +4 //num_cuxrs
  879. +1 //num_puxrs
  880. +4 //keylen
  881. +ule->keylen //actual key
  882. +4*(ule->num_cuxrs) //types+lengths for committed
  883. +8*(ule->num_cuxrs + ule->num_puxrs - 1); //txnids (excluding superroot)
  884. uint32_t i;
  885. //Count data from committed uxrs and innermost puxr
  886. for (i = 0; i < ule->num_cuxrs; i++) {
  887. UXR uxr = &ule->uxrs[i];
  888. if (uxr_is_insert(uxr)) {
  889. rval += uxr->vallen; //actual val
  890. }
  891. }
  892. if (ule->num_puxrs) {
  893. UXR uxr = ule_get_innermost_uxr(ule);
  894. if (uxr_is_insert(uxr)) {
  895. rval += uxr->vallen; //actual val
  896. }
  897. rval += 4; //type+length for innermost puxr
  898. rval += 1*(ule->num_puxrs - 1); //type for remaining puxrs.
  899. //Count data and lengths from other puxrs
  900. for (i = 0; i < ule->num_puxrs-1; i++) {
  901. uxr = &ule->uxrs[i+ule->num_cuxrs];
  902. if (uxr_is_insert(uxr)) {
  903. rval += 4 + uxr->vallen; //length plus actual val
  904. }
  905. }
  906. }
  907. }
  908. return rval;
  909. }
  910. size_t
  911. leafentry_memsize (LEAFENTRY le) {
  912. size_t rval = 0;
  913. uint32_t keylen = toku_dtoh32(le->keylen);
  914. uint8_t type = le->type;
  915. uint8_t *p;
  916. switch (type) {
  917. case LE_CLEAN: {
  918. uint32_t vallen = toku_dtoh32(le->u.clean.vallen);
  919. rval = LE_CLEAN_MEMSIZE(keylen, vallen);
  920. break;
  921. }
  922. case LE_MVCC: {
  923. UXR_S uxr;
  924. uint32_t num_cuxrs = toku_dtoh32(le->u.mvcc.num_cxrs);
  925. invariant(num_cuxrs);
  926. uint32_t num_puxrs = le->u.mvcc.num_pxrs;
  927. size_t lengths = 0;
  928. //Position p after the key.
  929. p = le->u.mvcc.key_xrs + keylen;
  930. //Skip TXNIDs
  931. if (num_puxrs!=0) {
  932. p += sizeof(TXNID);
  933. }
  934. p += (num_cuxrs-1)*sizeof(TXNID);
  935. //Retrieve interesting lengths inner to outer.
  936. if (num_puxrs!=0) {
  937. p += uxr_unpack_length_and_bit(&uxr, p);
  938. if (uxr_is_insert(&uxr)) {
  939. lengths += uxr.vallen;
  940. }
  941. }
  942. uint32_t i;
  943. for (i = 0; i < num_cuxrs; i++) {
  944. p += uxr_unpack_length_and_bit(&uxr, p);
  945. if (uxr_is_insert(&uxr)) {
  946. lengths += uxr.vallen;
  947. }
  948. }
  949. //Skip all interesting 'data'
  950. p += lengths;
  951. //unpack provisional xrs outer to inner
  952. if (num_puxrs > 1) {
  953. {
  954. p += uxr_unpack_type_and_length(&uxr, p);
  955. p += uxr_unpack_data(&uxr, p);
  956. }
  957. //unpack txnid, length, bit, data for non-outermost, non-innermost
  958. for (i = 0; i < num_puxrs - 2; i++) {
  959. p += uxr_unpack_txnid(&uxr, p);
  960. p += uxr_unpack_type_and_length(&uxr, p);
  961. p += uxr_unpack_data(&uxr, p);
  962. }
  963. {
  964. //Just unpack txnid for innermost
  965. p += uxr_unpack_txnid(&uxr, p);
  966. }
  967. }
  968. rval = (size_t)p - (size_t)le;
  969. break;
  970. }
  971. default:
  972. invariant(false);
  973. }
  974. #if ULE_DEBUG
  975. ULE_S ule;
  976. le_unpack(&ule, le);
  977. size_t slow_rval = le_memsize_from_ule(&ule);
  978. if (slow_rval!=rval) {
  979. int r = print_leafentry(stderr, le);
  980. fprintf(stderr, "\nSlow: [%" PRIu64 "] Fast: [%" PRIu64 "]\n", slow_rval, rval);
  981. invariant(r==0);
  982. }
  983. assert(slow_rval == rval);
  984. ule_cleanup(&ule);
  985. #endif
  986. return rval;
  987. }
  988. size_t
  989. leafentry_disksize (LEAFENTRY le) {
  990. return leafentry_memsize(le);
  991. }
  992. bool
  993. le_is_clean(LEAFENTRY le) {
  994. uint8_t type = le->type;
  995. uint32_t rval;
  996. switch (type) {
  997. case LE_CLEAN:
  998. rval = true;
  999. break;
  1000. case LE_MVCC:;
  1001. rval = false;
  1002. break;
  1003. default:
  1004. invariant(false);
  1005. }
  1006. return rval;
  1007. }
  1008. int le_latest_is_del(LEAFENTRY le) {
  1009. int rval;
  1010. uint32_t keylen = toku_dtoh32(le->keylen);
  1011. uint8_t type = le->type;
  1012. uint8_t *p;
  1013. switch (type) {
  1014. case LE_CLEAN: {
  1015. rval = 0;
  1016. break;
  1017. }
  1018. case LE_MVCC: {
  1019. UXR_S uxr;
  1020. uint32_t num_cuxrs = toku_dtoh32(le->u.mvcc.num_cxrs);
  1021. invariant(num_cuxrs);
  1022. uint32_t num_puxrs = le->u.mvcc.num_pxrs;
  1023. //Position p after the key.
  1024. p = le->u.mvcc.key_xrs + keylen;
  1025. //Skip TXNIDs
  1026. if (num_puxrs!=0) {
  1027. p += sizeof(TXNID);
  1028. }
  1029. p += (num_cuxrs-1)*sizeof(TXNID);
  1030. p += uxr_unpack_length_and_bit(&uxr, p);
  1031. rval = uxr_is_delete(&uxr);
  1032. break;
  1033. }
  1034. default:
  1035. invariant(false);
  1036. }
  1037. #if ULE_DEBUG
  1038. ULE_S ule;
  1039. le_unpack(&ule, le);
  1040. UXR uxr = ule_get_innermost_uxr(&ule);
  1041. int slow_rval = uxr_is_delete(uxr);
  1042. assert((rval==0) == (slow_rval==0));
  1043. ule_cleanup(&ule);
  1044. #endif
  1045. return rval;
  1046. }
  1047. //
  1048. // returns true if the outermost provisional transaction id on the leafentry's stack matches
  1049. // the outermost transaction id in xids
  1050. // It is used to determine if a broadcast commit/abort message (look in ft-ops.c) should be applied to this leafentry
  1051. // If the outermost transactions match, then the broadcast commit/abort should be applied
  1052. //
  1053. bool
  1054. le_has_xids(LEAFENTRY le, XIDS xids) {
  1055. //Read num_uxrs
  1056. uint32_t num_xids = xids_get_num_xids(xids);
  1057. invariant(num_xids > 0); //Disallow checking for having TXNID_NONE
  1058. TXNID xid = xids_get_xid(xids, 0);
  1059. invariant(xid!=TXNID_NONE);
  1060. bool rval = (le_outermost_uncommitted_xid(le) == xid);
  1061. return rval;
  1062. }
  1063. uint32_t
  1064. le_latest_keylen (LEAFENTRY le) {
  1065. uint32_t rval;
  1066. rval = le_latest_is_del(le) ? 0 : le_keylen(le);
  1067. #if ULE_DEBUG
  1068. ULE_S ule;
  1069. le_unpack(&ule, le);
  1070. UXR uxr = ule_get_innermost_uxr(&ule);
  1071. uint32_t slow_rval;
  1072. if (uxr_is_insert(uxr)) {
  1073. slow_rval = ule.keylen;
  1074. }
  1075. else {
  1076. slow_rval = 0;
  1077. }
  1078. ule_cleanup(&ule);
  1079. invariant(slow_rval == rval);
  1080. #endif
  1081. return rval;
  1082. }
  1083. void*
  1084. le_latest_val_and_len (LEAFENTRY le, uint32_t *len) {
  1085. uint32_t keylen = toku_dtoh32(le->keylen);
  1086. uint8_t type = le->type;
  1087. void *valp;
  1088. uint8_t *p;
  1089. switch (type) {
  1090. case LE_CLEAN:
  1091. *len = toku_dtoh32(le->u.clean.vallen);
  1092. valp = le->u.clean.key_val + keylen;
  1093. break;
  1094. case LE_MVCC:;
  1095. UXR_S uxr;
  1096. uint32_t num_cuxrs;
  1097. num_cuxrs = toku_dtoh32(le->u.mvcc.num_cxrs);
  1098. invariant(num_cuxrs);
  1099. uint32_t num_puxrs;
  1100. num_puxrs = le->u.mvcc.num_pxrs;
  1101. //Position p after the key.
  1102. p = le->u.mvcc.key_xrs + keylen;
  1103. //Skip TXNIDs
  1104. if (num_puxrs!=0) {
  1105. p += sizeof(TXNID);
  1106. }
  1107. p += (num_cuxrs-1)*sizeof(TXNID);
  1108. p += uxr_unpack_length_and_bit(&uxr, p);
  1109. if (uxr_is_insert(&uxr)) {
  1110. *len = uxr.vallen;
  1111. valp = p + (num_cuxrs - 1 + (num_puxrs!=0))*sizeof(uint32_t);
  1112. }
  1113. else {
  1114. *len = 0;
  1115. valp = NULL;
  1116. }
  1117. break;
  1118. default:
  1119. invariant(false);
  1120. }
  1121. #if ULE_DEBUG
  1122. ULE_S ule;
  1123. le_unpack(&ule, le);
  1124. UXR uxr = ule_get_innermost_uxr(&ule);
  1125. void *slow_valp;
  1126. uint32_t slow_len;
  1127. if (uxr_is_insert(uxr)) {
  1128. slow_valp = uxr->valp;
  1129. slow_len = uxr->vallen;
  1130. }
  1131. else {
  1132. slow_valp = NULL;
  1133. slow_len = 0;
  1134. }
  1135. assert(slow_valp == le_latest_val(le));
  1136. assert(slow_len == le_latest_vallen(le));
  1137. assert(valp==slow_valp);
  1138. assert(*len==slow_len);
  1139. ule_cleanup(&ule);
  1140. #endif
  1141. return valp;
  1142. }
  1143. //DEBUG ONLY can be slow
  1144. void*
  1145. le_latest_val (LEAFENTRY le) {
  1146. ULE_S ule;
  1147. le_unpack(&ule, le);
  1148. UXR uxr = ule_get_innermost_uxr(&ule);
  1149. void *slow_rval;
  1150. if (uxr_is_insert(uxr))
  1151. slow_rval = uxr->valp;
  1152. else
  1153. slow_rval = NULL;
  1154. ule_cleanup(&ule);
  1155. return slow_rval;
  1156. }
  1157. //needed to be fast for statistics.
  1158. uint32_t
  1159. le_latest_vallen (LEAFENTRY le) {
  1160. uint32_t rval;
  1161. uint32_t keylen = toku_dtoh32(le->keylen);
  1162. uint8_t type = le->type;
  1163. uint8_t *p;
  1164. switch (type) {
  1165. case LE_CLEAN:
  1166. rval = toku_dtoh32(le->u.clean.vallen);
  1167. break;
  1168. case LE_MVCC:;
  1169. UXR_S uxr;
  1170. uint32_t num_cuxrs;
  1171. num_cuxrs = toku_dtoh32(le->u.mvcc.num_cxrs);
  1172. invariant(num_cuxrs);
  1173. uint32_t num_puxrs;
  1174. num_puxrs = le->u.mvcc.num_pxrs;
  1175. //Position p after the key.
  1176. p = le->u.mvcc.key_xrs + keylen;
  1177. //Skip TXNIDs
  1178. if (num_puxrs!=0) {
  1179. p += sizeof(TXNID);
  1180. }
  1181. p += (num_cuxrs-1)*sizeof(TXNID);
  1182. uxr_unpack_length_and_bit(&uxr, p);
  1183. if (uxr_is_insert(&uxr)) {
  1184. rval = uxr.vallen;
  1185. }
  1186. else {
  1187. rval = 0;
  1188. }
  1189. break;
  1190. default:
  1191. invariant(false);
  1192. }
  1193. #if ULE_DEBUG
  1194. ULE_S ule;
  1195. le_unpack(&ule, le);
  1196. UXR uxr = ule_get_innermost_uxr(&ule);
  1197. uint32_t slow_rval;
  1198. if (uxr_is_insert(uxr))
  1199. slow_rval = uxr->vallen;
  1200. else
  1201. slow_rval = 0;
  1202. ule_cleanup(&ule);
  1203. invariant(slow_rval == rval);
  1204. #endif
  1205. return rval;
  1206. }
  1207. //Return key and keylen unconditionally
  1208. void*
  1209. le_key_and_len (LEAFENTRY le, uint32_t *len) {
  1210. *len = toku_dtoh32(le->keylen);
  1211. uint8_t type = le->type;
  1212. void *keyp;
  1213. switch (type) {
  1214. case LE_CLEAN:
  1215. keyp = le->u.clean.key_val;
  1216. break;
  1217. case LE_MVCC:
  1218. keyp = le->u.mvcc.key_xrs;
  1219. break;
  1220. default:
  1221. invariant(false);
  1222. }
  1223. #if ULE_DEBUG
  1224. ULE_S ule;
  1225. le_unpack(&ule, le);
  1226. void *slow_keyp;
  1227. uint32_t slow_len;
  1228. slow_keyp = ule.keyp;
  1229. slow_len = ule.keylen;
  1230. assert(slow_keyp == le_key(le));
  1231. assert(slow_len == le_keylen(le));
  1232. assert(keyp==slow_keyp);
  1233. assert(*len==slow_len);
  1234. ule_cleanup(&ule);
  1235. #endif
  1236. return keyp;
  1237. }
  1238. //WILL BE DELETED can be slow
  1239. void*
  1240. le_key (LEAFENTRY le) {
  1241. uint8_t type = le->type;
  1242. void *rval;
  1243. switch (type) {
  1244. case LE_CLEAN:
  1245. rval = le->u.clean.key_val;
  1246. break;
  1247. case LE_MVCC:
  1248. rval = le->u.mvcc.key_xrs;
  1249. break;
  1250. default:
  1251. invariant(false);
  1252. }
  1253. #if ULE_DEBUG
  1254. ULE_S ule;
  1255. le_unpack(&ule, le);
  1256. void *slow_rval = ule.keyp;
  1257. invariant(slow_rval == rval);
  1258. ule_cleanup(&ule);
  1259. #endif
  1260. return rval;
  1261. }
  1262. uint32_t
  1263. le_keylen (LEAFENTRY le) {
  1264. uint32_t rval = toku_dtoh32(le->keylen);
  1265. #if ULE_DEBUG
  1266. ULE_S ule;
  1267. le_unpack(&ule, le);
  1268. uint32_t slow_rval = ule.keylen;
  1269. assert(rval==slow_rval);
  1270. ule_cleanup(&ule);
  1271. #endif
  1272. return rval;
  1273. }
  1274. uint64_t
  1275. le_outermost_uncommitted_xid (LEAFENTRY le) {
  1276. uint64_t rval = TXNID_NONE;
  1277. uint32_t keylen = toku_dtoh32(le->keylen);
  1278. uint8_t type = le->type;
  1279. uint8_t *p;
  1280. switch (type) {
  1281. case LE_CLEAN:
  1282. break;
  1283. case LE_MVCC:;
  1284. UXR_S uxr;
  1285. uint32_t num_puxrs = le->u.mvcc.num_pxrs;
  1286. if (num_puxrs) {
  1287. p = le->u.mvcc.key_xrs + keylen;
  1288. uxr_unpack_txnid(&uxr, p);
  1289. rval = uxr.xid;
  1290. }
  1291. break;
  1292. }
  1293. #if ULE_DEBUG
  1294. ULE_S ule;
  1295. le_unpack(&ule, le);
  1296. TXNID slow_rval = 0;
  1297. if (ule.num_puxrs > 0)
  1298. slow_rval = ule.uxrs[ule.num_cuxrs].xid;
  1299. assert(rval==slow_rval);
  1300. ule_cleanup(&ule);
  1301. #endif
  1302. return rval;
  1303. }
  1304. //Optimization not required. This is a debug only function.
  1305. //Print a leafentry out in human-readable format
  1306. int
  1307. print_leafentry (FILE *outf, LEAFENTRY le) {
  1308. ULE_S ule;
  1309. le_unpack(&ule, le);
  1310. uint32_t i;
  1311. invariant(ule.num_cuxrs > 0);
  1312. UXR uxr;
  1313. if (!le) { printf("NULL"); return 0; }
  1314. fprintf(outf, "{key=");
  1315. toku_print_BYTESTRING(outf, ule.keylen, (char *) ule.keyp);
  1316. for (i = 0; i < ule.num_cuxrs+ule.num_puxrs; i++) {
  1317. // fprintf(outf, "\n%*s", i+1, " "); //Nested indenting
  1318. uxr = &ule.uxrs[i];
  1319. char prov = i < ule.num_cuxrs ? 'c' : 'p';
  1320. fprintf(outf, " ");
  1321. if (uxr_is_placeholder(uxr))
  1322. fprintf(outf, "P: xid=%016" PRIx64, uxr->xid);
  1323. else if (uxr_is_delete(uxr))
  1324. fprintf(outf, "%cD: xid=%016" PRIx64, prov, uxr->xid);
  1325. else {
  1326. assert(uxr_is_insert(uxr));
  1327. fprintf(outf, "%cI: xid=%016" PRIx64 " val=", prov, uxr->xid);
  1328. toku_print_BYTESTRING(outf, uxr->vallen, (char *) uxr->valp);
  1329. }
  1330. }
  1331. fprintf(outf, "}");
  1332. ule_cleanup(&ule);
  1333. return 0;
  1334. }
  1335. /////////////////////////////////////////////////////////////////////////////////
  1336. // This layer of abstraction (ule_xxx) knows the structure of the unpacked
  1337. // leafentry and no other structure.
  1338. //
  1339. // ule constructor
  1340. // Note that transaction 0 is explicit in the ule
  1341. static void
  1342. ule_init_empty_ule(ULE ule, uint32_t keylen, void * keyp) {
  1343. ule->num_cuxrs = 1;
  1344. ule->num_puxrs = 0;
  1345. ule->keylen = keylen;
  1346. ule->keyp = keyp;
  1347. ule->uxrs = ule->uxrs_static;
  1348. ule->uxrs[0] = committed_delete;
  1349. }
  1350. static inline int32_t
  1351. min_i32(int32_t a, int32_t b) {
  1352. int32_t rval = a < b ? a : b;
  1353. return rval;
  1354. }
  1355. ///////////////////
  1356. // Implicit promotion logic:
  1357. //
  1358. // If the leafentry has already been promoted, there is nothing to do.
  1359. // We have two transaction stacks (one from message, one from leaf entry).
  1360. // We want to implicitly promote transactions newer than (but not including)
  1361. // the innermost common ancestor (ICA) of the two stacks of transaction ids. We
  1362. // know that this is the right thing to do because each transaction with an id
  1363. // greater (later) than the ICA must have been either committed or aborted.
  1364. // If it was aborted then we would have seen an abort message and removed the
  1365. // xid from the stack of transaction records. So any transaction still on the
  1366. // leaf entry stack must have been successfully promoted.
  1367. //
  1368. // After finding the ICA, promote transaction later than the ICA by copying
  1369. // value and type from innermost transaction record of leafentry to transaction
  1370. // record of ICA, keeping the transaction id of the ICA.
  1371. // Outermost xid is zero for both ule and xids<>
  1372. //
  1373. static void
  1374. ule_do_implicit_promotions(ULE ule, XIDS xids) {
  1375. //Optimization for (most) common case.
  1376. //No commits necessary if everything is already committed.
  1377. if (ule->num_puxrs > 0) {
  1378. int num_xids = xids_get_num_xids(xids);
  1379. invariant(num_xids>0);
  1380. uint32_t max_index = ule->num_cuxrs + min_i32(ule->num_puxrs, num_xids) - 1;
  1381. uint32_t ica_index = max_index;
  1382. uint32_t index;
  1383. for (index = ule->num_cuxrs; index <= max_index; index++) {
  1384. TXNID current_msg_xid = xids_get_xid(xids, index - ule->num_cuxrs);
  1385. TXNID current_ule_xid = ule_get_xid(ule, index);
  1386. if (current_msg_xid != current_ule_xid) {
  1387. //ica is innermost transaction with matching xids.
  1388. ica_index = index - 1;
  1389. break;
  1390. }
  1391. }
  1392. if (ica_index < ule->num_cuxrs) {
  1393. invariant(ica_index == ule->num_cuxrs - 1);
  1394. ule_promote_provisional_innermost_to_committed(ule);
  1395. }
  1396. else if (ica_index < ule->num_cuxrs + ule->num_puxrs - 1) {
  1397. //If ica is the innermost uxr in the leafentry, no commits are necessary.
  1398. ule_promote_provisional_innermost_to_index(ule, ica_index);
  1399. }
  1400. }
  1401. }
  1402. static void
  1403. ule_promote_provisional_innermost_to_committed(ULE ule) {
  1404. //Must be something to promote.
  1405. invariant(ule->num_puxrs);
  1406. //Take value (or delete flag) from innermost.
  1407. //Take TXNID from outermost uncommitted txn
  1408. //"Delete" provisional stack
  1409. //add one UXR that is committed using saved TXNID,val/delete flag
  1410. UXR old_innermost_uxr = ule_get_innermost_uxr(ule);
  1411. assert(!uxr_is_placeholder(old_innermost_uxr));
  1412. UXR old_outermost_uncommitted_uxr = &ule->uxrs[ule->num_cuxrs];
  1413. ule->num_puxrs = 0; //Discard all provisional uxrs.
  1414. if (uxr_is_delete(old_innermost_uxr)) {
  1415. ule_push_delete_uxr(ule, true, old_outermost_uncommitted_uxr->xid);
  1416. }
  1417. else {
  1418. ule_push_insert_uxr(ule, true,
  1419. old_outermost_uncommitted_uxr->xid,
  1420. old_innermost_uxr->vallen,
  1421. old_innermost_uxr->valp);
  1422. }
  1423. }
  1424. static void
  1425. ule_try_promote_provisional_outermost(ULE ule, TXNID oldest_possible_live_xid) {
  1426. // Effect: If there is a provisional record whose outermost xid is older than
  1427. // the oldest known referenced_xid, promote it to committed.
  1428. if (ule->num_puxrs > 0 && ule_get_xid(ule, ule->num_cuxrs) < oldest_possible_live_xid) {
  1429. ule_promote_provisional_innermost_to_committed(ule);
  1430. }
  1431. }
  1432. // Purpose is to promote the value (and type) of the innermost transaction
  1433. // record to the uxr at the specified index (keeping the txnid of the uxr at
  1434. // specified index.)
  1435. static void
  1436. ule_promote_provisional_innermost_to_index(ULE ule, uint32_t index) {
  1437. //Must not promote to committed portion of stack.
  1438. invariant(index >= ule->num_cuxrs);
  1439. //Must actually be promoting.
  1440. invariant(index < ule->num_cuxrs + ule->num_puxrs - 1);
  1441. UXR old_innermost_uxr = ule_get_innermost_uxr(ule);
  1442. assert(!uxr_is_placeholder(old_innermost_uxr));
  1443. TXNID new_innermost_xid = ule->uxrs[index].xid;
  1444. ule->num_puxrs = index - ule->num_cuxrs; //Discard old uxr at index (and everything inner)
  1445. if (uxr_is_delete(old_innermost_uxr)) {
  1446. ule_push_delete_uxr(ule, false, new_innermost_xid);
  1447. }
  1448. else {
  1449. ule_push_insert_uxr(ule, false,
  1450. new_innermost_xid,
  1451. old_innermost_uxr->vallen,
  1452. old_innermost_uxr->valp);
  1453. }
  1454. }
  1455. ///////////////////
  1456. // All ule_apply_xxx operations are done after implicit promotions,
  1457. // so the innermost transaction record in the leafentry is the ICA.
  1458. //
  1459. // Purpose is to apply an insert message to this leafentry:
  1460. static void
  1461. ule_apply_insert(ULE ule, XIDS xids, uint32_t vallen, void * valp) {
  1462. ule_prepare_for_new_uxr(ule, xids);
  1463. TXNID this_xid = xids_get_innermost_xid(xids); // xid of transaction doing this insert
  1464. ule_push_insert_uxr(ule, this_xid == TXNID_NONE, this_xid, vallen, valp);
  1465. }
  1466. // Purpose is to apply a delete message to this leafentry:
  1467. static void
  1468. ule_apply_delete(ULE ule, XIDS xids) {
  1469. ule_prepare_for_new_uxr(ule, xids);
  1470. TXNID this_xid = xids_get_innermost_xid(xids); // xid of transaction doing this delete
  1471. ule_push_delete_uxr(ule, this_xid == TXNID_NONE, this_xid);
  1472. }
  1473. // First, discard anything done earlier by this transaction.
  1474. // Then, add placeholders if necessary. This transaction may be nested within
  1475. // outer transactions that are newer than then newest (innermost) transaction in
  1476. // the leafentry. If so, record those outer transactions in the leafentry
  1477. // with placeholders.
  1478. static void
  1479. ule_prepare_for_new_uxr(ULE ule, XIDS xids) {
  1480. TXNID this_xid = xids_get_innermost_xid(xids);
  1481. //This is for LOADER_USE_PUTS or transactionless environment
  1482. //where messages use XIDS of 0
  1483. if (this_xid == TXNID_NONE && ule_get_innermost_xid(ule) == TXNID_NONE) {
  1484. ule_remove_innermost_uxr(ule);
  1485. }
  1486. // case where we are transactional and xids stack matches ule stack
  1487. else if (ule->num_puxrs > 0 && ule_get_innermost_xid(ule) == this_xid) {
  1488. ule_remove_innermost_uxr(ule);
  1489. }
  1490. // case where we are transactional and xids stack does not match ule stack
  1491. else {
  1492. ule_add_placeholders(ule, xids);
  1493. }
  1494. }
  1495. // Purpose is to apply an abort message to this leafentry.
  1496. // If the aborted transaction (the transaction whose xid is the innermost xid
  1497. // in the id stack passed in the message), has not modified this leafentry,
  1498. // then there is nothing to be done.
  1499. // If this transaction did modify the leafentry, then undo whatever it did (by
  1500. // removing the transaction record (uxr) and any placeholders underneath.
  1501. // Remember, the innermost uxr can only be an insert or a delete, not a placeholder.
  1502. static void
  1503. ule_apply_abort(ULE ule, XIDS xids) {
  1504. TXNID this_xid = xids_get_innermost_xid(xids); // xid of transaction doing this abort
  1505. invariant(this_xid!=TXNID_NONE);
  1506. UXR innermost = ule_get_innermost_uxr(ule);
  1507. // need to check for provisional entries in ule, otherwise
  1508. // there is nothing to abort, not checking this may result
  1509. // in a bug where the most recently committed has same xid
  1510. // as the XID's innermost
  1511. if (ule->num_puxrs > 0 && innermost->xid == this_xid) {
  1512. invariant(ule->num_puxrs>0);
  1513. ule_remove_innermost_uxr(ule);
  1514. ule_remove_innermost_placeholders(ule);
  1515. }
  1516. invariant(ule->num_cuxrs > 0);
  1517. }
  1518. static void
  1519. ule_apply_broadcast_commit_all (ULE ule) {
  1520. ule->uxrs[0] = ule->uxrs[ule->num_puxrs + ule->num_cuxrs - 1];
  1521. ule->uxrs[0].xid = TXNID_NONE;
  1522. ule->num_puxrs = 0;
  1523. ule->num_cuxrs = 1;
  1524. }
  1525. // Purpose is to apply a commit message to this leafentry.
  1526. // If the committed transaction (the transaction whose xid is the innermost xid
  1527. // in the id stack passed in the message), has not modified this leafentry,
  1528. // then there is nothing to be done.
  1529. // Also, if there are no uncommitted transaction records there is nothing to do.
  1530. // If this transaction did modify the leafentry, then promote whatever it did.
  1531. // Remember, the innermost uxr can only be an insert or a delete, not a placeholder.
  1532. void ule_apply_commit(ULE ule, XIDS xids) {
  1533. TXNID this_xid = xids_get_innermost_xid(xids); // xid of transaction committing
  1534. invariant(this_xid!=TXNID_NONE);
  1535. // need to check for provisional entries in ule, otherwise
  1536. // there is nothing to abort, not checking this may result
  1537. // in a bug where the most recently committed has same xid
  1538. // as the XID's innermost
  1539. if (ule->num_puxrs > 0 && ule_get_innermost_xid(ule) == this_xid) {
  1540. // 3 cases:
  1541. //1- it's already a committed value (do nothing) (num_puxrs==0)
  1542. //2- it's provisional but root level (make a new committed value (num_puxrs==1)
  1543. //3- it's provisional and not root (promote); (num_puxrs>1)
  1544. if (ule->num_puxrs == 1) { //new committed value
  1545. ule_promote_provisional_innermost_to_committed(ule);
  1546. }
  1547. else if (ule->num_puxrs > 1) {
  1548. //ule->uxrs[ule->num_cuxrs+ule->num_puxrs-1] is the innermost (this transaction)
  1549. //ule->uxrs[ule->num_cuxrs+ule->num_puxrs-2] is the 2nd innermost
  1550. //We want to promote the innermost uxr one level out.
  1551. ule_promote_provisional_innermost_to_index(ule, ule->num_cuxrs+ule->num_puxrs-2);
  1552. }
  1553. }
  1554. }
  1555. ///////////////////
  1556. // Helper functions called from the functions above:
  1557. //
  1558. // Purpose is to record an insert for this transaction (and set type correctly).
  1559. static void
  1560. ule_push_insert_uxr(ULE ule, bool is_committed, TXNID xid, uint32_t vallen, void * valp) {
  1561. UXR uxr = ule_get_first_empty_uxr(ule);
  1562. if (is_committed) {
  1563. invariant(ule->num_puxrs==0);
  1564. ule->num_cuxrs++;
  1565. }
  1566. else {
  1567. ule->num_puxrs++;
  1568. }
  1569. uxr->xid = xid;
  1570. uxr->vallen = vallen;
  1571. uxr->valp = valp;
  1572. uxr->type = XR_INSERT;
  1573. }
  1574. // Purpose is to record a delete for this transaction. If this transaction
  1575. // is the root transaction, then truly delete the leafentry by marking the
  1576. // ule as empty.
  1577. static void
  1578. ule_push_delete_uxr(ULE ule, bool is_committed, TXNID xid) {
  1579. UXR uxr = ule_get_first_empty_uxr(ule);
  1580. if (is_committed) {
  1581. invariant(ule->num_puxrs==0);
  1582. ule->num_cuxrs++;
  1583. }
  1584. else {
  1585. ule->num_puxrs++;
  1586. }
  1587. uxr->xid = xid;
  1588. uxr->type = XR_DELETE;
  1589. }
  1590. // Purpose is to push a placeholder on the top of the leafentry's transaction stack.
  1591. static void
  1592. ule_push_placeholder_uxr(ULE ule, TXNID xid) {
  1593. invariant(ule->num_cuxrs>0);
  1594. UXR uxr = ule_get_first_empty_uxr(ule);
  1595. uxr->xid = xid;
  1596. uxr->type = XR_PLACEHOLDER;
  1597. ule->num_puxrs++;
  1598. }
  1599. // Return innermost transaction record.
  1600. static UXR
  1601. ule_get_innermost_uxr(ULE ule) {
  1602. invariant(ule->num_cuxrs > 0);
  1603. UXR rval = &(ule->uxrs[ule->num_cuxrs + ule->num_puxrs - 1]);
  1604. return rval;
  1605. }
  1606. // Return first empty transaction record
  1607. static UXR
  1608. ule_get_first_empty_uxr(ULE ule) {
  1609. invariant(ule->num_puxrs < MAX_TRANSACTION_RECORDS-1);
  1610. UXR rval = &(ule->uxrs[ule->num_cuxrs+ule->num_puxrs]);
  1611. return rval;
  1612. }
  1613. // Remove the innermost transaction (pop the leafentry's stack), undoing
  1614. // whatever the innermost transaction did.
  1615. static void
  1616. ule_remove_innermost_uxr(ULE ule) {
  1617. //It is possible to remove the committed delete at first insert.
  1618. invariant(ule->num_cuxrs > 0);
  1619. if (ule->num_puxrs) {
  1620. ule->num_puxrs--;
  1621. }
  1622. else {
  1623. //This is for LOADER_USE_PUTS or transactionless environment
  1624. //where messages use XIDS of 0
  1625. invariant(ule->num_cuxrs == 1);
  1626. invariant(ule_get_innermost_xid(ule)==TXNID_NONE);
  1627. ule->num_cuxrs--;
  1628. }
  1629. }
  1630. static TXNID
  1631. ule_get_innermost_xid(ULE ule) {
  1632. TXNID rval = ule_get_xid(ule, ule->num_cuxrs + ule->num_puxrs - 1);
  1633. return rval;
  1634. }
  1635. static TXNID
  1636. ule_get_xid(ULE ule, uint32_t index) {
  1637. invariant(index < ule->num_cuxrs + ule->num_puxrs);
  1638. TXNID rval = ule->uxrs[index].xid;
  1639. return rval;
  1640. }
  1641. // Purpose is to remove any placeholders from the top of the leaf stack (the
  1642. // innermost recorded transactions), if necessary. This function is idempotent.
  1643. // It makes no logical sense for a placeholder to be the innermost recorded
  1644. // transaction record, so placeholders at the top of the stack are not legal.
  1645. static void
  1646. ule_remove_innermost_placeholders(ULE ule) {
  1647. UXR uxr = ule_get_innermost_uxr(ule);
  1648. while (uxr_is_placeholder(uxr)) {
  1649. invariant(ule->num_puxrs>0);
  1650. ule_remove_innermost_uxr(ule);
  1651. uxr = ule_get_innermost_uxr(ule);
  1652. }
  1653. }
  1654. // Purpose is to add placeholders to the top of the leaf stack (the innermost
  1655. // recorded transactions), if necessary. This function is idempotent.
  1656. // Note, after placeholders are added, an insert or delete will be added. This
  1657. // function temporarily leaves the transaction stack in an illegal state (having
  1658. // placeholders on top).
  1659. static void
  1660. ule_add_placeholders(ULE ule, XIDS xids) {
  1661. //Placeholders can be placed on top of the committed uxr.
  1662. invariant(ule->num_cuxrs > 0);
  1663. uint32_t num_xids = xids_get_num_xids(xids);
  1664. // we assume that implicit promotion has happened
  1665. // when we get this call, so the number of xids MUST
  1666. // be greater than the number of provisional entries
  1667. invariant(num_xids >= ule->num_puxrs);
  1668. // make sure that the xids stack matches up to a certain amount
  1669. // this first for loop is just debug code
  1670. for (uint32_t i = 0; i < ule->num_puxrs; i++) {
  1671. TXNID current_msg_xid = xids_get_xid(xids, i);
  1672. TXNID current_ule_xid = ule_get_xid(ule, i + ule->num_cuxrs);
  1673. invariant(current_msg_xid == current_ule_xid);
  1674. }
  1675. for (uint32_t i = ule->num_puxrs; i < num_xids-1; i++) {
  1676. TXNID current_msg_xid = xids_get_xid(xids, i);
  1677. ule_push_placeholder_uxr(ule, current_msg_xid);
  1678. }
  1679. }
  1680. uint64_t
  1681. ule_num_uxrs(ULE ule) {
  1682. return ule->num_cuxrs + ule->num_puxrs;
  1683. }
  1684. UXR
  1685. ule_get_uxr(ULE ule, uint64_t ith) {
  1686. invariant(ith < ule_num_uxrs(ule));
  1687. return &ule->uxrs[ith];
  1688. }
  1689. uint32_t
  1690. ule_get_num_committed(ULE ule) {
  1691. return ule->num_cuxrs;
  1692. }
  1693. uint32_t
  1694. ule_get_num_provisional(ULE ule) {
  1695. return ule->num_puxrs;
  1696. }
  1697. int
  1698. ule_is_committed(ULE ule, uint64_t ith) {
  1699. invariant(ith < ule_num_uxrs(ule));
  1700. return ith < ule->num_cuxrs;
  1701. }
  1702. int
  1703. ule_is_provisional(ULE ule, uint64_t ith) {
  1704. invariant(ith < ule_num_uxrs(ule));
  1705. return ith >= ule->num_cuxrs;
  1706. }
  1707. void *
  1708. ule_get_key(ULE ule) {
  1709. return ule->keyp;
  1710. }
  1711. uint32_t
  1712. ule_get_keylen(ULE ule) {
  1713. return ule->keylen;
  1714. }
  1715. // return size of data for innermost uxr, size of key plus size of val
  1716. uint32_t
  1717. ule_get_innermost_numbytes(ULE ule) {
  1718. uint32_t rval;
  1719. UXR uxr = ule_get_innermost_uxr(ule);
  1720. if (uxr_is_delete(uxr))
  1721. rval = 0;
  1722. else {
  1723. rval = uxr_get_vallen(uxr);
  1724. rval += ule_get_keylen(ule);
  1725. }
  1726. return rval;
  1727. }
  1728. /////////////////////////////////////////////////////////////////////////////////
  1729. // This layer of abstraction (uxr_xxx) understands uxr and nothing else.
  1730. //
  1731. static inline bool
  1732. uxr_type_is_insert(uint8_t type) {
  1733. bool rval = (bool)(type == XR_INSERT);
  1734. return rval;
  1735. }
  1736. bool
  1737. uxr_is_insert(UXR uxr) {
  1738. return uxr_type_is_insert(uxr->type);
  1739. }
  1740. static inline bool
  1741. uxr_type_is_delete(uint8_t type) {
  1742. bool rval = (bool)(type == XR_DELETE);
  1743. return rval;
  1744. }
  1745. bool
  1746. uxr_is_delete(UXR uxr) {
  1747. return uxr_type_is_delete(uxr->type);
  1748. }
  1749. static inline bool
  1750. uxr_type_is_placeholder(uint8_t type) {
  1751. bool rval = (bool)(type == XR_PLACEHOLDER);
  1752. return rval;
  1753. }
  1754. bool
  1755. uxr_is_placeholder(UXR uxr) {
  1756. return uxr_type_is_placeholder(uxr->type);
  1757. }
  1758. void *
  1759. uxr_get_val(UXR uxr) {
  1760. return uxr->valp;
  1761. }
  1762. uint32_t
  1763. uxr_get_vallen(UXR uxr) {
  1764. return uxr->vallen;
  1765. }
  1766. TXNID
  1767. uxr_get_txnid(UXR uxr) {
  1768. return uxr->xid;
  1769. }
  1770. static int
  1771. le_iterate_get_accepted_index(TXNID *xids, uint32_t *index, uint32_t num_xids, LE_ITERATE_CALLBACK f, TOKUTXN context) {
  1772. uint32_t i;
  1773. int r = 0;
  1774. // if this for loop does not return anything, we return num_xids-1, which should map to T_0
  1775. for (i = 0; i < num_xids - 1; i++) {
  1776. TXNID xid = toku_dtoh64(xids[i]);
  1777. r = f(xid, context);
  1778. if (r==TOKUDB_ACCEPT) {
  1779. r = 0;
  1780. break; //or goto something
  1781. }
  1782. else if (r!=0) {
  1783. break;
  1784. }
  1785. }
  1786. *index = i;
  1787. return r;
  1788. }
  1789. #if ULE_DEBUG
  1790. static void
  1791. ule_verify_xids(ULE ule, uint32_t interesting, TXNID *xids) {
  1792. int has_p = (ule->num_puxrs != 0);
  1793. invariant(ule->num_cuxrs + has_p == interesting);
  1794. uint32_t i;
  1795. for (i = 0; i < interesting - 1; i++) {
  1796. TXNID xid = toku_dtoh64(xids[i]);
  1797. invariant(ule->uxrs[ule->num_cuxrs - 1 + has_p - i].xid == xid);
  1798. }
  1799. }
  1800. #endif
  1801. //
  1802. // Iterates over "possible" TXNIDs in a leafentry's stack, until one is accepted by 'f'. If the value
  1803. // associated with the accepted TXNID is not an insert, then set *is_emptyp to true, otherwise false
  1804. // The "possible" TXNIDs are:
  1805. // if provisionals exist, then the first possible TXNID is the outermost provisional.
  1806. // The next possible TXNIDs are the committed TXNIDs, from most recently committed to T_0.
  1807. // If provisionals exist, and the outermost provisional is accepted by 'f',
  1808. // the associated value checked is the innermost provisional's value.
  1809. // Parameters:
  1810. // le - leafentry to iterate over
  1811. // f - callback function that checks if a TXNID in le is accepted, and its associated value should be examined.
  1812. // is_delp - output parameter that returns answer
  1813. // context - parameter for f
  1814. //
  1815. int
  1816. le_iterate_is_del(LEAFENTRY le, LE_ITERATE_CALLBACK f, bool *is_delp, TOKUTXN context) {
  1817. #if ULE_DEBUG
  1818. ULE_S ule;
  1819. le_unpack(&ule, le);
  1820. #endif
  1821. //Read the keylen
  1822. uint8_t type = le->type;
  1823. int r;
  1824. bool is_del = false;
  1825. switch (type) {
  1826. case LE_CLEAN: {
  1827. r = 0;
  1828. #if ULE_DEBUG
  1829. invariant(ule.num_cuxrs == 1);
  1830. invariant(ule.num_puxrs == 0);
  1831. invariant(uxr_is_insert(ule.uxrs));
  1832. #endif
  1833. break;
  1834. }
  1835. case LE_MVCC:;
  1836. uint32_t keylen;
  1837. keylen = toku_dtoh32(le->keylen);
  1838. uint32_t num_cuxrs;
  1839. num_cuxrs = toku_dtoh32(le->u.mvcc.num_cxrs);
  1840. uint32_t num_puxrs;
  1841. num_puxrs = le->u.mvcc.num_pxrs;
  1842. uint8_t *p;
  1843. p = le->u.mvcc.key_xrs + keylen;
  1844. uint32_t index;
  1845. uint32_t num_interesting;
  1846. num_interesting = num_cuxrs + (num_puxrs != 0);
  1847. TXNID *xids;
  1848. xids = (TXNID*)p;
  1849. #if ULE_DEBUG
  1850. ule_verify_xids(&ule, num_interesting, xids);
  1851. #endif
  1852. r = le_iterate_get_accepted_index(xids, &index, num_interesting, f, context);
  1853. if (r!=0) goto cleanup;
  1854. invariant(index < num_interesting);
  1855. //Skip TXNIDs
  1856. p += (num_interesting - 1)*sizeof(TXNID);
  1857. uint32_t *length_and_bits;
  1858. length_and_bits = (uint32_t*)p;
  1859. uint32_t my_length_and_bit;
  1860. my_length_and_bit = toku_dtoh32(length_and_bits[index]);
  1861. is_del = !IS_INSERT(my_length_and_bit);
  1862. #if ULE_DEBUG
  1863. {
  1864. uint32_t has_p = (ule.num_puxrs != 0);
  1865. uint32_t ule_index = (index==0) ? ule.num_cuxrs + ule.num_puxrs - 1 : ule.num_cuxrs - 1 + has_p - index;
  1866. UXR uxr = ule.uxrs + ule_index;
  1867. invariant(uxr_is_delete(uxr) == is_del);
  1868. }
  1869. #endif
  1870. break;
  1871. default:
  1872. invariant(false);
  1873. }
  1874. cleanup:
  1875. #if ULE_DEBUG
  1876. ule_cleanup(&ule);
  1877. #endif
  1878. if (!r) *is_delp = is_del;
  1879. return r;
  1880. }
  1881. //
  1882. // Iterates over "possible" TXNIDs in a leafentry's stack, until one is accepted by 'f'. Set
  1883. // valpp and vallenp to value and length associated with accepted TXNID
  1884. // The "possible" TXNIDs are:
  1885. // if provisionals exist, then the first possible TXNID is the outermost provisional.
  1886. // The next possible TXNIDs are the committed TXNIDs, from most recently committed to T_0.
  1887. // If provisionals exist, and the outermost provisional is accepted by 'f',
  1888. // the associated length value is the innermost provisional's length and value.
  1889. // Parameters:
  1890. // le - leafentry to iterate over
  1891. // f - callback function that checks if a TXNID in le is accepted, and its associated value should be examined.
  1892. // valpp - output parameter that returns pointer to value
  1893. // vallenp - output parameter that returns length of value
  1894. // context - parameter for f
  1895. //
  1896. int
  1897. le_iterate_val(LEAFENTRY le, LE_ITERATE_CALLBACK f, void** valpp, uint32_t *vallenp, TOKUTXN context) {
  1898. #if ULE_DEBUG
  1899. ULE_S ule;
  1900. le_unpack(&ule, le);
  1901. #endif
  1902. //Read the keylen
  1903. uint32_t keylen = toku_dtoh32(le->keylen);
  1904. uint8_t type = le->type;
  1905. int r;
  1906. uint32_t vallen = 0;
  1907. void *valp = NULL;
  1908. switch (type) {
  1909. case LE_CLEAN: {
  1910. vallen = toku_dtoh32(le->u.clean.vallen);
  1911. valp = le->u.clean.key_val + keylen;
  1912. r = 0;
  1913. #if ULE_DEBUG
  1914. invariant(ule.num_cuxrs == 1);
  1915. invariant(ule.num_puxrs == 0);
  1916. invariant(uxr_is_insert(ule.uxrs));
  1917. invariant(ule.uxrs[0].vallen == vallen);
  1918. invariant(ule.uxrs[0].valp == valp);
  1919. #endif
  1920. break;
  1921. }
  1922. case LE_MVCC:;
  1923. uint32_t num_cuxrs;
  1924. num_cuxrs = toku_dtoh32(le->u.mvcc.num_cxrs);
  1925. uint32_t num_puxrs;
  1926. num_puxrs = le->u.mvcc.num_pxrs;
  1927. uint8_t *p;
  1928. p = le->u.mvcc.key_xrs + keylen;
  1929. uint32_t index;
  1930. uint32_t num_interesting;
  1931. num_interesting = num_cuxrs + (num_puxrs != 0);
  1932. TXNID *xids;
  1933. xids = (TXNID*)p;
  1934. #if ULE_DEBUG
  1935. ule_verify_xids(&ule, num_interesting, xids);
  1936. #endif
  1937. r = le_iterate_get_accepted_index(xids, &index, num_interesting, f, context);
  1938. if (r!=0) goto cleanup;
  1939. invariant(index < num_interesting);
  1940. //Skip TXNIDs
  1941. p += (num_interesting - 1)*sizeof(TXNID);
  1942. UXR_S temp;
  1943. size_t offset;
  1944. offset = 0;
  1945. uint32_t *length_and_bits;
  1946. length_and_bits = (uint32_t*)p;
  1947. uint32_t i;
  1948. //evaluate the offset
  1949. for (i=0; i < index; i++){
  1950. uxr_unpack_length_and_bit(&temp, (uint8_t*)&length_and_bits[i]);
  1951. offset += temp.vallen;
  1952. }
  1953. uxr_unpack_length_and_bit(&temp, (uint8_t*)&length_and_bits[index]);
  1954. if (uxr_is_delete(&temp)) {
  1955. goto verify_is_empty;
  1956. }
  1957. vallen = temp.vallen;
  1958. // move p past the length and bits, now points to beginning of data
  1959. p += num_interesting*sizeof(uint32_t);
  1960. // move p to point to the data we care about
  1961. p += offset;
  1962. valp = p;
  1963. #if ULE_DEBUG
  1964. {
  1965. uint32_t has_p = (ule.num_puxrs != 0);
  1966. uint32_t ule_index = (index==0) ? ule.num_cuxrs + ule.num_puxrs - 1 : ule.num_cuxrs - 1 + has_p - index;
  1967. UXR uxr = ule.uxrs + ule_index;
  1968. invariant(uxr_is_insert(uxr));
  1969. invariant(uxr->vallen == vallen);
  1970. invariant(uxr->valp == valp);
  1971. }
  1972. #endif
  1973. if (0) {
  1974. verify_is_empty:;
  1975. #if ULE_DEBUG
  1976. uint32_t has_p = (ule.num_puxrs != 0);
  1977. UXR uxr = ule.uxrs + ule.num_cuxrs - 1 + has_p - index;
  1978. invariant(uxr_is_delete(uxr));
  1979. #endif
  1980. }
  1981. break;
  1982. default:
  1983. invariant(false);
  1984. }
  1985. cleanup:
  1986. #if ULE_DEBUG
  1987. ule_cleanup(&ule);
  1988. #endif
  1989. if (!r) {
  1990. *valpp = valp;
  1991. *vallenp = vallen;
  1992. }
  1993. return r;
  1994. }
  1995. #if TOKU_WINDOWS
  1996. #pragma pack(push, 1)
  1997. #endif
  1998. // This is an on-disk format. static_asserts verify everything is packed and aligned correctly.
  1999. struct __attribute__ ((__packed__)) leafentry_13 {
  2000. struct leafentry_committed_13 {
  2001. uint8_t key_val[0]; //Actual key, then actual val
  2002. };
  2003. static_assert(0 == sizeof(leafentry_committed_13), "wrong size");
  2004. static_assert(0 == __builtin_offsetof(leafentry_committed_13, key_val), "wrong offset");
  2005. struct __attribute__ ((__packed__)) leafentry_provisional_13 {
  2006. uint8_t innermost_type;
  2007. TXNID xid_outermost_uncommitted;
  2008. uint8_t key_val_xrs[0]; //Actual key,
  2009. //then actual innermost inserted val,
  2010. //then transaction records.
  2011. };
  2012. static_assert(9 == sizeof(leafentry_provisional_13), "wrong size");
  2013. static_assert(9 == __builtin_offsetof(leafentry_provisional_13, key_val_xrs), "wrong offset");
  2014. uint8_t num_xrs;
  2015. uint32_t keylen;
  2016. uint32_t innermost_inserted_vallen;
  2017. union __attribute__ ((__packed__)) {
  2018. struct leafentry_committed_13 comm;
  2019. struct leafentry_provisional_13 prov;
  2020. } u;
  2021. };
  2022. static_assert(18 == sizeof(leafentry_13), "wrong size");
  2023. static_assert(9 == __builtin_offsetof(leafentry_13, u), "wrong offset");
  2024. #if TOKU_WINDOWS
  2025. #pragma pack(pop)
  2026. #endif
  2027. //Requires:
  2028. // Leafentry that ule represents should not be destroyed (is not just all deletes)
  2029. static size_t
  2030. le_memsize_from_ule_13 (ULE ule) {
  2031. uint32_t num_uxrs = ule->num_cuxrs + ule->num_puxrs;
  2032. assert(num_uxrs);
  2033. size_t rval;
  2034. if (num_uxrs == 1) {
  2035. assert(uxr_is_insert(&ule->uxrs[0]));
  2036. rval = 1 //num_uxrs
  2037. +4 //keylen
  2038. +4 //vallen
  2039. +ule->keylen //actual key
  2040. +ule->uxrs[0].vallen; //actual val
  2041. }
  2042. else {
  2043. rval = 1 //num_uxrs
  2044. +4 //keylen
  2045. +ule->keylen //actual key
  2046. +1*num_uxrs //types
  2047. +8*(num_uxrs-1); //txnids
  2048. uint8_t i;
  2049. for (i = 0; i < num_uxrs; i++) {
  2050. UXR uxr = &ule->uxrs[i];
  2051. if (uxr_is_insert(uxr)) {
  2052. rval += 4; //vallen
  2053. rval += uxr->vallen; //actual val
  2054. }
  2055. }
  2056. }
  2057. return rval;
  2058. }
  2059. //This function is mostly copied from 4.1.1 (which is version 12, same as 13 except that only 13 is upgradable).
  2060. // Note, number of transaction records in version 13 has been replaced by separate counters in version 14 (MVCC),
  2061. // one counter for committed transaction records and one counter for provisional transaction records. When
  2062. // upgrading a version 13 le to version 14, the number of committed transaction records is always set to one (1)
  2063. // and the number of provisional transaction records is set to the original number of transaction records
  2064. // minus one. The bottom transaction record is assumed to be a committed value. (If there is no committed
  2065. // value then the bottom transaction record of version 13 is a committed delete.)
  2066. // This is the only change from the 4.1.1 code. The rest of the leafentry is read as is.
  2067. static void
  2068. le_unpack_13(ULE ule, LEAFENTRY_13 le) {
  2069. //Read num_uxrs
  2070. uint8_t num_xrs = le->num_xrs;
  2071. assert(num_xrs > 0);
  2072. ule->uxrs = ule->uxrs_static; //Static version is always enough.
  2073. ule->num_cuxrs = 1;
  2074. ule->num_puxrs = num_xrs - 1;
  2075. //Read the keylen
  2076. ule->keylen = toku_dtoh32(le->keylen);
  2077. //Read the vallen of innermost insert
  2078. uint32_t vallen_of_innermost_insert = toku_dtoh32(le->innermost_inserted_vallen);
  2079. uint8_t *p;
  2080. if (num_xrs == 1) {
  2081. //Unpack a 'committed leafentry' (No uncommitted transactions exist)
  2082. ule->keyp = le->u.comm.key_val;
  2083. ule->uxrs[0].type = XR_INSERT; //Must be or the leafentry would not exist
  2084. ule->uxrs[0].vallen = vallen_of_innermost_insert;
  2085. ule->uxrs[0].valp = &le->u.comm.key_val[ule->keylen];
  2086. ule->uxrs[0].xid = 0; //Required.
  2087. //Set p to immediately after leafentry
  2088. p = &le->u.comm.key_val[ule->keylen + vallen_of_innermost_insert];
  2089. }
  2090. else {
  2091. //Unpack a 'provisional leafentry' (Uncommitted transactions exist)
  2092. //Read in type.
  2093. uint8_t innermost_type = le->u.prov.innermost_type;
  2094. assert(!uxr_type_is_placeholder(innermost_type));
  2095. //Read in xid
  2096. TXNID xid_outermost_uncommitted = toku_dtoh64(le->u.prov.xid_outermost_uncommitted);
  2097. //Read pointer to key
  2098. ule->keyp = le->u.prov.key_val_xrs;
  2099. //Read pointer to innermost inserted val (immediately after key)
  2100. uint8_t *valp_of_innermost_insert = &le->u.prov.key_val_xrs[ule->keylen];
  2101. //Point p to immediately after 'header'
  2102. p = &le->u.prov.key_val_xrs[ule->keylen + vallen_of_innermost_insert];
  2103. bool found_innermost_insert = false;
  2104. int i; //Index in ULE.uxrs[]
  2105. //Loop inner to outer
  2106. for (i = num_xrs - 1; i >= 0; i--) {
  2107. UXR uxr = &ule->uxrs[i];
  2108. //Innermost's type is in header.
  2109. if (i < num_xrs - 1) {
  2110. //Not innermost, so load the type.
  2111. uxr->type = *p;
  2112. p += 1;
  2113. }
  2114. else {
  2115. //Innermost, load the type previously read from header
  2116. uxr->type = innermost_type;
  2117. }
  2118. //Committed txn id is implicit (0). (i==0)
  2119. //Outermost uncommitted txnid is stored in header. (i==1)
  2120. if (i > 1) {
  2121. //Not committed nor outermost uncommitted, so load the xid.
  2122. uxr->xid = toku_dtoh64(*(TXNID*)p);
  2123. p += 8;
  2124. }
  2125. else if (i == 1) {
  2126. //Outermost uncommitted, load the xid previously read from header
  2127. uxr->xid = xid_outermost_uncommitted;
  2128. }
  2129. else {
  2130. // i == 0, committed entry
  2131. uxr->xid = 0;
  2132. }
  2133. if (uxr_is_insert(uxr)) {
  2134. if (found_innermost_insert) {
  2135. //Not the innermost insert. Load vallen/valp
  2136. uxr->vallen = toku_dtoh32(*(uint32_t*)p);
  2137. p += 4;
  2138. uxr->valp = p;
  2139. p += uxr->vallen;
  2140. }
  2141. else {
  2142. //Innermost insert, load the vallen/valp previously read from header
  2143. uxr->vallen = vallen_of_innermost_insert;
  2144. uxr->valp = valp_of_innermost_insert;
  2145. found_innermost_insert = true;
  2146. }
  2147. }
  2148. }
  2149. assert(found_innermost_insert);
  2150. }
  2151. #if ULE_DEBUG
  2152. size_t memsize = le_memsize_from_ule_13(ule);
  2153. assert(p == ((uint8_t*)le) + memsize);
  2154. #endif
  2155. }
  2156. size_t
  2157. leafentry_disksize_13(LEAFENTRY_13 le) {
  2158. ULE_S ule;
  2159. le_unpack_13(&ule, le);
  2160. size_t memsize = le_memsize_from_ule_13(&ule);
  2161. ule_cleanup(&ule);
  2162. return memsize;
  2163. }
  2164. int
  2165. toku_le_upgrade_13_14(LEAFENTRY_13 old_leafentry,
  2166. size_t *new_leafentry_memorysize,
  2167. LEAFENTRY *new_leafentry_p,
  2168. OMT* omtp,
  2169. struct mempool *mp) {
  2170. ULE_S ule;
  2171. int rval;
  2172. invariant(old_leafentry);
  2173. le_unpack_13(&ule, old_leafentry);
  2174. // We used to pass NULL for omt and mempool, so that we would use
  2175. // malloc instead of a mempool. However after supporting upgrade,
  2176. // we need to use mempools and the OMT.
  2177. rval = le_pack(&ule, // create packed leafentry
  2178. new_leafentry_memorysize,
  2179. new_leafentry_p,
  2180. omtp, mp, NULL);
  2181. ule_cleanup(&ule);
  2182. return rval;
  2183. }
  2184. #include <toku_race_tools.h>
  2185. void __attribute__((__constructor__)) toku_ule_helgrind_ignore(void);
  2186. void
  2187. toku_ule_helgrind_ignore(void) {
  2188. TOKU_VALGRIND_HG_DISABLE_CHECKING(&le_status, sizeof le_status);
  2189. }
  2190. #undef STATUS_VALUE