Skip to content

Commit

Permalink
lexi: Avoid generating key when reading (#3159)
Browse files Browse the repository at this point in the history
When looking up a key that did not exist in the db, it would attempt to
generate a new one, and a nil error would be returned instead of
ErrKeyNotFound.
  • Loading branch information
martonp authored Jan 27, 2025
1 parent 57a93e8 commit 69accc5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions dex/lexi/lexi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions dex/lexi/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 69accc5

Please sign in to comment.