コード例 #1
0
static void
print_environment(ham_env_t *env)
{
    /*
     * we need a temp. database
     */
    ham_db_t *db;
    ham_status_t st;

    st=ham_new(&db);
    if (st)
        error("ham_new", st);

    st=ham_env_open_db(env, db, 0xf001, 0, 0);
    if (st)
        error("ham_env_open_db", st);

    printf("environment\n");
    printf("    pagesize:                   %u\n", env_get_pagesize(env));
    printf("    version:                    %u.%u.%u.%u\n", 
            env_get_version(env, 0),
            env_get_version(env, 1),
            env_get_version(env, 2),
            env_get_version(env, 3));
    printf("    serialno:                   %u\n", env_get_serialno(env));
    printf("    max databases:              %u\n", env_get_max_databases(env));

    st=ham_close(db, 0);
    if (st)
        error("ham_close", st);
    ham_delete(db);
}
コード例 #2
0
ファイル: hamsterdb.hpp プロジェクト: Seldom/miranda-ng
    /** Opens an existing Database in the Environment. */
    db open_db(uint16_t name, uint32_t flags = 0,
                const ham_parameter_t *param = 0) {
      ham_db_t *dbh;

      ham_status_t st = ham_env_open_db(m_env, &dbh, name, flags, param);
      if (st)
        throw error(st);

      return (hamsterdb::db(dbh));
    }
コード例 #3
0
ファイル: db2.c プロジェクト: zhengshuxin/hamsterdb
int
main(int argc, char **argv) {
  ham_status_t st;
  ham_env_t *env = 0;
  ham_db_t *src_db = 0;
  ham_db_t *dest_db = 0;
  ham_u16_t src_name;
  ham_u16_t dest_name;
  const char *env_path = 0;

  /* check and parse the command line parameters */
  if (argc != 4)
    usage();
  env_path = argv[1];
  src_name = atoi(argv[2]);
  dest_name = atoi(argv[3]);
  if (src_name == 0 || dest_name == 0)
    usage();

  /* open the Environment */
  st = ham_env_open(&env, env_path, 0, 0);
  if (st)
    error("ham_env_open", st);

  /* open the source database */
  st = ham_env_open_db(env, &src_db, src_name, 0, 0);
  if (st)
    error("ham_env_open_db", st);

  /* create the destination database */
  st = ham_env_create_db(env, &dest_db, dest_name,
                  HAM_ENABLE_DUPLICATE_KEYS, 0);
  if (st)
    error("ham_env_create_db", st);

  /* copy the data */
  copy_db(src_db, dest_db);

  /* clean up and return */
  st = ham_env_close(env, HAM_AUTO_CLEANUP);
  if (st)
    error("ham_env_close", st);

  printf("\nsuccess!\n");
  return (0);
}
コード例 #4
0
ファイル: hammy_db.c プロジェクト: Zabrane/hammy
int hammy_setup_env(ErlNifEnv *env, hammy_db *db) {
    int retval = HAMMY_TRUE;
    ham_size_t count = HAMMY_MAX_DB;
    ham_u16_t *names = (ham_u16_t *) enif_alloc_compat(env, sizeof(ham_u16_t) * HAMMY_MAX_DB);
    ham_new(&(db->databases[0]));
    if ( ham_env_get_database_names(db->env, names, &count) == HAM_SUCCESS) {
        /* New env, so let's setup data database */
        if (count == 0) {
            /* Database 1 is data */
            ham_env_create_db(db->env, db->databases[0], 1, HAM_USE_BTREE, HAMMY_DB_OPTS);
            db->db_count = 1;
        }
        else {
            ham_env_open_db(db->env, db->databases[0], 1, 0, EMPTY_OPTS);
            /* TODO: Load existing user-defined indices */
            db->db_count = 1;
        }
    }
    else {
        retval = HAMMY_FALSE;
    }
    enif_free_compat(env, names);
    return retval;
}
コード例 #5
0
int
main(int argc, char **argv)
{
    unsigned opt;
    char *param, *filename=0, *endptr=0;
    unsigned short dbname=0xffff;
    int full=0;

    ham_u16_t names[1024];
    ham_size_t i, names_count=1024;
    ham_status_t st;
    ham_env_t *env;
    ham_db_t *db;

    ham_u32_t maj, min, rev;
    const char *licensee, *product;
    ham_get_license(&licensee, &product);
    ham_get_version(&maj, &min, &rev);

    getopts_init(argc, argv, "ham_info");

    while ((opt=getopts(&opts[0], &param))) {
        switch (opt) {
            case ARG_DBNAME:
                if (!param) {
                    printf("Parameter `dbname' is missing.\n");
                    return (-1);
                }
                dbname=(short)strtoul(param, &endptr, 0);
                if (endptr && *endptr) {
                    printf("Invalid parameter `dbname'; numerical value "
                           "expected.\n");
                    return (-1);
                }
                break;
            case ARG_FULL:
                full=1;
                break;
            case GETOPTS_PARAMETER:
                if (filename) {
                    printf("Multiple files specified. Please specify "
                           "only one filename.\n");
                    return (-1);
                }
                filename=param;
                break;
            case ARG_HELP:
                printf("hamsterdb %d.%d.%d - Copyright (C) 2005-2007 "
                       "Christoph Rupp ([email protected]).\n\n",
                       maj, min, rev);

                if (licensee[0]=='\0')
                    printf(
                       "This program is free software; you can redistribute "
                       "it and/or modify it\nunder the terms of the GNU "
                       "General Public License as published by the Free\n"
                       "Software Foundation; either version 2 of the License,\n"
                       "or (at your option) any later version.\n\n"
                       "See file COPYING.GPL2 and COPYING.GPL3 for License "
                       "information.\n\n");
                else
                    printf("Commercial version; licensed for %s (%s)\n\n",
                            licensee, product);

                printf("usage: ham_info [-db DBNAME] [-f] file\n");
                printf("usage: ham_info -h\n");
                printf("       -h:         this help screen (alias: --help)\n");
                printf("       -db DBNAME: only print info about "
                        "this database (alias: --dbname=<arg>)\n");
                printf("       -f:         print full information "
                        "(alias: --full)\n");
                return (0);
            default:
                printf("Invalid or unknown parameter `%s'. "
                       "Enter `ham_info --help' for usage.", param);
                return (-1);
        }
    }

    if (!filename) {
        printf("Filename is missing. Enter `ham_info --help' for usage.\n");
        return (-1);
    }

    /*
     * open the environment
     */
    st=ham_env_new(&env);
    if (st!=HAM_SUCCESS)
        error("ham_env_new", st);
    st=ham_env_open(env, filename, HAM_READ_ONLY);
    if (st==HAM_FILE_NOT_FOUND) {
        printf("File `%s' not found or unable to open it\n", filename);
        return (-1);
    }
    else if (st!=HAM_SUCCESS)
        error("ham_env_open", st);

    /*
     * print information about the environment
     */
    print_environment(env);

    /*
     * get a list of all databases
     */
    st=ham_env_get_database_names(env, names, &names_count);
    if (st!=HAM_SUCCESS)
        error("ham_env_get_database_names", st);

    /*
     * did the user specify a database name? if yes, show only this database
     */
    if (dbname!=0xffff) {
        st=ham_new(&db);
        if (st)
            error("ham_new", st);
    
        st=ham_env_open_db(env, db, dbname, 0, 0);
        if (st==HAM_DATABASE_NOT_FOUND) {
            printf("Database %u (0x%x) not found\n", dbname, dbname);
            return (-1);
        }
        else if (st)
            error("ham_env_open_db", st);
    
        print_database(db, dbname, full);
    
        st=ham_close(db, 0);
        if (st)
            error("ham_close", st);
        ham_delete(db);
    }
    else {
        /*
         * otherwise: for each database: print information about the database
         */
        for (i=0; i<names_count; i++) {
            st=ham_new(&db);
            if (st)
                error("ham_new", st);
    
            st=ham_env_open_db(env, db, names[i], 0, 0);
            if (st)
                error("ham_env_open_db", st);
    
            print_database(db, names[i], full);
    
            st=ham_close(db, 0);
            if (st)
                error("ham_close", st);
            ham_delete(db);
        }
    } 
    /*
     * clean up
     */
    st=ham_env_close(env, 0);
    if (st!=HAM_SUCCESS)
        error("ham_env_close", st);

    ham_env_delete(env);

    return (0);
}
コード例 #6
0
ファイル: client1.c プロジェクト: zhengshuxin/hamsterdb
int
main(int argc, char **argv) {
  int i;
  ham_status_t st;      /* status variable */
  ham_env_t *env;       /* hamsterdb Environment object */
  ham_db_t *db;         /* hamsterdb Database object */
  ham_key_t key = {0};     /* the structure for a key */
  ham_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 = ham_env_create(&env, "ham://localhost:8080/env1.db", 0, 0, 0);
  if (st != HAM_SUCCESS)
    error("ham_env_create", st);

  /* now open a Database in this Environment */
  st = ham_env_open_db(env, &db, 13, 0, 0);
  if (st != HAM_SUCCESS)
    error("ham_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 = ham_db_insert(db, 0, &key, &record, 0);
    if (st != HAM_SUCCESS)
      error("ham_db_insert", st);
  }

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

    st = ham_db_find(db, 0, &key, &record, 0);
    if (st != HAM_SUCCESS)
      error("ham_db_find", st);

    /* check if the value is ok */
    if (*(int *)record.data != i) {
      printf("ham_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 = ham_db_erase(db, 0, &key, 0);
    if (st != HAM_SUCCESS)
      error("ham_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 = ham_db_find(db, 0, &key, &record, 0);
    if (st != HAM_KEY_NOT_FOUND)
      error("ham_db_find", st);
  }

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

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

  printf("success!\n");
  return (0);
}
コード例 #7
0
ファイル: env1.c プロジェクト: ziposoft/ac1
int
main(int argc, char **argv) {
  int i;
  ham_status_t st;    /* status variable */
  ham_db_t *db[MAX_DBS];  /* hamsterdb database objects */
  ham_env_t *env;     /* hamsterdb environment */
  ham_cursor_t *cursor[MAX_DBS]; /* a cursor for each database */
  ham_key_t key, cust_key, ord_key;
  ham_record_t record, cust_record, ord_record;

  customer_t customers[MAX_CUSTOMERS] = {
    { 1, "Alan Antonov Corp." },
    { 2, "Barry Broke Inc." },
    { 3, "Carl Caesar Lat." },
    { 4, "Doris Dove Brd." }
  };

  order_t orders[MAX_ORDERS] = {
    { 1, 1, "Joe" },
    { 2, 1, "Tom" },
    { 3, 3, "Joe" },
    { 4, 4, "Tom" },
    { 5, 3, "Ben" },
    { 6, 3, "Ben" },
    { 7, 4, "Chris" },
    { 8, 1, "Ben" }
  };

  memset(&key, 0, sizeof(key));
  memset(&record, 0, sizeof(record));
  memset(&cust_key, 0, sizeof(cust_key));
  memset(&cust_record, 0, sizeof(cust_record));
  memset(&ord_key, 0, sizeof(ord_key));
  memset(&ord_record, 0, sizeof(ord_record));

  /* Now create a new hamsterdb Environment */
  st = ham_env_create(&env, "test.db", 0, 0664, 0);
  if (st != HAM_SUCCESS)
    error("ham_env_create", st);

  /*
   * Then create the two Databases in this Environment; each Database
   * has a name - the first is our "customer" Database, the second
   * is for the "orders"
   */
  st = ham_env_create_db(env, &db[0], DBNAME_CUSTOMER, 0, 0);
  if (st != HAM_SUCCESS)
    error("ham_env_create_db (customer)", st);
  st = ham_env_create_db(env, &db[1], DBNAME_ORDER, 0, 0);
  if (st != HAM_SUCCESS)
    error("ham_env_create_db (order)", st);

  /* Create a Cursor for each Database */
  for (i = 0; i < MAX_DBS; i++) {
    st = ham_cursor_create(&cursor[i], db[i], 0, 0);
    if (st != HAM_SUCCESS) {
      printf("ham_cursor_create() failed with error %d\n", st);
      return (-1);
    }
  }

  /* Insert a few customers in the first database */
  for (i = 0; i < MAX_CUSTOMERS; i++) {
    key.size = sizeof(int);
    key.data = &customers[i].id;

    record.size = sizeof(customer_t);
    record.data = &customers[i];

    st = ham_db_insert(db[0], 0, &key, &record, 0);
    if (st != HAM_SUCCESS)
      error("ham_db_insert (customer)", st);
  }

  /* And now the orders in the second database */
  for (i = 0; i < MAX_ORDERS; i++) {
    key.size = sizeof(int);
    key.data = &orders[i].id;

    record.size = sizeof(order_t);
    record.data = &orders[i];

    st = ham_db_insert(db[1], 0, &key, &record, 0);
    if (st != HAM_SUCCESS)
      error("ham_db_insert (order)", st);
  }

  /*
   * To demonstrate even more functions: close all objects, then
   * re-open the environment and the two databases.
   *
   * Note that ham_env_close automatically calls ham_db_close on all
   * databases.
   */
  for (i = 0; i < MAX_DBS; i++) {
    st = ham_cursor_close(cursor[i]);
    if (st != HAM_SUCCESS)
      error("ham_cursor_close", st);
  }
  st = ham_env_close(env, 0);
  if (st != HAM_SUCCESS)
    error("ham_env_close", st);

  /* Now reopen the environment and the databases */
  st = ham_env_open(&env, "test.db", 0, 0);
  if (st != HAM_SUCCESS)
    error("ham_env_open", st);
  st = ham_env_open_db(env, &db[0], DBNAME_CUSTOMER, 0, 0);
  if (st != HAM_SUCCESS)
    error("ham_env_open_db (customer)", st);
  st = ham_env_open_db(env, &db[1], DBNAME_ORDER, 0, 0);
  if (st != HAM_SUCCESS)
    error("ham_env_open_db (order)", st);

  /* Re-create a cursor for each database */
  for (i = 0; i < MAX_DBS; i++) {
    st = ham_cursor_create(&cursor[i], db[i], 0, 0);
    if (st != HAM_SUCCESS) {
      printf("ham_cursor_create() failed with error %d\n", st);
      return (-1);
    }
  }

  /*
   * Now start the query - we want to dump each customer with his
   * orders
   *
   * We have a loop with two cursors - the first cursor looping over
   * the database with customers, the second loops over the orders.
   */
  while (1) {
    customer_t *customer;

    st = ham_cursor_move(cursor[0], &cust_key, &cust_record,
            HAM_CURSOR_NEXT);
    if (st != HAM_SUCCESS) {
      /* reached end of the database? */
      if (st == HAM_KEY_NOT_FOUND)
        break;
      else
        error("ham_cursor_next(customer)", st);
    }

    customer = (customer_t *)cust_record.data;

    /* print the customer id and name */
    printf("customer %d ('%s')\n", customer->id, customer->name);

    /*
     * The inner loop prints all orders of this customer. Move the
     * cursor to the first entry.
     */
    st = ham_cursor_move(cursor[1], &ord_key, &ord_record,
            HAM_CURSOR_FIRST);
    if (st != HAM_SUCCESS) {
      /* reached end of the database? */
      if (st == HAM_KEY_NOT_FOUND)
        continue;
      else
        error("ham_cursor_next(order)", st);
    }

    do {
      order_t *order = (order_t *)ord_record.data;

      /* print this order, if it belongs to the current customer */
      if (order->customer_id == customer->id)
        printf("  order: %d (assigned to %s)\n",
            order->id, order->assignee);

      st = ham_cursor_move(cursor[1], &ord_key,
          &ord_record, HAM_CURSOR_NEXT);
      if (st != HAM_SUCCESS) {
        /* reached end of the database? */
        if (st == HAM_KEY_NOT_FOUND)
          break;
        else
          error("ham_cursor_next(order)", st);
      }
    } while (1);
  }

  /*
   * Now close the environment handle; the flag HAM_AUTO_CLEANUP will
   * automatically close all databases and cursors
   */
  st = ham_env_close(env, HAM_AUTO_CLEANUP);
  if (st != HAM_SUCCESS)
    error("ham_env_close", st);

  printf("success!\n");
  return (0);
}
コード例 #8
0
ファイル: db1.c プロジェクト: mathieurodic/hamsterdb
int
main(int argc, char **argv) {
  uint32_t i;
  ham_status_t st;             /* status variable */
  ham_env_t *env;              /* hamsterdb environment object */
  ham_db_t *db;                /* hamsterdb database object */
  ham_key_t key = {0};         /* the structure for a key */
  ham_record_t record = {0};   /* the structure for a record */
  ham_parameter_t params[] = { /* parameters for ham_env_create_db */
    {HAM_PARAM_KEY_TYPE, HAM_TYPE_UINT32},
    {HAM_PARAM_RECORD_SIZE, sizeof(uint32_t)},
    {0, }
  };

  /* First create a new hamsterdb Environment */
  st = ham_env_create(&env, "test.db", 0, 0664, 0);
  if (st != HAM_SUCCESS)
    error("ham_create", st);

  /* And in this Environment we create a new Database for uint32-keys
   * and uint32-records. */
  st = ham_env_create_db(env, &db, DATABASE_NAME, 0, &params[0]);
  if (st != HAM_SUCCESS)
    error("ham_create", st);

  /*
   * now we can insert, delete or lookup values in the database
   *
   * for our test program, we just insert a few values, then look them
   * up, then delete them and try to look them up again (which will fail).
   */
  for (i = 0; i < LOOP; i++) {
    key.data = &i;
    key.size = sizeof(i);

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

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

  /*
   * now lookup all values
   *
   * for ham_db_find(), we could use the flag HAM_RECORD_USER_ALLOC, if WE
   * allocate record.data (otherwise the memory is automatically allocated
   * by hamsterdb)
   */
  for (i = 0; i < LOOP; i++) {
    key.data = &i;
    key.size = sizeof(i);

    st = ham_db_find(db, 0, &key, &record, 0);
    if (st != HAM_SUCCESS)
      error("ham_db_find", st);

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

  /*
   * close the database handle, then re-open it (to demonstrate how to open
   * an Environment and a Database)
   */
  st = ham_db_close(db, 0);
  if (st != HAM_SUCCESS)
    error("ham_db_close", st);
  st = ham_env_close(env, 0);
  if (st != HAM_SUCCESS)
    error("ham_env_close", st);

  st = ham_env_open(&env, "test.db", 0, 0);
  if (st != HAM_SUCCESS)
    error("ham_env_open", st);
  st = ham_env_open_db(env, &db, DATABASE_NAME, 0, 0);
  if (st != HAM_SUCCESS)
    error("ham_env_open_db", st);

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

    st = ham_db_erase(db, 0, &key, 0);
    if (st != HAM_SUCCESS)
      error("ham_db_erase", st);
  }

  /*
   * once more we try to find all values... every ham_db_find() call must
   * now fail with HAM_KEY_NOT_FOUND
   */
  for (i = 0; i < LOOP; i++) {
    key.size = sizeof(i);
    key.data = &i;

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

  /* we're done! close the handles. HAM_AUTO_CLEANUP will also close the
   * 'db' handle */
  st = ham_env_close(env, HAM_AUTO_CLEANUP);
  if (st != HAM_SUCCESS)
    error("ham_env_close", st);

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