feat: add new All API that will return all queried locations

This commit is contained in:
Derrick Hammer 2024-02-27 10:49:47 -05:00
parent e9f4a7b0b9
commit 47c82c6a03
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 41 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"git.lumeweb.com/LumeWeb/libs5-go/service" "git.lumeweb.com/LumeWeb/libs5-go/service"
"git.lumeweb.com/LumeWeb/libs5-go/storage" "git.lumeweb.com/LumeWeb/libs5-go/storage"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/samber/lo"
"go.uber.org/zap" "go.uber.org/zap"
"sync" "sync"
"time" "time"
@ -157,6 +158,45 @@ func (s *StorageLocationProviderImpl) Next() (storage.SignedStorageLocation, err
} }
} }
func (s *StorageLocationProviderImpl) All() ([]storage.SignedStorageLocation, error) {
s.timeout = time.Now().Add(s.timeoutDuration)
for {
if len(s.availableNodes) > 0 {
s.isWaitingForUri = false
return lo.FilterMap[*encoding.NodeId, storage.SignedStorageLocation](s.availableNodes, func(nodeId *encoding.NodeId, index int) (storage.SignedStorageLocation, bool) {
nodIdStr, err := nodeId.ToString()
if err != nil {
s.logger.Error("Error decoding node id", zap.Error(err))
return nil, false
}
uri, exists := s.uris[nodIdStr]
if !exists {
s.logger.Error("Could not find uri for node id", zap.String("nodeId", nodIdStr))
return nil, false
}
return storage.NewSignedStorageLocation(nodeId, uri), true
}), nil
}
s.isWaitingForUri = true
if s.isTimedOut {
hashStr, err := s.hash.ToString()
if err != nil {
return nil, err
}
return nil, fmt.Errorf("Could not download raw file: Timed out after %s %s", s.timeoutDuration.String(), hashStr)
}
time.Sleep(10 * time.Millisecond) // Replace with a proper wait/notify mechanism if applicable
}
}
func (s *StorageLocationProviderImpl) Upvote(uri storage.SignedStorageLocation) error { func (s *StorageLocationProviderImpl) Upvote(uri storage.SignedStorageLocation) error {
err := s.services.P2P().UpVote(uri.NodeId()) err := s.services.P2P().UpVote(uri.NodeId())
if err != nil { if err != nil {

View File

@ -124,6 +124,7 @@ func NewStorageLocationMap() StorageLocationMap {
type StorageLocationProvider interface { type StorageLocationProvider interface {
Start() error Start() error
Next() (SignedStorageLocation, error) Next() (SignedStorageLocation, error)
All() ([]SignedStorageLocation, error)
Upvote(uri SignedStorageLocation) error Upvote(uri SignedStorageLocation) error
Downvote(uri SignedStorageLocation) error Downvote(uri SignedStorageLocation) error
} }