de-experimentalizing
[oweals/gnunet.git] / src / datastore / plugin_datastore_sqlite.c
index b05a0a9c1f5c4a6414c655e2a7f8096b72462fe4..6e77ec3646da49edcfae2c9438d877566c627aa7 100644 (file)
 #include "gnunet_datastore_plugin.h"
 #include <sqlite3.h>
 
+/**
+ * Enable or disable logging debug messages.
+ */
 #define DEBUG_SQLITE GNUNET_NO
 
-
 /**
- * Log an error message at log-level 'level' that indicates
- * a failure of the command 'cmd' on file 'filename'
- * with the message given by strerror(errno).
+ * We allocate items on the stack at times.  To prevent a stack
+ * overflow, we impose a limit on the maximum size for the data per
+ * item.  64k should be enough.
  */
-#define LOG_SQLITE(db, msg, level, cmd) do { GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); if (msg != NULL) GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
-
-
-#define SELECT_IT_NON_ANONYMOUS_1 \
-  "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (prio = ?1 AND expire > %llu AND anonLevel = 0 AND hash < ?2) "\
-  " ORDER BY hash DESC LIMIT 1"
-
-#define SELECT_IT_NON_ANONYMOUS_2 \
-  "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (prio < ?1 AND expire > %llu AND anonLevel = 0)"\
-  " ORDER BY prio DESC, hash DESC LIMIT 1"
-
-
-#define SELECT_IT_REPLICATION_ORDER \
-  "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (expire > ?1) "\
-  " ORDER BY repl DESC, Random() LIMIT 1"
-
-#define SELECT_IT_EXPIRATION_ORDER \
-  "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 WHERE (expire < ?1) "\
-  " OR NOT EXISTS (SELECT 1 from gn090 WHERE (expire < ?1)) "\
-  " ORDER BY prio ASC LIMIT 1"
-
+#define MAX_ITEM_SIZE 65536
 
 /**
  * After how many ms "busy" should a DB operation fail for good?
 #define BUSY_TIMEOUT_MS 250
 
 
+/**
+ * Log an error message at log-level 'level' that indicates
+ * a failure of the command 'cmd' on file 'filename'
+ * with the message given by strerror(errno).
+ */
+#define LOG_SQLITE(db, msg, level, cmd) do { GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); if (msg != NULL) GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
+
+
 
 /**
  * Context for all functions in this plugin.
@@ -155,15 +145,17 @@ sq_prepare (sqlite3 * dbh,
 {
   char *dummy;
   int result;
+
   result = sqlite3_prepare_v2 (dbh,
                               zSql,
                               strlen (zSql), 
                               ppStmt,
                               (const char **) &dummy);
-#if DEBUG_SQLITE
+#if DEBUG_SQLITE && 0
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
                   "sqlite",
-                   "Prepared %p: %d\n",
+                   "Prepared `%s' / %p: %d\n",
+                  zSql,
                   *ppStmt, 
                   result);
 #endif
@@ -182,19 +174,20 @@ create_indices (sqlite3 * dbh)
   /* create indices */
   sqlite3_exec (dbh,
                 "CREATE INDEX idx_hash ON gn090 (hash)", NULL, NULL, NULL);
-  sqlite3_exec (dbh, "CREATE INDEX idx_prio ON gn090 (prio)", NULL, NULL,
-                NULL);
-  sqlite3_exec (dbh, "CREATE INDEX idx_expire_prio ON gn090 (expire,prio)", NULL, NULL,
-                NULL);
   sqlite3_exec (dbh,
                 "CREATE INDEX idx_hash_vhash ON gn090 (hash,vhash)", NULL,
                 NULL, NULL);
-  sqlite3_exec (dbh, "CREATE INDEX idx_comb ON gn090 (prio,expire,anonLevel,hash)",
+  sqlite3_exec (dbh, "CREATE INDEX idx_expire_repl ON gn090 (expire ASC,repl DESC)", NULL, NULL,
+                NULL);
+  sqlite3_exec (dbh, "CREATE INDEX idx_comb ON gn090 (anonLevel ASC,expire ASC,prio,type,hash)",
+                NULL, NULL, NULL);
+  sqlite3_exec (dbh, "CREATE INDEX idx_expire ON gn090 (expire)",
+                NULL, NULL, NULL);
+  sqlite3_exec (dbh, "CREATE INDEX idx_repl ON gn090 (repl)",
                 NULL, NULL, NULL);
 }
 
 
-
 #if 0
 #define CHECK(a) GNUNET_break(a)
 #define ENULL NULL
@@ -205,8 +198,6 @@ create_indices (sqlite3 * dbh)
 #endif
 
 
-
-
 /**
  * Initialize the database connections and associated
  * data structures (create tables and indices
@@ -274,9 +265,15 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
   CHECK (SQLITE_OK ==
          sqlite3_exec (plugin->dbh,
                        "PRAGMA synchronous=OFF", NULL, NULL, ENULL));
+  CHECK (SQLITE_OK ==
+         sqlite3_exec (plugin->dbh,
+                       "PRAGMA legacy_file_format=OFF", NULL, NULL, ENULL));
   CHECK (SQLITE_OK ==
          sqlite3_exec (plugin->dbh,
                        "PRAGMA auto_vacuum=INCREMENTAL", NULL, NULL, ENULL));
+  CHECK (SQLITE_OK ==
+         sqlite3_exec (plugin->dbh,
+                       "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL, ENULL));
   CHECK (SQLITE_OK ==
          sqlite3_exec (plugin->dbh,
                        "PRAGMA count_changes=OFF", NULL, NULL, ENULL));
@@ -314,42 +311,25 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
   sqlite3_finalize (stmt);
   create_indices (plugin->dbh);
 
-  CHECK (SQLITE_OK ==
-         sq_prepare (plugin->dbh,
-                     "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn071'",
-                     &stmt));
-  if ( (sqlite3_step (stmt) == SQLITE_DONE) &&
-       (sqlite3_exec (plugin->dbh,
-                     "CREATE TABLE gn071 ("
-                     "  key TEXT NOT NULL DEFAULT '',"
-                     "  value INTEGER NOT NULL DEFAULT 0)", NULL, NULL,
-                     NULL) != SQLITE_OK) )
-    {
-      LOG_SQLITE (plugin, NULL,
-                 GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
-      sqlite3_finalize (stmt);
-      return GNUNET_SYSERR;
-    }
-  sqlite3_finalize (stmt);
-
   if ((sq_prepare (plugin->dbh,
-                   "UPDATE gn090 SET prio = prio + ?, expire = MAX(expire,?) WHERE "
-                   "_ROWID_ = ?",
+                   "UPDATE gn090 SET prio = prio + ?, expire = MAX(expire,?) WHERE _ROWID_ = ?",
                    &plugin->updPrio) != SQLITE_OK) ||
       (sq_prepare (plugin->dbh,
-                   "UPDATE gn090 SET repl = MAX (0, repl - 1) WHERE "
-                   "_ROWID_ = ?",
+                   "UPDATE gn090 SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?",
                    &plugin->updRepl) != SQLITE_OK) ||
       (sq_prepare (plugin->dbh,
-                   SELECT_IT_REPLICATION_ORDER,
+                  "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090"
+                  " ORDER BY repl DESC, Random() LIMIT 1",
                    &plugin->selRepl) != SQLITE_OK) ||
       (sq_prepare (plugin->dbh,
-                   SELECT_IT_EXPIRATION_ORDER,
+                  "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 "
+                  " WHERE NOT EXISTS (SELECT 1 FROM gn090 WHERE expire < ?1 LIMIT 1) OR expire < ?1 "
+                  " ORDER BY prio ASC LIMIT 1",
                    &plugin->selExpi) != SQLITE_OK) ||
       (sq_prepare (plugin->dbh,
                    "INSERT INTO gn090 (repl, type, prio, "
-                   "anonLevel, expire, hash, vhash, value) VALUES "
-                   "(?, ?, ?, ?, ?, ?, ?, ?)",
+                   "anonLevel, expire, hash, vhash, value) "
+                   "VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
                    &plugin->insertContent) != SQLITE_OK) ||
       (sq_prepare (plugin->dbh,
                    "DELETE FROM gn090 WHERE _ROWID_ = ?",
@@ -542,7 +522,7 @@ struct NextContext
 /**
  * Continuation of "sqlite_next_request".
  *
- * @param cls the next context
+ * @param cls the 'struct NextContext*'
  * @param tc the task context (unused)
  */
 static void 
@@ -554,12 +534,12 @@ sqlite_next_request_cont (void *cls,
   unsigned long long rowid;
   int ret;
   unsigned int size;
+  unsigned int hsize;
   uint32_t anonymity;
   uint32_t priority;
   enum GNUNET_BLOCK_Type type;
   const GNUNET_HashCode *key;
   struct GNUNET_TIME_Absolute expiration;
-  char data[GNUNET_SERVER_MAX_MESSAGE_SIZE];
   
   plugin = nc->plugin;
   plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
@@ -568,6 +548,12 @@ sqlite_next_request_cont (void *cls,
        (GNUNET_OK != (nc->prep(nc->prep_cls,
                               nc))) )
     {
+#if DEBUG_SQLITE
+      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                      "sqlite",
+                      "Iteration completes after %u results\n",
+                      nc->count);
+#endif
     END:
       nc->iter (nc->iter_cls, 
                NULL, NULL, 0, NULL, 0, 0, 0, 
@@ -581,15 +567,13 @@ sqlite_next_request_cont (void *cls,
   priority = sqlite3_column_int (nc->stmt, 1);
   anonymity = sqlite3_column_int (nc->stmt, 2);
   expiration.abs_value = sqlite3_column_int64 (nc->stmt, 3);
+  hsize = sqlite3_column_bytes (nc->stmt, 4);
   key = sqlite3_column_blob (nc->stmt, 4);
   size = sqlite3_column_bytes (nc->stmt, 5);
-  memcpy (data, sqlite3_column_blob (nc->stmt, 5), size);
   rowid = sqlite3_column_int64 (nc->stmt, 6);
-  if (sqlite3_column_bytes (nc->stmt, 4) != sizeof (GNUNET_HashCode))
+  if (hsize != sizeof (GNUNET_HashCode))
     {
-      GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, 
-                      "sqlite",
-                      _("Invalid data in database.  Trying to fix (by deletion).\n"));
+      GNUNET_break (0);
       if (SQLITE_OK != sqlite3_reset (nc->stmt))
        LOG_SQLITE (plugin, NULL,
                    GNUNET_ERROR_TYPE_ERROR |
@@ -599,37 +583,55 @@ sqlite_next_request_cont (void *cls,
                          - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));      
       goto END;
     }
-  nc->count++;
-  nc->last_rowid = rowid;
-  nc->lastPriority = priority;
-  nc->lastKey = *key;
-  if (SQLITE_OK != sqlite3_reset (nc->stmt))
-    LOG_SQLITE (plugin, NULL,
-               GNUNET_ERROR_TYPE_ERROR |
-               GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
-  ret = nc->iter (nc->iter_cls, nc,
-                 key,
-                 size, data,
-                 type, priority,
-                 anonymity, expiration,
-                 rowid);
+#if DEBUG_SQLITE
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                  "sqlite",
+                  "Iterator returns value with type %u/key `%s'/priority %u/expiration %llu (%lld).\n",
+                  type, 
+                  GNUNET_h2s(key),
+                  priority,
+                  (unsigned long long) GNUNET_TIME_absolute_get_remaining (expiration).rel_value,
+                  (long long) expiration.abs_value);
+#endif
+  if (size > MAX_ITEM_SIZE)
+    {
+      GNUNET_break (0);
+      if (SQLITE_OK != sqlite3_reset (nc->stmt))
+       LOG_SQLITE (plugin, NULL,
+                   GNUNET_ERROR_TYPE_ERROR |
+                   GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
+      if (GNUNET_OK == delete_by_rowid (plugin, rowid))
+       plugin->env->duc (plugin->env->cls,
+                         - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); 
+      goto END;
+    }
+  {
+    char data[size];
+    
+    memcpy (data, sqlite3_column_blob (nc->stmt, 5), size);
+    nc->count++;
+    nc->last_rowid = rowid;
+    nc->lastPriority = priority;
+    nc->lastKey = *key;
+    if (SQLITE_OK != sqlite3_reset (nc->stmt))
+      LOG_SQLITE (plugin, NULL,
+                 GNUNET_ERROR_TYPE_ERROR |
+                 GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
+    ret = nc->iter (nc->iter_cls, nc,
+                   &nc->lastKey,
+                   size, data,
+                   type, priority,
+                   anonymity, expiration,
+                   rowid);
+  }
   switch (ret)
     {
     case GNUNET_SYSERR:
       nc->end_it = GNUNET_YES;
       break;
     case GNUNET_NO:
-#if DEBUG_SQLITE
-      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
-                      "sqlite",
-                      "Asked to remove entry %llu (%u bytes)\n",
-                      (unsigned long long) rowid,
-                      size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
-#endif
       if (GNUNET_OK == delete_by_rowid (plugin, rowid))
        {
-         plugin->env->duc (plugin->env->cls,
-                           - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
 #if DEBUG_SQLITE
          GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
                           "sqlite",
@@ -637,6 +639,8 @@ sqlite_next_request_cont (void *cls,
                           (unsigned long long) rowid,
                           size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
 #endif
+         plugin->env->duc (plugin->env->cls,
+                           - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
        }
       break;
     case GNUNET_YES:
@@ -648,9 +652,8 @@ sqlite_next_request_cont (void *cls,
 
 
 /**
- * Function invoked on behalf of a "PluginIterator"
- * asking the database plugin to call the iterator
- * with the next item.
+ * Function invoked on behalf of a "PluginIterator" asking the
+ * database plugin to call the iterator with the next item.
  *
  * @param next_cls whatever argument was given
  *        to the PluginIterator as "next_cls".
@@ -702,9 +705,12 @@ sqlite_plugin_put (void *cls,
 {
   struct Plugin *plugin = cls;
   int n;
+  int ret;
   sqlite3_stmt *stmt;
   GNUNET_HashCode vhash;
 
+  if (size > MAX_ITEM_SIZE)
+    return GNUNET_SYSERR;
 #if DEBUG_SQLITE
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
                   "sqlite",
@@ -744,10 +750,6 @@ sqlite_plugin_put (void *cls,
   switch (n)
     {
     case SQLITE_DONE:
-      if (SQLITE_OK != sqlite3_reset (stmt))
-       LOG_SQLITE (plugin, NULL,
-                   GNUNET_ERROR_TYPE_ERROR |
-                   GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
       plugin->env->duc (plugin->env->cls,
                        size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
 #if DEBUG_SQLITE
@@ -756,24 +758,33 @@ sqlite_plugin_put (void *cls,
                       "Stored new entry (%u bytes)\n",
                       size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
 #endif
-      return GNUNET_OK;
+      ret = GNUNET_OK;
+      break;
     case SQLITE_BUSY:      
       GNUNET_break (0);
       LOG_SQLITE (plugin, msg,
                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 
                  "sqlite3_step");
-      sqlite3_reset (stmt);
-      return GNUNET_SYSERR;
+      ret = GNUNET_SYSERR;
+      break;
     default:
       LOG_SQLITE (plugin, msg,
                   GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 
                  "sqlite3_step");
-      sqlite3_reset (stmt);
+      if (SQLITE_OK != sqlite3_reset (stmt))
+       LOG_SQLITE (plugin, NULL,
+                   GNUNET_ERROR_TYPE_ERROR |
+                   GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
       database_shutdown (plugin);
       database_setup (plugin->env->cfg,
                      plugin);
       return GNUNET_SYSERR;    
     }
+  if (SQLITE_OK != sqlite3_reset (stmt))
+    LOG_SQLITE (plugin, NULL,
+               GNUNET_ERROR_TYPE_ERROR |
+               GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
+  return ret;
 }
 
 
@@ -840,15 +851,15 @@ sqlite_plugin_update (void *cls,
 /**
  * Internal context for an iteration.
  */
-struct IterContext
+struct ZeroIterContext
 {
   /**
-   * FIXME.
+   * First iterator statement for zero-anonymity iteration.
    */
   sqlite3_stmt *stmt_1;
 
   /**
-   * FIXME.
+   * Second iterator statement for zero-anonymity iteration.
    */
   sqlite3_stmt *stmt_2;
 
@@ -862,17 +873,17 @@ struct IterContext
 /**
  * Prepare our SQL query to obtain the next record from the database.
  *
- * @param cls our "struct IterContext"
+ * @param cls our "struct ZeroIterContext"
  * @param nc NULL to terminate the iteration, otherwise our context for
  *           getting the next result.
  * @return GNUNET_OK on success, GNUNET_NO if there are no more results,
  *         GNUNET_SYSERR on error (or end of iteration)
  */
 static int
-iter_next_prepare (void *cls,
-                  struct NextContext *nc)
+zero_iter_next_prepare (void *cls,
+                       struct NextContext *nc)
 {
-  struct IterContext *ic = cls;
+  struct ZeroIterContext *ic = cls;
   struct Plugin *plugin;
   int ret;
 
@@ -886,25 +897,30 @@ iter_next_prepare (void *cls,
       sqlite3_finalize (ic->stmt_2);
       return GNUNET_SYSERR;
     }
-  sqlite3_reset (ic->stmt_1);
-  sqlite3_reset (ic->stmt_2);
   plugin = nc->plugin;
+
+  /* first try iter 1 */
 #if DEBUG_SQLITE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Restricting to results larger than the last priority %u\n",
-             nc->lastPriority);
-#endif
-  sqlite3_bind_int (ic->stmt_1, 1, nc->lastPriority);
-  sqlite3_bind_int (ic->stmt_2, 1, nc->lastPriority);
-#if DEBUG_SQLITE
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Restricting to results larger than the last key `%s'\n",
-             GNUNET_h2s(&nc->lastKey));
+             "Restricting to results larger than the last priority %u and key `%s'\n",
+             nc->lastPriority,
+             GNUNET_h2s (&nc->lastKey));
 #endif
-  sqlite3_bind_blob (ic->stmt_1, 2, 
-                    &nc->lastKey, 
-                    sizeof (GNUNET_HashCode),
-                    SQLITE_TRANSIENT);
+  if ( (SQLITE_OK != sqlite3_bind_int (ic->stmt_1, 1, nc->lastPriority)) ||
+       (SQLITE_OK != sqlite3_bind_blob (ic->stmt_1, 2, 
+                                       &nc->lastKey, 
+                                       sizeof (GNUNET_HashCode),
+                                       SQLITE_TRANSIENT)) )
+    {
+      LOG_SQLITE (plugin, NULL,
+                  GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
+      if (SQLITE_OK != sqlite3_reset (ic->stmt_1))
+       LOG_SQLITE (plugin, NULL,
+                   GNUNET_ERROR_TYPE_ERROR | 
+                   GNUNET_ERROR_TYPE_BULK, 
+                   "sqlite3_reset");  
+      return GNUNET_SYSERR;
+    }
   if (SQLITE_ROW == (ret = sqlite3_step (ic->stmt_1)))
     {      
 #if DEBUG_SQLITE
@@ -920,13 +936,26 @@ iter_next_prepare (void *cls,
                  GNUNET_ERROR_TYPE_ERROR |
                  GNUNET_ERROR_TYPE_BULK,
                  "sqlite3_step");
+      if (SQLITE_OK != sqlite3_reset (ic->stmt_1))
+       LOG_SQLITE (plugin, NULL,
+                   GNUNET_ERROR_TYPE_ERROR | 
+                   GNUNET_ERROR_TYPE_BULK, 
+                   "sqlite3_reset");  
       return GNUNET_SYSERR;
     }
   if (SQLITE_OK != sqlite3_reset (ic->stmt_1))
     LOG_SQLITE (plugin, NULL,
                GNUNET_ERROR_TYPE_ERROR | 
                GNUNET_ERROR_TYPE_BULK, 
-               "sqlite3_reset");
+               "sqlite3_reset");  
+
+  /* now try iter 2 */
+  if (SQLITE_OK != sqlite3_bind_int (ic->stmt_2, 1, nc->lastPriority))
+    {
+      LOG_SQLITE (plugin, NULL,                  
+                  GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind_XXXX");
+      return GNUNET_SYSERR;
+    }
   if (SQLITE_ROW == (ret = sqlite3_step (ic->stmt_2))) 
     {
 #if DEBUG_SQLITE
@@ -942,6 +971,11 @@ iter_next_prepare (void *cls,
                  GNUNET_ERROR_TYPE_ERROR |
                  GNUNET_ERROR_TYPE_BULK,
                  "sqlite3_step");
+      if (SQLITE_OK != sqlite3_reset (ic->stmt_2))
+       LOG_SQLITE (plugin, NULL,
+                   GNUNET_ERROR_TYPE_ERROR |
+                   GNUNET_ERROR_TYPE_BULK,
+                   "sqlite3_reset");
       return GNUNET_SYSERR;
     }
   if (SQLITE_OK != sqlite3_reset (ic->stmt_2))
@@ -977,14 +1011,19 @@ sqlite_plugin_iter_zero_anonymity (void *cls,
   struct Plugin *plugin = cls;
   struct GNUNET_TIME_Absolute now;
   struct NextContext *nc;
-  struct IterContext *ic;
+  struct ZeroIterContext *ic;
   sqlite3_stmt *stmt_1;
   sqlite3_stmt *stmt_2;
   char *q;
 
+  GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
   now = GNUNET_TIME_absolute_get ();
-  GNUNET_asprintf (&q, SELECT_IT_NON_ANONYMOUS_1,
-                  (unsigned long long) now.abs_value);
+  GNUNET_asprintf (&q, 
+                  "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 "
+                  "WHERE (anonLevel = 0 AND expire > %llu AND prio = ?1 AND type=%d AND hash < ?2) "
+                  "ORDER BY hash DESC LIMIT 1",
+                  (unsigned long long) now.abs_value,
+                  type);
   if (sq_prepare (plugin->dbh, q, &stmt_1) != SQLITE_OK)
     {
       LOG_SQLITE (plugin, NULL,
@@ -995,8 +1034,12 @@ sqlite_plugin_iter_zero_anonymity (void *cls,
       return;
     }
   GNUNET_free (q);
-  GNUNET_asprintf (&q, SELECT_IT_NON_ANONYMOUS_2,
-                  (unsigned long long) now.abs_value);
+  GNUNET_asprintf (&q, 
+                  "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090 "
+                  "WHERE (anonLevel = 0 AND expire > %llu AND prio < ?1 AND type=%d) "
+                  "ORDER BY prio DESC, hash DESC LIMIT 1",
+                  (unsigned long long) now.abs_value,
+                  type);
   if (sq_prepare (plugin->dbh, q, &stmt_2) != SQLITE_OK)
     {
       LOG_SQLITE (plugin, NULL,
@@ -1009,160 +1052,78 @@ sqlite_plugin_iter_zero_anonymity (void *cls,
     }
   GNUNET_free (q);
   nc = GNUNET_malloc (sizeof(struct NextContext) + 
-                     sizeof(struct IterContext));
+                     sizeof(struct ZeroIterContext));
   nc->plugin = plugin;
   nc->iter = iter;
   nc->iter_cls = iter_cls;
   nc->stmt = NULL;
-  ic = (struct IterContext*) &nc[1];
+  ic = (struct ZeroIterContext*) &nc[1];
   ic->stmt_1 = stmt_1;
   ic->stmt_2 = stmt_2;
   ic->type = type;
-  nc->prep = &iter_next_prepare;
+  nc->prep = &zero_iter_next_prepare;
   nc->prep_cls = ic;
-  nc->lastPriority = 0x7FFFFFFF;
+  nc->lastPriority = INT32_MAX;
   memset (&nc->lastKey, 255, sizeof (GNUNET_HashCode));
   sqlite_next_request (nc, GNUNET_NO);
 }
 
 
 /**
- * Call sqlite using the already prepared query to get
- * the next result.
- *
- * @param cls context with the prepared query
- * @param nc context with the prepared query
- * @return GNUNET_OK on success, GNUNET_SYSERR on error, GNUNET_NO if
- *        there are no more results 
+ * Context for get_next_prepare.
  */
-static int
-all_next_prepare (void *cls,
-                 struct NextContext *nc)
+struct GetNextContext
 {
-  struct Plugin *plugin;
-  int ret;
-
-  if (nc == NULL)
-    {
-#if DEBUG_SQLITE
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                 "Asked to clean up iterator state.\n");
-#endif
-      nc = (struct NextContext *)cls;
-      if (nc->stmt)
-          sqlite3_finalize (nc->stmt);
-      nc->stmt = NULL;
-      return GNUNET_SYSERR;
-    }
-  plugin = nc->plugin;
-  ret = sqlite3_step (nc->stmt);
-  switch (ret)
-    {
-    case SQLITE_ROW:
-      return GNUNET_OK;  
-    case SQLITE_DONE:
-      return GNUNET_NO;
-    default:
-      LOG_SQLITE (plugin, NULL,
-                 GNUNET_ERROR_TYPE_ERROR |
-                 GNUNET_ERROR_TYPE_BULK,
-                 "sqlite3_step");
-      return GNUNET_SYSERR;
-    }
-}
-
 
-/**
- * Select a subset of the items in the datastore and call
- * the given iterator for each of them.
- *
- * @param cls our plugin context
- * @param type entries of which type should be considered?
- *        Use 0 for any type.
- * @param iter function to call on each matching value;
- *        will be called once with a NULL value at the end
- * @param iter_cls closure for iter
- */
-static void
-sqlite_plugin_iter_all_now (void *cls,
-                           enum GNUNET_BLOCK_Type type,
-                           PluginIterator iter,
-                           void *iter_cls)
-{
-  struct Plugin *plugin = cls;
-  struct NextContext *nc;
+  /**
+   * Our prepared statement.
+   */
   sqlite3_stmt *stmt;
 
-  if (sq_prepare (plugin->dbh, 
-                 "SELECT type,prio,anonLevel,expire,hash,value,_ROWID_ FROM gn090",
-                 &stmt) != SQLITE_OK)
-    {
-      LOG_SQLITE (plugin, NULL,
-                  GNUNET_ERROR_TYPE_ERROR |
-                  GNUNET_ERROR_TYPE_BULK, "sqlite3_prepare_v2");
-      iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
-      return;
-    }
-  nc = GNUNET_malloc (sizeof(struct NextContext));
-  nc->plugin = plugin;
-  nc->iter = iter;
-  nc->iter_cls = iter_cls;
-  nc->stmt = stmt;
-  nc->prep = &all_next_prepare;
-  nc->prep_cls = nc;
-  sqlite_next_request (nc, GNUNET_NO);
-}
-
-
-/**
- * FIXME.
- */
-struct GetNextContext
-{
-
   /**
-   * FIXME.
+   * Plugin handle.
    */
-  int total;
+  struct Plugin *plugin;
 
   /**
-   * FIXME.
+   * Key for the query.
    */
-  int off;
+  GNUNET_HashCode key;
 
   /**
-   * FIXME.
+   * Vhash for the query.
    */
-  int have_vhash;
+  GNUNET_HashCode vhash;
 
   /**
-   * FIXME.
+   * Expected total number of results.
    */
-  unsigned int type;
+  unsigned int total;
 
   /**
-   * FIXME.
+   * Offset to add for the selected result.
    */
-  sqlite3_stmt *stmt;
+  unsigned int off;
 
   /**
-   * FIXME.
+   * Is vhash set?
    */
-  GNUNET_HashCode key;
+  int have_vhash;
 
   /**
-   * FIXME.
+   * Desired block type.
    */
-  GNUNET_HashCode vhash;
-};
+  enum GNUNET_BLOCK_Type type;
 
+};
 
 
 /**
- * FIXME.
+ * Prepare the stmt in 'nc' for the next round of execution, selecting the
+ * next return value.
  *
  * @param cls our "struct GetNextContext*"
- * @param nc FIXME
+ * @param nc the general context
  * @return GNUNET_YES if there are more results, 
  *         GNUNET_NO if there are no more results,
  *         GNUNET_SYSERR on internal error
@@ -1172,9 +1133,9 @@ get_next_prepare (void *cls,
                  struct NextContext *nc)
 {
   struct GetNextContext *gnc = cls;
-  int sqoff;
   int ret;
   int limit_off;
+  unsigned int sqoff;
 
   if (nc == NULL)
     {
@@ -1189,8 +1150,8 @@ get_next_prepare (void *cls,
     limit_off = gnc->off;
   else
     limit_off = 0;
-  sqoff = 1;
   sqlite3_reset (nc->stmt);
+  sqoff = 1;
   ret = sqlite3_bind_blob (nc->stmt,
                           sqoff++,
                           &gnc->key, 
@@ -1204,14 +1165,31 @@ get_next_prepare (void *cls,
   if ((gnc->type != 0) && (ret == SQLITE_OK))
     ret = sqlite3_bind_int (nc->stmt, sqoff++, gnc->type);
   if (ret == SQLITE_OK)
-    ret = sqlite3_bind_int64 (nc->stmt, sqoff++, nc->last_rowid + 1);
-  if (ret == SQLITE_OK)
-    ret = sqlite3_bind_int (nc->stmt, sqoff++, limit_off);
+    ret = sqlite3_bind_int64 (nc->stmt, sqoff++, limit_off);
   if (ret != SQLITE_OK)
     return GNUNET_SYSERR;
-  if (SQLITE_ROW != sqlite3_step (nc->stmt))
-    return GNUNET_NO;
-  return GNUNET_OK;
+#if DEBUG_SQLITE 
+  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
+                  "sqlite",
+                   "Preparing to GET for key `%s' with type %d at offset %u\n",
+                  GNUNET_h2s (&gnc->key),
+                  gnc->type,
+                  limit_off);
+#endif
+  ret = sqlite3_step (nc->stmt);
+  switch (ret)
+    {
+    case SQLITE_ROW:
+      return GNUNET_OK;  
+    case SQLITE_DONE:
+      return GNUNET_NO;
+    default:
+      LOG_SQLITE (gnc->plugin, NULL,
+                 GNUNET_ERROR_TYPE_ERROR |
+                 GNUNET_ERROR_TYPE_BULK,
+                 "sqlite3_step");
+      return GNUNET_SYSERR;
+    }
 }
 
 
@@ -1220,7 +1198,7 @@ get_next_prepare (void *cls,
  * in the datastore.
  *
  * @param cls closure
- * @param key maybe NULL (to match all entries)
+ * @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
@@ -1234,31 +1212,26 @@ get_next_prepare (void *cls,
  */
 static void
 sqlite_plugin_get (void *cls,
-                  const GNUNET_HashCode * key,
-                  const GNUNET_HashCode * vhash,
+                  const GNUNET_HashCode *key,
+                  const GNUNET_HashCode *vhash,
                   enum GNUNET_BLOCK_Type type,
                   PluginIterator iter, void *iter_cls)
 {
   struct Plugin *plugin = cls;
-  struct GetNextContext *gpc;
+  struct GetNextContext *gnc;
   struct NextContext *nc;
   int ret;
   int total;
   sqlite3_stmt *stmt;
   char scratch[256];
-  int sqoff;
+  unsigned int sqoff;
 
   GNUNET_assert (iter != NULL);
-  if (key == NULL)
-    {
-      sqlite_plugin_iter_all_now (cls, type, iter, iter_cls);
-      return;
-    }
+  GNUNET_assert (key != NULL);
   GNUNET_snprintf (scratch, sizeof (scratch),
-                   "SELECT count(*) FROM gn090 WHERE hash=:1%s%s",
-                   vhash == NULL ? "" : " AND vhash=:2",
-                   type == 0 ? "" : (vhash ==
-                                     NULL) ? " AND type=:2" : " AND type=:3");
+                   "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,
@@ -1267,12 +1240,10 @@ sqlite_plugin_get (void *cls,
       return;
     }
   sqoff = 1;
-  ret = sqlite3_bind_blob (stmt,
-                           sqoff++,
+  ret = sqlite3_bind_blob (stmt, sqoff++,
                            key, sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
   if ((vhash != NULL) && (ret == SQLITE_OK))
-    ret = sqlite3_bind_blob (stmt,
-                             sqoff++,
+    ret = sqlite3_bind_blob (stmt, sqoff++,
                              vhash,
                              sizeof (GNUNET_HashCode), SQLITE_TRANSIENT);
   if ((type != 0) && (ret == SQLITE_OK))
@@ -1281,7 +1252,6 @@ sqlite_plugin_get (void *cls,
     {
       LOG_SQLITE (plugin, NULL,
                   GNUNET_ERROR_TYPE_ERROR, "sqlite_bind");
-      sqlite3_reset (stmt);
       sqlite3_finalize (stmt);
       iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
       return;
@@ -1292,28 +1262,24 @@ sqlite_plugin_get (void *cls,
       LOG_SQLITE (plugin, NULL,
                   GNUNET_ERROR_TYPE_ERROR| GNUNET_ERROR_TYPE_BULK, 
                  "sqlite_step");
-      sqlite3_reset (stmt);
       sqlite3_finalize (stmt);
       iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
       return;
     }
   total = sqlite3_column_int (stmt, 0);
-  sqlite3_reset (stmt);
   sqlite3_finalize (stmt);
   if (0 == total)
     {
       iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
       return;
     }
-
   GNUNET_snprintf (scratch, sizeof (scratch),
                    "SELECT type, prio, anonLevel, expire, hash, value, _ROWID_ "
-                   "FROM gn090 WHERE hash=:1%s%s AND _ROWID_ >= :%d "
-                   "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET :d",
-                   vhash == NULL ? "" : " AND vhash=:2",
-                   type == 0 ? "" : (vhash ==
-                                     NULL) ? " AND type=:2" : " AND type=:3",
-                   sqoff, sqoff + 1);
+                   "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,
@@ -1328,25 +1294,26 @@ sqlite_plugin_get (void *cls,
   nc->iter = iter;
   nc->iter_cls = iter_cls;
   nc->stmt = stmt;
-  gpc = (struct GetNextContext*) &nc[1];
-  gpc->total = total;
-  gpc->type = type;
-  gpc->key = *key;
-  gpc->stmt = stmt; /* alias used for freeing at the end! */
+  gnc = (struct GetNextContext*) &nc[1];
+  gnc->total = total;
+  gnc->type = type;
+  gnc->key = *key;
+  gnc->plugin = plugin;
+  gnc->stmt = stmt; /* alias used for freeing at the end! */
   if (NULL != vhash)
     {
-      gpc->have_vhash = GNUNET_YES;
-      gpc->vhash = *vhash;
+      gnc->have_vhash = GNUNET_YES;
+      gnc->vhash = *vhash;
     }
-  gpc->off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
+  gnc->off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
   nc->prep = &get_next_prepare;
-  nc->prep_cls = gpc;
+  nc->prep_cls = gnc;
   sqlite_next_request (nc, GNUNET_NO);
 }
 
 
 /**
- * Execute statement that gets a row and call the iterator
+ * Execute statement that gets a row and call the callback
  * with the result.  Resets the statement afterwards.
  *
  * @param plugin the plugin
@@ -1419,7 +1386,11 @@ execute_get (struct Plugin *plugin,
       LOG_SQLITE (plugin, NULL,
                  GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 
                  "sqlite3_step");
-      (void) sqlite3_reset (stmt);
+      if (SQLITE_OK != sqlite3_reset (stmt))
+       LOG_SQLITE (plugin, NULL,
+                   GNUNET_ERROR_TYPE_ERROR |
+                   GNUNET_ERROR_TYPE_BULK,
+                   "sqlite3_reset");
       GNUNET_break (0);
       database_shutdown (plugin);
       database_setup (plugin->env->cfg,
@@ -1432,7 +1403,95 @@ execute_get (struct Plugin *plugin,
 
 
 /**
- * Get a random item for replication.  Returns a single, not expired, random item
+ * Context for 'repl_iter' function.
+ */
+struct ReplCtx
+{
+  
+  /**
+   * Plugin handle.
+   */
+  struct Plugin *plugin;
+  
+  /**
+   * Function to call for the result (or the NULL).
+   */
+  PluginIterator iter;
+  
+  /**
+   * Closure for iter.
+   */
+  void *iter_cls;
+};
+
+
+/**
+ * Wrapper for the iterator for 'sqlite_plugin_replication_get'.
+ * Decrements the replication counter and calls the original
+ * iterator.
+ *
+ * @param cls closure
+ * @param next_cls closure to pass to the "next" function.
+ * @param key key for the content
+ * @param size number of bytes in data
+ * @param data content stored
+ * @param type type of the content
+ * @param priority priority of the content
+ * @param anonymity anonymity-level for the content
+ * @param expiration expiration time for the content
+ * @param uid unique identifier for the datum;
+ *        maybe 0 if no unique identifier is available
+ *
+ * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
+ *         (continue on call to "next", of course),
+ *         GNUNET_NO to delete the item and continue (if supported)
+ */
+static int
+repl_iter (void *cls,
+          void *next_cls,
+          const GNUNET_HashCode *key,
+          uint32_t size,
+          const void *data,
+          enum GNUNET_BLOCK_Type type,
+          uint32_t priority,
+          uint32_t anonymity,
+          struct GNUNET_TIME_Absolute expiration, 
+          uint64_t uid)
+{
+  struct ReplCtx *rc = cls;
+  struct Plugin *plugin = rc->plugin;
+  int ret;
+
+  ret = rc->iter (rc->iter_cls,
+                 next_cls, key,
+                 size, data, 
+                 type, priority, anonymity, expiration,
+                 uid);
+  if (NULL != key)
+    {
+      sqlite3_bind_int64 (plugin->updRepl, 1, uid);
+      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");
+         return GNUNET_SYSERR;
+       }
+      if (SQLITE_OK != sqlite3_reset (plugin->delRow))
+       LOG_SQLITE (plugin, NULL,
+                   GNUNET_ERROR_TYPE_ERROR |
+                   GNUNET_ERROR_TYPE_BULK, "sqlite3_reset");
+    }
+  return ret;
+}
+
+
+/**
+ * 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 'iter' with all values ZERO or NULL if the datastore is empty.
@@ -1446,28 +1505,17 @@ sqlite_plugin_replication_get (void *cls,
                               PluginIterator iter, void *iter_cls)
 {
   struct Plugin *plugin = cls;
-  sqlite3_stmt *stmt;
-  struct GNUNET_TIME_Absolute now;
+  struct ReplCtx rc;
 
 #if DEBUG_SQLITE
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
                   "sqlite",
                   "Getting random block based on replication order.\n");
 #endif
-  stmt = plugin->selRepl;
-  now = GNUNET_TIME_absolute_get ();
-  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");
-      iter (iter_cls, NULL, NULL, 0, NULL, 0, 0, 0, 
-           GNUNET_TIME_UNIT_ZERO_ABS, 0);
-      return;
-    }
-  execute_get (plugin, stmt, iter, iter_cls);
+  rc.plugin = plugin;
+  rc.iter = iter;
+  rc.iter_cls = iter_cls;
+  execute_get (plugin, plugin->selRepl, &repl_iter, &rc);
 }
 
 
@@ -1524,7 +1572,8 @@ sqlite_plugin_drop (void *cls)
 
 
 /**
- * FIXME.
+ * Get an estimate of how much space the database is
+ * currently using.
  *
  * @param cls the 'struct Plugin'
  * @return the size of the database on disk (estimate)
@@ -1612,7 +1661,6 @@ libgnunet_plugin_datastore_sqlite_init (void *cls)
   api->expiration_get = &sqlite_plugin_expiration_get;
   api->update = &sqlite_plugin_update;
   api->iter_zero_anonymity = &sqlite_plugin_iter_zero_anonymity;
-  api->iter_all_now = &sqlite_plugin_iter_all_now;
   api->drop = &sqlite_plugin_drop;
   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
                    "sqlite", _("Sqlite database running\n"));