/
getModel.go
118 lines (93 loc) · 3.4 KB
/
getModel.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
// Copyright (c) 2016 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package main
import (
"github.com/openmpp/go/ompp/db"
"github.com/openmpp/go/ompp/omppLog"
)
// ModelDicByDigest return model_dic db row by model digest.
func (mc *ModelCatalog) ModelDicByDigest(digest string) (db.ModelDicRow, bool) {
// if model digest is empty then return empty results
if digest == "" {
omppLog.Log("Warning: invalid (empty) model digest")
return db.ModelDicRow{}, false
}
// find model index by digest
mc.theLock.Lock()
defer mc.theLock.Unlock()
idx, ok := mc.indexByDigest(digest)
if !ok {
return db.ModelDicRow{}, false // model not found, empty result
}
return mc.modelLst[idx].meta.Model, true
}
// ModelDicByDigestOrName return model_dic db row by model digest or name.
func (mc *ModelCatalog) ModelDicByDigestOrName(dn string) (db.ModelDicRow, bool) {
// if model digest is empty then return empty results
if dn == "" {
omppLog.Log("Warning: invalid (empty) model digest and name")
return db.ModelDicRow{}, false
}
// find model index by digest
mc.theLock.Lock()
defer mc.theLock.Unlock()
idx, ok := mc.indexByDigestOrName(dn)
if !ok {
return db.ModelDicRow{}, false // model not found, empty result
}
return mc.modelLst[idx].meta.Model, true
}
// ModelMetaByDigestOrName return model_dic db row by model digest or name.
func (mc *ModelCatalog) ModelMetaByDigestOrName(dn string) (*db.ModelMeta, bool) {
// if model digest-or-name is empty then return empty results
if dn == "" {
omppLog.Log("Warning: invalid (empty) model digest and name")
return &db.ModelMeta{}, false
}
// if model metadata not loaded then read it from database
idx, ok := mc.loadModelMeta(dn)
if !ok {
return &db.ModelMeta{}, false // return empty result: model not found or error
}
// lock model catalog and return copy of model metadata
mc.theLock.Lock()
defer mc.theLock.Unlock()
m, err := mc.modelLst[idx].meta.Clone()
if err != nil {
omppLog.Log("Error at model metadata clone: ", dn, ": ", err.Error())
return &db.ModelMeta{}, false
}
return m, true
}
// loadModelMeta read language-neutral model metadata from db.
// If metadata already loaded then do skip db reading and return index in model list.
// It search model by digest or name if digest not found.
// Return index in model list or < 0 on error or if model not found.
func (mc *ModelCatalog) loadModelMeta(dn string) (int, bool) {
// if model digest-name is empty then return empty results
if dn == "" {
omppLog.Log("Warning: invalid (empty) model digest and name")
return 0, false
}
// find model index by digest or name
mc.theLock.Lock()
defer mc.theLock.Unlock()
idx, ok := mc.indexByDigestOrName(dn)
if !ok {
omppLog.Log("Warning: model digest or name not found: ", dn)
return 0, false // model not found, index is negative
}
if mc.modelLst[idx].meta != nil && mc.modelLst[idx].isMetaFull { // exit if model metadata already loaded
return idx, true
}
// read metadata from database
m, err := db.GetModelById(mc.modelLst[idx].dbConn, mc.modelLst[idx].meta.Model.ModelId)
if err != nil {
omppLog.Log("Error at get model metadata: ", dn, ": ", err.Error())
return 0, false
}
// store model metadata
mc.modelLst[idx].isMetaFull = true
mc.modelLst[idx].meta = m
return idx, true
}