/* 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 "ham.h"
#include "../common/db_dispatch.h"
#include "../common/extern.h"

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

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

	if ((ret = __ham_insdel_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);
}

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

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

	if ((ret = __ham_newpage_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);
}

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

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

	if ((ret = __ham_splitmeta_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);
}

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

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

	if ((ret = __ham_splitdata_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);
}

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

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

	if ((ret = __ham_replace_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);
}

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

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

	if ((ret = __ham_newpgno_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);
}

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

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

	if ((ret = __ham_ovfl_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);
}

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

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

	if ((ret = __ham_copypage_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);
}
