jitdb icon indicating copy to clipboard operation
jitdb copied to clipboard

`seekType` function

Open nichoth opened this issue 4 years ago • 0 comments

The readme includes this example

query(
  fromDB(db),
  where(equal(seekType, 'post', { indexType: 'type' })),
  toCallback((err, msgs) => {
    console.log('There are ' + msgs.length + ' messages of type "post"')
  })
)

// The `seekType` function takes a buffer and uses `bipf` APIs to search for
// the fields we want.
const bValue = Buffer.from('value') // better for performance if defined outside
const bContent = Buffer.from('content')
const bType = Buffer.from('type')
function seekType(buffer) {
  var p = 0 // p stands for "position" in the buffer, offset from start
  p = bipf.seekKey(buffer, p, bValue)
  if (p < 0) return
  p = bipf.seekKey(buffer, p, bContent)
  if (p < 0) return
  return bipf.seekKey(buffer, p, bType)
}

Including some higher level text would help me understand this I think. What is the seekType function doing here? It could be good to include an example using 'real' fields in messages, instead of 'value', 'content', and 'type' like above

thanks

nichoth avatar Jan 17 '22 17:01 nichoth