From 47c82c6a03d6bfd52fc9d671bec997c441bbb2c9 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Tue, 27 Feb 2024 10:49:47 -0500 Subject: [PATCH] feat: add new All API that will return all queried locations --- storage/provider/provider.go | 40 ++++++++++++++++++++++++++++++++++++ storage/storage.go | 1 + 2 files changed, 41 insertions(+) diff --git a/storage/provider/provider.go b/storage/provider/provider.go index 7381fdd..fd421a5 100644 --- a/storage/provider/provider.go +++ b/storage/provider/provider.go @@ -7,6 +7,7 @@ import ( "git.lumeweb.com/LumeWeb/libs5-go/service" "git.lumeweb.com/LumeWeb/libs5-go/storage" "git.lumeweb.com/LumeWeb/libs5-go/types" + "github.com/samber/lo" "go.uber.org/zap" "sync" "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 { err := s.services.P2P().UpVote(uri.NodeId()) if err != nil { diff --git a/storage/storage.go b/storage/storage.go index 8c96454..d4a7f7c 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -124,6 +124,7 @@ func NewStorageLocationMap() StorageLocationMap { type StorageLocationProvider interface { Start() error Next() (SignedStorageLocation, error) + All() ([]SignedStorageLocation, error) Upvote(uri SignedStorageLocation) error Downvote(uri SignedStorageLocation) error }