-
Notifications
You must be signed in to change notification settings - Fork 89
Extract db-related methods from baseTrie #74
Conversation
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Whew, at least tests are already passing, great! 👍 😄 |
Think I like this approach, this get's definitely more readable and distinctly sorted out regarding the functionality respectively responsibility. I wonder if we should generally switch to a more directory-structured file structure (and also take on some breaking import changes as a consequence), suggestion:
Update: Updated the above with latest reflections/ideas. |
Find this file and naming mix generally still too messy... |
Updated the directory structure above with latest reflections/ideas on this. Side node: we can also address this in a separate PR. |
Will do some more proper review now. 😄 Let's probably address these file structure changes in a separate PR, will eventually also open a new issue on this. You can nevertheless give some first opinion if you want. |
Tested the basic usage example, worked. |
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.
Looks more than it is (my review comments), generally this looks really good and very happy with the changes. This brings a lot more transparency in the behavior of the library, also eventually allows in the future to do easier switches of the DB backend store (respectively allows to easier add this functionality to the API + code base).
src/baseTrie.js
Outdated
this.db = db | ||
} else { | ||
this.db = new DB(db) | ||
} |
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 am a bit undecided here, since this adds complexity to the public API. Do we (already) want to expose the DB
class through this and allow the additional instantiation with DB
? Would you say this should now be the recommended way of using the API? Or should we stick for now to just keep the API as is and just forward the level
instance passed?
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.
You're right, we should probably keep DB
internal and have the public interface work with level
instance.
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 have actually a tendency to the other way around, to enforce instantiation by using the DB
wrapper class. Then we already have some good preparation if we want to add new storage options beside from level
DB in the future. What do you think? I am in the breaking-changes mood! 😄
Thanks for the extensive review! Will go through your comments now. Re. the file structure, agree and what you proposed looks good for the current state. Only maybe we can wait a bit more before solidifying the structure, as there's another PR incoming, and then there's transition to typescript which could offer new opportunities for structuring. |
Makes very much sense to let the file structure changes sink in a bit more and do this along |
Ok, have answered everything. Let me know if you have questions or would rather tend to choose other paths than the ones I have suggested. |
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Tried to apply your suggestions. Hope I didn't miss anything. |
Hmm, can't we use the tests on the There is still a linting error preventing the CI run to pass. |
} else { | ||
this.db = new DB(db) | ||
} | ||
this.db = db || new DB() |
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.
Can you update the API docs here as well?
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Ready for re-review? 😄 |
Yep 😄 I was just waiting to see if CI passes. I've replaced the |
Cool, will directly go for it. 😄 (already a +0.4% test coverage increase, great! 👍) |
Update: ah, strange coverage behavior now the final report is -1.5%, I had a +0.4% shown while CI was still running. Hmm. |
Just as some note, no need to change in this round: maybe we should name |
Also just as some note and not a PR blocker: these re-generated docs are better than nothing, but after all these changes we have to significantly restructure the docs to reflect the directory/file structure + API exposed. |
Maybe if we just release after |
Also some note: we had some extensive discussion with Alex when he was doing the changes around changing or not changing the API by the decision where to put the distribution files (so that However after some time dealing with this I have to say this is a total mess. On |
Signed-off-by: Sina Mahmoodi <itz.s1na@gmail.com>
Weird that coverage changed at the end! but luckily it helped us find a bug! A few points:
|
|
Regarding your comments:
|
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.
Ok, had some last overall look, would give this a cautious GO now. Feel free to merge if you feel confident with it.
On 3.: Think you have some better oversight then me on ways to organize this, feel free to realize the one you think is most appropriate. Just this throwing everything into root dir makes no sense. |
This PR extracts db-related methods such as
getRaw
,putRaw
fromBaseTrie
and moves them to a new classDB
. It also modifies how checkpointing is done inCheckpointTrie
.The goal was to do these two modifications according to this comment, but after implementation I figured out some problem with that design. I experimented with a few other designs, but they all seemed hacky like the current checkpointing method. This PR is one of those methods which I liked a bit more than others. But if you think it doesn't improve on the status quo, we can close this PR.
To achieve checkpointing we somehow have to make the original trie read-only, and perform all subsequent writes to a temporary (scratch) db. On
revert
the scratch db is dropped and trie root is set to the checkpointed root. Oncommit
nodes in the scratch db which correspond to the current trie (with current root) have to be written to the main persistent db.For this purpose, this PR introduces a
ScratchDB
, which is an in-memoryDB
with a backend (pointer to main persistent db). On checkpoint,CheckpointTrie
replaces the db thatBaseTrie
uses with an instance ofScratchDB
, therefore all writes are written to the scratch. If a key is not found in theScratchDB
, it tries to fetch it from itsbackend
.I also have some questions regarding
getRaw
andputRaw
. My impression from reading the existing code is thatputRaw
will write to the persistent DB even if trie is during a checkpoint. Is that intentional? If so, shouldgetRaw
anddelRaw
also be done on persistent db regardless of checkpointing? We should probably highlight this in documentation, or rename them to make this side-effect clear.