2020-11-17 12:23:15 +00:00
|
|
|
package shard
|
|
|
|
|
2020-11-18 11:52:26 +00:00
|
|
|
import (
|
|
|
|
"github.com/mr-tron/base58"
|
|
|
|
)
|
|
|
|
|
2020-11-17 12:23:15 +00:00
|
|
|
// ID represents Shard identifier.
|
|
|
|
//
|
|
|
|
// Each shard should have the unique ID within
|
|
|
|
// a single instance of local storage.
|
|
|
|
type ID []byte
|
|
|
|
|
|
|
|
// NewIDFromBytes constructs ID from byte slice.
|
|
|
|
func NewIDFromBytes(v []byte) *ID {
|
|
|
|
return (*ID)(&v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (id ID) String() string {
|
2020-11-18 11:52:26 +00:00
|
|
|
return base58.Encode(id)
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns Shard identifier.
|
|
|
|
func (s *Shard) ID() *ID {
|
2020-11-19 13:53:45 +00:00
|
|
|
return s.info.ID
|
2020-11-17 12:23:15 +00:00
|
|
|
}
|
2022-03-01 08:59:05 +00:00
|
|
|
|
|
|
|
// UpdateID reads shard ID saved in the metabase and updates it if it is missing.
|
|
|
|
func (s *Shard) UpdateID() (err error) {
|
|
|
|
if err = s.metaBase.Open(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
cErr := s.metaBase.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = cErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
id, err := s.metaBase.ReadShardID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(id) != 0 {
|
|
|
|
s.info.ID = NewIDFromBytes(id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return s.metaBase.WriteShardID(*s.info.ID)
|
|
|
|
}
|