Repository navigation
Conversation
The diff option returned a stub { blocks: 0 }. Count blocks that had data in the cleared range: from the bitfield on the default session, and from storage reads on dependency-backed sessions.
Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
Uncomments the diff-option test, asserting the first clear counts the stored block and a repeat clear counts zero. Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
The diff option now returns a `cleared` object counting how many blocks were actually cleared, e.g. { blocks: 1 }.
Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com>
[Medium impact] The change affects callers that ask `core.clear()` for a block count. Fix atomic-session counts before merging; the large-range and test concerns are non-blocking.
|
| if (this.isDefault()) { | ||
| for (let i = start; i < clearEnd; i++) { | ||
| if (this.core.bitfield.get(i)) cleared.blocks++ |
There was a problem hiding this comment.
isDefault() also includes atomic sessions, but core.bitfield does not include their staged appends. After creating core.session({ atom }) and appending a block without flushing the atom, clear(0, { diff: true }) clears that block but still reports { blocks: 0 }. Read the atomic session’s own stored blocks instead of the parent’s bitfield.
Knowledge Base Used: Sessions, snapshots, and views
Prompt To Fix With AI
This is a comment left during a code review.
Path: lib/session-state.js
Line: 624-626
Comment:
**Atomic clears report zero**
`isDefault()` also includes atomic sessions, but `core.bitfield` does not include their staged appends. After creating `core.session({ atom })` and appending a block without flushing the atom, `clear(0, { diff: true })` clears that block but still reports `{ blocks: 0 }`. Read the atomic session’s own stored blocks instead of the parent’s bitfield.
**Knowledge Base Used:** [Sessions, snapshots, and views](https://app.greptile.com/-/switch-tenant?tenant=tetherio&next=%2Fcustom-context%2Fknowledge-base%2Fholepunchto%2Fhypercore%2F-%2Fdocs%2Fsessions-and-snapshots.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| const hasPromises = [] | ||
| for (let i = start; i < clearEnd; i++) hasPromises.push(rx.getBlock(i)) | ||
| rx.tryFlush() | ||
|
|
||
| const had = await Promise.all(hasPromises) |
There was a problem hiding this comment.
A named-session clear with diff now reads every block and retains all the results in Promise.all() before deleting anything. Peak memory grows with all the block bytes being cleared, plus one promise per index. Use bounded batches or createBlockStream(), as count() already does, so clearing a large range does not require loading the whole range into memory.
Knowledge Base Used: Core lifecycle and mutation API
Prompt To Fix With AI
This is a comment left during a code review.
Path: lib/session-state.js
Line: 630-634
Comment:
**Clear loads all blocks**
A named-session clear with `diff` now reads every block and retains all the results in `Promise.all()` before deleting anything. Peak memory grows with all the block bytes being cleared, plus one promise per index. Use bounded batches or `createBlockStream()`, as `count()` already does, so clearing a large range does not require loading the whole range into memory.
**Knowledge Base Used:** [Core lifecycle and mutation API](https://app.greptile.com/-/switch-tenant?tenant=tetherio&next=%2Fcustom-context%2Fknowledge-base%2Fholepunchto%2Fhypercore%2F-%2Fdocs%2Fcore-lifecycle.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| for (let i = start; i < clearEnd; i++) { | ||
| if (this.core.bitfield.get(i)) cleared.blocks++ | ||
| } |
There was a problem hiding this comment.
Sparse clears scan every index
On a normal core, this synchronous loop checks every index, including every missing block in a sparse range. Large clears therefore block other JavaScript work for an avoidable per-block scan. Use core.bitfield.countSet(start, clearEnd - start) instead; it counts by page and already backs count().
| for (let i = start; i < clearEnd; i++) { | |
| if (this.core.bitfield.get(i)) cleared.blocks++ | |
| } | |
| cleared.blocks += this.core.bitfield.countSet(start, clearEnd - start) |
Knowledge Base Used: Sparse bitfields and availability state
Prompt To Fix With AI
This is a comment left during a code review.
Path: lib/session-state.js
Line: 625-627
Comment:
**Sparse clears scan every index**
On a normal core, this synchronous loop checks every index, including every missing block in a sparse range. Large clears therefore block other JavaScript work for an avoidable per-block scan. Use `core.bitfield.countSet(start, clearEnd - start)` instead; it counts by page and already backs `count()`.
```suggestion
cleared.blocks += this.core.bitfield.countSet(start, clearEnd - start)
```
**Knowledge Base Used:** [Sparse bitfields and availability state](https://app.greptile.com/-/switch-tenant?tenant=tetherio&next=%2Fcustom-context%2Fknowledge-base%2Fholepunchto%2Fhypercore%2F-%2Fdocs%2Fsparse-bitfields.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| const cleared2 = await core.clear(0, { diff: true }) | ||
| t.is(cleared2.blocks, 1, 'the stored block was cleared') | ||
|
|
||
| // const cleared2 = await core.clear(0, { diff: true }) | ||
| // t.ok(cleared2.blocks > 0) | ||
|
|
||
| // const cleared3 = await core.clear(0, { diff: true }) | ||
| // t.is(cleared3.blocks, 0) | ||
| const cleared3 = await core.clear(0, { diff: true }) | ||
| t.is(cleared3.blocks, 0, 'the block was already cleared') |
There was a problem hiding this comment.
The restored test checks only one stored block and its repeated clear. It does not check counting across holes or limiting the count at length. Add a default-session test that clears a partly stored multi-block range with an end beyond length, and an out-of-range call with diff: true. The existing clear(1337) call checks only the no-diff return.
Prompt To Fix With AI
This is a comment left during a code review.
Path: test/clear.js
Line: 184-188
Comment:
**Range counts lack tests**
The restored test checks only one stored block and its repeated clear. It does not check counting across holes or limiting the count at `length`. Add a default-session test that clears a partly stored multi-block range with an end beyond `length`, and an out-of-range call with `diff: true`. The existing `clear(1337)` call checks only the no-diff return.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
|
Closing as duplicate of #795, which already implements diff counting for clear(). My apologies - I mistook that open PR for an issue when scanning the tracker. |
Fixes #795.
Problem
core.clear(start, end, { diff: true })returned a stub{ blocks: 0 }- the counter was never implemented after the storage move, and the test covering it was commented out.Change
SessionState.clearnow counts the blocks that actually had data in the cleared range:The count is capped at the session's length so out-of-range clears report zero, and the commented-out diff test in
test/clear.jsis re-enabled: clearing a stored block reports{ blocks: 1 }, clearing it again reports{ blocks: 0 }. README updated to document the return value.Per the issue, named and atomic sessions are not covered by new tests here.
Verification:
test/clear.js12/12,test/basic.js658/658,prettier --checkandlunteclean.Co-authored-by: generaltony007 144252170+GeneralTony007@users.noreply.github.com