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

bulk create adds messages to db in wrong order #428

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
"ssb-caps": "1.1.0",
"ssb-db": "19.3.1",
"ssb-fixtures": "4.0.1",
"tap-arc": "^0.3.4",
"tape": "^5.2.2",
"tap-arc": "^1.2.2",
"tape": "^5.7.5",
"trammel": "~4.0.0"
},
"scripts": {
Expand Down
55 changes: 52 additions & 3 deletions test/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ const bipf = require('bipf')
const mkdirp = require('mkdirp')
const SecretStack = require('secret-stack')
const caps = require('ssb-caps')
const { where, isDecrypted, toCallback } = require('../operators')
const pull = require('pull-stream')
const { promisify: p } = require('util')

const {
where,
type,
isDecrypted,
toCallback,
toPullStream,
} = require('../operators')

const dir = '/tmp/ssb-db2-create'

Expand All @@ -19,7 +28,7 @@ mkdirp.sync(dir)

const keys = ssbKeys.loadOrCreateSync(path.join(dir, 'secret'))

let sbot = SecretStack({ appKey: caps.shs })
const sbot = SecretStack({ appKey: caps.shs })
.use(require('../'))
.use(require('../compat/ebt'))
.use(require('ssb-bendy-butt'))
Expand All @@ -28,7 +37,7 @@ let sbot = SecretStack({ appKey: caps.shs })
keys,
path: dir,
})
let db = sbot.db
const db = sbot.db

test('create() classic', (t) => {
db.create(
Expand Down Expand Up @@ -85,6 +94,46 @@ test('create() classic "too big"', (t) => {
})
})

test('create() bulk, fast!', async (t) => {
const N = 4
const many = new Array(N).fill('').map(() => {
return { type: 'counter' }
})

const expectedSequenceOrder = new Array(N).fill(0).map((_, i) => i + 1)

await Promise.all(
many.map((content) => {
return p(db.create)({ feedFormat: 'classic', content })
Copy link
Member

Choose a reason for hiding this comment

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

Promise.all() is not the right way of doing this, you need to do them sequentially. Otherwise it's very likely indeed that they'll end up in the wrong order, because this has to do with jitdb indexing the log.

ssb-db2 is optimized for fast reads not for fast writes, so be patient if it's not the best at bulk creating msgs.

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 wrote a test where I did it sequentially with a pull-stream, and it still got them wrong I think...

the fast writes thing is gonna be a challenge with Ahau importing some records. Working on that though

})
)
.then((msgs) => {
t.pass('bulk publishes messages')
t.deepEqual(
msgs.map((m) => m.value.sequence),
expectedSequenceOrder,
'messages created in correct order'
)
})
.catch((err) => t.error(err, 'bulk publishes messages'))

const sequencesFromDb = await pull(
db.query(where(type('counter')), toPullStream()),
pull.map((m) => m.value.sequence),
pull.collectAsPromise()
)

t.deepEqual(
sequencesFromDb,
expectedSequenceOrder,
'messages come out of the DB in the right order'
)
t.end()

// TEMP
sbot.close(true, () => t.end())
})

test('create() classic box', (t) => {
db.create(
{
Expand Down
Loading