mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-04-07 11:08:36 +00:00
Refactor sqlite_retry_state into rhizome_list_cursor
This commit is contained in:
parent
cb420c61e3
commit
94274ba5fa
@ -1945,8 +1945,7 @@ int app_rhizome_list(const struct cli_parsed *parsed, struct cli_context *contex
|
||||
return WHYF("Invalid <recipient: %s", recipient_hex);
|
||||
cursor.is_recipient_set = 1;
|
||||
}
|
||||
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
||||
if (rhizome_list_open(&retry, &cursor) == -1) {
|
||||
if (rhizome_list_open(&cursor) == -1) {
|
||||
keyring_free(keyring);
|
||||
return -1;
|
||||
}
|
||||
@ -1968,7 +1967,7 @@ int app_rhizome_list(const struct cli_parsed *parsed, struct cli_context *contex
|
||||
cli_columns(context, NELS(headers), headers);
|
||||
size_t rowcount = 0;
|
||||
int n;
|
||||
while ((n = rhizome_list_next(&retry, &cursor)) == 1) {
|
||||
while ((n = rhizome_list_next(&cursor)) == 1) {
|
||||
++rowcount;
|
||||
if (rowcount <= rowoffset)
|
||||
continue;
|
||||
|
@ -687,14 +687,15 @@ struct rhizome_list_cursor {
|
||||
uint64_t rowid_since;
|
||||
// Set by calling the next() function.
|
||||
rhizome_manifest *manifest;
|
||||
// Private state.
|
||||
// Private state - implementation that could change.
|
||||
sqlite_retry_state _retry;
|
||||
sqlite3_stmt *_statement;
|
||||
uint64_t _rowid_current;
|
||||
uint64_t _rowid_last; // for re-opening query
|
||||
};
|
||||
|
||||
int rhizome_list_open(sqlite_retry_state *, struct rhizome_list_cursor *);
|
||||
int rhizome_list_next(sqlite_retry_state *, struct rhizome_list_cursor *);
|
||||
int rhizome_list_open(struct rhizome_list_cursor *);
|
||||
int rhizome_list_next(struct rhizome_list_cursor *);
|
||||
void rhizome_list_commit(struct rhizome_list_cursor *);
|
||||
void rhizome_list_release(struct rhizome_list_cursor *);
|
||||
|
||||
|
@ -1575,7 +1575,7 @@ rollback:
|
||||
*
|
||||
* @author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
int rhizome_list_open(sqlite_retry_state *retry, struct rhizome_list_cursor *c)
|
||||
int rhizome_list_open(struct rhizome_list_cursor *c)
|
||||
{
|
||||
if (config.debug.rhizome)
|
||||
DEBUGF("c=%p c->service=%s c->name=%s c->sender=%s c->recipient=%s c->rowid_since=%"PRIu64" c->_rowid_last=%"PRIu64,
|
||||
@ -1609,18 +1609,19 @@ int rhizome_list_open(sqlite_retry_state *retry, struct rhizome_list_cursor *c)
|
||||
}
|
||||
if (strbuf_overrun(b))
|
||||
RETURN(WHYF("SQL command too long: %s", strbuf_str(b)));
|
||||
c->_statement = sqlite_prepare(retry, strbuf_str(b));
|
||||
c->_retry = SQLITE_RETRY_STATE_DEFAULT;
|
||||
c->_statement = sqlite_prepare(&c->_retry, strbuf_str(b));
|
||||
if (c->_statement == NULL)
|
||||
RETURN(-1);
|
||||
if (c->service && sqlite_bind(retry, c->_statement, NAMED|STATIC_TEXT, "@service", c->service, END) == -1)
|
||||
if (c->service && sqlite_bind(&c->_retry, c->_statement, NAMED|STATIC_TEXT, "@service", c->service, END) == -1)
|
||||
goto failure;
|
||||
if (c->name && sqlite_bind(retry, c->_statement, NAMED|STATIC_TEXT, "@name", c->name, END) == -1)
|
||||
if (c->name && sqlite_bind(&c->_retry, c->_statement, NAMED|STATIC_TEXT, "@name", c->name, END) == -1)
|
||||
goto failure;
|
||||
if (c->is_sender_set && sqlite_bind(retry, c->_statement, NAMED|SID_T, "@sender", &c->sender, END) == -1)
|
||||
if (c->is_sender_set && sqlite_bind(&c->_retry, c->_statement, NAMED|SID_T, "@sender", &c->sender, END) == -1)
|
||||
goto failure;
|
||||
if (c->is_recipient_set && sqlite_bind(retry, c->_statement, NAMED|SID_T, "@recipient", &c->recipient, END) == -1)
|
||||
if (c->is_recipient_set && sqlite_bind(&c->_retry, c->_statement, NAMED|SID_T, "@recipient", &c->recipient, END) == -1)
|
||||
goto failure;
|
||||
if (c->_rowid_last && sqlite_bind(retry, c->_statement, NAMED|INT64, "@last", c->_rowid_last, END) == -1)
|
||||
if (c->_rowid_last && sqlite_bind(&c->_retry, c->_statement, NAMED|INT64, "@last", c->_rowid_last, END) == -1)
|
||||
goto failure;
|
||||
c->manifest = NULL;
|
||||
c->_rowid_current = 0;
|
||||
@ -1641,7 +1642,7 @@ failure:
|
||||
*
|
||||
* @author Andrew Bettison <andrew@servalproject.com>
|
||||
*/
|
||||
int rhizome_list_next(sqlite_retry_state *retry, struct rhizome_list_cursor *c)
|
||||
int rhizome_list_next(struct rhizome_list_cursor *c)
|
||||
{
|
||||
if (config.debug.rhizome)
|
||||
DEBUGF("c=%p c->service=%s c->name=%s c->sender=%s c->recipient=%s c->rowid_since=%"PRIu64" c->_rowid_last=%"PRIu64,
|
||||
@ -1654,7 +1655,7 @@ int rhizome_list_next(sqlite_retry_state *retry, struct rhizome_list_cursor *c)
|
||||
c->_rowid_last
|
||||
);
|
||||
IN();
|
||||
if (c->_statement == NULL && rhizome_list_open(retry, c) == -1)
|
||||
if (c->_statement == NULL && rhizome_list_open(c) == -1)
|
||||
RETURN(-1);
|
||||
while (1) {
|
||||
if (c->manifest) {
|
||||
@ -1662,7 +1663,7 @@ int rhizome_list_next(sqlite_retry_state *retry, struct rhizome_list_cursor *c)
|
||||
c->_rowid_current = 0;
|
||||
c->manifest = NULL;
|
||||
}
|
||||
if (sqlite_step_retry(retry, c->_statement) != SQLITE_ROW)
|
||||
if (sqlite_step_retry(&c->_retry, c->_statement) != SQLITE_ROW)
|
||||
break;
|
||||
assert(sqlite3_column_count(c->_statement) == 6);
|
||||
assert(sqlite3_column_type(c->_statement, 0) == SQLITE_TEXT);
|
||||
|
@ -454,7 +454,7 @@ static int restful_rhizome_newsince(rhizome_http_request *r, const char *remaind
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int restful_rhizome_bundlelist_json_content_chunk(sqlite_retry_state *retry, struct rhizome_http_request *r, strbuf b)
|
||||
static int restful_rhizome_bundlelist_json_content_chunk(struct rhizome_http_request *r, strbuf b)
|
||||
{
|
||||
const char *headers[] = {
|
||||
".token",
|
||||
@ -487,7 +487,7 @@ static int restful_rhizome_bundlelist_json_content_chunk(sqlite_retry_state *ret
|
||||
return 1;
|
||||
case LIST_ROWS:
|
||||
{
|
||||
int ret = rhizome_list_next(retry, &r->u.list.cursor);
|
||||
int ret = rhizome_list_next(&r->u.list.cursor);
|
||||
if (ret == -1)
|
||||
return -1;
|
||||
if (ret == 0) {
|
||||
@ -567,12 +567,11 @@ static int restful_rhizome_bundlelist_json_content(struct http_request *hr, unsi
|
||||
{
|
||||
rhizome_http_request *r = (rhizome_http_request *) hr;
|
||||
assert(bufsz > 0);
|
||||
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
||||
int ret = rhizome_list_open(&retry, &r->u.list.cursor);
|
||||
int ret = rhizome_list_open(&r->u.list.cursor);
|
||||
if (ret == -1)
|
||||
return -1;
|
||||
strbuf b = strbuf_local((char *)buf, bufsz);
|
||||
while ((ret = restful_rhizome_bundlelist_json_content_chunk(&retry, r, b)) != -1) {
|
||||
while ((ret = restful_rhizome_bundlelist_json_content_chunk(r, b)) != -1) {
|
||||
if (strbuf_overrun(b)) {
|
||||
if (config.debug.rhizome)
|
||||
DEBUGF("overrun by %zu bytes", strbuf_count(b) - strbuf_len(b));
|
||||
|
Loading…
x
Reference in New Issue
Block a user