Fix broken persistence and improve reliability in dream_ets #32
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes critical bugs in dream_ets that made persistence operations completely non-functional, improves error handling to prevent panics, and adds comprehensive documentation with verified examples.
Why These Changes Were Made
While working on documentation, we discovered that example code snippets couldn't be verified because they weren't being compiled. This led to discovering several critical issues:
save_to_file()andload_from_file()had never been tested and didn't work due to FFI bugsError(_)patterns and panicking on decode failuresError(_)patterns users would copy into productionThe root cause: untested code is broken code. The persistence operations had zero unit tests and were fundamentally broken.
What Was Changed
Critical Bug Fixes
FFI Bugs Fixed:
save_to_file()/load_from_file(): Fixed binary/charlist conversion - Gleam Strings are binaries but Erlang file operations need charlistsupdate_element(): Fixed return type fromokatom to{ok, nil}tupleets_first(): Fixed return value fromemptytoempty_tableto match type systemError Handling Fixed:
Error(_)patterns - now explicitly handle all error variantskeys(),values(),to_list()helper functionsBreaking API Changes (v1.0.2 → v2.0.0)
Four functions now return
Resultinstead of bare values to properly handle decode errors:operations.keys():List(k)→Result(List(k), EtsError)operations.values():List(v)→Result(List(v), EtsError)operations.to_list():List(#(k,v))→Result(List(#(k,v)), EtsError)operations.size():Int→Result(Int, EtsError)Why this is necessary: These functions iterate the table and decode keys/values. If decode fails (corrupt data, encoder mismatch, migration issues), the old code would panic and crash the user's process. Now users can handle decode errors gracefully.
Migration is simple:
Documentation & Testing
Test Coverage:
src/dream_ets/Documentation:
Impact Assessment
Who's Affected:
keys(),values(),to_list(), orsize()needs to handle ResultWho Benefits:
Testing
✅ dream_ets: 77 tests, 0 failures
✅ Dream core: 239 tests, 0 failures
✅ rate_limiter example: 8 integration tests, 0 failures
✅ streaming example: 6 integration tests, 0 failures
Total: 330 tests across entire codebase, all passing
Files Changed
Review Notes
Focus areas for review:
This is a significant quality improvement that fixes production-breaking bugs while maintaining backward compatibility for the most common use cases.