Skip to content

Commit

Permalink
model: Use DatabaseModel to create mapper.Info
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
amorenoz committed Oct 13, 2021
1 parent 3ff43c5 commit 874ab7a
Show file tree
Hide file tree
Showing 15 changed files with 296 additions and 138 deletions.
44 changes: 23 additions & 21 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func newIndex(columns ...string) index {
// RowCache is a collections of Models hashed by UUID
type RowCache struct {
name string
schema ovsdb.TableSchema
dbModel *model.DatabaseModel
dataType reflect.Type
cache map[string]model.Model
indexes columnToValue
Expand All @@ -90,7 +90,7 @@ func (r *RowCache) RowByModel(m model.Model) model.Model {
if reflect.TypeOf(m) != r.dataType {
return nil
}
info, _ := mapper.NewInfo(r.name, &r.schema, m)
info, _ := r.dbModel.NewModelInfo(m)
uuid, err := info.FieldByColumn("_uuid")
if err != nil {
return nil
Expand Down Expand Up @@ -120,11 +120,11 @@ func (r *RowCache) Create(uuid string, m model.Model, checkIndexes bool) error {
if reflect.TypeOf(m) != r.dataType {
return fmt.Errorf("expected data of type %s, but got %s", r.dataType.String(), reflect.TypeOf(m).String())
}
info, err := mapper.NewInfo(r.name, &r.schema, m)
info, err := r.dbModel.NewModelInfo(m)
if err != nil {
return err
}
newIndexes := newColumnToValue(r.schema.Indexes)
newIndexes := newColumnToValue(r.dbModel.Schema().Table(r.name).Indexes)
for index := range r.indexes {
val, err := valueFromIndex(info, index)
if err != nil {
Expand Down Expand Up @@ -156,16 +156,17 @@ func (r *RowCache) Update(uuid string, m model.Model, checkIndexes bool) error {
return fmt.Errorf("row %s does not exist", uuid)
}
oldRow := model.Clone(r.cache[uuid])
oldInfo, err := mapper.NewInfo(r.name, &r.schema, oldRow)
oldInfo, err := r.dbModel.NewModelInfo(oldRow)
if err != nil {
return err
}
newInfo, err := mapper.NewInfo(r.name, &r.schema, m)
newInfo, err := r.dbModel.NewModelInfo(m)
if err != nil {
return err
}
newIndexes := newColumnToValue(r.schema.Indexes)
oldIndexes := newColumnToValue(r.schema.Indexes)
indexes := r.dbModel.Schema().Table(r.name).Indexes
newIndexes := newColumnToValue(indexes)
oldIndexes := newColumnToValue(indexes)
var errs []error
for index := range r.indexes {
var err error
Expand Down Expand Up @@ -218,7 +219,7 @@ func (r *RowCache) Update(uuid string, m model.Model, checkIndexes bool) error {
}

func (r *RowCache) IndexExists(row model.Model) error {
info, err := mapper.NewInfo(r.name, &r.schema, row)
info, err := r.dbModel.NewModelInfo(row)
if err != nil {
return err
}
Expand Down Expand Up @@ -252,7 +253,7 @@ func (r *RowCache) Delete(uuid string) error {
return fmt.Errorf("row %s does not exist", uuid)
}
oldRow := r.cache[uuid]
oldInfo, err := mapper.NewInfo(r.name, &r.schema, oldRow)
oldInfo, err := r.dbModel.NewModelInfo(oldRow)
if err != nil {
return err
}
Expand Down Expand Up @@ -280,6 +281,7 @@ func (r *RowCache) Rows() []string {

func (r *RowCache) RowsByCondition(conditions []ovsdb.Condition) ([]model.Model, error) {
var results []model.Model
schema := r.dbModel.Schema().Table(r.name)
if len(conditions) == 0 {
uuids := r.Rows()
for _, uuid := range uuids {
Expand Down Expand Up @@ -308,7 +310,7 @@ func (r *RowCache) RowsByCondition(conditions []ovsdb.Condition) ([]model.Model,
}
} else if index, err := r.Index(condition.Column); err != nil {
for k, v := range index {
tSchema := r.schema.Columns[condition.Column]
tSchema := schema.Columns[condition.Column]
nativeValue, err := ovsdb.OvsToNative(tSchema, condition.Value)
if err != nil {
return nil, err
Expand All @@ -325,7 +327,7 @@ func (r *RowCache) RowsByCondition(conditions []ovsdb.Condition) ([]model.Model,
} else {
for _, uuid := range r.Rows() {
row := r.Row(uuid)
info, err := mapper.NewInfo(r.name, &r.schema, row)
info, err := r.dbModel.NewModelInfo(row)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -422,8 +424,8 @@ func NewTableCache(dbModel *model.DatabaseModel, data Data) (*TableCache, error)
eventProcessor := newEventProcessor(bufferSize)
cache := make(map[string]*RowCache)
tableTypes := dbModel.Types()
for name, tableSchema := range dbModel.Schema().Tables {
cache[name] = newRowCache(name, tableSchema, tableTypes[name])
for name := range dbModel.Schema().Tables {
cache[name] = newRowCache(name, dbModel, tableTypes[name])
}
for table, rowData := range data {
if _, ok := dbModel.Schema().Tables[table]; !ok {
Expand Down Expand Up @@ -626,8 +628,8 @@ func (t *TableCache) Purge(dbModel *model.DatabaseModel) {
defer t.mutex.Unlock()
t.dbModel = dbModel
tableTypes := t.dbModel.Types()
for name, tableSchema := range t.dbModel.Schema().Tables {
t.cache[name] = newRowCache(name, tableSchema, tableTypes[name])
for name := range t.dbModel.Schema().Tables {
t.cache[name] = newRowCache(name, t.dbModel, tableTypes[name])
}
}

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

// newRowCache creates a new row cache with the provided data
// if the data is nil, and empty RowCache will be created
func newRowCache(name string, schema ovsdb.TableSchema, dataType reflect.Type) *RowCache {
func newRowCache(name string, dbModel *model.DatabaseModel, dataType reflect.Type) *RowCache {
r := &RowCache{
name: name,
schema: schema,
indexes: newColumnToValue(schema.Indexes),
dbModel: dbModel,
indexes: newColumnToValue(dbModel.Schema().Table(name).Indexes),
dataType: dataType,
cache: make(map[string]model.Model),
mutex: sync.RWMutex{},
Expand Down Expand Up @@ -761,7 +763,7 @@ func (t *TableCache) CreateModel(tableName string, row *ovsdb.Row, uuid string)
if err != nil {
return nil, err
}
info, err := mapper.NewInfo(tableName, table, model)
info, err := t.dbModel.NewModelInfo(model)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -790,7 +792,7 @@ func (t *TableCache) ApplyModifications(tableName string, base model.Model, upda
if schema == nil {
return fmt.Errorf("no schema for table %s", tableName)
}
info, err := mapper.NewInfo(tableName, schema, base)
info, err := t.dbModel.NewModelInfo(base)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 874ab7a

Please sign in to comment.