From 7a0a5ba82e4c30f8aa81ab3d29337b03fb5de5c0 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 18 Sep 2011 10:42:00 +0000 Subject: more efficient implementation, maybe helping with 1777 --- src/datacache/datacache.c | 2 +- src/datacache/plugin_datacache_sqlite.c | 34 +++++++++++++-------------------- 2 files changed, 14 insertions(+), 22 deletions(-) (limited to 'src/datacache') diff --git a/src/datacache/datacache.c b/src/datacache/datacache.c index 26402362b..43a9234e5 100644 --- a/src/datacache/datacache.c +++ b/src/datacache/datacache.c @@ -237,7 +237,7 @@ GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h, GNUNET_NO); GNUNET_CONTAINER_bloomfilter_add (h->filter, key); while (h->utilization + used > h->env.quota) - GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls)); + GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls)); h->utilization += used; return GNUNET_OK; } diff --git a/src/datacache/plugin_datacache_sqlite.c b/src/datacache/plugin_datacache_sqlite.c index 369e8c101..4bb61dcd1 100644 --- a/src/datacache/plugin_datacache_sqlite.c +++ b/src/datacache/plugin_datacache_sqlite.c @@ -115,7 +115,7 @@ sqlite_plugin_put (void *cls, const GNUNET_HashCode * key, size_t size, dval = INT64_MAX; if (sq_prepare (plugin->dbh, - "INSERT INTO ds090 " "(type, expire, key, value) " "VALUES (?, ?, ?, ?)", + "INSERT INTO ds090 (type, expire, key, value) VALUES (?, ?, ?, ?)", &stmt) != SQLITE_OK) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, @@ -275,11 +275,10 @@ static int sqlite_plugin_del (void *cls) { struct Plugin *plugin = cls; + unsigned long long rowid; unsigned int dsize; - unsigned int dtype; sqlite3_stmt *stmt; sqlite3_stmt *dstmt; - char blob[65536]; GNUNET_HashCode hc; #if DEBUG_DATACACHE_SQLITE @@ -289,7 +288,7 @@ sqlite_plugin_del (void *cls) dstmt = NULL; if (sq_prepare (plugin->dbh, - "SELECT type, key, value FROM ds090 ORDER BY expire ASC LIMIT 1", + "SELECT _ROWID_,key,value FROM ds090 ORDER BY expire ASC LIMIT 1", &stmt) != SQLITE_OK) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, @@ -307,18 +306,16 @@ sqlite_plugin_del (void *cls) (void) sqlite3_finalize (stmt); return GNUNET_SYSERR; } - dtype = sqlite3_column_int (stmt, 0); - GNUNET_break (sqlite3_column_bytes (stmt, 1) == sizeof (GNUNET_HashCode)); - dsize = sqlite3_column_bytes (stmt, 2); - GNUNET_assert (dsize <= sizeof (blob)); - memcpy (blob, sqlite3_column_blob (stmt, 2), dsize); + rowid = sqlite3_column_int64 (stmt, 0); + GNUNET_assert (sqlite3_column_bytes (stmt, 1) == sizeof (GNUNET_HashCode)); memcpy (&hc, sqlite3_column_blob (stmt, 1), sizeof (GNUNET_HashCode)); + dsize = sqlite3_column_bytes (stmt, 2); if (SQLITE_OK != sqlite3_finalize (stmt)) GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, _("`%s' failed at %s:%d with error: %s\n"), "sqlite3_step", __FILE__, __LINE__, sqlite3_errmsg (plugin->dbh)); if (sq_prepare - (plugin->dbh, "DELETE FROM ds090 " "WHERE key=? AND value=? AND type=?", + (plugin->dbh, "DELETE FROM ds090 WHERE _ROWID_=?", &dstmt) != SQLITE_OK) { GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, @@ -328,15 +325,12 @@ sqlite_plugin_del (void *cls) (void) sqlite3_finalize (stmt); return GNUNET_SYSERR; } - if ((SQLITE_OK != - sqlite3_bind_blob (dstmt, 1, &hc, sizeof (GNUNET_HashCode), - SQLITE_TRANSIENT)) || - (SQLITE_OK != sqlite3_bind_blob (dstmt, 2, blob, dsize, SQLITE_TRANSIENT)) - || (SQLITE_OK != sqlite3_bind_int (dstmt, 3, dtype))) - { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, - _("`%s' failed at %s:%d with error: %s\n"), "sqlite3_bind", - __FILE__, __LINE__, sqlite3_errmsg (plugin->dbh)); + if (SQLITE_OK != + sqlite3_bind_int64 (dstmt, 1, rowid)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, + _("`%s' failed at %s:%d with error: %s\n"), "sqlite3_bind", + __FILE__, __LINE__, sqlite3_errmsg (plugin->dbh)); (void) sqlite3_finalize (dstmt); return GNUNET_SYSERR; } @@ -420,8 +414,6 @@ libgnunet_plugin_datacache_sqlite_init (void *cls) return api; } -// explain SELECT type FROM gn090 WHERE NOT EXISTS (SELECT 1 from gn090 WHERE expire < 42 LIMIT 1) OR expire < 42 ORDER BY repl DESC, Random() LIMIT 1; - /** * Exit point from the plugin. -- cgit v1.2.3