-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor trickle DAG builder #4730
Conversation
License: MIT Signed-off-by: Lucas Molas <schomatis@gmail.com>
@whyrusleeping Could you review this please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks right to me but I'm not familiar with this code.
for j := 0; j < layerRepeat && !db.Done(); j++ { | ||
next := db.NewUnixfsNode() | ||
if err := fillTrickleRec(db, next, i); err != nil { | ||
for depth := 1; ; depth++ { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code nit: You can write this as for depth := 1; depth < maxDepth || maxDepth < 0; depth++ {
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put it separately to highlight the fact that maxDepth
is an optional parameter (that will most commonly be unset, which is the base case) but I can move it all together to make it more compact if you think it's more in line with Go coding principles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds reasonable. I don't feel strongly either way.
for layer := 0; layer < layerRepeat; layer++ { | ||
if db.Done() { | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why move do this separately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid repeating the same condition in both for
s, and make it more clear that if db.Done
is true nothing more needs to be done in this function and it should just exit there (removing the final return
from the code).
@Stebalien Thanks for the review! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SGTM, hash stability in case of big files was unaffected. I wasn't sure if it was covered by sharness so I checked it manually.
Thanks @schomatis, and congrats on your first PR landing :) |
Refactoring of the logic of the trickle DAG builder to simplify the future patch that will add support for Filestore (#4052).
The
Layout
function is mirroring the logic infillTrickleRec
duplicating almost entirely its code. The refactoredfillTrickleRec
now absorbs both scenarios of building a trickle tree with or without a maximum depth (Layout
didn't have a maximum depth, onlyfillTrickleRec
did). This is accomplished abusing thedepth
parameter (now renamed asmaxDepth
) to make it optional (a negative value is interpreted as no maximum). This hack is due to the fact that Go doesn't support optional parameters, suggestions are welcome for a more elegant solution.I'm submitting this WIP PR to have feedback on this refactoring, as it will be useful to extend it to other functions (
Append
,appendFillLastChild
,appendRec
) which also replicate similar loop patterns of the sort:https://github.com/ipfs/go-ipfs/blob/cc01b7f188622e7148ce041b9d09252c85041d9f/importer/trickle/trickledag.go#L212-L224