From 8995cddfa56c6a0a50f2e5db8319c500dddb8e3e Mon Sep 17 00:00:00 2001 From: Ralph Slooten Date: Fri, 21 Feb 2025 16:58:20 +1300 Subject: [PATCH] Chore: Handle BLOB storage for default database differently to rqlite to reduce memory overhead (#447) --- internal/storage/messages.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/storage/messages.go b/internal/storage/messages.go index 752d7fc..8eebe2e 100644 --- a/internal/storage/messages.go +++ b/internal/storage/messages.go @@ -104,8 +104,15 @@ func Store(body *[]byte) (string, error) { // insert compressed raw message encoded := dbEncoder.EncodeAll(*body, make([]byte, 0, int(size))) - hexStr := hex.EncodeToString(encoded) - _, err = tx.Exec(fmt.Sprintf(`INSERT INTO %s (ID, Email) VALUES(?, x'%s')`, tenant("mailbox_data"), hexStr), id) // #nosec + + if sqlDriver == "rqlite" { + // rqlite does not support binary data in query, so we need to encode the compressed message into hexadecimal + // string and then generate the SQL query, which is more memory intensive especially with large messages + hexStr := hex.EncodeToString(encoded) + _, err = tx.Exec(fmt.Sprintf(`INSERT INTO %s (ID, Email) VALUES(?, x'%s')`, tenant("mailbox_data"), hexStr), id) // #nosec + } else { + _, err = tx.Exec(fmt.Sprintf(`INSERT INTO %s (ID, Email) VALUES(?, ?)`, tenant("mailbox_data")), id, encoded) // #nosec + } if err != nil { return "", err } @@ -114,6 +121,8 @@ func Store(body *[]byte) (string, error) { return "", err } + encoded = nil + // extract tags using pre-set tag filters, empty slice if not set tags := findTagsInRawMessage(body)