lib/db: Make err(Closed|NotFound) values (#8215)

This commit is contained in:
greatroar 2022-03-13 20:53:34 +01:00 committed by GitHub
parent 6862dd04ab
commit d0fd6c6c82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 22 deletions

View File

@ -133,23 +133,13 @@ func OpenMemory() Backend {
return OpenLevelDBMemory()
}
type errClosed struct{}
var (
errClosed = errors.New("database is closed")
errNotFound = errors.New("key not found")
)
func (*errClosed) Error() string { return "database is closed" }
type errNotFound struct{}
func (*errNotFound) Error() string { return "key not found" }
func IsClosed(err error) bool {
e := &errClosed{}
return errors.As(err, &e)
}
func IsNotFound(err error) bool {
e := &errNotFound{}
return errors.As(err, &e)
}
func IsClosed(err error) bool { return errors.Is(err, errClosed) }
func IsNotFound(err error) bool { return errors.Is(err, errNotFound) }
// releaser manages counting on top of a waitgroup
type releaser struct {
@ -183,7 +173,7 @@ func (cg *closeWaitGroup) Add(i int) error {
cg.closeMut.RLock()
defer cg.closeMut.RUnlock()
if cg.closed {
return &errClosed{}
return errClosed
}
cg.WaitGroup.Add(i)
return nil

View File

@ -223,11 +223,11 @@ func (it *leveldbIterator) Error() error {
// wrapLeveldbErr wraps errors so that the backend package can recognize them
func wrapLeveldbErr(err error) error {
if err == leveldb.ErrClosed {
return &errClosed{}
}
if err == leveldb.ErrNotFound {
return &errNotFound{}
switch err {
case leveldb.ErrClosed:
return errClosed
case leveldb.ErrNotFound:
return errNotFound
}
return err
}