@@ -121,28 +121,39 @@ func Traverse(prefix []byte, iterFunc IterationFunc, opt storage.IteratorOption)
121121 return Iterate (prefix , prefix , iterFunc , opt )
122122}
123123
124- // Exists returns true if a key exists in the database.
124+ // Exists takes a key and a pointer to an a boolean variable `keyExists` as inputs and returns an function.
125+ // When this returned function is executed (and only then), it will write into the `keyExists` whether
126+ // the key exists.
125127// No errors are expected during normal operation.
126128func Exists (key []byte , keyExists * bool ) func (storage.Reader ) error {
127129 return func (r storage.Reader ) error {
128- _ , closer , err := r . Get ( key )
130+ exists , err := KeyExists ( r , key )
129131 if err != nil {
130- // the key does not exist in the database
131- if errors .Is (err , storage .ErrNotFound ) {
132- * keyExists = false
133- return nil
134- }
135- // exception while checking for the key
136- return irrecoverable .NewExceptionf ("could not load data: %w" , err )
132+ return err
137133 }
138- defer closer .Close ()
139-
140- // the key does exist in the database
141- * keyExists = true
134+ * keyExists = exists
142135 return nil
143136 }
144137}
145138
139+ // KeyExists returns true if a key exists in the database.
140+ // No errors are expected during normal operation.
141+ func KeyExists (r storage.Reader , key []byte ) (bool , error ) {
142+ _ , closer , err := r .Get (key )
143+ if err != nil {
144+ // the key does not exist in the database
145+ if errors .Is (err , storage .ErrNotFound ) {
146+ return false , nil
147+ }
148+ // exception while checking for the key
149+ return false , irrecoverable .NewExceptionf ("could not load data: %w" , err )
150+ }
151+ defer closer .Close ()
152+
153+ // the key does exist in the database
154+ return true , nil
155+ }
156+
146157// Retrieve will retrieve the binary data under the given key from the database
147158// and decode it into the given entity. The provided entity needs to be a
148159// pointer to an initialized entity of the correct type.
0 commit comments