2020-11-19 12:41:10 +00:00
|
|
|
package shard
|
|
|
|
|
2021-12-27 11:03:15 +00:00
|
|
|
import "errors"
|
|
|
|
|
2020-11-19 12:41:10 +00:00
|
|
|
// Mode represents enumeration of Shard work modes.
|
|
|
|
type Mode uint32
|
|
|
|
|
2021-12-27 11:03:15 +00:00
|
|
|
// ErrReadOnlyMode is returned when it is impossible to apply operation
|
|
|
|
// that changes shard's memory due to the "read-only" shard's mode.
|
|
|
|
var ErrReadOnlyMode = errors.New("shard is in read-only mode")
|
|
|
|
|
2020-11-19 12:41:10 +00:00
|
|
|
// TODO: more detailed description of shard modes.
|
|
|
|
|
|
|
|
const (
|
2021-12-27 11:03:15 +00:00
|
|
|
// ModeReadWrite is a Mode value for shard that is available
|
|
|
|
// for read and write operations. Default shard mode.
|
|
|
|
ModeReadWrite Mode = iota
|
2020-11-19 12:41:10 +00:00
|
|
|
|
2021-12-27 11:03:15 +00:00
|
|
|
// ModeReadOnly is a Mode value for shard that does not
|
|
|
|
// accept write operation but is readable.
|
2020-11-19 12:41:10 +00:00
|
|
|
ModeReadOnly
|
|
|
|
)
|
|
|
|
|
|
|
|
func (m Mode) String() string {
|
|
|
|
switch m {
|
|
|
|
default:
|
|
|
|
return "UNDEFINED"
|
2021-12-27 11:03:15 +00:00
|
|
|
case ModeReadWrite:
|
|
|
|
return "READ_WRITE"
|
2020-11-19 12:41:10 +00:00
|
|
|
case ModeReadOnly:
|
|
|
|
return "READ_ONLY"
|
|
|
|
}
|
|
|
|
}
|
2020-11-19 12:56:10 +00:00
|
|
|
|
|
|
|
// SetMode sets mode of the shard.
|
|
|
|
//
|
|
|
|
// Returns any error encountered that did not allow
|
2021-12-27 11:03:15 +00:00
|
|
|
// setting shard mode.
|
2020-11-19 12:56:10 +00:00
|
|
|
func (s *Shard) SetMode(m Mode) error {
|
2021-12-27 18:07:02 +00:00
|
|
|
s.m.Lock()
|
|
|
|
defer s.m.Unlock()
|
|
|
|
|
|
|
|
s.info.Mode = m
|
2020-11-19 13:51:58 +00:00
|
|
|
|
|
|
|
return nil
|
2020-11-19 12:56:10 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 18:07:02 +00:00
|
|
|
// GetMode returns mode of the shard.
|
|
|
|
func (s *Shard) GetMode() Mode {
|
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
|
|
|
|
|
|
|
return s.info.Mode
|
2020-11-19 12:56:10 +00:00
|
|
|
}
|