Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Refactor/async await #20

Merged
merged 4 commits into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
"leadMaintainer": "Pedro Teixeira <[email protected]>",
"main": "src/index.js",
"scripts": {
"lint": "aegir lint",
"build": "aegir build",
"test": "aegir test",
"flow": "flow",
"test:node": "aegir test --target node",
"test:browser": "aegir test --target browser",
"release": "aegir release --docs",
"release-minor": "aegir release --type minor --docs",
"release-major": "aegir release --type major --docs",
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage --provider codecov",
"docs": "aegir docs"
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser",
"test:webworker": "aegir test -t webworker",
"build": "aegir build",
"lint": "aegir lint",
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"coverage": "nyc -s npm run test:node && nyc report --reporter=html",
"dep-check": "aegir dep-check"
},
"files": [
"src",
Expand All @@ -39,16 +38,14 @@
},
"homepage": "https://github.com/ipfs/js-datastore-core#readme",
"devDependencies": {
"aegir": "^18.2.2",
"aegir": "^19.0.3",
"async-iterator-all": "^1.0.0",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
"flow-bin": "~0.98.1"
"dirty-chai": "^2.0.1"
},
"dependencies": {
"async": "^2.6.1",
"interface-datastore": "~0.6.0",
"pull-many": "^1.0.8",
"pull-stream": "^3.6.9"
"debug": "^4.1.1",
"interface-datastore": "~0.7.0"
},
"engines": {
"node": ">=6.0.0",
Expand Down
44 changes: 21 additions & 23 deletions src/keytransform.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* @flow */
'use strict'

const pull = require('pull-stream')
const utils = require('interface-datastore').utils
const map = utils.map

/* ::
import type {Key, Datastore, Batch, Query, QueryResult, Callback} from 'interface-datastore'
Expand Down Expand Up @@ -38,24 +39,24 @@ class KeyTransformDatastore /* :: <Value> */ {
this.transform = transform
}

open (callback /* : Callback<void> */) /* : void */ {
this.child.open(callback)
open () /* : Promise<void> */ {
return this.child.open()
}

put (key /* : Key */, val /* : Value */, callback /* : Callback<void> */) /* : void */ {
this.child.put(this.transform.convert(key), val, callback)
put (key /* : Key */, val /* : Value */) /* : Promise<void> */ {
return this.child.put(this.transform.convert(key), val)
}

get (key /* : Key */, callback /* : Callback<Value> */) /* : void */ {
this.child.get(this.transform.convert(key), callback)
get (key /* : Key */) /* : Promise<Value> */ {
return this.child.get(this.transform.convert(key))
}

has (key /* : Key */, callback /* : Callback<bool> */) /* : void */ {
this.child.has(this.transform.convert(key), callback)
has (key /* : Key */) /* : Promise<bool> */ {
return this.child.has(this.transform.convert(key))
}

delete (key /* : Key */, callback /* : Callback<void> */) /* : void */ {
this.child.delete(this.transform.convert(key), callback)
delete (key /* : Key */) /* : Promise<void> */ {
return this.child.delete(this.transform.convert(key))
}

batch () /* : Batch<Value> */ {
Expand All @@ -67,24 +68,21 @@ class KeyTransformDatastore /* :: <Value> */ {
delete: (key /* : Key */) /* : void */ => {
b.delete(this.transform.convert(key))
},
commit: (callback /* : Callback<void> */) /* : void */ => {
b.commit(callback)
commit: () /* : Promise<void> */ => {
return b.commit()
}
}
}

query (q /* : Query<Value> */) /* : QueryResult<Value> */ {
return pull(
this.child.query(q),
pull.map(e => {
e.key = this.transform.invert(e.key)
return e
})
)
query (q /* : Query<Value> */) /* : Iterator */ {
return map(this.child.query(q), e => {
e.key = this.transform.invert(e.key)
return e
})
}

close (callback /* : Callback<void> */) /* : void */ {
this.child.close(callback)
close () /* : Promise<void> */ {
return this.child.close()
}
}

Expand Down
100 changes: 45 additions & 55 deletions src/mount.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
/* @flow */
'use strict'

const each = require('async/each')
const many = require('pull-many')
const pull = require('pull-stream')

const Key = require('interface-datastore').Key
const Errors = require('interface-datastore').Errors
const utils = require('interface-datastore').utils
const asyncFilter = utils.asyncFilter
const asyncSort = utils.asyncSort
const filter = utils.filter
const take = utils.take
const sortAll = utils.sortAll
const replaceStartWith = utils.replaceStartWith

const Keytransform = require('./keytransform')
Expand All @@ -34,10 +31,8 @@ class MountDatastore /* :: <Value> */ {
this.mounts = mounts.slice()
}

open (callback /* : Callback<void> */) /* : void */ {
each(this.mounts, (m, cb) => {
m.datastore.open(cb)
}, callback)
open () /* : Promise<void> */ {
return Promise.all(this.mounts.map((m) => m.datastore.open()))
}

/**
Expand All @@ -60,53 +55,44 @@ class MountDatastore /* :: <Value> */ {
}
}

put (key /* : Key */, value /* : Value */, callback /* : Callback<void> */) /* : void */ {
put (key /* : Key */, value /* : Value */) /* : Promise<void> */ {
const match = this._lookup(key)
if (match == null) {
return callback(
Errors.dbWriteFailedError(new Error('No datastore mounted for this key'))
)
throw Errors.dbWriteFailedError(new Error('No datastore mounted for this key'))
}

match.datastore.put(match.rest, value, callback)
return match.datastore.put(match.rest, value)
}

get (key /* : Key */, callback /* : Callback<Value> */) /* : void */ {
get (key /* : Key */) /* : Promise<Value> */ {
const match = this._lookup(key)
if (match == null) {
return callback(
Errors.notFoundError(new Error('No datastore mounted for this key'))
)
throw Errors.notFoundError(new Error('No datastore mounted for this key'))
}

match.datastore.get(match.rest, callback)
return match.datastore.get(match.rest)
}

has (key /* : Key */, callback /* : Callback<bool> */) /* : void */ {
has (key /* : Key */) /* : Promise<bool> */ {
const match = this._lookup(key)
if (match == null) {
callback(null, false)
return
return false
}

match.datastore.has(match.rest, callback)
return match.datastore.has(match.rest)
}

delete (key /* : Key */, callback /* : Callback<void> */) /* : void */ {
delete (key /* : Key */) /* : Promise<void> */ {
const match = this._lookup(key)
if (match == null) {
return callback(
Errors.dbDeleteFailedError(new Error('No datastore mounted for this key'))
)
throw Errors.dbDeleteFailedError(new Error('No datastore mounted for this key'))
}

match.datastore.delete(match.rest, callback)
return match.datastore.delete(match.rest)
}

close (callback /* : Callback<void> */) /* : void */ {
each(this.mounts, (m, cb) => {
m.datastore.close(cb)
}, callback)
close () /* : Promise<void> */ {
return Promise.all(this.mounts.map((m) => {
return m.datastore.close()
}))
}

batch () /* : Batch<Value> */ {
Expand Down Expand Up @@ -137,10 +123,8 @@ class MountDatastore /* :: <Value> */ {
const match = lookup(key)
match.batch.delete(match.rest)
},
commit: (callback /* : Callback<void> */) /* : void */ => {
each(Object.keys(batchMounts), (p, cb) => {
batchMounts[p].commit(cb)
}, callback)
commit: () /* : Promise<void> */ => {
return Promise.all(Object.keys(batchMounts).map(p => batchMounts[p].commit()))
}
}
}
Expand Down Expand Up @@ -168,27 +152,33 @@ class MountDatastore /* :: <Value> */ {
})
})

let tasks = [many(qs)]

if (q.filters != null) {
tasks = tasks.concat(q.filters.map(f => asyncFilter(f)))
}

if (q.orders != null) {
tasks = tasks.concat(q.orders.map(o => asyncSort(o)))
}

let it = _many(qs)
if (q.filters) q.filters.forEach(f => { it = filter(it, f) })
if (q.orders) q.orders.forEach(o => { it = sortAll(it, o) })
if (q.offset != null) {
let i = 0
tasks.push(pull.filter(() => i++ >= q.offset))
it = filter(it, () => i++ >= q.offset)
}
if (q.limit != null) it = take(it, q.limit)

if (q.limit != null) {
tasks.push(pull.take(q.limit))
}

return pull.apply(null, tasks)
return it
}
}

function _many (iterable) {
return (async function * () {
let completed = iterable.map(() => false)
while (!completed.every(Boolean)) {
for (const [idx, itr] of iterable.entries()) {
const it = await itr.next()
if (it.done) {
completed[idx] = true
continue
}
yield it.value
}
}
})()
}

module.exports = MountDatastore
2 changes: 1 addition & 1 deletion src/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class NamespaceDatastore/* :: <Value> */ extends KeytransformDatastore /* :: <Va
this.prefix = prefix
}

query (q /* : Query<Value> */)/* : QueryResult<Value> */ {
query (q /* : Query<Value> */)/* : Iterator */ {
if (q.prefix && this.prefix.toString() !== '/') {
return super.query(Object.assign({}, q, {
prefix: this.prefix.child(new Key(q.prefix)).toString()
Expand Down
19 changes: 3 additions & 16 deletions src/shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,11 @@ function parseShardFun (str /* : string */) /* : ShardV1 */ {
}
}

exports.readShardFun = (path /* : string */, store /* : Datastore<Buffer> */, callback /* : Callback<ShardV1> */) /* : void */ => {
exports.readShardFun = async (path /* : string */, store /* : Datastore<Buffer> */) /* : Promise<ShardV1> */ => {
const key = new Key(path).child(new Key(SHARDING_FN))
const get = typeof store.getRaw === 'function' ? store.getRaw.bind(store) : store.get.bind(store)

get(key, (err, res) => {
if (err) {
return callback(err)
}

let shard
try {
shard = parseShardFun((res || '').toString().trim())
} catch (err) {
return callback(err)
}

callback(null, shard)
})
const res = await get(key)
return parseShardFun((res || '').toString().trim())
}

exports.readme = readme
Expand Down
Loading