db_iface.c

来自「这是国外的resip协议栈」· C语言 代码 · 共 2,322 行 · 第 1/4 页

C
2,322
字号
	/* Check for consistent transaction usage. */	if ((ret = __db_check_txn(dbp, txn, DB_LOCK_INVALIDID,	    mode == DB_WRITELOCK || LF_ISSET(DB_RMW) ? 0 : 1)) != 0)		goto err;	/* Check for replication block. */	handle_check = IS_REPLICATED(dbenv, dbp);	if (handle_check && (ret = __db_rep_enter(dbp, 1, 0, txn != NULL)) != 0)		goto err;	ret = __db_get(dbp, txn, key, data, flags);	/* Release replication block. */	if (handle_check)		__env_db_rep_exit(dbenv);err:	return (txn_local ? __db_txn_auto_resolve(dbenv, txn, 0, ret) : ret);}/* * __db_get -- *	DB->get. * * PUBLIC: int __db_get __P((DB *, DB_TXN *, DBT *, DBT *, u_int32_t)); */int__db_get(dbp, txn, key, data, flags)	DB *dbp;	DB_TXN *txn;	DBT *key, *data;	u_int32_t flags;{	DBC *dbc;	u_int32_t mode;	int ret, t_ret;	mode = 0;	if (LF_ISSET(DB_DIRTY_READ)) {		mode = DB_DIRTY_READ;		LF_CLR(DB_DIRTY_READ);	} else if (LF_ISSET(DB_DEGREE_2)) {		mode = DB_DEGREE_2;		LF_CLR(DB_DEGREE_2);	} else if ((flags & DB_OPFLAGS_MASK) == DB_CONSUME ||	    (flags & DB_OPFLAGS_MASK) == DB_CONSUME_WAIT)		mode = DB_WRITELOCK;	if ((ret = __db_cursor(dbp, txn, &dbc, mode)) != 0)		return (ret);	DEBUG_LREAD(dbc, txn, "DB->get", key, NULL, flags);	/*	 * The DBC_TRANSIENT flag indicates that we're just doing a	 * single operation with this cursor, and that in case of	 * error we don't need to restore it to its old position--we're	 * going to close it right away.  Thus, we can perform the get	 * without duplicating the cursor, saving some cycles in this	 * common case.	 */	F_SET(dbc, DBC_TRANSIENT);	/*	 * SET_RET_MEM indicates that if key and/or data have no DBT	 * flags set and DB manages the returned-data memory, that memory	 * will belong to this handle, not to the underlying cursor.	 */	SET_RET_MEM(dbc, dbp);	if (LF_ISSET(~(DB_RMW | DB_MULTIPLE)) == 0)		LF_SET(DB_SET);	ret = __db_c_get(dbc, key, data, flags);	if (dbc != NULL && (t_ret = __db_c_close(dbc)) != 0 && ret == 0)		ret = t_ret;	return (ret);}/* * __db_get_arg -- *	DB->get argument checking, used by both DB->get and DB->pget. */static int__db_get_arg(dbp, key, data, flags)	const DB *dbp;	const DBT *key;	DBT *data;	u_int32_t flags;{	DB_ENV *dbenv;	int check_thread, dirty, multi, ret;	dbenv = dbp->dbenv;	/*	 * Check for read-modify-write validity.  DB_RMW doesn't make sense	 * with CDB cursors since if you're going to write the cursor, you	 * had to create it with DB_WRITECURSOR.  Regardless, we check for	 * LOCKING_ON and not STD_LOCKING, as we don't want to disallow it.	 * If this changes, confirm that DB does not itself set the DB_RMW	 * flag in a path where CDB may have been configured.	 */	check_thread = dirty = 0;	if (LF_ISSET(DB_DIRTY_READ | DB_RMW | DB_DEGREE_2)) {		if (!LOCKING_ON(dbenv))			return (__db_fnl(dbenv, "DB->get"));		dirty = LF_ISSET(DB_DIRTY_READ | DB_DEGREE_2);		if ((ret = __db_fcchk(dbenv,		    "DB->get", flags, DB_DIRTY_READ, DB_DEGREE_2)) != 0)			return (ret);		LF_CLR(DB_DIRTY_READ | DB_RMW | DB_DEGREE_2);	}	multi = 0;	if (LF_ISSET(DB_MULTIPLE | DB_MULTIPLE_KEY)) {		if (LF_ISSET(DB_MULTIPLE_KEY))			goto multi_err;		multi = LF_ISSET(DB_MULTIPLE) ? 1 : 0;		LF_CLR(DB_MULTIPLE);	}	if (LF_ISSET(DB_AUTO_COMMIT)) {		LF_CLR(DB_AUTO_COMMIT);		if (flags != DB_CONSUME && flags != DB_CONSUME_WAIT)			goto err;	}	/* Check for invalid function flags. */	switch (flags) {	case 0:	case DB_GET_BOTH:		break;	case DB_SET_RECNO:		check_thread = 1;		if (!F_ISSET(dbp, DB_AM_RECNUM))			goto err;		break;	case DB_CONSUME:	case DB_CONSUME_WAIT:		check_thread = 1;		if (dirty) {			__db_err(dbenv,    "%s is not supported with DB_CONSUME or DB_CONSUME_WAIT",			     LF_ISSET(DB_DIRTY_READ) ?			     "DB_DIRTY_READ" : "DB_DEGREE_2");			return (EINVAL);		}		if (multi)multi_err:		return (__db_ferr(dbenv, "DB->get", 1));		if (dbp->type == DB_QUEUE)			break;		/* FALLTHROUGH */	default:err:		return (__db_ferr(dbenv, "DB->get", 0));	}	/*	 * Check for invalid key/data flags.	 *	 * XXX: Dave Krinsky	 * Remember to modify this when we fix the flag-returning problem.	 */	if ((ret = __dbt_ferr(dbp, "key", key, check_thread)) != 0)		return (ret);	if ((ret = __dbt_ferr(dbp, "data", data, 1)) != 0)		return (ret);	if (multi) {		if (!F_ISSET(data, DB_DBT_USERMEM)) {			__db_err(dbenv,			    "DB_MULTIPLE requires DB_DBT_USERMEM be set");			return (EINVAL);		}		if (F_ISSET(key, DB_DBT_PARTIAL) ||		    F_ISSET(data, DB_DBT_PARTIAL)) {			__db_err(dbenv,			    "DB_MULTIPLE does not support DB_DBT_PARTIAL");			return (EINVAL);		}		if (data->ulen < 1024 ||		    data->ulen < dbp->pgsize || data->ulen % 1024 != 0) {			__db_err(dbenv, "%s%s",			    "DB_MULTIPLE buffers must be ",			    "aligned, at least page size and multiples of 1KB");			return (EINVAL);		}	}	return (0);}/* * __db_join_pp -- *	DB->join pre/post processing. * * PUBLIC: int __db_join_pp __P((DB *, DBC **, DBC **, u_int32_t)); */int__db_join_pp(primary, curslist, dbcp, flags)	DB *primary;	DBC **curslist, **dbcp;	u_int32_t flags;{	DB_ENV *dbenv;	int handle_check, ret;	dbenv = primary->dbenv;	PANIC_CHECK(dbenv);	if ((ret = __db_join_arg(primary, curslist, flags)) != 0)		return (ret);	/* Check for replication block. */	handle_check = IS_REPLICATED(dbenv, primary);	if (handle_check && (ret =	    __db_rep_enter(primary, 1, 0, curslist[0]->txn != NULL)) != 0)		return (ret);	ret = __db_join(primary, curslist, dbcp, flags);	/* Release replication block. */	if (handle_check)		__env_db_rep_exit(dbenv);	return (ret);}/* * __db_join_arg -- *	Check DB->join arguments. */static int__db_join_arg(primary, curslist, flags)	DB *primary;	DBC **curslist;	u_int32_t flags;{	DB_ENV *dbenv;	DB_TXN *txn;	int i;	dbenv = primary->dbenv;	switch (flags) {	case 0:	case DB_JOIN_NOSORT:		break;	default:		return (__db_ferr(dbenv, "DB->join", 0));	}	if (curslist == NULL || curslist[0] == NULL) {		__db_err(dbenv,	    "At least one secondary cursor must be specified to DB->join");		return (EINVAL);	}	txn = curslist[0]->txn;	for (i = 1; curslist[i] != NULL; i++)		if (curslist[i]->txn != txn) {			__db_err(dbenv,		    "All secondary cursors must share the same transaction");			return (EINVAL);		}	return (0);}/* * __db_key_range_pp -- *	DB->key_range pre/post processing. * * PUBLIC: int __db_key_range_pp * PUBLIC:     __P((DB *, DB_TXN *, DBT *, DB_KEY_RANGE *, u_int32_t)); */int__db_key_range_pp(dbp, txn, key, kr, flags)	DB *dbp;	DB_TXN *txn;	DBT *key;	DB_KEY_RANGE *kr;	u_int32_t flags;{	DBC *dbc;	DB_ENV *dbenv;	int handle_check, ret, t_ret;	dbenv = dbp->dbenv;	PANIC_CHECK(dbp->dbenv);	DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->key_range");	/*	 * !!!	 * The actual argument checking is simple, do it inline.	 */	if (flags != 0)		return (__db_ferr(dbenv, "DB->key_range", 0));	/* Check for consistent transaction usage. */	if ((ret = __db_check_txn(dbp, txn, DB_LOCK_INVALIDID, 1)) != 0)		return (ret);	/* Check for replication block. */	handle_check = IS_REPLICATED(dbenv, dbp);	if (handle_check && (ret = __db_rep_enter(dbp, 1, 0, txn != NULL)) != 0)		return (ret);	/*	 * !!!	 * The actual method call is simple, do it inline.	 */	switch (dbp->type) {	case DB_BTREE:		/* Acquire a cursor. */		if ((ret = __db_cursor(dbp, txn, &dbc, 0)) != 0)			break;		DEBUG_LWRITE(dbc, NULL, "bam_key_range", NULL, NULL, 0);		ret = __bam_key_range(dbc, key, kr, flags);		if ((t_ret = __db_c_close(dbc)) != 0 && ret == 0)			ret = t_ret;		break;	case DB_HASH:	case DB_QUEUE:	case DB_RECNO:		ret = __dbh_am_chk(dbp, DB_OK_BTREE);		break;	case DB_UNKNOWN:	default:		ret = __db_unknown_type(dbenv, "DB->key_range", dbp->type);		break;	}	/* Release replication block. */	if (handle_check)		__env_db_rep_exit(dbenv);	return (ret);}/* * __db_open_pp -- *	DB->open pre/post processing. * * PUBLIC: int __db_open_pp __P((DB *, DB_TXN *, * PUBLIC:     const char *, const char *, DBTYPE, u_int32_t, int)); */int__db_open_pp(dbp, txn, fname, dname, type, flags, mode)	DB *dbp;	DB_TXN *txn;	const char *fname, *dname;	DBTYPE type;	u_int32_t flags;	int mode;{	DB_ENV *dbenv;	int handle_check, nosync, remove_me, ret, txn_local;	dbenv = dbp->dbenv;	nosync = 1;	remove_me = 0;	PANIC_CHECK(dbenv);	if ((ret = __db_open_arg(dbp, txn, fname, dname, type, flags)) != 0)		return (ret);	/*	 * Save the file and database names and flags.  We do this here	 * because we don't pass all of the flags down into the actual	 * DB->open method call, we strip DB_AUTO_COMMIT at this layer.	 */	if ((fname != NULL &&	    (ret = __os_strdup(dbenv, fname, &dbp->fname)) != 0) ||	    (dname != NULL &&	    (ret = __os_strdup(dbenv, dname, &dbp->dname)) != 0))		return (ret);	dbp->open_flags = flags;	/* Save the current DB handle flags for refresh. */	dbp->orig_flags = dbp->flags;	/*	 * Create local transaction as necessary, check for consistent	 * transaction usage.	 */	txn_local = 0;	if (IS_AUTO_COMMIT(dbenv, txn, flags)) {		if ((ret = __db_txn_auto_init(dbenv, &txn)) != 0)			return (ret);		txn_local = 1;		LF_CLR(DB_AUTO_COMMIT);	} else		if (txn != NULL && !TXN_ON(dbenv))			return (__db_not_txn_env(dbenv));	/* Check for replication block. */	handle_check = IS_REPLICATED(dbenv, dbp);	if (handle_check &&	    (ret = __db_rep_enter(dbp, 1, 0, txn != NULL)) != 0) {		handle_check = 0;		goto err;	}	if ((ret = __db_open(dbp,	    txn, fname, dname, type, flags, mode, PGNO_BASE_MD)) != 0)		goto err;	/*	 * You can open the database that describes the subdatabases in the	 * rest of the file read-only.  The content of each key's data is	 * unspecified and applications should never be adding new records	 * or updating existing records.  However, during recovery, we need	 * to open these databases R/W so we can redo/undo changes in them.	 * Likewise, we need to open master databases read/write during	 * rename and remove so we can be sure they're fully sync'ed, so	 * we provide an override flag for the purpose.	 */	if (dname == NULL && !IS_RECOVERING(dbenv) && !LF_ISSET(DB_RDONLY) &&	    !LF_ISSET(DB_RDWRMASTER) && F_ISSET(dbp, DB_AM_SUBDB)) {		__db_err(dbenv,    "files containing multiple databases may only be opened read-only");		ret = EINVAL;		goto err;	}	/*	 * Success: file creations have to be synchronous, otherwise we don't	 * care.	 */	if (F_ISSET(dbp, DB_AM_CREATED | DB_AM_CREATED_MSTR))		nosync = 0;	/* Success: don't discard the file on close. */	F_CLR(dbp, DB_AM_DISCARD | DB_AM_CREATED | DB_AM_CREATED_MSTR);	/*	 * If not transactional, remove the databases/subdatabases.  If we're	 * transactional, the child transaction abort cleans up.	 */err:	if (ret != 0 && txn == NULL) {		remove_me = F_ISSET(dbp, DB_AM_CREATED);		if (F_ISSET(dbp, DB_AM_CREATED_MSTR) ||		    (dname == NULL && remove_me))			/* Remove file. */			(void)__db_remove_int(dbp, txn, fname, NULL, DB_FORCE);		else if (remove_me)			/* Remove subdatabase. */			(void)__db_remove_int(dbp, txn, fname, dname, DB_FORCE);	}	/* Release replication block. */	if (handle_check)		__env_db_rep_exit(dbenv);	return (txn_local ?	    __db_txn_auto_resolve(dbenv, txn, nosync, ret) : ret);}/* * __db_open_arg -- *	Check DB->open arguments. */static int__db_open_arg(dbp, txn, fname, dname, type, flags)	DB *dbp;	DB_TXN *txn;	const char *fname, *dname;	DBTYPE type;	u_int32_t flags;{	DB_ENV *dbenv;	u_int32_t ok_flags;	int ret;	dbenv = dbp->dbenv;	/* Validate arguments. */#undef	OKFLAGS#define	OKFLAGS								\    (DB_AUTO_COMMIT | DB_CREATE | DB_DIRTY_READ | DB_EXCL |		\     DB_FCNTL_LOCKING | DB_NO_AUTO_COMMIT | DB_NOMMAP | DB_RDONLY |	\     DB_RDWRMASTER | DB_THREAD | DB_TRUNCATE | DB_WRITEOPEN)	if ((ret = __db_fchk(dbenv, "DB->open", flags, OKFLAGS)) != 0)		return (ret);	if (LF_ISSET(DB_EXCL) && !LF_ISSET(DB_CREATE))		return (__db_ferr(dbenv, "DB->open", 1));	if (LF_ISSET(DB_RDONLY) && LF_ISSET(DB_CREATE))		return (__db_ferr(dbenv, "DB->open", 1));#ifdef	HAVE_VXWORKS	if (LF_ISSET(DB_TRUNCATE)) {		__db_err(dbenv, "DB_TRUNCATE not supported on VxWorks");		return (DB_OPNOTSUP);	}#endif	switch (type) {	case DB_UNKNOWN:		if (LF_ISSET(DB_CREATE|DB_TRUNCATE)) {			__db_err(dbenv,	    "%s: DB_UNKNOWN type specified with DB_CREATE or DB_TRUNCATE",			    fname);			return (EINVAL);		}		ok_flags = 0;		break;	case DB_BTREE:		ok_flags = DB_OK_BTREE;		break;	case DB_HASH:#ifndef HAVE_HASH		return (__db_no_hash_am(dbenv));#endif		ok_flags = DB_OK_HASH;		break;	case DB_QUEUE:#ifndef HAVE_QUEUE		return (__db_no_queue_am(dbenv));#endif		ok_flags = DB_OK_QUEUE;		break;	case DB_RECNO:		ok_flags = DB_OK_RECNO;		break;	default:		__db_err(dbenv, "unknown type: %lu", (u_long)type);		return (EINVAL);	}	if (ok_flags)		DB_ILLEGAL_METHOD(dbp, ok_flags);	/* The environment may have been created, but never opened. */	if (!F_ISSET(dbenv, DB_ENV_DBLOCAL | DB_ENV_OPEN_CALLED)) {		__db_err(dbenv, "environment not yet opened");		return (EINVAL);	}	/*	 * Historically, you could pass in an environment that didn't have a	 * mpool, and DB would create a private one behind the scenes.  This	 * no longer works.	 */	if (!F_ISSET(dbenv, DB_ENV_DBLOCAL) && !MPOOL_ON(dbenv)) {		__db_err(dbenv, "environment did not include a memory pool");		return (EINVAL);	}	/*	 * You can't specify threads during DB->open if subsystems in the	 * environment weren't configured with them.	 */	if (LF_ISSET(DB_THREAD) &&	    !F_ISSET(dbenv, DB_ENV_DBLOCAL | DB_ENV_THREAD)) {		__db_err(dbenv, "environment not created using DB_THREAD");		return (EINVAL);	}	/* DB_TRUNCATE is neither transaction recoverable nor lockable. */	if (LF_ISSET(DB_TRUNCATE) && (LOCKING_ON(dbenv) || txn != NULL)) {		__db_err(dbenv,		    "DB_TRUNCATE illegal with %s specified",		    LOCKING_ON(dbenv) ? "locking" : "transactions");		return (EINVAL);	}	/* Subdatabase checks. */	if (dname != NULL) {		/* Subdatabases must be created in named files. */		if (fname == NULL) {			__db_err(dbenv,		    "multiple databases cannot be created in temporary files");			return (EINVAL);		}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?