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

/* rollback and rollforward routines. */

#include <stdlib.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <unistd.h>

#include "log_header.h"
#include "log-internal.h"
#include "cachetable.h"
#include "key.h"

int toku_commit_fcreate (TXNID xid __attribute__((__unused__)),
			 BYTESTRING bs_fname __attribute__((__unused__)),
			 TOKUTXN    txn       __attribute__((__unused__))) {
    return 0;
}

int toku_rollback_fcreate (TXNID xid __attribute__((__unused__)),
			   BYTESTRING bs_fname,
			   TOKUTXN    txn       __attribute__((__unused__))) {
    char *fname = fixup_fname(&bs_fname);
    char *directory = txn->logger->directory;
    int  full_len=strlen(fname)+strlen(directory)+2;
    char full_fname[full_len];
    int l = snprintf(full_fname,full_len, "%s/%s", directory, fname);
    assert(l<=full_len);
    int r = unlink(full_fname);
    assert(r==0);
    free(fname);
    return 0;
}

static int find_brt_from_filenum (OMTVALUE v, void *filenumvp) {
    FILENUM *filenump=filenumvp;
    BRT brt = v;
    FILENUM thisfnum = toku_cachefile_filenum(brt->cf);
    if (thisfnum.fileid<filenump->fileid) return -1;
    if (thisfnum.fileid>filenump->fileid) return +1;
    return 0;
}

int toku_commit_cmdinsert (TXNID xid, FILENUM filenum, BYTESTRING key,BYTESTRING data,TOKUTXN txn) {
    CACHEFILE cf;
    //printf("%s:%d committing insert %s %s\n", __FILE__, __LINE__, key.data, data.data);
    int r = toku_cachefile_of_filenum(txn->logger->ct, filenum, &cf);
    assert(r==0);

    DBT key_dbt,data_dbt;
    BRT_CMD_S brtcmd = { BRT_COMMIT_BOTH, xid,
			 .u.id={toku_fill_dbt(&key_dbt,  key.data,  key.len),
				toku_fill_dbt(&data_dbt, data.data, data.len)}};

    OMTVALUE brtv;
    r = toku_omt_find_zero(txn->open_brts, find_brt_from_filenum, &filenum, &brtv, NULL);

    if (r==DB_NOTFOUND) {
	r = toku_cachefile_root_put_cmd(cf, &brtcmd, toku_txn_logger(txn));
	if (r!=0) return r;
    } else {
	assert(r==0);
	BRT brt = brtv;
	r = toku_brt_root_put_cmd(brt, &brtcmd, txn->logger);
    }
    return toku_cachefile_close(&cf, toku_txn_logger(txn));
	
}

int toku_rollback_cmdinsert (TXNID xid, FILENUM filenum, BYTESTRING key,BYTESTRING data,TOKUTXN txn) {
    CACHEFILE cf;
    int r = toku_cachefile_of_filenum(txn->logger->ct, filenum, &cf);
    assert(r==0);
    //printf("%s:%d aborting insert %s %s\n", __FILE__, __LINE__, key.data, data.data);
    DBT key_dbt,data_dbt;
    BRT_CMD_S brtcmd = { BRT_ABORT_BOTH, xid,
			 .u.id={toku_fill_dbt(&key_dbt,  key.data,  key.len),
				toku_fill_dbt(&data_dbt, data.data, data.len)}};
    r = toku_cachefile_root_put_cmd(cf, &brtcmd, toku_txn_logger(txn));
    if (r!=0) return r;
    return toku_cachefile_close(&cf, toku_txn_logger(txn));
}

int toku_commit_cmddeleteboth (TXNID xid, FILENUM filenum, BYTESTRING key,BYTESTRING data,TOKUTXN txn) {
    return toku_commit_cmdinsert(xid, filenum, key, data, txn);
}

int toku_rollback_cmddeleteboth (TXNID xid, FILENUM filenum, BYTESTRING key,BYTESTRING data,TOKUTXN txn) {
    return toku_rollback_cmdinsert(xid, filenum, key, data, txn);
}

int toku_commit_cmddelete (TXNID xid, FILENUM filenum, BYTESTRING key,TOKUTXN txn) {
    CACHEFILE cf;
    int r = toku_cachefile_of_filenum(txn->logger->ct, filenum, &cf);
    assert(r==0);
    //printf("%s:%d aborting delete %s %s\n", __FILE__, __LINE__, key.data, data.data);
    DBT key_dbt,data_dbt;
    BRT_CMD_S brtcmd = { BRT_COMMIT_ANY, xid,
			 .u.id={toku_fill_dbt(&key_dbt,  key.data,  key.len),
				toku_init_dbt(&data_dbt)}};
    r = toku_cachefile_root_put_cmd(cf, &brtcmd, toku_txn_logger(txn));
    if (r!=0) return r;
    return toku_cachefile_close(&cf, toku_txn_logger(txn));
}

int toku_rollback_cmddelete (TXNID xid, FILENUM filenum, BYTESTRING key,TOKUTXN txn) {
    CACHEFILE cf;
    int r = toku_cachefile_of_filenum(txn->logger->ct, filenum, &cf);
    assert(r==0);
    //printf("%s:%d aborting delete %s %s\n", __FILE__, __LINE__, key.data, data.data);
    DBT key_dbt,data_dbt;
    BRT_CMD_S brtcmd = { BRT_ABORT_ANY, xid,
			 .u.id={toku_fill_dbt(&key_dbt,  key.data,  key.len),
				toku_init_dbt(&data_dbt)}};
    r = toku_cachefile_root_put_cmd(cf, &brtcmd, toku_txn_logger(txn));
    if (r!=0) return r;
    return toku_cachefile_close(&cf, toku_txn_logger(txn));
}

int toku_commit_fileentries (int fd, off_t filesize, TOKUTXN txn) {
    while (filesize>0) {
        int r;
        struct roll_entry *item;<commit_msg>All commands now push into tree instead of root if possible.  Fixes #735.<commit_after>/* -*- mode: C; c-basic-offset: 4 -*- */
#ident "Copyright (c) 2007, 2008 Tokutek Inc.  All rights reserved."

/* rollback and rollforward routines. */

#include <stdlib.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <unistd.h>

#include "log_header.h"
#include "log-internal.h"
#include "cachetable.h"
#include "key.h"

int toku_commit_fcreate (TXNID xid __attribute__((__unused__)),
			 BYTESTRING bs_fname __attribute__((__unused__)),
			 TOKUTXN    txn       __attribute__((__unused__))) {
    return 0;
}

int toku_rollback_fcreate (TXNID xid __attribute__((__unused__)),
			   BYTESTRING bs_fname,
			   TOKUTXN    txn       __attribute__((__unused__))) {
    char *fname = fixup_fname(&bs_fname);
    char *directory = txn->logger->directory;
    int  full_len=strlen(fname)+strlen(directory)+2;
    char full_fname[full_len];
    int l = snprintf(full_fname,full_len, "%s/%s", directory, fname);
    assert(l<=full_len);
    int r = unlink(full_fname);
    assert(r==0);
    free(fname);
    return 0;
}

static int find_brt_from_filenum (OMTVALUE v, void *filenumvp) {
    FILENUM *filenump=filenumvp;
    BRT brt = v;
    FILENUM thisfnum = toku_cachefile_filenum(brt->cf);
    if (thisfnum.fileid<filenump->fileid) return -1;
    if (thisfnum.fileid>filenump->fileid) return +1;
    return 0;
}

static int do_insertion (enum brt_cmd_type type, TXNID xid, FILENUM filenum, BYTESTRING key, BYTESTRING *data,TOKUTXN txn) {
    CACHEFILE cf;
    //printf("%s:%d committing insert %s %s\n", __FILE__, __LINE__, key.data, data.data);
    int r = toku_cachefile_of_filenum(txn->logger->ct, filenum, &cf);
    assert(r==0);

    DBT key_dbt,data_dbt;
    BRT_CMD_S brtcmd = { type, xid,
			 .u.id={toku_fill_dbt(&key_dbt,  key.data,  key.len),
				data
				? toku_fill_dbt(&data_dbt, data->data, data->len)
				: toku_init_dbt(&data_dbt) }};
    OMTVALUE brtv;
    r = toku_omt_find_zero(txn->open_brts, find_brt_from_filenum, &filenum, &brtv, NULL);

    if (r==DB_NOTFOUND) {
	r = toku_cachefile_root_put_cmd(cf, &brtcmd, toku_txn_logger(txn));
	if (r!=0) return r;
    } else {
	assert(r==0);
	BRT brt = brtv;
	r = toku_brt_root_put_cmd(brt, &brtcmd, txn->logger);
    }
    return toku_cachefile_close(&cf, toku_txn_logger(txn));
}


int toku_commit_cmdinsert (TXNID xid, FILENUM filenum, BYTESTRING key,BYTESTRING data,TOKUTXN txn) {
    return do_insertion (BRT_COMMIT_BOTH, xid, filenum, key, &data, txn);
}

int toku_rollback_cmdinsert (TXNID xid, FILENUM filenum, BYTESTRING key,BYTESTRING data,TOKUTXN txn) {
    return do_insertion (BRT_ABORT_BOTH, xid, filenum, key, &data, txn);
}

int toku_commit_cmddeleteboth (TXNID xid, FILENUM filenum, BYTESTRING key,BYTESTRING data,TOKUTXN txn) {
    return do_insertion (BRT_COMMIT_BOTH, xid, filenum, key, &data, txn);
}

int toku_rollback_cmddeleteboth (TXNID xid, FILENUM filenum, BYTESTRING key,BYTESTRING data,TOKUTXN txn) {
    return do_insertion (BRT_ABORT_BOTH, xid, filenum, key, &data, txn);
}

int toku_commit_cmddelete (TXNID xid, FILENUM filenum, BYTESTRING key,TOKUTXN txn) {
    return do_insertion (BRT_COMMIT_ANY, xid, filenum, key, 0, txn);
}

int toku_rollback_cmddelete (TXNID xid, FILENUM filenum, BYTESTRING key,TOKUTXN txn) {
    return do_insertion (BRT_ABORT_ANY, xid, filenum, key, 0, txn);
}

int toku_commit_fileentries (int fd, off_t filesize, TOKUTXN txn) {
    while (filesize>0) {
        int r;
        struct roll_entry *item;
{'fraction_non_alphanumeric': 0.1323492929794096, 'fraction_numerical': 0.006325973703795584, 'mean_word_length': 4.339735099337748, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '6221683', 'n_tokens_mistral': 3461, 'n_tokens_neox': 3355, 'n_words': 682}