示例#1
0
文件: termbox.c 项目: nsf/termbox
void tb_shutdown(void)
{
	if (termw == -1) {
		fputs("tb_shutdown() should not be called twice.", stderr);
		abort();
	}

	bytebuffer_puts(&output_buffer, funcs[T_SHOW_CURSOR]);
	bytebuffer_puts(&output_buffer, funcs[T_SGR0]);
	bytebuffer_puts(&output_buffer, funcs[T_CLEAR_SCREEN]);
	bytebuffer_puts(&output_buffer, funcs[T_EXIT_CA]);
	bytebuffer_puts(&output_buffer, funcs[T_EXIT_KEYPAD]);
	bytebuffer_puts(&output_buffer, funcs[T_EXIT_MOUSE]);
	bytebuffer_flush(&output_buffer, inout);
	tcsetattr(inout, TCSAFLUSH, &orig_tios);

	shutdown_term();
	close(inout);
	close(winch_fds[0]);
	close(winch_fds[1]);

	cellbuf_free(&back_buffer);
	cellbuf_free(&front_buffer);
	bytebuffer_free(&output_buffer);
	bytebuffer_free(&input_buffer);
	termw = termh = -1;
}
示例#2
0
static void
release_row_data(row_data_t *row_data) {
  if (row_data != NULL) {
    cell_data_t *cell = row_data->first_cell;
    while (cell) {
      bytebuffer_free(cell->value);
      free(cell->hb_cell);
      cell_data_t *cur_cell = cell;
      cell = cell->next_cell;
      free(cur_cell);
    }
    bytebuffer_free(row_data->key);
    free(row_data);
  }
}
示例#3
0
文件: termbox.c 项目: jesskay/termbox
void tb_shutdown(void)
{
	bytebuffer_puts(&output_buffer, funcs[T_SHOW_CURSOR]);
	bytebuffer_puts(&output_buffer, funcs[T_SGR0]);
	bytebuffer_puts(&output_buffer, funcs[T_CLEAR_SCREEN]);
	bytebuffer_puts(&output_buffer, funcs[T_EXIT_CA]);
	bytebuffer_puts(&output_buffer, funcs[T_EXIT_KEYPAD]);
	bytebuffer_flush(&output_buffer, inout);
	tcsetattr(inout, TCSAFLUSH, &orig_tios);

	shutdown_term();
	close(inout);
	close(winch_fds[0]);
	close(winch_fds[1]);

	cellbuf_free(&back_buffer);
	cellbuf_free(&front_buffer);
	bytebuffer_free(&output_buffer);
	bytebuffer_free(&input_buffer);
}
示例#4
0
static void
get_callback(int32_t err, hb_client_t client,
    hb_get_t get, hb_result_t result, void *extra) {
  bytebuffer rowKey = (bytebuffer)extra;
  if (err == 0) {
    const char *table_name;
    size_t table_name_len;
    hb_result_get_table(result, &table_name, &table_name_len);
    HBASE_LOG_INFO("Received get callback for table=\'%.*s\'.",
        table_name_len, table_name);

    printRow(result);

    const hb_cell_t *mycell;
    bytebuffer qualifier = bytebuffer_strcpy("column-a");
    HBASE_LOG_INFO("Looking up cell for family=\'%s\', qualifier=\'%.*s\'.",
        FAMILIES[0], qualifier->length, qualifier->buffer);
    if (hb_result_get_cell(result, FAMILIES[0], 1, qualifier->buffer,
        qualifier->length, &mycell) == 0) {
      HBASE_LOG_INFO("Cell found, value=\'%.*s\', timestamp=%lld.",
          mycell->value_len, mycell->value, mycell->ts);
    } else {
      HBASE_LOG_ERROR("Cell not found.");
    }
    bytebuffer_free(qualifier);
    hb_result_destroy(result);
  } else {
    HBASE_LOG_ERROR("Get failed with error code: %d.", err);
  }

  bytebuffer_free(rowKey);
  hb_get_destroy(get);

  pthread_mutex_lock(&get_mutex);
  get_done = true;
  pthread_cond_signal(&get_cv);
  pthread_mutex_unlock(&get_mutex);
}
示例#5
0
/**
 * Program entry point
 */
int
main(int argc, char **argv) {
  int32_t retCode = 0;
  FILE* logFile = NULL;
  hb_connection_t connection = NULL;
  hb_client_t client = NULL;
  const char *rowkey_prefix = "row";
  const char *value_prefix = "test value";
  bytebuffer column_a = bytebuffer_strcpy("column-a");
  bytebuffer column_b = bytebuffer_strcpy("column-b");

  const char *table_name      = (argc > 1) ? argv[1] : "TempTable";
  const char *zk_ensemble     = (argc > 2) ? argv[2] : "localhost:2181";
  const char *zk_root_znode   = (argc > 3) ? argv[3] : NULL;
  const size_t table_name_len = strlen(table_name);

  const int num_puts = 10;
  hb_put_t put = NULL;

  srand(time(NULL));
  hb_log_set_level(HBASE_LOG_LEVEL_DEBUG); // defaults to INFO
  const char *logFilePath = getenv("HBASE_LOG_FILE");
  if (logFilePath != NULL) {
    FILE* logFile = fopen(logFilePath, "a");
    if (!logFile) {
      retCode = errno;
      fprintf(stderr, "Unable to open log file \"%s\"", logFilePath);
      perror(NULL);
      goto cleanup;
    }
    hb_log_set_stream(logFile); // defaults to stderr
  }

  if ((retCode = hb_connection_create(zk_ensemble,
                                      zk_root_znode,
                                      &connection)) != 0) {
    HBASE_LOG_ERROR("Could not create HBase connection : errorCode = %d.", retCode);
    goto cleanup;
  }

  if ((retCode = ensureTable(connection, table_name)) != 0) {
    HBASE_LOG_ERROR("Failed to ensure table %s : errorCode = %d", table_name, retCode);
    goto cleanup;
  }

  HBASE_LOG_INFO("Connecting to HBase cluster using Zookeeper ensemble '%s'.",
                 zk_ensemble);
  if ((retCode = hb_client_create(connection, &client)) != 0) {
    HBASE_LOG_ERROR("Could not connect to HBase cluster : errorCode = %d.", retCode);
    goto cleanup;
  }

  // let's send a batch of 10 puts with single cell asynchronously
  outstanding_puts_count += num_puts;
  for (int i = 0; i < num_puts; ++i) {
    row_data_t *row_data = (row_data_t *) calloc(1, sizeof(row_data_t));
    row_data->key   = bytebuffer_printf("%s%02d", rowkey_prefix, i);
    hb_put_create(row_data->key->buffer, row_data->key->length, &put);
    hb_mutation_set_table(put, table_name, table_name_len);
    hb_mutation_set_durability(put, DURABILITY_SKIP_WAL);
    hb_mutation_set_bufferable(put, false);

    cell_data_t *cell_data = new_cell_data();
    row_data->first_cell = cell_data;
    cell_data->value = bytebuffer_printf("%s%02d", value_prefix, i);

    hb_cell_t *cell = (hb_cell_t*) calloc(1, sizeof(hb_cell_t));
    cell_data->hb_cell = cell;

    cell->row = row_data->key->buffer;
    cell->row_len = row_data->key->length;
    cell->family = FAMILIES[rand() % 2];
    cell->family_len = 1;
    cell->qualifier = column_a->buffer;
    cell->qualifier_len = column_a->length;
    cell->value = cell_data->value->buffer;
    cell->value_len = cell_data->value->length;
    cell->ts = HBASE_LATEST_TIMESTAMP;

    hb_put_add_cell(put, cell);
    HBASE_LOG_INFO("Sending row with row key : '%.*s'.",
                   cell->row_len, cell->row);
    hb_mutation_send(client, put, put_callback, row_data);
  }
  hb_client_flush(client, client_flush_callback, NULL);
  wait_for_flush();

  wait_for_puts(); // outside the loop, wait for 10 puts to complete

  // now, let's put two cells in a single row
  outstanding_puts_count++;
  {
    row_data_t *row_data = (row_data_t *) calloc(1, sizeof(row_data_t));
    row_data->key = bytebuffer_printf("row_with_two_cells");
    hb_put_create(row_data->key->buffer, row_data->key->length, &put);
    hb_mutation_set_table(put, table_name, table_name_len);
    hb_mutation_set_durability(put, DURABILITY_SYNC_WAL);

    // first cell
    cell_data_t *cell1_data = new_cell_data();
    row_data->first_cell = cell1_data;
    cell1_data->value = bytebuffer_printf("cell1_value_v1");

    hb_cell_t *cell1 = (hb_cell_t*) calloc(1, sizeof(hb_cell_t));
    cell1_data->hb_cell = cell1;

    cell1->row = row_data->key->buffer;
    cell1->row_len = row_data->key->length;
    cell1->family = FAMILIES[0];
    cell1->family_len = 1;
    cell1->qualifier = column_a->buffer;
    cell1->qualifier_len = column_a->length;
    cell1->value = cell1_data->value->buffer;
    cell1->value_len = cell1_data->value->length;
    cell1->ts = 1391111111111L;
    hb_put_add_cell(put, cell1);

    // second cell
    cell_data_t *cell2_data = new_cell_data();
    cell1_data->next_cell = cell2_data;
    cell2_data->value = bytebuffer_printf("cell2_value_v1");

    hb_cell_t *cell2 = (hb_cell_t*) calloc(1, sizeof(hb_cell_t));
    cell2_data->hb_cell = cell2;

    cell2->row = row_data->key->buffer;
    cell2->row_len = row_data->key->length;
    cell2->family = FAMILIES[1];
    cell2->family_len = 1;
    cell2->qualifier = column_b->buffer;
    cell2->qualifier_len = column_b->length;
    cell2->value = cell2_data->value->buffer;
    cell2->value_len = cell2_data->value->length;
    cell2->ts = 1391111111111L;
    hb_put_add_cell(put, cell2);

    HBASE_LOG_INFO("Sending row with row key : '%.*s'.",
                   cell1->row_len, cell1->row);
    hb_mutation_send(client, put, put_callback, row_data);
    wait_for_puts();
  }

  // now, let's put second version in one column
  outstanding_puts_count++;
  {
    row_data_t *row_data = (row_data_t *) calloc(1, sizeof(row_data_t));
    row_data->key = bytebuffer_printf("row_with_two_cells");
    hb_put_create(row_data->key->buffer, row_data->key->length, &put);
    hb_mutation_set_table(put, table_name, table_name_len);
    hb_mutation_set_durability(put, DURABILITY_SYNC_WAL);

    // first cell
    cell_data_t *cell1_data = new_cell_data();
    row_data->first_cell = cell1_data;
    cell1_data->value = bytebuffer_printf("cell1_value_v2");

    hb_cell_t *cell1 = (hb_cell_t*) calloc(1, sizeof(hb_cell_t));
    cell1_data->hb_cell = cell1;

    cell1->row = row_data->key->buffer;
    cell1->row_len = row_data->key->length;
    cell1->family = FAMILIES[0];
    cell1->family_len = 1;
    cell1->qualifier = column_a->buffer;
    cell1->qualifier_len = column_a->length;
    cell1->value = cell1_data->value->buffer;
    cell1->value_len = cell1_data->value->length;
    cell1->ts = 1392222222222L;
    hb_put_add_cell(put, cell1);

    HBASE_LOG_INFO("Sending row with row key : '%.*s'.",
                   cell1->row_len, cell1->row);
    hb_mutation_send(client, put, put_callback, row_data);
    wait_for_puts();
  }

  // now, scan the entire table
  {
    hb_scanner_t scanner = NULL;
    hb_scanner_create(client, &scanner);
    hb_scanner_set_table(scanner, table_name, table_name_len);
    hb_scanner_set_num_max_rows(scanner, 3);  // maximum 3 rows at a time
    hb_scanner_set_num_versions(scanner, 10); // up to 10 versions of the cell
    hb_scanner_next(scanner, scan_callback, NULL); // dispatch the call
    wait_for_scan();
  }

  // fetch a row with row-key="row_with_two_cells"
  {
    bytebuffer rowKey = bytebuffer_strcpy("row_with_two_cells");
    hb_get_t get = NULL;
    hb_get_create(rowKey->buffer, rowKey->length, &get);
    hb_get_add_column(get, FAMILIES[0], 1, NULL, 0);
    hb_get_add_column(get, FAMILIES[1], 1, NULL, 0);
    hb_get_set_table(get, table_name, table_name_len);
    hb_get_set_num_versions(get, 10); // up to ten versions of each column

    get_done = false;
    hb_get_send(client, get, get_callback, rowKey);
    wait_for_get();
  }

  // delete a specific version of a column
  {
    bytebuffer rowKey = bytebuffer_strcpy("row_with_two_cells");
    hb_delete_t del = NULL;
    hb_delete_create(rowKey->buffer, rowKey->length, &del);
    hb_delete_add_column(del, FAMILIES[0], 1,
        column_a->buffer, column_a->length, 1391111111112L);
    hb_mutation_set_table(del, table_name, table_name_len);

    delete_done = false;
    hb_mutation_send(client, del, delete_callback, rowKey);
    wait_for_delete();
  }

  // fetch a row with row-key="row_with_two_cells"
  {
    bytebuffer rowKey = bytebuffer_strcpy("row_with_two_cells");
    hb_get_t get = NULL;
    hb_get_create(rowKey->buffer, rowKey->length, &get);
    hb_get_add_column(get, FAMILIES[0], 1, NULL, 0);
    hb_get_add_column(get, FAMILIES[1], 1, NULL, 0);
    hb_get_set_table(get, table_name, table_name_len);
    hb_get_set_num_versions(get, 10); // up to ten versions of each column

    get_done = false;
    hb_get_send(client, get, get_callback, rowKey);
    wait_for_get();
  }

cleanup:
  if (client) {
    HBASE_LOG_INFO("Disconnecting client.");
    hb_client_destroy(client, client_disconnection_callback, NULL);
    wait_client_disconnection();
  }

  if (connection) {
    hb_connection_destroy(connection);
  }

  if (column_a) {
    bytebuffer_free(column_a);  // do not need 'column' anymore
  }
  if (column_b) {
    bytebuffer_free(column_b);
  }

  if (logFile) {
    fclose(logFile);
  }

  pthread_cond_destroy(&puts_cv);
  pthread_mutex_destroy(&puts_mutex);

  pthread_cond_destroy(&get_cv);
  pthread_mutex_destroy(&get_mutex);

  pthread_cond_destroy(&del_cv);
  pthread_mutex_destroy(&del_mutex);

  pthread_cond_destroy(&client_destroyed_cv);
  pthread_mutex_destroy(&client_destroyed_mutex);

  return retCode;
}