Skip to content

Commit

Permalink
Keep precompiled jq selectors handy
Browse files Browse the repository at this point in the history
  • Loading branch information
colinleroy committed Mar 3, 2024
1 parent 6f021f9 commit 829d1a8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 16 deletions.
6 changes: 6 additions & 0 deletions src/debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
a2tools (2.0.1.1) bionic; urgency=medium

* Keep precompiled jq selectors handy

-- Colin Leroy-Mira <[email protected]> Sun, 03 Mar 2024 21:28:19 +0200

a2tools (2.0.0.1) bionic; urgency=medium

* Protocol change: Streaming
Expand Down
65 changes: 56 additions & 9 deletions src/surl-server/helpers/jq-get.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,73 @@
#include "jq-get.h"

char out[8192];

typedef struct _precompiled_jq {
char *selector;
jq_state *jq;
} precompiled_jq;

#define N_JQS_MAX 512

static precompiled_jq jqs[N_JQS_MAX];
static int n_jqs = 0;

jq_state *jq_get_for_selector(char *selector) {
int i;
static jq_state *fallback_jq = NULL;
if (n_jqs == 0) {
memset(jqs, 0, sizeof(jqs));
}

for (i = 0; i < n_jqs; i++) {
if (!strcmp(selector, jqs[i].selector)) {
return jqs[i].jq;
}
}

if (i < N_JQS_MAX) {
jq_state *new_jq = jq_init();
if (new_jq == NULL) {
return NULL;
}
if (!jq_compile(new_jq, selector)) {
return NULL;
}
jqs[i].selector = strdup(selector);
jqs[i].jq = new_jq;
n_jqs = i + 1;
printf("Adding precompiled jq %p for selector %s [%d/%d]\n", jqs[i].jq, selector, i, N_JQS_MAX);

return new_jq;
} else {
printf("out of precompiled jqs, using static one\n");
if (fallback_jq == NULL) {
fallback_jq = jq_init();
if (fallback_jq == NULL) {
return NULL;
}
}
if (!jq_compile(fallback_jq, selector)) {
return NULL;
}
return fallback_jq;
}
}

char *jq_get(jv json_data, char *selector) {
static jq_state *jq = NULL;
jq_state *jq = NULL;
jv result;
size_t out_len = 0;
size_t rem = sizeof(out) - 1;

out[0] = '\0';

if (jq == NULL) {
jq = jq_init();
}
if (jq == NULL) {
return NULL;
}

if (!jv_is_valid(json_data)) {
return NULL;
}

if (!jq_compile(jq, selector)) {
jq = jq_get_for_selector(selector);
if (jq == NULL) {
return NULL;
}

Expand Down
37 changes: 30 additions & 7 deletions src/surl-server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,12 @@ int main(int argc, char **argv)
int sending_headers = 0;
int sending_body = 0;
curl_buffer *response = NULL;
long start_secs = 0;
long start_msecs = 0;
time_t secs;
long msecs;
struct timespec cur_time;

if (argc > 1) {
if (!strcmp(argv[1], "--help")) {
printf("Usage: %s [--verbose|--very-verbose]\n", argv[0]);
Expand Down Expand Up @@ -275,6 +277,10 @@ int main(int argc, char **argv)
}
} while (strcmp(reqbuf, "\n"));

clock_gettime(CLOCK_REALTIME, &cur_time);
start_secs = cur_time.tv_sec;
start_msecs = cur_time.tv_nsec / 1000000;

/* Perform the request */
response = surl_handle_request(method, url, headers, n_headers);
if (response == NULL) {
Expand All @@ -291,9 +297,20 @@ int main(int argc, char **argv)

/* Parse JSON if it is */
if (!strncasecmp(response->content_type, "application/json", 16)) {
clock_gettime(CLOCK_REALTIME, &cur_time);
start_secs = cur_time.tv_sec;
start_msecs = cur_time.tv_nsec / 1000000;

response->json_data = jv_parse(response->buffer);
printf(" [built json data]");

clock_gettime(CLOCK_REALTIME, &cur_time);
secs = cur_time.tv_sec - 1;
msecs = 1000 + (cur_time.tv_nsec / 1000000);

printf(" [built json data in %lums]",
(1000*(secs - start_secs))+(msecs - start_msecs));
}

printf("\n");

if (VERBOSE && VERY_VERBOSE) {
Expand All @@ -315,8 +332,6 @@ int main(int argc, char **argv)
char *translit = NULL;
unsigned short size;
size_t l;
long start_secs = 0;
long start_msecs = 0;

/* read command */
cmd = simple_serial_getc();
Expand Down Expand Up @@ -526,12 +541,20 @@ int main(int argc, char **argv)
simple_serial_putc(SURL_ERROR_NOT_JSON);
} else {
/* Extract result */
char *result = jq_get(jv_copy(response->json_data), param);
printf("RESP: JSON '%s' into %zu bytes%s, translit: %s: %zu bytes %s\n", param, bufsize,
char *result;

result = jq_get(jv_copy(response->json_data), param);

clock_gettime(CLOCK_REALTIME, &cur_time);
secs = cur_time.tv_sec - 1;
msecs = 1000 + (cur_time.tv_nsec / 1000000);

printf("RESP: JSON '%s' into %zu bytes%s, translit: %s: %zu bytes %s (%lums)\n", param, bufsize,
striphtml ? ", striphtml":"",
translit,
result != NULL ? min(strlen(result),bufsize) : 0,
result != NULL ? "" :"not found");
result != NULL ? "" :"not found",
(1000*(secs - start_secs))+(msecs - start_msecs));

if (result) {
/* We have a result */
Expand Down

0 comments on commit 829d1a8

Please sign in to comment.