refactor: abstract button creation to a generic utility method

This commit is contained in:
Derrick Hammer 2024-01-06 09:46:01 -05:00
parent 4959270f51
commit 9654fadfee
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 54 additions and 11 deletions

35
node.go
View File

@ -1,26 +1,41 @@
package libs5_go
import (
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/service"
"git.lumeweb.com/LumeWeb/libs5-go/structs"
"git.lumeweb.com/LumeWeb/libs5-go/utils"
"github.com/vmihailenco/msgpack/v5"
bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
"time"
)
type Metadata interface {
ToJson() map[string]interface{}
}
type services struct {
type Services struct {
p2p *service.P2P
}
func (s *Services) P2P() *service.P2P {
return s.p2p
}
const cacheBucketName = "object-cache"
type Node struct {
nodeConfig *NodeConfig
metadataCache *structs.Map
started bool
hashQueryRoutingTable *structs.Map
services services
services Services
cacheBucket *bolt.Bucket
}
func (n *Node) Services() *Services {
return &n.services
}
func NewNode(config *NodeConfig) *Node {
@ -57,6 +72,20 @@ func (n *Node) Db() *bolt.DB {
return nil
}
func (n *Node) Start() error {
err :=
utils.CreateBucket(cacheBucketName, n.Db(), func(bucket *bolt.Bucket) {
n.cacheBucket = bucket
})
if err != nil {
return err
}
n.started = true
return nil
}
/*
func (n *Node) Services() *S5Services {
if n.nodeConfig != nil {
@ -117,7 +146,7 @@ func (n *Node) GetCachedStorageLocations(hash *encoding.Multihash, types []int)
}
}
locations[NodeId(key)] = storageLocation
locations[key] = storageLocation
}
}
return locations, nil

View File

@ -8,6 +8,7 @@ import (
"git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
"git.lumeweb.com/LumeWeb/libs5-go/structs"
"git.lumeweb.com/LumeWeb/libs5-go/utils"
"github.com/vmihailenco/msgpack/v5"
bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
@ -93,16 +94,10 @@ func (p *P2P) Init() error {
}
p.localNodeID = encoding.NewNodeId(p.nodeKeyPair.PublicKey())
err := p.Node().Db().Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(nodeBucketName))
if err != nil {
return err
}
err := utils.CreateBucket(nodeBucketName, p.Node().Db(), func(bucket *bolt.Bucket) {
p.nodesBucket = bucket
return nil
})
if err != nil {
return err
}

19
utils/bucket.go Normal file
View File

@ -0,0 +1,19 @@
package utils
import bolt "go.etcd.io/bbolt"
func CreateBucket(name string, db *bolt.DB, cb func(bucket *bolt.Bucket)) error {
err :=
db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists([]byte(name))
if err != nil {
return err
}
cb(bucket)
return nil
})
return err
}