diff options
Diffstat (limited to 'src/datacache/plugin_datacache_postgres.c')
-rw-r--r-- | src/datacache/plugin_datacache_postgres.c | 85 |
1 files changed, 37 insertions, 48 deletions
diff --git a/src/datacache/plugin_datacache_postgres.c b/src/datacache/plugin_datacache_postgres.c index df5fc4454..8bfd04aea 100644 --- a/src/datacache/plugin_datacache_postgres.c +++ b/src/datacache/plugin_datacache_postgres.c | |||
@@ -66,64 +66,45 @@ struct Plugin | |||
66 | static enum GNUNET_GenericReturnValue | 66 | static enum GNUNET_GenericReturnValue |
67 | init_connection (struct Plugin *plugin) | 67 | init_connection (struct Plugin *plugin) |
68 | { | 68 | { |
69 | struct GNUNET_PQ_ExecuteStatement es[] = { | ||
70 | GNUNET_PQ_make_try_execute ( | ||
71 | "CREATE TEMPORARY SEQUENCE IF NOT EXISTS gn180dc_oid_seq"), | ||
72 | GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS gn180dc (" | ||
73 | " oid OID NOT NULL DEFAULT nextval('gn180dc_oid_seq')," | ||
74 | " type INT4 NOT NULL," | ||
75 | " ro INT4 NOT NULL," | ||
76 | " prox INT4 NOT NULL," | ||
77 | " expiration_time INT8 NOT NULL," | ||
78 | " key BYTEA NOT NULL CHECK(LENGTH(key)=64)," | ||
79 | " trunc BYTEA NOT NULL CHECK(LENGTH(trunc)=32)," | ||
80 | " value BYTEA NOT NULL," | ||
81 | " path BYTEA DEFAULT NULL)"), | ||
82 | GNUNET_PQ_make_try_execute ( | ||
83 | "ALTER SEQUENCE gnu011dc_oid_seq OWNED BY gn180dc.oid"), | ||
84 | GNUNET_PQ_make_try_execute ( | ||
85 | "CREATE INDEX IF NOT EXISTS idx_oid ON gn180dc (oid)"), | ||
86 | GNUNET_PQ_make_try_execute ( | ||
87 | "CREATE INDEX IF NOT EXISTS idx_key ON gn180dc (key)"), | ||
88 | GNUNET_PQ_make_try_execute ( | ||
89 | "CREATE INDEX IF NOT EXISTS idx_dt ON gn180dc (expiration_time)"), | ||
90 | GNUNET_PQ_make_execute ( | ||
91 | "ALTER TABLE gn180dc ALTER value SET STORAGE EXTERNAL"), | ||
92 | GNUNET_PQ_make_execute ("ALTER TABLE gn180dc ALTER key SET STORAGE PLAIN"), | ||
93 | GNUNET_PQ_EXECUTE_STATEMENT_END | ||
94 | }; | ||
95 | struct GNUNET_PQ_PreparedStatement ps[] = { | 69 | struct GNUNET_PQ_PreparedStatement ps[] = { |
96 | GNUNET_PQ_make_prepare ("getkt", | 70 | GNUNET_PQ_make_prepare ("getkt", |
97 | "SELECT expiration_time,type,ro,value,trunc,path FROM gn180dc " | 71 | "SELECT expiration_time,type,ro,value,trunc,path" |
98 | "WHERE key=$1 AND type=$2 AND expiration_time >= $3"), | 72 | " FROM datacache.gn180dc" |
73 | " WHERE key=$1 AND type=$2 AND expiration_time >= $3"), | ||
99 | GNUNET_PQ_make_prepare ("getk", | 74 | GNUNET_PQ_make_prepare ("getk", |
100 | "SELECT expiration_time,type,ro,value,trunc,path FROM gn180dc " | 75 | "SELECT expiration_time,type,ro,value,trunc,path" |
101 | "WHERE key=$1 AND expiration_time >= $2"), | 76 | " FROM datacache.gn180dc" |
77 | " WHERE key=$1 AND expiration_time >= $2"), | ||
102 | GNUNET_PQ_make_prepare ("getex", | 78 | GNUNET_PQ_make_prepare ("getex", |
103 | "SELECT LENGTH(value) AS len,oid,key FROM gn180dc" | 79 | "SELECT LENGTH(value) AS len,oid,key" |
80 | " FROM datacache.gn180dc" | ||
104 | " WHERE expiration_time < $1" | 81 | " WHERE expiration_time < $1" |
105 | " ORDER BY expiration_time ASC LIMIT 1"), | 82 | " ORDER BY expiration_time ASC LIMIT 1"), |
106 | GNUNET_PQ_make_prepare ("getm", | 83 | GNUNET_PQ_make_prepare ("getm", |
107 | "SELECT LENGTH(value) AS len,oid,key FROM gn180dc" | 84 | "SELECT LENGTH(value) AS len,oid,key" |
85 | " FROM datacache.gn180dc" | ||
108 | " ORDER BY prox ASC, expiration_time ASC LIMIT 1"), | 86 | " ORDER BY prox ASC, expiration_time ASC LIMIT 1"), |
109 | GNUNET_PQ_make_prepare ("get_closest", | 87 | GNUNET_PQ_make_prepare ("get_closest", |
110 | "(SELECT expiration_time,type,ro,value,trunc,path,key FROM gn180dc" | 88 | "(SELECT expiration_time,type,ro,value,trunc,path,key" |
89 | " FROM datacache.gn180dc" | ||
111 | " WHERE key >= $1" | 90 | " WHERE key >= $1" |
112 | " AND expiration_time >= $2" | 91 | " AND expiration_time >= $2" |
113 | " AND ( (type = $3) OR ( 0 = $3) )" | 92 | " AND ( (type = $3) OR ( 0 = $3) )" |
114 | " ORDER BY key ASC" | 93 | " ORDER BY key ASC" |
115 | " LIMIT $4)" | 94 | " LIMIT $4)" |
116 | " UNION " | 95 | " UNION " |
117 | "(SELECT expiration_time,type,ro,value,trunc,path,key FROM gn180dc" | 96 | "(SELECT expiration_time,type,ro,value,trunc,path,key" |
97 | " FROM datacache.gn180dc" | ||
118 | " WHERE key <= $1" | 98 | " WHERE key <= $1" |
119 | " AND expiration_time >= $2" | 99 | " AND expiration_time >= $2" |
120 | " AND ( (type = $3) OR ( 0 = $3) )" | 100 | " AND ( (type = $3) OR ( 0 = $3) )" |
121 | " ORDER BY key DESC" | 101 | " ORDER BY key DESC" |
122 | " LIMIT $4)"), | 102 | " LIMIT $4)"), |
123 | GNUNET_PQ_make_prepare ("delrow", | 103 | GNUNET_PQ_make_prepare ("delrow", |
124 | "DELETE FROM gn180dc WHERE oid=$1"), | 104 | "DELETE FROM datacache.gn180dc" |
105 | " WHERE oid=$1"), | ||
125 | GNUNET_PQ_make_prepare ("put", | 106 | GNUNET_PQ_make_prepare ("put", |
126 | "INSERT INTO gn180dc" | 107 | "INSERT INTO datacache.gn180dc" |
127 | " (type, ro, prox, expiration_time, key, value, trunc, path) " | 108 | " (type, ro, prox, expiration_time, key, value, trunc, path) " |
128 | "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"), | 109 | "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"), |
129 | GNUNET_PQ_PREPARED_STATEMENT_END | 110 | GNUNET_PQ_PREPARED_STATEMENT_END |
@@ -131,8 +112,8 @@ init_connection (struct Plugin *plugin) | |||
131 | 112 | ||
132 | plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg, | 113 | plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg, |
133 | "datacache-postgres", | 114 | "datacache-postgres", |
115 | "datacache-", | ||
134 | NULL, | 116 | NULL, |
135 | es, | ||
136 | ps); | 117 | ps); |
137 | if (NULL == plugin->dbh) | 118 | if (NULL == plugin->dbh) |
138 | return GNUNET_SYSERR; | 119 | return GNUNET_SYSERR; |
@@ -165,9 +146,12 @@ postgres_plugin_put (void *cls, | |||
165 | GNUNET_PQ_query_param_fixed_size (block->data, | 146 | GNUNET_PQ_query_param_fixed_size (block->data, |
166 | block->data_size), | 147 | block->data_size), |
167 | GNUNET_PQ_query_param_auto_from_type (&block->trunc_peer), | 148 | GNUNET_PQ_query_param_auto_from_type (&block->trunc_peer), |
168 | GNUNET_PQ_query_param_fixed_size (block->put_path, | 149 | (0 == block->put_path_length) |
169 | block->put_path_length | 150 | ? GNUNET_PQ_query_param_null () |
170 | * sizeof(struct GNUNET_DHT_PathElement)), | 151 | : GNUNET_PQ_query_param_fixed_size ( |
152 | block->put_path, | ||
153 | block->put_path_length | ||
154 | * sizeof(struct GNUNET_DHT_PathElement)), | ||
171 | GNUNET_PQ_query_param_end | 155 | GNUNET_PQ_query_param_end |
172 | }; | 156 | }; |
173 | enum GNUNET_DB_QueryStatus ret; | 157 | enum GNUNET_DB_QueryStatus ret; |
@@ -226,8 +210,8 @@ handle_results (void *cls, | |||
226 | uint32_t bro32; | 210 | uint32_t bro32; |
227 | void *data; | 211 | void *data; |
228 | struct GNUNET_DATACACHE_Block block; | 212 | struct GNUNET_DATACACHE_Block block; |
229 | void *path; | 213 | void *path = NULL; |
230 | size_t path_size; | 214 | size_t path_size = 0; |
231 | struct GNUNET_PQ_ResultSpec rs[] = { | 215 | struct GNUNET_PQ_ResultSpec rs[] = { |
232 | GNUNET_PQ_result_spec_absolute_time ("expiration_time", | 216 | GNUNET_PQ_result_spec_absolute_time ("expiration_time", |
233 | &block.expiration_time), | 217 | &block.expiration_time), |
@@ -240,9 +224,11 @@ handle_results (void *cls, | |||
240 | &block.data_size), | 224 | &block.data_size), |
241 | GNUNET_PQ_result_spec_auto_from_type ("trunc", | 225 | GNUNET_PQ_result_spec_auto_from_type ("trunc", |
242 | &block.trunc_peer), | 226 | &block.trunc_peer), |
243 | GNUNET_PQ_result_spec_variable_size ("path", | 227 | GNUNET_PQ_result_spec_allow_null ( |
244 | &path, | 228 | GNUNET_PQ_result_spec_variable_size ("path", |
245 | &path_size), | 229 | &path, |
230 | &path_size), | ||
231 | NULL), | ||
246 | GNUNET_PQ_result_spec_end | 232 | GNUNET_PQ_result_spec_end |
247 | }; | 233 | }; |
248 | 234 | ||
@@ -351,12 +337,12 @@ postgres_plugin_del (void *cls) | |||
351 | GNUNET_PQ_query_param_end | 337 | GNUNET_PQ_query_param_end |
352 | }; | 338 | }; |
353 | uint32_t size; | 339 | uint32_t size; |
354 | uint32_t oid; | 340 | uint64_t oid; |
355 | struct GNUNET_HashCode key; | 341 | struct GNUNET_HashCode key; |
356 | struct GNUNET_PQ_ResultSpec rs[] = { | 342 | struct GNUNET_PQ_ResultSpec rs[] = { |
357 | GNUNET_PQ_result_spec_uint32 ("len", | 343 | GNUNET_PQ_result_spec_uint32 ("len", |
358 | &size), | 344 | &size), |
359 | GNUNET_PQ_result_spec_uint32 ("oid", | 345 | GNUNET_PQ_result_spec_uint64 ("oid", |
360 | &oid), | 346 | &oid), |
361 | GNUNET_PQ_result_spec_auto_from_type ("key", | 347 | GNUNET_PQ_result_spec_auto_from_type ("key", |
362 | &key), | 348 | &key), |
@@ -364,7 +350,7 @@ postgres_plugin_del (void *cls) | |||
364 | }; | 350 | }; |
365 | enum GNUNET_DB_QueryStatus res; | 351 | enum GNUNET_DB_QueryStatus res; |
366 | struct GNUNET_PQ_QueryParam dparam[] = { | 352 | struct GNUNET_PQ_QueryParam dparam[] = { |
367 | GNUNET_PQ_query_param_uint32 (&oid), | 353 | GNUNET_PQ_query_param_uint64 (&oid), |
368 | GNUNET_PQ_query_param_end | 354 | GNUNET_PQ_query_param_end |
369 | }; | 355 | }; |
370 | struct GNUNET_TIME_Absolute now; | 356 | struct GNUNET_TIME_Absolute now; |
@@ -617,6 +603,9 @@ libgnunet_plugin_datacache_postgres_done (void *cls) | |||
617 | struct GNUNET_DATACACHE_PluginFunctions *api = cls; | 603 | struct GNUNET_DATACACHE_PluginFunctions *api = cls; |
618 | struct Plugin *plugin = api->cls; | 604 | struct Plugin *plugin = api->cls; |
619 | 605 | ||
606 | GNUNET_break (GNUNET_OK == | ||
607 | GNUNET_PQ_exec_sql (plugin->dbh, | ||
608 | "datacache-drop")); | ||
620 | GNUNET_PQ_disconnect (plugin->dbh); | 609 | GNUNET_PQ_disconnect (plugin->dbh); |
621 | GNUNET_free (plugin); | 610 | GNUNET_free (plugin); |
622 | GNUNET_free (api); | 611 | GNUNET_free (api); |