END_TEST

START_TEST (test_text_read)
{
  uint8_t buf [] = "0.123456\t1\t42\tabde\t3.1416\t111\nbleftover text for next line";
  char meta [] = "1 mympstrm label:string pi:double fighter:uint32";
  MBuffer *mbuf = mbuf_create();
  struct oml_message msg;
  struct schema *schema = schema_from_meta (meta);
  OmlValue values[3];

  oml_value_array_init(values, 3);

  bzero(&msg, sizeof(msg));

  mbuf_write (mbuf, buf, sizeof(buf));

  int result = text_read_msg_start (&msg, mbuf);

  fprintf (stderr, "STRM: %d\n", msg.stream);
  fprintf (stderr, "SEQN: %u\n", msg.seqno);
  fprintf (stderr, "TS  : %f\n", msg.timestamp);
  fprintf (stderr, "LEN : %d\n", msg.length);
  fprintf (stderr, "COUNT: %d\n", msg.count);

  result = text_read_msg_values (&msg, mbuf, schema, values);

  result *= 42;

  oml_value_array_reset(values, 3);
}
END_TEST

START_TEST (test_bin_read)
{
  /* DATA_P, count=1, stream=3, { LONG_T 42 } */
  uint8_t buf [] = { 0xAA, 0xAA, 0x01, 0x00, 0x00,
                     0x3, 0x1, // count = 1, stream = 3
                     0x01, 0x00, 0x00, 0x00, 0x32, // LONG_T 50
                     0x02, 0x54, 0x00, 0x00, 0x00, 0x05, // DOUBLE_T 42.0
                     0x01, 0x00, 0x10, 0xF4, 0x47, // LONG_T 1111111
                     0x02, 0x54, 0x00, 0x00, 0x00, 0x05, // DOUBLE_T 42.0
                     0x04, 0x03, 'A',  'B',  'C' // STRING_T "ABC"
  };
  char meta [] = "3 mympstrm id:long hitchhiker:double sesame:string";
  MBuffer *mbuf = mbuf_create ();
  struct oml_message msg;
  struct schema *schema = schema_from_meta (meta);
  OmlValue values [3];
  int result;

  oml_value_array_init(values, 3);

  bzero(&msg, sizeof(msg));

  int size = sizeof (buf) - 5;
  uint16_t nv = htons (size);
  memcpy (buf + 3, &nv, 2);

  mbuf_write (mbuf, buf, sizeof (buf));

  result = bin_read_msg_start (&msg, mbuf);

  fail_unless(result > 0, "Unable to start reading binary message");

  fprintf (stderr, "---\n");
  fprintf (stderr, "STRM: %d\n", msg.stream);
  fprintf (stderr, "SEQN: %u\n", msg.seqno);
  fprintf (stderr, "TS  : %f\n", msg.timestamp);
  fprintf (stderr, "LEN : %d\n", msg.length);
  fprintf (stderr, "COUNT: %d\n", msg.count);

  result = bin_read_msg_values (&msg, mbuf, schema, values);

  int i = 0;
  for (i = 0; i < 3; i++) {
    char s[64];
    oml_value_to_s (&values[i], s, 64);
    fprintf (stderr, "%s\n", s);
  }

  oml_value_array_reset(values, 3);
}
Beispiel #3
0
TableDescr*
psql_get_table_list (Database *database, int *num_tables)
{
  PsqlDB *self = database->handle;
  const char *stmt_tablename =
    "SELECT tablename FROM pg_tables WHERE tablename NOT LIKE 'pg%' AND tablename NOT LIKE 'sql%';";
  PGresult *res;
  TableDescr *tables = NULL;
  int rows, cols, i;

  *num_tables = -1;
  res = PQexec (self->conn, stmt_tablename);
  if (PQresultStatus (res) != PGRES_TUPLES_OK) {
    logerror("psql:%s: Couldn't get list of tables: %s\n",
        database->name, PQerrorMessage (self->conn));
    PQclear (res);
    return NULL;
  }
  rows = PQntuples (res);
  cols = PQnfields (res);

  if (cols < 1)
    return NULL;

  int have_meta = 0;
  for (i = 0; i < rows && !have_meta; i++)
    if (strcmp (PQgetvalue (res, i, 0), "_experiment_metadata") == 0)
      have_meta = 1;

  if(!have_meta)
    logdebug("psql:%s: No metadata found\n", database->name);

  *num_tables = 0;

  for (i = 0; i < rows; i++) {
    char *val = PQgetvalue (res, i, 0);
    logdebug("psql:%s: Found table '%s'", database->name, val);
    MString *str = mstring_create ();
    TableDescr *t = NULL;

    if (have_meta) {
      mstring_sprintf (str, "SELECT value FROM _experiment_metadata WHERE key='table_%s';", val);
      PGresult *schema_res = PQexec (self->conn, mstring_buf (str));
      if (PQresultStatus (schema_res) != PGRES_TUPLES_OK) {
        logdebug("psql:%s: Couldn't get schema for table '%s': %s; skipping\n",
            database->name, val, PQerrorMessage (self->conn));
        mstring_delete (str);
        continue;
      }
      int rows = PQntuples (schema_res);
      if (rows == 0) {
        logdebug("psql:%s: Metadata for table '%s' found but empty\n", database->name, val);
        t = table_descr_new (val, NULL); // Don't know the schema for this table
      } else {
        logdebug("psql:%s: Stored schema for table '%s': %s\n", database->name, val, PQgetvalue (schema_res, 0, 0));
        struct schema *schema = schema_from_meta (PQgetvalue (schema_res, 0, 0));
        t = table_descr_new (val, schema);
      }
      PQclear (schema_res);
      mstring_delete (str);
    } else {
      t = table_descr_new (val, NULL);
    }

    if (t) {
      t->next = tables;
      tables = t;
      (*num_tables)++;
    }
  }

  return tables;
}