Skip to content

Commit

Permalink
Avoid duplicate ocbinds schema loading (sonic-net#73)
Browse files Browse the repository at this point in the history
Introduced a new function ocbinds.GetSchema(), which is equivalent of
the generated function ocbinds.Schema(), but loads the schema only
once. Translib request_binder and transformer are changed to use the
new GetSchema() function. Avoids duplicate schema loads during init.

Signed-off-by: Sachin Holla <[email protected]>
Co-authored-by: anand-kumar-subramanian <[email protected]>
  • Loading branch information
sachinholla and anand-kumar-subramanian authored May 11, 2023
1 parent 3036d3a commit 80e8035
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
34 changes: 34 additions & 0 deletions translib/ocbinds/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright 2021 Broadcom. The term Broadcom refers to Broadcom Inc. and/or //
// its subsidiaries. //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// //
////////////////////////////////////////////////////////////////////////////////

package ocbinds

import (
"github.com/openconfig/ygot/ytypes"
)

// GetSchema is equivalent of the Schema function, but avoids
// UnzipSchema call. Reuses the SchemaTree loaded during init.
func GetSchema() (*ytypes.Schema, error) {
return &ytypes.Schema{
Root: &Device{},
SchemaTree: SchemaTree,
Unmarshal: Unmarshal,
}, nil
}
2 changes: 1 addition & 1 deletion translib/request_binder.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func init() {
func initSchema() {
log.Flush()
var err error
if ygSchema, err = ocbinds.Schema(); err != nil {
if ygSchema, err = ocbinds.GetSchema(); err != nil {
panic("Error in getting the schema: " + err.Error())
}
}
Expand Down
2 changes: 1 addition & 1 deletion translib/transformer/xlate_to_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
log "github.com/golang/glog"
)

var ocbSch, _ = ocbinds.Schema()
var ocbSch, _ = ocbinds.GetSchema()

/* Fill redis-db map with field & value info */
func dataToDBMapAdd(tableName string, dbKey string, result map[string]map[string]db.Value, field string, value string) {
Expand Down

0 comments on commit 80e8035

Please sign in to comment.