Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lexi: Avoid generating key when reading #3159

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading