Commit 67337ffa8f7b0c4ef26040aa3f09c492d659afab
1 parent
2ea67927f5
Exists in
master
and in
1 other branch
payload editing
Showing
1 changed file
with
67 additions
and
40 deletions
Show diff stats
json_utility.go
... | ... | @@ -14,8 +14,9 @@ import ( |
14 | 14 | ) |
15 | 15 | |
16 | 16 | var ( |
17 | - mu = &sync.Mutex{} | |
18 | - metadata map[string]Payload | |
17 | + mu = &sync.Mutex{} | |
18 | + metadata = make(map[string]Payload) | |
19 | + updateQue = make(map[string][]byte) | |
19 | 20 | |
20 | 21 | metadataDB *ora.Ses |
21 | 22 | activeProject string |
... | ... | @@ -74,28 +75,46 @@ func LoadPayloadsMetadata(db *ora.Ses, project string, hotloading bool, hlPeriod |
74 | 75 | return nil |
75 | 76 | } |
76 | 77 | |
77 | -func UpdateMetadataModels(md map[string][]byte) (upd, add int, err error) { | |
78 | +func GetMetadataForAllEntities() map[string]Payload { | |
79 | + return metadata | |
80 | +} | |
81 | + | |
82 | +func GetMetadataForEntity(t string) (Payload, bool) { | |
83 | + p, ok := metadata[t] | |
84 | + return p, ok | |
85 | +} | |
86 | + | |
87 | +func QueEntityModelUpdate(entityType string, v interface{}) { | |
88 | + updateQue[entityType], _ = json.Marshal(v) | |
89 | +} | |
90 | + | |
91 | +func UpdateEntityModels(forceUpdate bool) (total, upd, add int, err error) { | |
78 | 92 | if !inited { |
79 | - return 0, 0, errors.New("webutil: metadata not initialized but update was tried.") | |
93 | + return 0, 0, 0, errors.New("webutil: metadata not initialized but update was tried.") | |
80 | 94 | } |
81 | 95 | |
96 | + total = len(updateQue) | |
97 | + | |
82 | 98 | forUpdate := make([]string, 0) |
83 | - forCreate := make([]string, 0) | |
99 | + forAdd := make([]string, 0) | |
84 | 100 | |
85 | - for k, _ := range md { | |
101 | + for k, _ := range updateQue { | |
86 | 102 | if _, exists := metadata[k]; exists { |
87 | - forUpdate = append(forUpdate, k) | |
103 | + if forceUpdate { | |
104 | + forUpdate = append(forUpdate, k) | |
105 | + } | |
88 | 106 | } else { |
89 | - forCreate = append(forCreate, k) | |
107 | + forAdd = append(forAdd, k) | |
90 | 108 | } |
91 | 109 | } |
92 | 110 | |
93 | 111 | for _, k := range forUpdate { |
94 | 112 | fmt.Printf("for update: %s\n", k) |
113 | + | |
95 | 114 | _, err := metadataDB.PrepAndExe(`update entities set |
96 | - entity_model = :1 | |
97 | - where entity_type = :2`, | |
98 | - string(md[k]), | |
115 | + entity_model = :1 | |
116 | + where entity_type = :2`, | |
117 | + string(updateQue[k]), | |
99 | 118 | k) |
100 | 119 | |
101 | 120 | if err != nil { |
... | ... | @@ -105,39 +124,35 @@ func UpdateMetadataModels(md map[string][]byte) (upd, add int, err error) { |
105 | 124 | upd++ |
106 | 125 | } |
107 | 126 | |
108 | - for _, k := range forCreate { | |
127 | + blankPayload, _ := json.Marshal(Payload{}) | |
128 | + for _, k := range forAdd { | |
109 | 129 | fmt.Printf("for add: %s\n", k) |
110 | - /* | |
111 | - _, err := metadataDB.PrepAndExe(`insert into entities | |
112 | - (projekat, metadata, entity_type, entity_model) | |
113 | - values(:1, :2, :3, :4)`, | |
114 | - activeProject, "", k, string(md[k])) | |
115 | 130 | |
116 | - if err != nil { | |
117 | - fmt.Printf("webutility: add metadata: prep and exe: %v\n", err) | |
118 | - continue | |
119 | - } | |
120 | - */ | |
131 | + _, err := metadataDB.PrepAndExe(`insert into entities | |
132 | + (projekat, metadata, entity_type, entity_model) | |
133 | + values(:1, :2, :3, :4)`, | |
134 | + activeProject, | |
135 | + string(blankPayload), | |
136 | + k, | |
137 | + string(updateQue[k])) | |
138 | + | |
139 | + if err != nil { | |
140 | + fmt.Printf("webutility: add metadata: prep and exe: %v\n", err) | |
141 | + continue | |
142 | + } | |
143 | + metadata[k] = Payload{} | |
121 | 144 | add++ |
122 | 145 | } |
123 | 146 | |
124 | - return upd, add, nil | |
125 | -} | |
126 | - | |
127 | -func GetMetadataForAllEntities() map[string]Payload { | |
128 | - return metadata | |
129 | -} | |
130 | - | |
131 | -func GetMetadataForEntityType(t string) Payload { | |
132 | - return metadata[t] | |
147 | + return total, upd, add, nil | |
133 | 148 | } |
134 | 149 | |
135 | -func UpdateMetadata(entityType string, p *Payload) error { | |
136 | - md, err := json.Marshal(p) | |
150 | +func ModifyMetadataForEntity(entityType string, p *Payload) error { | |
151 | + md, err := json.Marshal(*p) | |
137 | 152 | if err != nil { |
138 | 153 | return err |
139 | 154 | } |
140 | - fmt.Printf("md: %s %s\n", entityType, string(md)) | |
155 | + | |
141 | 156 | mu.Lock() |
142 | 157 | defer mu.Unlock() |
143 | 158 | _, err = metadataDB.PrepAndExe(`update entities set |
... | ... | @@ -153,10 +168,14 @@ func UpdateMetadata(entityType string, p *Payload) error { |
153 | 168 | return nil |
154 | 169 | } |
155 | 170 | |
156 | -// DecodeJSON decodes JSON data from r to v. | |
157 | -// Returns an error if it fails. | |
158 | -func DecodeJSON(r io.Reader, v interface{}) error { | |
159 | - return json.NewDecoder(r).Decode(v) | |
171 | +func DeleteEntityModel(entityType string) error { | |
172 | + _, err := metadataDB.PrepAndExe("delete from entities where entity_type = :1", entityType) | |
173 | + if err == nil { | |
174 | + mu.Lock() | |
175 | + delete(metadata, entityType) | |
176 | + mu.Unlock() | |
177 | + } | |
178 | + return err | |
160 | 179 | } |
161 | 180 | |
162 | 181 | // NewPayload returs a payload sceleton for entity described with etype. |
... | ... | @@ -166,6 +185,12 @@ func NewPayload(r *http.Request, etype string) Payload { |
166 | 185 | return pload |
167 | 186 | } |
168 | 187 | |
188 | +// DecodeJSON decodes JSON data from r to v. | |
189 | +// Returns an error if it fails. | |
190 | +func DecodeJSON(r io.Reader, v interface{}) error { | |
191 | + return json.NewDecoder(r).Decode(v) | |
192 | +} | |
193 | + | |
169 | 194 | func initMetadata(project string) error { |
170 | 195 | metadataDB.SetCfg(metadataDB.Cfg().SetClob(ora.S)) |
171 | 196 | stmt, err := metadataDB.Prep(`select |
... | ... | @@ -188,6 +213,9 @@ func initMetadata(project string) error { |
188 | 213 | |
189 | 214 | count := 0 |
190 | 215 | success := 0 |
216 | + if len(metadata) > 0 { | |
217 | + metadata = nil | |
218 | + } | |
191 | 219 | metadata = make(map[string]Payload) |
192 | 220 | for rset.Next() { |
193 | 221 | name := rset.Row[0].(string) |
... | ... | @@ -264,7 +292,7 @@ func hotload(n int) { |
264 | 292 | |
265 | 293 | func refreshMetadata(entities []string) { |
266 | 294 | for _, e := range entities { |
267 | - //fmt.Printf("refreshing %s\n", e) | |
295 | + fmt.Printf("refreshing %s\n", e) | |
268 | 296 | stmt, err := metadataDB.Prep(`select |
269 | 297 | metadata |
270 | 298 | from entities |
... | ... | @@ -293,7 +321,6 @@ func refreshMetadata(entities []string) { |
293 | 321 | fmt.Printf("couldn't refresh: '%s' metadata\n", e) |
294 | 322 | } else { |
295 | 323 | metadata[e] = p |
296 | - //fmt.Printf("unmarshaled %s %v\n", e, metadata[e]) | |
297 | 324 | } |
298 | 325 | } |
299 | 326 | stmt.Close() | ... | ... |