-
-
Save juliangruber/8728c398418b73fa5ec9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var leveled = require('leveled') | |
var fs = require('fs') | |
var http = require('http') | |
var db | |
// insert values | |
if (!fs.existsSync('db')) { | |
db = leveled('db') | |
var batch = db.batch() | |
for (var i = 0, t = Date.now(); i < 120000; i++, t++) { | |
batch.put(t, '1337,1337,1337,1337,1337,1337') | |
} | |
batch.writeSync() | |
} | |
if (!db) db = leveled('db') | |
http.createServer(function (req, res) { | |
if (req.url == '/favicon.ico') return res.end() | |
var start = Date.now() | |
if (req.url == '/') { | |
db.find('*', function (err, entries) { | |
if (err) return res.end(err.toString()) | |
print(res, entries) | |
}) | |
} else if (req.url.indexOf(',') > -1) { | |
var url = req.url.substr(1, req.url.length - 1) | |
var segs = url.split(',') | |
db.range(segs[0], segs[1], function (err, entries) { | |
if (err) return res.end(err.toString()) | |
print(res, entries) | |
}) | |
} else { | |
var url = req.url.substr(1, req.url.length - 1) | |
db.get(url, function (err, val) { | |
if (err) return res.end(err.toString()) | |
res.end(url + ' : ' + val) | |
}) | |
} | |
}).listen(3000) | |
/** | |
* REPLICATE HERE | |
*/ | |
/** | |
* Utility | |
*/ | |
function print (res, entries) { | |
var buf = [] | |
Object.keys(entries).forEach(function (key) { | |
buf.push(key + ' : ' + entries[key]) | |
}) | |
res.end(buf.join('\n')) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment