1
0
mirror of https://github.com/axllent/mailpit.git synced 2025-03-17 21:18:19 +02:00

Chore: Use memory pointer for internal message parsing & storage

This commit is contained in:
Ralph Slooten 2024-01-02 13:14:21 +13:00
parent bf181eaad5
commit e0dc3726bc
5 changed files with 22 additions and 18 deletions

View File

@ -124,9 +124,9 @@ func Close() {
// Store will save an email to the database tables.
// Returns the database ID of the saved message.
func Store(body []byte) (string, error) {
func Store(body *[]byte) (string, error) {
// Parse message body with enmime
env, err := enmime.ReadEnvelope(bytes.NewReader(body))
env, err := enmime.ReadEnvelope(bytes.NewReader(*body))
if err != nil {
logger.Log().Warningf("[db] %s", err.Error())
return "", nil
@ -170,7 +170,7 @@ func Store(body []byte) (string, error) {
}
// extract tags from body matches based on --tag
tagStr := findTagsInRawMessage(&body)
tagStr := findTagsInRawMessage(body)
// extract tags from X-Tags header
headerTags := strings.TrimSpace(env.Root.Header.Get("X-Tags"))
@ -192,7 +192,7 @@ func Store(body []byte) (string, error) {
defer tx.Rollback()
subject := env.GetHeader("Subject")
size := len(body)
size := len(*body)
inline := len(env.Inlines)
attachments := len(env.Attachments)
snippet := tools.CreateSnippet(env.Text, env.HTML)
@ -205,7 +205,7 @@ func Store(body []byte) (string, error) {
}
// insert compressed raw message
compressed := dbEncoder.EncodeAll(body, make([]byte, 0, len(body)))
compressed := dbEncoder.EncodeAll(*body, make([]byte, 0, size))
_, err = tx.Exec("INSERT INTO mailbox_data(ID, Email) values(?,?)", id, string(compressed))
if err != nil {
return "", err

View File

@ -16,7 +16,7 @@ func TestTextEmailInserts(t *testing.T) {
assertEqualStats(t, 0, 0)
for i := 0; i < testRuns; i++ {
if _, err := Store(testTextEmail); err != nil {
if _, err := Store(&testTextEmail); err != nil {
t.Log("error ", err)
t.Fail()
}
@ -50,7 +50,7 @@ func TestMimeEmailInserts(t *testing.T) {
start := time.Now()
for i := 0; i < testRuns; i++ {
if _, err := Store(testMimeEmail); err != nil {
if _, err := Store(&testMimeEmail); err != nil {
t.Log("error ", err)
t.Fail()
}
@ -79,7 +79,7 @@ func TestRetrieveMimeEmail(t *testing.T) {
t.Log("Testing mime email retrieval")
id, err := Store(testMimeEmail)
id, err := Store(&testMimeEmail)
if err != nil {
t.Log("error ", err)
t.Fail()
@ -123,7 +123,7 @@ func TestMessageSummary(t *testing.T) {
t.Log("Testing message summary")
if _, err := Store(testMimeEmail); err != nil {
if _, err := Store(&testMimeEmail); err != nil {
t.Log("error ", err)
t.Fail()
}
@ -154,7 +154,7 @@ func BenchmarkImportText(b *testing.B) {
defer Close()
for i := 0; i < b.N; i++ {
if _, err := Store(testTextEmail); err != nil {
if _, err := Store(&testTextEmail); err != nil {
b.Log("error ", err)
b.Fail()
}
@ -166,7 +166,7 @@ func BenchmarkImportMime(b *testing.B) {
defer Close()
for i := 0; i < b.N; i++ {
if _, err := Store(testMimeEmail); err != nil {
if _, err := Store(&testMimeEmail); err != nil {
b.Log("error ", err)
b.Fail()
}

View File

@ -34,7 +34,9 @@ func TestSearch(t *testing.T) {
t.Fail()
}
if _, err := Store(buf.Bytes()); err != nil {
bufBytes := buf.Bytes()
if _, err := Store(&bufBytes); err != nil {
t.Log("error ", err)
t.Fail()
}
@ -85,11 +87,11 @@ func TestSearchDelete100(t *testing.T) {
t.Log("Testing search delete of 100 messages")
for i := 0; i < 100; i++ {
if _, err := Store(testTextEmail); err != nil {
if _, err := Store(&testTextEmail); err != nil {
t.Log("error ", err)
t.Fail()
}
if _, err := Store(testMimeEmail); err != nil {
if _, err := Store(&testMimeEmail); err != nil {
t.Log("error ", err)
t.Fail()
}
@ -123,7 +125,7 @@ func TestSearchDelete1100(t *testing.T) {
t.Log("Testing search delete of 1100 messages")
for i := 0; i < 1100; i++ {
if _, err := Store(testTextEmail); err != nil {
if _, err := Store(&testTextEmail); err != nil {
t.Log("error ", err)
t.Fail()
}

View File

@ -15,7 +15,7 @@ func TestTags(t *testing.T) {
ids := []string{}
for i := 0; i < 10; i++ {
id, err := Store(testMimeEmail)
id, err := Store(&testMimeEmail)
if err != nil {
t.Log("error ", err)
t.Fail()
@ -48,7 +48,7 @@ func TestTags(t *testing.T) {
}
// test 20 tags
id, err := Store(testMimeEmail)
id, err := Store(&testMimeEmail)
if err != nil {
t.Log("error ", err)
t.Fail()

View File

@ -274,7 +274,9 @@ func insertEmailData(t *testing.T) {
t.Fail()
}
id, err := storage.Store(buf.Bytes())
bufBytes := buf.Bytes()
id, err := storage.Store(&bufBytes)
if err != nil {
t.Log("error ", err)
t.Fail()