Skip to content

Conversation

@achow101
Copy link
Member

In order to override these later, the specific details of how the Read, Write, Erase, and Exists functions interact with the actual database file need to go into functions that are not templated.

The functions ReadKey, WriteKey, EraseKey, and HasKey are introduced to handle the actual interaction with the database.

This is mostly a moveonly.

Based on #19290

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 16, 2020

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@maflcko
Copy link
Member

maflcko commented Jun 17, 2020

Needs some kind of force push to prod github

@maflcko
Copy link
Member

maflcko commented Jun 17, 2020

Have you considered making a commit for each function? Would help with scrolling during review, but no big deal if it is too cumbersome to split up.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 69e56486186bbcae7c8e68d891493949ea0f2af8

In order to override these later, the specific details of how the Read,
Write, Erase, and Exists functions interact with the actual database
file need to go into functions that are not templated.
@achow101
Copy link
Member Author

Rebased

@achow101 achow101 force-pushed the refactor-bdb-read branch from 69e5648 to a389ed5 Compare June 17, 2020 14:29
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK a389ed5. No changes since last review, just non-conflicting rebase

Copy link
Member

@Sjors Sjors left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK a389ed5

};

private:
bool ReadKey(CDataStream& key, CDataStream& value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could use documentation. In particular the behavior when pdb is absent is not super intuitive, e.g. it returns false for ReadKey and true for WriteKey.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WriteKey returns true so that it works with dummy database. After #19102, this can (and probably should) be changed to false.


// Read
SafeDbt datValue;
int ret = pdb->get(activeTxn, datKey, datValue, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just early return false here if ret != 0 || datValue.get_data() == nullptr?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh. Will change if another push is needed.

SafeDbt datValue;
int ret = pdb->get(activeTxn, datKey, datValue, 0);
if (ret == 0 && datValue.get_data() != nullptr) {
value.write((char*)datValue.get_data(), datValue.get_size());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer inside a try-catch block, is that safe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The try-catch block is for the deserialization operation (the >> operator), not this write.


bool BerkeleyBatch::WriteKey(CDataStream& key, CDataStream& value, bool overwrite)
{
if (!pdb)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a non-trivial move, so might as well add brackets or make it inline.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will change if another push is needed.

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK a389ed5 🔳

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

ACK a389ed52e8f4939ab5b4adcf93dcb7783d9006f1 🔳
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUi6RwwAo8bI3tkD2UACsJtRgQS6CUj/gTqTq0lCZ9LijSV9USsdnwawka4GUQt+
Gd9h7dvn7c3I8F9fo1SnLQS+hB0VfYSB+Vo68V2dkxMiNixTxGt+CNoLsNfD3AQb
LLSvBZxA+ul1rAzopNEIGRLFBeV5ddFr3RlspZPUKTo4b7iryovqAzDmeM5YnjaW
4J95TT48By2IxwV+Q7gnw0u0OaW2I6lFcklrS8QGU17n/7phTwYu1O9ML1NmuPp4
WSvkMwx2nkJ3FXCXg572P9eCAcaj7UPwSQcA6QFeNwapUoOknIS2Qq1hhAjhtBXH
3+QxywB6Jb24ik3h57O+wWk3NQeygt7z2qXSJOr2YjfouY43M/BwyoRdE6rB9wzq
byXTD0WLrMLgk9qMzxquS8d+Pm0FCzI0R54SnesvllhT1DgvrAny6KU934YkuZVW
2mZoPwtMTzRFuDOlvoDZVHwkFlOueuLHwpO1IFdtqERAd3V6KGHV9GCdc5F1jVBq
k130gEHT
=3CaZ
-----END PGP SIGNATURE-----

Timestamp of file with hash 6bf76cee68718cc9e56aef4002215a6e74aed5587fdc5d0275fb0e5769554ec1 -

bool ReadKey(CDataStream& key, CDataStream& value);
bool WriteKey(CDataStream& key, CDataStream& value, bool overwrite=true);
bool EraseKey(CDataStream& key);
bool HasKey(CDataStream& key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it really confusing that all the const& are replaced by &. Is there any reason for that? If not, it could make sense to fix this up in a follow-up commit.

Copy link
Member

@laanwj laanwj Jun 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. If nothing else, const is good documentation about what can potentially be modified and what not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re: #19292 (comment)

I found it really confusing that all the const& are replaced by &. Is there any reason for that? If not, it could make sense to fix this up in a follow-up commit.

I assumed it was necessary to remove const from all the arguments because reading from CDataStream objects changes their internal real read position. This is a reason I suggested replacing all the CDataStream arguments with Spans #18971 (comment). This could be done as a later followup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both data and size are marked const in the CDataStream class. How would this affect the read position anyway?

I think the reason is that the in-memory key data needs to be purged (in case it is a private key)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both data and size are marked const in the CDataStream class. How would this affect the read position anyway?

Guess it wouldn't. I didn't check if these were const because I just remembered CDataStream being goofy about reading. Maybe it's less goofy than I thought but I still think Spans are better.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@achow101 Let us know if you want to change this to span or rather have the current version of the pr merged

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like my patch only compiles after commit b8740d6 (Merge #18468: Span improvements)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave it for a followup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on using spans
I also thought about making BerkeleyBatch::ReadKey return an Option<CDataStream> instead of passing it in, but dunno.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #19320

@maflcko maflcko merged commit c7ebab1 into bitcoin:master Jun 18, 2020
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jun 2, 2021
Summary:
In order to override these later, the specific details of how the Read,
Write, Erase, and Exists functions interact with the actual database
file need to go into functions that are not templated.

This is a backport of [[bitcoin/bitcoin#19292 | core#19292]]

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D9618
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants