[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r239 - in GNUnet/src/applications: fs/ecrs sqstore_mysql
From: |
grothoff |
Subject: |
[GNUnet-SVN] r239 - in GNUnet/src/applications: fs/ecrs sqstore_mysql |
Date: |
Fri, 11 Feb 2005 08:38:27 -0800 (PST) |
Author: grothoff
Date: 2005-02-11 08:38:27 -0800 (Fri, 11 Feb 2005)
New Revision: 239
Modified:
GNUnet/src/applications/fs/ecrs/download.c
GNUnet/src/applications/sqstore_mysql/mysql.c
GNUnet/src/applications/sqstore_mysql/mysqltest.c
Log:
committing incomplete, broken mysql updates since I'm moving to a different
machine
Modified: GNUnet/src/applications/fs/ecrs/download.c
===================================================================
--- GNUnet/src/applications/fs/ecrs/download.c 2005-02-11 03:08:53 UTC (rev
238)
+++ GNUnet/src/applications/fs/ecrs/download.c 2005-02-11 16:38:27 UTC (rev
239)
@@ -1072,8 +1072,8 @@
FREE(perm);
if (minSleep < cronMILLIS * 100)
minSleep = cronMILLIS * 100; /* maximum resolution: 100ms */
-
MUTEX_UNLOCK(&rm->lock);
+ gnunet_util_sleep(minSleep);
}
@@ -1125,7 +1125,8 @@
top.level = computeDepth(ctx.total);
addRequest(rm, &top);
while ( (OK == tt(ttClosure)) &&
- (rm->abortFlag == NO) )
+ (rm->abortFlag == NO) &&
+ (rm->requestListIndex != 0) )
processRequests(rm);
if ( (rm->requestListIndex == 0) &&
Modified: GNUnet/src/applications/sqstore_mysql/mysql.c
===================================================================
--- GNUnet/src/applications/sqstore_mysql/mysql.c 2005-02-11 03:08:53 UTC
(rev 238)
+++ GNUnet/src/applications/sqstore_mysql/mysql.c 2005-02-11 16:38:27 UTC
(rev 239)
@@ -172,8 +172,24 @@
* in SHOW TABLE STATUS resultset */
int useDelayed; /* use potentially unsafe delayed inserts? */
char * cnffile;
+ MYSQL_STMT * insert;
+ MYSQL_BIND bind[7];
+ MYSQL_STMT * select;
+ MYSQL_STMT * selectc;
+ MYSQL_STMT * selects;
+ MYSQL_STMT * selectsc;
+ MYSQL_BIND sbind[2];
+
} mysqlHandle;
+#define INSERT_SAMPLE "INSERT INTO gn070
(size,type,prio,anonLevel,expire,hash,value) VALUES (?,?,?,?,?,?,?)"
+#define INSERT_SAMPLE_DELAYED "INSERT DELAYED INTO gn070
(size,type,prio,anonLevel,expire,hash,value) VALUES (?,?,?,?,?,?,?)"
+
+#define SELECT_SAMPLE "SELECT * FROM gn070 WHERE hash=?"
+#define SELECT_SAMPLE_COUNT "SELECT count(*) FROM gn070 WHERE hash=?"
+#define SELECT_TYPE_SAMPLE "SELECT * FROM gn070 WHERE hash=? AND type=?"
+#define SELECT_TYPE_SAMPLE_COUNT "SELECT count(*) FROM gn070 WHERE hash=? AND
type=?"
+
static mysqlHandle * dbh;
/**
@@ -229,6 +245,8 @@
* @return OK on success
*/
static int iopen(mysqlHandle * dbhI) {
+ char * scratch;
+
if (dbhI->cnffile == NULL)
return SYSERR;
dbhI->dbf = mysql_init(NULL);
@@ -254,7 +272,107 @@
dbhI);
dbhI->dbf = NULL;
return SYSERR;
- }
+ }
+
+ scratch = MALLOC(1024);
+ SNPRINTF(scratch,
+ 1024,
+ "CREATE TABLE IF NOT EXISTS gn070 ("
+ " size INT(11) UNSIGNED NOT NULL DEFAULT 0,"
+ " type INT(11) UNSIGNED NOT NULL DEFAULT 0,"
+ " prio INT(11) UNSIGNED NOT NULL DEFAULT 0,"
+ " anonLevel INT(11) UNSIGNED NOT NULL DEFAULT 0,"
+ " expire BIGINT UNSIGNED NOT NULL DEFAULT 0,"
+ " hash TINYBLOB BINARY NOT NULL DEFAULT '',"
+ " value MEDIUMBLOB NOT NULL DEFAULT '',"
+ " INDEX (hash(20)),"
+ " INDEX (prio),"
+ " INDEX (expire)"
+ ") TYPE=MyISAM");
+ mysql_query(dbhI->dbf,
+ scratch);
+ if (mysql_error(dbhI->dbf)[0]) {
+ LOG_MYSQL(LOG_ERROR,
+ "mysql_query",
+ dbhI);
+ mysql_close(dbhI->dbf);
+ dbhI->dbf = NULL;
+ FREE(scratch);
+ return SYSERR;
+ }
+ FREE(scratch);
+
+
+ dbhI->insert = mysql_stmt_init(dbhI->dbf);
+ dbhI->select = mysql_stmt_init(dbhI->dbf);
+ dbhI->selectc = mysql_stmt_init(dbhI->dbf);
+ dbhI->selects = mysql_stmt_init(dbhI->dbf);
+ dbhI->selectsc = mysql_stmt_init(dbhI->dbf);
+ if ( (dbhI->insert == NULL) ||
+ (dbhI->select == NULL) ||
+ (dbhI->selectc == NULL) ||
+ (dbhI->selects == NULL) ||
+ (dbhI->selectsc == NULL) ) {
+ BREAK();
+ if (dbhI->insert != NULL)
+ mysql_stmt_close(dbhI->insert);
+ if (dbhI->select != NULL)
+ mysql_stmt_close(dbhI->select);
+ if (dbhI->selectc != NULL)
+ mysql_stmt_close(dbhI->selectc);
+ if (dbhI->selects != NULL)
+ mysql_stmt_close(dbhI->selects);
+ if (dbhI->selectsc != NULL)
+ mysql_stmt_close(dbhI->selectsc);
+ mysql_close(dbhI->dbf);
+ dbhI->dbf = NULL;
+ return SYSERR;
+ }
+ if (mysql_stmt_prepare(dbhI->insert,
+ dbh->useDelayed
+ ? INSERT_SAMPLE_DELAYED
+ : INSERT_SAMPLE,
+ strlen(dbh->useDelayed
+ ? INSERT_SAMPLE_DELAYED
+ : INSERT_SAMPLE)) ||
+ mysql_stmt_prepare(dbhI->select,
+ SELECT_SAMPLE,
+ strlen(SELECT_SAMPLE)) ||
+ mysql_stmt_prepare(dbhI->selectc,
+ SELECT_SAMPLE_COUNT,
+ strlen(SELECT_SAMPLE_COUNT)) ||
+ mysql_stmt_prepare(dbhI->selects,
+ SELECT_TYPE_SAMPLE,
+ strlen(SELECT_TYPE_SAMPLE)) ||
+ mysql_stmt_prepare(dbhI->selectsc,
+ SELECT_TYPE_SAMPLE_COUNT,
+ strlen(SELECT_TYPE_SAMPLE_COUNT)) ) {
+ LOG(LOG_ERROR,
+ _("'%s' failed at %s:%d with error: %s\n"),
+ "mysql_stmt_prepare",
+ __FILE__, __LINE__,
+ mysql_stmt_error(dbhI->insert));
+ mysql_stmt_close(dbhI->insert);
+ mysql_close(dbhI->dbf);
+ dbhI->dbf = NULL;
+ return SYSERR;
+ }
+ memset(dbhI->bind,
+ 0,
+ sizeof(dbhI->bind));
+ dbhI->bind[0].buffer_type = MYSQL_TYPE_LONG; /* size */
+ dbhI->bind[1].buffer_type = MYSQL_TYPE_LONG; /* type */
+ dbhI->bind[2].buffer_type = MYSQL_TYPE_LONG; /* prio */
+ dbhI->bind[3].buffer_type = MYSQL_TYPE_LONG; /* anon level */
+ dbhI->bind[4].buffer_type = MYSQL_TYPE_LONGLONG; /* expiration */
+ dbhI->bind[5].buffer_type = MYSQL_TYPE_TINY_BLOB; /* hash */
+ dbhI->bind[6].buffer_type = MYSQL_TYPE_MEDIUM_BLOB; /* value */
+ memset(dbhI->sbind,
+ 0,
+ sizeof(dbhI->sbind));
+ dbhI->sbind[0].buffer_type = MYSQL_TYPE_TINY_BLOB; /* hash */
+ dbhI->sbind[1].buffer_type = MYSQL_TYPE_LONG; /* type */
+
MUTEX_CREATE(&dbhI->DATABASE_Lock_);
return OK;
}
@@ -265,6 +383,16 @@
static int iclose(mysqlHandle * dbhI) {
if (dbhI->dbf == NULL)
return SYSERR;
+ mysql_stmt_free_result(dbhI->insert);
+ mysql_stmt_free_result(dbhI->select);
+ mysql_stmt_free_result(dbhI->selectc);
+ mysql_stmt_free_result(dbhI->selects);
+ mysql_stmt_free_result(dbhI->selectsc);
+ mysql_stmt_close(dbhI->insert);
+ mysql_stmt_close(dbhI->select);
+ mysql_stmt_close(dbhI->selectc);
+ mysql_stmt_close(dbhI->selects);
+ mysql_stmt_close(dbhI->selectsc);
MUTEX_DESTROY(&dbhI->DATABASE_Lock_);
mysql_close(dbhI->dbf);
dbhI->dbf = NULL;
@@ -298,8 +426,8 @@
MUTEX_LOCK(&dbhI.DATABASE_Lock_);
- if(type==0) {
- typestr[0]=0;
+ if (type==0) {
+ typestr[0] = 0;
} else {
SNPRINTF(typestr,
32,
@@ -382,8 +510,8 @@
return SYSERR;
MUTEX_LOCK(&dbhI.DATABASE_Lock_);
- if(type==0) {
- typestr[0]=0;
+ if (type==0) {
+ typestr[0] = 0;
} else {
SNPRINTF(typestr,
32,
@@ -435,6 +563,8 @@
return count;
}
+#define MAX_DATUM_SIZE 65536
+
/**
* Iterate over the results for a particular key
* in the datastore.
@@ -450,109 +580,114 @@
unsigned int type,
Datum_Iterator iter,
void * closure) {
+ static unsigned long twenty = sizeof(HashCode160);
MYSQL_RES * sql_res;
- MYSQL_ROW sql_row;
- char * scratch;
int count;
+ MYSQL_STMT * stmt;
+ unsigned int size;
+ unsigned int rtype;
+ unsigned int prio;
+ unsigned int level;
+ unsigned long long expiration;
+ unsigned long datasize;
+ Datastore_Value * datum;
+ HashCode160 key;
+ if (query == NULL)
+ return iterateLowPriority(type, iter, closure);
+
MUTEX_LOCK(&dbh->DATABASE_Lock_);
+ if (type != 0) {
+ if (iter == NULL)
+ stmt = dbh->selectsc;
+ else
+ stmt = dbh->selects;
+ } else {
+ if (iter == NULL)
+ stmt = dbh->selectc;
+ else
+ stmt = dbh->select;
+ }
+ dbh->sbind[0].buffer = (char*) query;
+ dbh->sbind[1].buffer = (char*) &type;
+ dbh->sbind[0].length = &twenty;
- scratch = MALLOC(256);
- if(query!=NULL) {
- char * escapedHash;
-
- escapedHash = MALLOC(sizeof(HashCode160)*2+1);
- mysql_escape_string(escapedHash,
- (char *)query,
- sizeof(HashCode160));
- if (type!=0) {
- SNPRINTF(scratch,
- 256,
- "SELECT %s FROM gn070"
- " WHERE hash='%s' AND type=%u",
- (iter == NULL ? "count(*)" : "*"),
- escapedHash,
- type);
- } else {
- SNPRINTF(scratch,
- 256,
- "SELECT %s FROM gn070"
- " WHERE hash='%s'",
- (iter == NULL ? "count(*)" : "*"),
- escapedHash);
- }
- FREE(escapedHash);
- } else { /* query is NULL */
- if (type==0) {
- SNPRINTF(scratch,
- 256,
- "SELECT %s FROM gn070",
- (iter == NULL ? "count(*)" : "*"));
- } else {
- SNPRINTF(scratch,
- 256,
- "SELECT %s FROM gn070"
- " WHERE type = %u",
- (iter == NULL ? "count(*)" : "*"),
- type);
- }
- }
-
- mysql_query(dbh->dbf,
- scratch);
- if (mysql_error(dbh->dbf)[0]) {
- LOG_MYSQL(LOG_ERROR, "mysql_query", dbh);
- FREE(scratch);
+ if (mysql_stmt_bind_param(stmt,
+ dbh->sbind)) {
+ LOG(LOG_ERROR,
+ _("'%s' failed at %s:%d with error: %s\n"),
+ "mysql_stmt_bind_param",
+ __FILE__, __LINE__,
+ mysql_stmt_error(stmt));
MUTEX_UNLOCK(&dbh->DATABASE_Lock_);
return SYSERR;
- }
+ }
+ if (mysql_stmt_execute(dbh->insert)) {
+ LOG(LOG_ERROR,
+ _("'%s' failed at %s:%d with error: %s\n"),
+ "mysql_stmt_execute",
+ __FILE__, __LINE__,
+ mysql_stmt_error(dbh->insert));
+ MUTEX_UNLOCK(&dbh->DATABASE_Lock_);
+ return SYSERR;
+ }
- if (!(sql_res=mysql_use_result(dbh->dbf))) {
- LOG_MYSQL(LOG_ERROR, "mysql_use_result", dbh);
+ datum = MALLOC(sizeof(Datastore_Value) + MAX_DATUM_SIZE);
+ dbh->bind[0].buffer = (char*) &size;
+ dbh->bind[1].buffer = (char*) &rtype;
+ dbh->bind[2].buffer = (char*) &prio;
+ dbh->bind[3].buffer = (char*) &level;
+ dbh->bind[4].buffer = (char*) &expiration;
+ dbh->bind[5].buffer = (char*) &key;
+ dbh->bind[6].buffer = (char*) &datum[1];
+ dbh->bind[5].length = &twenty;
+ dbh->bind[6].length = &datasize;
+ sql_res = mysql_stmt_result_metadata(stmt);
+ if (mysql_stmt_bind_result(stmt,
+ dbh->bind)) {
+ LOG(LOG_ERROR,
+ _("'%s' failed at %s:%d with error: %s\n"),
+ "mysql_stmt_bind_result",
+ __FILE__, __LINE__,
+ mysql_stmt_error(dbh->insert));
MUTEX_UNLOCK(&dbh->DATABASE_Lock_);
- FREE(scratch);
+ FREE(datum);
return SYSERR;
}
-
+ datasize = MAX_DATUM_SIZE;
count = 0;
- while( (sql_row=mysql_fetch_row(sql_res))) {
- if(count == SYSERR) /* we are not allowed to break under mysql_use_result
*/
- continue;
+ while (mysql_stmt_fetch(stmt)) {
+ count++;
- if (iter!=NULL) {
- Datastore_Datum * datum;
-
- datum = assembleDatum(sql_res,
- sql_row);
- if (datum == NULL) {
+ if (iter != NULL) {
+ datum->size = htonl(size);
+ datum->type = htonl(rtype);
+ datum->prio = htonl(prio);
+ datum->anonymityLevel = htonl(level);
+ datum->expirationTime = htonll(expiration);
+ if (datasize != size - sizeof(Datastore_Value))
+ BREAK();
LOG(LOG_WARNING,
_("Invalid data in MySQL database. Please verify integrity!\n"));
continue;
}
LOG(LOG_DEBUG,
"Found in database block with type %u.\n",
- ntohl(*(int*)&((&datum->value)[1])));
+ ntohl(*(int*)&datum[1]));
- if( SYSERR == iter(&datum->key,
- &datum->value,
+ if( SYSERR == iter(&key,
+ datum,
closure) ) {
count = SYSERR;
- FREE(datum);
break;
}
- FREE(datum);
-
- count++;
- } else {
- count += atol(sql_row[0]);
- }
+ datasize = MAX_DATUM_SIZE;
}
-
mysql_free_result(sql_res);
- FREE(scratch);
+ FREE(datum);
MUTEX_UNLOCK(&dbh->DATABASE_Lock_);
- return(count);
+ return count;
}
/**
@@ -562,11 +697,15 @@
*/
static int put(const HashCode160 * key,
const Datastore_Value * value) {
- char * escapedBlock;
- char * escapedHash;
- char * scratch;
- int n;
+ static unsigned long twenty = sizeof(HashCode160);
unsigned int contentSize;
+ unsigned long currentSize;
+ unsigned int size;
+ unsigned int type;
+ unsigned int prio;
+ unsigned int level;
+ unsigned long long expiration;
+ EncName enc;
if ( (ntohl(value->size) <= sizeof(Datastore_Value)) ) {
BREAK();
@@ -575,44 +714,52 @@
MUTEX_LOCK(&dbh->DATABASE_Lock_);
contentSize = ntohl(value->size)-sizeof(Datastore_Value);
-
- escapedHash = MALLOC(2*sizeof(HashCode160)+1);
- mysql_escape_string(escapedHash,
- (char *)key,
- sizeof(HashCode160));
+
+ size = ntohl(value->size);
+ type = ntohl(value->type);
+ prio = ntohl(value->prio);
+ level = ntohl(value->anonymityLevel);
+ expiration = ntohll(value->expirationTime);
+
+ IFLOG(LOG_DEBUG,
+ hash2enc(key,
+ &enc));
LOG(LOG_DEBUG,
- "Storing in database block with type %u.\n",
- ntohl(*(int*)&value[1]));
- escapedBlock = MALLOC(2*contentSize+1);
- mysql_escape_string(escapedBlock,
- (char *)&value[1],
- contentSize);
+ "Storing in database block with type %u and key %s.\n",
+ type,
+ &enc);
+ dbh->bind[0].buffer = (char*) &size;
+ dbh->bind[1].buffer = (char*) &type;
+ dbh->bind[2].buffer = (char*) &prio;
+ dbh->bind[3].buffer = (char*) &level;
+ dbh->bind[4].buffer = (char*) &expiration;
+ dbh->bind[5].buffer = (char*) key;
+ dbh->bind[6].buffer = (char*) &value[1];
+ dbh->bind[5].length = &twenty;
+ dbh->bind[6].length = ¤tSize;
+ currentSize = contentSize;
- n = contentSize*2+sizeof(HashCode160)*2+500+1;
- scratch = MALLOC(n);
- SNPRINTF(scratch,
- n,
- "INSERT %s INTO gn070"
- " (size,type,prio,anonLevel,expire,hash,value)"
- " VALUES (%u,%u,%u,%u,%lld,'%s','%s')",
- (dbh->useDelayed == YES ? "DELAYED" : ""),
- ntohl(value->size),
- ntohl(value->type),
- ntohl(value->prio),
- ntohl(value->anonymityLevel),
- ntohll(value->expirationTime),
- escapedHash,
- escapedBlock);
- mysql_query(dbh->dbf, scratch);
- FREE(scratch);
- FREE(escapedBlock);
- FREE(escapedHash);
- if(mysql_error(dbh->dbf)[0]) {
- LOG_MYSQL(LOG_ERROR, "mysql_query", dbh);
+ if (mysql_stmt_bind_param(dbh->insert,
+ dbh->bind)) {
+ LOG(LOG_ERROR,
+ _("'%s' failed at %s:%d with error: %s\n"),
+ "mysql_stmt_bind_param",
+ __FILE__, __LINE__,
+ mysql_stmt_error(dbh->insert));
MUTEX_UNLOCK(&dbh->DATABASE_Lock_);
return SYSERR;
- }
+ }
+
+ if (mysql_stmt_execute(dbh->insert)) {
+ LOG(LOG_ERROR,
+ _("'%s' failed at %s:%d with error: %s\n"),
+ "mysql_stmt_execute",
+ __FILE__, __LINE__,
+ mysql_stmt_error(dbh->insert));
+ MUTEX_UNLOCK(&dbh->DATABASE_Lock_);
+ return SYSERR;
+ }
MUTEX_UNLOCK(&dbh->DATABASE_Lock_);
return OK;
}
@@ -894,34 +1041,7 @@
}
scratch = MALLOC(1024);
- SNPRINTF(scratch,
- 1024,
- "CREATE TABLE IF NOT EXISTS gn070 ("
- " size int(11) UNSIGNED NOT NULL default 0,"
- " type int(11) UNSIGNED NOT NULL default 0,"
- " prio int(11) UNSIGNED NOT NULL default 0,"
- " anonLevel int(11) UNSIGNED NOT NULL default 0,"
- " expire bigint UNSIGNED NOT NULL default 0,"
- " hash char(20) BINARY NOT NULL default '',"
- " value mediumblob NOT NULL default '',"
- " INDEX (hash),"
- " INDEX (prio),"
- " INDEX (expire)"
- ") TYPE=MyISAM");
- mysql_query(dbh->dbf,
- scratch);
- if (mysql_error(dbh->dbf)[0]) {
- LOG_MYSQL(LOG_ERROR,
- "mysql_query",
- dbh);
- iclose(dbh);
- FREE(dbh);
- FREE(cnffile);
- FREE(scratch);
- return NULL;
- }
-
/* Find out which column contains the avg row length field and assume
* that mysqld always gives it in the same order across calls :) */
SNPRINTF(scratch,
Modified: GNUnet/src/applications/sqstore_mysql/mysqltest.c
===================================================================
--- GNUnet/src/applications/sqstore_mysql/mysqltest.c 2005-02-11 03:08:53 UTC
(rev 238)
+++ GNUnet/src/applications/sqstore_mysql/mysqltest.c 2005-02-11 16:38:27 UTC
(rev 239)
@@ -10,7 +10,7 @@
#include "gnunet_sqstore_service.h"
#include "core.h"
-#define ASSERT(x) do { if (! (x)) goto FAILURE; } while (0)
+#define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__,
__LINE__); goto FAILURE; } } while (0)
static cron_t now;
@@ -35,7 +35,7 @@
Datastore_Value * value;
i = *(int*) closure;
- value = initValue(i);
+ value = initValue(i+1);
if ( ( value->size == val->size) &&
(0 == memcmp(val,
value,
@@ -79,20 +79,21 @@
cronTime(&now);
oldSize = api->getSize();
for (i=0;i<256;i++) {
- value = initValue(i);
+ value = initValue(i+1);
memset(&key, 256-i, sizeof(HashCode160));
api->put(&key, value);
FREE(value);
}
ASSERT(oldSize < api->getSize());
for (i=255;i>=0;i--) {
- memset(&key, 256-i, sizeof(HashCode160));
+ memset(&key, 256-i, sizeof(HashCode160));
ASSERT(1 == api->get(&key, i, &checkValue, (void*) &i));
+ printf("OK: %d\n", i);
}
oldSize = api->getSize();
for (i=255;i>=0;i-=2) {
memset(&key, 256-i, sizeof(HashCode160));
- value = initValue(i);
+ value = initValue(i+1);
ASSERT(1 == api->del(&key, value));
FREE(value);
}
@@ -105,15 +106,21 @@
ASSERT(128 == api->iterateExpirationTime(ANY_BLOCK,
(Datum_Iterator) &iterateDown,
&i));
- ASSERT(0 == i);
+ ASSERT(1 == i);
+ for (i=254;i>=0;i-=2) {
+ memset(&key, 256-i, sizeof(HashCode160));
+ value = initValue(i+1);
+ ASSERT(1 == api->del(&key, value));
+ FREE(value);
+ }
+
-
/* FIXME: test 'update' here! */
- api->drop();
+ // api->drop();
return OK;
FAILURE:
- api->drop();
+ // api->drop();
return SYSERR;
}
@@ -130,6 +137,9 @@
FREENONNULL(setConfigurationString("GNUNETD",
"LOGFILE",
NULL));
+ FREENONNULL(setConfigurationString("GNUNETD",
+ "LOGLEVEL",
+ "DEBUG"));
FREENONNULL(setConfigurationString("",
"GNUNETD_HOME",
"/tmp/gnunet_test/"));
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r239 - in GNUnet/src/applications: fs/ecrs sqstore_mysql,
grothoff <=