aboutsummaryrefslogtreecommitdiff
path: root/src/datastore/plugin_datastore_sqlite.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2011-04-13 19:41:26 +0000
committerChristian Grothoff <christian@grothoff.org>2011-04-13 19:41:26 +0000
commit57a074bce8ef0c538d1b1906c9de7482275232fb (patch)
tree0d8730be01a7e6b42496c406e0715eb0c9ca94e7 /src/datastore/plugin_datastore_sqlite.c
parent47f30b90cf1f18ac682b6fd3a8c9585811368ee9 (diff)
downloadgnunet-57a074bce8ef0c538d1b1906c9de7482275232fb.tar.gz
gnunet-57a074bce8ef0c538d1b1906c9de7482275232fb.zip
getting mysql code to compile again, fixes to sqlite code
Diffstat (limited to 'src/datastore/plugin_datastore_sqlite.c')
-rw-r--r--src/datastore/plugin_datastore_sqlite.c135
1 files changed, 98 insertions, 37 deletions
diff --git a/src/datastore/plugin_datastore_sqlite.c b/src/datastore/plugin_datastore_sqlite.c
index fd10a38cb..6e77ec364 100644
--- a/src/datastore/plugin_datastore_sqlite.c
+++ b/src/datastore/plugin_datastore_sqlite.c
@@ -181,7 +181,9 @@ create_indices (sqlite3 * dbh)
181 NULL); 181 NULL);
182 sqlite3_exec (dbh, "CREATE INDEX idx_comb ON gn090 (anonLevel ASC,expire ASC,prio,type,hash)", 182 sqlite3_exec (dbh, "CREATE INDEX idx_comb ON gn090 (anonLevel ASC,expire ASC,prio,type,hash)",
183 NULL, NULL, NULL); 183 NULL, NULL, NULL);
184 sqlite3_exec (dbh, "CREATE INDEX expire ON gn090 (expire)", 184 sqlite3_exec (dbh, "CREATE INDEX idx_expire ON gn090 (expire)",
185 NULL, NULL, NULL);
186 sqlite3_exec (dbh, "CREATE INDEX idx_repl ON gn090 (repl)",
185 NULL, NULL, NULL); 187 NULL, NULL, NULL);
186} 188}
187 189
@@ -309,24 +311,6 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
309 sqlite3_finalize (stmt); 311 sqlite3_finalize (stmt);
310 create_indices (plugin->dbh); 312 create_indices (plugin->dbh);
311 313
312 CHECK (SQLITE_OK ==
313 sq_prepare (plugin->dbh,
314 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn071'",
315 &stmt));
316 if ( (sqlite3_step (stmt) == SQLITE_DONE) &&
317 (sqlite3_exec (plugin->dbh,
318 "CREATE TABLE gn071 ("
319 " key TEXT NOT NULL DEFAULT '',"
320 " value INTEGER NOT NULL DEFAULT 0)", NULL, NULL,
321 NULL) != SQLITE_OK) )
322 {
323 LOG_SQLITE (plugin, NULL,
324 GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
325 sqlite3_finalize (stmt);
326 return GNUNET_SYSERR;
327 }
328 sqlite3_finalize (stmt);
329
330 if ((sq_prepare (plugin->dbh, 314 if ((sq_prepare (plugin->dbh,
331 "UPDATE gn090 SET prio = prio + ?, expire = MAX(expire,?) WHERE _ROWID_ = ?", 315 "UPDATE gn090 SET prio = prio + ?, expire = MAX(expire,?) WHERE _ROWID_ = ?",
332 &plugin->updPrio) != SQLITE_OK) || 316 &plugin->updPrio) != SQLITE_OK) ||
@@ -334,7 +318,7 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
334 "UPDATE gn090 SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?", 318 "UPDATE gn090 SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?",
335 &plugin->updRepl) != SQLITE_OK) || 319 &plugin->updRepl) != SQLITE_OK) ||
336 (sq_prepare (plugin->dbh, 320 (sq_prepare (plugin->dbh,
337 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE expire > ?" 321 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090"
338 " ORDER BY repl DESC, Random() LIMIT 1", 322 " ORDER BY repl DESC, Random() LIMIT 1",
339 &plugin->selRepl) != SQLITE_OK) || 323 &plugin->selRepl) != SQLITE_OK) ||
340 (sq_prepare (plugin->dbh, 324 (sq_prepare (plugin->dbh,
@@ -1419,7 +1403,95 @@ execute_get (struct Plugin *plugin,
1419 1403
1420 1404
1421/** 1405/**
1422 * Get a random item for replication. Returns a single, not expired, random item 1406 * Context for 'repl_iter' function.
1407 */
1408struct ReplCtx
1409{
1410
1411 /**
1412 * Plugin handle.
1413 */
1414 struct Plugin *plugin;
1415
1416 /**
1417 * Function to call for the result (or the NULL).
1418 */
1419 PluginIterator iter;
1420
1421 /**
1422 * Closure for iter.
1423 */
1424 void *iter_cls;
1425};
1426
1427
1428/**
1429 * Wrapper for the iterator for 'sqlite_plugin_replication_get'.
1430 * Decrements the replication counter and calls the original
1431 * iterator.
1432 *
1433 * @param cls closure
1434 * @param next_cls closure to pass to the "next" function.
1435 * @param key key for the content
1436 * @param size number of bytes in data
1437 * @param data content stored
1438 * @param type type of the content
1439 * @param priority priority of the content
1440 * @param anonymity anonymity-level for the content
1441 * @param expiration expiration time for the content
1442 * @param uid unique identifier for the datum;
1443 * maybe 0 if no unique identifier is available
1444 *
1445 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
1446 * (continue on call to "next", of course),
1447 * GNUNET_NO to delete the item and continue (if supported)
1448 */
1449static int
1450repl_iter (void *cls,
1451 void *next_cls,
1452 const GNUNET_HashCode *key,
1453 uint32_t size,
1454 const void *data,
1455 enum GNUNET_BLOCK_Type type,
1456 uint32_t priority,
1457 uint32_t anonymity,
1458 struct GNUNET_TIME_Absolute expiration,
1459 uint64_t uid)
1460{
1461 struct ReplCtx *rc = cls;
1462 struct Plugin *plugin = rc->plugin;
1463 int ret;
1464
1465 ret = rc->iter (rc->iter_cls,
1466 next_cls, key,
1467 size, data,
1468 type, priority, anonymity, expiration,
1469 uid);
1470 if (NULL != key)
1471 {
1472 sqlite3_bind_int64 (plugin->updRepl, 1, uid);
1473 if (SQLITE_DONE != sqlite3_step (plugin->updRepl))
1474 {
1475 LOG_SQLITE (plugin, NULL,
1476 GNUNET_ERROR_TYPE_ERROR |
1477 GNUNET_ERROR_TYPE_BULK, "sqlite3_step");
1478 if (SQLITE_OK != sqlite3_reset (plugin->updRepl))
1479 LOG_SQLITE (plugin, NULL,
1480 GNUNET_ERROR_TYPE_ERROR |
1481 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1482 return GNUNET_SYSERR;
1483 }
1484 if (SQLITE_OK != sqlite3_reset (plugin->delRow))
1485 LOG_SQLITE (plugin, NULL,
1486 GNUNET_ERROR_TYPE_ERROR |
1487 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1488 }
1489 return ret;
1490}
1491
1492
1493/**
1494 * Get a random item for replication. Returns a single random item
1423 * from those with the highest replication counters. The item's 1495 * from those with the highest replication counters. The item's
1424 * replication counter is decremented by one IF it was positive before. 1496 * replication counter is decremented by one IF it was positive before.
1425 * Call 'iter' with all values ZERO or NULL if the datastore is empty. 1497 * Call 'iter' with all values ZERO or NULL if the datastore is empty.
@@ -1433,28 +1505,17 @@ sqlite_plugin_replication_get (void *cls,
1433 PluginIterator iter, void *iter_cls) 1505 PluginIterator iter, void *iter_cls)
1434{ 1506{
1435 struct Plugin *plugin = cls; 1507 struct Plugin *plugin = cls;
1436 sqlite3_stmt *stmt; 1508 struct ReplCtx rc;
1437 struct GNUNET_TIME_Absolute now;
1438 1509
1439#if DEBUG_SQLITE 1510#if DEBUG_SQLITE
1440 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, 1511 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
1441 "sqlite", 1512 "sqlite",
1442 "Getting random block based on replication order.\n"); 1513 "Getting random block based on replication order.\n");
1443#endif 1514#endif
1444 stmt = plugin->selRepl; 1515 rc.plugin = plugin;
1445 now = GNUNET_TIME_absolute_get (); 1516 rc.iter = iter;
1446 if (SQLITE_OK != sqlite3_bind_int64 (stmt, 1, now.abs_value)) 1517 rc.iter_cls = iter_cls;
1447 { 1518 execute_get (plugin, plugin->selRepl, &repl_iter, &rc);
1448 LOG_SQLITE (plugin, NULL,
1449 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
1450 if (SQLITE_OK != sqlite3_reset (stmt))
1451 LOG_SQLITE (plugin, NULL,
1452 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
1453 iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0,
1454 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1455 return;
1456 }
1457 execute_get (plugin, stmt, iter, iter_cls);
1458} 1519}
1459 1520
1460 1521