Exemplo n.º 1
0
void
group_entry_execute(struct group_entry *entry,
                          struct packet *packet) {

    VLOG_DBG_RL(LOG_MODULE, &rl, "Executing group %u.", entry->stats->group_id);

    /* NOTE: Packet is copied for all buckets now (even if there is only one).
     * This allows execution of the original packet onward. It is not clear
     * whether that is allowed or not according to the spec. though. */

    switch (entry->desc->type) {
        case (OFPGT_ALL): {
            execute_all(entry, packet);
            break;
        }
        case (OFPGT_SELECT): {
            execute_select(entry, packet);
            break;
        }
        case (OFPGT_INDIRECT): {
            execute_indirect(entry, packet);
            break;
        }
        case (OFPGT_FF): {
            execute_ff(entry, packet);
            break;
        }
        default: {
            VLOG_WARN_RL(LOG_MODULE, &rl, "Trying to execute unknown group type (%u) in group (%u).", entry->desc->type, entry->stats->group_id);
        }
    }
}
/**
 * Get a random item for replication.  Returns a single, not expired,
 * random item from those with the highest replication counters.  The
 * item's replication counter is decremented by one IF it was positive
 * before.  Call 'proc' with all values ZERO or NULL if the datastore
 * is empty.
 *
 * @param cls closure
 * @param proc function to call the value (once only).
 * @param proc_cls closure for proc
 */
static void
mysql_plugin_get_replication (void *cls, PluginDatumProcessor proc,
                              void *proc_cls)
{
  struct Plugin *plugin = cls;
  struct ReplCtx rc;
  unsigned long long rvalue;
  unsigned long repl;
  MYSQL_BIND results;

  rc.plugin = plugin;
  rc.proc = proc;
  rc.proc_cls = proc_cls;
  memset (&results, 0, sizeof (results));
  results.buffer_type = MYSQL_TYPE_LONG;
  results.buffer = &repl;
  results.is_unsigned = GNUNET_YES;

  if (1 !=
      GNUNET_MYSQL_statement_run_prepared_select (plugin->mc, plugin->max_repl, 1, &results, NULL, NULL, -1))
  {
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }

  rvalue =
      (unsigned long long) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
                                                     UINT64_MAX);
  execute_select (plugin, plugin->select_replication, &repl_proc, &rc,
                  MYSQL_TYPE_LONG, &repl, GNUNET_YES, MYSQL_TYPE_LONGLONG,
                  &rvalue, GNUNET_YES, MYSQL_TYPE_LONG, &repl, GNUNET_YES,
                  MYSQL_TYPE_LONGLONG, &rvalue, GNUNET_YES, -1);

}
/**
 * Get a zero-anonymity datum from the datastore.
 *
 * @param cls our "struct Plugin*"
 * @param offset offset of the result
 * @param type entries of which type should be considered?
 *        Use 0 for any type.
 * @param proc function to call on a matching value or NULL
 * @param proc_cls closure for iter
 */
static void
mysql_plugin_get_zero_anonymity (void *cls, uint64_t offset,
                                 enum GNUNET_BLOCK_Type type,
                                 PluginDatumProcessor proc, void *proc_cls)
{
  struct Plugin *plugin = cls;
  unsigned long long rvalue =
      (unsigned long long) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
                                                     UINT64_MAX);

  execute_select (plugin, plugin->zero_iter, proc, proc_cls, MYSQL_TYPE_LONG,
                  &type, GNUNET_YES, MYSQL_TYPE_LONGLONG, &rvalue, GNUNET_YES,
                  MYSQL_TYPE_LONG, &type, GNUNET_YES, MYSQL_TYPE_LONGLONG,
                  &rvalue, GNUNET_YES, -1);
}
/**
 * Get a random item for expiration.
 * Call 'proc' with all values ZERO or NULL if the datastore is empty.
 *
 * @param cls closure
 * @param proc function to call the value (once only).
 * @param proc_cls closure for proc
 */
static void
mysql_plugin_get_expiration (void *cls, PluginDatumProcessor proc,
                             void *proc_cls)
{
  struct Plugin *plugin = cls;
  long long nt;
  struct ExpiCtx rc;

  rc.plugin = plugin;
  rc.proc = proc;
  rc.proc_cls = proc_cls;
  nt = (long long) GNUNET_TIME_absolute_get ().abs_value_us;
  execute_select (plugin, plugin->select_expiration, expi_proc, &rc,
                  MYSQL_TYPE_LONGLONG, &nt, GNUNET_YES, -1);

}
/**
 * Wrapper for the processor for 'mysql_plugin_get_expiration'.
 * If no expired value was found, we do a second query for
 * low-priority content.
 *
 * @param cls closure
 * @param key key for the content
 * @param size number of bytes in data
 * @param data content stored
 * @param type type of the content
 * @param priority priority of the content
 * @param anonymity anonymity-level for the content
 * @param expiration expiration time for the content
 * @param uid unique identifier for the datum;
 *        maybe 0 if no unique identifier is available
 *
 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
 *         (continue on call to "next", of course),
 *         GNUNET_NO to delete the item and continue (if supported)
 */
static int
expi_proc (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
           const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
           uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
           uint64_t uid)
{
  struct ExpiCtx *rc = cls;
  struct Plugin *plugin = rc->plugin;

  if (NULL == key)
  {
    execute_select (plugin, plugin->select_priority, rc->proc, rc->proc_cls,
                    -1);
    return GNUNET_SYSERR;
  }
  return rc->proc (rc->proc_cls, key, size, data, type, priority, anonymity,
                   expiration, uid);
}
Exemplo n.º 6
0
int sqlcli( int aArg1 )
{
    SQLRETURN    rc;


    SQLHENV  env;  // Environment Handle
    SQLHDBC  dbc;  // Connection Handle
    int      conn_flag;

    env = SQL_NULL_HENV;
    dbc = SQL_NULL_HDBC;
    conn_flag = 0;

    /* allocate handle */
    rc = alloc_handle(env, dbc);
    if ( rc != SQL_SUCCESS )
    {
        free_handle(env, dbc, conn_flag);
        exit(1);
    }

    /* Connect to Altibase Server */
    rc = db_connect(dbc, conn_flag);
    if ( rc != SQL_SUCCESS )
    {
        free_handle(env, dbc, conn_flag);
        exit(1);
    }

    /* select data */
    rc = execute_select(dbc);
    if ( rc != SQL_SUCCESS )
    {
        free_handle(env, dbc, conn_flag);
        exit(1);
    }

    /* disconnect, free handles */
    free_handle(env, dbc, conn_flag);
}
Exemplo n.º 7
0
void
_gda_postgres_compute_types (GdaConnection *cnc, GdaPostgresReuseable *rdata)
{
	if (rdata->types_oid_hash)
		return;

	rdata->types_oid_hash = g_hash_table_new_full (g_int_hash, g_int_equal,
						       NULL, (GDestroyNotify) gda_postgres_type_oid_free);
	rdata->types_dbtype_hash = g_hash_table_new (g_str_hash, g_str_equal);

	GdaDataModel *model, *model_avoid, *model_anyoid = NULL;
	gint ncols, nrows, i;
	gchar *avoid_types = NULL;
	GString *string;

	if (rdata->version_float == 0)
		_gda_postgres_compute_version (cnc, rdata, NULL);
	if (rdata->version_float < 7.3) {
		gchar *query;
		avoid_types = "'SET', 'cid', 'oid', 'int2vector', 'oidvector', 'regproc', 'smgr', 'tid', 'unknown', 'xid'";
		/* main query to fetch infos about the data types */
		query = g_strdup_printf ("SELECT pg_type.oid, typname, usename, obj_description(pg_type.oid) "
					 "FROM pg_type, pg_user "
					 "WHERE typowner=usesysid AND typrelid = 0 AND typname !~ '^_' "
					 "AND  typname not in (%s) "
					 "ORDER BY typname", avoid_types);
		model = execute_select (cnc, rdata, query);
		g_free (query);

		/* query to fetch non returned data types */
		query = g_strdup_printf ("SELECT pg_type.oid FROM pg_type WHERE typname in (%s)", avoid_types);
		model_avoid = execute_select (cnc, rdata, query);
		g_free (query);
	}
	else {
		gchar *query;
		avoid_types = "'any', 'anyarray', 'anyelement', 'cid', 'cstring', 'int2vector', 'internal', 'language_handler', 'oidvector', 'opaque', 'record', 'refcursor', 'regclass', 'regoper', 'regoperator', 'regproc', 'regprocedure', 'regtype', 'SET', 'smgr', 'tid', 'trigger', 'unknown', 'void', 'xid'";

		/* main query to fetch infos about the data types */
		query = g_strdup_printf (
                          "SELECT t.oid, t.typname, u.usename, pg_catalog.obj_description(t.oid), t.typinput "
			  "FROM pg_catalog.pg_type t LEFT JOIN pg_catalog.pg_user u ON (t.typowner=u.usesysid), pg_catalog.pg_namespace n "
			  "WHERE n.oid = t.typnamespace "
			  "AND pg_catalog.pg_type_is_visible(t.oid) "
			  /*--AND (n.nspname = 'public' OR n.nspname = 'pg_catalog')*/
			  "AND typname !~ '^_' "
			  "AND (t.typrelid = 0 OR "
			  "(SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) "
			  "AND t.typname not in (%s) "
			  "ORDER BY typname", avoid_types);
		model = execute_select (cnc, rdata, query);
		g_free (query);

		/* query to fetch non returned data types */
		query = g_strdup_printf ("SELECT t.oid FROM pg_catalog.pg_type t WHERE t.typname in (%s)",
					 avoid_types);
		model_avoid = execute_select (cnc, rdata, query);
		g_free (query);

		/* query to fetch the oid of the 'any' data type */
		model_anyoid = execute_select (cnc, rdata,
					       "SELECT t.oid FROM pg_catalog.pg_type t WHERE t.typname = 'any'");
	}

	if (rdata->version_float == 0)
		_gda_postgres_compute_version (cnc, rdata, NULL);
	if (!model || !model_avoid ||
	    ((rdata->version_float >= 7.3) && !model_anyoid)) {
		if (model)
			g_object_unref (model);
		if (model_avoid)
			g_object_unref (model_avoid);
		if (model_anyoid)
			g_object_unref (model_anyoid);
		return;
	}

	/* Data types returned to the Gda client */
	nrows = gda_data_model_get_n_rows (model);
	ncols = gda_data_model_get_n_columns (model);
	if (nrows == 0)
		g_warning ("PostgreSQL provider did not find any data type (expect some mis-behaviours) please report the error to bugzilla.gnome.org");
	for (i = 0; i < nrows; i++) {
		const GValue *conv_func_name = NULL;
		const GValue *values[4];
		gint j;
		gboolean allread = TRUE;
		if (ncols >= 5)
			conv_func_name = gda_data_model_get_value_at (model, 4, i, NULL);
		for (j = 0; j < 4; j++) {
			values[j] = gda_data_model_get_value_at (model, j, i, NULL);
			if (!values [j]) {
				allread = FALSE;
				break;
			}
		}
		if (allread && (G_VALUE_TYPE (values[1]) == G_TYPE_STRING)) {
			GdaPostgresTypeOid *td;
			td = g_new0 (GdaPostgresTypeOid, 1);
			td->name = g_value_dup_string (values [1]);
			td->oid = (guint) g_ascii_strtoull (g_value_get_string (values[0]), NULL, 10);
			td->type = postgres_name_to_g_type (td->name,
							   conv_func_name ? g_value_get_string (conv_func_name) : NULL);
			if (G_VALUE_TYPE (values[3]) == G_TYPE_STRING)
				td->comments = g_value_dup_string (values [3]);
			if (G_VALUE_TYPE (values[2]) == G_TYPE_STRING)
				td->owner = g_value_dup_string (values [2]);

			g_hash_table_insert (rdata->types_oid_hash, &(td->oid), td);
			g_hash_table_insert (rdata->types_dbtype_hash, &(td->name), td);
		}
	}

	/* Make a string of data types internal to postgres and not returned, for future queries */
        string = NULL;
        nrows = gda_data_model_get_n_rows (model_avoid);
        for (i = 0; i < nrows; i++) {
		const GValue *cvalue;

		cvalue = gda_data_model_get_value_at (model_avoid, 0, i, NULL);
		if (cvalue && (G_VALUE_TYPE (cvalue) == G_TYPE_STRING)) {
			if (!string)
				string = g_string_new (g_value_get_string (cvalue));
			else {
				g_string_append (string, ", ");
				g_string_append (string, g_value_get_string (cvalue));
			}
		}
        }
        rdata->avoid_types = avoid_types;
	if (string)
		rdata->avoid_types_oids = g_string_free (string, FALSE);

	g_object_unref (model);
	g_object_unref (model_avoid);

        /* make a string of the oid of type 'any' */
        rdata->any_type_oid = NULL;
        if (model_anyoid) {
                if (gda_data_model_get_n_rows (model_anyoid) == 1) {
			const GValue *cvalue;
			cvalue = gda_data_model_get_value_at (model_anyoid, 0, 0, NULL);
			if (cvalue && (G_VALUE_TYPE (cvalue) == G_TYPE_STRING))
				rdata->any_type_oid = g_value_dup_string (cvalue);
                }
		g_object_unref (model_anyoid);
        }
}
/**
 * Get one of the results for a particular key in the datastore.
 *
 * @param cls closure
 * @param offset offset of the result (modulo num-results);
 *               specific ordering does not matter for the offset
 * @param key key to match, never NULL
 * @param vhash hash of the value, maybe NULL (to
 *        match all values that have the right key).
 *        Note that for DBlocks there is no difference
 *        betwen key and vhash, but for other blocks
 *        there may be!
 * @param type entries of which type are relevant?
 *     Use 0 for any type.
 * @param proc function to call on the matching value,
 *        with NULL for if no value matches
 * @param proc_cls closure for proc
 */
static void
mysql_plugin_get_key (void *cls, uint64_t offset, const struct GNUNET_HashCode * key,
                      const struct GNUNET_HashCode * vhash,
                      enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc,
                      void *proc_cls)
{
  struct Plugin *plugin = cls;
  int ret;
  MYSQL_BIND cbind[1];
  long long total;
  unsigned long hashSize;
  unsigned long hashSize2;
  unsigned long long off;

  GNUNET_assert (key != NULL);
  GNUNET_assert (NULL != proc);
  hashSize = sizeof (struct GNUNET_HashCode);
  hashSize2 = sizeof (struct GNUNET_HashCode);
  memset (cbind, 0, sizeof (cbind));
  total = -1;
  cbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
  cbind[0].buffer = &total;
  cbind[0].is_unsigned = GNUNET_NO;
  if (type != 0)
  {
    if (vhash != NULL)
    {
      ret =
	GNUNET_MYSQL_statement_run_prepared_select (plugin->mc,
                                         plugin->
                                         count_entry_by_hash_vhash_and_type, 1,
						    cbind, NULL, NULL, MYSQL_TYPE_BLOB, key, hashSize,
                                         &hashSize, MYSQL_TYPE_BLOB, vhash,
                                         hashSize2, &hashSize2, MYSQL_TYPE_LONG,
                                         &type, GNUNET_YES, -1);
    }
    else
    {
      ret =
	GNUNET_MYSQL_statement_run_prepared_select (plugin->mc,
                                         plugin->count_entry_by_hash_and_type,
						    1, cbind, NULL, NULL, MYSQL_TYPE_BLOB, key,
                                         hashSize, &hashSize, MYSQL_TYPE_LONG,
                                         &type, GNUNET_YES, -1);
    }
  }
  else
  {
    if (vhash != NULL)
    {
      ret =
	GNUNET_MYSQL_statement_run_prepared_select (plugin->mc,
                                         plugin->count_entry_by_hash_and_vhash,
						    1, cbind, NULL, NULL, MYSQL_TYPE_BLOB, key,
                                         hashSize, &hashSize, MYSQL_TYPE_BLOB,
                                         vhash, hashSize2, &hashSize2, -1);

    }
    else
    {
      ret =
	GNUNET_MYSQL_statement_run_prepared_select (plugin->mc, plugin->count_entry_by_hash, 1,
						    cbind, NULL, NULL, MYSQL_TYPE_BLOB, key, hashSize,
                                         &hashSize, -1);
    }
  }
  if ((ret != GNUNET_OK) || (0 >= total))
  {
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }
  offset = offset % total;
  off = (unsigned long long) offset;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Obtaining %llu/%lld result for GET `%s'\n", off, total,
              GNUNET_h2s (key));
  if (type != GNUNET_BLOCK_TYPE_ANY)
  {
    if (NULL != vhash)
    {
      execute_select (plugin, plugin->select_entry_by_hash_vhash_and_type, proc,
                      proc_cls, MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
                      MYSQL_TYPE_BLOB, vhash, hashSize, &hashSize,
                      MYSQL_TYPE_LONG, &type, GNUNET_YES, MYSQL_TYPE_LONGLONG,
                      &off, GNUNET_YES, -1);
    }
    else
    {
      execute_select (plugin, plugin->select_entry_by_hash_and_type, proc,
                      proc_cls, MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
                      MYSQL_TYPE_LONG, &type, GNUNET_YES, MYSQL_TYPE_LONGLONG,
                      &off, GNUNET_YES, -1);
    }
  }
  else
  {
    if (NULL != vhash)
    {
      execute_select (plugin, plugin->select_entry_by_hash_and_vhash, proc,
                      proc_cls, MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
                      MYSQL_TYPE_BLOB, vhash, hashSize, &hashSize,
                      MYSQL_TYPE_LONGLONG, &off, GNUNET_YES, -1);
    }
    else
    {
      execute_select (plugin, plugin->select_entry_by_hash, proc, proc_cls,
                      MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
                      MYSQL_TYPE_LONGLONG, &off, GNUNET_YES, -1);
    }
  }
}