plugin_peerstore_sqlite.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. /*
  2. * This file is part of GNUnet
  3. * Copyright (C) 2013, 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 peerstore/plugin_peerstore_sqlite.c
  21. * @brief sqlite-based peerstore backend
  22. * @author Omar Tarabai
  23. * @author Christian Grothoff
  24. */
  25. #include "platform.h"
  26. #include "gnunet_peerstore_plugin.h"
  27. #include "gnunet_peerstore_service.h"
  28. #include "gnunet_sq_lib.h"
  29. #include "peerstore.h"
  30. #include <sqlite3.h>
  31. /**
  32. * After how many ms "busy" should a DB operation fail for good? A
  33. * low value makes sure that we are more responsive to requests
  34. * (especially PUTs). A high value guarantees a higher success rate
  35. * (SELECTs in iterate can take several seconds despite LIMIT=1).
  36. *
  37. * The default value of 1s should ensure that users do not experience
  38. * huge latencies while at the same time allowing operations to
  39. * succeed with reasonable probability.
  40. */
  41. #define BUSY_TIMEOUT_MS 1000
  42. /**
  43. * Log an error message at log-level 'level' that indicates
  44. * a failure of the command 'cmd' on file 'filename'
  45. * with the message given by strerror(errno).
  46. */
  47. #define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, "peerstore-sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
  48. #define LOG(kind,...) GNUNET_log_from (kind, "peerstore-sqlite", __VA_ARGS__)
  49. /**
  50. * Context for all functions in this plugin.
  51. */
  52. struct Plugin
  53. {
  54. /**
  55. * Configuration handle
  56. */
  57. const struct GNUNET_CONFIGURATION_Handle *cfg;
  58. /**
  59. * Database filename.
  60. */
  61. char *fn;
  62. /**
  63. * Native SQLite database handle.
  64. */
  65. sqlite3 *dbh;
  66. /**
  67. * Precompiled SQL for inserting into peerstoredata
  68. */
  69. sqlite3_stmt *insert_peerstoredata;
  70. /**
  71. * Precompiled SQL for selecting from peerstoredata
  72. */
  73. sqlite3_stmt *select_peerstoredata;
  74. /**
  75. * Precompiled SQL for selecting from peerstoredata
  76. */
  77. sqlite3_stmt *select_peerstoredata_by_pid;
  78. /**
  79. * Precompiled SQL for selecting from peerstoredata
  80. */
  81. sqlite3_stmt *select_peerstoredata_by_key;
  82. /**
  83. * Precompiled SQL for selecting from peerstoredata
  84. */
  85. sqlite3_stmt *select_peerstoredata_by_all;
  86. /**
  87. * Precompiled SQL for deleting expired
  88. * records from peerstoredata
  89. */
  90. sqlite3_stmt *expire_peerstoredata;
  91. /**
  92. * Precompiled SQL for deleting records
  93. * with given key
  94. */
  95. sqlite3_stmt *delete_peerstoredata;
  96. };
  97. /**
  98. * Delete records with the given key
  99. *
  100. * @param cls closure (internal context for the plugin)
  101. * @param sub_system name of sub system
  102. * @param peer Peer identity (can be NULL)
  103. * @param key entry key string (can be NULL)
  104. * @return number of deleted records, #GNUNE_SYSERR on error
  105. */
  106. static int
  107. peerstore_sqlite_delete_records (void *cls,
  108. const char *sub_system,
  109. const struct GNUNET_PeerIdentity *peer,
  110. const char *key)
  111. {
  112. struct Plugin *plugin = cls;
  113. sqlite3_stmt *stmt = plugin->delete_peerstoredata;
  114. struct GNUNET_SQ_QueryParam params[] = {
  115. GNUNET_SQ_query_param_string (sub_system),
  116. GNUNET_SQ_query_param_auto_from_type (peer),
  117. GNUNET_SQ_query_param_string (key),
  118. GNUNET_SQ_query_param_end
  119. };
  120. int ret;
  121. if (GNUNET_OK !=
  122. GNUNET_SQ_bind (stmt,
  123. params))
  124. {
  125. LOG_SQLITE (plugin,
  126. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  127. "sqlite3_bind");
  128. GNUNET_SQ_reset (plugin->dbh,
  129. stmt);
  130. return GNUNET_SYSERR;
  131. }
  132. if (SQLITE_DONE !=
  133. sqlite3_step (stmt))
  134. {
  135. LOG_SQLITE (plugin,
  136. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  137. "sqlite3_step");
  138. ret = GNUNET_SYSERR;
  139. }
  140. else
  141. {
  142. ret = sqlite3_changes (plugin->dbh);
  143. }
  144. GNUNET_SQ_reset (plugin->dbh,
  145. stmt);
  146. return ret;
  147. }
  148. /**
  149. * Delete expired records (expiry < now)
  150. *
  151. * @param cls closure (internal context for the plugin)
  152. * @param now time to use as reference
  153. * @param cont continuation called with the number of records expired
  154. * @param cont_cls continuation closure
  155. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and cont is not
  156. * called
  157. */
  158. static int
  159. peerstore_sqlite_expire_records (void *cls, struct GNUNET_TIME_Absolute now,
  160. GNUNET_PEERSTORE_Continuation cont,
  161. void *cont_cls)
  162. {
  163. struct Plugin *plugin = cls;
  164. sqlite3_stmt *stmt = plugin->expire_peerstoredata;
  165. struct GNUNET_SQ_QueryParam params[] = {
  166. GNUNET_SQ_query_param_absolute_time (&now),
  167. GNUNET_SQ_query_param_end
  168. };
  169. if (GNUNET_OK !=
  170. GNUNET_SQ_bind (stmt,
  171. params))
  172. {
  173. LOG_SQLITE (plugin,
  174. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  175. "sqlite3_bind");
  176. GNUNET_SQ_reset (plugin->dbh,
  177. stmt);
  178. return GNUNET_SYSERR;
  179. }
  180. if (SQLITE_DONE != sqlite3_step (stmt))
  181. {
  182. LOG_SQLITE (plugin,
  183. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  184. "sqlite3_step");
  185. GNUNET_SQ_reset (plugin->dbh,
  186. stmt);
  187. return GNUNET_SYSERR;
  188. }
  189. if (NULL != cont)
  190. cont (cont_cls,
  191. sqlite3_changes (plugin->dbh));
  192. GNUNET_SQ_reset (plugin->dbh,
  193. stmt);
  194. return GNUNET_OK;
  195. }
  196. /**
  197. * Iterate over the records given an optional peer id
  198. * and/or key.
  199. *
  200. * @param cls closure (internal context for the plugin)
  201. * @param sub_system name of sub system
  202. * @param peer Peer identity (can be NULL)
  203. * @param key entry key string (can be NULL)
  204. * @param iter function to call asynchronously with the results, terminated
  205. * by a NULL result
  206. * @param iter_cls closure for @a iter
  207. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and iter is not
  208. * called
  209. */
  210. static int
  211. peerstore_sqlite_iterate_records (void *cls,
  212. const char *sub_system,
  213. const struct GNUNET_PeerIdentity *peer,
  214. const char *key,
  215. GNUNET_PEERSTORE_Processor iter,
  216. void *iter_cls)
  217. {
  218. struct Plugin *plugin = cls;
  219. sqlite3_stmt *stmt;
  220. int err = 0;
  221. int sret;
  222. struct GNUNET_PEERSTORE_Record rec;
  223. LOG (GNUNET_ERROR_TYPE_DEBUG,
  224. "Executing iterate request on sqlite db.\n");
  225. if (NULL == peer)
  226. {
  227. if (NULL == key)
  228. {
  229. struct GNUNET_SQ_QueryParam params[] = {
  230. GNUNET_SQ_query_param_string (sub_system),
  231. GNUNET_SQ_query_param_end
  232. };
  233. stmt = plugin->select_peerstoredata;
  234. err = GNUNET_SQ_bind (stmt,
  235. params);
  236. }
  237. else
  238. {
  239. struct GNUNET_SQ_QueryParam params[] = {
  240. GNUNET_SQ_query_param_string (sub_system),
  241. GNUNET_SQ_query_param_string (key),
  242. GNUNET_SQ_query_param_end
  243. };
  244. stmt = plugin->select_peerstoredata_by_key;
  245. err = GNUNET_SQ_bind (stmt,
  246. params);
  247. }
  248. }
  249. else
  250. {
  251. if (NULL == key)
  252. {
  253. struct GNUNET_SQ_QueryParam params[] = {
  254. GNUNET_SQ_query_param_string (sub_system),
  255. GNUNET_SQ_query_param_auto_from_type (peer),
  256. GNUNET_SQ_query_param_end
  257. };
  258. stmt = plugin->select_peerstoredata_by_pid;
  259. err = GNUNET_SQ_bind (stmt,
  260. params);
  261. }
  262. else
  263. {
  264. struct GNUNET_SQ_QueryParam params[] = {
  265. GNUNET_SQ_query_param_string (sub_system),
  266. GNUNET_SQ_query_param_auto_from_type (peer),
  267. GNUNET_SQ_query_param_string (key),
  268. GNUNET_SQ_query_param_end
  269. };
  270. stmt = plugin->select_peerstoredata_by_all;
  271. err = GNUNET_SQ_bind (stmt,
  272. params);
  273. }
  274. }
  275. if (GNUNET_OK != err)
  276. {
  277. LOG_SQLITE (plugin,
  278. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  279. "sqlite3_bind_XXXX");
  280. GNUNET_SQ_reset (plugin->dbh,
  281. stmt);
  282. return GNUNET_SYSERR;
  283. }
  284. err = 0;
  285. while (SQLITE_ROW == (sret = sqlite3_step (stmt)))
  286. {
  287. LOG (GNUNET_ERROR_TYPE_DEBUG,
  288. "Returning a matched record.\n");
  289. struct GNUNET_SQ_ResultSpec rs[] = {
  290. GNUNET_SQ_result_spec_string (&rec.sub_system),
  291. GNUNET_SQ_result_spec_auto_from_type (&rec.peer),
  292. GNUNET_SQ_result_spec_string (&rec.key),
  293. GNUNET_SQ_result_spec_variable_size (&rec.value, &rec.value_size),
  294. GNUNET_SQ_result_spec_absolute_time (&rec.expiry),
  295. GNUNET_SQ_result_spec_end
  296. };
  297. if (GNUNET_OK !=
  298. GNUNET_SQ_extract_result (stmt,
  299. rs))
  300. {
  301. GNUNET_break (0);
  302. break;
  303. }
  304. if (NULL != iter)
  305. iter (iter_cls,
  306. &rec,
  307. NULL);
  308. GNUNET_SQ_cleanup_result (rs);
  309. }
  310. if (SQLITE_DONE != sret)
  311. {
  312. LOG_SQLITE (plugin,
  313. GNUNET_ERROR_TYPE_ERROR,
  314. "sqlite_step");
  315. err = 1;
  316. }
  317. GNUNET_SQ_reset (plugin->dbh,
  318. stmt);
  319. if (NULL != iter)
  320. iter (iter_cls,
  321. NULL,
  322. err ? "sqlite error" : NULL);
  323. return GNUNET_OK;
  324. }
  325. /**
  326. * Store a record in the peerstore.
  327. * Key is the combination of sub system and peer identity.
  328. * One key can store multiple values.
  329. *
  330. * @param cls closure (internal context for the plugin)
  331. * @param sub_system name of the GNUnet sub system responsible
  332. * @param peer peer identity
  333. * @param key record key string
  334. * @param value value to be stored
  335. * @param size size of value to be stored
  336. * @param expiry absolute time after which the record is (possibly) deleted
  337. * @param options options related to the store operation
  338. * @param cont continuation called when record is stored
  339. * @param cont_cls continuation closure
  340. * @return #GNUNET_OK on success, else #GNUNET_SYSERR and cont is not called
  341. */
  342. static int
  343. peerstore_sqlite_store_record (void *cls,
  344. const char *sub_system,
  345. const struct GNUNET_PeerIdentity *peer,
  346. const char *key,
  347. const void *value,
  348. size_t size,
  349. struct GNUNET_TIME_Absolute expiry,
  350. enum GNUNET_PEERSTORE_StoreOption options,
  351. GNUNET_PEERSTORE_Continuation cont,
  352. void *cont_cls)
  353. {
  354. struct Plugin *plugin = cls;
  355. sqlite3_stmt *stmt = plugin->insert_peerstoredata;
  356. struct GNUNET_SQ_QueryParam params[] = {
  357. GNUNET_SQ_query_param_string (sub_system),
  358. GNUNET_SQ_query_param_auto_from_type (peer),
  359. GNUNET_SQ_query_param_string (key),
  360. GNUNET_SQ_query_param_fixed_size (value, size),
  361. GNUNET_SQ_query_param_absolute_time (&expiry),
  362. GNUNET_SQ_query_param_end
  363. };
  364. if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options)
  365. {
  366. peerstore_sqlite_delete_records (cls,
  367. sub_system,
  368. peer,
  369. key);
  370. }
  371. if (GNUNET_OK !=
  372. GNUNET_SQ_bind (stmt,
  373. params))
  374. LOG_SQLITE (plugin,
  375. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  376. "sqlite3_bind");
  377. else if (SQLITE_DONE != sqlite3_step (stmt))
  378. {
  379. LOG_SQLITE (plugin,
  380. GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
  381. "sqlite3_step");
  382. }
  383. GNUNET_SQ_reset (plugin->dbh,
  384. stmt);
  385. if (NULL != cont)
  386. cont (cont_cls,
  387. GNUNET_OK);
  388. return GNUNET_OK;
  389. }
  390. /**
  391. * @brief Prepare a SQL statement
  392. *
  393. * @param dbh handle to the database
  394. * @param sql SQL statement, UTF-8 encoded
  395. * @return 0 on success
  396. */
  397. static int
  398. sql_exec (sqlite3 *dbh,
  399. const char *sql)
  400. {
  401. int result;
  402. result = sqlite3_exec (dbh,
  403. sql,
  404. NULL,
  405. NULL,
  406. NULL);
  407. LOG (GNUNET_ERROR_TYPE_DEBUG,
  408. "Executed `%s' / %d\n",
  409. sql,
  410. result);
  411. if (SQLITE_OK != result)
  412. LOG (GNUNET_ERROR_TYPE_ERROR,
  413. _("Error executing SQL query: %s\n %s\n"),
  414. sqlite3_errmsg (dbh),
  415. sql);
  416. return result;
  417. }
  418. /**
  419. * @brief Prepare a SQL statement
  420. *
  421. * @param dbh handle to the database
  422. * @param sql SQL statement, UTF-8 encoded
  423. * @param stmt set to the prepared statement
  424. * @return 0 on success
  425. */
  426. static int
  427. sql_prepare (sqlite3 *dbh,
  428. const char *sql,
  429. sqlite3_stmt ** stmt)
  430. {
  431. char *tail;
  432. int result;
  433. result = sqlite3_prepare_v2 (dbh,
  434. sql,
  435. strlen (sql),
  436. stmt,
  437. (const char **) &tail);
  438. LOG (GNUNET_ERROR_TYPE_DEBUG,
  439. "Prepared `%s' / %p: %d\n",
  440. sql,
  441. *stmt,
  442. result);
  443. if (SQLITE_OK != result)
  444. LOG (GNUNET_ERROR_TYPE_ERROR,
  445. _("Error preparing SQL query: %s\n %s\n"),
  446. sqlite3_errmsg (dbh),
  447. sql);
  448. return result;
  449. }
  450. /**
  451. * Initialize the database connections and associated
  452. * data structures (create tables and indices
  453. * as needed as well).
  454. *
  455. * @param plugin the plugin context (state for this module)
  456. * @return #GNUNET_OK on success
  457. */
  458. static int
  459. database_setup (struct Plugin *plugin)
  460. {
  461. char *filename;
  462. if (GNUNET_OK !=
  463. GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
  464. "peerstore-sqlite",
  465. "FILENAME",
  466. &filename))
  467. {
  468. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  469. "peerstore-sqlite",
  470. "FILENAME");
  471. return GNUNET_SYSERR;
  472. }
  473. if (GNUNET_OK != GNUNET_DISK_file_test (filename))
  474. {
  475. if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (filename))
  476. {
  477. GNUNET_break (0);
  478. GNUNET_free (filename);
  479. return GNUNET_SYSERR;
  480. }
  481. }
  482. /* filename should be UTF-8-encoded. If it isn't, it's a bug */
  483. plugin->fn = filename;
  484. /* Open database and precompile statements */
  485. if (SQLITE_OK != sqlite3_open (plugin->fn,
  486. &plugin->dbh))
  487. {
  488. LOG (GNUNET_ERROR_TYPE_ERROR,
  489. _("Unable to initialize SQLite: %s.\n"),
  490. sqlite3_errmsg (plugin->dbh));
  491. return GNUNET_SYSERR;
  492. }
  493. sql_exec (plugin->dbh,
  494. "PRAGMA temp_store=MEMORY");
  495. sql_exec (plugin->dbh,
  496. "PRAGMA synchronous=OFF");
  497. sql_exec (plugin->dbh,
  498. "PRAGMA legacy_file_format=OFF");
  499. sql_exec (plugin->dbh,
  500. "PRAGMA auto_vacuum=INCREMENTAL");
  501. sql_exec (plugin->dbh,
  502. "PRAGMA encoding=\"UTF-8\"");
  503. sql_exec (plugin->dbh,
  504. "PRAGMA page_size=4096");
  505. sqlite3_busy_timeout (plugin->dbh,
  506. BUSY_TIMEOUT_MS);
  507. /* Create tables */
  508. sql_exec (plugin->dbh,
  509. "CREATE TABLE IF NOT EXISTS peerstoredata (\n"
  510. " sub_system TEXT NOT NULL,\n"
  511. " peer_id BLOB NOT NULL,\n"
  512. " key TEXT NOT NULL,\n"
  513. " value BLOB NULL,\n"
  514. " expiry INT8 NOT NULL" ");");
  515. /* Create Indices */
  516. if (SQLITE_OK !=
  517. sqlite3_exec (plugin->dbh,
  518. "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)",
  519. NULL,
  520. NULL,
  521. NULL))
  522. {
  523. LOG (GNUNET_ERROR_TYPE_ERROR,
  524. _("Unable to create indices: %s.\n"),
  525. sqlite3_errmsg (plugin->dbh));
  526. return GNUNET_SYSERR;
  527. }
  528. /* Prepare statements */
  529. sql_prepare (plugin->dbh,
  530. "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry)"
  531. " VALUES (?,?,?,?,?);",
  532. &plugin->insert_peerstoredata);
  533. sql_prepare (plugin->dbh,
  534. "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
  535. " WHERE sub_system = ?",
  536. &plugin->select_peerstoredata);
  537. sql_prepare (plugin->dbh,
  538. "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
  539. " WHERE sub_system = ?"
  540. " AND peer_id = ?",
  541. &plugin->select_peerstoredata_by_pid);
  542. sql_prepare (plugin->dbh,
  543. "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
  544. " WHERE sub_system = ?"
  545. " AND key = ?",
  546. &plugin->select_peerstoredata_by_key);
  547. sql_prepare (plugin->dbh,
  548. "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
  549. " WHERE sub_system = ?"
  550. " AND peer_id = ?" " AND key = ?",
  551. &plugin->select_peerstoredata_by_all);
  552. sql_prepare (plugin->dbh,
  553. "DELETE FROM peerstoredata"
  554. " WHERE expiry < ?",
  555. &plugin->expire_peerstoredata);
  556. sql_prepare (plugin->dbh,
  557. "DELETE FROM peerstoredata"
  558. " WHERE sub_system = ?"
  559. " AND peer_id = ?" " AND key = ?",
  560. &plugin->delete_peerstoredata);
  561. return GNUNET_OK;
  562. }
  563. /**
  564. * Shutdown database connection and associate data
  565. * structures.
  566. * @param plugin the plugin context (state for this module)
  567. */
  568. static void
  569. database_shutdown (struct Plugin *plugin)
  570. {
  571. int result;
  572. sqlite3_stmt *stmt;
  573. while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh,
  574. NULL)))
  575. {
  576. result = sqlite3_finalize (stmt);
  577. if (SQLITE_OK != result)
  578. LOG (GNUNET_ERROR_TYPE_WARNING,
  579. "Failed to close statement %p: %d\n",
  580. stmt,
  581. result);
  582. }
  583. if (SQLITE_OK != sqlite3_close (plugin->dbh))
  584. LOG_SQLITE (plugin,
  585. GNUNET_ERROR_TYPE_ERROR,
  586. "sqlite3_close");
  587. GNUNET_free_non_null (plugin->fn);
  588. }
  589. /**
  590. * Entry point for the plugin.
  591. *
  592. * @param cls The struct GNUNET_CONFIGURATION_Handle.
  593. * @return NULL on error, otherwise the plugin context
  594. */
  595. void *
  596. libgnunet_plugin_peerstore_sqlite_init (void *cls)
  597. {
  598. static struct Plugin plugin;
  599. const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
  600. struct GNUNET_PEERSTORE_PluginFunctions *api;
  601. if (NULL != plugin.cfg)
  602. return NULL; /* can only initialize once! */
  603. memset (&plugin,
  604. 0,
  605. sizeof (struct Plugin));
  606. plugin.cfg = cfg;
  607. if (GNUNET_OK != database_setup (&plugin))
  608. {
  609. database_shutdown (&plugin);
  610. return NULL;
  611. }
  612. api = GNUNET_new (struct GNUNET_PEERSTORE_PluginFunctions);
  613. api->cls = &plugin;
  614. api->store_record = &peerstore_sqlite_store_record;
  615. api->iterate_records = &peerstore_sqlite_iterate_records;
  616. api->expire_records = &peerstore_sqlite_expire_records;
  617. LOG (GNUNET_ERROR_TYPE_DEBUG,
  618. "Sqlite plugin is running\n");
  619. return api;
  620. }
  621. /**
  622. * Exit point from the plugin.
  623. *
  624. * @param cls The plugin context (as returned by "init")
  625. * @return Always NULL
  626. */
  627. void *
  628. libgnunet_plugin_peerstore_sqlite_done (void *cls)
  629. {
  630. struct GNUNET_PEERSTORE_PluginFunctions *api = cls;
  631. struct Plugin *plugin = api->cls;
  632. database_shutdown (plugin);
  633. plugin->cfg = NULL;
  634. GNUNET_free (api);
  635. LOG (GNUNET_ERROR_TYPE_DEBUG,
  636. "Sqlite plugin is finished\n");
  637. return NULL;
  638. }
  639. /* end of plugin_peerstore_sqlite.c */