/* Check to see that delete_both works on both dup and nodup databases. *
 * For recovery to work right, delete_both must work on both cases.
 * Specifically, for a nodup database delete_both must not remove pairs unless
 * they match both key and value.
 */

#include "includes.h"
#include "test.h"


static TOKUTXN const null_txn = 0;

static void
doit (void) {
    int r;
    CACHETABLE ct;
    BRT t;
    char fname[] = __FILE__ ".tdb";
    DBT k,v;
    if (verbose) printf("%s\n", __FUNCTION__);
    r = toku_brt_create_cachetable(&ct, 0, ZERO_LSN, NULL_LOGGER); assert(r==0);
    unlink(fname);
    r = toku_brt_create(&t); assert(r==0);
    r = toku_brt_open(t, fname, fname, 1, 1, ct, null_txn, (DB*)0); assert(r==0);

    r = toku_brt_insert(t, toku_fill_dbt(&k, "a", 2), toku_fill_dbt(&v, "x", 2), null_txn);
    assert(r==0);

    r = toku_brt_delete_both(t, toku_fill_dbt(&k, "a", 2), toku_fill_dbt(&v, "y", 2), null_txn);
    assert(r==0);

    {
	struct check_pair pair = {len_ignore, 0, 2, "x", 0};
	r = toku_brt_lookup(t, toku_fill_dbt(&k, "a", 2), NULL, lookup_checkf, &pair);
	assert(r==0);
	assert(pair.call_count==1);
    }

    r = toku_brt_delete_both(t, toku_fill_dbt(&k, "a", 2), toku_fill_dbt(&v, "x", 2), null_txn);
    assert(r==0);
    
    {
	struct check_pair pair = {0,0,0,0,0};
	r = toku_brt_lookup(t, toku_fill_dbt(&k, "a", 2), NULL, lookup_checkf, &pair);
	assert(r==DB_NOTFOUND);
	assert(pair.call_count==0);
    }

    r = toku_close_brt(t, 0, 0);        assert(r==0);
    r = toku_cachetable_close(&ct);     assert(r==0);
}

int
test_main (int argc, const char *argv[]) {
    default_parse_args(argc, argv);
    doit();
    if (verbose) printf("test ok\n");
    toku_malloc_cleanup();
    return 0;
}
