Ejemplo n.º 1
0
/* These next two functions are also needed because in db42 these
   are #define macros */
DB_ENV *db_env_cr(u_int32_t flags, int *errno) {
  DB_ENV *envp;
  *errno = db_env_create(&envp, flags);
  return envp;
}
Ejemplo n.º 2
0
int
main(int argc, char *argv[])
{
    /* Initialize our handles */
    DB *dbp = NULL;
    DB_ENV *envp = NULL;

    thread_t writer_threads[NUMWRITERS];
    int ch, i, ret, ret_t;
    u_int32_t env_flags;
    char *db_home_dir;
    /* Application name */
    const char *prog_name = "txn_guide";
    /* Database file name */
    const char *file_name = "mydb.db";

    /* Parse the command line arguments */
#ifdef _WIN32
    db_home_dir = ".\\";
#else
    db_home_dir = "./";
#endif
    while ((ch = getopt(argc, argv, "h:")) != EOF)
        switch (ch) {
        case 'h':
            db_home_dir = optarg;
            break;
        case '?':
        default:
            return (usage());
        }

    /* Create the environment */
    ret = db_env_create(&envp, 0);
    if (ret != 0) {
        fprintf(stderr, "Error creating environment handle: %s\n",
                db_strerror(ret));
        goto err;
    }

    /*
    * Indicate that we want db to perform lock detection internally.
    * Also indicate that the transaction with the fewest number of
    * write locks will receive the deadlock notification in
    * the event of a deadlock.
    */
    ret = envp->set_lk_detect(envp, DB_LOCK_MINWRITE);
    if (ret != 0) {
        fprintf(stderr, "Error setting lock detect: %s\n",
                db_strerror(ret));
        goto err;
    }

    env_flags =
        DB_CREATE     |  /* Create the environment if it does not exist */
        DB_RECOVER    |  /* Run normal recovery. */
        DB_INIT_LOCK  |  /* Initialize the locking subsystem */
        DB_INIT_LOG   |  /* Initialize the logging subsystem */
        DB_INIT_TXN   |  /* Initialize the transactional subsystem. This
			* also turns on logging. */
        DB_INIT_MPOOL |  /* Initialize the memory pool (in-memory cache) */
        DB_THREAD;       /* Cause the environment to be free-threaded */

    /* Now actually open the environment */
    ret = envp->open(envp, db_home_dir, env_flags, 0);
    if (ret != 0) {
        fprintf(stderr, "Error opening environment: %s\n",
                db_strerror(ret));
        goto err;
    }

    /*
     * If we had utility threads (for running checkpoints or
     * deadlock detection, for example) we would spawn those
     * here. However, for a simple example such as this,
     * that is not required.
     */

    /* Open the database */
    ret = open_db(&dbp, prog_name, file_name,
                  envp, DB_DUPSORT);
    if (ret != 0)
        goto err;

    /* Initialize a mutex. Used to help provide thread ids. */
    (void)mutex_init(&thread_num_lock, NULL);

    /* Start the writer threads. */
    for (i = 0; i < NUMWRITERS; i++)
        (void)thread_create(
            &writer_threads[i], NULL, writer_thread, (void *)dbp);

    /* Join the writers */
    for (i = 0; i < NUMWRITERS; i++)
        (void)thread_join(writer_threads[i], NULL);

err:
    /* Close our database handle, if it was opened. */
    if (dbp != NULL) {
        ret_t = dbp->close(dbp, 0);
        if (ret_t != 0) {
            fprintf(stderr, "%s database close failed: %s\n",
                    file_name, db_strerror(ret_t));
            ret = ret_t;
        }
    }

    /* Close our environment, if it was opened. */
    if (envp != NULL) {
        ret_t = envp->close(envp, 0);
        if (ret_t != 0) {
            fprintf(stderr, "environment close failed: %s\n",
                    db_strerror(ret_t));
            ret = ret_t;
        }
    }

    /* Final status message and return. */
    printf("I'm all done.\n");
    return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
Ejemplo n.º 3
0
/*
 * csv_env_init --
 *	Initialize the database environment.
 */
int
csv_env_open(const char *home, int is_rdonly)
{
	int ret;

	dbenv = NULL;
	db = NULL;

	/* Create a database environment handle. */
	if ((ret = db_env_create(&dbenv, 0)) != 0) {
		fprintf(stderr,
		    "%s: db_env_create: %s\n", progname, db_strerror(ret));
		return (1);
	}

	/*
	 * Configure Berkeley DB error reporting to stderr, with our program
	 * name as the prefix.
	 */
	dbenv->set_errfile(dbenv, stderr);
	dbenv->set_errpfx(dbenv, progname);

	/*
	 * The default Berkeley DB cache size is fairly small; configure a
	 * 1MB cache for now.  This value will require tuning in the future.
	 */
	if ((ret = dbenv->set_cachesize(dbenv, 0, 1048576, 1)) != 0) {
		dbenv->err(dbenv, ret, "DB_ENV->set_cachesize");
		return (1);
	}

	/*
	 * We may be working with an existing environment -- try and join it.
	 * If that fails, create a new database environment; for now, we only
	 * need a cache, no logging, locking, or transactions.
	 */
	if ((ret = dbenv->open(dbenv, home,
	    DB_JOINENV | DB_USE_ENVIRON, 0)) != 0 &&
	    (ret = dbenv->open(dbenv, home,
	    DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0) {
		dbenv->err(dbenv, ret, "DB_ENV->open");
		return (1);
	}

	/* Create the primary database handle. */
	if ((ret = db_create(&db, dbenv, 0)) != 0) {
		dbenv->err(dbenv, ret, "db_create");
		return (1);
	}

	/*
	 * Records may be relatively large -- use a large page size.
	 */
	if ((ret = db->set_pagesize(db, 32 * 1024)) != 0) {
		dbenv->err(dbenv, ret, "DB->set_pagesize");
		return (1);
	}

	/*
	 * The primary database uses an integer as its key; on little-endian
	 * machines, integers sort badly using the default Berkeley DB sort
	 * function (which is lexicographic).  Specify a comparison function
	 * for the database.
	 */
	if ((ret = db->set_bt_compare(db, compare_uint32)) != 0) {
		dbenv->err(dbenv, ret, "DB->set_bt_compare");
		return (1);
	}

	/* Open the primary database. */
	if ((ret = db->open(db, NULL,
	    "primary", NULL, DB_BTREE, is_rdonly ? 0 : DB_CREATE, 0)) != 0) {
		dbenv->err(dbenv, ret, "DB->open: primary");
		return (1);
	}

	/* Open the secondaries.  */
	if ((ret = csv_secondary_open()) != 0)
		return (1);

	return (0);
}
Ejemplo n.º 4
0
static struct nb_db *
nb_db_berkeleydb_open(const struct nb_db_opts *opts)
{
	struct nb_db_berkeleydb *berkeleydb = malloc(sizeof(*berkeleydb));
	if (berkeleydb == NULL) {
		fprintf(stderr, "malloc(%zu) failed", sizeof(*berkeleydb));
		goto error_1;
	}

	int r;
	r = mkdir(opts->path, 0777);
	if (r != 0 && errno != EEXIST) {
		fprintf(stderr, "mkdir: %d\n", r);
		goto error_1;
	}

	DB_ENV *env;
	DB *db;

	int env_flags = DB_REGION_INIT;
	r = db_env_create(&env, 0);
	if (r != 0) {
		fprintf(stderr, "db_env_create: %s\n",
			db_strerror(r));
		goto error_1;
	}
	r = env->set_cachesize(env, 0, 0, 1);
	if (r != 0) {
		fprintf(stderr, "set_cachesize: %s\n",
			db_strerror(r));
		goto error_2;
	}
	env_flags |= DB_TXN_WRITE_NOSYNC;
	r = env->set_flags(env, env_flags, 1);
	if (r != 0) {
		fprintf(stderr, "set_flags: %s\n",
			db_strerror(r));
		goto error_2;
	}
	int log_flags = DB_LOG_AUTO_REMOVE;
	r = env->log_set_config(env, log_flags, 1);

	int env_open_flags = DB_CREATE|DB_INIT_MPOOL;
	r = env->open(env, opts->path, env_open_flags, 0666);
	if (r != 0) {
		fprintf(stderr, "env->open: %s\n",
			db_strerror(r));
		goto error_2;
	}
	r = db_create(&db, env, 0);
	if (r != 0) {
		fprintf(stderr, "db_create: %s\n",
			db_strerror(r));
		goto error_2;
	}
	int open_flags = DB_CREATE;
	r = db->open(db, NULL, "data.bdb", NULL, DB_BTREE, open_flags, 0664);
	if (r != 0) {
		fprintf(stderr, "db->open: %s\n",
			db_strerror(r));
		goto error_3;
	}

	berkeleydb->env = env;
	berkeleydb->db = db;
	return (struct nb_db *) berkeleydb;

error_3:
	db->close(db, 0);
error_2:
	env->close(env, 0);
error_1:
	return NULL;
}
Ejemplo n.º 5
0
static isc_result_t
bdb_create(const char *dlzname, unsigned int argc, char *argv[],
	   void *driverarg, void **dbdata)
{
	isc_result_t result;
	int bdbres;
	bdb_instance_t *db = NULL;

	UNUSED(dlzname);
	UNUSED(driverarg);

	/* verify we have 3 arg's passed to the driver */
	if (argc != 3) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "Berkeley DB driver requires at least "
			      "2 command line args.");
		return (ISC_R_FAILURE);
	}

	/* allocate and zero memory for driver structure */
	db = isc_mem_get(ns_g_mctx, sizeof(bdb_instance_t));
	if (db == NULL) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "Could not allocate memory for "
			      "database instance object.");
		return (ISC_R_NOMEMORY);
	}
	memset(db, 0, sizeof(bdb_instance_t));

	/* attach to the memory context */
	isc_mem_attach(ns_g_mctx, &db->mctx);

	/* create BDB environment
	 * Basically BDB allocates and assigns memory to db->dbenv
	 */
	bdbres = db_env_create(&db->dbenv, 0);
	if (bdbres != 0) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "BDB environment could not be created. "
			      "BDB error: %s",
			      db_strerror(bdbres));
		result = ISC_R_FAILURE;
		goto init_cleanup;
	}

	/* open BDB environment */
	bdbres = db->dbenv->open(db->dbenv, argv[1],
				 DB_INIT_CDB | DB_INIT_MPOOL |
				 bdb_threads | DB_CREATE,
				 0);
	if (bdbres != 0) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "BDB environment at '%s' could not be opened. "
			      "BDB error: %s",
			      argv[1], db_strerror(bdbres));
		result = ISC_R_FAILURE;
		goto init_cleanup;
	}

	/* open dlz_data database. */
	result = bdb_opendb(db->dbenv, DB_UNKNOWN, &db->data,
			    dlz_data, argv[2], 0);
	if (result != ISC_R_SUCCESS)
		goto init_cleanup;

	/* open dlz_host database. */
	result = bdb_opendb(db->dbenv, DB_UNKNOWN, &db->host,
			    dlz_host, argv[2],
			    DB_DUP | DB_DUPSORT);
	if (result != ISC_R_SUCCESS)
		goto init_cleanup;

	/* open dlz_zone database. */
	result = bdb_opendb(db->dbenv, DB_UNKNOWN, &db->zone,
			    dlz_zone, argv[2],
			    DB_DUP | DB_DUPSORT);
	if (result != ISC_R_SUCCESS)
		goto init_cleanup;

	/* open dlz_client database. */
	result = bdb_opendb(db->dbenv, DB_UNKNOWN, &db->client,
			    dlz_client, argv[2], DB_DUP | DB_DUPSORT);
	if (result != ISC_R_SUCCESS)
		goto init_cleanup;

	/* associate the host secondary database with the primary database */
	bdbres = db->data->associate(db->data, NULL, db->host, NULL, 0);
	if (bdbres != 0) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "BDB could not associate %s database with %s. "
			      "BDB error: %s",
			      dlz_host, dlz_data, db_strerror(bdbres));
		result = ISC_R_FAILURE;
		goto init_cleanup;
	}

	/* associate the zone secondary database with the primary database */
	bdbres = db->data->associate(db->data, NULL, db->zone, NULL, 0);
	if (bdbres != 0) {
		isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
			      DNS_LOGMODULE_DLZ, ISC_LOG_ERROR,
			      "BDB could not associate %s database with %s. "
			      "BDB error: %s",
			      dlz_zone, dlz_data, db_strerror(bdbres));
		result = ISC_R_FAILURE;
		goto init_cleanup;
	}

	*dbdata = db;

	return(ISC_R_SUCCESS);

 init_cleanup:

	bdb_cleanup(db);
	return result;
}
Ejemplo n.º 6
0
/*
 * Deletes all data and environment files of the given Btree.  Requires
 * that there are no other handles using the BtShared when this function
 * is called.
 */
int btreeDeleteEnvironment(Btree *p, const char *home, int rename)
{
	BtShared *pBt;
	int rc, ret, iDb, storage;
	sqlite3 *db;
	DB_ENV *tmp_env;
	char path[BT_MAX_PATH];
#ifdef BDBSQL_FILE_PER_TABLE
	int numFiles;
	char **files;
#endif

	rc = SQLITE_OK;
	ret = 0;
	tmp_env = NULL;

	if (p != NULL) {
		if ((rc = btreeUpdateBtShared(p, 1)) != SQLITE_OK)
			goto err;
		pBt = p->pBt;
		if (pBt->nRef > 1)
			return SQLITE_BUSY;

		storage = pBt->dbStorage;
		db = p->db;
		for (iDb = 0; iDb < db->nDb; iDb++) {
			if (db->aDb[iDb].pBt == p)
				break;
		}
		if ((rc = sqlite3BtreeClose(p)) != SQLITE_OK)
			goto err;
		pBt = NULL;
		p = NULL;
		db->aDb[iDb].pBt = NULL;
	}
	if (home == NULL)
		goto done;

	sqlite3_snprintf(sizeof(path), path, "%s-journal", home);
	ret = btreeCleanupEnv(path);
	/* EFAULT can be returned on Windows when the file does not exist.*/
	if (ret == ENOENT || ret == EFAULT)
		ret = 0;
	else if (ret != 0)
		goto err;

	if ((ret = db_env_create(&tmp_env, 0)) != 0)
		goto err;

	if (rename) {
		if (!(ret = __os_exists(tmp_env->env, home, 0))) {
			sqlite3_snprintf(sizeof(path), path,
			    "%s%s", home, BACKUP_SUFFIX);
			ret = __os_rename(tmp_env->env, home, path, 0);
		}
	} else {
#ifdef BDBSQL_FILE_PER_TABLE
		ret = __os_dirlist(tmp_env->env, home, 0, &files, &numFiles);
		if (ret == 0) {
			int i, ret2;
			for (i = 0; i < numFiles; i++) {
				sqlite3_snprintf(sizeof(path), path, "%s/%s",
				    home, files[i]);
				if ((ret2 = __os_unlink (tmp_env->env, path, 0))
				    != 0)
					ret = ret2;
			}
		}
		__os_dirfree(tmp_env->env, files, numFiles);
		if (ret == 0)
			ret = __os_unlink(tmp_env->env, home, 0);

#else
		if (!(ret = __os_exists(tmp_env->env, home, 0)))
			ret = __os_unlink(tmp_env->env, home, 0);
#endif
	}
	/* EFAULT can be returned on Windows when the file does not exist.*/
	if (ret == ENOENT || ret == EFAULT)
		ret = 0;
	else if (ret != 0)
		goto err;

err:
done:	if (tmp_env != NULL)
		tmp_env->close(tmp_env, 0);

	return MAP_ERR(rc, ret, p);
}
Ejemplo n.º 7
0
int main(void)
{
  int ret, ret_c;
  u_int32_t db_flags, env_flags;
  DB *dbp;
  DB_ENV *envp;
  DBT key, data;
  DB_TXN *txn;
  const char *db_home_dir = "/tmp/mai-test-good-1/";
  const char *file_name = "mydb.db";
  const char keystr[BUF_SIZE];
  const char datastr[BUF_SIZE];
  int i = 0;

  dbp = NULL;
  envp = NULL;

  /* Open the environment */
  ret = db_env_create(&envp, 0);
  if (ret != 0) {
    fprintf(stderr, "Error creating environment handle: %s\n",
        db_strerror(ret));
    return (EXIT_FAILURE);
  }

  env_flags = DB_CREATE | /* Create the environment if it does
                           * not already exist. */
    DB_INIT_TXN | /* Initialize transactions */
    DB_INIT_LOCK | /* Initialize locking. */
    DB_INIT_LOG | /* Initialize logging */
    DB_INIT_MPOOL; /* Initialize the in-memory cache. */
  ret = envp->open(envp, db_home_dir, env_flags, 0);
  if (ret != 0) {
    fprintf(stderr, "Error opening environment: %s\n",
        db_strerror(ret));
    goto err;
  }

  /* Initialize the DB handle */
  ret = db_create(&dbp, envp, 0);
  if (ret != 0) {
    envp->err(envp, ret, "Database creation failed");
    goto err;
  }

  db_flags = DB_CREATE | DB_AUTO_COMMIT;
  /*
     Open the database. Note that we are using auto commit for the open,
     so the database is able to support transactions.
     */
  ret = dbp->open(dbp, /* Pointer to the database */
      NULL, /* Txn pointer */
      file_name, /* File name */
      NULL, /* Logical db name */
      DB_BTREE, /* Database type (using btree) */
      db_flags, /* Open flags */
      0); /* File mode. Using defaults */
  if (ret != 0) {
    envp->err(envp, ret, "Database '%s' open failed",
        file_name);
    goto err;
  }

  /* Get the txn handle */
  txn = NULL;
  ret = envp->txn_begin(envp, NULL, &txn, 0);
  if (ret != 0) {
    envp->err(envp, ret, "Transaction begin failed.");
    goto err;
  }

  for (i = 0; i < LOOP_SIZE; i++) {
    /* Prepare the DBTs */
    memset(&key, 0, sizeof(DBT));
    memset(&data, 0, sizeof(DBT));

    sprintf((char*)keystr, "key%d", i);
    sprintf((char*)datastr, "data%d", i);
  
    key.data = (char*)keystr;
    key.size = strlen((char*)keystr) + 1;
    data.data = (char*)datastr;
    data.size = strlen((char*)datastr) + 1;
  
    /* Perform the database write. If this fails, abort the transaction. */
    ret = dbp->put(dbp, txn, &key, &data, 0);
    if (ret != 0) {
      envp->err(envp, ret, "Database put failed.");
      txn->abort(txn);
      goto err;
    }
  }

  /* Commit the transaction. Note that the transaction handle
     can no longer be used. */
  ret = txn->commit(txn, 0);
  if (ret != 0) {
    envp->err(envp, ret, "Transaction commit failed.");
    goto err;
  }

err:
  /* Close the database */
  if (dbp != NULL) {
    ret_c = dbp->close(dbp, 0);
    if (ret_c != 0) {
      envp->err(envp, ret_c, "Database close failed.");
      ret = ret_c;
    }
  }

  /* Close the environment */
  if (envp != NULL) {
    ret_c = envp->close(envp, 0);
    if (ret_c != 0) {
      fprintf(stderr, "environment close failed: %s\n",
          db_strerror(ret_c));
      ret = ret_c;
    }
  }

  return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
Ejemplo n.º 8
0
static void insert_db(const char *file, const uint8_t type)
{
   DB *db = NULL;
   DBT key, data;
   DB_ENV *env;
   union {
      IP_LIST ip_list;
      DOMAIN_LIST dom_list;
   } u;
   u_int32_t i, j;
   char buf[350];
   FILE *fd = NULL;
   
   memset(buf, '\0', sizeof(buf));
   
   fd = fopen(file, "r");
   
   if ((i = db_env_create(&env, 0)) != 0) {
        fprintf(stderr, "db_input: %s\n", db_strerror(i));
        return;
   }
    
   if ((i = env->open(env, DB_ENVIRONMENT,
              DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL | DB_THREAD, 0)) != 0) {
       fprintf(stderr, "environment open: %s\n", DB_ENVIRONMENT);
       goto err;
   }
    
   if ((i = db_create(&db, env, 0)) != 0) {
       fprintf(stderr, "database create\n");
       goto err;
   }
    
   if (!strcmp(file, "ip_list"))
     strncpy(buf, IP_DB_NAME, 32);
   else
     strncpy(buf, DOMAIN_DB_NAME, 32);
    
   if ((i = db->open(db, NULL, buf, NULL, DB_BTREE,
              DB_CREATE | DB_THREAD, 0644)) != 0) {
       fprintf(stderr, "DB->open: %s\n", buf);
       goto err;
   }
   
   uint32_t idnum = 0;
   //get idnum
   memset(&key, 0, sizeof(DBT));
   memset(&data, 0, sizeof(DBT));
   
   if (!strcmp(file, "dom_list")) {
      key.data = ID_NUM;
      key.size = 2;
      data.data = &idnum;
      data.ulen = sizeof(uint32_t);
      data.flags = DB_DBT_USERMEM;
            
      i = db->get(db, NULL, &key, &data, 0);
      if (i != 0)
         idnum = 0;
      else
         idnum++;
   }
      
   while (fgets(buf, 350, fd)) {
      j = strlen(buf);
      memset(buf +(j-1), '\0', 1);
      
      if (!strcmp(file, "ip_list")) {
         memset(&u.ip_list, 0, sizeof(IP_LIST));
         
         u.ip_list.id = 0;
         u.ip_list.type = type;
         u.ip_list.flag = DB_ENABLE | DB_WHOLE_MATCH;
      } else {
         memset(&u.dom_list, 0, sizeof(DOMAIN_LIST));
         idnum++;
         
         u.dom_list.id = idnum;
         u.dom_list.ttl = time(NULL);
         u.dom_list.type = type;
         u.dom_list.flag = DB_ENABLE | DB_WHOLE_MATCH;
      }
            
      memset(&key, 0, sizeof(DBT));
      memset(&data, 0, sizeof(DBT));
           
      if (!strcmp(file, "ip_list")) {
         u_int32_t addr;
         if (inet_pton(AF_INET, buf, &addr)) {
            key.data = &addr;
            key.size = sizeof(uint32_t);
         
            data.data = &u.ip_list;
            data.size = sizeof(IP_LIST);
         }
      } else {
         mirror(buf);
         
         key.data = buf;
         key.size = j;
      
         data.data = &u.dom_list;
         data.size = sizeof(DOMAIN_LIST);
      }
         
      j = db->put(db, NULL, &key, &data, DB_NOOVERWRITE);
      if (j == DB_KEYEXIST)
         fprintf(stderr, "Put failed because key %s already exists\n", buf);
   }
   
   fclose(fd);
   // save ID_NUM
   if (!strcmp(file, "dom_list")) {
      memset(&key, 0, sizeof(DBT));
      memset(&data, 0, sizeof(DBT));
      
      key.data = ID_NUM;
      key.size = 2;
      data.data = &idnum;
      data.size = sizeof(uint32_t);
      
      // cek jika blom ada maka put, kalau sudah ada maka overwrite
      j = db->put(db, NULL, &key, &data, DB_OVERWRITE_DUP);
   }
   
   config_db(env);
   
   err:
   if (db)
      db->close(db, 0);
   if (env)
      env->close(env, 0);
      
   printf("Done.\n");
}
static int create_dbenv_handle(DB_ENV **dbenvpp) {
	int ret;
	if ((ret = db_env_create(dbenvpp, 0)) == 0)
		info.dbenvp = *dbenvpp;
	return ret;
}
Ejemplo n.º 10
0
int
b_recover(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind;
	DB *dbp;
	DBT key, data;
	DB_ENV *dbenv;
	DB_TXN *txn;
	u_int32_t cachesize;
	int ch, i, count;

	/*
	 * Recover was too slow before release 4.0 that it's not worth
	 * running the test.
	 */
#if DB_VERSION_MAJOR < 4
	return (0);
#endif
	cachesize = MEGABYTE;
	count = 1000;
	while ((ch = getopt(argc, argv, "C:c:")) != EOF)
		switch (ch) {
		case 'C':
			cachesize = (u_int32_t)atoi(optarg);
			break;
		case 'c':
			count = atoi(optarg);
			break;
		case '?':
		default:
			return (usage());
		}
	argc -= optind;
	argv += optind;
	if (argc != 0)
		return (usage());

	/* Create the environment. */
	DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
	dbenv->set_errfile(dbenv, stderr);
	DB_BENCH_ASSERT(dbenv->set_cachesize(dbenv, 0, cachesize, 0) == 0);

#define	OFLAGS								\
	(DB_CREATE | DB_INIT_LOCK |					\
	DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE)
#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0
	DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, NULL, OFLAGS, 0666) == 0);
#endif
#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 1
	DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, OFLAGS, 0666) == 0);
#endif
#if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 1
	DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR, OFLAGS, 0666) == 0);
#endif

	/* Create the database. */
	DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0);
#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
	DB_BENCH_ASSERT(dbp->open(dbp, NULL,
	    TESTFILE, NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, 0666) == 0);
#else
	DB_BENCH_ASSERT(
	    dbp->open(dbp, TESTFILE, NULL, DB_BTREE, DB_CREATE, 0666) == 0);
#endif

	/* Initialize the data. */
	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	key.size = data.size = 20;
	key.data = data.data = "01234567890123456789";

	/* Start/commit a transaction count times. */
	for (i = 0; i < count; ++i) {
#if DB_VERSION_MAJOR < 4
		DB_BENCH_ASSERT(
		    txn_begin(dbenv, NULL, &txn, DB_TXN_NOSYNC) == 0);
		DB_BENCH_ASSERT(dbp->put(dbp, txn, &key, &data, 0) == 0);
		DB_BENCH_ASSERT(txn_commit(txn, 0) == 0);
#else
		DB_BENCH_ASSERT(
		    dbenv->txn_begin(dbenv, NULL, &txn, DB_TXN_NOSYNC) == 0);
		DB_BENCH_ASSERT(dbp->put(dbp, txn, &key, &data, 0) == 0);
		DB_BENCH_ASSERT(txn->commit(txn, 0) == 0);
#endif
	}

	DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
	DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);

	/* Create a new DB_ENV handle. */
	DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
	dbenv->set_errfile(dbenv, stderr);
	DB_BENCH_ASSERT(
	    dbenv->set_cachesize(dbenv, 0, 1048576 /* 1MB */, 0) == 0);

	/* Now run recovery. */
	TIMER_START;
#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0
	DB_BENCH_ASSERT(dbenv->open(
	    dbenv, TESTDIR, NULL, OFLAGS | DB_RECOVER, 0666) == 0);
#endif
#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 1
	DB_BENCH_ASSERT(
	    dbenv->open(dbenv, TESTDIR, OFLAGS | DB_RECOVER, 0666) == 0);
#endif
#if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 1
	DB_BENCH_ASSERT(
	    dbenv->open(dbenv, TESTDIR, OFLAGS | DB_RECOVER, 0666) == 0);
#endif
	TIMER_STOP;

	/*
	 * We divide the time by the number of transactions, so an "operation"
	 * is the recovery of a single transaction.
	 */
	printf("# recovery after %d transactions\n", count);
	TIMER_DISPLAY(count);

	DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);

	return (0);
}
Ejemplo n.º 11
0
/*
 * Called to initialize the driver
 */
isc_result_t
dlz_create(const char *dlzname, unsigned int argc, char *argv[],
	   void **dbdata, ...)
{
  isc_result_t result;
  int bdbhptres;
  int bdbFlags = 0;
  bdbhpt_instance_t *db = NULL;
  
  const char *helper_name;
  va_list ap;

  UNUSED(dlzname);

  /* Allocate memory for our db structures and helper functions */
  db = calloc(1, sizeof(struct bdbhpt_instance));
  if (db == NULL)
    return (ISC_R_NOMEMORY);

  /* Fill in the helper functions */
  va_start(ap, dbdata);
  while ((helper_name = va_arg(ap, const char *)) != NULL) {
    b9_add_helper(db, helper_name, va_arg(ap, void*));
  }
  va_end(ap);

  /* verify we have 4 arg's passed to the driver */
  if (argc != 4) {
    db->log(ISC_LOG_ERROR,
            "bdbhpt_dynamic: please supply 3 command line args.  "
            "You supplied: %s",
            argc);
    return (ISC_R_FAILURE);
  }

  switch((char) *argv[1]) {
    /*
     * Transactional mode.  Highest safety - lowest speed.
     */
  case 'T':
  case 't':
    bdbFlags = DB_INIT_MPOOL | DB_INIT_LOCK |
               DB_INIT_LOG | DB_INIT_TXN;
    db->log(ISC_LOG_INFO,
            "bdbhpt_dynamic: using transactional mode.");
    break;

    /*
     * Concurrent mode.  Lower safety (no rollback) -
     * higher speed.
     */
  case 'C':
  case 'c':
    bdbFlags = DB_INIT_CDB | DB_INIT_MPOOL;
    db->log(ISC_LOG_INFO,
            "bdbhpt_dynamic: using concurrent mode.");
    break;

    /*
     * Private mode. No inter-process communication & no locking.
     * Lowest saftey - highest speed.
     */
  case 'P':
  case 'p':
    bdbFlags = DB_PRIVATE | DB_INIT_MPOOL;
    db->log(ISC_LOG_INFO,
            "bdbhpt_dynamic: using private mode.");
    break;
  default:
    db->log(ISC_LOG_ERROR,
            "bdbhpt_dynamic: operating mode must be set to P or C or T.  "
            "You specified '%s'",
            argv[1]);
    return (ISC_R_FAILURE);
  }
  
  /*
   * create bdbhpt environment
   * Basically bdbhpt allocates and assigns memory to db->dbenv
   */
  bdbhptres = db_env_create(&db->dbenv, 0);
  if (bdbhptres != 0) {
    db->log(ISC_LOG_ERROR,
            "bdbhpt_dynamic: db environment could not be created. "
            "BerkeleyDB error: %s",
            db_strerror(bdbhptres));
    result = ISC_R_FAILURE;
    goto init_cleanup;
  }
  
  /* open bdbhpt environment */
  bdbhptres = db->dbenv->open(db->dbenv, argv[2],
                              bdbFlags | bdbhpt_threads | DB_CREATE, 0);
  if (bdbhptres != 0) {
    db->log(ISC_LOG_ERROR,
            "bdbhpt_dynamic: db environment at '%s' could not be opened. "
            "BerkeleyDB error: %s",
            argv[2], db_strerror(bdbhptres));
    result = ISC_R_FAILURE;
    goto init_cleanup;
  }

  /* open dlz_data database. */
  result = bdbhpt_opendb(db->log, db->dbenv, DB_UNKNOWN, &db->data,
                         dlz_data, argv[3], DB_DUP | DB_DUPSORT);
  if (result != ISC_R_SUCCESS)
    goto init_cleanup;

  /* open dlz_xfr database. */
  result = bdbhpt_opendb(db->log, db->dbenv, DB_UNKNOWN, &db->xfr,
                         dlz_xfr, argv[3], DB_DUP | DB_DUPSORT);
  if (result != ISC_R_SUCCESS)
    goto init_cleanup;
  
  /* open dlz_zone database. */
  result = bdbhpt_opendb(db->log, db->dbenv, DB_UNKNOWN, &db->zone,
                         dlz_zone, argv[3], 0);
  if (result != ISC_R_SUCCESS)
    goto init_cleanup;
  
  /* open dlz_client database. */
  result = bdbhpt_opendb(db->log, db->dbenv, DB_UNKNOWN, &db->client,
                         dlz_client, argv[3], DB_DUP | DB_DUPSORT);
  if (result != ISC_R_SUCCESS)
    goto init_cleanup;
  
  *dbdata = db;

  db->log(ISC_LOG_INFO,
          "bdbhpt_dynamic: version %s, started", dlz_bdbhpt_dynamic_version);
  return(ISC_R_SUCCESS);
  
 init_cleanup:
  bdbhpt_cleanup(db);
  return result;
}
Ejemplo n.º 12
0
int
cache_init(cache_t *c, void (*freevalue)(void *value))
{
	memset(c, '\0', sizeof(*c));

	int ret;


	DB_ENV *dbenv;

  	ret = db_env_create(&dbenv, 0);
  	      dbenv->err(dbenv,ret,"err db_env_create ");

	dbenv->set_errfile(dbenv, stderr);
		dbenv->err(dbenv,ret,"err set_errfile ");

        if ((ret = dbenv->set_shm_key(dbenv, 664)) != 0) {
		dbenv->err(dbenv,ret,"err set_shm_key ");

		return 0;
        }


  	ret = dbenv->open(dbenv,bfile(_temp_path),DB_CREATE | DB_INIT_MPOOL | DB_INIT_CDB ,0);
  	     dbenv->err(dbenv,ret,"err db_env_open ");


        /* Create and initialize database object */
        if ((ret = db_create(&c->c_data, dbenv, 0)) != 0) {
                fprintf(stderr,
                     "%s: db_create: %s\n", "bbdocument", db_strerror(ret));
 		return 0;
        }


	#ifdef DEBUG
	dbenv->stat_print(dbenv, DB_STAT_ALL);
	#endif

        /* open the database. */
        if ((ret = c->c_data->open(c->c_data, NULL, "libcachedb", NULL, DB_BTREE, DB_CREATE, 0)) != 0) {
                        c->c_data->err(c->c_data, ret, "db open");
                        //goto err1;
                        //dette skjer nor collection mappen ikke er opprettet enda, typisk forde vi ikke har lagret et dokument der enda
                        #ifdef DEBUG
                        printf("can't dbp->open(), but db_create() was sucessful!\n");
                        #endif

                        return 0;
        }


	#ifdef DEBUG
	c->c_data->stat_print(c->c_data, DB_STAT_ALL);
	#endif

	pthread_mutex_init(&c->c_lock, NULL);
	c->c_freevalue = freevalue;

	return 1;
}
Ejemplo n.º 13
0
/*
 *  This should be done before the program is open for business.
 *  As such, it does not need to be reentrant.
 */
int
open_db_env(char *topdir)
{
	DB_ENV		*tmpenv = NULL;
	int		st;
	struct stat64	sbuf;
	char		logdir[MAXPATHLEN+1];
	char		tmpdir[MAXPATHLEN+1];
	char		*dirarr[3];
	int		i;

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

	snprintf(logdir, sizeof (tmpdir), "%s/.logs", topdir);
	snprintf(tmpdir, sizeof (tmpdir), "%s/.tmp", topdir);

	dirarr[0] = topdir;
	dirarr[1] = logdir;
	dirarr[2] = tmpdir;

	/* first, set up the environment */
	st = db_env_create(&tmpenv, 0);

	if (st != 0) {
		return (st);
	}

	/* make sure the directories exist */
	for (i = 0; i < 3; i++) {
		st = stat64(dirarr[i], &sbuf);
		if ((st != 0) && (errno == ENOENT)) {
			st = mkdirp(dirarr[i], 0744);
			if (st == 0) {
				st = stat64(dirarr[i], &sbuf);
			}
		}
		if ((st == 0) && (!S_ISDIR(sbuf.st_mode))) {
			st = -1;
			break;
		}
	}

	if (st != 0) {
		return (st);
	}

	st = tmpenv->set_data_dir(tmpenv, topdir);
	if (st != 0) {
		return (st);
	}
	st = tmpenv->set_lg_dir(tmpenv, logdir);
	if (st != 0) {
		return (st);
	}

	st = tmpenv->set_tmp_dir(tmpenv, tmpdir);
	if (st != 0) {
		return (st);
	}

	st = tmpenv->set_flags(tmpenv, env_fl, 1);
	if (st != 0) {
		return (st);
	}

	/* overall database cache size */
	st = tmpenv->set_cachesize(tmpenv, 0, (60 * MEGA), 1);

	st = tmpenv->set_shm_key(tmpenv, FSM_SHM_MASTER_KEY);
	if (st != 0) {
		return (st);
	}

	/* log buffer in memory */
	st = tmpenv->set_lg_bsize(tmpenv, (30 * MEGA));
	if (st != 0) {
		return (st);
	}

	/* set up additional error logging */
	tmpenv->set_errcall(tmpenv, fsmdb_log_err);

	/* Increase the number of locks available */
	tmpenv->set_lk_max_locks(tmpenv, 10000);
	tmpenv->set_lk_max_lockers(tmpenv, 10000);
	tmpenv->set_lk_max_objects(tmpenv, 10000);

	/* Increase the number of concurrent transactions available */
	/* Note:  Default in 4.4-20 is '20'.  In later versions it's 100 */
	tmpenv->set_tx_max(tmpenv, 100);

	st = tmpenv->open(tmpenv, topdir, env_ofl, 0644);
	if (st != 0) {
		/* check for a major failure */
		if (st == DB_RUNRECOVERY) {
			st = tmpenv->open(tmpenv, topdir, env_ffl, 0644);
		}
		/* log catastrophic failure and remove all db files. */
		if (st == DB_RUNRECOVERY) {
			fsmdb_log_err(dbEnv, NULL,
			    "Database files corrupt, cannot recover.  "
			    "Files will be removed.  Please re-index any "
			    "recovery points to regenerate the database.");
			fsmdb_remove_all(topdir);
		}
	}

	if (st != 0) {
		return (st);
	}

	/* clear out unneeded log files */
	tmpenv->log_archive(tmpenv, NULL, DB_ARCH_REMOVE);

	/* all set, ready to use */
	dbEnv = tmpenv;

	return (st);
}
Ejemplo n.º 14
0
int main(int argc, char *argv[])
{

DB_ENV *myEnv; /* Env structure handle */
DB *dbp; /* DB structure handle */
u_int32_t db_flags; /* database open flags */
u_int32_t env_flags; /* env open flags */
int ret; /* function return value */
/*
Create an environment object and initialize it for error
reporting.
*/
ret = db_env_create(&myEnv, 0);
if (ret != 0) {
fprintf(stderr, "Error creating env handle: %s\n", db_strerror(ret));
return -1;
}
/* Open the environment. */
env_flags = DB_CREATE | /* If the environment does not exist,
* create it. */
DB_INIT_MPOOL; /* Initialize the in-memory cache. */
ret = myEnv->open(myEnv, /* DB_ENV ptr */
"/export1/testEnv", /* env home directory */
env_flags, /* Open flags */
0); /* File mode (default) */
if (ret != 0) {
fprintf(stderr, "Environment open failed: %s", db_strerror(ret));
return -1;
}

/*
Once an environment is opened, you can open databases in it. Note that by default databases
are stored in the environment's home directory, or relative to that directory if you provide any
sort of a path in the database's file name:
Library Version 11.2.5.3 Databases
12/19/2011 Getting Started with DB Page 16
/*
* Initialize the DB structure. Pass the pointer
* to the environment in which this DB is opened.
*/
ret = db_create(&dbp, myEnv, 0);
if (ret != 0) {
/* Error handling goes here */
}
/* Database open flags */
db_flags = DB_CREATE; /* If the database does not exist,
* create it.*/
/* open the database */
ret = dbp->open(dbp, /* DB structure pointer */
NULL, /* Transaction pointer */
"my_db.db", /* On-disk file that holds the database. */
NULL, /* Optional logical database name */
DB_BTREE, /* Database access method */
db_flags, /* Open flags */
0); /* File mode (using defaults) */
if (ret != 0) {
/* Error handling goes here */
}

/*
When you are done with an environment, you must close it. It is recommended that before
closing an environment, you close any open databases.
*/
/*
* Close the database and environment
*/
if (dbp != NULL) {
dbp->close(dbp, 0);
}
if (myEnv != NULL) {
myEnv->close(myEnv, 0);
}

}
Ejemplo n.º 15
0
static int 
bdb_init_tx_handle(struct storage* s, char* db_env_path)
{
	int result;
	DB_ENV* dbenv; 
	
	//Create environment handle
	result = db_env_create(&dbenv, 0);
	if (result != 0) {
		paxos_log_error("DB_ENV creation failed: %s", db_strerror(result));
		return -1;
	}
	
	//Durability mode
	if (!paxos_config.bdb_sync)
		result = dbenv->set_flags(dbenv, DB_TXN_WRITE_NOSYNC, 1);
	
	if (result != 0) {
		paxos_log_error("DB_ENV set_flags failed: %s", db_strerror(result));
		return -1;
	}
	
	//Redirect errors to sdout
	dbenv->set_errfile(dbenv, stdout);

	//Set the size of the memory cache
	result = dbenv->set_cachesize(dbenv, 0, paxos_config.bdb_cachesize, 1);
	if (result != 0) {
		paxos_log_error("DB_ENV set_cachesize failed: %s",
			db_strerror(result));
		return -1;
	}
	
	//TODO see page size impact
	//Set page size for this db
	// result = dbp->set_pagesize(dbp, pagesize);
	// assert(result  == 0);

	//FIXME set log size


	// Environment open flags
	int flags;
	flags =
		DB_CREATE       |  /* Create if not existing */ 
		DB_RECOVER      |  /* Run normal recovery. */
		DB_INIT_LOCK    |  /* Initialize the locking subsystem */
		DB_INIT_LOG     |  /* Initialize the logging subsystem */
		DB_INIT_TXN     |  /* Initialize the transactional subsystem. */
		DB_THREAD       |  /* Cause the environment to be free-threaded */  
		DB_REGISTER 	|
		DB_INIT_MPOOL;     /* Initialize the memory pool (in-memory cache) */

	//Open the DB environment
	result = dbenv->open(dbenv, 
		db_env_path,            /* Environment directory */
		flags,                  /* Open flags */
		0);                     /* Default file permissions */

	if (result != 0) {
		paxos_log_error("DB_ENV open failed: %s", db_strerror(result));
		return -1;
	}

	paxos_log_info("Berkeley DB storage opened successfully");
	
	s->env = dbenv;
	return 0;
}
Ejemplo n.º 16
0
static void insert_row(DB_ENV *db_env, struct table *t, DB_TXN *txn, long a, long b, long c, long d) {
    int r;

    // generate the primary key
    char key_buffer[8];
    a = htonl64(a);
    memcpy(key_buffer, &a, sizeof a);

    // generate the primary value
    char val_buffer[3*8];
    b = htonl64(b);
    memcpy(val_buffer+0, &b, sizeof b);
    c = htonl64(c);
    memcpy(val_buffer+8, &c, sizeof c);
    d = htonl64(d);
    memcpy(val_buffer+16, &d, sizeof d);

    DBT key = { .data = key_buffer, .size = sizeof key_buffer };
    DBT value = { .data = val_buffer, .size = sizeof val_buffer };
#if defined(TOKUDB)
    if (!force_multiple && t->ndbs == 1) {
        r = t->dbs[0]->put(t->dbs[0], txn, &key, &value, t->mult_flags[0]); assert(r == 0);
    } else {
        r = db_env->put_multiple(db_env, t->dbs[0], txn, &key, &value, t->ndbs, &t->dbs[0], t->mult_keys, t->mult_vals, t->mult_flags); assert(r == 0);
    }
#else
    assert(db_env);
    r = t->dbs[0]->put(t->dbs[0], txn, &key, &value, 0); assert(r == 0);
#endif
}

static inline float tdiff (struct timeval *a, struct timeval *b) {
    return (a->tv_sec - b->tv_sec) +1e-6*(a->tv_usec - b->tv_usec);
}

static void insert_all(DB_ENV *db_env, struct table *t, long nrows, long max_rows_per_txn, long key_range, long rows_per_report, bool do_txn) {
    int r;

    struct timeval tstart;
    r = gettimeofday(&tstart, NULL); assert(r == 0);
    struct timeval tlast = tstart;
    DB_TXN *txn = NULL;
    if (do_txn) {
        r = db_env->txn_begin(db_env, NULL, &txn, 0); assert(r == 0);
    }
    long n_rows_per_txn = 0;
    long rowi;
    for (rowi = 0; rowi < nrows; rowi++) {
        long a = rowi;
        long b = random64() % key_range;
        long c = random64() % key_range;
        long d = random64() % key_range;
        insert_row(db_env, t, txn, a, b, c, d);
        n_rows_per_txn++;
        
        // maybe commit
        if (do_txn && n_rows_per_txn == max_rows_per_txn) {
            r = txn->commit(txn, 0); assert(r == 0);
            r = db_env->txn_begin(db_env, NULL, &txn, 0); assert(r == 0);
            n_rows_per_txn = 0;
        }

        // maybe report performance
        if (((rowi + 1) % rows_per_report) == 0) {
            struct timeval tnow;
            r = gettimeofday(&tnow, NULL); assert(r == 0);
            float last_time = tdiff(&tnow, &tlast);
            float total_time = tdiff(&tnow, &tstart);
            printf("%ld %.3f %.0f/s %.0f/s\n", rowi + 1, last_time, rows_per_report/last_time, rowi/total_time); fflush(stdout);
            tlast = tnow;
        }
    }

    if (do_txn) {
        r = txn->commit(txn, 0); assert(r == 0);
    }
    struct timeval tnow;
    r = gettimeofday(&tnow, NULL); assert(r == 0);
    printf("total %ld %.3f %.0f/s\n", nrows, tdiff(&tnow, &tstart), nrows/tdiff(&tnow, &tstart)); fflush(stdout);
}

int main(int argc, char *argv[]) {
#if defined(TOKDUB)
    char *db_env_dir = "insertm.env.tokudb";
#else
    char *db_env_dir = "insertm.env.bdb";
#endif
    int db_env_open_flags = DB_CREATE | DB_PRIVATE | DB_INIT_MPOOL | DB_INIT_TXN | DB_INIT_LOCK | DB_INIT_LOG;
    long rows = 100000000;
    long rows_per_txn = 1000;
    long rows_per_report = 100000;
    long key_range = 100000;
    bool do_txn = true;
    u_int32_t pagesize = 0;
    u_int64_t cachesize = 1000000000;
    int ndbs = 4;
#if defined(TOKUDB)
    u_int32_t checkpoint_period = 60;
#endif

    int i;
    for (i = 1; i < argc; i++) {
        char *arg = argv[i];
        if (strcmp(arg, "--verbose") == 0) {
            verbose++;
            continue;
        }
        if (strcmp(arg, "--ndbs") == 0 && i+1 < argc) {
            ndbs = atoi(argv[++i]);
            continue;
        }
        if (strcmp(arg, "--rows") == 0 && i+1 < argc) {
            rows = atol(argv[++i]);
            continue;
        }
        if (strcmp(arg, "--rows_per_txn") == 0 && i+1 < argc) {
            rows_per_txn = atol(argv[++i]);
            continue;
        }
        if (strcmp(arg, "--rows_per_report") == 0 && i+1 < argc) {
            rows_per_report = atol(argv[++i]);
            continue;
        }
        if (strcmp(arg, "--key_range") == 0 && i+1 < argc) {
            key_range = atol(argv[++i]);
            continue;
        }
        if (strcmp(arg, "--txn") == 0 && i+1 < argc) {
            do_txn = atoi(argv[++i]);
            continue;
        }
        if (strcmp(arg, "--pagesize") == 0 && i+1 < argc) {
            pagesize = atoi(argv[++i]);
            continue;
        }
        if (strcmp(arg, "--cachesize") == 0 && i+1 < argc) {
            cachesize = atol(argv[++i]);
            continue;
        }
        if (strcmp(arg, "--force_multiple") == 0 && i+1 < argc) {
            force_multiple = atoi(argv[++i]);
            continue;
        }
#if defined(TOKUDB)
        if (strcmp(arg, "--checkpoint_period") == 0 && i+1 < argc) {
            checkpoint_period = atoi(argv[++i]);
            continue;
        }
#endif

        assert(0);
    }

    int r;
    char rm_cmd[strlen(db_env_dir) + strlen("rm -rf ") + 1];
    snprintf(rm_cmd, sizeof(rm_cmd), "rm -rf %s", db_env_dir);
    r = system(rm_cmd); assert(r == 0);

    r = mkdir(db_env_dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); assert(r == 0);

    // create and open the env
    DB_ENV *db_env = NULL;
    r = db_env_create(&db_env, 0); assert(r == 0);
    if (!do_txn)
        db_env_open_flags &= ~(DB_INIT_TXN | DB_INIT_LOG);
    if (cachesize) {
        const u_int64_t gig = 1 << 30;
        r = db_env->set_cachesize(db_env, cachesize / gig, cachesize % gig, 1); assert(r == 0);
    }
#if defined(TOKUDB)
    r = db_env->set_generate_row_callback_for_put(db_env, my_generate_row_for_put); assert(r == 0);
#endif
    r = db_env->open(db_env, db_env_dir, db_env_open_flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); assert(r == 0);
#if defined(TOKUDB)
    if (checkpoint_period) {
        r = db_env->checkpointing_set_period(db_env, checkpoint_period); assert(r == 0);
        u_int32_t period;
        r = db_env->checkpointing_get_period(db_env, &period); assert(r == 0 && period == checkpoint_period);
    }
#endif


    // create the db
    DB *dbs[ndbs];
    for (i = 0; i < ndbs; i++) {
        DB *db = NULL;
        r = db_create(&db, db_env, 0); assert(r == 0);
        DB_TXN *create_txn = NULL;
        if (do_txn) {
            r = db_env->txn_begin(db_env, NULL, &create_txn, 0); assert(r == 0);
        }
        if (pagesize) {
            r = db->set_pagesize(db, pagesize); assert(r == 0);
        }
        char db_filename[32]; sprintf(db_filename, "test%d", i);
        r = db->open(db, create_txn, db_filename, NULL, DB_BTREE, DB_CREATE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); assert(r == 0);

#if defined(TOKUDB)
        DESCRIPTOR_S new_descriptor;
        int index_num = htonl(i);
        new_descriptor.dbt.data = &index_num;
        new_descriptor.dbt.size = sizeof i;
        r = db->change_descriptor(db, create_txn, &new_descriptor.dbt, 0); assert(r == 0);
#else
        db->app_private = (void *) (intptr_t) i;
        if (i > 0) {
            r = dbs[0]->associate(dbs[0], create_txn, db, my_secondary_key, 0); assert(r == 0);
        }
#endif
        if (do_txn) {
            r = create_txn->commit(create_txn, 0); assert(r == 0);
        }
        dbs[i] = db;
    }

    // insert all rows
    struct table table;
    table_init(&table, ndbs, dbs, 4 * 8, 4 * 8);

    insert_all(db_env, &table, rows, rows_per_txn, key_range, rows_per_report, do_txn);

    table_destroy(&table);

    // shutdown
    for (i = 0; i < ndbs; i++) {
        DB *db = dbs[i];
        r = db->close(db, 0); assert(r == 0); db = NULL;
    }
    r = db_env->close(db_env, 0); assert(r == 0); db_env = NULL;

    return 0;
}
Ejemplo n.º 17
0
/* ***************************************************************************
 *				init database
 *	init all databases present in the name array if 'index' is < 0
 *	otherwise initialize only the specific database pointed to by 'index'
 *
 *	Exception:	if index == DB_RUNRECOVERY, then DBENV->remove
 *			is performed before the environment is opened.
 * ***************************************************************************

Returns:	0 on success, or error code
 */
int
dbtp_init(DBTPD * dbtp, unsigned char * home, int index)
{
  u_int32_t eflags = DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL;
  u_int32_t dflags = DB_CREATE;
  u_int32_t info = DB_RECNUM;
  DBTYPE type = DB_BTREE;
  int mode = 0664;
  int ai;

/*
 *	OPEN / CREATE ENVIRONMENT
 *
 *	Since the databases are not transactional, we really don't run recovery.
 *	All that is typically corrupted is the DB environment so destroy the 
 *	environment files and recreate them instead. If this does not work,
 *	operator intervention will be necessary.
 */
 
#if DB_VERSION_MAJOR == 2
  if (index == DB_RUNRECOVERY)
	goto DBTP_env_err;		/* sorry, can't do recovery unless > version 2	*/

  if ((dbtp->dbenv = (DB_ENV *) calloc(sizeof(DB_ENV), 1)) == NULL )
	return (dbtp->dberr = errno);

  if (dbtp->dberr = db_appinit(home, NULL, dbtp->dbenv, eflags | DB_USE_ENVIRON) != 0)
  {
    free(dbtp->dbenv);
DBTP_env_err:
    dbtp->dbenv = NULL;
    return(dbtp->dberr);
  }

#else	/*	DB_VERSION_MAJOR > 2	*/

DBTP_reopen:
  if ((dbtp->dberr = db_env_create(&dbtp->dbenv,0)) != 0)
	goto DBTP_env_err;

  if (index == DB_RUNRECOVERY) {
#ifdef IS_DB_3_0_x
    if ((dbtp->dberr = dbtp->dbenv->remove(dbtp->dbenv, (char *)home, NULL, DB_FORCE)) != 0)
#else	/*	> 3.0	*/
    if ((dbtp->dberr = dbtp->dbenv->remove(dbtp->dbenv, (char *)home, DB_FORCE)) != 0)
#endif
	goto DBTP_env_err;

    index = -1;
    dbtp->dbenv = NULL;
    goto DBTP_reopen;
  }

#ifdef IS_DB_3_0_x
  if ((dbtp->dberr = dbtp->dbenv->open(dbtp->dbenv, (char *)home, NULL, eflags, mode)) != 0) {
#else	/*	> 3.0	*/
  if ((dbtp->dberr = dbtp->dbenv->open(dbtp->dbenv, (char *)home, eflags, mode)) != 0) {
#endif
    (void)dbtp->dbenv->close(dbtp->dbenv, 0);
DBTP_env_err:
    dbtp->dbenv = NULL;
    return(dbtp->dberr);
  }

#endif	/*	DB_VERSION_MAJOR > 2	*/

/*
 *	CREATE / OPEN DATABASE's
 */

  for(ai=0; ai<DBTP_MAXdbf; ai++) {
    if (index < 0) {
      if (dbtp->dbfile[ai] == NULL) {
        if (ai)
	  return(dbtp->dberr);		/* last status if end of array and something opened	*/
        else {
	  dbtp_env_close(dbtp);
          return(dbtp->dberr = ENODATA);		/* fatal if nothing to open	*/
        }    
      }
    }
    else {
      ai = index;
      index = 1;		/* always 'positive' if active	*/
    }
    if ((dbtp->dberr = db_create(&dbtp->dbaddr[ai],dbtp->dbenv,0)) != 0)
    {
  Bail:
      dbtp_close(dbtp);
      return(dbtp->dberr);
    }
    if ((dbtp->dberr = dbtp->dbaddr[ai]->set_flags(dbtp->dbaddr[ai], info)) != 0)
	goto Bail;
#if DB_VERSION_MAJOR > 2
#ifdef AT_LEAST_DB_4_1
    if ((dbtp->dberr = (dbtp->dbaddr[ai]->open(dbtp->dbaddr[ai], NULL, dbtp->dbfile[ai], NULL, type, dflags, mode))) != 0)
#else
    if ((dbtp->dberr = (dbtp->dbaddr[ai]->open(dbtp->dbaddr[ai], dbtp->dbfile[ai], NULL, type, dflags, mode))) != 0)
#endif /* AT_LEAST_DB_4_1 */
#else /* DB_VERSION_MAJOR == 2 */
    if ((dbtp->dberr = db_open(dbtp->dbfile[ai], type, dflags, mode, dbtp->dbenv, info, &dbtp->dbaddr[ai]))) != 0)
#endif /* DB_VERSION_MAJOR == 2 */
	goto Bail;
    if (index > 0)
      break;
  }  
  return(dbtp->dberr);
}

/*	returns 0 if addr and *addr are valid
 *	else it returns DB_KEYEMPTY
 */
int
_dbtp_set(DBTPD * dbtp, void * addr, size_t size)
{
  if (size != sizeof(u_int32_t) || addr == NULL || *(u_int32_t *)addr == 0)
  	return(DB_KEYEMPTY);
  memset(&dbtp->keydbt, 0, sizeof(DBT));
  memset(&dbtp->mgdbt, 0, sizeof(DBT));
  dbtp->keydbt.data = addr;
  dbtp->keydbt.size = (u_int32_t)size;
  return(0);
}

/* ****************************	*
 *	return 0 on success	*
 *	or the bdb error code	*
 *	external data structure	*
 *	mgdbt is filled		*
 *
 *	typedef struct {
 *           void *data;
 *           u_int32_t size;
 *           u_int32_t ulen;
 *           u_int32_t dlen;
 *           u_int32_t doff;
 *           u_int32_t flags;
 *	} DBT;
 *
 */

int
dbtp_get(DBTPD * dbtp, int ai, void * addr, size_t size)
{
  if(dbtp->dbaddr[ai] == NULL)
  	return(dbtp->dberr = ENODATA);

  if ((dbtp->dberr = _dbtp_set(dbtp,addr,size)))
  	return(dbtp->dberr); 
  return(dbtp->dberr = dbtp->dbaddr[ai]->get(dbtp->dbaddr[ai], NULL, &dbtp->keydbt, &dbtp->mgdbt, 0));
}

/* ****************************	*
 *	return 0 on success	*
 *	or the bdb error code	*
 *	external data structure	*
 *	mgdbt and keydbt are	*
 *	filled			*
 *
 *	typedef struct {
 *           void *data;
 *           u_int32_t size;
 *           u_int32_t ulen;
 *           u_int32_t dlen;
 *           u_int32_t doff;
 *           u_int32_t flags;
 *	} DBT;
 *
 */
 
int
dbtp_getrecno(DBTPD * dbtp, int ai, u_int32_t cursor)
{
  if(dbtp->dbaddr[ai] == NULL)
  	return(dbtp->dberr = ENODATA);

REGET_by_cursor:
  memset(&dbtp->keydbt, 0, sizeof(DBT));
  memset(&dbtp->mgdbt, 0, sizeof(DBT));
  dbtp->keydbt.data = &cursor;
  dbtp->keydbt.size = (u_int32_t)sizeof(cursor);

  return(dbtp->dberr = dbtp->dbaddr[ai]->get(dbtp->dbaddr[ai], NULL, &dbtp->keydbt, &dbtp->mgdbt, DB_SET_RECNO));

  dbtp->dberr = dbtp->dbaddr[ai]->get(dbtp->dbaddr[ai], NULL, &dbtp->keydbt, &dbtp->mgdbt, DB_SET_RECNO);
  if (! dbtp->dberr && dbtp->keydbt.size != sizeof(INADDR_BROADCAST)) {
    dbtp_del(dbtp,ai,dbtp->keydbt.data,dbtp->keydbt.size);
    goto REGET_by_cursor;
  }
  return(dbtp->dberr);
}

/* get the number of keys or records from the target database, by index	*/

u_int32_t
dbtp_stati(DBTPD * dbtp, int ai)
{
  DB_BTREE_STAT * statistics = NULL;
  u_int32_t bt_nkeys;
  
  if(dbtp->dbaddr[ai] == NULL) {
    dbtp->dberr = DB_NOTFOUND;
    return(0);
  }

#ifdef AT_LEAST_DB_4_3
  dbtp->dberr = dbtp->dbaddr[ai]->stat(dbtp->dbaddr[ai],NULL,&statistics,DB_FAST_STAT);
#else  
# ifdef AT_LEAST_DB_3_3
  dbtp->dberr = dbtp->dbaddr[ai]->stat(dbtp->dbaddr[ai],&statistics,DB_FAST_STAT);
# else
  dbtp->dberr = dbtp->dbaddr[ai]->stat(dbtp->dbaddr[ai],&statistics,NULL,DB_RECORDCOUNT);
# endif
#endif

  if (dbtp->dberr)
	bt_nkeys = 0;
  else
#ifdef AT_LEAST_DB_3_1
	bt_nkeys = statistics->bt_nkeys;
#else
	bt_nkeys = statistics->bt_nrecs;
#endif

  free(statistics);
  return(bt_nkeys);
}
Ejemplo n.º 18
0
int
main(int argc, char* argv[])
{
	DB *dbp2;
	DBT key, data;
	FBFR *buf, *replyBuf;
	HDbRec rec;
	TPINIT *initBuf;
        DB_ENV *dbenv2, *dbenv1;
	long len, replyLen, seqNo;
	int ch, cnt, cnt_abort, cnt_commit, cnt_server1, i, ret;
	char *target;
	char *home = HOME;
	u_int32_t flags = DB_INIT_MPOOL | DB_INIT_LOG | DB_INIT_TXN |
	  DB_INIT_LOCK | DB_CREATE | DB_RECOVER | DB_REGISTER;
	u_int32_t dbflags = DB_CREATE;

	progname = argv[0];

	dbenv2 = dbenv1  = NULL;
	dbp2 = NULL;
	buf = replyBuf = NULL;
	initBuf = NULL;
	cnt = 1000;
	cnt_abort = cnt_commit = cnt_server1 = 0;

	while ((ch = getopt(argc, argv, "n:v")) != EOF)
		switch (ch) {
		case 'n':
			cnt = atoi(optarg);
			break;
		case 'v':
			verbose = 1;
			break;
		case '?':
		default:
			return (usage());
		}
	argc -= optind;
	argv += optind;

	if (verbose)
		printf("%s: called\n", progname);

	/* Seed random number generator. */
	srand((u_int)(time(NULL) | getpid()));

	if (tpinit((TPINIT *)NULL) == -1)
		goto tuxedo_err;
	if (verbose)
		printf("%s: tpinit() OK\n", progname);

	/* Allocate init buffer */
	if ((initBuf = (TPINIT *)tpalloc("TPINIT", NULL, TPINITNEED(0))) == 0)
		goto tuxedo_err;
	if (verbose)
		printf("%s: tpalloc(\"TPINIT\") OK\n", progname);

	/* Create the DB environment. */
	if ((ret = db_env_create(&dbenv2, 0)) != 0 ||
	    (ret = dbenv2->open(dbenv2, home, flags, 0)) != 0) {
		fprintf(stderr,
		    "%s: %s: %s\n", progname, home, db_strerror(ret));
		goto err;
	}
	dbenv2->set_errfile(dbenv2, stderr);
	if (verbose)
		printf("%s: opened %s OK\n", progname, home);

	/* 
	 * Open table #2 -- Data is inserted into table 1 using XA
	 * transactions, and inserted into table 2 using regular transactions.
	 */
	if ((ret = db_create(&dbp2, dbenv2, 0)) != 0 ||
	    (ret = dbp2->open(dbp2,
	    NULL, TABLE2, NULL, DB_BTREE, dbflags, 0660)) != 0) {
		fprintf(stderr,
		    "%s: %s %s\n", progname, TABLE2, db_strerror(ret));
		goto err;
	}
	if (verbose)
		printf("%s: opened %s OK\n", progname, TABLE2);

	/* Allocate send buffer. */
	len = Fneeded(1, 3 * sizeof(long));
	if ((buf = (FBFR*)tpalloc("FML32", NULL, len)) == 0)
		goto tuxedo_err;
	if (verbose)
		printf("%s: tpalloc(\"FML32\"), send buffer OK\n", progname);

	/* Allocate reply buffer. */
	replyLen = 1024;
	if ((replyBuf = (FBFR*)tpalloc("FML32", NULL, replyLen)) == NULL)
		goto tuxedo_err;
	if (verbose)
		printf("%s: tpalloc(\"FML32\"), reply buffer OK\n", progname);

	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	for (rec.SeqNo = 1, i = 0; i < cnt; ++i, ++rec.SeqNo) {
		GetTime(&rec.Ts);

		if (Fchg(buf, SEQ_NO, 0, (char *)&rec.SeqNo, 0) == -1)
			goto tuxedo_fml_err;
		if (verbose)
			printf("%s: Fchg(), sequence number OK\n", progname);
		if (Fchg(buf, TS_SEC, 0, (char *)&rec.Ts.Sec, 0) == -1)
			goto tuxedo_fml_err;
		if (verbose)
			printf("%s: Fchg(), seconds OK\n", progname);
		if (Fchg(buf, TS_USEC, 0, (char *)&rec.Ts.Usec, 0) == -1)
			goto tuxedo_fml_err;
		if (verbose)
			printf("%s: Fchg(), microseconds OK\n", progname);

		if (tpbegin(60L, 0L) == -1)
			goto tuxedo_err;
		if (verbose)
			printf("%s: tpbegin() OK\n", progname);

		/* Randomly send half of our requests to each server. */
		if (rand() % 2 > 0) {
			++cnt_server1;
			target = "TestTxn1";
		} else
			target = "TestTxn2";
		if (tpcall(target, (char *)buf,
		    0L, (char **)&replyBuf, &replyLen, TPSIGRSTRT) == -1)
			goto tuxedo_err;
		/* Commit for a return value of 0, otherwise abort. */
		if (tpurcode == 0) {
			++cnt_commit;
			if (verbose)
				printf("%s: txn success\n", progname);

			if (tpcommit(0L) == -1)
				goto abort;
			if (verbose)
				printf("%s: tpcommit() OK\n", progname);

			/*
			 * Store a copy of the key/data pair into table #2
			 * on success, we'll compare table #1 and table #2
			 * after the run finishes.
			 */
			seqNo = rec.SeqNo;
			key.data = &seqNo;
			key.size = sizeof(seqNo);
			data.data = &seqNo;
			data.size = sizeof(seqNo);
			if ((ret =
			    dbp2->put(dbp2, NULL, &key, &data, 0)) != 0) {
				fprintf(stderr, "%s: DB->put: %s %s\n",
				    progname, TABLE2, db_strerror(ret));
				goto err;
			}
		} else {
 abort:			++cnt_abort;
			if (verbose)
				printf("%s: txn failure\n", progname);

			if (tpabort(0L) == -1)
				goto tuxedo_err;
			if (verbose)
				printf("%s: tpabort() OK\n", progname);
		}
	}

	printf("%s: %d requests: %d committed, %d aborted\n",
	    progname, cnt, cnt_commit, cnt_abort);
	printf("%s: %d sent to server #1, %d sent to server #2\n",
	    progname, cnt_server1, cnt - cnt_server1);

	/* Check that database 1 and database 2 are identical. */
	if (dbp2 != NULL)
		(void)dbp2->close(dbp2, 0);
	dbp2 = NULL;
	if ((ret = db_env_create(&dbenv1, 0)) != 0 ||
	    (ret = dbenv1->open(dbenv1, "../data", flags, 0)) != 0) 
		goto err;
	ret = check_data(dbenv1, TABLE1, dbenv2, TABLE2, progname);

	if (0) {
tuxedo_err:	fprintf(stderr, "%s: TUXEDO ERROR: %s (code %d)\n",
		    progname, tpstrerror(tperrno), tperrno);
		goto err;
	}
	if (0) {
tuxedo_fml_err:	fprintf(stderr, "%s: FML ERROR: %s (code %d)\n",
		    progname, Fstrerror(Ferror), Ferror);
	}
	if (0) {
err:		ret = EXIT_FAILURE;
	}

	if (replyBuf != NULL)
		tpfree((char *)replyBuf);
	if (buf != NULL)
		tpfree((char *)buf);
	if (initBuf != NULL)
		tpfree((char *)initBuf);
	if (dbp2 != NULL)
		(void)dbp2->close(dbp2, 0);
	if (dbenv1 != NULL)
		(void)dbenv1->close(dbenv1, 0);
	if (dbenv2 != NULL)
		(void)dbenv2->close(dbenv2, 0);

	tpterm();
	if (verbose)
		printf("%s: tpterm() OK\n", progname);

	return (ret);
}
Ejemplo n.º 19
0
/*
 * file_end --
 *	Stop editing a file.
 *
 * PUBLIC: int file_end __P((SCR *, EXF *, int));
 */
int
file_end(SCR *sp, EXF *ep, int force)
{
	FREF *frp;

	/*
	 * !!!
	 * ep MAY NOT BE THE SAME AS sp->ep, DON'T USE THE LATTER.
	 * (If argument ep is NULL, use sp->ep.)
	 *
	 * If multiply referenced, just decrement the count and return.
	 */
	if (ep == NULL)
		ep = sp->ep;
	CIRCLEQ_REMOVE(&ep->scrq, sp, eq);
	if (--ep->refcnt != 0)
		return (0);

	/*
	 *
	 * Clean up the FREF structure.
	 *
	 * Save the cursor location.
	 *
	 * XXX
	 * It would be cleaner to do this somewhere else, but by the time
	 * ex or vi knows that we're changing files it's already happened.
	 */
	frp = sp->frp;
	frp->lno = sp->lno;
	frp->cno = sp->cno;
	F_SET(frp, FR_CURSORSET);

	/*
	 * We may no longer need the temporary backing file, so clean it
	 * up.  We don't need the FREF structure either, if the file was
	 * never named, so lose it.
	 *
	 * !!!
	 * Re: FR_DONTDELETE, see the comment above in file_init().
	 */
	if (!F_ISSET(frp, FR_DONTDELETE) && frp->tname != NULL) {
		if (unlink(frp->tname))
			msgq_str(sp, M_SYSERR, frp->tname, "240|%s: remove");
		free(frp->tname);
		frp->tname = NULL;
		if (F_ISSET(frp, FR_TMPFILE)) {
			CIRCLEQ_REMOVE(&sp->gp->frefq, frp, q);
			if (frp->name != NULL)
				free(frp->name);
			free(frp);
		}
		sp->frp = NULL;
	}

	/*
	 * Clean up the EXF structure.
	 *
	 * Close the db structure.
	 */
	if (ep->db->close != NULL) {
		if ((sp->db_error = ep->db->close(ep->db, DB_NOSYNC)) != 0 && 
		    !force) {
			msgq_str(sp, M_DBERR, frp->name, "241|%s: close");
			CIRCLEQ_INSERT_HEAD(&ep->scrq, sp, eq);
			++ep->refcnt;
			return (1);
		}
		ep->db = NULL;
	}

	/* COMMITTED TO THE CLOSE.  THERE'S NO GOING BACK... */

	/* Stop logging. */
	(void)log_end(sp, ep);

	/* Free up any marks. */
	(void)mark_end(sp, ep);

	if (ep->env) {
		DB_ENV *env;

		ep->env->close(ep->env, 0);
		ep->env = 0;
		if ((sp->db_error = db_env_create(&env, 0)))
			msgq(sp, M_DBERR, "env_create");
		if ((sp->db_error = db_env_remove(env, ep->env_path, 0)))
			msgq(sp, M_DBERR, "env->remove");
		if (ep->env_path != NULL && rmdir(ep->env_path))
			msgq_str(sp, M_SYSERR, ep->env_path, "242|%s: remove");
	}

	/*
	 * Delete recovery files, close the open descriptor, free recovery
	 * memory.  See recover.c for a description of the protocol.
	 *
	 * XXX
	 * Unlink backup file first, we can detect that the recovery file
	 * doesn't reference anything when the user tries to recover it.
	 * There's a race, here, obviously, but it's fairly small.
	 */
	if (!F_ISSET(ep, F_RCV_NORM)) {
		if (ep->rcv_path != NULL && unlink(ep->rcv_path))
			msgq_str(sp, M_SYSERR, ep->rcv_path, "242|%s: remove");
		if (ep->rcv_mpath != NULL && unlink(ep->rcv_mpath))
			msgq_str(sp, M_SYSERR, ep->rcv_mpath, "243|%s: remove");
	}
	CIRCLEQ_REMOVE(&sp->gp->exfq, ep, q);
	if (ep->fd != -1)
		(void)close(ep->fd);
	if (ep->fcntl_fd != -1)
		(void)close(ep->fcntl_fd);
	if (ep->rcv_fd != -1)
		(void)close(ep->rcv_fd);
	if (ep->env_path != NULL)
		free(ep->env_path);
	if (ep->rcv_path != NULL) {
		free(ep->rcv_path);
		ep->rcv_path = NULL;
	}
	if (ep->rcv_mpath != NULL)
		free(ep->rcv_mpath);

	free(ep);
	return (0);
}
Ejemplo n.º 20
0
int gw_acct_db_open(gw_boolean_t server)
{
	int       rc;
	u_int32_t flags;
	u_int32_t env_flags;
	DB        *db;
	DB_ENV    *db_env;
	char      env_dir[PATH_MAX];
	char *    gw_location;
	mode_t    mask;
	
    gw_location = getenv("GW_LOCATION");
    
    if ( gw_location == NULL)
    {
		if (server == GW_TRUE)
           gw_log_print("GW",'E',"Variable GW_LOCATION is not defined.\n");
        else
			fprintf(stderr,"Variable GW_LOCATION is not defined!\n");
			
        return -1;
    }
     
    snprintf(env_dir, PATH_MAX-1, "%s/" GW_VAR_DIR "/acct/", gw_location);
	
	/* ----- Open the environment ------ */
	
	mask = umask(002);
	
	rc = db_env_create(&(gw_acct_db.env_db), 0);
	
	if ( rc != 0 )
	{
		if (server == GW_TRUE)
			gw_log_print("DB",'E',"Error initializing environment structure (%s).\n",
				db_strerror(rc));
		else
			fprintf(stderr,"Error initializing environment structure (%s).\n",
				db_strerror(rc));
				
		return -1;
	}
	db_env = gw_acct_db.env_db;
	
	env_flags = DB_INIT_CDB	| DB_INIT_MPOOL;
	
	if (server == GW_TRUE)
		env_flags = env_flags | DB_CREATE | DB_THREAD;
	
	rc = db_env->open(db_env,env_dir,env_flags,0664);

	if (rc!=0)
	{
		if (server == GW_TRUE)
			gw_log_print("DB",'E',"Error openning environment (%s).\n",db_strerror(rc));
		else
			fprintf(stderr,"Error openning environment (%s).\n",db_strerror(rc));

		return -1;
	}

	umask(mask);
	
	/* ----- Primary Database, acct.db ------ */
	
	rc = db_create(&(gw_acct_db.acct_db), db_env, 0);
	
	if ( rc != 0 )
	{		
		if (server == GW_TRUE)
			gw_log_print("DB",'E',"Error initializing database structure (%s).\n",
					db_strerror(rc));
		else
			fprintf(stderr,"Error initializing database structure (%s).\n",
					db_strerror(rc));

		return -1;
	}
	
	db = gw_acct_db.acct_db;
	
	if (server == GW_TRUE)
	{
		db->set_errcall(db,gw_acct_db_error);
		db->set_errpfx(db,"ACCT-DB");		
		flags = DB_CREATE | DB_THREAD;
	}
	else
		flags = DB_RDONLY;

	rc = db->open(db,
	              NULL, 
	              "acct.db",
                  NULL, 
                  DB_BTREE, 
                  flags, 
                  0644);
	if (rc!=0)
	{
		if (server == GW_TRUE)
			gw_log_print("DB",'E',"Error openning database.\n");
		else
			fprintf(stderr,"Error openning database.\n");

		return -1;
	}

	/* ----- Secondary Database (user index), uidx.db ------ */
	
	rc = db_create(&(gw_acct_db.uinx_db), db_env, 0);
	
	if ( rc != 0 )
	{		
		if (server == GW_TRUE)
			gw_log_print("DB",'E',"Error initializing database structure (%s).\n",
					db_strerror(rc));
		else
			fprintf(stderr,"Error initializing database structure (%s).\n",
					db_strerror(rc));

		return -1;
	}
	
	db = gw_acct_db.uinx_db;
	
	db->set_flags(db,DB_DUPSORT);

	if (server == GW_TRUE)
	{
		db->set_errcall(db,gw_acct_db_error);
		db->set_errpfx(db,"UINX-DB");		
		
		flags = DB_CREATE | DB_THREAD;
	}
	else
		flags = DB_RDONLY;
	
	rc = db->open(db,
	              NULL, 
	              "uinx.db",
                  NULL, 
                  DB_BTREE, 
                  flags, 
                  0644);
	if (rc!=0)
	{
		if (server == GW_TRUE)
			gw_log_print("DB",'E',"Error openning database.\n");
		else
			fprintf(stderr,"Error openning database.\n");

		return -1;
	}

	gw_acct_db.acct_db->associate(gw_acct_db.acct_db,
	                              NULL,
	                              db,
	                              gw_acct_uidx_cb,
	                              DB_CREATE);
	                              
	/* ----- Secondary Database (host index), hidx.db ------ */
		
	rc = db_create(&(gw_acct_db.hinx_db), db_env, 0);

	if ( rc != 0 )
	{		
		if (server == GW_TRUE)
			gw_log_print("DB",'E',"Error initializing database structure (%s).\n",
					db_strerror(rc));
		else
			fprintf(stderr,"Error initializing database structure (%s).\n",
					db_strerror(rc));

		return -1;
	}
	
	db = gw_acct_db.hinx_db;
	
	db->set_flags(db,DB_DUPSORT);

	if (server == GW_TRUE)
	{
		db->set_errcall(db,gw_acct_db_error);
		db->set_errpfx(db,"HINX-DB");
		
		flags = DB_CREATE | DB_THREAD;
	}
	else
		flags = DB_RDONLY;
	
	rc = db->open(db,
	              NULL, 
	              "hinx.db",
                  NULL, 
                  DB_BTREE, 
                  flags, 
                  0644);
	if (rc!=0)
	{
		if (server == GW_TRUE)
			gw_log_print("DB",'E',"Error openning database.\n");
		else
			fprintf(stderr,"Error openning database.\n");

		return -1;
	}

	gw_acct_db.acct_db->associate(gw_acct_db.acct_db,
	                              NULL,
	                              db,
	                              gw_acct_hidx_cb,
	                              DB_CREATE);

	if (server == GW_TRUE)	                              		
		gw_log_print ("DB",'I',"Accounting databases opened.\n");
	
	return 0;
}
Ejemplo n.º 21
0
static void
op_tds(u_int ops, int update, u_int32_t env_flags, u_int32_t log_flags)
{
    DB *dbp;
    DBT key, data;
    DB_ENV *dbenv;
    DB_MPOOL_STAT  *gsp;
    DB_TXN *txn;
    char *keybuf, *databuf;

    DB_BENCH_ASSERT((keybuf = malloc(keysize)) != NULL);
    DB_BENCH_ASSERT((databuf = malloc(datasize)) != NULL);

    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));
    key.data = keybuf;
    key.size = keysize;
    memset(keybuf, 'a', keysize);

    data.data = databuf;
    data.size = datasize;
    memset(databuf, 'b', datasize);

    DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);

    dbenv->set_errfile(dbenv, stderr);

    /* General environment configuration. */
#ifdef DB_AUTO_COMMIT
    DB_BENCH_ASSERT(dbenv->set_flags(dbenv, DB_AUTO_COMMIT, 1) == 0);
#endif
    if (env_flags != 0)
        DB_BENCH_ASSERT(dbenv->set_flags(dbenv, env_flags, 1) == 0);

    /* Logging configuration. */
    if (log_flags != 0)
#if DB_VERSION_MINOR >= 7
        DB_BENCH_ASSERT(
            dbenv->log_set_config(dbenv, log_flags, 1) == 0);
#else
        DB_BENCH_ASSERT(dbenv->set_flags(dbenv, log_flags, 1) == 0);
#endif
#ifdef DB_LOG_INMEMORY
    if (!(log_flags & DB_LOG_INMEMORY))
#endif
#ifdef DB_LOG_IN_MEMORY
        if (!(log_flags & DB_LOG_IN_MEMORY))
#endif
            DB_BENCH_ASSERT(dbenv->set_lg_max(dbenv, logbufsize * 10) == 0);
    DB_BENCH_ASSERT(dbenv->set_lg_bsize(dbenv, logbufsize) == 0);

    DB_BENCH_ASSERT(dbenv->open(dbenv, "TESTDIR",
                                DB_CREATE | DB_PRIVATE | DB_INIT_LOCK |
                                DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0666) == 0);

    DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0);
    DB_BENCH_ASSERT(dbp->set_pagesize(dbp, pagesize) == 0);
    DB_BENCH_ASSERT(dbp->open(
                        dbp, NULL, TESTFILE, NULL, DB_BTREE, DB_CREATE, 0666) == 0);

    if (update) {
        (void)dbenv->memp_stat(dbenv, &gsp, NULL, DB_STAT_CLEAR);

        TIMER_START;
        for (; ops > 0; --ops)
            DB_BENCH_ASSERT(
                dbp->put(dbp, NULL, &key, &data, 0) == 0);
        TIMER_STOP;

        (void)dbenv->memp_stat(dbenv, &gsp, NULL, 0);
        DB_BENCH_ASSERT(gsp->st_page_out == 0);
    } else {
        DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0);
        (void)dbenv->memp_stat(dbenv, &gsp, NULL, DB_STAT_CLEAR);

        TIMER_START;
        for (; ops > 0; --ops) {
            DB_BENCH_ASSERT(
                dbenv->txn_begin(dbenv, NULL, &txn, 0) == 0);
            DB_BENCH_ASSERT(
                dbp->get(dbp, NULL, &key, &data, 0) == 0);
            DB_BENCH_ASSERT(txn->commit(txn, 0) == 0);
        }
        TIMER_STOP;

        (void)dbenv->memp_stat(dbenv, &gsp, NULL, 0);
        DB_BENCH_ASSERT(gsp->st_cache_miss == 0);
    }

    DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
    DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);
}
Ejemplo n.º 22
0
void bdb_env_init(void){
    int ret;
    /* db env init */
    if ((ret = db_env_create(&env, 0)) != 0) {
        fprintf(stderr, "db_env_create: %s\n", db_strerror(ret));
        exit(EXIT_FAILURE);
    }

    /* set err&msg display */
    env->set_errpfx(env, PACKAGE);
    /* env->set_errfile(env, stderr); */
    /* env->set_msgfile(env, stderr); */
	env->set_errcall(env, bdb_err_callback);
  	env->set_msgcall(env, bdb_msg_callback);

    /* set BerkeleyDB verbose*/
    if (settings.verbose > 1) {
        if ((ret = env->set_verbose(env, DB_VERB_FILEOPS_ALL, 1)) != 0) {
            fprintf(stderr, "env->set_verbose[DB_VERB_FILEOPS_ALL]: %s\n",
                    db_strerror(ret));
            exit(EXIT_FAILURE);
        }
        if ((ret = env->set_verbose(env, DB_VERB_DEADLOCK, 1)) != 0) {
            fprintf(stderr, "env->set_verbose[DB_VERB_DEADLOCK]: %s\n",
                    db_strerror(ret));
            exit(EXIT_FAILURE);
        }
        if ((ret = env->set_verbose(env, DB_VERB_RECOVERY, 1)) != 0) {
            fprintf(stderr, "env->set_verbose[DB_VERB_RECOVERY]: %s\n",
                    db_strerror(ret));
            exit(EXIT_FAILURE);
        }
    }
    
    /* set log dir*/
    if (bdb_settings.log_home != NULL){
        /* if no log dir existed, we create it */
        if (0 != access(bdb_settings.log_home, F_OK)) {
            if (0 != mkdir(bdb_settings.log_home, 0750)) {
                fprintf(stderr, "mkdir log_home error:[%s]\n", bdb_settings.log_home);
                exit(EXIT_FAILURE);
            }
        }
        env->set_lg_dir(env, bdb_settings.log_home);
    }
    
    /* set MPOOL size */
    if (sizeof(void *) == 4 && bdb_settings.cache_size > (1024uLL * 1024uLL * 1024uLL * 2uLL)) {
        fprintf(stderr, "32bit box only max 2GB memory pool allowed\n");
        exit(EXIT_FAILURE);
    }
    env->set_cachesize(env, (u_int32_t) (bdb_settings.cache_size / (1024uLL * 1024uLL * 1024uLL)), 
                            (u_int32_t) (bdb_settings.cache_size % (1024uLL * 1024uLL * 1024uLL)), 
                            (int) (bdb_settings.cache_size / (1024uLL * 1024uLL * 1024uLL * 4uLL) + 1uLL) );
    
    /* set DB_TXN_NOSYNC flag */
    if (bdb_settings.txn_nosync){
        env->set_flags(env, DB_TXN_NOSYNC, 1);
    }

    /* set locking */
    env->set_lk_max_lockers(env, 20000);
    env->set_lk_max_locks(env, 20000);
    env->set_lk_max_objects(env, 20000);

    /* at least max active transactions */
  	env->set_tx_max(env, 10000);

    /* set transaction log buffer */
    env->set_lg_bsize(env, bdb_settings.txn_lg_bsize);
    
    /* NOTICE:
    If set, Berkeley DB will automatically remove log files that are no longer needed.
    Automatic log file removal is likely to make catastrophic recovery impossible.
    Replication applications will rarely want to configure automatic log file removal as it
    increases the likelihood a master will be unable to satisfy a client's request 
    for a recent log record.
     */
    if (!bdb_settings.is_replicated && bdb_settings.log_auto_remove){
        fprintf(stderr, "log_auto_remove\n");        
        env->log_set_config(env, DB_LOG_AUTO_REMOVE, 1);
    }
    
    /* if no home dir existed, we create it */
    if (0 != access(bdb_settings.env_home, F_OK)) {
        if (0 != mkdir(bdb_settings.env_home, 0750)) {
            fprintf(stderr, "mkdir env_home error:[%s]\n", bdb_settings.env_home);
            exit(EXIT_FAILURE);
        }
    }
    
    if(bdb_settings.is_replicated) {
        bdb_settings.env_flags |= DB_INIT_REP;

        /* verbose messages that help us to debug */
        if (settings.verbose > 1) {
            if ((ret = env->set_verbose(env, DB_VERB_REPLICATION, 1)) != 0) {
                fprintf(stderr, "env->set_verbose[DB_VERB_REPLICATION]: %s\n",
                        db_strerror(ret));
                exit(EXIT_FAILURE);
            }
        }

        /* set up the event hook, so we can do logging when something happens */
        env->set_event_notify(env, bdb_event_callback);

        /* ack policy can have a great impact in performance, lantency and consistency */
        env->repmgr_set_ack_policy(env, bdb_settings.rep_ack_policy);

        /* timeout configs */
        env->rep_set_timeout(env, DB_REP_ACK_TIMEOUT, bdb_settings.rep_ack_timeout);
        env->rep_set_timeout(env, DB_REP_CHECKPOINT_DELAY, bdb_settings.rep_chkpoint_delay);
        env->rep_set_timeout(env, DB_REP_CONNECTION_RETRY, bdb_settings.rep_conn_retry);
        env->rep_set_timeout(env, DB_REP_ELECTION_TIMEOUT, bdb_settings.rep_elect_timeout);
        env->rep_set_timeout(env, DB_REP_ELECTION_RETRY, bdb_settings.rep_elect_retry);

        /* notice:
           The "monitor" time should always be at least a little bit longer than the "send" time */
        env->rep_set_timeout(env, DB_REP_HEARTBEAT_MONITOR, bdb_settings.rep_heartbeat_monitor);
        env->rep_set_timeout(env, DB_REP_HEARTBEAT_SEND, bdb_settings.rep_heartbeat_send);

        /* Bulk transfers simply cause replication messages to accumulate
           in a buffer until a triggering event occurs.  
	    	   Bulk transfer occurs when:
           1. Bulk transfers are configured for the master environment, and
           2. the message buffer is full or
           3. a permanent record (for example, a transaction commit or a checkpoint record)
		   is placed in the buffer for the replica.
        */
        env->rep_set_config(env, DB_REP_CONF_BULK, bdb_settings.rep_bulk);

        /* we now never use master lease */
        /*
        env->rep_set_timeout(env, DB_REP_LEASE_TIMEOUT, bdb_settings.rep_lease_timeout);
        env->rep_set_config(env, DB_REP_CONF_LEASE, bdb_settings.rep_lease);
        env->rep_set_clockskew(env, bdb_settings.rep_fast_clock, bdb_settings.rep_slow_clock);
        */

        env->rep_set_priority(env, bdb_settings.rep_priority);
        env->rep_set_request(env, bdb_settings.rep_req_min, bdb_settings.rep_req_max);
		env->rep_set_limit(env, bdb_settings.rep_limit_gbytes, bdb_settings.rep_limit_bytes);

        /* publish the local site communication channel */
        if ((ret = env->repmgr_set_local_site(env, bdb_settings.rep_localhost, bdb_settings.rep_localport, 0)) != 0) {
            fprintf(stderr, "repmgr_set_local_site[%s:%d]: %s\n", 
                    bdb_settings.rep_localhost, bdb_settings.rep_localport, db_strerror(ret));
            exit(EXIT_FAILURE);
        }
        /* add a remote site, mostly this is a master */
        if(NULL != bdb_settings.rep_remotehost) {
            if ((ret = env->repmgr_add_remote_site(env, bdb_settings.rep_remotehost, bdb_settings.rep_remoteport, NULL, 0)) != 0) {
                fprintf(stderr, "repmgr_add_remote_site[%s:%d]: %s\n", 
                        bdb_settings.rep_remotehost, bdb_settings.rep_remoteport, db_strerror(ret));
                exit(EXIT_FAILURE);
            }
        }
        /* nsite is important for electing, default nvotes is (nsite/2 + 1)
           if nsite is equel to 2, then nvotes is 1 */
        if ((ret = env->rep_set_nsites(env, bdb_settings.rep_nsites)) != 0) {
            fprintf(stderr, "rep_set_nsites: %s\n", db_strerror(ret));
            exit(EXIT_FAILURE);
        }
    }

    if ((ret = env->open(env, bdb_settings.env_home, bdb_settings.env_flags, 0)) != 0) {
        fprintf(stderr, "db_env_open: %s\n", db_strerror(ret));
        exit(EXIT_FAILURE);
    }

    if(bdb_settings.is_replicated) {
        /* repmgr_start must run after daemon !!!*/
        if ((ret = env->repmgr_start(env, 3, bdb_settings.rep_start_policy)) != 0) {
            fprintf(stderr, "env->repmgr_start: %s\n", db_strerror(ret));
            exit(EXIT_FAILURE);
        }
        /* sleep 5 second for electing or client startup */
        if (bdb_settings.rep_start_policy == DB_REP_ELECTION ||
            bdb_settings.rep_start_policy == DB_REP_CLIENT) {
            sleep(5);
        }
    }
}
Ejemplo n.º 23
0
Archivo: b_put.c Proyecto: kanbang/Colt
int
b_put(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind, __db_getopt_reset;
	DB_ENV *dbenv;
	DB *dbp, **second;
	DBTYPE type;
	DBT key, data;
	db_recno_t recno;
	u_int32_t cachesize, dsize;
	int ch, i, count, secondaries;
	char *ts, buf[64];

	second = NULL;
	type = DB_BTREE;
	cachesize = MEGABYTE;
	dsize = 20;
	count = 100000;
	secondaries = 0;
	ts = "Btree";
	__db_getopt_reset = 1;
	while ((ch = getopt(argc, argv, "C:c:d:s:t:")) != EOF)
		switch (ch) {
		case 'C':
			cachesize = (u_int32_t)atoi(optarg);
			break;
		case 'c':
			count = atoi(optarg);
			break;
		case 'd':
			dsize = (u_int32_t)atoi(optarg);
			break;
		case 's':
			secondaries = atoi(optarg);
			break;
		case 't':
			switch (optarg[0]) {
			case 'B': case 'b':
				ts = "Btree";
				type = DB_BTREE;
				break;
			case 'H': case 'h':
				if (b_util_have_hash())
					return (0);
				ts = "Hash";
				type = DB_HASH;
				break;
			case 'Q': case 'q':
				if (b_util_have_queue())
					return (0);
				ts = "Queue";
				type = DB_QUEUE;
				break;
			case 'R': case 'r':
				ts = "Recno";
				type = DB_RECNO;
				break;
			default:
				return (b_put_usage());
			}
			break;
		case '?':
		default:
			return (b_put_usage());
		}
	argc -= optind;
	argv += optind;
	if (argc != 0)
		return (b_put_usage());

#if DB_VERSION_MAJOR < 3 || DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
	/*
	 * Secondaries were added after DB 3.2.9.
	 */
	if (secondaries)
		return (0);
#endif

	/* Create the environment. */
	DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
	dbenv->set_errfile(dbenv, stderr);
	DB_BENCH_ASSERT(dbenv->set_cachesize(dbenv, 0, cachesize, 0) == 0);
#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 1
	DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR,
	    NULL, DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE, 0666) == 0);
#else
	DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR,
	    DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE, 0666) == 0);
#endif

	/*
	 * Create the database.
	 * Optionally set the record length for Queue.
	 */
	DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0);
	if (type == DB_QUEUE)
		DB_BENCH_ASSERT(dbp->set_re_len(dbp, dsize) == 0);
#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1
	DB_BENCH_ASSERT(
	    dbp->open(dbp, NULL, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
#else
	DB_BENCH_ASSERT(
	    dbp->open(dbp, TESTFILE, NULL, type, DB_CREATE, 0666) == 0);
#endif

	/* Optionally create the secondaries. */
	if (secondaries != 0) {
		DB_BENCH_ASSERT((second =
		    calloc(sizeof(DB *), (size_t)secondaries)) != NULL);
		for (i = 0; i < secondaries; ++i) {
			DB_BENCH_ASSERT(db_create(&second[i], dbenv, 0) == 0);
			(void)snprintf(buf, sizeof(buf), "%d.db", i);
#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1
			DB_BENCH_ASSERT(second[i]->open(second[i], NULL,
			    buf, NULL, DB_BTREE, DB_CREATE, 0600) == 0);
#else
			DB_BENCH_ASSERT(second[i]->open(second[i],
			    buf, NULL, DB_BTREE, DB_CREATE, 0600) == 0);
#endif
#if DB_VERSION_MAJOR > 3 || DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR >= 3
#if DB_VERSION_MAJOR > 3 && DB_VERSION_MINOR > 0
			/*
			 * The DB_TXN argument to Db.associate was added in
			 * 4.1.25.
			 */
			DB_BENCH_ASSERT(dbp->associate(
			    dbp, NULL, second[i], b_put_secondary, 0) == 0);
#else
			DB_BENCH_ASSERT(dbp->associate(
			    dbp, second[i], b_put_secondary, 0) == 0);
#endif
#endif
		}
	}

	/* Store a key/data pair. */
	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	switch (type) {
	case DB_BTREE:
	case DB_HASH:
		key.data = "01234567890123456789";
		key.size = 20;
		break;
	case DB_QUEUE:
	case DB_RECNO:
		recno = 1;
		key.data = &recno;
		key.size = sizeof(recno);
		break;
	case DB_UNKNOWN:
		b_util_abort();
		break;
	}

	data.size = dsize;
	DB_BENCH_ASSERT(
	    (data.data = malloc((size_t)dsize)) != NULL);

	/* Store the key/data pair count times. */
	TIMER_START;
	for (i = 0; i < count; ++i) {
		/* Change data value so the secondaries are updated. */
		(void)snprintf(data.data, data.size, "%10lu", (u_long)i);
		DB_BENCH_ASSERT(dbp->put(dbp, NULL, &key, &data, 0) == 0);
	}
	TIMER_STOP;

	if (type == DB_BTREE || type == DB_HASH)
		printf(
		    "# %d %s database put of 10 byte key, %lu byte data",
		    count, ts, (u_long)dsize);
	else
		printf("# %d %s database put of key, %lu byte data",
		    count, ts, (u_long)dsize);
	if (secondaries)
		printf(" with %d secondaries", secondaries);
	printf("\n");
	TIMER_DISPLAY(count);

	if (second != NULL) {
		for (i = 0; i < secondaries; ++i)
			DB_BENCH_ASSERT(second[i]->close(second[i], 0) == 0);
		free(second);
	}

	DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
	DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);

	return (0);
}
Ejemplo n.º 24
0
Archivo: db3.c Proyecto: xrg/RPM
static int db_init(dbiIndex dbi, const char * dbhome,
		const char * dbfile,
		const char * dbsubfile,
		DB_ENV ** dbenvp)
{
    rpmdb rpmdb = dbi->dbi_rpmdb;
    DB_ENV *dbenv = NULL;
    int eflags;
    int rc;

    if (dbenvp == NULL)
	return 1;

    /* XXX HACK */
    if (rpmdb->db_errfile == NULL)
	rpmdb->db_errfile = stderr;

    eflags = (dbi->dbi_oeflags | dbi->dbi_eflags);
    if (eflags & DB_JOINENV) eflags &= DB_JOINENV;

    if (dbfile) {
	char *dbiflags = prDbiOpenFlags(eflags, 1);
	rpmlog(RPMLOG_DEBUG, "opening  db environment %s/%s %s\n",
		dbhome, dbfile, dbiflags);
	free(dbiflags);
    }

    /* XXX Can't do RPC w/o host. */
    if (dbi->dbi_host == NULL)
	dbi->dbi_ecflags &= ~DB_CLIENT;

    /* XXX Set a default shm_key. */
    if ((dbi->dbi_eflags & DB_SYSTEM_MEM) && dbi->dbi_shmkey == 0) {
#if defined(HAVE_FTOK)
	dbi->dbi_shmkey = ftok(dbhome, 0);
#else
	dbi->dbi_shmkey = 0x44631380;
#endif
    }

    rc = db_env_create(&dbenv, dbi->dbi_ecflags);
    rc = cvtdberr(dbi, "db_env_create", rc, _debug);
    if (dbenv == NULL || rc)
	goto errxit;

  { int xx;

 /* 4.1: dbenv->set_app_dispatch(???) */
 /* 4.1: dbenv->set_alloc(???) */
 /* 4.1: dbenv->set_data_dir(???) */
 /* 4.1: dbenv->set_encrypt(???) */

    dbenv->set_errcall(dbenv, (void *) rpmdb->db_errcall);
    dbenv->set_errfile(dbenv, rpmdb->db_errfile);
    dbenv->set_errpfx(dbenv, rpmdb->db_errpfx);

 /* 4.1: dbenv->set_feedback(???) */
 /* 4.1: dbenv->set_flags(???) */

 /* dbenv->set_paniccall(???) */

#if (DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 5)
    /* 
     * These enable automatic stale lock removal. 
     * thread_count 8 is some kind of "magic minimum" value...
     */
    dbenv->set_thread_count(dbenv, 8);
    dbenv->set_isalive(dbenv, db3isalive);
#endif

    if ((dbi->dbi_ecflags & DB_CLIENT) && dbi->dbi_host) {
	const char * home;
	int retry = 0;

	if ((home = strrchr(dbhome, '/')) != NULL)
	    dbhome = ++home;

	while (retry++ < 5) {
	    xx = dbenv->set_rpc_server(dbenv, NULL, dbi->dbi_host,
		dbi->dbi_cl_timeout, dbi->dbi_sv_timeout, 0);
	    xx = cvtdberr(dbi, "dbenv->set_server", xx, _debug);
	    if (!xx)
		break;
	    (void) sleep(15);
	}
    } else {
#if !(DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
	xx = dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT,
		(dbi->dbi_verbose & DB_VERB_CHKPOINT));
#endif
	xx = dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK,
		(dbi->dbi_verbose & DB_VERB_DEADLOCK));
	xx = dbenv->set_verbose(dbenv, DB_VERB_RECOVERY,
		(dbi->dbi_verbose & DB_VERB_RECOVERY));
	xx = dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR,
		(dbi->dbi_verbose & DB_VERB_WAITSFOR));

	if (dbi->dbi_mmapsize) {
	    xx = dbenv->set_mp_mmapsize(dbenv, dbi->dbi_mmapsize);
	    xx = cvtdberr(dbi, "dbenv->set_mp_mmapsize", xx, _debug);
	}
	if (dbi->dbi_tmpdir) {
	    const char * root;
	    char * tmpdir;

	    root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
	    if ((root[0] == '/' && root[1] == '\0') || rpmdb->db_chrootDone)
		root = NULL;
	    tmpdir = rpmGenPath(root, dbi->dbi_tmpdir, NULL);
	    xx = dbenv->set_tmp_dir(dbenv, tmpdir);
	    xx = cvtdberr(dbi, "dbenv->set_tmp_dir", xx, _debug);
	    tmpdir = _free(tmpdir);
	}
    }

 /* dbenv->set_lk_conflicts(???) */
 /* dbenv->set_lk_detect(???) */
 /* 4.1: dbenv->set_lk_max_lockers(???) */
 /* 4.1: dbenv->set_lk_max_locks(???) */
 /* 4.1: dbenv->set_lk_max_objects(???) */

 /* 4.1: dbenv->set_lg_bsize(???) */
 /* 4.1: dbenv->set_lg_dir(???) */
 /* 4.1: dbenv->set_lg_max(???) */
 /* 4.1: dbenv->set_lg_regionmax(???) */

    if (dbi->dbi_cachesize) {
	xx = dbenv->set_cachesize(dbenv, 0, dbi->dbi_cachesize, 0);
	xx = cvtdberr(dbi, "dbenv->set_cachesize", xx, _debug);
    }

 /* 4.1 dbenv->set_timeout(???) */
 /* dbenv->set_tx_max(???) */
 /* 4.1: dbenv->set_tx_timestamp(???) */
 /* dbenv->set_tx_recover(???) */

 /* dbenv->set_rep_transport(???) */
 /* dbenv->set_rep_limit(???) */

    if (dbi->dbi_no_fsync) {
	xx = db_env_set_func_fsync(db3_fsync_disable);
	xx = cvtdberr(dbi, "db_env_set_func_fsync", xx, _debug);
    }

    if (dbi->dbi_shmkey) {
	xx = dbenv->set_shm_key(dbenv, dbi->dbi_shmkey);
	xx = cvtdberr(dbi, "dbenv->set_shm_key", xx, _debug);
    }
  }

    rc = (dbenv->open)(dbenv, dbhome, eflags, dbi->dbi_perms);
    rc = cvtdberr(dbi, "dbenv->open", rc, _debug);
    if (rc)
	goto errxit;

#if (DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 5)
    /* stale lock removal */
    rc = dbenv->failchk(dbenv, 0);
    rc = cvtdberr(dbi, "dbenv->failchk", rc, _debug);
    if (rc)
	goto errxit;
#endif

    *dbenvp = dbenv;

    return 0;

errxit:
    if (dbenv) {
	int xx;
	xx = dbenv->close(dbenv, 0);
	xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
    }
    return rc;
}
int TestKeyExistErrorReturn(CuTest *ct) {
	DB *pdbp;
	DB *sdbp;
	DB_ENV *dbenv;

	const char *sec_db_file = "secondary.db";
	const char *pri_db_file = "primary.db";
	const char *env_dir = "TESTDIR";
	int i;
	thread_t writer_threads[NUMWRITERS];
	u_int32_t db_flags, env_flags;

	pdbp = sdbp = NULL;
	dbenv = NULL;
	db_flags = DB_CREATE | DB_AUTO_COMMIT | DB_READ_UNCOMMITTED;
	env_flags = DB_CREATE | DB_RECOVER | DB_INIT_LOCK | DB_INIT_LOG |
	    DB_INIT_MPOOL | DB_INIT_TXN | DB_THREAD;

	TestEnvConfigTestSetup(ct);

	CuAssert(ct, "db_env_create", db_env_create(&dbenv, 0) == 0);

	dbenv->set_errfile(dbenv, stderr);
	dbenv->set_errpfx(dbenv, "TestKeyExistErrorReturn");

	/* Run deadlock detector on every lock conflict. */
	CuAssert(ct, "dbenv->set_lk_detect",
	    dbenv->set_lk_detect(dbenv, DB_LOCK_MINWRITE) == 0);

	CuAssert(ct, "dbenv->open",
	    dbenv->open(dbenv, env_dir, env_flags, 0) == 0);

	CuAssert(ct, "db_create", db_create(&pdbp, dbenv, 0) == 0);
	CuAssert(ct, "pdbp->open", pdbp->open(pdbp, NULL,
	    pri_db_file, NULL, DB_BTREE, db_flags, 0) == 0);

	CuAssert(ct, "db_create", db_create(&sdbp, dbenv, 0) == 0);
	CuAssert(ct, "sdbp->set_flags", sdbp->set_flags(sdbp,
	    DB_DUPSORT) == 0);
	CuAssert(ct, "sdbp->open", sdbp->open(sdbp, NULL, sec_db_file,
	    NULL, DB_BTREE, db_flags, 0) == 0);

	CuAssert(ct, "DB->associate", pdbp->associate(pdbp, NULL, sdbp,
	    assoc_callback, DB_AUTO_COMMIT) == 0);

	/* Initialize a mutex. Used to help provide thread ids. */
	(void)mutex_init(&thread_num_lock, NULL);

	for (i = 0; i < NUMWRITERS; ++i)
		(void)thread_create(&writer_threads[i], NULL,
		    writer_thread, (void *)pdbp);

	for (i = 0; i < NUMWRITERS; ++i)
		(void)thread_join(writer_threads[i], NULL);

	if (sdbp != NULL) 
		CuAssert(ct, "sdbp->close", sdbp->close(sdbp, 0) == 0);
	if (pdbp != NULL)
		CuAssert(ct, "pdbp->close", pdbp->close(pdbp, 0) == 0);
	if (dbenv != NULL)
		CuAssert(ct, "dbenv->close", dbenv->close(dbenv, 0) == 0);

	TestEnvConfigTestTeardown(ct);

	return (EXIT_SUCCESS);
}
Ejemplo n.º 26
0
Archivo: db3.c Proyecto: xrg/RPM
static int db3close(dbiIndex dbi, unsigned int flags)
{
    rpmdb rpmdb = dbi->dbi_rpmdb;
    const char * root;
    const char * home;
    char * dbhome;
    const char * dbfile;
    const char * dbsubfile;
    DB * db = dbi->dbi_db;
    int _printit;
    int rc = 0, xx;

    flags = 0;	/* XXX unused */

    /*
     * Get the prefix/root component and directory path.
     */
    root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
    if ((root[0] == '/' && root[1] == '\0') || rpmdb->db_chrootDone)
	root = NULL;
    home = (dbi->dbi_home ? dbi->dbi_home : rpmdb->db_home);

    dbhome = rpmGenPath(root, home, NULL);
    if (dbi->dbi_temporary) {
	dbfile = NULL;
	dbsubfile = NULL;
    } else {
#ifdef	HACK	/* XXX necessary to support dbsubfile */
	dbfile = (dbi->dbi_file ? dbi->dbi_file : db3basename);
	dbsubfile = (dbi->dbi_subfile ? dbi->dbi_subfile : rpmTagGetName(dbi->dbi_rpmtag));
#else
	dbfile = (dbi->dbi_file ? dbi->dbi_file : rpmTagGetName(dbi->dbi_rpmtag));
	dbsubfile = NULL;
#endif
    }

    if (db) {
	rc = db->close(db, 0);
	/* XXX ignore not found error messages. */
	_printit = (rc == ENOENT ? 0 : _debug);
	rc = cvtdberr(dbi, "db->close", rc, _printit);
	db = dbi->dbi_db = NULL;

	rpmlog(RPMLOG_DEBUG, "closed   db index       %s/%s\n",
		dbhome, (dbfile ? dbfile : rpmTagGetName(dbi->dbi_rpmtag)));

    }

    if (rpmdb->db_dbenv != NULL && dbi->dbi_use_dbenv) {
	if (rpmdb->db_opens == 1) {
	    xx = db_fini(dbi, (dbhome ? dbhome : ""), dbfile, dbsubfile);
	    rpmdb->db_dbenv = NULL;
	}
	rpmdb->db_opens--;
    }

    if (dbi->dbi_verify_on_close && !dbi->dbi_temporary) {
	DB_ENV * dbenv = NULL;

	rc = db_env_create(&dbenv, 0);
	rc = cvtdberr(dbi, "db_env_create", rc, _debug);
	if (rc || dbenv == NULL) goto exit;

	dbenv->set_errcall(dbenv, (void *) rpmdb->db_errcall);
	dbenv->set_errfile(dbenv, rpmdb->db_errfile);
	dbenv->set_errpfx(dbenv, rpmdb->db_errpfx);
 /*	dbenv->set_paniccall(???) */
#if !(DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
	xx = dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT,
		(dbi->dbi_verbose & DB_VERB_CHKPOINT));
#endif
	xx = dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK,
		(dbi->dbi_verbose & DB_VERB_DEADLOCK));
	xx = dbenv->set_verbose(dbenv, DB_VERB_RECOVERY,
		(dbi->dbi_verbose & DB_VERB_RECOVERY));
	xx = dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR,
		(dbi->dbi_verbose & DB_VERB_WAITSFOR));

	if (dbi->dbi_tmpdir) {
	    char * tmpdir = rpmGenPath(root, dbi->dbi_tmpdir, NULL);
	    rc = dbenv->set_tmp_dir(dbenv, tmpdir);
	    rc = cvtdberr(dbi, "dbenv->set_tmp_dir", rc, _debug);
	    tmpdir = _free(tmpdir);
	    if (rc) goto exit;
	}
	    
	rc = (dbenv->open)(dbenv, dbhome,
            DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0);
	rc = cvtdberr(dbi, "dbenv->open", rc, _debug);
	if (rc) goto exit;

	rc = db_create(&db, dbenv, 0);
	rc = cvtdberr(dbi, "db_create", rc, _debug);

	if (db != NULL) {
		char * dbf = rpmGetPath(dbhome, "/", dbfile, NULL);

		rc = db->verify(db, dbf, NULL, NULL, flags);
		rc = cvtdberr(dbi, "db->verify", rc, _debug);

		rpmlog(RPMLOG_DEBUG, "verified db index       %s/%s\n",
			(dbhome ? dbhome : ""),
			(dbfile ? dbfile : rpmTagGetName(dbi->dbi_rpmtag)));

	        /*
		 * The DB handle may not be accessed again after
		 * DB->verify is called, regardless of its return.
		 */
		db = NULL;
		dbf = _free(dbf);
	}
	xx = dbenv->close(dbenv, 0);
	xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
	if (rc == 0 && xx) rc = xx;
    }

exit:
    dbi->dbi_db = NULL;

    free(dbhome);

    dbi = db3Free(dbi);

    return rc;
}
Ejemplo n.º 27
0
int
main(void)
{
    /* Initialize our handles */
    DB *dbp = NULL;
    DB_ENV *envp = NULL;

    thread_t writer_threads[NUMWRITERS];
    int i, ret, ret_t;
    u_int32_t env_flags;

    /* Application name */
    const char *prog_name = "txn_guide_inmemory";

    /* Create the environment */
    ret = db_env_create(&envp, 0);
    if (ret != 0) {
	fprintf(stderr, "Error creating environment handle: %s\n",
	    db_strerror(ret));
	goto err;
    }

    env_flags =
      DB_CREATE     |  /* Create the environment if it does not exist */
      DB_INIT_LOCK  |  /* Initialize the locking subsystem */
      DB_INIT_LOG   |  /* Initialize the logging subsystem */
      DB_INIT_TXN   |  /* Initialize the transactional subsystem. This
			* also turns on logging. */
      DB_INIT_MPOOL |  /* Initialize the memory pool (in-memory cache) */
      DB_PRIVATE    |  /* Region files are backed by heap memory.  */
      DB_THREAD;       /* Cause the environment to be free-threaded */

    /* Specify in-memory logging */
    ret = envp->log_set_config(envp, DB_LOG_IN_MEMORY, 1);
    if (ret != 0) {
	fprintf(stderr, "Error setting log subsystem to in-memory: %s\n",
	    db_strerror(ret));
	goto err;
    }

    /*
     * Specify the size of the in-memory log buffer.
     */
    ret = envp->set_lg_bsize(envp, 10 * 1024 * 1024);
    if (ret != 0) {
	fprintf(stderr, "Error increasing the log buffer size: %s\n",
	    db_strerror(ret));
	goto err;
    }

    /*
     * Specify the size of the in-memory cache.
     */
    ret = envp->set_cachesize(envp, 0, 10 * 1024 * 1024, 1);
    if (ret != 0) {
	fprintf(stderr, "Error increasing the cache size: %s\n",
	    db_strerror(ret));
	goto err;
    }

     /*
     * Indicate that we want db to perform lock detection internally.
     * Also indicate that the transaction with the fewest number of
     * write locks will receive the deadlock notification in
     * the event of a deadlock.
     */
    ret = envp->set_lk_detect(envp, DB_LOCK_MINWRITE);
    if (ret != 0) {
	fprintf(stderr, "Error setting lock detect: %s\n",
	    db_strerror(ret));
	goto err;
    }

    /* Now actually open the environment */
    ret = envp->open(envp, NULL, env_flags, 0);
    if (ret != 0) {
	fprintf(stderr, "Error opening environment: %s\n",
	    db_strerror(ret));
	goto err;
    }

    /*
     * If we had utility threads (for running checkpoints or
     * deadlock detection, for example) we would spawn those
     * here. However, for a simple example such as this,
     * that is not required.
     */

    /* Open the database */
    ret = open_db(&dbp, prog_name, NULL,
      envp, DB_DUPSORT);
    if (ret != 0)
	goto err;

    /* Initialize a mutex. Used to help provide thread ids. */
    (void)mutex_init(&thread_num_lock, NULL);

    /* Start the writer threads. */
    for (i = 0; i < NUMWRITERS; i++)
	(void)thread_create(
	    &writer_threads[i], NULL, writer_thread, (void *)dbp);

    /* Join the writers */
    for (i = 0; i < NUMWRITERS; i++)
	(void)thread_join(writer_threads[i], NULL);

err:
    /* Close our database handle, if it was opened. */
    if (dbp != NULL) {
	ret_t = dbp->close(dbp, 0);
	if (ret_t != 0) {
	    fprintf(stderr, "%s database close failed.\n",
		db_strerror(ret_t));
	    ret = ret_t;
	}
    }

    /* Close our environment, if it was opened. */
    if (envp != NULL) {
	ret_t = envp->close(envp, 0);
	if (ret_t != 0) {
	    fprintf(stderr, "environment close failed: %s\n",
		db_strerror(ret_t));
		ret = ret_t;
	}
    }

    /* Final status message and return. */
    printf("I'm all done.\n");
    return (ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
Ejemplo n.º 28
0
int
b_txn(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind;
	DB_ENV *dbenv;
	DB_TXN *txn;
	int tabort, ch, i, count;

	count = 1000;
	tabort = 0;
	while ((ch = getopt(argc, argv, "ac:")) != EOF)
		switch (ch) {
		case 'a':
			tabort = 1;
			break;
		case 'c':
			count = atoi(optarg);
			break;
		case '?':
		default:
			return (usage());
		}
	argc -= optind;
	argv += optind;
	if (argc != 0)
		return (usage());

	/* Create the environment. */
	DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
	dbenv->set_errfile(dbenv, stderr);
#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 1
	DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR,
	    NULL, DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG |
	    DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE, 0666) == 0);
#else
	DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR,
	    DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG |
	    DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE, 0666) == 0);
#endif

	/* Start and commit/abort a transaction count times. */
	TIMER_START;
	if (tabort)
		for (i = 0; i < count; ++i) {
#if DB_VERSION_MAJOR < 4
			DB_BENCH_ASSERT(txn_begin(dbenv, NULL, &txn, 0) == 0);
			DB_BENCH_ASSERT(txn_abort(txn) == 0);
#else
			DB_BENCH_ASSERT(
			    dbenv->txn_begin(dbenv, NULL, &txn, 0) == 0);
			DB_BENCH_ASSERT(txn->abort(txn) == 0);
#endif
		}
	else
		for (i = 0; i < count; ++i) {
#if DB_VERSION_MAJOR < 4
			DB_BENCH_ASSERT(txn_begin(dbenv, NULL, &txn, 0) == 0);
			DB_BENCH_ASSERT(txn_commit(txn, 0) == 0);
#else
			DB_BENCH_ASSERT(
			    dbenv->txn_begin(dbenv, NULL, &txn, 0) == 0);
			DB_BENCH_ASSERT(txn->commit(txn, 0) == 0);
#endif
		}
	TIMER_STOP;

	printf("# %d empty transaction start/%s pairs\n",
	    count, tabort ? "abort" : "commit");
	TIMER_DISPLAY(count);

	DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);

	return (0);
}
Ejemplo n.º 29
0
int
wiredtiger_extension_init(WT_CONNECTION *connection, WT_CONFIG_ARG *config)
{
	/*
	 * List of the WT_DATA_SOURCE methods -- it's static so it breaks at
	 * compile-time should the structure changes underneath us.
	 */
	static WT_DATA_SOURCE wtds = {
		kvs_session_create,		/* session.create */
		NULL,				/* No session.compaction */
		kvs_session_drop,		/* session.drop */
		kvs_session_open_cursor,	/* session.open_cursor */
		kvs_session_rename,		/* session.rename */
		NULL,				/* No session.salvage */
		kvs_session_truncate,		/* session.truncate */
		NULL,				/* No range_truncate */
		kvs_session_verify,		/* session.verify */
		NULL,				/* session.checkpoint */
		kvs_terminate			/* termination */
	};
	DATA_SOURCE *ds;
	DB_ENV *dbenv;
	WT_EXTENSION_API *wt_api;
	size_t len;
	int ret = 0;
	const char *home;
	char *path;

	(void)config;				/* Unused parameters */

	ds = NULL;
	dbenv = NULL;
	path = NULL;
						/* Acquire the extension API */
	wt_api = connection->get_extension_api(connection);

	/* Allocate the local data-source structure. */
	if ((ds = calloc(1, sizeof(DATA_SOURCE))) == NULL)
		return (os_errno());
	ds->wt_api = wt_api;
						/* Configure the global lock */
	if ((ret = lock_init(wt_api, NULL, &ds->rwlock)) != 0)
		goto err;

	ds->wtds = wtds;			/* Configure the methods */

						/* Berkeley DB environment */
	if ((ret = db_env_create(&dbenv, 0)) != 0) {
		ESET(wt_api,
		    NULL, WT_ERROR, "db_env_create: %s", db_strerror(ret));
		goto err;
	}
	dbenv->set_errpfx(dbenv, "bdb");
	dbenv->set_errfile(dbenv, stderr);

	home = connection->get_home(connection);
	len = strlen(home) + 10;
	if ((path = malloc(len)) == NULL)
		goto err;
	(void)snprintf(path, len, "%s/KVS", home);
	if ((ret = dbenv->open(dbenv, path,
	    DB_CREATE | DB_INIT_LOCK | DB_INIT_MPOOL | DB_PRIVATE, 0)) != 0) {
		ESET(wt_api,
		    NULL, WT_ERROR, "DbEnv.open: %s", db_strerror(ret));
		goto err;
	}
	ds->dbenv = dbenv;

	if ((ret =				/* Add the data source */
	    connection->add_data_source(
	    connection, "kvsbdb:", (WT_DATA_SOURCE *)ds, NULL)) != 0) {
		ESET(wt_api, NULL, ret, "WT_CONNECTION.add_data_source");
		goto err;
	}

	if (0) {
err:		if (dbenv != NULL)
			(void)dbenv->close(dbenv, 0);
		free(ds);
	}
	free(path);
	return (ret);
}
Ejemplo n.º 30
0
int
b_open(int argc, char *argv[])
{
	extern char *optarg;
	extern int optind;
	DB_ENV *dbenv;
	DB *dbp;
	DBTYPE type;
	int ch, i, count;
	char *fname, *dbname, *ts;

	type = DB_BTREE;
	count = 1000;
	fname = dbname = NULL;
	ts = "Btree";
	while ((ch = getopt(argc, argv, "c:dft:")) != EOF)
		switch (ch) {
		case 'c':
			count = atoi(optarg);
			break;
		case 'd':
			dbname = "dbname";
			break;
		case 'f':
			fname = "filename";
			break;
		case 't':
			switch (optarg[0]) {
			case 'B': case 'b':
				ts = "Btree";
				type = DB_BTREE;
				break;
			case 'H': case 'h':
				if (b_util_have_hash())
					return (0);
				ts = "Hash";
				type = DB_HASH;
				break;
			case 'Q': case 'q':
				if (b_util_have_queue())
					return (0);
				ts = "Queue";
				type = DB_QUEUE;
				break;
			case 'R': case 'r':
				ts = "Recno";
				type = DB_RECNO;
				break;
			default:
				return (usage());
			}
			break;
		case '?':
		default:
			return (usage());
		}
	argc -= optind;
	argv += optind;
	if (argc != 0)
		return (usage());

#if DB_VERSION_MAJOR < 4
	/*
	 * Don't run in-memory database tests on versions less than 3, it
	 * takes forever and eats memory.
	 */
	if (fname == NULL && dbname == NULL)
		return (0);
#endif
#if DB_VERSION_MAJOR < 4 || DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR < 4
	/*
	 * Named in-memory databases weren't available until 4.4.
	 */
	if (fname == NULL && dbname != NULL)
		return (0);
#endif

	/* Create the environment. */
	DB_BENCH_ASSERT(db_env_create(&dbenv, 0) == 0);
	dbenv->set_errfile(dbenv, stderr);
#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0
	DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR,
	    NULL, DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE, 0666) == 0);
#else
	DB_BENCH_ASSERT(dbenv->open(dbenv, TESTDIR,
	    DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE, 0666) == 0);
#endif

	/* Create the database. */
	DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0);

#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
	DB_BENCH_ASSERT(dbp->open(
	    dbp, NULL, fname, dbname, type, DB_CREATE, 0666) == 0);
#else
	DB_BENCH_ASSERT(dbp->open(
	    dbp, fname, dbname, type, DB_CREATE, 0666) == 0);
#endif
	DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);

	/* Open the database count times. */
	TIMER_START;
	for (i = 0; i < count; ++i) {
		DB_BENCH_ASSERT(db_create(&dbp, dbenv, 0) == 0);
#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
		DB_BENCH_ASSERT(dbp->open(
		    dbp, NULL, fname, dbname, type, DB_CREATE, 0666) == 0);
#else
		DB_BENCH_ASSERT(dbp->open(
		    dbp, fname, dbname, type, DB_CREATE, 0666) == 0);
#endif
		DB_BENCH_ASSERT(dbp->close(dbp, 0) == 0);
	}
	TIMER_STOP;

	printf("# %d %s %sdatabase open/close pairs\n",
	    count, ts,
	    fname == NULL ?
		(dbname == NULL ? "in-memory " : "named in-memory ") :
		(dbname == NULL ? "" : "sub-"));
	TIMER_DISPLAY(count);

	DB_BENCH_ASSERT(dbenv->close(dbenv, 0) == 0);

	return (0);
}