/* Do not edit: automatically built by dist/distrib. */
#include <sys/queue.h>
#include <sys/shqueue.h>

#include <ctype.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include "db_int.h"
#include "db_page.h"
#include "../log/log.h"
#include "txn.h"
#include "../common/db_dispatch.h"
#include "../common/extern.h"

/*
 * __txn_regop_recover --
 *	Recovery function for regop.
 *
 * PUBLIC: int __txn_regop_recover
 * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
 */
int
__txn_regop_recover(logp, dbtp, lsnp, redo, info)
	DB_LOG *logp;
	DBT *dbtp;
	DB_LSN *lsnp;
	int redo;
	void *info;
{
	__txn_regop_args *argp;
	DB *file_dbp;
	DB_MPOOLFILE *mpf;
	PAGE *pagep;
	int cmp_n, cmp_p, modified, ret;

#ifdef DEBUG_RECOVER
	(void)__txn_regop_print(logp, dbtp, lsnp, redo, info);
#endif
	info = info;				/* XXX: Shut the compiler up. */

	if ((ret = __txn_regop_read(dbtp->data, &argp)) != 0)
		goto out;
	if (__db_fileid_to_db(logp, &file_dbp, argp->fileid)) {
		ret = 0;
		goto out;
	}
	if (file_dbp == NULL)
		goto out;

	F_SET(file_dbp, DB_AM_RECOVER);
	mpf = file_dbp->mpf;

	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0)
		if (redo) {
			if ((ret = memp_fget(mpf,
			    &argp->pgno, DB_MPOOL_CREATE, &pagep)) != 0)
				goto out;
		} else {
			*lsnp = *argp->prev_lsn;
			ret = 0;
			goto out;
		}

	modified = 0;
	cmp_n = log_compare(lsnp, &LSN(pagep));

	/*
	 * Use this when there is something like "pagelsn" in the argp
	 * structure.  Sometimes, you might need to compare meta-data
	 * lsn's instead.
	 *
	 * cmp_p = log_compare(&LSN(pagep), argp->pagelsn);
	 */
	if (cmp_p == 0 && redo) {
		/* Need to redo update described. */
		modified = 1;
	} else if (cmp_n == 0 && !redo) {
		/* Need to undo update described. */
		modified = 1;
	}
	if (ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0))
		goto out;

	*lsnp = *argp->prev_lsn;

out:	if (argp != NULL)
		free (argp);
	F_CLR(file_dbp, DB_AM_RECOVER);
	return (ret);
}

/*
 * __txn_xa_regop_recover --
 *	Recovery function for xa_regop.
 *
 * PUBLIC: int __txn_xa_regop_recover
 * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
 */
int
__txn_xa_regop_recover(logp, dbtp, lsnp, redo, info)
	DB_LOG *logp;
	DBT *dbtp;
	DB_LSN *lsnp;
	int redo;
	void *info;
{
	__txn_xa_regop_args *argp;
	DB *file_dbp;
	DB_MPOOLFILE *mpf;
	PAGE *pagep;
	int cmp_n, cmp_p, modified, ret;

#ifdef DEBUG_RECOVER
	(void)__txn_xa_regop_print(logp, dbtp, lsnp, redo, info);
#endif
	info = info;				/* XXX: Shut the compiler up. */

	if ((ret = __txn_xa_regop_read(dbtp->data, &argp)) != 0)
		goto out;
	if (__db_fileid_to_db(logp, &file_dbp, argp->fileid)) {
		ret = 0;
		goto out;
	}
	if (file_dbp == NULL)
		goto out;

	F_SET(file_dbp, DB_AM_RECOVER);
	mpf = file_dbp->mpf;

	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0)
		if (redo) {
			if ((ret = memp_fget(mpf,
			    &argp->pgno, DB_MPOOL_CREATE, &pagep)) != 0)
				goto out;
		} else {
			*lsnp = *argp->prev_lsn;
			ret = 0;
			goto out;
		}

	modified = 0;
	cmp_n = log_compare(lsnp, &LSN(pagep));

	/*
	 * Use this when there is something like "pagelsn" in the argp
	 * structure.  Sometimes, you might need to compare meta-data
	 * lsn's instead.
	 *
	 * cmp_p = log_compare(&LSN(pagep), argp->pagelsn);
	 */
	if (cmp_p == 0 && redo) {
		/* Need to redo update described. */
		modified = 1;
	} else if (cmp_n == 0 && !redo) {
		/* Need to undo update described. */
		modified = 1;
	}
	if (ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0))
		goto out;

	*lsnp = *argp->prev_lsn;

out:	if (argp != NULL)
		free (argp);
	F_CLR(file_dbp, DB_AM_RECOVER);
	return (ret);
}

/*
 * __txn_ckp_recover --
 *	Recovery function for ckp.
 *
 * PUBLIC: int __txn_ckp_recover
 * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
 */
int
__txn_ckp_recover(logp, dbtp, lsnp, redo, info)
	DB_LOG *logp;
	DBT *dbtp;
	DB_LSN *lsnp;
	int redo;
	void *info;
{
	__txn_ckp_args *argp;
	DB *file_dbp;
	DB_MPOOLFILE *mpf;
	PAGE *pagep;
	int cmp_n, cmp_p, modified, ret;

#ifdef DEBUG_RECOVER
	(void)__txn_ckp_print(logp, dbtp, lsnp, redo, info);
#endif
	info = info;				/* XXX: Shut the compiler up. */

	if ((ret = __txn_ckp_read(dbtp->data, &argp)) != 0)
		goto out;
	if (__db_fileid_to_db(logp, &file_dbp, argp->fileid)) {
		ret = 0;
		goto out;
	}
	if (file_dbp == NULL)
		goto out;

	F_SET(file_dbp, DB_AM_RECOVER);
	mpf = file_dbp->mpf;

	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0)
		if (redo) {
			if ((ret = memp_fget(mpf,
			    &argp->pgno, DB_MPOOL_CREATE, &pagep)) != 0)
				goto out;
		} else {
			*lsnp = *argp->prev_lsn;
			ret = 0;
			goto out;
		}

	modified = 0;
	cmp_n = log_compare(lsnp, &LSN(pagep));

	/*
	 * Use this when there is something like "pagelsn" in the argp
	 * structure.  Sometimes, you might need to compare meta-data
	 * lsn's instead.
	 *
	 * cmp_p = log_compare(&LSN(pagep), argp->pagelsn);
	 */
	if (cmp_p == 0 && redo) {
		/* Need to redo update described. */
		modified = 1;
	} else if (cmp_n == 0 && !redo) {
		/* Need to undo update described. */
		modified = 1;
	}
	if (ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0))
		goto out;

	*lsnp = *argp->prev_lsn;

out:	if (argp != NULL)
		free (argp);
	F_CLR(file_dbp, DB_AM_RECOVER);
	return (ret);
}
