From b87c0bff3e9c1fe3ad3c5769c2af2c454ea1a6cc Mon Sep 17 00:00:00 2001 From: ycc Date: Fri, 9 Feb 2024 22:30:00 +0100 Subject: [PATCH] add LoadAllServerCards from badger --- client/serverstorage.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/client/serverstorage.go b/client/serverstorage.go index f84f85b..aa236c6 100644 --- a/client/serverstorage.go +++ b/client/serverstorage.go @@ -126,6 +126,39 @@ func (ss *ServerStorage) LoadAllServers() ([]*Server, error) { return scs, err } +// LoadAllServers function loads all ServersCards from a badger database +func (ss *ServerStorage) LoadAllServerCards() ([]*meowlib.ServerCard, error) { + var scs []*meowlib.ServerCard + err := ss.open() + if err != nil { + return nil, err + } + defer ss.close() + err = ss.db.View(func(txn *badger.Txn) error { + opts := badger.DefaultIteratorOptions + opts.PrefetchSize = 10 + it := txn.NewIterator(opts) + defer it.Close() + for it.Rewind(); it.Valid(); it.Next() { + item := it.Item() + var sc Server + err := item.Value(func(val []byte) error { + jsonsrv, err := meowlib.SymDecrypt(GetConfig().memoryPassword, val) + if err != nil { + return err + } + return json.Unmarshal(jsonsrv, &sc) + }) + if err != nil { + return err + } + scs = append(scs, sc.GetServerCard()) + } + return nil + }) + return scs, err +} + // LoadServersFromUids function loads Servers with id in []Uid parameter from a badger database func (ss *ServerStorage) LoadServersFromUids(uids []string) ([]*Server, error) { var scs []*Server