Skip to content
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

db: add new backend ffldb #331

Closed
wants to merge 16 commits into from
Closed

Conversation

tuxcanfly
Copy link
Member

@tuxcanfly tuxcanfly commented Oct 8, 2017

This PR adds a new db backend, ffldb, which stores blocks in flat files, using leveldb as a metadata database to store the indexes. The file format blocks/*.fdb is inspired from btcd and compatibility is maintained.

Working on wrapping up some of the remaining cases and scenarios. Will be able to run and share benchmarks soon.

WIP: Not ready for merge


bw.writeU32(this.network.magic);
bw.writeU32(this.block.length);
bw.writeBytes(this.block);
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to write the block to disk directly and build the CRC incrementally. This prevents a reallocation of 1mb+ just for a write.

BlockIO.prototype.open = function open(file = 0, offset = 0) {
// TODO: load cursor state from db
const filepath = this.filepath(file);
const fd = fs.openSync(filepath, 'a+');
Copy link
Member

Choose a reason for hiding this comment

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

Async calls please: await fs.open(filepath, 'a+')

*/

BlockIO.prototype.blockFile = async function blockFile(file) {
if (file in this.files) {
Copy link
Member

Choose a reason for hiding this comment

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

No usage of in. Use if (this.files[file] != null) if you need to.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

lib/db/ffldb.js Outdated
FlatFileDB.prototype.open = function open(options, callback) {
this.blockio.ensure();
this.blockio.open();
LevelDOWN.prototype.open.call(this, options, callback);
Copy link
Member

Choose a reason for hiding this comment

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

Not good to inherit from Leveldown directly. Use db/lowlevelup instead, to avoid having to call co.promisify repeatedly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated, no co.promisify anymore.

@bucko13 bucko13 added the WIP label Oct 16, 2017
@tuxcanfly
Copy link
Member Author

Most recent benchmarks for comparision:

Novigrad :: ~/Work/bcoin » node bench/leveldb.js
read block: ops=100000, time=2.850579412, rate=35080.58733
Novigrad :: ~/Work/bcoin » node bench/ffldb.js
read block: ops=100000, time=5.580575833, rate=17919.29776

There doesn't seem to be any performance improvement in block read times, which was one of the reasons for moving to the flat file backend.

@chjj I guess this PR can be closed.

@chjj
Copy link
Member

chjj commented Apr 6, 2018

We can explore this more in the future. I feel like maybe node.js's use of pwrite makes our ffldb impl slower than others. We still have multiple branches with many different impls, so we'll see where we get to in the future.

@chjj chjj closed this Apr 6, 2018
@bucko13 bucko13 added the WIP label Apr 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants