2007-11-29 14:18:54 +00:00
|
|
|
/* -*- mode: C; c-basic-offset: 4 -*- */
|
2008-01-24 15:10:32 +00:00
|
|
|
#ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved."
|
2007-11-29 14:18:54 +00:00
|
|
|
|
2013-04-16 23:57:32 -04:00
|
|
|
#include "test.h"
|
|
|
|
|
2013-04-16 23:57:20 -04:00
|
|
|
#include "includes.h"
|
2007-07-13 19:37:47 +00:00
|
|
|
|
2007-11-29 18:14:40 +00:00
|
|
|
static void test_serialize(void) {
|
2007-08-01 02:37:21 +00:00
|
|
|
// struct brt source_brt;
|
|
|
|
int nodesize = 1024;
|
|
|
|
struct brtnode sn, *dn;
|
2013-04-16 23:57:39 -04:00
|
|
|
int fd = open(__FILE__ ".brt", O_RDWR|O_CREAT|O_BINARY, S_IRWXU|S_IRWXG|S_IRWXO);
|
2007-07-13 19:37:47 +00:00
|
|
|
int r;
|
2007-11-14 17:58:38 +00:00
|
|
|
const u_int32_t randval = random();
|
2007-07-13 19:37:47 +00:00
|
|
|
assert(fd>=0);
|
|
|
|
|
2007-08-01 02:37:21 +00:00
|
|
|
// source_brt.fd=fd;
|
|
|
|
char *hello_string;
|
|
|
|
sn.nodesize = nodesize;
|
2008-06-15 17:09:14 +00:00
|
|
|
sn.ever_been_written = 0;
|
2007-11-27 15:22:56 +00:00
|
|
|
sn.flags = 0x11223344;
|
2013-04-16 23:57:18 -04:00
|
|
|
sn.thisnodename.b = 20;
|
2007-11-24 03:50:28 +00:00
|
|
|
sn.disk_lsn.lsn = 789;
|
|
|
|
sn.log_lsn.lsn = 123456;
|
2008-07-27 22:16:49 +00:00
|
|
|
sn.layout_version = BRT_LAYOUT_VERSION;
|
2007-07-13 19:37:47 +00:00
|
|
|
sn.height = 1;
|
2007-11-14 17:58:38 +00:00
|
|
|
sn.rand4fingerprint = randval;
|
|
|
|
sn.local_fingerprint = 0;
|
2007-07-13 19:37:47 +00:00
|
|
|
sn.u.n.n_children = 2;
|
2007-11-27 18:16:45 +00:00
|
|
|
hello_string = toku_strdup("hello");
|
2008-03-06 21:46:57 +00:00
|
|
|
MALLOC_N(2, sn.u.n.childinfos);
|
|
|
|
MALLOC_N(1, sn.u.n.childkeys);
|
2007-11-27 18:16:45 +00:00
|
|
|
sn.u.n.childkeys[0] = kv_pair_malloc(hello_string, 6, 0, 0);
|
2007-07-13 19:37:47 +00:00
|
|
|
sn.u.n.totalchildkeylens = 6;
|
2013-04-16 23:57:18 -04:00
|
|
|
BNC_BLOCKNUM(&sn, 0).b = 30;
|
|
|
|
BNC_BLOCKNUM(&sn, 1).b = 35;
|
2008-01-31 22:05:43 +00:00
|
|
|
BNC_SUBTREE_FINGERPRINT(&sn, 0) = random();
|
|
|
|
BNC_SUBTREE_FINGERPRINT(&sn, 1) = random();
|
2008-04-30 13:23:04 +00:00
|
|
|
BNC_SUBTREE_LEAFENTRY_ESTIMATE(&sn, 0) = random() + (((long long)random())<<32);
|
|
|
|
BNC_SUBTREE_LEAFENTRY_ESTIMATE(&sn, 1) = random() + (((long long)random())<<32);
|
2008-01-31 22:05:43 +00:00
|
|
|
r = toku_fifo_create(&BNC_BUFFER(&sn,0)); assert(r==0);
|
|
|
|
r = toku_fifo_create(&BNC_BUFFER(&sn,1)); assert(r==0);
|
2008-07-27 22:16:49 +00:00
|
|
|
r = toku_fifo_enq(BNC_BUFFER(&sn,0), "a", 2, "aval", 5, BRT_NONE, (TXNID)0); assert(r==0); sn.local_fingerprint += randval*toku_calc_fingerprint_cmd(BRT_NONE, (TXNID)0, "a", 2, "aval", 5);
|
|
|
|
r = toku_fifo_enq(BNC_BUFFER(&sn,0), "b", 2, "bval", 5, BRT_NONE, (TXNID)123); assert(r==0); sn.local_fingerprint += randval*toku_calc_fingerprint_cmd(BRT_NONE, (TXNID)123, "b", 2, "bval", 5);
|
|
|
|
r = toku_fifo_enq(BNC_BUFFER(&sn,1), "x", 2, "xval", 5, BRT_NONE, (TXNID)234); assert(r==0); sn.local_fingerprint += randval*toku_calc_fingerprint_cmd(BRT_NONE, (TXNID)234, "x", 2, "xval", 5);
|
2008-01-31 22:05:43 +00:00
|
|
|
BNC_NBYTESINBUF(&sn, 0) = 2*(BRT_CMD_OVERHEAD+KEY_VALUE_OVERHEAD+2+5);
|
|
|
|
BNC_NBYTESINBUF(&sn, 1) = 1*(BRT_CMD_OVERHEAD+KEY_VALUE_OVERHEAD+2+5);
|
2008-01-11 14:03:33 +00:00
|
|
|
sn.u.n.n_bytes_in_buffers = 3*(BRT_CMD_OVERHEAD+KEY_VALUE_OVERHEAD+2+5);
|
2007-07-13 19:37:47 +00:00
|
|
|
|
2013-04-16 23:57:18 -04:00
|
|
|
struct brt *XMALLOC(brt);
|
|
|
|
struct brt_header *XMALLOC(brt_h);
|
|
|
|
struct block_translation_pair *XMALLOC_N(21, btps);
|
|
|
|
memset(btps, 0, sizeof(btps));
|
|
|
|
brt->h = brt_h;
|
2013-04-16 23:57:38 -04:00
|
|
|
brt_h->panic = 0; brt_h->panic_string = 0;
|
2013-04-16 23:57:41 -04:00
|
|
|
toku_blocktable_create_new(&brt_h->blocktable);
|
|
|
|
toku_blocktable_debug_set_translation(brt_h->blocktable, 1, btps);
|
|
|
|
btps[20].diskoff = 4096;
|
|
|
|
btps[20].size = 100;
|
2013-04-16 23:57:18 -04:00
|
|
|
{
|
|
|
|
u_int64_t b;
|
2013-04-16 23:57:41 -04:00
|
|
|
toku_block_alloc(brt_h->blocktable, 100, &b);
|
2013-04-16 23:57:18 -04:00
|
|
|
assert(b==4096);
|
|
|
|
}
|
|
|
|
|
2013-04-16 23:57:38 -04:00
|
|
|
toku_serialize_brtnode_to(fd, make_blocknum(20), &sn, brt->h, 1, 1); assert(r==0);
|
2013-04-16 23:57:18 -04:00
|
|
|
|
2013-04-16 23:57:18 -04:00
|
|
|
r = toku_deserialize_brtnode_from(fd, make_blocknum(20), 0/*pass zero for hash*/, &dn, brt_h);
|
2007-11-14 17:58:38 +00:00
|
|
|
assert(r==0);
|
2007-08-01 02:37:21 +00:00
|
|
|
|
2013-04-16 23:57:18 -04:00
|
|
|
assert(dn->thisnodename.b==20);
|
|
|
|
|
2007-11-24 03:50:28 +00:00
|
|
|
assert(dn->disk_lsn.lsn==123456);
|
2008-07-27 22:16:49 +00:00
|
|
|
assert(dn->layout_version ==BRT_LAYOUT_VERSION);
|
2007-07-13 19:37:47 +00:00
|
|
|
assert(dn->height == 1);
|
2007-11-14 17:58:38 +00:00
|
|
|
assert(dn->rand4fingerprint==randval);
|
2007-07-13 19:37:47 +00:00
|
|
|
assert(dn->u.n.n_children==2);
|
2007-11-27 18:16:45 +00:00
|
|
|
assert(strcmp(kv_pair_key(dn->u.n.childkeys[0]), "hello")==0);
|
2007-12-06 14:20:47 +00:00
|
|
|
assert(toku_brtnode_pivot_key_len(dn, dn->u.n.childkeys[0])==6);
|
2007-07-13 19:37:47 +00:00
|
|
|
assert(dn->u.n.totalchildkeylens==6);
|
2013-04-16 23:57:18 -04:00
|
|
|
assert(BNC_BLOCKNUM(dn,0).b==30);
|
|
|
|
assert(BNC_BLOCKNUM(dn,1).b==35);
|
2007-11-14 17:58:38 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<2; i++) {
|
2008-01-31 22:05:43 +00:00
|
|
|
assert(BNC_SUBTREE_FINGERPRINT(dn, i)==BNC_SUBTREE_FINGERPRINT(&sn, i));
|
2008-04-30 13:23:04 +00:00
|
|
|
assert(BNC_SUBTREE_LEAFENTRY_ESTIMATE(dn, i)==BNC_SUBTREE_LEAFENTRY_ESTIMATE(&sn, i));
|
2007-11-14 17:58:38 +00:00
|
|
|
}
|
|
|
|
assert(dn->local_fingerprint==sn.local_fingerprint);
|
|
|
|
}
|
2008-01-11 14:03:33 +00:00
|
|
|
#if 0
|
2007-07-13 19:37:47 +00:00
|
|
|
{
|
2007-09-06 21:36:45 +00:00
|
|
|
bytevec data; ITEMLEN datalen; int type;
|
2008-01-11 14:03:33 +00:00
|
|
|
r = toku_hash_find(dn->u.n.buffers[0], "a", 2, &data, &datalen, &type);
|
2007-07-13 19:37:47 +00:00
|
|
|
assert(r==0);
|
|
|
|
assert(strcmp(data,"aval")==0);
|
|
|
|
assert(datalen==5);
|
2007-09-06 21:36:45 +00:00
|
|
|
assert(type == BRT_NONE);
|
2007-07-13 19:37:47 +00:00
|
|
|
|
2008-01-11 14:03:33 +00:00
|
|
|
r=toku_hash_find(dn->u.n.buffers[0], "b", 2, &data, &datalen, &type);
|
2007-07-13 19:37:47 +00:00
|
|
|
assert(r==0);
|
|
|
|
assert(strcmp(data,"bval")==0);
|
|
|
|
assert(datalen==5);
|
2007-09-06 21:36:45 +00:00
|
|
|
assert(type == BRT_NONE);
|
2007-07-13 19:37:47 +00:00
|
|
|
|
2008-01-11 14:03:33 +00:00
|
|
|
r=toku_hash_find(dn->u.n.buffers[1], "x", 2, &data, &datalen, &type);
|
2007-07-13 19:37:47 +00:00
|
|
|
assert(r==0);
|
|
|
|
assert(strcmp(data,"xval")==0);
|
|
|
|
assert(datalen==5);
|
2007-09-06 21:36:45 +00:00
|
|
|
assert(type == BRT_NONE);
|
2007-07-13 19:37:47 +00:00
|
|
|
}
|
2008-01-11 14:03:33 +00:00
|
|
|
#endif
|
2007-11-29 15:09:14 +00:00
|
|
|
toku_brtnode_free(&dn);
|
2007-08-01 02:37:21 +00:00
|
|
|
|
2007-11-27 18:16:45 +00:00
|
|
|
kv_pair_free(sn.u.n.childkeys[0]);
|
2007-08-01 02:37:21 +00:00
|
|
|
toku_free(hello_string);
|
2008-01-31 22:05:43 +00:00
|
|
|
toku_fifo_free(&BNC_BUFFER(&sn,0));
|
|
|
|
toku_fifo_free(&BNC_BUFFER(&sn,1));
|
2008-04-02 23:40:36 +00:00
|
|
|
toku_free(sn.u.n.childinfos);
|
|
|
|
toku_free(sn.u.n.childkeys);
|
2013-04-16 23:57:18 -04:00
|
|
|
|
2013-04-16 23:57:41 -04:00
|
|
|
toku_block_free(brt_h->blocktable, 4096);
|
|
|
|
toku_blocktable_destroy(&brt_h->blocktable);
|
2013-04-16 23:57:18 -04:00
|
|
|
toku_free(brt_h);
|
|
|
|
toku_free(brt);
|
2007-07-13 19:37:47 +00:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:57:32 -04:00
|
|
|
int
|
|
|
|
test_main (int argc __attribute__((__unused__)), const char *argv[] __attribute__((__unused__))) {
|
2007-11-29 15:34:49 +00:00
|
|
|
toku_memory_check = 1;
|
2007-07-13 19:37:47 +00:00
|
|
|
test_serialize();
|
2007-11-29 15:34:49 +00:00
|
|
|
toku_malloc_cleanup();
|
2007-07-13 19:37:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|