Don't keep sql blob open from http server

This commit is contained in:
Jeremy Lakeman 2013-01-06 19:50:56 +10:30
parent 78f7a293d1
commit 340003ed99
2 changed files with 38 additions and 25 deletions

View File

@ -439,7 +439,9 @@ typedef struct rhizome_http_request {
int source_record_size; int source_record_size;
unsigned int source_flags; unsigned int source_flags;
sqlite3_blob *blob; const char *sql_table;
const char *sql_row;
int64_t rowid;
/* source_index used for offset in blob */ /* source_index used for offset in blob */
long long blob_end; long long blob_end;

View File

@ -312,8 +312,6 @@ int rhizome_server_free_http_request(rhizome_http_request *r)
close(r->alarm.poll.fd); close(r->alarm.poll.fd);
if (r->buffer) if (r->buffer)
free(r->buffer); free(r->buffer);
if (r->blob)
sqlite3_blob_close(r->blob);
free(r); free(r);
return 0; return 0;
} }
@ -421,7 +419,7 @@ int rhizome_server_sql_query_fill_buffer(rhizome_http_request *r, char *table, c
sqlite3_finalize(statement); sqlite3_finalize(statement);
return WHY("sqlite3 returned multiple columns for a single column query"); return WHY("sqlite3 returned multiple columns for a single column query");
} }
sqlite3_blob *blob; sqlite3_blob *blob=NULL;
const unsigned char *value; const unsigned char *value;
int column_type=sqlite3_column_type(statement, 0); int column_type=sqlite3_column_type(statement, 0);
switch(column_type) { switch(column_type) {
@ -541,18 +539,22 @@ int rhizome_server_parse_http_request(rhizome_http_request *r)
} else { } else {
// TODO: Check for Range: header and return 206 if returning partial content // TODO: Check for Range: header and return 206 if returning partial content
str_toupper_inplace(id); str_toupper_inplace(id);
long long rowid = -1; r->rowid=-1;
sqlite_exec_int64(&rowid, "select rowid from fileblobs where id='%s';", id); sqlite3_blob *blob=NULL;
if (rowid >= 0 && sqlite3_blob_open(rhizome_db, "main", "fileblobs", "data", rowid, 0, &r->blob) != SQLITE_OK) sqlite_exec_int64(&r->rowid, "select rowid from fileblobs where id='%s';", id);
rowid = -1; r->sql_table="fileblobs";
if (rowid == -1) { r->sql_row="data";
if (r->rowid >= 0 && sqlite3_blob_open(rhizome_db, "main", r->sql_table, r->sql_row, r->rowid, 0, &blob) != SQLITE_OK)
r->rowid = -1;
if (r->rowid == -1) {
rhizome_server_simple_http_response(r, 404, "<html><h1>Payload not found</h1></html>\r\n"); rhizome_server_simple_http_response(r, 404, "<html><h1>Payload not found</h1></html>\r\n");
} else { } else {
r->source_index = 0; r->source_index = 0;
r->blob_end = sqlite3_blob_bytes(r->blob); r->blob_end = sqlite3_blob_bytes(blob);
rhizome_server_http_response_header(r, 200, "application/binary", r->blob_end - r->source_index); rhizome_server_http_response_header(r, 200, "application/binary", r->blob_end - r->source_index);
r->request_type |= RHIZOME_HTTP_REQUEST_BLOB; r->request_type |= RHIZOME_HTTP_REQUEST_BLOB;
} }
if (blob) sqlite3_blob_close(blob);
} }
} else if (str_startswith(path, "/rhizome/manifest/", (const char **)&id)) { } else if (str_startswith(path, "/rhizome/manifest/", (const char **)&id)) {
// TODO: Stream the specified manifest // TODO: Stream the specified manifest
@ -576,20 +578,26 @@ int rhizome_server_parse_http_request(rhizome_http_request *r)
DEBUGF("Looking for manifest between %s and %s", DEBUGF("Looking for manifest between %s and %s",
bid_low,bid_high); bid_low,bid_high);
long long rowid = -1; r->rowid = -1;
sqlite_exec_int64(&rowid, "select rowid from manifests where id between '%s' and '%s';", bid_low,bid_high); sqlite3_blob *blob=NULL;
if (rowid >= 0 && sqlite3_blob_open(rhizome_db, "main", "manifests", "manifest", rowid, 0, &r->blob) != SQLITE_OK) r->sql_table="manifests";
rowid = -1; r->sql_row="manifest";
if (rowid == -1) { sqlite_exec_int64(&r->rowid, "select rowid from manifests where id between '%s' and '%s';", bid_low,bid_high);
if (r->rowid >= 0 && sqlite3_blob_open(rhizome_db, "main", r->sql_table, r->sql_row, r->rowid, 0, &blob) != SQLITE_OK)
r->rowid = -1;
if (r->rowid == -1) {
DEBUGF("Row not found"); DEBUGF("Row not found");
rhizome_server_simple_http_response(r, 404, "<html><h1>Payload not found</h1></html>\r\n"); rhizome_server_simple_http_response(r, 404, "<html><h1>Payload not found</h1></html>\r\n");
} else { } else {
DEBUGF("row id = %d",rowid); DEBUGF("row id = %d",r->rowid);
r->source_index = 0; r->source_index = 0;
r->blob_end = sqlite3_blob_bytes(r->blob); r->blob_end = sqlite3_blob_bytes(blob);
rhizome_server_http_response_header(r, 200, "application/binary", r->blob_end - r->source_index); rhizome_server_http_response_header(r, 200, "application/binary", r->blob_end - r->source_index);
r->request_type |= RHIZOME_HTTP_REQUEST_BLOB; r->request_type |= RHIZOME_HTTP_REQUEST_BLOB;
} }
if (blob)
sqlite3_blob_close(blob);
}else { }else {
rhizome_server_simple_http_response(r, 404, "<html><h1>Not found</h1></html>\r\n"); rhizome_server_simple_http_response(r, 404, "<html><h1>Not found</h1></html>\r\n");
DEBUGF("Sending 404 not found for '%s'",path); DEBUGF("Sending 404 not found for '%s'",path);
@ -771,20 +779,23 @@ int rhizome_server_http_send_bytes(rhizome_http_request *r)
} }
r->buffer_size=read_size; r->buffer_size=read_size;
} }
if(sqlite3_blob_read(r->blob,&r->buffer[0],read_size,r->source_index) sqlite3_blob *blob=NULL;
==SQLITE_OK) int ret=sqlite3_blob_open(rhizome_db, "main", r->sql_table, r->sql_row, r->rowid, 0, &blob);
{ if (ret==SQLITE_OK){
if (sqlite3_blob_read(blob,&r->buffer[0],read_size,r->source_index)==SQLITE_OK) {
r->buffer_length = read_size; r->buffer_length = read_size;
r->source_index+=read_size; r->source_index+=read_size;
r->request_type|=RHIZOME_HTTP_REQUEST_FROMBUFFER; r->request_type|=RHIZOME_HTTP_REQUEST_FROMBUFFER;
} }
}
if (blob)
sqlite3_blob_close(blob);
// if the database was busy, just wait for the alarm to fire again.
} }
if (r->source_index >= r->blob_end){ if (r->source_index < r->blob_end)
sqlite3_blob_close(r->blob);
r->blob=0;
}else
r->request_type|=RHIZOME_HTTP_REQUEST_BLOB; r->request_type|=RHIZOME_HTTP_REQUEST_BLOB;
} }
break; break;