diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-11-13 20:05:40 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-11-13 20:05:40 +0000 |
commit | 4e1d85bac66209e6655f3fb4d3ddef7929f0f3fc (patch) | |
tree | df5c5a0d972f35d741b51c3a3fc8d2298ac8b4fd /src/datacache/plugin_datacache_sqlite.c | |
parent | 855362e698928b0a5a8f72fc6427c2ea42af4e79 (diff) | |
download | gnunet-4e1d85bac66209e6655f3fb4d3ddef7929f0f3fc.tar.gz gnunet-4e1d85bac66209e6655f3fb4d3ddef7929f0f3fc.zip |
changing datacache API to separate put-paths from data (so that plugins can test for duplicates), removing support for MySQL
Diffstat (limited to 'src/datacache/plugin_datacache_sqlite.c')
-rw-r--r-- | src/datacache/plugin_datacache_sqlite.c | 53 |
1 files changed, 39 insertions, 14 deletions
diff --git a/src/datacache/plugin_datacache_sqlite.c b/src/datacache/plugin_datacache_sqlite.c index 74fa38544..3dae4643a 100644 --- a/src/datacache/plugin_datacache_sqlite.c +++ b/src/datacache/plugin_datacache_sqlite.c | |||
@@ -95,12 +95,16 @@ sq_prepare (sqlite3 * dbh, const char *zSql, /* SQL statement, UTF-8 encoded | |||
95 | * @param data data to store | 95 | * @param data data to store |
96 | * @param type type of the value | 96 | * @param type type of the value |
97 | * @param discard_time when to discard the value in any case | 97 | * @param discard_time when to discard the value in any case |
98 | * @return 0 on error, number of bytes used otherwise | 98 | * @return 0 if duplicate, -1 on error, number of bytes used otherwise |
99 | */ | 99 | */ |
100 | static size_t | 100 | static ssize_t |
101 | sqlite_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size, | 101 | sqlite_plugin_put (void *cls, |
102 | const char *data, enum GNUNET_BLOCK_Type type, | 102 | const struct GNUNET_HashCode *key, |
103 | struct GNUNET_TIME_Absolute discard_time) | 103 | size_t size, const char *data, |
104 | enum GNUNET_BLOCK_Type type, | ||
105 | struct GNUNET_TIME_Absolute discard_time, | ||
106 | unsigned int path_info_len, | ||
107 | const struct GNUNET_PeerIdentity *path_info) | ||
104 | { | 108 | { |
105 | struct Plugin *plugin = cls; | 109 | struct Plugin *plugin = cls; |
106 | sqlite3_stmt *stmt; | 110 | sqlite3_stmt *stmt; |
@@ -115,31 +119,38 @@ sqlite_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size, | |||
115 | dval = INT64_MAX; | 119 | dval = INT64_MAX; |
116 | if (sq_prepare | 120 | if (sq_prepare |
117 | (plugin->dbh, | 121 | (plugin->dbh, |
118 | "INSERT INTO ds090 (type, expire, key, value) VALUES (?, ?, ?, ?)", | 122 | "INSERT INTO ds090 (type, expire, key, value, path) VALUES (?, ?, ?, ?, ?)", |
119 | &stmt) != SQLITE_OK) | 123 | &stmt) != SQLITE_OK) |
120 | { | 124 | { |
121 | LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 125 | LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
122 | "sq_prepare"); | 126 | "sq_prepare"); |
123 | return 0; | 127 | return -1; |
124 | } | 128 | } |
125 | if ((SQLITE_OK != sqlite3_bind_int (stmt, 1, type)) || | 129 | if ((SQLITE_OK != sqlite3_bind_int (stmt, 1, type)) || |
126 | (SQLITE_OK != sqlite3_bind_int64 (stmt, 2, dval)) || | 130 | (SQLITE_OK != sqlite3_bind_int64 (stmt, 2, dval)) || |
127 | (SQLITE_OK != | 131 | (SQLITE_OK != |
128 | sqlite3_bind_blob (stmt, 3, key, sizeof (struct GNUNET_HashCode), | 132 | sqlite3_bind_blob (stmt, 3, |
133 | key, sizeof (struct GNUNET_HashCode), | ||
129 | SQLITE_TRANSIENT)) || | 134 | SQLITE_TRANSIENT)) || |
130 | (SQLITE_OK != sqlite3_bind_blob (stmt, 4, data, size, SQLITE_TRANSIENT))) | 135 | (SQLITE_OK != sqlite3_bind_blob (stmt, 4, |
136 | data, size, | ||
137 | SQLITE_TRANSIENT)) || | ||
138 | (SQLITE_OK != sqlite3_bind_blob (stmt, 5, | ||
139 | path_info, | ||
140 | path_info_len * sizeof (struct GNUNET_PeerIdentity), | ||
141 | SQLITE_TRANSIENT))) | ||
131 | { | 142 | { |
132 | LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 143 | LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
133 | "sqlite3_bind_xxx"); | 144 | "sqlite3_bind_xxx"); |
134 | sqlite3_finalize (stmt); | 145 | sqlite3_finalize (stmt); |
135 | return 0; | 146 | return -1; |
136 | } | 147 | } |
137 | if (SQLITE_DONE != sqlite3_step (stmt)) | 148 | if (SQLITE_DONE != sqlite3_step (stmt)) |
138 | { | 149 | { |
139 | LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 150 | LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
140 | "sqlite3_step"); | 151 | "sqlite3_step"); |
141 | sqlite3_finalize (stmt); | 152 | sqlite3_finalize (stmt); |
142 | return 0; | 153 | return -1; |
143 | } | 154 | } |
144 | if (SQLITE_OK != sqlite3_finalize (stmt)) | 155 | if (SQLITE_OK != sqlite3_finalize (stmt)) |
145 | LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 156 | LOG_SQLITE (plugin->dbh, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
@@ -173,8 +184,10 @@ sqlite_plugin_get (void *cls, const struct GNUNET_HashCode * key, | |||
173 | unsigned int cnt; | 184 | unsigned int cnt; |
174 | unsigned int off; | 185 | unsigned int off; |
175 | unsigned int total; | 186 | unsigned int total; |
187 | unsigned int psize; | ||
176 | char scratch[256]; | 188 | char scratch[256]; |
177 | int64_t ntime; | 189 | int64_t ntime; |
190 | const struct GNUNET_PeerIdentity *path; | ||
178 | 191 | ||
179 | now = GNUNET_TIME_absolute_get (); | 192 | now = GNUNET_TIME_absolute_get (); |
180 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' for key `%4s'\n", "GET", | 193 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing `%s' for key `%4s'\n", "GET", |
@@ -229,7 +242,7 @@ sqlite_plugin_get (void *cls, const struct GNUNET_HashCode * key, | |||
229 | { | 242 | { |
230 | off = (off + 1) % total; | 243 | off = (off + 1) % total; |
231 | GNUNET_snprintf (scratch, sizeof (scratch), | 244 | GNUNET_snprintf (scratch, sizeof (scratch), |
232 | "SELECT value,expire FROM ds090 WHERE key=? AND type=? AND expire >= ? LIMIT 1 OFFSET %u", | 245 | "SELECT value,expire,path FROM ds090 WHERE key=? AND type=? AND expire >= ? LIMIT 1 OFFSET %u", |
233 | off); | 246 | off); |
234 | if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK) | 247 | if (sq_prepare (plugin->dbh, scratch, &stmt) != SQLITE_OK) |
235 | { | 248 | { |
@@ -253,6 +266,17 @@ sqlite_plugin_get (void *cls, const struct GNUNET_HashCode * key, | |||
253 | size = sqlite3_column_bytes (stmt, 0); | 266 | size = sqlite3_column_bytes (stmt, 0); |
254 | dat = sqlite3_column_blob (stmt, 0); | 267 | dat = sqlite3_column_blob (stmt, 0); |
255 | exp.abs_value = sqlite3_column_int64 (stmt, 1); | 268 | exp.abs_value = sqlite3_column_int64 (stmt, 1); |
269 | psize = sqlite3_column_bytes (stmt, 2); | ||
270 | if (0 != psize % sizeof (struct GNUNET_PeerIdentity)) | ||
271 | { | ||
272 | GNUNET_break (0); | ||
273 | psize = 0; | ||
274 | } | ||
275 | psize /= sizeof (struct GNUNET_PeerIdentity); | ||
276 | if (0 != psize) | ||
277 | path = sqlite3_column_blob (stmt, 2); | ||
278 | else | ||
279 | path = NULL; | ||
256 | ntime = (int64_t) exp.abs_value; | 280 | ntime = (int64_t) exp.abs_value; |
257 | if (ntime == INT64_MAX) | 281 | if (ntime == INT64_MAX) |
258 | exp = GNUNET_TIME_UNIT_FOREVER_ABS; | 282 | exp = GNUNET_TIME_UNIT_FOREVER_ABS; |
@@ -260,7 +284,7 @@ sqlite_plugin_get (void *cls, const struct GNUNET_HashCode * key, | |||
260 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 284 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
261 | "Found %u-byte result when processing `%s' for key `%4s'\n", | 285 | "Found %u-byte result when processing `%s' for key `%4s'\n", |
262 | (unsigned int) size, "GET", GNUNET_h2s (key)); | 286 | (unsigned int) size, "GET", GNUNET_h2s (key)); |
263 | if (GNUNET_OK != iter (iter_cls, exp, key, size, dat, type)) | 287 | if (GNUNET_OK != iter (iter_cls, key, size, dat, type, exp, psize, path)) |
264 | { | 288 | { |
265 | sqlite3_finalize (stmt); | 289 | sqlite3_finalize (stmt); |
266 | break; | 290 | break; |
@@ -408,7 +432,8 @@ libgnunet_plugin_datacache_sqlite_init (void *cls) | |||
408 | "CREATE TABLE ds090 (" " type INTEGER NOT NULL DEFAULT 0," | 432 | "CREATE TABLE ds090 (" " type INTEGER NOT NULL DEFAULT 0," |
409 | " expire INTEGER NOT NULL DEFAULT 0," | 433 | " expire INTEGER NOT NULL DEFAULT 0," |
410 | " key BLOB NOT NULL DEFAULT ''," | 434 | " key BLOB NOT NULL DEFAULT ''," |
411 | " value BLOB NOT NULL DEFAULT '')"); | 435 | " value BLOB NOT NULL DEFAULT ''," |
436 | " path BLOB DEFAULT '')"); | ||
412 | SQLITE3_EXEC (dbh, "CREATE INDEX idx_hashidx ON ds090 (key,type,expire)"); | 437 | SQLITE3_EXEC (dbh, "CREATE INDEX idx_hashidx ON ds090 (key,type,expire)"); |
413 | SQLITE3_EXEC (dbh, "CREATE INDEX idx_expire ON ds090 (expire)"); | 438 | SQLITE3_EXEC (dbh, "CREATE INDEX idx_expire ON ds090 (expire)"); |
414 | plugin = GNUNET_malloc (sizeof (struct Plugin)); | 439 | plugin = GNUNET_malloc (sizeof (struct Plugin)); |