ethdb/pebble: handle errors (#30367)

This commit is contained in:
maskpp 2024-09-19 15:41:10 +08:00 committed by GitHub
parent c4c2c4fb14
commit 7513966d6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -293,7 +293,9 @@ func (d *Database) Has(key []byte) (bool, error) {
} else if err != nil { } else if err != nil {
return false, err return false, err
} }
closer.Close() if err = closer.Close(); err != nil {
return false, err
}
return true, nil return true, nil
} }
@ -310,7 +312,9 @@ func (d *Database) Get(key []byte) ([]byte, error) {
} }
ret := make([]byte, len(dat)) ret := make([]byte, len(dat))
copy(ret, dat) copy(ret, dat)
closer.Close() if err = closer.Close(); err != nil {
return nil, err
}
return ret, nil return ret, nil
} }
@ -519,14 +523,18 @@ type batch struct {
// Put inserts the given value into the batch for later committing. // Put inserts the given value into the batch for later committing.
func (b *batch) Put(key, value []byte) error { func (b *batch) Put(key, value []byte) error {
b.b.Set(key, value, nil) if err := b.b.Set(key, value, nil); err != nil {
return err
}
b.size += len(key) + len(value) b.size += len(key) + len(value)
return nil return nil
} }
// Delete inserts the key removal into the batch for later committing. // Delete inserts the key removal into the batch for later committing.
func (b *batch) Delete(key []byte) error { func (b *batch) Delete(key []byte) error {
b.b.Delete(key, nil) if err := b.b.Delete(key, nil); err != nil {
return err
}
b.size += len(key) b.size += len(key)
return nil return nil
} }
@ -558,19 +566,22 @@ func (b *batch) Replay(w ethdb.KeyValueWriter) error {
for { for {
kind, k, v, ok, err := reader.Next() kind, k, v, ok, err := reader.Next()
if !ok || err != nil { if !ok || err != nil {
break return err
} }
// The (k,v) slices might be overwritten if the batch is reset/reused, // The (k,v) slices might be overwritten if the batch is reset/reused,
// and the receiver should copy them if they are to be retained long-term. // and the receiver should copy them if they are to be retained long-term.
if kind == pebble.InternalKeyKindSet { if kind == pebble.InternalKeyKindSet {
w.Put(k, v) if err = w.Put(k, v); err != nil {
return err
}
} else if kind == pebble.InternalKeyKindDelete { } else if kind == pebble.InternalKeyKindDelete {
w.Delete(k) if err = w.Delete(k); err != nil {
return err
}
} else { } else {
return fmt.Errorf("unhandled operation, keytype: %v", kind) return fmt.Errorf("unhandled operation, keytype: %v", kind)
} }
} }
return nil
} }
// pebbleIterator is a wrapper of underlying iterator in storage engine. // pebbleIterator is a wrapper of underlying iterator in storage engine.