Exemple #1
0
static void handle_mget(tcp_client* c, struct evbuffer* buffer) {
	int i, n, ksize;
	char k[MAX_TRANSACTION_SIZE];
	val* values[64];
	struct evbuffer* b = evbuffer_copy(buffer);

	for (i = 0; i < 64; i++) values[i] = NULL;
	transaction_set_get_cb(c->t, on_mget, c);
	
	evbuffer_remove(b, &n, sizeof(int));
	for (i = 0; i < n; i++) {
		evbuffer_remove(b, &ksize, sizeof(int));
		evbuffer_remove(b, k, ksize);
		values[i] = execute_get(c->t, k, ksize);
		if (values[i] == NULL)
			goto cleanup;
	}

	//	assert(transaction_read_only(c->t));
	int rv = transaction_remote_count(c->t);
	send_mget_result(c, rv, n, values);
	//transaction_clear(c->t);
	evbuffer_drain(buffer, evbuffer_get_length(buffer));

cleanup:
	evbuffer_free(b);
	for (i = 0; i < 64; i++)
		if (values[i] != NULL)
			val_free(values[i]);
}
Exemple #2
0
static void handle_mget_put(tcp_client* c, struct evbuffer* buffer) {
	int rv, i, n;
	int ksize, vsize;
	char k[MAX_TRANSACTION_SIZE];
	char v[MAX_TRANSACTION_SIZE];
	val* tmp;
	struct evbuffer* b = evbuffer_copy(buffer);
	transaction_set_get_cb(c->t, on_mget_put, c);

	evbuffer_remove(b, &n, sizeof(int));
	for (i = 0; i < n; i++) {
		evbuffer_remove(b, &ksize, sizeof(int));
		evbuffer_remove(b, k, ksize);
		evbuffer_remove(b, &vsize, sizeof(int));
		evbuffer_remove(b, v, vsize);
		tmp = execute_get(c->t, k, ksize);
		if (tmp == NULL) {
			evbuffer_free(b);
			return;
		}
		val_free(tmp);
		execute_put(c->t, k, ksize, v, vsize);
	}
	
	evbuffer_free(b);
	evbuffer_drain(buffer, evbuffer_get_length(buffer));
	
	rv = transaction_commit(c->t, c->id, on_commit);
	if (rv < 0) {
		transaction_clear(c->t);
		send_result(c->buffer_ev, -1);
		return;
	}
	evtimer_add(&c->timeout_ev, &commit_timeout);
}
/**
 * Get a random item that has expired or has low priority.
 * 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
sqlite_plugin_get_expiration (void *cls, PluginDatumProcessor proc,
                              void *proc_cls)
{
  struct Plugin *plugin = cls;
  sqlite3_stmt *stmt;
  struct GNUNET_TIME_Absolute now;

  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite",
                   "Getting random block based on expiration and priority order.\n");
  now = GNUNET_TIME_absolute_get ();
  stmt = plugin->selExpi;
  if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, now.abs_value))
  {
    LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                "sqlite3_bind_XXXX");
    if (SQLITE_OK != sqlite3_reset (stmt))
      LOG_SQLITE (plugin, NULL,
                  GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                  "sqlite3_reset");
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }
  execute_get (plugin, stmt, proc, proc_cls);
}
Exemple #4
0
static void handle_get(tcp_client* c, struct evbuffer* buffer) {
	int ksize;
	char k[MAX_TRANSACTION_SIZE];
	val* v;
	struct evbuffer* b = evbuffer_copy(buffer);
	evbuffer_remove(b, &ksize, sizeof(int));
	evbuffer_remove(b, k, ksize);
	transaction_set_get_cb(c->t, on_get, c);
	v = execute_get(c->t, k, ksize);
	if (v != NULL) {
		evbuffer_drain(buffer, evbuffer_get_length(buffer));
		send_get_result(c, v);
		val_free(v);
	}
	evbuffer_free(b);
}
Exemple #5
0
void *run_task(void *p)
{
  thread_context_st *context= (thread_context_st *)p;
  memcached_st *memc;

  memc= context->memc;

  pthread_mutex_lock(&sleeper_mutex);
  while (master_wakeup)
  {
    pthread_cond_wait(&sleep_threshhold, &sleeper_mutex);
  } 
  pthread_mutex_unlock(&sleeper_mutex);

  /* Do Stuff */
  switch (context->test)
  {
  case SET_TEST:
    execute_set(memc, context->execute_pairs, context->execute_number);
    break;
  case GET_TEST:
    execute_get(memc, context->initial_pairs, context->initial_number);
    break;
  }

  memcached_free(memc);

  if (context->execute_pairs)
    pairs_free(context->execute_pairs);

  free(context);

  pthread_mutex_lock(&counter_mutex);
  thread_counter--;
  pthread_cond_signal(&count_threshhold);
  pthread_mutex_unlock(&counter_mutex);

  return NULL;
}
int32_t execute_command( dfu_device_t *device,
                         struct programmer_arguments *args )
{
    device->type = args->device_type;
    switch( args->command ) {
        case com_erase:
            return execute_erase( device, args );
        case com_flash:
            return execute_flash_normal( device, args );
        case com_eflash:
            return execute_flash_eeprom( device, args );
        case com_user:
            return execute_flash_user_page( device, args );
        case com_reset:
            return atmel_reset( device ); 
        case com_start_app:
            return atmel_start_app( device );
        case com_get:
            return execute_get( device, args );
        case com_getfuse:
            return execute_getfuse( device, args );
        case com_dump:
            return execute_dump_normal( device, args );
        case com_edump:
            return execute_dump_eeprom( device, args );
        case com_udump:
            return execute_dump_user_page( device, args );
        case com_configure:
            return execute_configure( device, args );
        case com_setfuse:
            return execute_setfuse( device, args );
        default:
            fprintf( stderr, "Not supported at this time.\n" );
    }

    return -1;
}
/**
 * Select a subset of the items in the datastore and call
 * the given processor for the item.
 *
 * @param cls our plugin context
 * @param offset offset of the result (modulo num-results);
 *               specific ordering does not matter for the offset
 * @param type entries of which type should be considered?
 *        Use 0 for any type.
 * @param proc function to call on each matching value;
 *        will be called once with a NULL value at the end
 * @param proc_cls closure for proc
 */
static void
sqlite_plugin_get_zero_anonymity (void *cls, uint64_t offset,
                                  enum GNUNET_BLOCK_Type type,
                                  PluginDatumProcessor proc, void *proc_cls)
{
  struct Plugin *plugin = cls;
  sqlite3_stmt *stmt;

  GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
  stmt = plugin->selZeroAnon;
  if ((SQLITE_OK != sqlite3_bind_int (stmt, 1, type)) ||
      (SQLITE_OK != sqlite3_bind_int64 (stmt, 2, offset)))
  {
    LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                "sqlite3_bind_XXXX");
    if (SQLITE_OK != sqlite3_reset (stmt))
      LOG_SQLITE (plugin, NULL,
                  GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                  "sqlite3_reset");
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }
  execute_get (plugin, stmt, proc, proc_cls);
}
/**
 * Get a random item for replication.  Returns a single 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
sqlite_plugin_get_replication (void *cls, PluginDatumProcessor proc,
                               void *proc_cls)
{
  struct Plugin *plugin = cls;
  struct ReplCtx rc;
  uint64_t rvalue;
  uint32_t repl;
  sqlite3_stmt *stmt;

  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite",
                   "Getting random block based on replication order.\n");
  rc.have_uid = GNUNET_NO;
  rc.proc = proc;
  rc.proc_cls = proc_cls;
  stmt = plugin->maxRepl;
  if (SQLITE_ROW != sqlite3_step (stmt))
  {
    if (SQLITE_OK != sqlite3_reset (stmt))
      LOG_SQLITE (plugin, NULL,
                  GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                  "sqlite3_reset");
    /* DB empty */
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }
  repl = sqlite3_column_int (stmt, 0);
  if (SQLITE_OK != sqlite3_reset (stmt))
    LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                "sqlite3_reset");
  stmt = plugin->selRepl;
  rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
  if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, rvalue))
  {
    LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                "sqlite3_bind_XXXX");
    if (SQLITE_OK != sqlite3_reset (stmt))
      LOG_SQLITE (plugin, NULL,
                  GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                  "sqlite3_reset");
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }
  if (SQLITE_OK != sqlite3_bind_int (stmt, 2, repl))
  {
    LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                "sqlite3_bind_XXXX");
    if (SQLITE_OK != sqlite3_reset (stmt))
      LOG_SQLITE (plugin, NULL,
                  GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                  "sqlite3_reset");
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }
  execute_get (plugin, stmt, &repl_proc, &rc);
  if (GNUNET_YES == rc.have_uid)
  {
    if (SQLITE_OK != sqlite3_bind_int64 (plugin->updRepl, 1, rc.uid))
    {
      LOG_SQLITE (plugin, NULL,
                  GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                  "sqlite3_bind_XXXX");
      if (SQLITE_OK != sqlite3_reset (plugin->updRepl))
        LOG_SQLITE (plugin, NULL,
                    GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                    "sqlite3_reset");
      return;
    }
    if (SQLITE_DONE != sqlite3_step (plugin->updRepl))
      LOG_SQLITE (plugin, NULL,
                  GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                  "sqlite3_step");
    if (SQLITE_OK != sqlite3_reset (plugin->updRepl))
      LOG_SQLITE (plugin, NULL,
                  GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                  "sqlite3_reset");
  }
}
/**
 * Get results for a particular key in the datastore.
 *
 * @param cls closure
 * @param offset offset (mod count).
 * @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 each matching value;
 *        will be called once with a NULL value at the end
 * @param proc_cls closure for proc
 */
static void
sqlite_plugin_get_key (void *cls, uint64_t offset, const GNUNET_HashCode * key,
                       const GNUNET_HashCode * vhash,
                       enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc,
                       void *proc_cls)
{
  struct Plugin *plugin = cls;
  int ret;
  int total;
  int limit_off;
  unsigned int sqoff;
  sqlite3_stmt *stmt;
  char scratch[256];

  GNUNET_assert (proc != NULL);
  GNUNET_assert (key != NULL);
  GNUNET_snprintf (scratch, sizeof (scratch),
                   "SELECT count(*) FROM gn090 WHERE hash=?%s%s",
                   vhash == NULL ? "" : " AND vhash=?",
                   type == 0 ? "" : " AND type=?");
  if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
  {
    LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                "sqlite_prepare");
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }
  sqoff = 1;
  ret =
      sqlite3_bind_blob (stmt, sqoff++, key, sizeof (GNUNET_HashCode),
                         SQLITE_TRANSIENT);
  if ((vhash != NULL) && (ret == SQLITE_OK))
    ret =
        sqlite3_bind_blob (stmt, sqoff++, vhash, sizeof (GNUNET_HashCode),
                           SQLITE_TRANSIENT);
  if ((type != 0) && (ret == SQLITE_OK))
    ret = sqlite3_bind_int (stmt, sqoff++, type);
  if (SQLITE_OK != ret)
  {
    LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_bind");
    sqlite3_finalize (stmt);
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }
  ret = sqlite3_step (stmt);
  if (ret != SQLITE_ROW)
  {
    LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                "sqlite_step");
    sqlite3_finalize (stmt);
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }
  total = sqlite3_column_int (stmt, 0);
  sqlite3_finalize (stmt);
  if (0 == total)
  {
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }
  limit_off = (int) (offset % total);
  if (limit_off < 0)
    limit_off += total;
  GNUNET_snprintf (scratch, sizeof (scratch),
                   "SELECT type, prio, anonLevel, expire, hash, value, _ROWID_ "
                   "FROM gn090 WHERE hash=?%s%s "
                   "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET ?",
                   vhash == NULL ? "" : " AND vhash=?",
                   type == 0 ? "" : " AND type=?");
  if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK)
  {
    LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                "sqlite_prepare");
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }
  sqoff = 1;
  ret =
      sqlite3_bind_blob (stmt, sqoff++, key, sizeof (GNUNET_HashCode),
                         SQLITE_TRANSIENT);
  if ((vhash != NULL) && (ret == SQLITE_OK))
    ret =
        sqlite3_bind_blob (stmt, sqoff++, vhash, sizeof (GNUNET_HashCode),
                           SQLITE_TRANSIENT);
  if ((type != 0) && (ret == SQLITE_OK))
    ret = sqlite3_bind_int (stmt, sqoff++, type);
  if (ret == SQLITE_OK)
    ret = sqlite3_bind_int64 (stmt, sqoff++, limit_off);
  if (ret != SQLITE_OK)
  {
    LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
                "sqlite_bind");
    proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
    return;
  }
  execute_get (plugin, stmt, proc, proc_cls);
  sqlite3_finalize (stmt);
}