Skip to content

Commit

Permalink
refactor: more simple code
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed May 19, 2024
1 parent 3fbc693 commit f6a1085
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 38 deletions.
16 changes: 5 additions & 11 deletions pkg/loader/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ func (r *Reconciler) Reconcile(ctx context.Context) error {
return nil
}

exists := make(map[uuid.UUID]struct{})
var priority []uuid.UUID

var next []uuid.UUID
for {
select {
case <-ctx.Done():
Expand All @@ -92,16 +90,12 @@ func (r *Reconciler) Reconcile(ctx context.Context) error {
return nil
}

if _, ok := exists[event.NodeID]; !ok {
exists[event.NodeID] = struct{}{}
priority = append(priority, event.NodeID)
}
next = append(next, event.NodeID)

for i := len(priority) - 1; i >= 0; i-- {
id := priority[i]
for i := len(next) - 1; i >= 0; i-- {
id := next[i]
if _, err := r.loader.LoadOne(ctx, id); err == nil {
delete(exists, id)
priority = append(priority[:i], priority[i+1:]...)
next = append(next[:i], next[i+1:]...)
}
}
}
Expand Down
50 changes: 23 additions & 27 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ type Storage struct {
}

var indexes = []database.IndexModel{
{
Name: "kind",
Keys: []string{scheme.KeyKind},
},
{
Name: "namespace_name",
Keys: []string{scheme.KeyNamespace, scheme.KeyName},
Expand All @@ -43,10 +47,28 @@ func New(ctx context.Context, config Config) (*Storage, error) {
return nil, err
}

if err := ensureIndexes(ctx, nodes, indexes); err != nil {
origins, err := nodes.Indexes().List(ctx)
if err != nil {
return nil, err
}

for _, index := range indexes {
var exists bool
for _, origin := range origins {
if origin.Name == index.Name {
if !reflect.DeepEqual(origin, index) {
nodes.Indexes().Drop(ctx, origin.Name)
} else {
exists = true
}
break
}
}
if !exists {
nodes.Indexes().Create(ctx, index)
}
}

return &Storage{
scheme: scheme,
nodes: nodes,
Expand Down Expand Up @@ -263,29 +285,3 @@ func (s *Storage) specToDoc(spec scheme.Spec) (*primitive.Map, error) {
}
return unstructured.Doc(), nil
}

func ensureIndexes(ctx context.Context, coll database.Collection, indexes []database.IndexModel) error {
origins, err := coll.Indexes().List(ctx)
if err != nil {
return err
}

for _, index := range indexes {
var exists bool
for _, origin := range origins {
if origin.Name == index.Name {
if !reflect.DeepEqual(origin, index) {
coll.Indexes().Drop(ctx, origin.Name)
} else {
exists = true
}
break
}
}
if !exists {
coll.Indexes().Create(ctx, index)
}
}

return nil
}

0 comments on commit f6a1085

Please sign in to comment.