Compare commits

...

2 Commits

3 changed files with 47 additions and 8 deletions

View File

@ -4,8 +4,8 @@ import (
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/storage"
"git.lumeweb.com/LumeWeb/libs5-go/structs"
"git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/emirpasic/gods/sets/hashset"
"github.com/vmihailenco/msgpack/v5"
"go.uber.org/zap"
"log"
@ -146,7 +146,7 @@ func (h *HashQuery) HandleMessage(message IncomingMessageData) error {
}
}
var peers *hashset.Set
var peers *structs.SetImpl
hashString, err := h.hash.ToString()
logger.Debug("HashQuery", zap.Any("hashString", hashString))
if err != nil {
@ -154,7 +154,7 @@ func (h *HashQuery) HandleMessage(message IncomingMessageData) error {
}
peersVal, ok := mediator.HashQueryRoutingTable().Get(hashString)
if ok {
peers = peersVal.(*hashset.Set)
peers = peersVal.(*structs.SetImpl)
if !peers.Contains(peer.Id()) {
peers.Add(peer.Id())
}
@ -162,7 +162,7 @@ func (h *HashQuery) HandleMessage(message IncomingMessageData) error {
return nil
}
peerList := hashset.New()
peerList := structs.NewSet()
peerList.Add(peer.Id())
mediator.HashQueryRoutingTable().Put(hashString, peerList)

View File

@ -6,9 +6,9 @@ import (
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/storage"
"git.lumeweb.com/LumeWeb/libs5-go/structs"
"git.lumeweb.com/LumeWeb/libs5-go/types"
"git.lumeweb.com/LumeWeb/libs5-go/utils"
"github.com/emirpasic/gods/sets/hashset"
"github.com/vmihailenco/msgpack/v5"
"go.uber.org/zap"
)
@ -89,12 +89,12 @@ func (s *StorageLocation) HandleMessage(message IncomingMessageData) error {
return err
}
var list *hashset.Set
var list *structs.SetImpl
listVal, ok := mediator.HashQueryRoutingTable().Get(hashStr) // Implement HashQueryRoutingTable method
if !ok {
list = hashset.New()
list = structs.NewSet()
} else {
list = listVal.(*hashset.Set)
list = listVal.(*structs.SetImpl)
}
for _, peerIdVal := range list.Values() {

39
structs/set.go Normal file
View File

@ -0,0 +1,39 @@
package structs
import (
"github.com/emirpasic/gods/sets"
"github.com/emirpasic/gods/sets/hashset"
"sync"
)
var _ sets.Set = (*SetImpl)(nil)
type SetImpl struct {
*hashset.Set
mutex *sync.RWMutex
}
func NewSet() *SetImpl {
return &SetImpl{
Set: hashset.New(),
mutex: &sync.RWMutex{},
}
}
func (s *SetImpl) Add(items ...interface{}) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.Set.Add(items...)
}
func (s *SetImpl) Remove(items ...interface{}) {
s.mutex.Lock()
defer s.mutex.Unlock()
s.Set.Remove(items...)
}
func (s *SetImpl) Contains(items ...interface{}) bool {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.Set.Contains(items...)
}