Skip to content

Commit

Permalink
Added a basic solution based on level-sublevel.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Sep 17, 2013
1 parent 4b5ce62 commit 4a9481e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
7 changes: 7 additions & 0 deletions problems/queue/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

function setup(run, callback) {
console.log(arguments)
}

module.exports = setup
module.exports.async = true
45 changes: 45 additions & 0 deletions problems/queue/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var sublevel = require('level-sublevel')

function Queue(db, name) {
if (!(this instanceof Queue)) return new Queue(db, name)

this._db = sublevel(db).sublevel(name)
}

module.exports = Queue

Queue.prototype.push = function(element, callback) {
var key = (new Date()).toISOString()

this._db.put(key, element, callback)

return this;
}

Queue.prototype.shift = function(callback) {
var stream = this._db.createReadStream({
limit: 1
})

, db = this._db

stream.once('data', function(data) {
db.del(data.key, function(err) {
callback(err, data.value)
})
})

stream.once('error', callback)

return this;
}

Queue.prototype.clear = function(callback) {
this._db.createReadStream()
.pipe(this._db.createWriteStream({
type: 'del'
}))

This comment has been minimized.

Copy link
@juliangruber

juliangruber Sep 17, 2013

Contributor

aah, now I get what you pointed me to in that level-delete-stream issue :D

This comment has been minimized.

Copy link
@mcollina

mcollina Sep 17, 2013

Author

🍻

.on('close', callback)

return this;
}

0 comments on commit 4a9481e

Please sign in to comment.