ReadMap returns an iterator to read the map along with the map size. The size can be used to pre-allocate a map with the correct capacity for better performance. The first value from the iterator is the key. Please note that this byte slice is only valid during the iteration. This is done to avoid a
()
| 219 | // for later use. The second value is an error indicating that the database is |
| 220 | // malformed or that the pointed value is not a map. |
| 221 | func (d *Decoder) ReadMap() (iter.Seq2[[]byte, error], uint, error) { |
| 222 | size, offset, err := d.decodeCtrlDataAndFollow(KindMap) |
| 223 | if err != nil { |
| 224 | return nil, 0, d.wrapError(err) |
| 225 | } |
| 226 | |
| 227 | iterator := func(yield func([]byte, error) bool) { |
| 228 | currentOffset := offset |
| 229 | |
| 230 | for i := range size { |
| 231 | key, keyEndOffset, err := d.d.decodeKey(currentOffset) |
| 232 | if err != nil { |
| 233 | yield(nil, d.wrapErrorAtOffset(err, currentOffset)) |
| 234 | return |
| 235 | } |
| 236 | |
| 237 | // Position decoder to read value after yielding key |
| 238 | d.reset(keyEndOffset) |
| 239 | |
| 240 | ok := yield(key, nil) |
| 241 | if !ok { |
| 242 | d.finishMapAfterStop(keyEndOffset, size-i-1) |
| 243 | return |
| 244 | } |
| 245 | |
| 246 | // Skip the value to get to next key-value pair |
| 247 | valueEndOffset, err := d.d.nextValueOffset(keyEndOffset, 1) |
| 248 | if err != nil { |
| 249 | yield(nil, d.wrapError(err)) |
| 250 | return |
| 251 | } |
| 252 | currentOffset = valueEndOffset |
| 253 | } |
| 254 | |
| 255 | // Set the final offset after map iteration |
| 256 | d.reset(currentOffset) |
| 257 | } |
| 258 | |
| 259 | return iterator, size, nil |
| 260 | } |
| 261 | |
| 262 | // ReadSlice returns an iterator over the values of the slice along with the |
| 263 | // slice size. The size can be used to pre-allocate a slice with the correct |