Example #1
0
static void
cleanup()
{
    int rc;
    DB *db;
    DB_ENV *dbenv;
    rc = get_db(&db, 0);
    assert(! rc);
    rc = get_dbenv(&dbenv, 0);
    assert(! rc);
    if (dbkey.data)
	free(dbkey.data);
    if (db)
	call_db(db->close(db, 0), "DB close");
    if (dbenv) {
	rc = call_db(db_create(&db, dbenv, 0), "db_create");
	if (!rc)
	    if (! db->remove(db, "tls_stats.db", 0, 0))
		syslog(LOG_NOTICE, "Unused database tls_stats.db removed");
	call_db(dbenv->txn_checkpoint(dbenv, 100 * 1024, 24 * 60, 0),
		"txn_checkpoint");
	call_db(dbenv->log_archive(dbenv, NULL, DB_ARCH_REMOVE), "log_archive");
	call_db(dbenv->close(dbenv, 0), "DB_ENV close");
    }
    policy_cleanup();
}
Example #2
0
bool delete_db()
{
    if (db == NULL)
        return false;
    
    // close db.
    close();
    
    // remove.
    DB* agent = NULL;
    if (db_create(&agent, NULL, 0) != 0)
    {
        trace_msg("failed to create agent bdb.");
        return false;
    }
    
    if (agent->remove(agent, dbpath.c_str(), NULL, 0) != 0)
    {
        trace_msg("failed to delete db.");
        return false;
    }
    
    // had free while remove.
    //agent->close(agent, DB_NOSYNC);

    return true;
}
Example #3
0
static int
kvs_session_drop(WT_DATA_SOURCE *wtds,
    WT_SESSION *session, const char *uri, WT_CONFIG_ARG *config)
{
	DB *db;
	DATA_SOURCE *ds;
	WT_EXTENSION_API *wt_api;
	int ret = 0;
	const char *name;

	(void)config;				/* Unused parameters */

	ds = (DATA_SOURCE *)wtds;
	wt_api = ds->wt_api;
						/* Get the object name */
	if ((ret = uri2name(wt_api, session, uri, &name)) != 0)
		return (ret);

	if ((ret = single_thread(wtds, session, &ds->rwlock)) != 0)
		return (ret);

	if ((ret = db_create(&db, ds->dbenv, 0)) != 0)
		ESET(wt_api,
		    session, WT_ERROR, "db_create: %s", db_strerror(ret));
	else if ((ret = db->remove(db, name, NULL, 0)) != 0)
		ESET(wt_api,
		    session, WT_ERROR, "Db.remove: %s", db_strerror(ret));
	/* db handle is dead */

	ETRET(unlock(wt_api, session, &ds->rwlock));
	return (ret);
}
Example #4
0
int HB_remove(DB &db, string key) {
	int ret = db.remove(key); //True for success False for fail.
	if (!ret) {
		cout << "DB Error: fail to remove :ret= " << ret << endl;
		return -2;
	}
	return 0; //succeed.
}
Example #5
0
static int remove_function(void *_db, gnutls_datum_t key)
{
    try {
        DB* db = static_cast<DB*>(_db);
    
        if (db->remove( key)==false) return -1;
    } catch(...) {
        return -1;
    }

    return 0;
}
Example #6
0
int Db::remove(const char *file, const char *database, u_int32_t flags)
{
	int err = 0;
	DB *db = unwrap(this);

	if (!db) {
		DB_ERROR("Db::remove", EINVAL, error_policy());
		return (EINVAL);
	}

	// after a DB->remove (no matter if success or failure),
	// the underlying DB object must not be accessed,
	// so we clean up in advance.
	//
	cleanup();

	if ((err = db->remove(db, file, database, flags)) != 0)
		DB_ERROR("Db::remove", err, error_policy());

	return (err);
}
Example #7
0
int
open_db(DB** pDb, const char* pcszDBName, int nClear, int nDuplicate){
	DB*	db;
	int	nRet;
	
	*pDb = NULL;
 opendb:
	/* Initialize BDB handle */
	if ( db_create(&db, NULL, 0) ){
		write_log(LT_BOTH, "Error: Can't init BDB,"
			  "when open database %s!\n", pcszDBName ) ;
		return 0;
	}

	/* if need clear table */
	if ( nClear == 1 ){
		db->remove(db, pcszDBName, NULL, 0);
		nClear = 0;
		goto opendb;
	}

	/* set up error handling for this database */
	db->set_errcall(db, db_err_handler);
	db->set_errpfx(db, "jau_db");
	if ( nDuplicate == 1 )
		db->set_flags(db, DB_DUPSORT);
	
	/* Now open the database */
	nRet = db->open(db, NULL, pcszDBName,
			     NULL, DB_BTREE, DB_CREATE, 0);
	if (nRet != 0) {
		write_log(LT_BOTH, "Error: Can't open database %s!\n",
			  pcszDBName);
		db->close(db, 0);
		return 0;
	}
	*pDb = db;
	
	return 1;
}
Example #8
0
/* {{{ rberkeley_db_remove */
SEXP rberkeley_db_remove (SEXP _dbp, SEXP _file, SEXP _database)
{
  DB *dbp;
  int ret;
  u_int32_t flags = 0;
  const char * database; 
  if(isNull(_database)) {
    database = NULL;
  } else {
    database = (const char *)CHAR(STRING_ELT(_database,0));
  }

  dbp = R_ExternalPtrAddr(_dbp);
  if(R_ExternalPtrTag(_dbp) != RBerkeley_DB || dbp == NULL)
    error("invalid 'db' handle");
  ret = dbp->remove(dbp,
                    (const char *)CHAR(STRING_ELT(_file,0)),
                    database,
                    flags);

  return ScalarInteger(ret);
}
Example #9
0
static int
databaseRemove(const char* file,
               const char* database,
               int         flags)
{
#ifdef TRACE_FUNC_CALLS
    LOG_TRACE_IN;
#endif

    DB* localDbHandle;
    int ret;

    // Removing the database file
    db_create(&localDbHandle, NULL, 0);

    ret = localDbHandle->remove(localDbHandle,
                                file,
                                database,
                                flags);
#ifdef TRACE_FUNC_CALLS
    LOG_TRACE_OUT;
#endif
    return ret;
}
Example #10
0
/*
 *  worker function shared between top10Users and top10Groups
 *
 *  Creates in-memory only database to store temporary results
 *  as long as done == FALSE.  As soon as done is set to TRUE,
 *  generate the top10 lists for the database.
 *
 *  The results parameter is used to pass the database pointer around
 *  while calculating, then changes to an agg_type_t when complete.
 *
 */
static int
do_top10(filinfo_t *filinfo, filvar_t *filvar, boolean_t done,	/* ARGSUSED */
         fm_rptno_t which, fmFuncRes_t *results)
{
    int		st;
    DB		*dbp;
    DBC		*curs;
    DBT		key;
    DBT		data;
    DB_TXN		*txn = NULL;
    agg_size_t	agg;
    agg_list_t	*agglist = NULL;
    agg_size_t	*aggp;
    int		i;
    agg_size_t	*aggreport;

    dbp = results->rptDB;

    if (done == FALSE) {
        if ((filinfo == NULL) || (filvar == NULL)) {
            return (-1);
        }

        if (dbp == NULL) {
            st = db_create(&dbp, dbEnv, 0);
            if (st != 0) {
                return (st);
            }
            /* no duplicates, sort on id */
            st = dbp->set_bt_compare(dbp, bt_compare_uint64);
            if (st != 0) {
                dbp->remove(dbp, NULL, NULL, 0);
                return (st);
            }

            st = dbp->open(dbp, NULL, NULL, NULL, DB_BTREE,
                           db_fl, 0644);
            if (st != 0) {
                dbp->remove(dbp, NULL, NULL, 0);
                return (st);
            }

            results->rptDB = dbp;
        }

        memset(&agg, 0, sizeof (agg_size_t));

        if (which == TOP_USERS) {
            agg.id = filinfo->owner;
        } else {
            agg.id = filinfo->group;
        }

        /* fetch id info if it exists */
        memset(&key, 0, sizeof (DBT));
        memset(&data, 0, sizeof (DBT));

        key.data = &agg.id;
        key.size = sizeof (uint64_t);

        data.data = &agg;
        data.size = data.ulen = sizeof (agg_size_t);
        data.flags = DB_DBT_USERMEM;

        dbEnv->txn_begin(dbEnv, NULL, &txn, 0);
        st = dbp->get(dbp, txn, &key, &data, 0);

        if ((st != 0) && (st != DB_NOTFOUND)) {
            txn->abort(txn);
            return (st);
        }

        agg.count++;
        agg.total_size += filinfo->size;
        agg.total_osize += filinfo->osize;

        st = dbp->put(dbp, txn, &key, &data, 0);
        if (st == 0) {
            st = txn->commit(txn, 0);
        } else {
            txn->abort(txn);
        }
    }

    /* final processing */
    if (done == TRUE) {
        uint64_t	aggid = 0;
        int 		numids = 0;
        agg_list_t	*aggent;
        agg_list_t	*ptr;
        agg_list_t	*last;

        if (dbp == NULL) {
            return (-1);
        }

        dbEnv->txn_begin(dbEnv, NULL, &txn, 0);

        st = dbp->cursor(dbp, txn, &curs, 0);
        if (st != 0) {
            /* blast db? */
            txn->abort(txn);
            return (st);
        }

        memset(&key, 0, sizeof (DBT));
        memset(&data, 0, sizeof (DBT));

        key.data = &aggid;
        key.size = key.ulen = sizeof (uint64_t);
        key.flags = DB_DBT_USERMEM;

        data.flags = DB_DBT_MALLOC;

        /* fetch first entry */
        while ((st = curs->c_get(curs, &key, &data, DB_NEXT)) == 0) {
            aggp = (agg_size_t *)data.data;
            last = NULL;

            if (agglist == NULL) {
                aggent = malloc(sizeof (agg_list_t));
                aggent->agg = aggp;

                aggent->prev = NULL;
                aggent->next = NULL;
                agglist = aggent;

                numids = 1;
                continue;
            }

            ptr = agglist;

            while (ptr != NULL) {
                last = ptr;
                if (aggp->total_size >
                        ptr->agg->total_size) {
                    break;
                }
                ptr = ptr->next;
            }

            if ((ptr != NULL) || (numids < 10)) {
                aggent = malloc(sizeof (agg_list_t));
                if (aggent == NULL) {
                    st = ENOMEM;
                    goto done;
                }
                aggent->agg = aggp;
                aggent->next = ptr;
                if (ptr != NULL) {
                    aggent->prev = ptr->prev;
                    if (ptr->prev != NULL) {
                        ptr->prev->next = aggent;
                    }
                    ptr->prev = aggent;
                } else {
                    aggent->prev = last;
                    if (last != NULL) {
                        last->next = aggent;
                    }
                }

                if (aggent->prev == NULL) {
                    agglist = aggent;
                }
                numids++;
                aggp = NULL;
            }

            if (numids > 10) {
                ptr = agglist;

                while (ptr->next != NULL) {
                    ptr = ptr->next;
                }

                free(ptr->agg);
                ptr->prev->next = NULL;
                free(ptr);
                numids--;
            }

            if (aggp != NULL) {
                free(aggp);
            }
        }

        /* done with the in-mem database now */
        (void) curs->c_close(curs);
        (void) txn->commit(txn, 0);

        (void) dbp->close(dbp, 0);
        results->rptDB = NULL;

        if ((st != 0) && (st != DB_NOTFOUND)) {
            /* something bad happened */
            goto done;
        }

        /* got the top 10, allocate the final structure */
        results->rptSize = 10 * (sizeof (agg_size_t));
        aggreport = calloc(1, results->rptSize);
        results->result = (void *)aggreport;

        if (aggreport == NULL) {
            st = ENOMEM;
            goto done;
        }

        ptr = agglist;

        for (i = 0; ptr != NULL; i++) {
            aggp = ptr->agg;
            memcpy(&aggreport[i], aggp, sizeof (agg_size_t));
            ptr = ptr->next;
        }

        st = 0;
done:
        while (agglist != NULL) {
            ptr = agglist;
            agglist = ptr->next;

            free(ptr->agg);
            free(ptr);
        }

    }

    return (st);
}