Skip to content

Commit 4c1ca73

Browse files
committed
refactor: use inline array initialization
Better for locality of behavior and reduces LOC
1 parent bef0667 commit 4c1ca73

File tree

1 file changed

+32
-67
lines changed

1 file changed

+32
-67
lines changed

src/worker.c

Lines changed: 32 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,6 @@ worker_main(Datum main_arg)
209209
SPI_connect();
210210

211211
{
212-
int argCount = 2;
213-
Oid argTypes[2];
214-
Datum argValues[2];
215-
216-
argTypes[0] = INTERVALOID;
217-
argValues[0] = DirectFunctionCall3(interval_in, CStringGetDatum(ttl), ObjectIdGetDatum(InvalidOid), Int32GetDatum(-1));
218-
219-
argTypes[1] = INT4OID;
220-
argValues[1] = Int32GetDatum(batch_size);
221-
222212
int ttl_query_rc = SPI_execute_with_args("\
223213
WITH\
224214
rows AS (\
@@ -230,7 +220,12 @@ worker_main(Datum main_arg)
230220
)\
231221
DELETE FROM net._http_response r\
232222
USING rows WHERE r.ctid = rows.ctid",
233-
argCount, argTypes, argValues, NULL, false, 0);
223+
2,
224+
(Oid[]){INTERVALOID, INT4OID},
225+
(Datum[]){
226+
DirectFunctionCall3(interval_in, CStringGetDatum(ttl), ObjectIdGetDatum(InvalidOid), Int32GetDatum(-1))
227+
, Int32GetDatum(batch_size)
228+
}, NULL, false, 0);
234229

235230
if (ttl_query_rc != SPI_OK_DELETE)
236231
{
@@ -239,13 +234,6 @@ worker_main(Datum main_arg)
239234
}
240235

241236
{
242-
int argCount = 1;
243-
Oid argTypes[1];
244-
Datum argValues[1];
245-
246-
argTypes[0] = INT4OID;
247-
argValues[0] = Int32GetDatum(batch_size);
248-
249237
int queue_query_rc = SPI_execute_with_args("\
250238
WITH\
251239
rows AS (\
@@ -257,7 +245,10 @@ worker_main(Datum main_arg)
257245
DELETE FROM net.http_request_queue q\
258246
USING rows WHERE q.id = rows.id\
259247
RETURNING q.id, q.method, q.url, timeout_milliseconds, array(select key || ': ' || value from jsonb_each_text(q.headers)), q.body",
260-
argCount, argTypes, argValues, NULL, false, 0);
248+
1,
249+
(Oid[]){INT4OID},
250+
(Datum[]){Int32GetDatum(batch_size)},
251+
NULL, false, 0);
261252

262253
if (queue_query_rc == SPI_OK_DELETE_RETURNING)
263254
{
@@ -337,75 +328,49 @@ worker_main(Datum main_arg)
337328
eh = msg->easy_handle;
338329

339330
if (return_code != CURLE_OK) {
340-
int argCount = 2;
341-
Oid argTypes[2];
342-
Datum argValues[2];
343-
const char *error_msg = curl_easy_strerror(return_code);
344331
CurlData *cdata = NULL;
345332

346333
curl_easy_getinfo(eh, CURLINFO_PRIVATE, &cdata);
347334

348-
argTypes[0] = INT8OID;
349-
argValues[0] = Int64GetDatum(cdata->id);
350-
351-
argTypes[1] = CSTRINGOID;
352-
argValues[1] = CStringGetDatum(error_msg);
353-
354-
int failed_query_rc = SPI_execute_with_args("\
335+
int failed_query_rc = SPI_execute_with_args("\
355336
insert into net._http_response(id, error_msg) values ($1, $2)",
356-
argCount, argTypes, argValues, NULL, false, 1);
337+
2,
338+
(Oid[]){INT8OID, CSTRINGOID},
339+
(Datum[]){Int64GetDatum(cdata->id), CStringGetDatum(curl_easy_strerror(return_code))},
340+
NULL, false, 1);
357341

358342
if (failed_query_rc != SPI_OK_INSERT)
359343
{
360344
ereport(ERROR, errmsg("Error when inserting failed response: %s", SPI_result_code_string(failed_query_rc)));
361345
}
362346
} else {
363-
int argCount = 6;
364-
Oid argTypes[6];
365-
Datum argValues[6];
366-
char nulls[6];
367347
CurlData *cdata = NULL;
368348
char *contentType = NULL;
369-
bool timedOut = false;
370349
int http_status_code;
371350

372351
curl_easy_getinfo(eh, CURLINFO_RESPONSE_CODE, &http_status_code);
373352
curl_easy_getinfo(eh, CURLINFO_CONTENT_TYPE, &contentType);
374353
curl_easy_getinfo(eh, CURLINFO_PRIVATE, &cdata);
375354

376-
argTypes[0] = INT8OID;
377-
argValues[0] = Int64GetDatum(cdata->id);
378-
nulls[0] = ' ';
379-
380-
argTypes[1] = INT4OID;
381-
argValues[1] = Int32GetDatum(http_status_code);
382-
nulls[1] = ' ';
383-
384-
argTypes[2] = CSTRINGOID;
385-
argValues[2] = CStringGetDatum(cdata->body->data);
386-
if(cdata->body->data[0] == '\0')
387-
nulls[2] = 'n';
388-
else
389-
nulls[2] = ' ';
390-
391-
argTypes[3] = JSONBOID;
392-
argValues[3] = JsonbPGetDatum(JsonbValueToJsonb(pushJsonbValue(&cdata->response_headers, WJB_END_OBJECT, NULL)));
393-
nulls[3] = ' ';
394-
395-
argTypes[4] = CSTRINGOID;
396-
argValues[4] = CStringGetDatum(contentType);
397-
if(!contentType)
398-
nulls[4] = 'n';
399-
else
400-
nulls[4] = ' ';
401-
402-
argTypes[5] = BOOLOID;
403-
argValues[5] = BoolGetDatum(timedOut);
404-
nulls[5] = ' ';
405-
406355
int succ_query_rc = SPI_execute_with_args("\
407356
insert into net._http_response(id, status_code, content, headers, content_type, timed_out) values ($1, $2, $3, $4, $5, $6)",
408-
argCount, argTypes, argValues, nulls, false, 1);
357+
6,
358+
(Oid[]){INT8OID, INT4OID, CSTRINGOID, JSONBOID, CSTRINGOID, BOOLOID},
359+
(Datum[]){
360+
Int64GetDatum(cdata->id)
361+
, Int32GetDatum(http_status_code)
362+
, CStringGetDatum(cdata->body->data)
363+
, JsonbPGetDatum(JsonbValueToJsonb(pushJsonbValue(&cdata->response_headers, WJB_END_OBJECT, NULL)))
364+
, CStringGetDatum(contentType)
365+
// TODO Why is this hardcoded?
366+
, BoolGetDatum(false)
367+
},
368+
(char[6]){
369+
' '
370+
, [2] = cdata->body->data[0] == '\0'? 'n' : ' '
371+
, [4] = !contentType? 'n' :' '
372+
},
373+
false, 1);
409374

410375
if ( succ_query_rc != SPI_OK_INSERT)
411376
{

0 commit comments

Comments
 (0)