kappa-db / kappa-core

Minimal peer-to-peer database, based on kappa architecture.
254 stars 22 forks source link

README example fails: Key not found in database [state] #10

Closed richburdon closed 5 years ago

richburdon commented 5 years ago

The example in the README fails with:

Key not found in database [state]

Actually it fails silently, but adding the following makes the error visiable

  core.on('error', err => {
    console.error('ERROR', String(err));
  });

I don't understand the semantics yet, but fetchState is called before storeState.

hackergrrl commented 5 years ago

Good catch! I forgot to update the readme with an API change (feed is now writer). Could you retry with the updated README example and re-open if this is still broken?

richburdon commented 5 years ago

Hey noffle -- thanks for the quick update! (and nice to see you :).

I had already tried feed => writer, but am still having the same error. I'm running this as a jest test (node 11) but can't see how that changes anything?

I'm sure I'll get things working and will send you a PR. Just haven't had time today.

import kappa from 'kappa-core';
import memdb from 'memdb';

test('kappa-core', done => {
  const core = kappa('./out', { valueEncoding: 'json' });
  const idx = memdb();

  let sum = 0;

  let sumview = {
    api: {
      get: function(core, cb) {
        this.ready(function() {
          cb(null, sum)
        })
      }
    },

    map: function(msgs, next) {
      msgs.forEach(function(msg) {
        if (typeof msg.value === 'number') sum += msg.value;
      });

      next();
    },

    // where to store and fetch the indexer's state (which log entries have been processed so far)
    storeState: function(state, cb) {
      idx.put('state', state, cb)
    },

    fetchState: function(cb) {
      idx.get('state', cb)
    }
  };

  core.on('error', err => { console.error(String(err)); });

  // the api will be mounted at core.api.sum
  core.use('sum', 1, sumview);  // name the view 'sum' and consider the 'sumview' logic as version 1

  core.writer('default', function(err, feed) {
    feed.append(1, function(err) {
      expect(err).toBeFalsy();
      core.api.sum.get(function(err, value) {
        expect(value).toBe(1);
        done();
      })
    })
  });
});
hackergrrl commented 5 years ago

You're right: this shouldn't be an error case. I just published multifeed-index 3.3.2, which no longer interprets a leveldb error with err.notFound as a regular error. If you reinstall kappa-core, it should pick that up & start working for you!