int pack_ops(WT_SESSION *session) { int ret; { /*! [Get the packed size] */ size_t size; ret = wiredtiger_struct_size(session, &size, "iSh", 42, "hello", -3); /*! [Get the packed size] */ assert(size < 100); } { /*! [Pack fields into a buffer] */ char buf[100]; ret = wiredtiger_struct_pack( session, buf, sizeof(buf), "iSh", 42, "hello", -3); /*! [Pack fields into a buffer] */ { /*! [Unpack fields from a buffer] */ int i; char *s; short h; ret = wiredtiger_struct_unpack( session, buf, sizeof(buf), "iSh", &i, &s, &h); /*! [Unpack fields from a buffer] */ } } return (ret); }
static void pack_ops(WT_SESSION *session) { { /*! [Get the packed size] */ size_t size; error_check(wiredtiger_struct_size( session, &size, "iSh", 42, "hello", -3)); /*! [Get the packed size] */ } { /*! [Pack fields into a buffer] */ char buf[100]; error_check(wiredtiger_struct_pack( session, buf, sizeof(buf), "iSh", 42, "hello", -3)); /*! [Pack fields into a buffer] */ { /*! [Unpack fields from a buffer] */ int i; char *s; short h; error_check(wiredtiger_struct_unpack( session, buf, sizeof(buf), "iSh", &i, &s, &h)); /*! [Unpack fields from a buffer] */ } } }
int main(void) { WT_CONNECTION *conn; WT_SESSION *session; int i, j, k, ret; /* * Create a clean test directory for this run of the test program if the * environment variable isn't already set (as is done by make check). */ if (getenv("WIREDTIGER_HOME") == NULL) { home = "WT_HOME"; ret = system("rm -rf WT_HOME && mkdir WT_HOME"); } else home = NULL; /* Open a connection to the database, creating it if necessary. */ if ((ret = wiredtiger_open(home, NULL, "create", &conn)) != 0) { fprintf(stderr, "Error connecting to %s: %s\n", home == NULL ? "." : home, wiredtiger_strerror(ret)); return (EXIT_FAILURE); } /* Open a session for the current thread's work. */ if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) { fprintf(stderr, "Error opening a session on %s: %s\n", home == NULL ? "." : home, wiredtiger_strerror(ret)); return (EXIT_FAILURE); } { /*! [packing] */ size_t size; char buf[50]; ret = wiredtiger_struct_size(session, &size, "iii", 42, 1000, -9); if (size > sizeof(buf)) { /* Allocate a bigger buffer. */ } ret = wiredtiger_struct_pack(session, buf, size, "iii", 42, 1000, -9); ret = wiredtiger_struct_unpack(session, buf, size, "iii", &i, &j, &k); /*! [packing] */ } /* Note: closing the connection implicitly closes open session(s). */ if ((ret = conn->close(conn, NULL)) != 0) { fprintf(stderr, "Error closing %s: %s\n", home == NULL ? "." : home, wiredtiger_strerror(ret)); return (EXIT_FAILURE); } return (EXIT_SUCCESS); }
/* * __curjoin_pack_recno -- * Pack the given recno into a buffer; prepare an item referencing it. * */ static int __curjoin_pack_recno(WT_SESSION_IMPL *session, uint64_t r, uint8_t *buf, size_t bufsize, WT_ITEM *item) { WT_SESSION *wtsession; size_t sz; wtsession = (WT_SESSION *)session; WT_RET(wiredtiger_struct_size(wtsession, &sz, "r", r)); WT_ASSERT(session, sz < bufsize); WT_RET(wiredtiger_struct_pack(wtsession, buf, bufsize, "r", r)); item->size = sz; item->data = buf; return (0); }
int main(void) { WT_CONNECTION *conn; WT_SESSION *session; char buf[50]; size_t size; int i, j, k, ret; /* Open a connection to the database, creating it if necessary. */ if ((ret = wiredtiger_open(home, NULL, "create", &conn)) != 0) fprintf(stderr, "Error connecting to %s: %s\n", home, wiredtiger_strerror(ret)); /* Open a session for the current thread's work. */ if ((ret = conn->open_session(conn, NULL, NULL, &session)) != 0) fprintf(stderr, "Error opening a session on %s: %s\n", home, wiredtiger_strerror(ret)); /*! [packing] */ ret = wiredtiger_struct_size(session, &size, "iii", 42, 1000, -9); if (size > sizeof(buf)) { /* Allocate a bigger buffer. */ } ret = wiredtiger_struct_pack(session, buf, size, "iii", 42, 1000, -9); ret = wiredtiger_struct_unpack(session, buf, size, "iii", &i, &j, &k); /*! [packing] */ /* Note: closing the connection implicitly closes open session(s). */ if ((ret = conn->close(conn, NULL)) != 0) fprintf(stderr, "Error connecting to %s: %s\n", home, wiredtiger_strerror(ret)); return (ret); }
/* * __curlog_kv -- * Set the key and value of the log cursor to return to the user. */ static int __curlog_kv(WT_SESSION_IMPL *session, WT_CURSOR *cursor) { WT_CURSOR_LOG *cl; WT_ITEM item; uint32_t fileid, key_count, opsize, optype; cl = (WT_CURSOR_LOG *)cursor; /* * If it is a commit and we have stepped over the header, peek to get * the size and optype and read out any key/value from this operation. */ if ((key_count = cl->step_count++) > 0) { WT_RET(__wt_logop_read(session, &cl->stepp, cl->stepp_end, &optype, &opsize)); WT_RET(__curlog_op_read(session, cl, optype, opsize, &fileid)); /* Position on the beginning of the next record part. */ cl->stepp += opsize; } else { optype = WT_LOGOP_INVALID; fileid = 0; cl->opkey->data = NULL; cl->opkey->size = 0; /* * Non-commit records we want to return the record without the * header and the adjusted size. Add one to skip over the type * which is normally consumed by __wt_logrec_read. */ cl->opvalue->data = WT_LOG_SKIP_HEADER(cl->logrec->data) + 1; cl->opvalue->size = WT_LOG_REC_SIZE(cl->logrec->size) - 1; } /* * The log cursor sets the LSN and step count as the cursor key and * and log record related data in the value. The data in the value * contains any operation key/value that was in the log record. * For the special case that the caller needs the result in raw form, * we create packed versions of the key/value. */ if (FLD_ISSET(cursor->flags, WT_CURSTD_RAW)) { memset(&item, 0, sizeof(item)); WT_RET(wiredtiger_struct_size((WT_SESSION *)session, &item.size, WT_LOGC_KEY_FORMAT, cl->cur_lsn->l.file, cl->cur_lsn->l.offset, key_count)); WT_RET(__wt_realloc(session, NULL, item.size, &cl->packed_key)); item.data = cl->packed_key; WT_RET(wiredtiger_struct_pack((WT_SESSION *)session, cl->packed_key, item.size, WT_LOGC_KEY_FORMAT, cl->cur_lsn->l.file, cl->cur_lsn->l.offset, key_count)); __wt_cursor_set_key(cursor, &item); WT_RET(wiredtiger_struct_size((WT_SESSION *)session, &item.size, WT_LOGC_VALUE_FORMAT, cl->txnid, cl->rectype, optype, fileid, cl->opkey, cl->opvalue)); WT_RET(__wt_realloc(session, NULL, item.size, &cl->packed_value)); item.data = cl->packed_value; WT_RET(wiredtiger_struct_pack((WT_SESSION *)session, cl->packed_value, item.size, WT_LOGC_VALUE_FORMAT, cl->txnid, cl->rectype, optype, fileid, cl->opkey, cl->opvalue)); __wt_cursor_set_value(cursor, &item); } else { __wt_cursor_set_key(cursor, cl->cur_lsn->l.file, cl->cur_lsn->l.offset, key_count); __wt_cursor_set_value(cursor, cl->txnid, cl->rectype, optype, fileid, cl->opkey, cl->opvalue); } return (0); }