diff options
author | Christian Grothoff <christian@grothoff.org> | 2018-05-02 01:01:29 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2018-05-02 01:01:29 +0200 |
commit | ffe43cb1e86f49ddff73554df200853248af012d (patch) | |
tree | d75b9a36fcdd760e71c2d0aaf82d4e671e424b00 /src/namestore/plugin_namestore_postgres.c | |
parent | ee713e989d329969c6ed65a91b93620e8d4123b6 (diff) | |
download | gnunet-ffe43cb1e86f49ddff73554df200853248af012d.tar.gz gnunet-ffe43cb1e86f49ddff73554df200853248af012d.zip |
implement UPSERT instead of DELETE+INSERT to reduce number of DB transactions
Diffstat (limited to 'src/namestore/plugin_namestore_postgres.c')
-rw-r--r-- | src/namestore/plugin_namestore_postgres.c | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/src/namestore/plugin_namestore_postgres.c b/src/namestore/plugin_namestore_postgres.c index 4e4ca1f43..67c72c3c2 100644 --- a/src/namestore/plugin_namestore_postgres.c +++ b/src/namestore/plugin_namestore_postgres.c | |||
@@ -72,7 +72,8 @@ database_setup (struct Plugin *plugin) | |||
72 | " rvalue BYTEA NOT NULL DEFAULT ''," | 72 | " rvalue BYTEA NOT NULL DEFAULT ''," |
73 | " record_count INTEGER NOT NULL DEFAULT 0," | 73 | " record_count INTEGER NOT NULL DEFAULT 0," |
74 | " record_data BYTEA NOT NULL DEFAULT ''," | 74 | " record_data BYTEA NOT NULL DEFAULT ''," |
75 | " label TEXT NOT NULL DEFAULT ''" | 75 | " label TEXT NOT NULL DEFAULT ''," |
76 | " CONSTRAINT zl UNIQUE (zone_private_key,label)" | ||
76 | ")" | 77 | ")" |
77 | "WITH OIDS"); | 78 | "WITH OIDS"); |
78 | struct GNUNET_PQ_ExecuteStatement es_default = | 79 | struct GNUNET_PQ_ExecuteStatement es_default = |
@@ -84,6 +85,7 @@ database_setup (struct Plugin *plugin) | |||
84 | " record_count INTEGER NOT NULL DEFAULT 0," | 85 | " record_count INTEGER NOT NULL DEFAULT 0," |
85 | " record_data BYTEA NOT NULL DEFAULT ''," | 86 | " record_data BYTEA NOT NULL DEFAULT ''," |
86 | " label TEXT NOT NULL DEFAULT ''" | 87 | " label TEXT NOT NULL DEFAULT ''" |
88 | " CONSTRAINT zl UNIQUE (zone_private_key,label)" | ||
87 | ")" | 89 | ")" |
88 | "WITH OIDS"); | 90 | "WITH OIDS"); |
89 | const struct GNUNET_PQ_ExecuteStatement *cr; | 91 | const struct GNUNET_PQ_ExecuteStatement *cr; |
@@ -132,6 +134,8 @@ database_setup (struct Plugin *plugin) | |||
132 | "ON ns098records (zone_private_key,seq)"), | 134 | "ON ns098records (zone_private_key,seq)"), |
133 | GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_label " | 135 | GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS ir_label " |
134 | "ON ns098records (label)"), | 136 | "ON ns098records (label)"), |
137 | GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS zone_label " | ||
138 | "ON ns098records (zone_private_key,label)"), | ||
135 | GNUNET_PQ_EXECUTE_STATEMENT_END | 139 | GNUNET_PQ_EXECUTE_STATEMENT_END |
136 | }; | 140 | }; |
137 | 141 | ||
@@ -148,8 +152,14 @@ database_setup (struct Plugin *plugin) | |||
148 | { | 152 | { |
149 | struct GNUNET_PQ_PreparedStatement ps[] = { | 153 | struct GNUNET_PQ_PreparedStatement ps[] = { |
150 | GNUNET_PQ_make_prepare ("store_records", | 154 | GNUNET_PQ_make_prepare ("store_records", |
151 | "INSERT INTO ns098records (zone_private_key, pkey, rvalue, record_count, record_data, label) VALUES " | 155 | "INSERT INTO ns098records" |
152 | "($1, $2, $3, $4, $5, $6)", | 156 | " (zone_private_key, pkey, rvalue, record_count, record_data, label)" |
157 | " VALUES ($1, $2, $3, $4, $5, $6)" | ||
158 | " ON CONFLICT ON CONSTRAINT zl" | ||
159 | " DO UPDATE" | ||
160 | " SET pkey=$2,rvalue=$3,record_count=$4,record_data=$5" | ||
161 | " WHERE ns098records.zone_private_key = $1" | ||
162 | " AND ns098records.label = $6", | ||
153 | 6), | 163 | 6), |
154 | GNUNET_PQ_make_prepare ("delete_records", | 164 | GNUNET_PQ_make_prepare ("delete_records", |
155 | "DELETE FROM ns098records " | 165 | "DELETE FROM ns098records " |
@@ -233,7 +243,8 @@ namestore_postgres_store_records (void *cls, | |||
233 | GNUNET_break (0); | 243 | GNUNET_break (0); |
234 | return GNUNET_SYSERR; | 244 | return GNUNET_SYSERR; |
235 | } | 245 | } |
236 | /* first, delete existing records */ | 246 | /* if record set is empty, delete existing records */ |
247 | if (0 == rd_count) | ||
237 | { | 248 | { |
238 | struct GNUNET_PQ_QueryParam params[] = { | 249 | struct GNUNET_PQ_QueryParam params[] = { |
239 | GNUNET_PQ_query_param_auto_from_type (zone_key), | 250 | GNUNET_PQ_query_param_auto_from_type (zone_key), |
@@ -251,14 +262,12 @@ namestore_postgres_store_records (void *cls, | |||
251 | GNUNET_break (0); | 262 | GNUNET_break (0); |
252 | return GNUNET_SYSERR; | 263 | return GNUNET_SYSERR; |
253 | } | 264 | } |
254 | } | ||
255 | if (0 == rd_count) | ||
256 | { | ||
257 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | 265 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, |
258 | "postgres", | 266 | "postgres", |
259 | "Record deleted\n"); | 267 | "Record deleted\n"); |
260 | return GNUNET_OK; | 268 | return GNUNET_OK; |
261 | } | 269 | } |
270 | /* otherwise, UPSERT (i.e. UPDATE if exists, otherwise INSERT) */ | ||
262 | { | 271 | { |
263 | char data[data_size]; | 272 | char data[data_size]; |
264 | struct GNUNET_PQ_QueryParam params[] = { | 273 | struct GNUNET_PQ_QueryParam params[] = { |