diff --git a/json_utility.go b/json_utility.go index 4209e08..c74202b 100644 --- a/json_utility.go +++ b/json_utility.go @@ -14,8 +14,9 @@ import ( ) var ( - mu = &sync.Mutex{} - metadata map[string]Payload + mu = &sync.Mutex{} + metadata = make(map[string]Payload) + updateQue = make(map[string][]byte) metadataDB *ora.Ses activeProject string @@ -74,28 +75,46 @@ func LoadPayloadsMetadata(db *ora.Ses, project string, hotloading bool, hlPeriod return nil } -func UpdateMetadataModels(md map[string][]byte) (upd, add int, err error) { +func GetMetadataForAllEntities() map[string]Payload { + return metadata +} + +func GetMetadataForEntity(t string) (Payload, bool) { + p, ok := metadata[t] + return p, ok +} + +func QueEntityModelUpdate(entityType string, v interface{}) { + updateQue[entityType], _ = json.Marshal(v) +} + +func UpdateEntityModels(forceUpdate bool) (total, upd, add int, err error) { if !inited { - return 0, 0, errors.New("webutil: metadata not initialized but update was tried.") + return 0, 0, 0, errors.New("webutil: metadata not initialized but update was tried.") } + total = len(updateQue) + forUpdate := make([]string, 0) - forCreate := make([]string, 0) + forAdd := make([]string, 0) - for k, _ := range md { + for k, _ := range updateQue { if _, exists := metadata[k]; exists { - forUpdate = append(forUpdate, k) + if forceUpdate { + forUpdate = append(forUpdate, k) + } } else { - forCreate = append(forCreate, k) + forAdd = append(forAdd, k) } } for _, k := range forUpdate { fmt.Printf("for update: %s\n", k) + _, err := metadataDB.PrepAndExe(`update entities set - entity_model = :1 - where entity_type = :2`, - string(md[k]), + entity_model = :1 + where entity_type = :2`, + string(updateQue[k]), k) if err != nil { @@ -105,39 +124,35 @@ func UpdateMetadataModels(md map[string][]byte) (upd, add int, err error) { upd++ } - for _, k := range forCreate { + blankPayload, _ := json.Marshal(Payload{}) + for _, k := range forAdd { fmt.Printf("for add: %s\n", k) - /* - _, err := metadataDB.PrepAndExe(`insert into entities - (projekat, metadata, entity_type, entity_model) - values(:1, :2, :3, :4)`, - activeProject, "", k, string(md[k])) - if err != nil { - fmt.Printf("webutility: add metadata: prep and exe: %v\n", err) - continue - } - */ + _, err := metadataDB.PrepAndExe(`insert into entities + (projekat, metadata, entity_type, entity_model) + values(:1, :2, :3, :4)`, + activeProject, + string(blankPayload), + k, + string(updateQue[k])) + + if err != nil { + fmt.Printf("webutility: add metadata: prep and exe: %v\n", err) + continue + } + metadata[k] = Payload{} add++ } - return upd, add, nil -} - -func GetMetadataForAllEntities() map[string]Payload { - return metadata -} - -func GetMetadataForEntityType(t string) Payload { - return metadata[t] + return total, upd, add, nil } -func UpdateMetadata(entityType string, p *Payload) error { - md, err := json.Marshal(p) +func ModifyMetadataForEntity(entityType string, p *Payload) error { + md, err := json.Marshal(*p) if err != nil { return err } - fmt.Printf("md: %s %s\n", entityType, string(md)) + mu.Lock() defer mu.Unlock() _, err = metadataDB.PrepAndExe(`update entities set @@ -153,10 +168,14 @@ func UpdateMetadata(entityType string, p *Payload) error { return nil } -// DecodeJSON decodes JSON data from r to v. -// Returns an error if it fails. -func DecodeJSON(r io.Reader, v interface{}) error { - return json.NewDecoder(r).Decode(v) +func DeleteEntityModel(entityType string) error { + _, err := metadataDB.PrepAndExe("delete from entities where entity_type = :1", entityType) + if err == nil { + mu.Lock() + delete(metadata, entityType) + mu.Unlock() + } + return err } // NewPayload returs a payload sceleton for entity described with etype. @@ -166,6 +185,12 @@ func NewPayload(r *http.Request, etype string) Payload { return pload } +// DecodeJSON decodes JSON data from r to v. +// Returns an error if it fails. +func DecodeJSON(r io.Reader, v interface{}) error { + return json.NewDecoder(r).Decode(v) +} + func initMetadata(project string) error { metadataDB.SetCfg(metadataDB.Cfg().SetClob(ora.S)) stmt, err := metadataDB.Prep(`select @@ -188,6 +213,9 @@ func initMetadata(project string) error { count := 0 success := 0 + if len(metadata) > 0 { + metadata = nil + } metadata = make(map[string]Payload) for rset.Next() { name := rset.Row[0].(string) @@ -264,7 +292,7 @@ func hotload(n int) { func refreshMetadata(entities []string) { for _, e := range entities { - //fmt.Printf("refreshing %s\n", e) + fmt.Printf("refreshing %s\n", e) stmt, err := metadataDB.Prep(`select metadata from entities @@ -293,7 +321,6 @@ func refreshMetadata(entities []string) { fmt.Printf("couldn't refresh: '%s' metadata\n", e) } else { metadata[e] = p - //fmt.Printf("unmarshaled %s %v\n", e, metadata[e]) } } stmt.Close()