refactor: use interfaces, rename struct to be an impl

This commit is contained in:
Derrick Hammer 2024-01-07 03:56:50 -05:00
parent 26a51a25d5
commit ca1e2dcf72
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 14 additions and 14 deletions

View File

@ -18,15 +18,15 @@ const cacheBucketName = "object-cache"
type NodeImpl struct { type NodeImpl struct {
nodeConfig *config.NodeConfig nodeConfig *config.NodeConfig
metadataCache *structs.Map metadataCache structs.Map
started bool started bool
hashQueryRoutingTable *structs.Map hashQueryRoutingTable structs.Map
services interfaces.Services services interfaces.Services
cacheBucket *bolt.Bucket cacheBucket *bolt.Bucket
} }
func (n *NodeImpl) Services() *interfaces.Services { func (n *NodeImpl) Services() interfaces.Services {
return &n.services return n.services
} }
func NewNode(config *config.NodeConfig) *NodeImpl { func NewNode(config *config.NodeConfig) *NodeImpl {
@ -37,7 +37,7 @@ func NewNode(config *config.NodeConfig) *NodeImpl {
hashQueryRoutingTable: structs.NewMap(), hashQueryRoutingTable: structs.NewMap(),
} }
} }
func (n *NodeImpl) HashQueryRoutingTable() *structs.Map { func (n *NodeImpl) HashQueryRoutingTable() structs.Map {
return n.hashQueryRoutingTable return n.hashQueryRoutingTable
} }
@ -95,15 +95,15 @@ func (n *NodeImpl) Start() error {
return nil return nil
} }
*/ */
func (n *NodeImpl) GetCachedStorageLocations(hash *encoding.Multihash, types []int) (map[string]*interfaces.StorageLocation, error) { func (n *NodeImpl) GetCachedStorageLocations(hash *encoding.Multihash, types []int) (map[string]interfaces.StorageLocation, error) {
locations := make(map[string]*interfaces.StorageLocation) locations := make(map[string]interfaces.StorageLocation)
locationMap, err := n.readStorageLocationsFromDB(hash) locationMap, err := n.readStorageLocationsFromDB(hash)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(locationMap) == 0 { if len(locationMap) == 0 {
return make(map[string]*interfaces.StorageLocation), nil return make(map[string]interfaces.StorageLocation), nil
} }
ts := time.Now().Unix() ts := time.Now().Unix()
@ -133,7 +133,7 @@ func (n *NodeImpl) GetCachedStorageLocations(hash *encoding.Multihash, types []i
storageLocation := NewStorageLocation(t, addresses, expiry) storageLocation := NewStorageLocation(t, addresses, expiry)
if len(value) > 4 { if len(value) > 4 {
if providerMessage, ok := value[4].([]byte); ok { if providerMessage, ok := value[4].([]byte); ok {
(*storageLocation).SetProviderMessage(providerMessage) (storageLocation).SetProviderMessage(providerMessage)
} }
} }
@ -157,7 +157,7 @@ func (n *NodeImpl) readStorageLocationsFromDB(hash *encoding.Multihash) (storage
return locationMap, nil return locationMap, nil
} }
func (n *NodeImpl) AddStorageLocation(hash *encoding.Multihash, nodeId *encoding.NodeId, location *interfaces.StorageLocation, message []byte, config *config.NodeConfig) error { func (n *NodeImpl) AddStorageLocation(hash *encoding.Multihash, nodeId *encoding.NodeId, location interfaces.StorageLocation, message []byte, config *config.NodeConfig) error {
// Read existing storage locations // Read existing storage locations
locationDb, err := n.readStorageLocationsFromDB(hash) locationDb, err := n.readStorageLocationsFromDB(hash)
if err != nil { if err != nil {
@ -170,7 +170,7 @@ func (n *NodeImpl) AddStorageLocation(hash *encoding.Multihash, nodeId *encoding
} }
// Get or create the inner map for the specific type // Get or create the inner map for the specific type
innerMap, exists := locationDb[(*location).Type()] innerMap, exists := locationDb[location.Type()]
if !exists { if !exists {
innerMap = make(nodeStorage, 1) innerMap = make(nodeStorage, 1)
innerMap[nodeIdStr] = make(nodeDetailsStorage, 1) innerMap[nodeIdStr] = make(nodeDetailsStorage, 1)
@ -178,13 +178,13 @@ func (n *NodeImpl) AddStorageLocation(hash *encoding.Multihash, nodeId *encoding
// Create location map with new data // Create location map with new data
locationMap := make(map[int]interface{}, 3) locationMap := make(map[int]interface{}, 3)
locationMap[1] = (*location).Parts locationMap[1] = location.Parts()
locationMap[3] = (*location).Expiry locationMap[3] = location.Expiry()
locationMap[4] = message locationMap[4] = message
// Update the inner map with the new location // Update the inner map with the new location
innerMap[nodeIdStr] = locationMap innerMap[nodeIdStr] = locationMap
locationDb[(*location).Type()] = innerMap locationDb[location.Type()] = innerMap
// Serialize the updated map and store it in the database // Serialize the updated map and store it in the database
packedBytes, err := msgpack.Marshal(locationDb) packedBytes, err := msgpack.Marshal(locationDb)