Exemple #1
0
 /** Closes the Database. */
 void close(uint32_t flags = 0) {
   if (!m_db)
     return;
   // disable auto-cleanup; all objects will be destroyed when 
   // going out of scope
   flags &= ~UPS_AUTO_CLEANUP;
   ups_status_t st = ups_db_close(m_db, flags);
   m_db = 0;
   if (st)
     throw error(st);
 }
Exemple #2
0
int
main(int argc, char **argv) {
  int i;
  ups_status_t st;      /* status variable */
  ups_env_t *env;       /* upscaledb Environment object */
  ups_db_t *db;         /* upscaledb Database object */
  ups_key_t key = {0};     /* the structure for a key */
  ups_record_t record = {0};   /* the structure for a record */

  /*
   * Connect to the server which should listen at 8080. The server is
   * implemented in server1.c.
   */
  st = ups_env_create(&env, "ups://localhost:8080/env1.db", 0, 0, 0);
  if (st != UPS_SUCCESS)
    error("ups_env_create", st);

  /* now open a Database in this Environment */
  st = ups_env_open_db(env, &db, 13, 0, 0);
  if (st != UPS_SUCCESS)
    error("ups_env_open_db", st);

  /* now we can insert, delete or lookup values in the database */
  for (i = 0; i < LOOP; i++) {
    key.data = &i;
    key.size = sizeof(i);

    record.size = key.size;
    record.data = key.data;

    st = ups_db_insert(db, 0, &key, &record, 0);
    if (st != UPS_SUCCESS)
      error("ups_db_insert", st);
  }

  /* now lookup all values */
  for (i = 0; i < LOOP; i++) {
    key.data = &i;
    key.size = sizeof(i);

    st = ups_db_find(db, 0, &key, &record, 0);
    if (st != UPS_SUCCESS)
      error("ups_db_find", st);

    /* check if the value is ok */
    if (*(int *)record.data != i) {
      printf("ups_db_find() ok, but returned bad value\n");
      return (-1);
    }
  }

  /* erase everything */
  for (i = 0; i < LOOP; i++) {
    key.data = &i;
    key.size = sizeof(i);

    st = ups_db_erase(db, 0, &key, 0);
    if (st != UPS_SUCCESS)
      error("ups_db_erase", st);
  }

  /* and make sure that the database is empty */
  for (i = 0; i < LOOP; i++) {
    key.data = &i;
    key.size = sizeof(i);

    st = ups_db_find(db, 0, &key, &record, 0);
    if (st != UPS_KEY_NOT_FOUND)
      error("ups_db_find", st);
  }

  /* close the database handle */
  st = ups_db_close(db, 0);
  if (st != UPS_SUCCESS)
    error("ups_db_close", st);

  /* close the environment handle */
  st = ups_env_close(env, 0);
  if (st != UPS_SUCCESS)
    error("ups_env_close", st);

  printf("success!\n");
  return (0);
}