批准 dataset 分类入库;SKIP LOCKED 分批;销毁默认 fail closed。restore 演练拒绝共享 env DSN。 Co-authored-by: Cursor <cursoragent@cursor.com>
71 lines
1.2 KiB
Go
71 lines
1.2 KiB
Go
package dataset
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
)
|
|
|
|
type Registry struct {
|
|
mu sync.Mutex
|
|
byID map[string]Dataset
|
|
}
|
|
|
|
func NewRegistry() *Registry {
|
|
return &Registry{byID: map[string]Dataset{}}
|
|
}
|
|
|
|
func (r *Registry) Put(d Dataset) error {
|
|
if d.ID == "" {
|
|
return fmt.Errorf("dataset: id required")
|
|
}
|
|
if err := ValidateClass(d.Class); err != nil {
|
|
return err
|
|
}
|
|
if d.Status == "" {
|
|
d.Status = "approved"
|
|
}
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.byID[d.ID] = d
|
|
return nil
|
|
}
|
|
|
|
func (r *Registry) Get(id string) (Dataset, bool) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
d, ok := r.byID[id]
|
|
return d, ok
|
|
}
|
|
|
|
func (r *Registry) ListApproved() []Dataset {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
out := make([]Dataset, 0, len(r.byID))
|
|
for _, d := range r.byID {
|
|
if d.Status == "approved" || d.Status == "imported" {
|
|
out = append(out, d)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func LoadRegistryFile(path string) (*Registry, error) {
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var items []Dataset
|
|
if err := json.Unmarshal(raw, &items); err != nil {
|
|
return nil, err
|
|
}
|
|
reg := NewRegistry()
|
|
for _, d := range items {
|
|
if err := reg.Put(d); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return reg, nil
|
|
}
|