ssbc / jitdb

A database on top of a log with automatic index generation and maintenance
50 stars 7 forks source link

`absent` operator -- an example of `seek` #190

Closed nichoth closed 2 years ago

nichoth commented 2 years ago

This is part of this sbot -- https://github.com/planetary-social/planetary-pub/blob/7ea9eb2fd3b021f1f862eaea9b0349a5fb0c2d97/viewer.js#L133

What is the type of value does absent take?

absent('value.root', { indexType: 'roots' }),
/Users/nick/code/planetary-pub/node_modules/jitdb/index.js:345
    const fieldStart = opData.seek(buffer)
                              ^

TypeError: opData.seek is not a function
    at checkAbsent (/Users/nick/code/planetary-pub/node_modules/jitdb/index.js:345:31)
    at updateIndexValue (/Users/nick/code/planetary-pub/node_modules/jitdb/index.js:427:38)
    at /Users/nick/code/planetary-pub/node_modules/jitdb/index.js:687:16
    at Array.forEach (<anonymous>)
    at Object.write (/Users/nick/code/planetary-pub/node_modules/jitdb/index.js:668:23)
    at Object.o.write (/Users/nick/code/planetary-pub/node_modules/ssb-db2/log.js:95:30)
    at Stream._writeToSink (/Users/nick/code/planetary-pub/node_modules/async-append-only-log/stream.js:60:33)
    at Stream._handleBlock (/Users/nick/code/planetary-pub/node_modules/async-append-only-log/stream.js:96:12)
    at Stream._resumeCallback (/Users/nick/code/planetary-pub/node_modules/async-append-only-log/stream.js:143:24)
    at Request._callback (/Users/nick/code/planetary-pub/node_modules/async-append-only-log/index.js:151:9)
arj03 commented 2 years ago

See the tests :)

https://github.com/ssb-ngi-pointer/jitdb/blob/5d197adfe551888a8ba813a1ab84394e12c18130/test/operators.js#L1475

It needs to be a function.

This should be in the README. If not, a PR to make it more clear would be appreciated :)

staltz commented 2 years ago

Actually, absent() is explained in the README already. See previous definitions of the seek argument in the README, a bit above the description of absent()

nichoth commented 2 years ago

So, looking at #191 -- it's not clear to me what these example values are used for. And what does the seekChannel function do here?

const B_VALUE = Buffer.from('value')
const B_CHANNEL = Buffer.from('channel')
const B_CONTENT = Buffer.from('content')
function seekChannel (buffer) {
  var p = 0 // note you pass in p!
  p = bipf.seekKey(buffer, p, B_VALUE)
  if (~p) {
    p = bipf.seekKey(buffer, p, B_CONTENT)
    if (~p) return bipf.seekKey(buffer, p, B_CHANNEL)
  }
}

This is assuming a standard message format --

 {
  key: '%1HbhmsEc4OCiLD5o8raRl+x8QUO7Y6oZ3C57vwNM78c=.sha256',
  value: {
    previous: '%CCceib4KRShqtaN95hPGl0qORtefsTEb/qwbB0YZVyQ=.sha256',
    sequence: 3,
    author: '@lV5MISER9oGaZJ7OLhlsUNVWHu982USYgMEWfIs6le0=.ed25519',
    timestamp: 1638820387617,
    hash: 'sha256',
    content: { type: 'post', text: 'three' },
    signature: 'E2sC6mH9F+HhfIVl6MjobLdZX6RG3QRwFBiMoT5vb64L6XkS5TutPh2gYRRIqKZSzzW5ld0sLvvEc81pcrRtCQ==.sig.ed25519'
  },
  timestamp: 1638820387618
}

As an example, lets say you are looking for messages that are not replies to other messages, so messages without a root property in the JSON

arj03 commented 2 years ago

What are you trying to do here? It seems like you are looking for isRoot. Why not just use that? Also absent is very special is means as the name implies that the value is absent, not even undefined.

nichoth commented 2 years ago

It seems like you are looking for isRoot.

Thanks for that; i didn't know about that one.

absent is very special is means as the name implies that the value is absent, not even undefined.

That's interesting; I didn't know that you could detect a difference between undefined vs not existing.