📄 mysql.c
字号:
*/static intiterateNonAnonymous (unsigned int type, GNUNET_DatastoreValueIterator iter, void *closure){ return iterateHelper (type, GNUNET_NO, GNUNET_YES, GNUNET_NO, 1, iter, closure);}/** * Iterate over the items in the datastore in ascending * order of expiration time. * * @param type entries of which type should be considered? * Use 0 for any type. * @param iter never NULL * @return the number of results, GNUNET_SYSERR if the * iter is non-NULL and aborted the iteration */static intiterateExpirationTime (unsigned int type, GNUNET_DatastoreValueIterator iter, void *closure){ return iterateHelper (type, GNUNET_YES, GNUNET_NO, GNUNET_NO, 2, iter, closure);}/** * Iterate over the items in the datastore in migration * order. * * @param iter never NULL * @return the number of results, GNUNET_SYSERR if the * iter is non-NULL and aborted the iteration */static intiterateMigrationOrder (GNUNET_DatastoreValueIterator iter, void *closure){ return iterateHelper (0, GNUNET_NO, GNUNET_NO, GNUNET_YES, 3, iter, closure);}/** * Iterate over the items in the datastore as * quickly as possible (in any order). * * @param iter never NULL * @return the number of results, GNUNET_SYSERR if the * iter is non-NULL and aborted the iteration */static intiterateAllNow (GNUNET_DatastoreValueIterator iter, void *closure){ return iterateHelper (0, GNUNET_YES, GNUNET_YES, GNUNET_NO, 0, iter, closure);}/** * Iterate over the results for a particular key * in the datastore. If there are n results, the * code will start the iteration at result offset * "off=rand()%n" in order to ensure diversity of * the responses if iterators process only a subset. * * @param key maybe NULL (to match all entries) * @param vhash hash of the value; maybe NULL * @param type entries of which type are relevant? * Use 0 for any type. * @param iter maybe NULL (to just count); iter * should return GNUNET_SYSERR to abort the * iteration, GNUNET_NO to delete the entry and * continue and GNUNET_OK to continue iterating * @return the number of results, GNUNET_SYSERR if the * iter is non-NULL and aborted the iteration */static intget (const GNUNET_HashCode * query, const GNUNET_HashCode * vhash, unsigned int type, GNUNET_DatastoreValueIterator iter, void *closure){ int count; unsigned long long total; int off; int ret; MYSQL_STMT *stmt; unsigned int size; unsigned int rtype; unsigned int prio; unsigned int level; unsigned int limit_off; unsigned long long expiration; unsigned long long vkey; unsigned long long last_vkey; GNUNET_DatastoreValue *datum; GNUNET_HashCode key; unsigned long hashSize; unsigned long hashSize2; MYSQL_BIND qbind[5]; MYSQL_BIND rbind[7];#if DEBUG_MYSQL GNUNET_EncName enc;#endif int sqoff; if (query == NULL) return iterateLowPriority (type, iter, closure);#if DEBUG_MYSQL IF_GELOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER, GNUNET_hash_to_enc (query, &enc)); GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER, "MySQL looks for `%s' of type %u\n", &enc, type);#endif hashSize = sizeof (GNUNET_HashCode); hashSize2 = sizeof (GNUNET_HashCode); memset (qbind, 0, sizeof (qbind)); qbind[0].buffer_type = MYSQL_TYPE_BLOB; qbind[0].buffer = (void *) query; qbind[0].length = &hashSize; qbind[0].buffer_length = hashSize; sqoff = 1; if (vhash != NULL) { qbind[sqoff].buffer_type = MYSQL_TYPE_BLOB; qbind[sqoff].buffer = (void *) vhash; qbind[sqoff].length = &hashSize2; qbind[sqoff].buffer_length = hashSize2; sqoff++; } if (type != 0) { qbind[sqoff].buffer_type = MYSQL_TYPE_LONG; qbind[sqoff].is_unsigned = GNUNET_YES; qbind[sqoff].buffer = &type; sqoff++; } memset (rbind, 0, sizeof (rbind)); rbind[0].buffer_type = MYSQL_TYPE_LONGLONG; rbind[0].buffer = &total; rbind[0].is_unsigned = GNUNET_YES; /* first, determine total number of results */ mysql_thread_init (); GNUNET_mutex_lock (lock); if (GNUNET_OK != CHECK_DBH) { mysql_thread_end (); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } if (type != 0) stmt = (vhash != NULL) ? dbh->count_entry_by_hash_vhash_and_type : dbh-> count_entry_by_hash_and_type; else stmt = (vhash != NULL) ? dbh->count_entry_by_hash_and_vhash : dbh->count_entry_by_hash; GNUNET_GE_ASSERT (ectx, mysql_stmt_param_count (stmt) <= 3); GNUNET_GE_ASSERT (ectx, mysql_stmt_field_count (stmt) == 1); if (mysql_stmt_bind_param (stmt, qbind)) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("`%s' failed at %s:%d with error: %s\n"), "mysql_stmt_bind_param", __FILE__, __LINE__, mysql_stmt_error (stmt)); iclose (); mysql_thread_end (); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } if (mysql_stmt_execute (stmt)) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("`%s' failed at %s:%d with error: %s\n"), "mysql_stmt_execute", __FILE__, __LINE__, mysql_stmt_error (stmt)); iclose (); GNUNET_mutex_unlock (lock); mysql_thread_end (); return GNUNET_SYSERR; } if (mysql_stmt_bind_result (stmt, rbind)) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("`%s' failed at %s:%d with error: %s\n"), "mysql_stmt_bind_result", __FILE__, __LINE__, mysql_stmt_error (stmt)); iclose (); mysql_thread_end (); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } if (0 != mysql_stmt_fetch (stmt)) { mysql_stmt_reset (stmt); GNUNET_mutex_unlock (lock); mysql_thread_end (); return GNUNET_SYSERR; } if (-1 == total) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("`%s' failed at %s:%d with error: %s\n"), "mysql_stmt_num_rows", __FILE__, __LINE__, mysql_stmt_error (stmt)); iclose (); GNUNET_mutex_unlock (lock); mysql_thread_end (); return GNUNET_SYSERR; } mysql_stmt_reset (stmt); if ((iter == NULL) || (total == 0)) { GNUNET_mutex_unlock (lock); mysql_thread_end (); return (int) total; } GNUNET_mutex_unlock (lock); last_vkey = 0; count = 0; off = GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, total); memset (qbind, 0, sizeof (qbind)); qbind[0].buffer_type = MYSQL_TYPE_BLOB; qbind[0].buffer = (void *) query; qbind[0].length = &hashSize; qbind[0].buffer_length = hashSize; sqoff = 1; if (vhash != NULL) { qbind[sqoff].buffer_type = MYSQL_TYPE_BLOB; qbind[sqoff].buffer = (void *) vhash; qbind[sqoff].length = &hashSize2; qbind[sqoff].buffer_length = hashSize2; sqoff++; } qbind[sqoff].buffer_type = MYSQL_TYPE_LONGLONG; qbind[sqoff].is_unsigned = GNUNET_YES; qbind[sqoff].buffer = &last_vkey; sqoff++; if (type != 0) { qbind[sqoff].buffer_type = MYSQL_TYPE_LONG; qbind[sqoff].is_unsigned = GNUNET_YES; qbind[sqoff].buffer = &type; sqoff++; } qbind[sqoff].buffer_type = MYSQL_TYPE_LONG; qbind[sqoff].is_unsigned = GNUNET_YES; qbind[sqoff].buffer = &limit_off; sqoff++; memset (rbind, 0, sizeof (rbind)); rbind[0].buffer_type = MYSQL_TYPE_LONG; rbind[0].buffer = &size; rbind[0].is_unsigned = GNUNET_YES; rbind[1].buffer_type = MYSQL_TYPE_LONG; rbind[1].buffer = &rtype; rbind[1].is_unsigned = GNUNET_YES; rbind[2].buffer_type = MYSQL_TYPE_LONG; rbind[2].buffer = &prio; rbind[2].is_unsigned = GNUNET_YES; rbind[3].buffer_type = MYSQL_TYPE_LONG; rbind[3].buffer = &level; rbind[3].is_unsigned = GNUNET_YES; rbind[4].buffer_type = MYSQL_TYPE_LONGLONG; rbind[4].buffer = &expiration; rbind[4].is_unsigned = GNUNET_YES; rbind[5].buffer_type = MYSQL_TYPE_BLOB; rbind[5].buffer = &key; rbind[5].buffer_length = hashSize; rbind[5].length = &hashSize; rbind[6].buffer_type = MYSQL_TYPE_LONGLONG; rbind[6].buffer = &vkey; rbind[6].is_unsigned = GNUNET_YES; while (1) { GNUNET_mutex_lock (lock); if (GNUNET_OK != CHECK_DBH) { GNUNET_mutex_unlock (lock); mysql_thread_end (); return GNUNET_SYSERR; } if (type != 0) stmt = (vhash != NULL) ? dbh->select_entry_by_hash_vhash_and_type : dbh->select_entry_by_hash_and_type; else stmt = (vhash != NULL) ? dbh->select_entry_by_hash_and_vhash : dbh-> select_entry_by_hash; if (count == 0) limit_off = off; else limit_off = 0; GNUNET_GE_ASSERT (ectx, mysql_stmt_param_count (stmt) <= 5); GNUNET_GE_ASSERT (ectx, mysql_stmt_field_count (stmt) == 7); if (mysql_stmt_bind_param (stmt, qbind)) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("`%s' failed at %s:%d with error: %s\n"), "mysql_stmt_bind_param", __FILE__, __LINE__, mysql_stmt_error (stmt)); iclose (); mysql_thread_end (); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } if (mysql_stmt_execute (stmt)) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("`%s' failed at %s:%d with error: %s\n"), "mysql_stmt_execute", __FILE__, __LINE__, mysql_stmt_error (stmt)); iclose (); GNUNET_mutex_unlock (lock); mysql_thread_end (); return GNUNET_SYSERR; } if (mysql_stmt_bind_result (stmt, rbind)) { GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_BULK | GNUNET_GE_USER, _("`%s' failed at %s:%d with error: %s\n"), "mysql_stmt_bind_result", __FILE__, __LINE__, mysql_stmt_error (stmt)); iclose (); mysql_thread_end (); GNUNET_mutex_unlock (lock); return GNUNET_SYSERR; } if (0 != mysql_stmt_fetch (stmt)) { mysql_stmt_reset (stmt); GNUNET_mutex_unlock (lock); break; } last_vkey = vkey; mysql_stmt_reset (stmt); GNUNET_mutex_unlock (lock); datum = assembleDatum (rbind); if (datum == NULL) continue; count++; ret = iter (&key, datum, closure, vkey); if (ret == GNUNET_SYSERR) { GNUNET_free (datum); break; } if (ret == GNUNET_NO) { GNUNET_mutex_lock (lock); delete_value (vkey); delete_entry_by_vkey (vkey); content_size -= ntohl (datum->size); GNUNET_mutex_unlock (lock); } GNUNET_free (datum); if (count + off == total) last_vkey = 0; /* back to start */ if (count == total) break; } mysql_thread_end ();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -