diff --git a/dex/lexi/lexi.go b/dex/lexi/lexi.go index 99a56d7cc3..63909d9ec9 100644 --- a/dex/lexi/lexi.go +++ b/dex/lexi/lexi.go @@ -26,7 +26,7 @@ func convertError(err error) error { case errors.Is(err, badger.ErrKeyNotFound): return ErrKeyNotFound } - return nil + return err } // DB is the Lexi DB. The Lexi DB wraps a badger key-value database and provides @@ -177,13 +177,13 @@ func (db *DB) nextID() (dbID DBID, _ error) { // method is provided as a tool to keep database index entries short. func (db *DB) KeyID(kB []byte) (dbID DBID, err error) { err = db.View(func(txn *badger.Txn) error { - dbID, err = db.keyID(txn, kB) + dbID, err = db.keyID(txn, kB, true) return err }) return } -func (db *DB) keyID(txn *badger.Txn, kB []byte) (dbID DBID, err error) { +func (db *DB) keyID(txn *badger.Txn, kB []byte, readOnly bool) (dbID DBID, err error) { item, err := txn.Get(prefixedKey(keyToIDPrefix, kB)) if err == nil { err = item.Value(func(v []byte) error { @@ -192,7 +192,7 @@ func (db *DB) keyID(txn *badger.Txn, kB []byte) (dbID DBID, err error) { }) return } - if errors.Is(err, ErrKeyNotFound) { + if !readOnly && errors.Is(err, ErrKeyNotFound) { if dbID, err = db.nextID(); err != nil { return } diff --git a/dex/lexi/table.go b/dex/lexi/table.go index 7f9d47df40..1a11c216ed 100644 --- a/dex/lexi/table.go +++ b/dex/lexi/table.go @@ -42,7 +42,7 @@ func (t *Table) Get(k encoding.BinaryMarshaler, v encoding.BinaryUnmarshaler) er return fmt.Errorf("error marshaling key: %w", err) } return t.View(func(txn *badger.Txn) error { - dbID, err := t.keyID(txn, kB) + dbID, err := t.keyID(txn, kB, true) if err != nil { return convertError(err) } @@ -122,7 +122,7 @@ func (t *Table) Set(k, v encoding.BinaryMarshaler, setOpts ...SetOption) error { } d := &datum{v: vB, indexes: make([][]byte, len(t.indexes))} return t.Update(func(txn *badger.Txn) error { - dbID, err := t.keyID(txn, kB) + dbID, err := t.keyID(txn, kB, false) if err != nil { return convertError(err) } @@ -160,7 +160,7 @@ func (t *Table) Set(k, v encoding.BinaryMarshaler, setOpts ...SetOption) error { // and the id<->key mappings. func (t *Table) Delete(kB []byte) error { return t.Update(func(txn *badger.Txn) error { - dbID, err := t.keyID(txn, kB) + dbID, err := t.keyID(txn, kB, true) if err != nil { return convertError(err) }