/
dbToDbRun.go
339 lines (282 loc) · 10.1 KB
/
dbToDbRun.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package main
import (
"container/list"
"database/sql"
"errors"
"strconv"
"time"
"github.com/openmpp/go/ompp/config"
"github.com/openmpp/go/ompp/db"
"github.com/openmpp/go/ompp/omppLog"
)
// copy model run from source database to destination database
func dbToDbRun(modelName string, modelDigest string, runOpts *config.RunOptions) error {
// validate source and destination
csInp, dnInp := db.IfEmptyMakeDefaultReadOnly(modelName, runOpts.String(fromSqliteArgKey), runOpts.String(dbConnStrArgKey), runOpts.String(dbDriverArgKey))
csOut, dnOut := db.IfEmptyMakeDefault(modelName, runOpts.String(toSqliteArgKey), runOpts.String(toDbConnStrArgKey), runOpts.String(toDbDriverArgKey))
if csInp == csOut && dnInp == dnOut {
return errors.New("source same as destination: cannot overwrite model in database")
}
// open source database connection and check is it valid
srcDb, _, err := db.Open(csInp, dnInp, false)
if err != nil {
return err
}
defer srcDb.Close()
if err := db.CheckOpenmppSchemaVersion(srcDb); err != nil {
return err
}
// open destination database and check is it valid
dstDb, dbFacet, err := db.Open(csOut, dnOut, true)
if err != nil {
return err
}
defer dstDb.Close()
if err := db.CheckOpenmppSchemaVersion(dstDb); err != nil {
return err
}
// source: get model metadata
srcModel, err := db.GetModel(srcDb, modelName, modelDigest)
if err != nil {
return err
}
modelName = srcModel.Model.Name // set model name: it can be empty and only model digest specified
// find source model run metadata by id, run digest or name
runId, runDigest, runName, isFirst, isLast := runIdDigestNameFromOptions(runOpts)
if runId < 0 || runId == 0 && runName == "" && runDigest == "" && !isFirst && !isLast {
return errors.New("dbcopy invalid argument(s) run id: " + runOpts.String(runIdArgKey) + ", run name: " + runOpts.String(runNameArgKey) + ", run digest: " + runOpts.String(runDigestArgKey))
}
runRow, e := findModelRunByIdDigestName(srcDb, srcModel.Model.ModelId, runId, runDigest, runName, isFirst, isLast)
if e != nil {
return e
}
if runRow == nil {
return errors.New("model run not found: " + runOpts.String(runIdArgKey) + " " + runOpts.String(runNameArgKey) + " " + runOpts.String(runDigestArgKey))
}
// check is this run belong to the source model
if runRow.ModelId != srcModel.Model.ModelId {
return errors.New("model run " + strconv.Itoa(runRow.RunId) + " " + runRow.Name + " " + runRow.RunDigest + " does not belong to model " + modelName + " " + modelDigest)
}
// run must be completed: status success, error or exit
if !db.IsRunCompleted(runRow.Status) {
return errors.New("model run not completed: " + strconv.Itoa(runRow.RunId) + " " + runRow.Name)
}
// get full model run metadata
meta, err := db.GetRunFullText(srcDb, runRow, true, "")
if err != nil {
return err
}
// destination: get model metadata
dstModel, err := db.GetModel(dstDb, modelName, modelDigest)
if err != nil {
return err
}
// destination: get list of languages
dstLang, err := db.GetLanguages(dstDb)
if err != nil {
return err
}
// convert model run db rows into "public" format
pub, err := meta.ToPublic(srcDb, srcModel)
if err != nil {
return err
}
// if model digest validation disabled
if theCfg.isNoDigestCheck {
pub.ModelDigest = ""
}
// copy source model run metadata, parameter values, output results into destination database
_, err = copyRunDbToDb(srcDb, dstDb, dbFacet, srcModel, dstModel, meta.Run.RunId, pub, dstLang)
if err != nil {
return err
}
return nil
}
// copyRunListDbToDb do copy all model runs parameters and output tables from source to destination database
// Double format is used for float model types digest calculation, if non-empty format supplied
func copyRunListDbToDb(
srcDb *sql.DB, dstDb *sql.DB, dbFacet db.Facet, srcModel *db.ModelMeta, dstModel *db.ModelMeta, dstLang *db.LangMeta) error {
// source: get all successfully completed model runs in all languages
srcRl, err := db.GetRunFullTextList(srcDb, srcModel.Model.ModelId, true, "")
if err != nil {
return err
}
if len(srcRl) <= 0 {
return nil
}
// copy all run metadata, run parameters, output accumulators and expressions from source to destination
// model run "public" format is used
for k := range srcRl {
// convert model db rows into "public"" format
pub, err := srcRl[k].ToPublic(srcDb, srcModel)
if err != nil {
return err
}
// save into destination database
_, err = copyRunDbToDb(srcDb, dstDb, dbFacet, srcModel, dstModel, srcRl[k].Run.RunId, pub, dstLang)
if err != nil {
return err
}
}
return nil
}
// copyRunDbToDb do copy model run metadata, run parameters and output tables from source to destination database
// it return destination run id (run id in destination database)
func copyRunDbToDb(
srcDb *sql.DB, dstDb *sql.DB, dbFacet db.Facet, srcModel *db.ModelMeta, dstModel *db.ModelMeta, srcId int, pub *db.RunPub, dstLang *db.LangMeta) (int, error) {
// validate parameters
if pub == nil {
return 0, errors.New("invalid (empty) source model run metadata, source run not found or not exists")
}
// destination: convert from "public" format into destination db rows
dstRun, err := pub.FromPublic(dstDb, dstModel)
if err != nil {
return 0, err
}
// destination: save model run metadata
isExist, err := dstRun.UpdateRun(dstDb, dstModel, dstLang, theCfg.doubleFmt)
if err != nil {
return 0, err
}
dstId := dstRun.Run.RunId
if isExist { // exit if model run already exist
omppLog.Log("Model run ", srcId, " ", pub.Name, " already exists as ", dstId)
return dstId, nil
}
// copy all run parameters, output accumulators and expressions from source to destination
omppLog.Log("Model run from ", srcId, " ", pub.Name, " to ", dstId)
nP := len(srcModel.Param)
omppLog.Log(" Parameters: ", nP)
logT := time.Now().Unix()
// copy all parameters values for that model run
for j := range srcModel.Param {
// source: read parameter values
paramLt := db.ReadParamLayout{
ReadLayout: db.ReadLayout{
Name: srcModel.Param[j].Name,
FromId: srcId,
},
}
logT = omppLog.LogIfTime(logT, logPeriod, " ", j, " of ", nP, ": ", paramLt.Name)
cLst := list.New()
_, err := db.ReadParameterTo(srcDb, srcModel, ¶mLt, func(src interface{}) (bool, error) {
cLst.PushBack(src)
return true, nil
})
if err != nil {
return 0, err
}
if cLst.Len() <= 0 { // parameter data must exist for all parameters
return 0, errors.New("missing run parameter values " + paramLt.Name + " run id: " + strconv.Itoa(paramLt.FromId))
}
// destination: insert parameter values in model run
dstParamLt := db.WriteParamLayout{
WriteLayout: db.WriteLayout{
Name: dstModel.Param[j].Name,
ToId: dstId,
},
SubCount: dstRun.Param[j].SubCount,
IsToRun: true,
DoubleFmt: theCfg.doubleFmt,
}
if err = db.WriteParameterFrom(dstDb, dstModel, &dstParamLt, makeFromList(cLst)); err != nil {
return 0, err
}
}
// copy all output tables values for that model run, if the table included in run results
nT := len(srcModel.Table)
omppLog.Log(" Tables: ", nT)
for j := range srcModel.Table {
// check if table exist in model run results
var isFound bool
for k := range pub.Table {
isFound = pub.Table[k].Name == srcModel.Table[j].Name
if isFound {
break
}
}
if !isFound {
continue // skip table: it is suppressed and not in run results
}
// source: read output table accumulator
tblLt := db.ReadTableLayout{
ReadLayout: db.ReadLayout{
Name: srcModel.Table[j].Name,
FromId: srcId,
},
IsAccum: true,
}
logT = omppLog.LogIfTime(logT, logPeriod, " ", j, " of ", nT, ": ", tblLt.Name)
acLst := list.New()
_, err = db.ReadOutputTableTo(srcDb, srcModel, &tblLt, func(src interface{}) (bool, error) {
acLst.PushBack(src)
return true, nil
})
if err != nil {
return 0, err
}
// source: read output table expression values
tblLt.IsAccum = false
ecLst := list.New()
_, err = db.ReadOutputTableTo(srcDb, srcModel, &tblLt, func(src interface{}) (bool, error) {
ecLst.PushBack(src)
return true, nil
})
if err != nil {
return 0, err
}
// insert output table values (accumulators and expressions) in model run
dstTblLt := db.WriteTableLayout{
WriteLayout: db.WriteLayout{
Name: dstModel.Table[j].Name,
ToId: dstId,
},
SubCount: dstRun.Run.SubCount,
DoubleFmt: theCfg.doubleFmt,
}
err = db.WriteOutputTableFrom(dstDb, dstModel, &dstTblLt, makeFromList(acLst), makeFromList(ecLst))
if err != nil {
return 0, err
}
}
// copy entity microdata values from source run into destination
nMd := len(pub.Entity)
if nMd > 0 {
omppLog.Log(" Microdata: ", nMd)
for j := 0; j < nMd; j++ {
// source: read microdata values
microLt := db.ReadMicroLayout{
ReadLayout: db.ReadLayout{
Name: pub.Entity[j].Name,
FromId: srcId},
GenDigest: pub.Entity[j].GenDigest,
}
logT = omppLog.LogIfTime(logT, logPeriod, " ", j, " of ", nMd, ": ", microLt.Name)
cLst := list.New()
_, err := db.ReadMicrodataTo(srcDb, srcModel, µLt, func(src interface{}) (bool, error) {
cLst.PushBack(src)
return true, nil
})
if err != nil {
return 0, err
}
if cLst.Len() != pub.Entity[j].RowCount {
return 0, errors.New("missing run microdata values " + microLt.Name + " run id: " + strconv.Itoa(microLt.FromId))
}
// destination: insert microdata values into model run
dstMicroLt := db.WriteMicroLayout{
WriteLayout: db.WriteLayout{
Name: pub.Entity[j].Name,
ToId: dstId,
},
DoubleFmt: theCfg.doubleFmt,
}
if err = db.WriteMicrodataFrom(dstDb, dbFacet, dstModel, dstRun, &dstMicroLt, makeFromList(cLst)); err != nil {
return 0, err
}
}
}
return dstId, nil
}