Skip to content

Commit

Permalink
fix: init settings
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Oct 16, 2024
1 parent dcde91a commit fb63898
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 72 deletions.
59 changes: 7 additions & 52 deletions internal/bootstrap/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,28 @@ import (
"github.com/synctv-org/synctv/internal/db"
"github.com/synctv-org/synctv/internal/model"
"github.com/synctv-org/synctv/internal/settings"
"github.com/zijiren233/gencontainer/heap"
)

var _ heap.Interface[maxHeapItem] = (*maxHeap)(nil)

type maxHeapItem struct {
priority int
settings.Setting
}

type maxHeap struct {
items []maxHeapItem
}

func (h *maxHeap) Len() int {
return len(h.items)
}

func (h *maxHeap) Less(i, j int) bool {
return h.items[i].priority > h.items[j].priority
}

func (h *maxHeap) Swap(i, j int) {
h.items[i], h.items[j] = h.items[j], h.items[i]
}

func (h *maxHeap) Push(x maxHeapItem) {
h.items = append(h.items, x)
}

func (h *maxHeap) Pop() maxHeapItem {
n := len(h.items)
x := h.items[n-1]
h.items = h.items[:n-1]
return x
}

func InitSetting(ctx context.Context) error {
ss := []settings.Setting{}
for _, s := range settings.Settings {
ss = append(ss, s)
}
return initAndFixSettings(ss)
return initAndFixSettings()
}

func settingEqual(s *model.Setting, b settings.Setting) bool {
return s.Type == b.Type() && s.Group == b.Group() && s.Name == b.Name()
}

func initAndFixSettings(ss []settings.Setting) error {
func initAndFixSettings() error {
settingsCache, err := db.GetSettingItemsToMap()
if err != nil {
return err
}
var setting *model.Setting
list := new(maxHeap)
for _, s := range ss {
heap.Push(list, maxHeapItem{
priority: s.InitPriority(),
Setting: s,
})
}

for list.Len() > 0 {
b := heap.Pop(list)
for {
b, ok := settings.PopNeedInit()
if !ok {
return nil
}

if sc, ok := settingsCache[b.Name()]; ok && settingEqual(sc, b) {
setting = sc
Expand All @@ -95,6 +52,4 @@ func initAndFixSettings(ss []settings.Setting) error {
}
}
}

return nil
}
13 changes: 8 additions & 5 deletions internal/settings/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ func newBool(name string, value bool, group model.SettingGroup, options ...BoolS
return b
}

func (b *Bool) SetInitPriority(priority int) {
b.initPriority = priority
}

func (b *Bool) SetBeforeInit(beforeInit func(BoolSetting, bool) (bool, error)) {
b.beforeInit = beforeInit
}
Expand Down Expand Up @@ -111,6 +107,10 @@ func (b *Bool) Get() bool {
}

func (b *Bool) Init(value string) error {
if b.Inited() {
return ErrSettingAlreadyInited
}

v, err := b.Parse(value)
if err != nil {
return err
Expand All @@ -129,6 +129,8 @@ func (b *Bool) Init(value string) error {
b.afterInit(b, v)
}

b.inited = true

return nil
}

Expand Down Expand Up @@ -221,9 +223,10 @@ func CoverBoolSetting(k string, v bool, g model.SettingGroup, options ...BoolSet
b := newBool(k, v, g, options...)
Settings[k] = b
if GroupSettings[g] == nil {
GroupSettings[g] = make(map[string]Setting)
GroupSettings[g] = make(map[model.SettingGroup]Setting)
}
GroupSettings[g][k] = b
pushNeedInit(b)
return b
}

Expand Down
13 changes: 8 additions & 5 deletions internal/settings/floate64.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ func newFloat64(name string, value float64, group model.SettingGroup, options ..
return f
}

func (f *Float64) SetInitPriority(priority int) {
f.initPriority = priority
}

func (f *Float64) SetBeforeInit(beforeInit func(Float64Setting, float64) (float64, error)) {
f.beforeInit = beforeInit
}
Expand Down Expand Up @@ -122,6 +118,10 @@ func (f *Float64) Stringify(value float64) string {
}

func (f *Float64) Init(value string) error {
if f.Inited() {
return ErrSettingAlreadyInited
}

v, err := f.Parse(value)
if err != nil {
return err
Expand All @@ -140,6 +140,8 @@ func (f *Float64) Init(value string) error {
f.afterInit(f, v)
}

f.inited = true

return nil
}

Expand Down Expand Up @@ -239,9 +241,10 @@ func CoverFloat64Setting(k string, v float64, g model.SettingGroup, options ...F
f := newFloat64(k, v, g, options...)
Settings[k] = f
if GroupSettings[g] == nil {
GroupSettings[g] = make(map[string]Setting)
GroupSettings[g] = make(map[model.SettingGroup]Setting)
}
GroupSettings[g][k] = f
pushNeedInit(f)
return f
}

Expand Down
13 changes: 8 additions & 5 deletions internal/settings/int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ func newInt64(name string, value int64, group model.SettingGroup, options ...Int
return i
}

func (i *Int64) SetInitPriority(priority int) {
i.initPriority = priority
}

func (i *Int64) SetBeforeInit(beforeInit func(Int64Setting, int64) (int64, error)) {
i.beforeInit = beforeInit
}
Expand Down Expand Up @@ -121,6 +117,10 @@ func (i *Int64) Stringify(value int64) string {
}

func (i *Int64) Init(value string) error {
if i.Inited() {
return ErrSettingAlreadyInited
}

v, err := i.Parse(value)
if err != nil {
return err
Expand All @@ -139,6 +139,8 @@ func (i *Int64) Init(value string) error {
i.afterInit(i, v)
}

i.inited = true

return nil
}

Expand Down Expand Up @@ -238,9 +240,10 @@ func CoverInt64Setting(k string, v int64, g model.SettingGroup, options ...Int64
i := newInt64(k, v, g, options...)
Settings[k] = i
if GroupSettings[g] == nil {
GroupSettings[g] = make(map[string]Setting)
GroupSettings[g] = make(map[model.SettingGroup]Setting)
}
GroupSettings[g][k] = i
pushNeedInit(i)
return i
}

Expand Down
81 changes: 81 additions & 0 deletions internal/settings/setting.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,94 @@
package settings

import (
"errors"
"fmt"

json "github.com/json-iterator/go"
"github.com/synctv-org/synctv/internal/model"
"github.com/zijiren233/gencontainer/heap"
)

var ErrSettingAlreadyInited = errors.New("setting already inited")

var _ heap.Interface[maxHeapItem] = (*maxHeap)(nil)

type maxHeapItem struct {
priority int
Setting
}

type maxHeap struct {
items []maxHeapItem
}

func (h *maxHeap) Len() int {
return len(h.items)
}

func (h *maxHeap) Less(i, j int) bool {
return h.items[i].priority > h.items[j].priority
}

func (h *maxHeap) Swap(i, j int) {
h.items[i], h.items[j] = h.items[j], h.items[i]
}

func (h *maxHeap) Push(x maxHeapItem) {
h.items = append(h.items, x)
}

func (h *maxHeap) Pop() maxHeapItem {
n := len(h.items)
x := h.items[n-1]
h.items = h.items[:n-1]
return x
}

var (
Settings = make(map[string]Setting)
GroupSettings = make(map[model.SettingGroup]map[string]Setting)
needInit = new(maxHeap)
)

func pushNeedInit(s Setting) {
if s == nil {
panic("push need init failed, setting is nil")
}
for i, item := range needInit.items {
if item.Setting.Name() == s.Name() {
heap.Remove(needInit, i)
break
}
}
heap.Push(needInit, maxHeapItem{
priority: s.InitPriority(),
Setting: s,
})
}

func hasNeedInit() bool {
return needInit.Len() > 0
}

func PopNeedInit() (Setting, bool) {
for hasNeedInit() {
item := heap.Pop(needInit)
s := item.Setting
if s.Inited() {
continue
}
return s, true
}
return nil, false
}

type Setting interface {
Name() string
Type() model.SettingType
Group() model.SettingGroup
Init(string) error
Inited() bool
SetInitPriority(int)
InitPriority() int
String() string
Expand Down Expand Up @@ -49,6 +121,7 @@ type setting struct {
settingType model.SettingType
group model.SettingGroup
initPriority int
inited bool
}

func (d *setting) Name() string {
Expand All @@ -66,3 +139,11 @@ func (d *setting) Group() model.SettingGroup {
func (d *setting) InitPriority() int {
return d.initPriority
}

func (d *setting) Inited() bool {
return d.inited
}

func (d *setting) SetInitPriority(priority int) {
d.initPriority = priority
}
13 changes: 8 additions & 5 deletions internal/settings/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ func newString(name string, value string, group model.SettingGroup, options ...S
return s
}

func (s *String) SetInitPriority(priority int) {
s.initPriority = priority
}

func (s *String) SetBeforeInit(beforeInit func(StringSetting, string) (string, error)) {
s.beforeInit = beforeInit
}
Expand Down Expand Up @@ -117,6 +113,10 @@ func (s *String) Stringify(value string) string {
}

func (s *String) Init(value string) error {
if s.Inited() {
return ErrSettingAlreadyInited
}

v, err := s.Parse(value)
if err != nil {
return err
Expand All @@ -135,6 +135,8 @@ func (s *String) Init(value string) error {
s.afterInit(s, v)
}

s.inited = true

return nil
}

Expand Down Expand Up @@ -238,9 +240,10 @@ func CoverStringSetting(k string, v string, g model.SettingGroup, options ...Str
s := newString(k, v, g, options...)
Settings[k] = s
if GroupSettings[g] == nil {
GroupSettings[g] = make(map[string]Setting)
GroupSettings[g] = make(map[model.SettingGroup]Setting)
}
GroupSettings[g][k] = s
pushNeedInit(s)
return s
}

Expand Down

0 comments on commit fb63898

Please sign in to comment.