mafintosh / hyperdb

Distributed scalable database
MIT License
753 stars 75 forks source link

local content hypercore is not writable when re-opening using disk storage #95

Closed jimpick closed 6 years ago

jimpick commented 6 years ago

Here's a test case:

tape('reopen content', function (t) {
  rimraf.sync('./db-1')
  var db = create.one(null, {
    contentFeed: true,
    storage: './db-1'
  })
  db.put('hello', 'world', function () {
    db.localContent.append('data', function () {
      db.get('hello', function (err, node) {
        t.error(err, 'no error')
        t.same(node.value, 'world')
        db.put('hello', {start: 0, end: 1}, function (err) {
          t.error(err, 'no error')
          // Reopen, re-using storage
          var dbReopened = create.one(null, {
            storage: './db-1',
            contentFeed: true
          })
          dbReopened.ready(() => {
            t.ok(dbReopened.localContent.writable)
          })
          t.end()
        })
      })
    })
  })
})
jimpick commented 6 years ago

Here's a possible fix:

diff --git a/index.js b/index.js
index 9965fde..029acf2 100644
--- a/index.js
+++ b/index.js
@@ -584,11 +584,11 @@ HyperDB.prototype._ready = function (cb) {
       if (err) return done(err)

       self._localWriter = self._writers[self.feeds.indexOf(self.local)]
+      if (self._contentStorage) self._localWriter._ensureContentFeed(null)
       self._localWriter.head(function (err) {
         if (err) return done(err)
         if (!self._contentStorage) return done(null)

-        self._localWriter._ensureContentFeed(null)
         self.localContent = self._localWriter._contentFeed
         self.localContent.ready(done)
       })
mafintosh commented 6 years ago

added your fix to master

mafintosh commented 6 years ago

re-open if it still happens :)