aboutsummaryrefslogtreecommitdiff
path: root/src/datacache/plugin_datacache_postgres.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-11-13 20:05:40 +0000
committerChristian Grothoff <christian@grothoff.org>2012-11-13 20:05:40 +0000
commit4e1d85bac66209e6655f3fb4d3ddef7929f0f3fc (patch)
treedf5c5a0d972f35d741b51c3a3fc8d2298ac8b4fd /src/datacache/plugin_datacache_postgres.c
parent855362e698928b0a5a8f72fc6427c2ea42af4e79 (diff)
downloadgnunet-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_postgres.c')
-rw-r--r--src/datacache/plugin_datacache_postgres.c69
1 files changed, 47 insertions, 22 deletions
diff --git a/src/datacache/plugin_datacache_postgres.c b/src/datacache/plugin_datacache_postgres.c
index e9157742c..e7d2e72c2 100644
--- a/src/datacache/plugin_datacache_postgres.c
+++ b/src/datacache/plugin_datacache_postgres.c
@@ -75,13 +75,18 @@ init_connection (struct Plugin *plugin)
75 " type INTEGER NOT NULL DEFAULT 0," 75 " type INTEGER NOT NULL DEFAULT 0,"
76 " discard_time BIGINT NOT NULL DEFAULT 0," 76 " discard_time BIGINT NOT NULL DEFAULT 0,"
77 " key BYTEA NOT NULL DEFAULT ''," 77 " key BYTEA NOT NULL DEFAULT '',"
78 " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS"); 78 " value BYTEA NOT NULL DEFAULT '',"
79 if ((ret == NULL) || ((PQresultStatus (ret) != PGRES_COMMAND_OK) && (0 != strcmp ("42P07", /* duplicate table */ 79 " path BYTEA DEFAULT '')"
80 PQresultErrorField 80 "WITH OIDS");
81 (ret, 81 if ( (ret == NULL) ||
82 PG_DIAG_SQLSTATE))))) 82 ((PQresultStatus (ret) != PGRES_COMMAND_OK) &&
83 (0 != strcmp ("42P07", /* duplicate table */
84 PQresultErrorField
85 (ret,
86 PG_DIAG_SQLSTATE)))))
83 { 87 {
84 (void) GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "CREATE TABLE", 88 (void) GNUNET_POSTGRES_check_result (plugin->dbh, ret,
89 PGRES_COMMAND_OK, "CREATE TABLE",
85 "gn090dc"); 90 "gn090dc");
86 PQfinish (plugin->dbh); 91 PQfinish (plugin->dbh);
87 plugin->dbh = NULL; 92 plugin->dbh = NULL;
@@ -123,11 +128,11 @@ init_connection (struct Plugin *plugin)
123 PQclear (ret); 128 PQclear (ret);
124 if ((GNUNET_OK != 129 if ((GNUNET_OK !=
125 GNUNET_POSTGRES_prepare (plugin->dbh, "getkt", 130 GNUNET_POSTGRES_prepare (plugin->dbh, "getkt",
126 "SELECT discard_time,type,value FROM gn090dc " 131 "SELECT discard_time,type,value,path FROM gn090dc "
127 "WHERE key=$1 AND type=$2 ", 2)) || 132 "WHERE key=$1 AND type=$2 ", 2)) ||
128 (GNUNET_OK != 133 (GNUNET_OK !=
129 GNUNET_POSTGRES_prepare (plugin->dbh, "getk", 134 GNUNET_POSTGRES_prepare (plugin->dbh, "getk",
130 "SELECT discard_time,type,value FROM gn090dc " 135 "SELECT discard_time,type,value,path FROM gn090dc "
131 "WHERE key=$1", 1)) || 136 "WHERE key=$1", 1)) ||
132 (GNUNET_OK != 137 (GNUNET_OK !=
133 GNUNET_POSTGRES_prepare (plugin->dbh, "getm", 138 GNUNET_POSTGRES_prepare (plugin->dbh, "getm",
@@ -137,8 +142,8 @@ init_connection (struct Plugin *plugin)
137 GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090dc WHERE oid=$1", 1)) || 142 GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090dc WHERE oid=$1", 1)) ||
138 (GNUNET_OK != 143 (GNUNET_OK !=
139 GNUNET_POSTGRES_prepare (plugin->dbh, "put", 144 GNUNET_POSTGRES_prepare (plugin->dbh, "put",
140 "INSERT INTO gn090dc (type, discard_time, key, value) " 145 "INSERT INTO gn090dc (type, discard_time, key, value, path) "
141 "VALUES ($1, $2, $3, $4)", 4))) 146 "VALUES ($1, $2, $3, $4, $5)", 5)))
142 { 147 {
143 PQfinish (plugin->dbh); 148 PQfinish (plugin->dbh);
144 plugin->dbh = NULL; 149 plugin->dbh = NULL;
@@ -157,12 +162,16 @@ init_connection (struct Plugin *plugin)
157 * @param data data to store 162 * @param data data to store
158 * @param type type of the value 163 * @param type type of the value
159 * @param discard_time when to discard the value in any case 164 * @param discard_time when to discard the value in any case
160 * @return 0 on error, number of bytes used otherwise 165 * @param path_info_len number of entries in 'path_info'
166 * @param path_info a path through the network
167 * @return 0 if duplicate, -1 on error, number of bytes used otherwise
161 */ 168 */
162static size_t 169static ssize_t
163postgres_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size, 170postgres_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size,
164 const char *data, enum GNUNET_BLOCK_Type type, 171 const char *data, enum GNUNET_BLOCK_Type type,
165 struct GNUNET_TIME_Absolute discard_time) 172 struct GNUNET_TIME_Absolute discard_time,
173 unsigned int path_info_len,
174 const struct GNUNET_PeerIdentity *path_info)
166{ 175{
167 struct Plugin *plugin = cls; 176 struct Plugin *plugin = cls;
168 PGresult *ret; 177 PGresult *ret;
@@ -173,22 +182,25 @@ postgres_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size,
173 (const char *) &btype, 182 (const char *) &btype,
174 (const char *) &bexpi, 183 (const char *) &bexpi,
175 (const char *) key, 184 (const char *) key,
176 (const char *) data 185 (const char *) data,
186 (const char *) path_info
177 }; 187 };
178 int paramLengths[] = { 188 int paramLengths[] = {
179 sizeof (btype), 189 sizeof (btype),
180 sizeof (bexpi), 190 sizeof (bexpi),
181 sizeof (struct GNUNET_HashCode), 191 sizeof (struct GNUNET_HashCode),
182 size 192 size,
193 path_info_len * sizeof (struct GNUNET_PeerIdentity)
183 }; 194 };
184 const int paramFormats[] = { 1, 1, 1, 1 }; 195 const int paramFormats[] = { 1, 1, 1, 1, 1 };
185 196
186 ret = 197 ret =
187 PQexecPrepared (plugin->dbh, "put", 4, paramValues, paramLengths, 198 PQexecPrepared (plugin->dbh, "put", 5, paramValues, paramLengths,
188 paramFormats, 1); 199 paramFormats, 1);
189 if (GNUNET_OK != 200 if (GNUNET_OK !=
190 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put")) 201 GNUNET_POSTGRES_check_result (plugin->dbh, ret,
191 return GNUNET_SYSERR; 202 PGRES_COMMAND_OK, "PQexecPrepared", "put"))
203 return -1;
192 PQclear (ret); 204 PQclear (ret);
193 return size + OVERHEAD; 205 return size + OVERHEAD;
194} 206}
@@ -226,6 +238,8 @@ postgres_plugin_get (void *cls, const struct GNUNET_HashCode * key,
226 uint32_t size; 238 uint32_t size;
227 unsigned int cnt; 239 unsigned int cnt;
228 unsigned int i; 240 unsigned int i;
241 unsigned int path_len;
242 const struct GNUNET_PeerIdentity *path;
229 PGresult *res; 243 PGresult *res;
230 244
231 res = 245 res =
@@ -254,7 +268,7 @@ postgres_plugin_get (void *cls, const struct GNUNET_HashCode * key,
254 PQclear (res); 268 PQclear (res);
255 return cnt; 269 return cnt;
256 } 270 }
257 if ((3 != PQnfields (res)) || (sizeof (uint64_t) != PQfsize (res, 0)) || 271 if ((4 != PQnfields (res)) || (sizeof (uint64_t) != PQfsize (res, 0)) ||
258 (sizeof (uint32_t) != PQfsize (res, 1))) 272 (sizeof (uint32_t) != PQfsize (res, 1)))
259 { 273 {
260 GNUNET_break (0); 274 GNUNET_break (0);
@@ -267,12 +281,23 @@ postgres_plugin_get (void *cls, const struct GNUNET_HashCode * key,
267 GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, i, 0)); 281 GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, i, 0));
268 type = ntohl (*(uint32_t *) PQgetvalue (res, i, 1)); 282 type = ntohl (*(uint32_t *) PQgetvalue (res, i, 1));
269 size = PQgetlength (res, i, 2); 283 size = PQgetlength (res, i, 2);
284 path_len = PQgetlength (res, i, 3);
285 if (0 != (path_len % sizeof (struct GNUNET_PeerIdentity)))
286 {
287 GNUNET_break (0);
288 path_len = 0;
289 }
290 path_len %= sizeof (struct GNUNET_PeerIdentity);
291 path = (const struct GNUNET_PeerIdentity *) PQgetvalue (res, i, 3);
270 LOG (GNUNET_ERROR_TYPE_DEBUG, 292 LOG (GNUNET_ERROR_TYPE_DEBUG,
271 "Found result of size %u bytes and type %u in database\n", 293 "Found result of size %u bytes and type %u in database\n",
272 (unsigned int) size, (unsigned int) type); 294 (unsigned int) size, (unsigned int) type);
273 if (GNUNET_SYSERR == 295 if (GNUNET_SYSERR ==
274 iter (iter_cls, expiration_time, key, size, PQgetvalue (res, i, 2), 296 iter (iter_cls, key, size, PQgetvalue (res, i, 2),
275 (enum GNUNET_BLOCK_Type) type)) 297 (enum GNUNET_BLOCK_Type) type,
298 expiration_time,
299 path_len,
300 path))
276 { 301 {
277 LOG (GNUNET_ERROR_TYPE_DEBUG, 302 LOG (GNUNET_ERROR_TYPE_DEBUG,
278 "Ending iteration (client error)\n"); 303 "Ending iteration (client error)\n");