Skip to content

Commit 7f488b0

Browse files
committed
Updated components to use mini-megastore
1 parent 7f8f4c8 commit 7f488b0

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const argv = require('yargs').argv
99
const grpc = require('grpc')
1010
const { rpc, loadMetadata } = require('hyperdrive-daemon-client')
1111

12-
const Megastore = require('megastore')
12+
const Megastore = require('mini-megastore')
1313
const SwarmNetworker = require('megastore-swarm-networking')
1414

1515
const { DriveManager, createDriveHandlers } = require('./lib/drives')
@@ -31,19 +31,18 @@ class HyperdriveDaemon extends EventEmitter {
3131
this.opts = opts
3232

3333
const dbs = {
34-
megastore: sub(this.db, 'megastore'),
3534
fuse: sub(this.db, 'fuse', { valueEncoding: 'json' }),
3635
drives: sub(this.db, 'drives', { valueEncoding: 'json' })
3736
}
3837

3938
const megastoreOpts = {
4039
storage: path => raf(`${storage}/cores/${path}`),
41-
db: dbs.megastore,
42-
networker: new SwarmNetworker(opts.network)
40+
sparse: true
4341
}
4442

45-
this.megastore = new Megastore(megastoreOpts.storage, megastoreOpts.db , megastoreOpts.networker)
46-
this.drives = new DriveManager(this.megastore, dbs.drives, this.opts)
43+
this.megastore = new Megastore(megastoreOpts.storage, megastoreOpts)
44+
this.networking = new SwarmNetworker(this.megastore, opts.network)
45+
this.drives = new DriveManager(this.megastore, this.networking, dbs.drives, this.opts)
4746
this.fuse = hyperfuse ? new FuseManager(this.megastore, this.drives, dbs.fuse, this.opts) : null
4847

4948
this.drives.on('error', err => this.emit('error', err))
@@ -62,6 +61,7 @@ class HyperdriveDaemon extends EventEmitter {
6261
return Promise.all([
6362
this.db.open(),
6463
this.megastore.ready(),
64+
this.networking.listen(),
6565
this.drives.ready(),
6666
this.fuse ? this.fuse.ready() : Promise.resolve()
6767
]).then(() => {

lib/drives/index.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@ const { EventEmitter } = require('events')
33

44
const hyperdrive = require('hyperdrive')
55
const sub = require('subleveldown')
6-
const collect = require('stream-collector')
6+
const collectStream = require('stream-collector')
77
const datEncoding = require('dat-encoding')
88

99
const { fromHyperdriveOptions, toHyperdriveOptions } = require('hyperdrive-daemon-client/lib/common')
1010
const log = require('../log').child({ component: 'drive-manager' })
1111

1212
class DriveManager extends EventEmitter {
13-
constructor (megastore, db, opts) {
13+
constructor (megastore, networking, db, opts) {
1414
super()
1515

1616
this.megastore = megastore
17+
this.networking = networking
1718
this.db = db
1819
this.opts = opts || {}
1920

2021
this._driveIndex = sub(this.db, 'drives', { valueEncoding: 'json' })
21-
this._nameIndex = sub(this.db, 'names', { valueEncoding: 'utf-8' })
22+
this._seedIndex = sub(this.db, 'seeding', { valueEncoding: 'utf8' })
23+
this._nameIndex = sub(this.db, 'names', { valueEncoding: 'utf8' })
2224

2325
if (this.opts.stats) {
2426
this._statsIndex = sub(this.db, 'stats', { valueEncoding: 'json' })
@@ -27,8 +29,18 @@ class DriveManager extends EventEmitter {
2729

2830
// TODO: Replace with an LRU cache.
2931
this._drives = new Map()
30-
// TODO: Any ready behavior here?
31-
this.ready = () => Promise.resolve()
32+
this._ready = new Promise(async resolve => {
33+
await this._reseed()
34+
return resolve()
35+
})
36+
this.ready = () => this._ready
37+
}
38+
39+
async _reseed () {
40+
const driveList = await collect(this._driveIndex)
41+
for (const { key: discoveryKey } of driveList) {
42+
this.networking.seed(discoveryKey)
43+
}
3244
}
3345

3446
_configureDrive (drive, opts) {
@@ -147,11 +159,15 @@ class DriveManager extends EventEmitter {
147159
}
148160

149161
async publish (drive) {
150-
return this.megastore.seed(drive.discoveryKey)
162+
const encodedKey = datEncoding.encode(drive.discoveryKey)
163+
this.networking.seed(drive.discoveryKey)
164+
await this._seedIndex.put(encodedKey, '')
151165
}
152166

153167
async unpublish (drive) {
154-
return this.megastore.unseed(drive.discoveryKey)
168+
const encodedKey = datEncoding.encode(drive.discoveryKey)
169+
this.networking.unseed(drive.discoveryKey)
170+
await this._seedIndex.del(encodedKey)
155171
}
156172

157173
// TODO: Retrieving stats from managed hyperdrives is trickier with corestores/mounts.
@@ -206,6 +222,15 @@ function createDriveHandlers (driveManager) {
206222
}
207223
}
208224

225+
function collect (index, opts) {
226+
return new Promise((resolve, reject) => {
227+
collectStream(index.createReadStream(opts), (err, list) => {
228+
if (err) return reject(err)
229+
return resolve(list)
230+
})
231+
})
232+
}
233+
209234
module.exports = {
210235
DriveManager,
211236
createDriveHandlers

0 commit comments

Comments
 (0)