/**
 * Initialize the database storage
 * @return true if the database was initialized successfully, false otherwise
 */
bool initialize_storage(void) 
{
  ib_err_t error;
  ib_id_t tid;

  checked(ib_init());
  checked(ib_cfg_set_text("data_home_dir", "/tmp/memcached_light"));
  checked(ib_cfg_set_text("log_group_home_dir", "/tmp/memcached_light"));
  checked(ib_cfg_set_bool_on("file_per_table"));
  checked(ib_startup("barracuda"));

  /* check to see if the table exists or if we should create the schema */
  error= ib_table_get_id(tablename, &tid);
  if (error == DB_TABLE_NOT_FOUND) 
  {
    if (!create_schema()) 
    {
      return false;
    }
  } 
  else if (error != DB_SUCCESS) 
  {
    fprintf(stderr, "Failed to get table id: %s\n", ib_strerror(error));
    return false;
  }

  return true;

 error_exit:

  return false;
}
Beispiel #2
0
static void do_init_table(void* arg)
{
    PortState* state = (PortState*)arg;

    unsigned char fmt_code = UNPACK_BYTE(state->work_buffer, 0);
    // Unpack the table name (pre-formatted to be Database/TableName)
    char* table = UNPACK_STRING(state->work_buffer, 1);
    ib_id_t table_id;
    ib_tbl_fmt_t table_fmt;
    
    switch(fmt_code)
    {
      case FORMAT_REDUNDANT:
        table_fmt = IB_TBL_REDUNDANT;
        break;
      case FORMAT_COMPACT:
        table_fmt = IB_TBL_COMPACT;
        break;
      case FORMAT_DYNAMIC:
        table_fmt = IB_TBL_DYNAMIC;
        break;
      case FORMAT_COMPRESSED:
        table_fmt = IB_TBL_COMPRESSED;
        break;
      default:
        send_error_atom(state, "bad_format");
        return;
    }

    // If the table doesn't exist, create it
    if (ib_table_get_id(table, &table_id) != DB_SUCCESS)
    {
        // Start a txn for schema access and be sure to make it serializable
        ib_trx_t txn = ib_trx_begin(IB_TRX_SERIALIZABLE);
        ib_schema_lock_exclusive(txn);

        // Create the table schema
        ib_tbl_sch_t schema;
        ib_table_schema_create(table, &schema, table_fmt, 0);
        ib_table_schema_add_col(schema, "key", IB_VARBINARY, IB_COL_NONE, 0, 255);
        ib_table_schema_add_col(schema, "value", IB_BLOB, IB_COL_NONE, 0, 0);

        // Create primary index on key
        ib_idx_sch_t index;
        ib_table_schema_add_index(schema, "PRIMARY_KEY", &index);
        ib_index_schema_add_col(index, "key", 0);
        ib_index_schema_set_clustered(index);

        // Create the actual table
        ib_err_t rc = ib_table_create(txn, schema, &table_id);

        // Release the schema -- doesn't matter if table was created or not at this point
        ib_schema_unlock(txn);

        if (rc == DB_SUCCESS)
        {
            // Commit changes to schema (if any)
            ib_trx_commit(txn);
        }
        else
        {
            // Failed to create table -- rollback and exit
            ib_trx_rollback(txn);
            send_error_str(state, ib_strerror(rc));
            return;
        }
    }

    // Guaranteed at this point to have a valid table_id
    ErlDrvTermData response[] = { ERL_DRV_ATOM,   driver_mk_atom("innostore_ok"),
                                  ERL_DRV_BUF2BINARY, (ErlDrvTermData)&table_id,
                                                      (ErlDrvUInt)sizeof(table_id),
                                  ERL_DRV_TUPLE, 2};
    driver_send_term(state->port, state->port_owner, response,
                     sizeof(response) / sizeof(response[0]));
}