<commit_before>/* -*- mode: C; c-basic-offset: 4 -*- */
#ident "Copyright (c) 2007, 2008 Tokutek Inc.  All rights reserved."

#include "test.h"

#include "includes.h"

#define TESTMSNDSKVAL 0x1234567890123456    // arbitrary number
#define TESTMSNMEMVAL 0x6543210987654321    // arbitrary number

#define MIN(x, y) (((x) < (y)) ? (x) : (y))

static int omt_int_cmp(OMTVALUE p, void *q)
{
    LEAFENTRY a = p, b = q;
    void *ak, *bk;
    u_int32_t al, bl;
    ak = le_key_and_len(a, &al);
    bk = le_key_and_len(b, &bl);
    assert(al == 4 && bl == 4);
    int ai = *(int *) ak;
    int bi = *(int *) bk;
    int c = ai - bi;
    if (c < 0) { return -1; }
    if (c > 0) { return +1; }
    else       { return  0; }
}

static int omt_cmp(OMTVALUE p, void *q)
{
    LEAFENTRY a = p, b = q;
    void *ak, *bk;
    u_int32_t al, bl;
    ak = le_key_and_len(a, &al);
    bk = le_key_and_len(b, &bl);
    int l = MIN(al, bl);
    int c = memcmp(ak, bk, l);
    if (c < 0) { return -1; }
    if (c > 0) { return +1; }
    int d = al - bl;
    if (d < 0) { return -1; }
    if (d > 0) { return +1; }
    else       { return  0; }
}

static LEAFENTRY
le_fastmalloc(char *key, int keylen, char *val, int vallen)
{
    LEAFENTRY r = toku_malloc(sizeof(r->type) + sizeof(r->keylen) + sizeof(r->u.clean.vallen) +
                              keylen + vallen);
    resource_assert(r);
    r->type = LE_CLEAN;
    r->keylen = keylen;
    r->u.clean.vallen = vallen;
    memcpy(&r->u.clean.key_val[0], key, keylen);
    memcpy(&r->u.clean.key_val[keylen], val, vallen);
    return r;
}

static LEAFENTRY
le_malloc(char *key, char *val)
{
    int keylen = strlen(key) + 1;
    int vallen = strlen(val) + 1;
    return le_fastmalloc(key, keylen, val, vallen);
}

struct check_leafentries_struct {
    int nelts;
    LEAFENTRY *elts;
    int i;
    int (*cmp)(OMTVALUE, void *);
};

static int check_leafentries(OMTVALUE v, u_int32_t UU(i), void *extra) {
    struct check_leafentries_struct *e = extra;
    assert(e->i < e->nelts);
    assert(e->cmp(v, e->elts[e->i]) == 0);
    e->i++;
    return 0;
}

enum brtnode_verify_type {
    read_all=1,
    read_compressed,
    read_none
};

static void
setup_dn(enum brtnode_verify_type bft, int fd, struct brt_header *brt_h, BRTNODE *dn) {
    int r;
    if (bft == read_all) {
        struct brtnode_fetch_extra bfe;
        fill_bfe_for_full_read(&bfe, brt_h);
        r = toku_deserialize_brtnode_from(fd, make_blocknum(20), 0/*pass zero for hash*/, dn, &bfe);
        assert(r==0);
    }
    else if (bft == read_compressed || bft == read_none) {
        struct brtnode_fetch_extra bfe;
        fill_bfe_for_min_read(&bfe, brt_h);
        r = toku_deserialize_brtnode_from(fd, make_blocknum(20), 0/*pass zero for hash*/, dn, &bfe);
        assert(r==0);
        // assert all bp's are compressed
        for (int i = 0; i < (*dn)->n_children; i++) {
            assert(BP_STATE(*dn,i) == PT_COMPRESSED);
        }
        // if read_none, get rid of the compressed bp's
        if (bft == read_none) {
            long bytes_freed = 0;
            toku_brtnode_pe_callback(*dn, 0xffffffff, &bytes_freed, NULL);
            // assert all bp's are on disk
            for (int i = 0; i < (*dn)->n_children; i++) {
                if ((*dn)->height == 0) {
                    assert(BP_STATE(*dn,i) == PT_ON_DISK);
		    assert(is_BNULL(*dn, i));
                }
                else {
                    assert(BP_STATE(*dn,i) == PT_COMPRESSED);
		    assert(is_BNULL(*dn, i));
                }
            }
        }
        // now decompress them
        fill_bfe_for_full_read(&bfe, brt_h);
        assert(toku_brtnode_pf_req_callback(*dn, &bfe));
        long size;
        r = toku_brtnode_pf_callback(*dn, &bfe, fd, &size);
        assert(r==0);
        // assert all bp's are available
        for (int i = 0; i < (*dn)->n_children; i++) {
            assert(BP_STATE(*dn,i) == PT_AVAIL);
        }
        // continue on with test
    }
    else {
        // if we get here, this is a test bug, NOT a bug in development code
        assert(FALSE);
    }<commit_msg>[t:3564] Fix brt-serialize-test. Refs #3564.<commit_after>/* -*- mode: C; c-basic-offset: 4 -*- */
#ident "Copyright (c) 2007, 2008 Tokutek Inc.  All rights reserved."

#include "test.h"

#include "includes.h"

#define TESTMSNDSKVAL 0x1234567890123456    // arbitrary number
#define TESTMSNMEMVAL 0x6543210987654321    // arbitrary number

#define MIN(x, y) (((x) < (y)) ? (x) : (y))

static int omt_int_cmp(OMTVALUE p, void *q)
{
    LEAFENTRY a = p, b = q;
    void *ak, *bk;
    u_int32_t al, bl;
    ak = le_key_and_len(a, &al);
    bk = le_key_and_len(b, &bl);
    assert(al == 4 && bl == 4);
    int ai = *(int *) ak;
    int bi = *(int *) bk;
    int c = ai - bi;
    if (c < 0) { return -1; }
    if (c > 0) { return +1; }
    else       { return  0; }
}

static int omt_cmp(OMTVALUE p, void *q)
{
    LEAFENTRY a = p, b = q;
    void *ak, *bk;
    u_int32_t al, bl;
    ak = le_key_and_len(a, &al);
    bk = le_key_and_len(b, &bl);
    int l = MIN(al, bl);
    int c = memcmp(ak, bk, l);
    if (c < 0) { return -1; }
    if (c > 0) { return +1; }
    int d = al - bl;
    if (d < 0) { return -1; }
    if (d > 0) { return +1; }
    else       { return  0; }
}

static LEAFENTRY
le_fastmalloc(char *key, int keylen, char *val, int vallen)
{
    LEAFENTRY r = toku_malloc(sizeof(r->type) + sizeof(r->keylen) + sizeof(r->u.clean.vallen) +
                              keylen + vallen);
    resource_assert(r);
    r->type = LE_CLEAN;
    r->keylen = keylen;
    r->u.clean.vallen = vallen;
    memcpy(&r->u.clean.key_val[0], key, keylen);
    memcpy(&r->u.clean.key_val[keylen], val, vallen);
    return r;
}

static LEAFENTRY
le_malloc(char *key, char *val)
{
    int keylen = strlen(key) + 1;
    int vallen = strlen(val) + 1;
    return le_fastmalloc(key, keylen, val, vallen);
}

struct check_leafentries_struct {
    int nelts;
    LEAFENTRY *elts;
    int i;
    int (*cmp)(OMTVALUE, void *);
};

static int check_leafentries(OMTVALUE v, u_int32_t UU(i), void *extra) {
    struct check_leafentries_struct *e = extra;
    assert(e->i < e->nelts);
    assert(e->cmp(v, e->elts[e->i]) == 0);
    e->i++;
    return 0;
}

enum brtnode_verify_type {
    read_all=1,
    read_compressed,
    read_none
};

static void
setup_dn(enum brtnode_verify_type bft, int fd, struct brt_header *brt_h, BRTNODE *dn) {
    int r;
    if (bft == read_all) {
        struct brtnode_fetch_extra bfe;
        fill_bfe_for_full_read(&bfe, brt_h);
        r = toku_deserialize_brtnode_from(fd, make_blocknum(20), 0/*pass zero for hash*/, dn, &bfe);
        assert(r==0);
    }
    else if (bft == read_compressed || bft == read_none) {
        struct brtnode_fetch_extra bfe;
        fill_bfe_for_min_read(&bfe, brt_h);
        r = toku_deserialize_brtnode_from(fd, make_blocknum(20), 0/*pass zero for hash*/, dn, &bfe);
        assert(r==0);
        // assert all bp's are compressed
        for (int i = 0; i < (*dn)->n_children; i++) {
            assert(BP_STATE(*dn,i) == PT_COMPRESSED);
        }
        // if read_none, get rid of the compressed bp's
        if (bft == read_none) {
            long bytes_freed = 0;
            toku_brtnode_pe_callback(*dn, 0xffffffff, &bytes_freed, NULL);
            // assert all bp's are on disk
            for (int i = 0; i < (*dn)->n_children; i++) {
                if ((*dn)->height == 0) {
                    assert(BP_STATE(*dn,i) == PT_ON_DISK);
		    assert(is_BNULL(*dn, i));
                }
                else {
                    assert(BP_STATE(*dn,i) == PT_COMPRESSED);
                }
            }
        }
        // now decompress them
        fill_bfe_for_full_read(&bfe, brt_h);
        assert(toku_brtnode_pf_req_callback(*dn, &bfe));
        long size;
        r = toku_brtnode_pf_callback(*dn, &bfe, fd, &size);
        assert(r==0);
        // assert all bp's are available
        for (int i = 0; i < (*dn)->n_children; i++) {
            assert(BP_STATE(*dn,i) == PT_AVAIL);
        }
        // continue on with test
    }
    else {
        // if we get here, this is a test bug, NOT a bug in development code
        assert(FALSE);
    }
{'fraction_non_alphanumeric': 0.14990881458966565, 'fraction_numerical': 0.02188449848024316, 'mean_word_length': 2.2220916568742655, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1987773', 'n_tokens_mistral': 3304, 'n_tokens_neox': 3164, 'n_words': 790}