📄 db_method.c
字号:
/*- * See the file LICENSE for redistribution information. * * Copyright (c) 1999-2004 * Sleepycat Software. All rights reserved. * * $Id: db_method.c,v 11.116 2004/10/11 18:22:05 bostic Exp $ */#include "db_config.h"#ifndef NO_SYSTEM_INCLUDES#include <sys/types.h>#ifdef HAVE_RPC#include <rpc/rpc.h>#endif#include <string.h>#endif#ifdef HAVE_RPC#include "db_server.h"#endif#include "db_int.h"#include "dbinc/crypto.h"#include "dbinc/db_page.h"#include "dbinc/db_shash.h"#include "dbinc/btree.h"#include "dbinc/hash.h"#include "dbinc/lock.h"#include "dbinc/mp.h"#include "dbinc/qam.h"#include "dbinc/xa.h"#include "dbinc_auto/xa_ext.h"#ifdef HAVE_RPC#include "dbinc_auto/rpc_client_ext.h"#endifstatic int __db_get_byteswapped __P((DB *, int *));static int __db_get_dbname __P((DB *, const char **, const char **));static DB_ENV *__db_get_env __P((DB *));static int __db_get_transactional __P((DB *));static int __db_get_type __P((DB *, DBTYPE *dbtype));static int __db_init __P((DB *, u_int32_t));static int __db_set_alloc __P((DB *, void *(*)(size_t), void *(*)(void *, size_t), void (*)(void *)));static int __db_set_append_recno __P((DB *, int (*)(DB *, DBT *, db_recno_t)));static int __db_get_cachesize __P((DB *, u_int32_t *, u_int32_t *, int *));static int __db_set_cachesize __P((DB *, u_int32_t, u_int32_t, int));static int __db_set_dup_compare __P((DB *, int (*)(DB *, const DBT *, const DBT *)));static int __db_get_encrypt_flags __P((DB *, u_int32_t *));static int __db_set_encrypt __P((DB *, const char *, u_int32_t));static int __db_set_feedback __P((DB *, void (*)(DB *, int, int)));static void __db_map_flags __P((DB *, u_int32_t *, u_int32_t *));static int __db_get_pagesize __P((DB *, u_int32_t *));static int __db_set_paniccall __P((DB *, void (*)(DB_ENV *, int)));static void __db_set_errcall __P((DB *, void (*)(const DB_ENV *, const char *, const char *)));static void __db_get_errfile __P((DB *, FILE **));static void __db_set_errfile __P((DB *, FILE *));static void __db_get_errpfx __P((DB *, const char **));static void __db_set_errpfx __P((DB *, const char *));static void __db_set_msgcall __P((DB *, void (*)(const DB_ENV *, const char *)));static void __db_get_msgfile __P((DB *, FILE **));static void __db_set_msgfile __P((DB *, FILE *));static void __dbh_err __P((DB *, int, const char *, ...));static void __dbh_errx __P((DB *, const char *, ...));#ifdef HAVE_RPCstatic int __dbcl_init __P((DB *, DB_ENV *, u_int32_t));#endif/* * db_create -- * DB constructor. * * EXTERN: int db_create __P((DB **, DB_ENV *, u_int32_t)); */intdb_create(dbpp, dbenv, flags) DB **dbpp; DB_ENV *dbenv; u_int32_t flags;{ DB *dbp; int ret; /* Check for invalid function flags. */ switch (flags) { case 0: break; case DB_REP_CREATE: break; case DB_XA_CREATE: if (dbenv != NULL) { __db_err(dbenv, "XA applications may not specify an environment to db_create"); return (EINVAL); } /* * If it's an XA database, open it within the XA environment, * taken from the global list of environments. (When the XA * transaction manager called our xa_start() routine the * "current" environment was moved to the start of the list. */ dbenv = TAILQ_FIRST(&DB_GLOBAL(db_envq)); break; default: return (__db_ferr(dbenv, "db_create", 0)); } /* Allocate the DB. */ if ((ret = __os_calloc(dbenv, 1, sizeof(*dbp), &dbp)) != 0) return (ret);#ifdef HAVE_RPC if (dbenv != NULL && RPC_ON(dbenv)) ret = __dbcl_init(dbp, dbenv, flags); else#endif ret = __db_init(dbp, flags); if (ret != 0) goto err; /* If we don't have an environment yet, allocate a local one. */ if (dbenv == NULL) { if ((ret = db_env_create(&dbenv, 0)) != 0) goto err; F_SET(dbenv, DB_ENV_DBLOCAL); } dbp->dbenv = dbenv; MUTEX_THREAD_LOCK(dbenv, dbenv->dblist_mutexp); ++dbenv->db_ref; MUTEX_THREAD_UNLOCK(dbenv, dbenv->dblist_mutexp); /* * Set the replication timestamp; it's 0 if we're not in a replicated * environment. */ dbp->timestamp = (F_ISSET(dbenv, DB_ENV_DBLOCAL) || !REP_ON(dbenv)) ? 0 : ((REGENV *)((REGINFO *)dbenv->reginfo)->primary)->rep_timestamp; /* If not RPC, open a backing DB_MPOOLFILE handle in the memory pool. */#ifdef HAVE_RPC if (!RPC_ON(dbenv))#endif if ((ret = __memp_fcreate(dbenv, &dbp->mpf)) != 0) goto err; dbp->type = DB_UNKNOWN; *dbpp = dbp; return (0);err: if (dbp->mpf != NULL) (void)__memp_fclose(dbp->mpf, 0); if (dbenv != NULL && F_ISSET(dbenv, DB_ENV_DBLOCAL)) (void)__dbenv_close(dbenv, 0); __os_free(dbenv, dbp); *dbpp = NULL; return (ret);}/* * __db_init -- * Initialize a DB structure. */static int__db_init(dbp, flags) DB *dbp; u_int32_t flags;{ int ret; dbp->lid = DB_LOCK_INVALIDID; LOCK_INIT(dbp->handle_lock); TAILQ_INIT(&dbp->free_queue); TAILQ_INIT(&dbp->active_queue); TAILQ_INIT(&dbp->join_queue); LIST_INIT(&dbp->s_secondaries); FLD_SET(dbp->am_ok, DB_OK_BTREE | DB_OK_HASH | DB_OK_QUEUE | DB_OK_RECNO); dbp->associate = __db_associate_pp; dbp->close = __db_close_pp; dbp->cursor = __db_cursor_pp; dbp->del = __db_del_pp; dbp->dump = __db_dump_pp; dbp->err = __dbh_err; dbp->errx = __dbh_errx; dbp->fd = __db_fd_pp; dbp->get = __db_get_pp; dbp->get_byteswapped = __db_get_byteswapped; dbp->get_dbname = __db_get_dbname; dbp->get_env = __db_get_env; dbp->get_open_flags = __db_get_open_flags; dbp->get_transactional = __db_get_transactional; dbp->get_type = __db_get_type; dbp->join = __db_join_pp; dbp->key_range = __db_key_range_pp; dbp->open = __db_open_pp; dbp->pget = __db_pget_pp; dbp->put = __db_put_pp; dbp->remove = __db_remove_pp; dbp->rename = __db_rename_pp; dbp->truncate = __db_truncate_pp; dbp->set_alloc = __db_set_alloc; dbp->set_append_recno = __db_set_append_recno; dbp->get_cachesize = __db_get_cachesize; dbp->set_cachesize = __db_set_cachesize; dbp->set_dup_compare = __db_set_dup_compare; dbp->get_encrypt_flags = __db_get_encrypt_flags; dbp->set_encrypt = __db_set_encrypt; dbp->set_errcall = __db_set_errcall; dbp->get_errfile = __db_get_errfile; dbp->set_errfile = __db_set_errfile; dbp->get_errpfx = __db_get_errpfx; dbp->set_errpfx = __db_set_errpfx; dbp->set_feedback = __db_set_feedback; dbp->get_flags = __db_get_flags; dbp->set_flags = __db_set_flags; dbp->get_lorder = __db_get_lorder; dbp->set_lorder = __db_set_lorder; dbp->set_msgcall = __db_set_msgcall; dbp->get_msgfile = __db_get_msgfile; dbp->set_msgfile = __db_set_msgfile; dbp->get_pagesize = __db_get_pagesize; dbp->set_pagesize = __db_set_pagesize; dbp->set_paniccall = __db_set_paniccall; dbp->stat = __db_stat_pp; dbp->stat_print = __db_stat_print_pp; dbp->sync = __db_sync_pp; dbp->upgrade = __db_upgrade_pp; dbp->verify = __db_verify_pp; /* Access method specific. */ if ((ret = __bam_db_create(dbp)) != 0) return (ret); if ((ret = __ham_db_create(dbp)) != 0) return (ret); if ((ret = __qam_db_create(dbp)) != 0) return (ret); /* * XA specific: must be last, as we replace methods set by the * access methods. */ if (LF_ISSET(DB_XA_CREATE) && (ret = __db_xa_create(dbp)) != 0) return (ret); if (LF_ISSET(DB_REP_CREATE)) F_SET(dbp, DB_AM_REPLICATION); return (0);}/* * __dbh_am_chk -- * Error if an unreasonable method is called. * * PUBLIC: int __dbh_am_chk __P((DB *, u_int32_t)); */int__dbh_am_chk(dbp, flags) DB *dbp; u_int32_t flags;{ /* * We start out allowing any access methods to be called, and as the * application calls the methods the options become restricted. The * idea is to quit as soon as an illegal method combination is called. */ if ((LF_ISSET(DB_OK_BTREE) && FLD_ISSET(dbp->am_ok, DB_OK_BTREE)) || (LF_ISSET(DB_OK_HASH) && FLD_ISSET(dbp->am_ok, DB_OK_HASH)) || (LF_ISSET(DB_OK_QUEUE) && FLD_ISSET(dbp->am_ok, DB_OK_QUEUE)) || (LF_ISSET(DB_OK_RECNO) && FLD_ISSET(dbp->am_ok, DB_OK_RECNO))) { FLD_CLR(dbp->am_ok, ~flags); return (0); } __db_err(dbp->dbenv, "call implies an access method which is inconsistent with previous calls"); return (EINVAL);}/* * __dbh_err -- * Error message, including the standard error string. */static void#ifdef STDC_HEADERS__dbh_err(DB *dbp, int error, const char *fmt, ...)#else__dbh_err(dbp, error, fmt, va_alist) DB *dbp; int error; const char *fmt; va_dcl#endif{ DB_REAL_ERR(dbp->dbenv, error, 1, 1, fmt);}/* * __dbh_errx -- * Error message. */static void#ifdef STDC_HEADERS__dbh_errx(DB *dbp, const char *fmt, ...)#else__dbh_errx(dbp, fmt, va_alist) DB *dbp; const char *fmt; va_dcl#endif{ DB_REAL_ERR(dbp->dbenv, 0, 0, 1, fmt);}/* * __db_get_byteswapped -- * Return if database requires byte swapping. */static int__db_get_byteswapped(dbp, isswapped) DB *dbp; int *isswapped;{ DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->get_byteswapped"); *isswapped = F_ISSET(dbp, DB_AM_SWAP) ? 1 : 0; return (0);}/* * __db_get_dbname -- * Get the name of the database as passed to DB->open. */static int__db_get_dbname(dbp, fnamep, dnamep) DB *dbp; const char **fnamep, **dnamep;{ DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->get_dbname"); if (fnamep != NULL) *fnamep = dbp->fname; if (dnamep != NULL) *dnamep = dbp->dname; return (0);}/* * __db_get_env -- * Get the DB_ENV handle that was passed to db_create. */static DB_ENV *__db_get_env(dbp) DB *dbp;{ return (dbp->dbenv);}/* * get_transactional -- * Get whether this database was created in a transaction. */static int__db_get_transactional(dbp) DB *dbp;{ return (F_ISSET(dbp, DB_AM_TXN) ? 1 : 0);}/* * __db_get_type -- * Return type of underlying database. */static int__db_get_type(dbp, dbtype) DB *dbp; DBTYPE *dbtype;{ DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->get_type"); *dbtype = dbp->type; return (0);}/* * __db_set_append_recno -- * Set record number append routine. */static int__db_set_append_recno(dbp, func) DB *dbp; int (*func) __P((DB *, DBT *, db_recno_t));{ DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_append_recno"); DB_ILLEGAL_METHOD(dbp, DB_OK_QUEUE | DB_OK_RECNO); dbp->db_append_recno = func; return (0);}/* * __db_get_cachesize -- * Get underlying cache size. */static int__db_get_cachesize(dbp, cache_gbytesp, cache_bytesp, ncachep) DB *dbp; u_int32_t *cache_gbytesp, *cache_bytesp; int *ncachep;{ DB_ILLEGAL_IN_ENV(dbp, "DB->get_cachesize"); return (__memp_get_cachesize(dbp->dbenv, cache_gbytesp, cache_bytesp, ncachep));}/* * __db_set_cachesize -- * Set underlying cache size. */static int__db_set_cachesize(dbp, cache_gbytes, cache_bytes, ncache) DB *dbp; u_int32_t cache_gbytes, cache_bytes; int ncache;{ DB_ILLEGAL_IN_ENV(dbp, "DB->set_cachesize"); DB_ILLEGAL_AFTER_OPEN(dbp, "DB->set_cachesize"); return (__memp_set_cachesize( dbp->dbenv, cache_gbytes, cache_bytes, ncache));}/* * __db_set_dup_compare -- * Set duplicate comparison routine. */static int__db_set_dup_compare(dbp, func) DB *dbp; int (*func) __P((DB *, const DBT *, const DBT *));{ int ret; DB_ILLEGAL_AFTER_OPEN(dbp, "DB->dup_compare"); DB_ILLEGAL_METHOD(dbp, DB_OK_BTREE | DB_OK_HASH);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -