kaspad/dbaccess/subnetwork.go
Ori Newman 7609c50641
[NOD-885] Use database.Key and database.Bucket instead of byte slices (#692)
* [NOD-885] Create database.Key type

* [NOD-885] Rename FullKey()->FullKeyBytes() and Key()->KeyBytes()

* [NOD-885] Make Key.String return a hex string

* [NOD-885] Rename key parts

* [NOD-885] Rename separator->bucketSeparator

* [NOD-885] Rename SuffixBytes->Suffix and PrefixBytes->Prefix

* [NOD-885] Change comments

* [NOD-885] Change key prefix to bucket

* [NOD-885] Don't use database.NewKey inside dbaccess

* [NOD-885] Fix nil bug in Bucket.Path()

* [NOD-885] Rename helpers.go -> keys.go

* [NOD-885] Unexport database.NewKey

* [NOD-885] Remove redundant code in Bucket.Path()
2020-04-08 12:12:21 +03:00

46 lines
1.2 KiB
Go

package dbaccess
import (
"github.com/kaspanet/kaspad/database"
"github.com/kaspanet/kaspad/util/subnetworkid"
)
var subnetworkBucket = database.MakeBucket([]byte("subnetworks"))
func subnetworkKey(subnetworkID *subnetworkid.SubnetworkID) *database.Key {
return subnetworkBucket.Key(subnetworkID[:])
}
// FetchSubnetworkData returns the subnetwork data by its ID.
func FetchSubnetworkData(context Context, subnetworkID *subnetworkid.SubnetworkID) ([]byte, error) {
accessor, err := context.accessor()
if err != nil {
return nil, err
}
key := subnetworkKey(subnetworkID)
return accessor.Get(key)
}
// StoreSubnetwork stores mappings from ID of the subnetwork to the subnetwork data.
func StoreSubnetwork(context Context, subnetworkID *subnetworkid.SubnetworkID, subnetworkData []byte) error {
accessor, err := context.accessor()
if err != nil {
return err
}
key := subnetworkKey(subnetworkID)
return accessor.Put(key, subnetworkData)
}
// HasSubnetwork returns whether the subnetwork exists in the database.
func HasSubnetwork(context Context, subnetworkID *subnetworkid.SubnetworkID) (bool, error) {
accessor, err := context.accessor()
if err != nil {
return false, err
}
key := subnetworkKey(subnetworkID)
return accessor.Has(key)
}