Skip to content

Commit

Permalink
feat: Add Store.List method
Browse files Browse the repository at this point in the history
List all IDs of the data available in the store.
  • Loading branch information
LBeernaertProton committed Oct 7, 2022
1 parent 695c831 commit 8322437
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
34 changes: 34 additions & 0 deletions store/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"github.com/sirupsen/logrus"
"io/fs"
"os"
"path/filepath"

Expand Down Expand Up @@ -115,6 +117,38 @@ func (c *onDiskStore) Delete(messageIDs ...imap.InternalMessageID) error {
return nil
}

func (c *onDiskStore) List() ([]imap.InternalMessageID, error) {
if c.sem != nil {
c.sem.Lock()
defer c.sem.Unlock()
}

var ids []imap.InternalMessageID

if err := filepath.Walk(c.path, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
return nil
}

id, err := imap.InternalMessageIDFromString(info.Name())
if err != nil {
logrus.WithError(err).Errorf("Invalid id file in cache: %v", info.Name())
}

ids = append(ids, id)

return nil
}); err != nil {
return nil, err
}

return ids, nil
}

func (c *onDiskStore) Close() error {
return nil
}
Expand Down
1 change: 1 addition & 0 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Store interface {
Set(messageID imap.InternalMessageID, literal []byte) error
Delete(messageID ...imap.InternalMessageID) error
Close() error
List() ([]imap.InternalMessageID, error)
}

type Builder interface {
Expand Down
12 changes: 12 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package store_test

import (
"github.com/ProtonMail/gluon/imap"
"runtime"
"testing"

Expand All @@ -24,6 +25,7 @@ func TestOnDiskStore(t *testing.T) {
require.NoError(t, err)

testStore(t, store)
testStoreList(t, store)
})
}
}
Expand All @@ -38,6 +40,16 @@ func testStore(t *testing.T, store store.Store) {
require.Equal(t, []byte("literal3"), must(store.Get(3)))
}

func testStoreList(t *testing.T, store store.Store) {
require.NoError(t, store.Set(1, []byte("literal1")))
require.NoError(t, store.Set(2, []byte("literal2")))
require.NoError(t, store.Set(3, []byte("literal3")))

list, err := store.List()
require.NoError(t, err)
require.ElementsMatch(t, list, []imap.InternalMessageID{1, 2, 3})
}

func must[T any](val T, err error) T {
if err != nil {
panic(err)
Expand Down

0 comments on commit 8322437

Please sign in to comment.