Skip to content

Commit f4daab4

Browse files
committed
model: Use DatabaseModel to create mapper.Info
The core mapper API uses mapper.Info sctructs which can be created just by inspecting a TableSchema. However, having the DatabaseModel now centralizing accesses to the mapper API and containing both the Model types and the Schema, we can pre-create the mapper.Info.Metadata sctructs and cache them so we create Info sctructs more efficiently Signed-off-by: Adrian Moreno <[email protected]>
1 parent 3ff43c5 commit f4daab4

File tree

15 files changed

+298
-140
lines changed

15 files changed

+298
-140
lines changed

cache/cache.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func newIndex(columns ...string) index {
6666
// RowCache is a collections of Models hashed by UUID
6767
type RowCache struct {
6868
name string
69-
schema ovsdb.TableSchema
69+
dbModel *model.DatabaseModel
7070
dataType reflect.Type
7171
cache map[string]model.Model
7272
indexes columnToValue
@@ -90,7 +90,7 @@ func (r *RowCache) RowByModel(m model.Model) model.Model {
9090
if reflect.TypeOf(m) != r.dataType {
9191
return nil
9292
}
93-
info, _ := mapper.NewInfo(r.name, &r.schema, m)
93+
info, _ := r.dbModel.NewModelInfo(m)
9494
uuid, err := info.FieldByColumn("_uuid")
9595
if err != nil {
9696
return nil
@@ -120,11 +120,11 @@ func (r *RowCache) Create(uuid string, m model.Model, checkIndexes bool) error {
120120
if reflect.TypeOf(m) != r.dataType {
121121
return fmt.Errorf("expected data of type %s, but got %s", r.dataType.String(), reflect.TypeOf(m).String())
122122
}
123-
info, err := mapper.NewInfo(r.name, &r.schema, m)
123+
info, err := r.dbModel.NewModelInfo(m)
124124
if err != nil {
125125
return err
126126
}
127-
newIndexes := newColumnToValue(r.schema.Indexes)
127+
newIndexes := newColumnToValue(r.dbModel.Schema().Table(r.name).Indexes)
128128
for index := range r.indexes {
129129
val, err := valueFromIndex(info, index)
130130
if err != nil {
@@ -156,16 +156,17 @@ func (r *RowCache) Update(uuid string, m model.Model, checkIndexes bool) error {
156156
return fmt.Errorf("row %s does not exist", uuid)
157157
}
158158
oldRow := model.Clone(r.cache[uuid])
159-
oldInfo, err := mapper.NewInfo(r.name, &r.schema, oldRow)
159+
oldInfo, err := r.dbModel.NewModelInfo(oldRow)
160160
if err != nil {
161161
return err
162162
}
163-
newInfo, err := mapper.NewInfo(r.name, &r.schema, m)
163+
newInfo, err := r.dbModel.NewModelInfo(m)
164164
if err != nil {
165165
return err
166166
}
167-
newIndexes := newColumnToValue(r.schema.Indexes)
168-
oldIndexes := newColumnToValue(r.schema.Indexes)
167+
indexes := r.dbModel.Schema().Table(r.name).Indexes
168+
newIndexes := newColumnToValue(indexes)
169+
oldIndexes := newColumnToValue(indexes)
169170
var errs []error
170171
for index := range r.indexes {
171172
var err error
@@ -218,7 +219,7 @@ func (r *RowCache) Update(uuid string, m model.Model, checkIndexes bool) error {
218219
}
219220

220221
func (r *RowCache) IndexExists(row model.Model) error {
221-
info, err := mapper.NewInfo(r.name, &r.schema, row)
222+
info, err := r.dbModel.NewModelInfo(row)
222223
if err != nil {
223224
return err
224225
}
@@ -252,7 +253,7 @@ func (r *RowCache) Delete(uuid string) error {
252253
return fmt.Errorf("row %s does not exist", uuid)
253254
}
254255
oldRow := r.cache[uuid]
255-
oldInfo, err := mapper.NewInfo(r.name, &r.schema, oldRow)
256+
oldInfo, err := r.dbModel.NewModelInfo(oldRow)
256257
if err != nil {
257258
return err
258259
}
@@ -280,6 +281,7 @@ func (r *RowCache) Rows() []string {
280281

281282
func (r *RowCache) RowsByCondition(conditions []ovsdb.Condition) ([]model.Model, error) {
282283
var results []model.Model
284+
schema := r.dbModel.Schema().Table(r.name)
283285
if len(conditions) == 0 {
284286
uuids := r.Rows()
285287
for _, uuid := range uuids {
@@ -308,7 +310,7 @@ func (r *RowCache) RowsByCondition(conditions []ovsdb.Condition) ([]model.Model,
308310
}
309311
} else if index, err := r.Index(condition.Column); err != nil {
310312
for k, v := range index {
311-
tSchema := r.schema.Columns[condition.Column]
313+
tSchema := schema.Columns[condition.Column]
312314
nativeValue, err := ovsdb.OvsToNative(tSchema, condition.Value)
313315
if err != nil {
314316
return nil, err
@@ -325,7 +327,7 @@ func (r *RowCache) RowsByCondition(conditions []ovsdb.Condition) ([]model.Model,
325327
} else {
326328
for _, uuid := range r.Rows() {
327329
row := r.Row(uuid)
328-
info, err := mapper.NewInfo(r.name, &r.schema, row)
330+
info, err := r.dbModel.NewModelInfo(row)
329331
if err != nil {
330332
return nil, err
331333
}
@@ -422,8 +424,8 @@ func NewTableCache(dbModel *model.DatabaseModel, data Data) (*TableCache, error)
422424
eventProcessor := newEventProcessor(bufferSize)
423425
cache := make(map[string]*RowCache)
424426
tableTypes := dbModel.Types()
425-
for name, tableSchema := range dbModel.Schema().Tables {
426-
cache[name] = newRowCache(name, tableSchema, tableTypes[name])
427+
for name := range dbModel.Schema().Tables {
428+
cache[name] = newRowCache(name, dbModel, tableTypes[name])
427429
}
428430
for table, rowData := range data {
429431
if _, ok := dbModel.Schema().Tables[table]; !ok {
@@ -626,8 +628,8 @@ func (t *TableCache) Purge(dbModel *model.DatabaseModel) {
626628
defer t.mutex.Unlock()
627629
t.dbModel = dbModel
628630
tableTypes := t.dbModel.Types()
629-
for name, tableSchema := range t.dbModel.Schema().Tables {
630-
t.cache[name] = newRowCache(name, tableSchema, tableTypes[name])
631+
for name := range t.dbModel.Schema().Tables {
632+
t.cache[name] = newRowCache(name, t.dbModel, tableTypes[name])
631633
}
632634
}
633635

@@ -643,11 +645,11 @@ func (t *TableCache) Run(stopCh <-chan struct{}) {
643645

644646
// newRowCache creates a new row cache with the provided data
645647
// if the data is nil, and empty RowCache will be created
646-
func newRowCache(name string, schema ovsdb.TableSchema, dataType reflect.Type) *RowCache {
648+
func newRowCache(name string, dbModel *model.DatabaseModel, dataType reflect.Type) *RowCache {
647649
r := &RowCache{
648650
name: name,
649-
schema: schema,
650-
indexes: newColumnToValue(schema.Indexes),
651+
dbModel: dbModel,
652+
indexes: newColumnToValue(dbModel.Schema().Table(name).Indexes),
651653
dataType: dataType,
652654
cache: make(map[string]model.Model),
653655
mutex: sync.RWMutex{},
@@ -761,7 +763,7 @@ func (t *TableCache) CreateModel(tableName string, row *ovsdb.Row, uuid string)
761763
if err != nil {
762764
return nil, err
763765
}
764-
info, err := mapper.NewInfo(tableName, table, model)
766+
info, err := t.dbModel.NewModelInfo(model)
765767
if err != nil {
766768
return nil, err
767769
}
@@ -790,7 +792,7 @@ func (t *TableCache) ApplyModifications(tableName string, base model.Model, upda
790792
if schema == nil {
791793
return fmt.Errorf("no schema for table %s", tableName)
792794
}
793-
info, err := mapper.NewInfo(tableName, schema, base)
795+
info, err := t.dbModel.NewModelInfo(base)
794796
if err != nil {
795797
return err
796798
}

0 commit comments

Comments
 (0)