plugin_datastore_sqlite.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. /*
  2. * This file is part of GNUnet
  3. * Copyright (C) 2009, 2011, 2017 GNUnet e.V.
  4. *
  5. * GNUnet is free software: you can redistribute it and/or modify it
  6. * under the terms of the GNU Affero General Public License as published
  7. * by the Free Software Foundation, either version 3 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * GNUnet is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. SPDX-License-Identifier: AGPL3.0-or-later
  18. */
  19. /**
  20. * @file datastore/plugin_datastore_sqlite.c
  21. * @brief sqlite-based datastore backend
  22. * @author Christian Grothoff
  23. */
  24. #include "platform.h"
  25. #include "gnunet_datastore_plugin.h"
  26. #include "gnunet_sq_lib.h"
  27. #include <sqlite3.h>
  28. /**
  29. * We allocate items on the stack at times. To prevent a stack
  30. * overflow, we impose a limit on the maximum size for the data per
  31. * item. 64k should be enough.
  32. */
  33. #define MAX_ITEM_SIZE 65536
  34. /**
  35. * After how many ms "busy" should a DB operation fail for good?
  36. * A low value makes sure that we are more responsive to requests
  37. * (especially PUTs). A high value guarantees a higher success
  38. * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
  39. *
  40. * The default value of 250ms should ensure that users do not experience
  41. * huge latencies while at the same time allowing operations to succeed
  42. * with reasonable probability.
  43. */
  44. #define BUSY_TIMEOUT_MS 250
  45. /**
  46. * Log an error message at log-level 'level' that indicates
  47. * a failure of the command 'cmd' on file 'filename'
  48. * with the message given by strerror(errno).
  49. */
  50. #define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
  51. /**
  52. * Log an error message at log-level 'level' that indicates
  53. * a failure of the command 'cmd' on file 'filename'
  54. * with the message given by strerror(errno).
  55. */
  56. #define LOG_SQLITE_MSG(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)); GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
  57. /**
  58. * Context for all functions in this plugin.
  59. */
  60. struct Plugin
  61. {
  62. /**
  63. * Our execution environment.
  64. */
  65. struct GNUNET_DATASTORE_PluginEnvironment *env;
  66. /**
  67. * Database filename.
  68. */
  69. char *fn;
  70. /**
  71. * Native SQLite database handle.
  72. */
  73. sqlite3 *dbh;
  74. /**
  75. * Precompiled SQL for remove_key.
  76. */
  77. sqlite3_stmt *remove;
  78. /**
  79. * Precompiled SQL for deletion.
  80. */
  81. sqlite3_stmt *delRow;
  82. /**
  83. * Precompiled SQL for update.
  84. */
  85. sqlite3_stmt *update;
  86. /**
  87. * Get maximum repl value in database.
  88. */
  89. sqlite3_stmt *maxRepl;
  90. /**
  91. * Precompiled SQL for replication decrement.
  92. */
  93. sqlite3_stmt *updRepl;
  94. /**
  95. * Precompiled SQL for replication selection.
  96. */
  97. sqlite3_stmt *selRepl;
  98. /**
  99. * Precompiled SQL for expiration selection.
  100. */
  101. sqlite3_stmt *selExpi;
  102. /**
  103. * Precompiled SQL for expiration selection.
  104. */
  105. sqlite3_stmt *selZeroAnon;
  106. /**
  107. * Precompiled SQL for insertion.
  108. */
  109. sqlite3_stmt *insertContent;
  110. /**
  111. * Precompiled SQL for selection
  112. */
  113. sqlite3_stmt *get[8];
  114. /**
  115. * Should the database be dropped on shutdown?
  116. */
  117. int drop_on_shutdown;
  118. };
  119. /**
  120. * @brief Prepare a SQL statement
  121. *
  122. * @param dbh handle to the database
  123. * @param zSql SQL statement, UTF-8 encoded
  124. * @param ppStmt set to the prepared statement
  125. * @return 0 on success
  126. */
  127. static int
  128. sq_prepare (sqlite3 *dbh,
  129. const char *zSql,
  130. sqlite3_stmt **ppStmt)
  131. {
  132. char *dummy;
  133. int result;
  134. result = sqlite3_prepare_v2 (dbh,
  135. zSql,
  136. strlen (zSql),
  137. ppStmt,
  138. (const char **) &dummy);
  139. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
  140. "sqlite",
  141. "Prepared `%s' / %p: %d\n",
  142. zSql,
  143. *ppStmt,
  144. result);
  145. return result;
  146. }
  147. /**
  148. * Create our database indices.
  149. *
  150. * @param dbh handle to the database
  151. */
  152. static void
  153. create_indices (sqlite3 * dbh)
  154. {
  155. /* create indices */
  156. if (0 !=
  157. (SQLITE_OK !=
  158. sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_hash ON gn091 (hash)",
  159. NULL, NULL, NULL)) +
  160. (SQLITE_OK !=
  161. sqlite3_exec (dbh,
  162. "CREATE INDEX IF NOT EXISTS idx_anon_type ON gn091 (anonLevel ASC,type)",
  163. NULL, NULL, NULL)) +
  164. (SQLITE_OK !=
  165. sqlite3_exec (dbh,
  166. "CREATE INDEX IF NOT EXISTS idx_expire ON gn091 (expire ASC)",
  167. NULL, NULL, NULL)) +
  168. (SQLITE_OK !=
  169. sqlite3_exec (dbh,
  170. "CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn091 (repl,rvalue)",
  171. NULL, NULL, NULL)) )
  172. GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "sqlite",
  173. "Failed to create indices: %s\n", sqlite3_errmsg (dbh));
  174. }
  175. #if 0
  176. #define CHECK(a) GNUNET_break(a)
  177. #define ENULL NULL
  178. #else
  179. #define ENULL &e
  180. #define ENULL_DEFINED 1
  181. #define CHECK(a) if (! (a)) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
  182. #endif
  183. /**
  184. * Initialize the database connections and associated
  185. * data structures (create tables and indices
  186. * as needed as well).
  187. *
  188. * @param cfg our configuration
  189. * @param plugin the plugin context (state for this module)
  190. * @return #GNUNET_OK on success
  191. */
  192. static int
  193. database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
  194. struct Plugin *plugin)
  195. {
  196. sqlite3_stmt *stmt;
  197. char *afsdir;
  198. #if ENULL_DEFINED
  199. char *e;
  200. #endif
  201. if (GNUNET_OK !=
  202. GNUNET_CONFIGURATION_get_value_filename (cfg,
  203. "datastore-sqlite",
  204. "FILENAME",
  205. &afsdir))
  206. {
  207. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  208. "datastore-sqlite",
  209. "FILENAME");
  210. return GNUNET_SYSERR;
  211. }
  212. if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
  213. {
  214. if (GNUNET_OK !=
  215. GNUNET_DISK_directory_create_for_file (afsdir))
  216. {
  217. GNUNET_break (0);
  218. GNUNET_free (afsdir);
  219. return GNUNET_SYSERR;
  220. }
  221. /* database is new or got deleted, reset payload to zero! */
  222. if (NULL != plugin->env->duc)
  223. plugin->env->duc (plugin->env->cls,
  224. 0);
  225. }
  226. /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
  227. plugin->fn = afsdir;
  228. /* Open database and precompile statements */
  229. if (SQLITE_OK !=
  230. sqlite3_open (plugin->fn, &plugin->dbh))
  231. {
  232. GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "sqlite",
  233. _("Unable to initialize SQLite: %s.\n"),
  234. sqlite3_errmsg (plugin->dbh));
  235. return GNUNET_SYSERR;
  236. }
  237. CHECK (SQLITE_OK ==
  238. sqlite3_exec (plugin->dbh,
  239. "PRAGMA temp_store=MEMORY", NULL, NULL,
  240. ENULL));
  241. CHECK (SQLITE_OK ==
  242. sqlite3_exec (plugin->dbh,
  243. "PRAGMA synchronous=OFF", NULL, NULL,
  244. ENULL));
  245. CHECK (SQLITE_OK ==
  246. sqlite3_exec (plugin->dbh,
  247. "PRAGMA legacy_file_format=OFF", NULL, NULL,
  248. ENULL));
  249. CHECK (SQLITE_OK ==
  250. sqlite3_exec (plugin->dbh,
  251. "PRAGMA auto_vacuum=INCREMENTAL", NULL,
  252. NULL, ENULL));
  253. CHECK (SQLITE_OK ==
  254. sqlite3_exec (plugin->dbh,
  255. "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
  256. ENULL));
  257. CHECK (SQLITE_OK ==
  258. sqlite3_exec (plugin->dbh,
  259. "PRAGMA page_size=4096", NULL, NULL,
  260. ENULL));
  261. CHECK (SQLITE_OK ==
  262. sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
  263. /* We have to do it here, because otherwise precompiling SQL might fail */
  264. CHECK (SQLITE_OK ==
  265. sq_prepare (plugin->dbh,
  266. "SELECT 1 FROM sqlite_master WHERE tbl_name = 'gn091'",
  267. &stmt));
  268. /* FIXME: SQLite does not have unsigned integers! This is ok for the type column because
  269. * we only test equality on it and can cast it to/from uint32_t. For repl, prio, and anonLevel
  270. * we do math or inequality tests, so we can't handle the entire range of uint32_t.
  271. * This will also cause problems for expiration times after 294247-01-10-04:00:54 UTC.
  272. */
  273. if ( (SQLITE_DONE ==
  274. sqlite3_step (stmt)) &&
  275. (SQLITE_OK !=
  276. sqlite3_exec (plugin->dbh,
  277. "CREATE TABLE gn091 ("
  278. " repl INT4 NOT NULL DEFAULT 0,"
  279. " type INT4 NOT NULL DEFAULT 0,"
  280. " prio INT4 NOT NULL DEFAULT 0,"
  281. " anonLevel INT4 NOT NULL DEFAULT 0,"
  282. " expire INT8 NOT NULL DEFAULT 0,"
  283. " rvalue INT8 NOT NULL,"
  284. " hash TEXT NOT NULL DEFAULT '',"
  285. " vhash TEXT NOT NULL DEFAULT '',"
  286. " value BLOB NOT NULL DEFAULT '')",
  287. NULL,
  288. NULL,
  289. NULL)) )
  290. {
  291. LOG_SQLITE (plugin,
  292. GNUNET_ERROR_TYPE_ERROR,
  293. "sqlite3_exec");
  294. sqlite3_finalize (stmt);
  295. return GNUNET_SYSERR;
  296. }
  297. sqlite3_finalize (stmt);
  298. create_indices (plugin->dbh);
  299. #define RESULT_COLUMNS "repl, type, prio, anonLevel, expire, hash, value, _ROWID_"
  300. if ( (SQLITE_OK !=
  301. sq_prepare (plugin->dbh,
  302. "UPDATE gn091 "
  303. "SET prio = prio + ?, "
  304. "repl = repl + ?, "
  305. "expire = MAX(expire, ?) "
  306. "WHERE hash = ? AND vhash = ?",
  307. &plugin->update)) ||
  308. (SQLITE_OK !=
  309. sq_prepare (plugin->dbh,
  310. "UPDATE gn091 " "SET repl = MAX (0, repl - 1) WHERE _ROWID_ = ?",
  311. &plugin->updRepl)) ||
  312. (SQLITE_OK !=
  313. sq_prepare (plugin->dbh,
  314. "SELECT " RESULT_COLUMNS " FROM gn091 "
  315. "WHERE repl=?2 AND " " (rvalue>=?1 OR "
  316. " NOT EXISTS (SELECT 1 FROM gn091 "
  317. "WHERE repl=?2 AND rvalue>=?1 LIMIT 1) ) "
  318. "ORDER BY rvalue ASC LIMIT 1",
  319. &plugin->selRepl)) ||
  320. (SQLITE_OK !=
  321. sq_prepare (plugin->dbh,
  322. "SELECT MAX(repl) FROM gn091",
  323. &plugin->maxRepl)) ||
  324. (SQLITE_OK !=
  325. sq_prepare (plugin->dbh,
  326. "SELECT " RESULT_COLUMNS " FROM gn091 "
  327. "WHERE NOT EXISTS (SELECT 1 FROM gn091 WHERE expire < ?1 LIMIT 1) OR (expire < ?1) "
  328. "ORDER BY expire ASC LIMIT 1",
  329. &plugin->selExpi)) ||
  330. (SQLITE_OK !=
  331. sq_prepare (plugin->dbh,
  332. "SELECT " RESULT_COLUMNS " FROM gn091 "
  333. "WHERE _ROWID_ >= ? AND "
  334. "anonLevel = 0 AND "
  335. "type = ? "
  336. "ORDER BY _ROWID_ ASC LIMIT 1",
  337. &plugin->selZeroAnon)) ||
  338. (SQLITE_OK !=
  339. sq_prepare (plugin->dbh,
  340. "INSERT INTO gn091 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
  341. "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
  342. &plugin->insertContent)) ||
  343. (SQLITE_OK !=
  344. sq_prepare (plugin->dbh,
  345. "SELECT " RESULT_COLUMNS " FROM gn091 "
  346. "WHERE _ROWID_ >= ?1 "
  347. "ORDER BY _ROWID_ ASC LIMIT 1",
  348. &plugin->get[0])) ||
  349. (SQLITE_OK !=
  350. sq_prepare (plugin->dbh,
  351. "SELECT " RESULT_COLUMNS " FROM gn091 "
  352. "WHERE _ROWID_ >= ?1 AND "
  353. "type = ?4 "
  354. "ORDER BY _ROWID_ ASC LIMIT 1",
  355. &plugin->get[1])) ||
  356. (SQLITE_OK !=
  357. sq_prepare (plugin->dbh,
  358. "SELECT " RESULT_COLUMNS " FROM gn091 "
  359. "WHERE _ROWID_ >= ?1 AND "
  360. "hash = ?3 "
  361. "ORDER BY _ROWID_ ASC LIMIT 1",
  362. &plugin->get[2])) ||
  363. (SQLITE_OK !=
  364. sq_prepare (plugin->dbh,
  365. "SELECT " RESULT_COLUMNS " FROM gn091 "
  366. "WHERE _ROWID_ >= ?1 AND "
  367. "hash = ?3 AND "
  368. "type = ?4 "
  369. "ORDER BY _ROWID_ ASC LIMIT 1",
  370. &plugin->get[3])) ||
  371. (SQLITE_OK !=
  372. sq_prepare (plugin->dbh,
  373. "SELECT " RESULT_COLUMNS " FROM gn091 "
  374. "WHERE _ROWID_ >= ?1 AND "
  375. "rvalue >= ?2 "
  376. "ORDER BY _ROWID_ ASC LIMIT 1",
  377. &plugin->get[4])) ||
  378. (SQLITE_OK !=
  379. sq_prepare (plugin->dbh,
  380. "SELECT " RESULT_COLUMNS " FROM gn091 "
  381. "WHERE _ROWID_ >= ?1 AND "
  382. "rvalue >= ?2 AND "
  383. "type = ?4 "
  384. "ORDER BY _ROWID_ ASC LIMIT 1",
  385. &plugin->get[5])) ||
  386. (SQLITE_OK !=
  387. sq_prepare (plugin->dbh,
  388. "SELECT " RESULT_COLUMNS " FROM gn091 "
  389. "WHERE _ROWID_ >= ?1 AND "
  390. "rvalue >= ?2 AND "
  391. "hash = ?3 "
  392. "ORDER BY _ROWID_ ASC LIMIT 1",
  393. &plugin->get[6])) ||
  394. (SQLITE_OK !=
  395. sq_prepare (plugin->dbh,
  396. "SELECT " RESULT_COLUMNS " FROM gn091 "
  397. "WHERE _ROWID_ >= ?1 AND "
  398. "rvalue >= ?2 AND "
  399. "hash = ?3 AND "
  400. "type = ?4 "
  401. "ORDER BY _ROWID_ ASC LIMIT 1",
  402. &plugin->get[7])) ||
  403. (SQLITE_OK !=
  404. sq_prepare (plugin->dbh,
  405. "DELETE FROM gn091 WHERE _ROWID_ = ?",
  406. &plugin->delRow)) ||
  407. (SQLITE_OK !=
  408. sq_prepare (plugin->dbh,
  409. "DELETE FROM gn091 "
  410. "WHERE hash = ? AND "
  411. "value = ? ",
  412. &plugin->remove)) ||
  413. false)
  414. {
  415. LOG_SQLITE (plugin,
  416. GNUNET_ERROR_TYPE_ERROR,
  417. "precompiling");
  418. return GNUNET_SYSERR;
  419. }
  420. return GNUNET_OK;
  421. }
  422. /**
  423. * Shutdown database connection and associate data
  424. * structures.
  425. *
  426. * @param plugin the plugin context (state for this module)
  427. */
  428. static void
  429. database_shutdown (struct Plugin *plugin)
  430. {
  431. int result;
  432. #if SQLITE_VERSION_NUMBER >= 3007000
  433. sqlite3_stmt *stmt;
  434. #endif
  435. if (NULL != plugin->remove)
  436. sqlite3_finalize (plugin->remove);
  437. if (NULL != plugin->delRow)
  438. sqlite3_finalize (plugin->delRow);
  439. if (NULL != plugin->update)
  440. sqlite3_finalize (plugin->update);
  441. if (NULL != plugin->updRepl)
  442. sqlite3_finalize (plugin->updRepl);
  443. if (NULL != plugin->selRepl)
  444. sqlite3_finalize (plugin->selRepl);
  445. if (NULL != plugin->maxRepl)
  446. sqlite3_finalize (plugin->maxRepl);
  447. if (NULL != plugin->selExpi)
  448. sqlite3_finalize (plugin->selExpi);
  449. if (NULL != plugin->selZeroAnon)
  450. sqlite3_finalize (plugin->selZeroAnon);
  451. if (NULL != plugin->insertContent)
  452. sqlite3_finalize (plugin->insertContent);
  453. for (int i = 0; i < 8; ++i)
  454. if (NULL != plugin->get[i])
  455. sqlite3_finalize (plugin->get[i]);
  456. result = sqlite3_close (plugin->dbh);
  457. #if SQLITE_VERSION_NUMBER >= 3007000
  458. if (result == SQLITE_BUSY)
  459. {
  460. GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
  461. "sqlite",
  462. _("Tried to close sqlite without finalizing all prepared statements.\n"));
  463. stmt = sqlite3_next_stmt (plugin->dbh,
  464. NULL);
  465. while (NULL != stmt)
  466. {
  467. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
  468. "sqlite",
  469. "Closing statement %p\n",
  470. stmt);
  471. result = sqlite3_finalize (stmt);
  472. if (result != SQLITE_OK)
  473. GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
  474. "sqlite",
  475. "Failed to close statement %p: %d\n",
  476. stmt,
  477. result);
  478. stmt = sqlite3_next_stmt (plugin->dbh,
  479. NULL);
  480. }
  481. result = sqlite3_close (plugin->dbh);
  482. }
  483. #endif
  484. if (SQLITE_OK != result)
  485. LOG_SQLITE (plugin,
  486. GNUNET_ERROR_TYPE_ERROR,
  487. "sqlite3_close");
  488. GNUNET_free_non_null (plugin->fn);
  489. }
  490. /**
  491. * Delete the database entry with the given
  492. * row identifier.
  493. *
  494. * @param plugin the plugin context (state for this module)
  495. * @param rid the ID of the row to delete
  496. */
  497. static int
  498. delete_by_rowid (struct Plugin *plugin,
  499. uint64_t rid)
  500. {
  501. struct GNUNET_SQ_QueryParam params[] = {
  502. GNUNET_SQ_query_param_uint64 (&rid),
  503. GNUNET_SQ_query_param_end
  504. };
  505. if (GNUNET_OK !=
  506. GNUNET_SQ_bind (plugin->delRow,
  507. params))
  508. return GNUNET_SYSERR;
  509. if (SQLITE_DONE != sqlite3_step (plugin->delRow))
  510. {
  511. LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  512. "sqlite3_step");
  513. GNUNET_SQ_reset (plugin->dbh,
  514. plugin->delRow);
  515. return GNUNET_SYSERR;
  516. }
  517. GNUNET_SQ_reset (plugin->dbh,
  518. plugin->delRow);
  519. return GNUNET_OK;
  520. }
  521. /**
  522. * Store an item in the datastore.
  523. *
  524. * @param cls closure
  525. * @param key key for the item
  526. * @param absent true if the key was not found in the bloom filter
  527. * @param size number of bytes in @a data
  528. * @param data content stored
  529. * @param type type of the content
  530. * @param priority priority of the content
  531. * @param anonymity anonymity-level for the content
  532. * @param replication replication-level for the content
  533. * @param expiration expiration time for the content
  534. * @param cont continuation called with success or failure status
  535. * @param cont_cls continuation closure
  536. */
  537. static void
  538. sqlite_plugin_put (void *cls,
  539. const struct GNUNET_HashCode *key,
  540. bool absent,
  541. uint32_t size,
  542. const void *data,
  543. enum GNUNET_BLOCK_Type type,
  544. uint32_t priority,
  545. uint32_t anonymity,
  546. uint32_t replication,
  547. struct GNUNET_TIME_Absolute expiration,
  548. PluginPutCont cont,
  549. void *cont_cls)
  550. {
  551. struct Plugin *plugin = cls;
  552. struct GNUNET_HashCode vhash;
  553. char *msg = NULL;
  554. GNUNET_CRYPTO_hash (data,
  555. size,
  556. &vhash);
  557. if (!absent)
  558. {
  559. struct GNUNET_SQ_QueryParam params[] = {
  560. GNUNET_SQ_query_param_uint32 (&priority),
  561. GNUNET_SQ_query_param_uint32 (&replication),
  562. GNUNET_SQ_query_param_absolute_time (&expiration),
  563. GNUNET_SQ_query_param_auto_from_type (key),
  564. GNUNET_SQ_query_param_auto_from_type (&vhash),
  565. GNUNET_SQ_query_param_end
  566. };
  567. if (GNUNET_OK !=
  568. GNUNET_SQ_bind (plugin->update,
  569. params))
  570. {
  571. cont (cont_cls,
  572. key,
  573. size,
  574. GNUNET_SYSERR,
  575. _("sqlite bind failure"));
  576. return;
  577. }
  578. if (SQLITE_DONE != sqlite3_step (plugin->update))
  579. {
  580. LOG_SQLITE_MSG (plugin, &msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  581. "sqlite3_step");
  582. cont (cont_cls,
  583. key,
  584. size,
  585. GNUNET_SYSERR,
  586. msg);
  587. GNUNET_free_non_null (msg);
  588. return;
  589. }
  590. int changes = sqlite3_changes (plugin->dbh);
  591. GNUNET_SQ_reset (plugin->dbh,
  592. plugin->update);
  593. if (0 != changes)
  594. {
  595. cont (cont_cls,
  596. key,
  597. size,
  598. GNUNET_NO,
  599. NULL);
  600. return;
  601. }
  602. }
  603. uint64_t rvalue;
  604. uint32_t type32 = (uint32_t) type;
  605. struct GNUNET_SQ_QueryParam params[] = {
  606. GNUNET_SQ_query_param_uint32 (&replication),
  607. GNUNET_SQ_query_param_uint32 (&type32),
  608. GNUNET_SQ_query_param_uint32 (&priority),
  609. GNUNET_SQ_query_param_uint32 (&anonymity),
  610. GNUNET_SQ_query_param_absolute_time (&expiration),
  611. GNUNET_SQ_query_param_uint64 (&rvalue),
  612. GNUNET_SQ_query_param_auto_from_type (key),
  613. GNUNET_SQ_query_param_auto_from_type (&vhash),
  614. GNUNET_SQ_query_param_fixed_size (data, size),
  615. GNUNET_SQ_query_param_end
  616. };
  617. int n;
  618. int ret;
  619. sqlite3_stmt *stmt;
  620. if (size > MAX_ITEM_SIZE)
  621. {
  622. cont (cont_cls, key, size, GNUNET_SYSERR, _("Data too large"));
  623. return;
  624. }
  625. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite",
  626. "Storing in database block with type %u/key `%s'/priority %u/expiration in %s (%s).\n",
  627. type,
  628. GNUNET_h2s (key),
  629. priority,
  630. GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (expiration),
  631. GNUNET_YES),
  632. GNUNET_STRINGS_absolute_time_to_string (expiration));
  633. stmt = plugin->insertContent;
  634. rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
  635. if (GNUNET_OK !=
  636. GNUNET_SQ_bind (stmt,
  637. params))
  638. {
  639. cont (cont_cls, key, size, GNUNET_SYSERR, NULL);
  640. return;
  641. }
  642. n = sqlite3_step (stmt);
  643. switch (n)
  644. {
  645. case SQLITE_DONE:
  646. if (NULL != plugin->env->duc)
  647. plugin->env->duc (plugin->env->cls,
  648. size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
  649. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite",
  650. "Stored new entry (%u bytes)\n",
  651. size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
  652. ret = GNUNET_OK;
  653. break;
  654. case SQLITE_BUSY:
  655. GNUNET_break (0);
  656. LOG_SQLITE_MSG (plugin,
  657. &msg,
  658. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  659. "sqlite3_step");
  660. ret = GNUNET_SYSERR;
  661. break;
  662. default:
  663. LOG_SQLITE_MSG (plugin,
  664. &msg,
  665. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  666. "sqlite3_step");
  667. GNUNET_SQ_reset (plugin->dbh,
  668. stmt);
  669. database_shutdown (plugin);
  670. database_setup (plugin->env->cfg, plugin);
  671. cont (cont_cls, key, size, GNUNET_SYSERR, msg);
  672. GNUNET_free_non_null(msg);
  673. return;
  674. }
  675. GNUNET_SQ_reset (plugin->dbh,
  676. stmt);
  677. cont (cont_cls, key, size, ret, msg);
  678. GNUNET_free_non_null(msg);
  679. }
  680. /**
  681. * Execute statement that gets a row and call the callback
  682. * with the result. Resets the statement afterwards.
  683. *
  684. * @param plugin the plugin
  685. * @param stmt the statement
  686. * @param proc processor to call
  687. * @param proc_cls closure for @a proc
  688. */
  689. static void
  690. execute_get (struct Plugin *plugin,
  691. sqlite3_stmt *stmt,
  692. PluginDatumProcessor proc,
  693. void *proc_cls)
  694. {
  695. int n;
  696. struct GNUNET_TIME_Absolute expiration;
  697. uint32_t replication;
  698. uint32_t type;
  699. uint32_t priority;
  700. uint32_t anonymity;
  701. uint64_t rowid;
  702. void *value;
  703. size_t value_size;
  704. struct GNUNET_HashCode key;
  705. int ret;
  706. struct GNUNET_SQ_ResultSpec rs[] = {
  707. GNUNET_SQ_result_spec_uint32 (&replication),
  708. GNUNET_SQ_result_spec_uint32 (&type),
  709. GNUNET_SQ_result_spec_uint32 (&priority),
  710. GNUNET_SQ_result_spec_uint32 (&anonymity),
  711. GNUNET_SQ_result_spec_absolute_time (&expiration),
  712. GNUNET_SQ_result_spec_auto_from_type (&key),
  713. GNUNET_SQ_result_spec_variable_size (&value,
  714. &value_size),
  715. GNUNET_SQ_result_spec_uint64 (&rowid),
  716. GNUNET_SQ_result_spec_end
  717. };
  718. n = sqlite3_step (stmt);
  719. switch (n)
  720. {
  721. case SQLITE_ROW:
  722. if (GNUNET_OK !=
  723. GNUNET_SQ_extract_result (stmt,
  724. rs))
  725. {
  726. GNUNET_break (0);
  727. break;
  728. }
  729. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
  730. "sqlite",
  731. "Found reply in database with expiration %s\n",
  732. GNUNET_STRINGS_absolute_time_to_string (expiration));
  733. ret = proc (proc_cls,
  734. &key,
  735. value_size,
  736. value,
  737. type,
  738. priority,
  739. anonymity,
  740. replication,
  741. expiration,
  742. rowid);
  743. GNUNET_SQ_cleanup_result (rs);
  744. GNUNET_SQ_reset (plugin->dbh,
  745. stmt);
  746. if ( (GNUNET_NO == ret) &&
  747. (GNUNET_OK == delete_by_rowid (plugin,
  748. rowid)) &&
  749. (NULL != plugin->env->duc) )
  750. plugin->env->duc (plugin->env->cls,
  751. -(value_size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
  752. return;
  753. case SQLITE_DONE:
  754. /* database must be empty */
  755. break;
  756. case SQLITE_BUSY:
  757. case SQLITE_ERROR:
  758. case SQLITE_MISUSE:
  759. default:
  760. LOG_SQLITE (plugin,
  761. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  762. "sqlite3_step");
  763. if (SQLITE_OK !=
  764. sqlite3_reset (stmt))
  765. LOG_SQLITE (plugin,
  766. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  767. "sqlite3_reset");
  768. GNUNET_break (0);
  769. proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
  770. database_shutdown (plugin);
  771. database_setup (plugin->env->cfg,
  772. plugin);
  773. return;
  774. }
  775. GNUNET_SQ_reset (plugin->dbh,
  776. stmt);
  777. proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
  778. }
  779. /**
  780. * Select a subset of the items in the datastore and call
  781. * the given processor for the item.
  782. *
  783. * @param cls our plugin context
  784. * @param next_uid return the result with lowest uid >= next_uid
  785. * @param type entries of which type should be considered?
  786. * Must not be zero (ANY).
  787. * @param proc function to call on the matching value;
  788. * will be called with NULL if no value matches
  789. * @param proc_cls closure for @a proc
  790. */
  791. static void
  792. sqlite_plugin_get_zero_anonymity (void *cls,
  793. uint64_t next_uid,
  794. enum GNUNET_BLOCK_Type type,
  795. PluginDatumProcessor proc,
  796. void *proc_cls)
  797. {
  798. struct Plugin *plugin = cls;
  799. uint32_t type32 = type;
  800. struct GNUNET_SQ_QueryParam params[] = {
  801. GNUNET_SQ_query_param_uint64 (&next_uid),
  802. GNUNET_SQ_query_param_uint32 (&type32),
  803. GNUNET_SQ_query_param_end
  804. };
  805. GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
  806. if (GNUNET_OK !=
  807. GNUNET_SQ_bind (plugin->selZeroAnon,
  808. params))
  809. {
  810. proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
  811. return;
  812. }
  813. execute_get (plugin, plugin->selZeroAnon, proc, proc_cls);
  814. }
  815. /**
  816. * Get results for a particular key in the datastore.
  817. *
  818. * @param cls closure
  819. * @param next_uid return the result with lowest uid >= next_uid
  820. * @param random if true, return a random result instead of using next_uid
  821. * @param key maybe NULL (to match all entries)
  822. * @param type entries of which type are relevant?
  823. * Use 0 for any type.
  824. * @param proc function to call on the matching value;
  825. * will be called with NULL if nothing matches
  826. * @param proc_cls closure for @a proc
  827. */
  828. static void
  829. sqlite_plugin_get_key (void *cls,
  830. uint64_t next_uid,
  831. bool random,
  832. const struct GNUNET_HashCode *key,
  833. enum GNUNET_BLOCK_Type type,
  834. PluginDatumProcessor proc,
  835. void *proc_cls)
  836. {
  837. struct Plugin *plugin = cls;
  838. uint64_t rvalue;
  839. int use_rvalue = random;
  840. uint32_t type32 = (uint32_t) type;
  841. int use_type = GNUNET_BLOCK_TYPE_ANY != type;
  842. int use_key = NULL != key;
  843. sqlite3_stmt *stmt = plugin->get[use_rvalue * 4 + use_key * 2 + use_type];
  844. struct GNUNET_SQ_QueryParam params[] = {
  845. GNUNET_SQ_query_param_uint64 (&next_uid),
  846. GNUNET_SQ_query_param_uint64 (&rvalue),
  847. GNUNET_SQ_query_param_auto_from_type (key),
  848. GNUNET_SQ_query_param_uint32 (&type32),
  849. GNUNET_SQ_query_param_end
  850. };
  851. /* SQLite doesn't like it when you try to bind a parameter greater than the
  852. * last numbered parameter, but unused parameters in the middle are OK.
  853. */
  854. if (! use_type)
  855. {
  856. params[3] = (struct GNUNET_SQ_QueryParam) GNUNET_SQ_query_param_end;
  857. if (! use_key)
  858. {
  859. params[2] = (struct GNUNET_SQ_QueryParam) GNUNET_SQ_query_param_end;
  860. if (! use_rvalue)
  861. params[1] = (struct GNUNET_SQ_QueryParam) GNUNET_SQ_query_param_end;
  862. }
  863. }
  864. if (random)
  865. {
  866. rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
  867. UINT64_MAX);
  868. next_uid = 0;
  869. }
  870. else
  871. rvalue = 0;
  872. if (GNUNET_OK !=
  873. GNUNET_SQ_bind (stmt,
  874. params))
  875. {
  876. proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
  877. return;
  878. }
  879. execute_get (plugin,
  880. stmt,
  881. proc,
  882. proc_cls);
  883. }
  884. /**
  885. * Context for #repl_proc() function.
  886. */
  887. struct ReplCtx
  888. {
  889. /**
  890. * Function to call for the result (or the NULL).
  891. */
  892. PluginDatumProcessor proc;
  893. /**
  894. * Closure for @e proc.
  895. */
  896. void *proc_cls;
  897. /**
  898. * UID to use.
  899. */
  900. uint64_t uid;
  901. /**
  902. * Yes if UID was set.
  903. */
  904. int have_uid;
  905. };
  906. /**
  907. * Wrapper for the processor for #sqlite_plugin_get_replication().
  908. * Decrements the replication counter and calls the original
  909. * processor.
  910. *
  911. * @param cls closure
  912. * @param key key for the content
  913. * @param size number of bytes in @a data
  914. * @param data content stored
  915. * @param type type of the content
  916. * @param priority priority of the content
  917. * @param anonymity anonymity-level for the content
  918. * @param replication replication-level for the content
  919. * @param expiration expiration time for the content
  920. * @param uid unique identifier for the datum;
  921. * maybe 0 if no unique identifier is available
  922. * @return #GNUNET_OK for normal return,
  923. * #GNUNET_NO to delete the item
  924. */
  925. static int
  926. repl_proc (void *cls,
  927. const struct GNUNET_HashCode *key,
  928. uint32_t size,
  929. const void *data,
  930. enum GNUNET_BLOCK_Type type,
  931. uint32_t priority,
  932. uint32_t anonymity,
  933. uint32_t replication,
  934. struct GNUNET_TIME_Absolute expiration,
  935. uint64_t uid)
  936. {
  937. struct ReplCtx *rc = cls;
  938. int ret;
  939. if (GNUNET_SYSERR == rc->have_uid)
  940. rc->have_uid = GNUNET_NO;
  941. ret = rc->proc (rc->proc_cls,
  942. key,
  943. size,
  944. data,
  945. type,
  946. priority,
  947. anonymity,
  948. replication,
  949. expiration,
  950. uid);
  951. if (NULL != key)
  952. {
  953. rc->uid = uid;
  954. rc->have_uid = GNUNET_YES;
  955. }
  956. return ret;
  957. }
  958. /**
  959. * Get a random item for replication. Returns a single random item
  960. * from those with the highest replication counters. The item's
  961. * replication counter is decremented by one IF it was positive before.
  962. * Call @a proc with all values ZERO or NULL if the datastore is empty.
  963. *
  964. * @param cls closure
  965. * @param proc function to call the value (once only).
  966. * @param proc_cls closure for @a proc
  967. */
  968. static void
  969. sqlite_plugin_get_replication (void *cls,
  970. PluginDatumProcessor proc,
  971. void *proc_cls)
  972. {
  973. struct Plugin *plugin = cls;
  974. struct ReplCtx rc;
  975. uint64_t rvalue;
  976. uint32_t repl;
  977. struct GNUNET_SQ_QueryParam params_sel_repl[] = {
  978. GNUNET_SQ_query_param_uint64 (&rvalue),
  979. GNUNET_SQ_query_param_uint32 (&repl),
  980. GNUNET_SQ_query_param_end
  981. };
  982. struct GNUNET_SQ_QueryParam params_upd_repl[] = {
  983. GNUNET_SQ_query_param_uint64 (&rc.uid),
  984. GNUNET_SQ_query_param_end
  985. };
  986. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
  987. "datastore-sqlite",
  988. "Getting random block based on replication order.\n");
  989. if (SQLITE_ROW !=
  990. sqlite3_step (plugin->maxRepl))
  991. {
  992. GNUNET_SQ_reset (plugin->dbh,
  993. plugin->maxRepl);
  994. /* DB empty */
  995. proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
  996. return;
  997. }
  998. repl = sqlite3_column_int (plugin->maxRepl,
  999. 0);
  1000. GNUNET_SQ_reset (plugin->dbh,
  1001. plugin->maxRepl);
  1002. rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
  1003. UINT64_MAX);
  1004. if (GNUNET_OK !=
  1005. GNUNET_SQ_bind (plugin->selRepl,
  1006. params_sel_repl))
  1007. {
  1008. proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
  1009. return;
  1010. }
  1011. rc.have_uid = GNUNET_SYSERR;
  1012. rc.proc = proc;
  1013. rc.proc_cls = proc_cls;
  1014. execute_get (plugin,
  1015. plugin->selRepl,
  1016. &repl_proc,
  1017. &rc);
  1018. if (GNUNET_YES == rc.have_uid)
  1019. {
  1020. if (GNUNET_OK !=
  1021. GNUNET_SQ_bind (plugin->updRepl,
  1022. params_upd_repl))
  1023. {
  1024. proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
  1025. return;
  1026. }
  1027. if (SQLITE_DONE !=
  1028. sqlite3_step (plugin->updRepl))
  1029. LOG_SQLITE (plugin,
  1030. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  1031. "sqlite3_step");
  1032. GNUNET_SQ_reset (plugin->dbh,
  1033. plugin->updRepl);
  1034. }
  1035. if (GNUNET_SYSERR == rc.have_uid)
  1036. {
  1037. /* proc was not called at all so far, do it now. */
  1038. proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
  1039. }
  1040. }
  1041. /**
  1042. * Get a random item that has expired or has low priority.
  1043. * Call @a proc with all values ZERO or NULL if the datastore is empty.
  1044. *
  1045. * @param cls closure
  1046. * @param proc function to call the value (once only).
  1047. * @param proc_cls closure for @a proc
  1048. */
  1049. static void
  1050. sqlite_plugin_get_expiration (void *cls, PluginDatumProcessor proc,
  1051. void *proc_cls)
  1052. {
  1053. struct Plugin *plugin = cls;
  1054. sqlite3_stmt *stmt;
  1055. struct GNUNET_TIME_Absolute now;
  1056. struct GNUNET_SQ_QueryParam params[] = {
  1057. GNUNET_SQ_query_param_absolute_time (&now),
  1058. GNUNET_SQ_query_param_end
  1059. };
  1060. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
  1061. "sqlite",
  1062. "Getting random block based on expiration and priority order.\n");
  1063. now = GNUNET_TIME_absolute_get ();
  1064. stmt = plugin->selExpi;
  1065. if (GNUNET_OK !=
  1066. GNUNET_SQ_bind (stmt,
  1067. params))
  1068. {
  1069. proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
  1070. return;
  1071. }
  1072. execute_get (plugin, stmt, proc, proc_cls);
  1073. }
  1074. /**
  1075. * Get all of the keys in the datastore.
  1076. *
  1077. * @param cls closure
  1078. * @param proc function to call on each key
  1079. * @param proc_cls closure for @a proc
  1080. */
  1081. static void
  1082. sqlite_plugin_get_keys (void *cls,
  1083. PluginKeyProcessor proc,
  1084. void *proc_cls)
  1085. {
  1086. struct Plugin *plugin = cls;
  1087. struct GNUNET_HashCode key;
  1088. struct GNUNET_SQ_ResultSpec results[] = {
  1089. GNUNET_SQ_result_spec_auto_from_type (&key),
  1090. GNUNET_SQ_result_spec_end
  1091. };
  1092. sqlite3_stmt *stmt;
  1093. int ret;
  1094. GNUNET_assert (NULL != proc);
  1095. if (SQLITE_OK !=
  1096. sq_prepare (plugin->dbh,
  1097. "SELECT hash FROM gn091",
  1098. &stmt))
  1099. {
  1100. LOG_SQLITE (plugin,
  1101. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  1102. "sqlite_prepare");
  1103. proc (proc_cls,
  1104. NULL,
  1105. 0);
  1106. return;
  1107. }
  1108. while (SQLITE_ROW == (ret = sqlite3_step (stmt)))
  1109. {
  1110. if (GNUNET_OK ==
  1111. GNUNET_SQ_extract_result (stmt,
  1112. results))
  1113. proc (proc_cls,
  1114. &key,
  1115. 1);
  1116. else
  1117. GNUNET_break (0);
  1118. }
  1119. if (SQLITE_DONE != ret)
  1120. LOG_SQLITE (plugin,
  1121. GNUNET_ERROR_TYPE_ERROR,
  1122. "sqlite_step");
  1123. sqlite3_finalize (stmt);
  1124. proc (proc_cls,
  1125. NULL,
  1126. 0);
  1127. }
  1128. /**
  1129. * Drop database.
  1130. *
  1131. * @param cls our plugin context
  1132. */
  1133. static void
  1134. sqlite_plugin_drop (void *cls)
  1135. {
  1136. struct Plugin *plugin = cls;
  1137. plugin->drop_on_shutdown = GNUNET_YES;
  1138. }
  1139. /**
  1140. * Remove a particular key in the datastore.
  1141. *
  1142. * @param cls closure
  1143. * @param key key for the content
  1144. * @param size number of bytes in data
  1145. * @param data content stored
  1146. * @param cont continuation called with success or failure status
  1147. * @param cont_cls continuation closure for @a cont
  1148. */
  1149. static void
  1150. sqlite_plugin_remove_key (void *cls,
  1151. const struct GNUNET_HashCode *key,
  1152. uint32_t size,
  1153. const void *data,
  1154. PluginRemoveCont cont,
  1155. void *cont_cls)
  1156. {
  1157. struct Plugin *plugin = cls;
  1158. struct GNUNET_SQ_QueryParam params[] = {
  1159. GNUNET_SQ_query_param_auto_from_type (key),
  1160. GNUNET_SQ_query_param_fixed_size (data, size),
  1161. GNUNET_SQ_query_param_end
  1162. };
  1163. if (GNUNET_OK !=
  1164. GNUNET_SQ_bind (plugin->remove,
  1165. params))
  1166. {
  1167. cont (cont_cls,
  1168. key,
  1169. size,
  1170. GNUNET_SYSERR,
  1171. "bind failed");
  1172. return;
  1173. }
  1174. if (SQLITE_DONE != sqlite3_step (plugin->remove))
  1175. {
  1176. LOG_SQLITE (plugin,
  1177. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  1178. "sqlite3_step");
  1179. GNUNET_SQ_reset (plugin->dbh,
  1180. plugin->remove);
  1181. cont (cont_cls,
  1182. key,
  1183. size,
  1184. GNUNET_SYSERR,
  1185. "sqlite3_step failed");
  1186. return;
  1187. }
  1188. int changes = sqlite3_changes (plugin->dbh);
  1189. GNUNET_SQ_reset (plugin->dbh,
  1190. plugin->remove);
  1191. if (0 == changes)
  1192. {
  1193. cont (cont_cls,
  1194. key,
  1195. size,
  1196. GNUNET_NO,
  1197. NULL);
  1198. return;
  1199. }
  1200. if (NULL != plugin->env->duc)
  1201. plugin->env->duc (plugin->env->cls,
  1202. -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
  1203. cont (cont_cls,
  1204. key,
  1205. size,
  1206. GNUNET_OK,
  1207. NULL);
  1208. }
  1209. /**
  1210. * Get an estimate of how much space the database is
  1211. * currently using.
  1212. *
  1213. * @param cls the `struct Plugin`
  1214. * @return the size of the database on disk (estimate)
  1215. */
  1216. static void
  1217. sqlite_plugin_estimate_size (void *cls,
  1218. unsigned long long *estimate)
  1219. {
  1220. struct Plugin *plugin = cls;
  1221. sqlite3_stmt *stmt;
  1222. uint64_t pages;
  1223. uint64_t page_size;
  1224. #if ENULL_DEFINED
  1225. char *e;
  1226. #endif
  1227. if (NULL == estimate)
  1228. return;
  1229. if (SQLITE_VERSION_NUMBER < 3006000)
  1230. {
  1231. GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
  1232. "datastore-sqlite",
  1233. _("sqlite version to old to determine size, assuming zero\n"));
  1234. *estimate = 0;
  1235. return;
  1236. }
  1237. CHECK (SQLITE_OK ==
  1238. sqlite3_exec (plugin->dbh,
  1239. "VACUUM",
  1240. NULL,
  1241. NULL,
  1242. ENULL));
  1243. CHECK (SQLITE_OK ==
  1244. sqlite3_exec (plugin->dbh,
  1245. "PRAGMA auto_vacuum=INCREMENTAL",
  1246. NULL,
  1247. NULL, ENULL));
  1248. CHECK (SQLITE_OK ==
  1249. sq_prepare (plugin->dbh,
  1250. "PRAGMA page_count",
  1251. &stmt));
  1252. if (SQLITE_ROW == sqlite3_step (stmt))
  1253. pages = sqlite3_column_int64 (stmt,
  1254. 0);
  1255. else
  1256. pages = 0;
  1257. sqlite3_finalize (stmt);
  1258. CHECK (SQLITE_OK ==
  1259. sq_prepare (plugin->dbh,
  1260. "PRAGMA page_size",
  1261. &stmt));
  1262. CHECK (SQLITE_ROW ==
  1263. sqlite3_step (stmt));
  1264. page_size = sqlite3_column_int64 (stmt, 0);
  1265. sqlite3_finalize (stmt);
  1266. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1267. _("Using sqlite page utilization to estimate payload (%llu pages of size %llu bytes)\n"),
  1268. (unsigned long long) pages,
  1269. (unsigned long long) page_size);
  1270. *estimate = pages * page_size;
  1271. }
  1272. /**
  1273. * Entry point for the plugin.
  1274. *
  1275. * @param cls the `struct GNUNET_DATASTORE_PluginEnvironment *`
  1276. * @return NULL on error, othrewise the plugin context
  1277. */
  1278. void *
  1279. libgnunet_plugin_datastore_sqlite_init (void *cls)
  1280. {
  1281. static struct Plugin plugin;
  1282. struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
  1283. struct GNUNET_DATASTORE_PluginFunctions *api;
  1284. if (NULL != plugin.env)
  1285. return NULL; /* can only initialize once! */
  1286. memset (&plugin,
  1287. 0,
  1288. sizeof (struct Plugin));
  1289. plugin.env = env;
  1290. if (GNUNET_OK != database_setup (env->cfg, &plugin))
  1291. {
  1292. database_shutdown (&plugin);
  1293. return NULL;
  1294. }
  1295. api = GNUNET_new (struct GNUNET_DATASTORE_PluginFunctions);
  1296. api->cls = &plugin;
  1297. api->estimate_size = &sqlite_plugin_estimate_size;
  1298. api->put = &sqlite_plugin_put;
  1299. api->get_key = &sqlite_plugin_get_key;
  1300. api->get_replication = &sqlite_plugin_get_replication;
  1301. api->get_expiration = &sqlite_plugin_get_expiration;
  1302. api->get_zero_anonymity = &sqlite_plugin_get_zero_anonymity;
  1303. api->get_keys = &sqlite_plugin_get_keys;
  1304. api->drop = &sqlite_plugin_drop;
  1305. api->remove_key = &sqlite_plugin_remove_key;
  1306. GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
  1307. "sqlite",
  1308. _("Sqlite database running\n"));
  1309. return api;
  1310. }
  1311. /**
  1312. * Exit point from the plugin.
  1313. *
  1314. * @param cls the plugin context (as returned by "init")
  1315. * @return always NULL
  1316. */
  1317. void *
  1318. libgnunet_plugin_datastore_sqlite_done (void *cls)
  1319. {
  1320. char *fn;
  1321. struct GNUNET_DATASTORE_PluginFunctions *api = cls;
  1322. struct Plugin *plugin = api->cls;
  1323. GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
  1324. "sqlite",
  1325. "sqlite plugin is done\n");
  1326. fn = NULL;
  1327. if (plugin->drop_on_shutdown)
  1328. fn = GNUNET_strdup (plugin->fn);
  1329. database_shutdown (plugin);
  1330. plugin->env = NULL;
  1331. GNUNET_free (api);
  1332. if (NULL != fn)
  1333. {
  1334. if (0 != UNLINK (fn))
  1335. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  1336. "unlink",
  1337. fn);
  1338. GNUNET_free (fn);
  1339. }
  1340. return NULL;
  1341. }
  1342. /* end of plugin_datastore_sqlite.c */