From 341fd734b1ff8486e6c7ceecdc9a50cd5500bb92 Mon Sep 17 00:00:00 2001 From: Sachin Holla <51310506+sachinholla@users.noreply.github.com> Date: Wed, 5 Jul 2023 20:16:31 +0530 Subject: [PATCH] Remove invalid db type definitions: ERROR_DB, USER_DB (#94) * Removed ERROR_DB & USER_DB db types and all their references * Return error from NewDB() if unsupported DbNum is passed * Handle gaps in DBNum values while creating 'all db objects' array * Ignore nil db objects in DeleteDB() --- translib/db/db.go | 17 +++++++---------- translib/db/db_test.go | 5 ----- translib/transformer/transformer_test.go | 2 +- translib/transformer/xlate_utils.go | 4 ++-- translib/translib.go | 3 +++ 5 files changed, 13 insertions(+), 18 deletions(-) diff --git a/translib/db/db.go b/translib/db/db.go index 00c5e0aee262..431a2ff01096 100644 --- a/translib/db/db.go +++ b/translib/db/db.go @@ -140,8 +140,6 @@ const ( FlexCounterDB // 5 StateDB // 6 SnmpDB // 7 - ErrorDB // 8 - UserDB // 9 // All DBs added above this line, please ---- MaxDB // The Number of DBs ) @@ -322,10 +320,6 @@ func getDBInstName (dbNo DBNum) string { return "STATE_DB" case SnmpDB: return "SNMP_OVERLAY_DB" - case ErrorDB: - return "ERROR_DB" - case UserDB: - return "USER_DB" } return "" } @@ -345,8 +339,6 @@ func GetdbNameToIndex(dbName string) DBNum { dbIndex = FlexCounterDB case "STATE_DB": dbIndex = StateDB - case "ERROR_DB": - dbIndex = ErrorDB } return dbIndex } @@ -382,7 +374,9 @@ func NewDB(opt Options) (*DB, error) { glog.Warning("Database instance not present for the Db name: ", dbInstName) } } else { - glog.Error(fmt.Errorf("Invalid database number %d", dbId)) + glog.Errorf("NewDB: invalid database number: %d", dbId) + e = tlerr.TranslibDBCannotOpen{} + goto NewDBExit } if opt.IsOnChangeEnabled && !opt.IsWriteDisabled { @@ -428,7 +422,7 @@ func NewDB(opt Options) (*DB, error) { if len(d.Opts.InitIndicator) == 0 { - glog.Info("NewDB: Init indication not requested") + glog.V(5).Info("NewDB: Init indication not requested") } else if init, _ := d.client.Get(d.Opts.InitIndicator).Int(); init != 1 { @@ -450,6 +444,9 @@ NewDBExit: // DeleteDB is the gentle way to close the DB connection. func (d *DB) DeleteDB() error { + if d == nil { + return nil + } if glog.V(3) { glog.Info("DeleteDB: Begin: d: ", d) diff --git a/translib/db/db_test.go b/translib/db/db_test.go index 6a1bf68e725f..2c65e67f1fd2 100644 --- a/translib/db/db_test.go +++ b/translib/db/db_test.go @@ -101,11 +101,6 @@ var dbConfig = ` "id" : 7, "separator": "|", "instance" : "redis" - }, - "ERROR_DB" : { - "id" : 8, - "separator": ":", - "instance" : "redis" } }, "VERSION" : "1.0" diff --git a/translib/transformer/transformer_test.go b/translib/transformer/transformer_test.go index 28d893b0dbac..26942ccdfcc9 100644 --- a/translib/transformer/transformer_test.go +++ b/translib/transformer/transformer_test.go @@ -48,7 +48,7 @@ func getDBOptions(dbNo db.DBNum, isWriteDisabled bool) db.Options { case db.ApplDB, db.CountersDB, db.AsicDB, db.FlexCounterDB: opt = getDBOptionsWithSeparator(dbNo, "", ":", ":", isWriteDisabled) break - case db.ConfigDB, db.StateDB, db.ErrorDB: + case db.ConfigDB, db.StateDB: opt = getDBOptionsWithSeparator(dbNo, "", "|", "|", isWriteDisabled) break } diff --git a/translib/transformer/xlate_utils.go b/translib/transformer/xlate_utils.go index 5251b332eed1..a120cde74c0d 100644 --- a/translib/transformer/xlate_utils.go +++ b/translib/transformer/xlate_utils.go @@ -562,9 +562,9 @@ func getDBOptions(dbNo db.DBNum) db.Options { var opt db.Options switch dbNo { - case db.ApplDB, db.CountersDB: + case db.ApplDB, db.CountersDB, db.FlexCounterDB, db.AsicDB: opt = getDBOptionsWithSeparator(dbNo, "", ":", ":") - case db.FlexCounterDB, db.AsicDB, db.ConfigDB, db.StateDB, db.ErrorDB, db.UserDB: + case db.ConfigDB, db.StateDB: opt = getDBOptionsWithSeparator(dbNo, "", "|", "|") } diff --git a/translib/translib.go b/translib/translib.go index 91281acbef7b..191cf427afee 100644 --- a/translib/translib.go +++ b/translib/translib.go @@ -796,6 +796,9 @@ func getAllDbs(opts ...func(*db.Options)) ([db.MaxDB]*db.DB, error) { var dbs [db.MaxDB]*db.DB var err error for dbNum := db.DBNum(0); dbNum < db.MaxDB; dbNum++ { + if len(dbNum.Name()) == 0 { + continue + } dbs[dbNum], err = db.NewDB(getDBOptions(dbNum, opts...)) if err != nil { closeAllDbs(dbs[:])