Fold get all the data and perform the operation specified by the user. The function returns false to exit
(f func(key []byte, value []byte) bool)
| 291 | // Fold get all the data and perform the operation specified by the user. |
| 292 | // The function returns false to exit |
| 293 | func (db *DB) Fold(f func(key []byte, value []byte) bool) error { |
| 294 | // Acquire a read lock to ensure data consistency |
| 295 | db.lock.RLock() |
| 296 | defer db.lock.RUnlock() |
| 297 | |
| 298 | // Retrieve an iterator for the index |
| 299 | iterator := db.index.Iterator(false) |
| 300 | |
| 301 | // Iterate over the index |
| 302 | for iterator.Rewind(); iterator.Valid(); iterator.Next() { |
| 303 | // Retrieve the value associated with the current key |
| 304 | value, err := db.getValueByPosition(iterator.Value()) |
| 305 | if err != nil { |
| 306 | return err |
| 307 | } |
| 308 | |
| 309 | // Invoke the provided function with the key and value |
| 310 | // If the function returns false, stop folding and exit the loop |
| 311 | if !f(iterator.Key(), value) { |
| 312 | break |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // Return nil to indicate successful folding |
| 317 | return nil |
| 318 | } |
| 319 | |
| 320 | // getValueByPosition Get the corresponding value based on the location index information |
| 321 | func (db *DB) getValueByPosition(logRecordPst *data2.LogRecordPst) ([]byte, error) { |