Skip to content
Browse files

refactor into seperate files

  • Loading branch information...
1 parent 3fc9cb4 commit 9496653a6ece1d12af5dcb5f87216c72dffb753f @dominictarr committed
Showing with 120 additions and 94 deletions.
  1. +44 −0 cli.js
  2. +55 −0 endpoints.js
  3. +21 −94 index.js
View
44 cli.js
@@ -0,0 +1,44 @@
+#! /usr/bin/env node
+
+if(!module.parent) {
+ var argv = require('optimist').argv
+ var op = (argv._[0] || '').toLowerCase()
+ var key = argv._[1]
+ var base = argv.base || argv.b || process.env.KV_BASE
+
+ if (!~['put', 'get', 'del', 'has'].indexOf(op) &&
+ (!key || key.length < 1) && op !== 'list' || !base) {
+ var e = console.error
+ e('USAGE: kv put|get|del|has $KEY --base $BASEDIR')
+ e('')
+ e(' source | kv put $KEY # write a record')
+ e('')
+ e(' kv get $KEY | sink # read a record')
+ e('')
+ e(' kv del $KEY # delete a record')
+ e('')
+ e(' kv get __list # get list of changes')
+ e('')
+ e(' kv has $KEY # test wether $KEY is in db')
+ process.exit(1)
+ }
+
+ var kv = require('./')(base)
+
+ if(op == 'get')
+ kv.get.raw(key).pipe(process.stdout)
+ else if (op == 'put')
+ process.stdin.pipe(kv.put.raw(key))
+ else if (op == 'del')
+ kv.del(key, function () {})
+ else if (op == 'has')
+ kv.has(key, function (err, has) {
+ if(err) console.error(err.message)
+ process.exit(has ? 0 : 1)
+ })
+ else if (op == 'list') {
+ kv.on('sync', function () {
+ console.log(kv.list().join('\n'))
+ })
+ }
+}
View
55 endpoints.js
@@ -0,0 +1,55 @@
+
+var fs = require('fs')
+var mkdirP = require('mkdirp')
+var fs = require('fs')
+var crypto = require('crypto')
+var es = require('event-stream')
+var join = require('path').join
+
+function hash (key) {
+ return crypto.createHash('sha1').update(key).digest('hex')
+}
+
+module.exports = function (basedir, exports) {
+ exports = exports || {}
+
+ /*
+ okay, so this is gonna run in the browser, attached to localStorage also.
+
+
+*/
+ exports.put = function (key, opts) {
+ var _key = hash(key)
+ var dir = _key.substring(0, 2)
+ var file = key
+ var stream = es.gate(true)
+ mkdirP(join(basedir, dir), function (err) {
+ if(err)
+ return stream.emit('error', err)
+ stream.pipe(fs.createWriteStream(join(basedir, dir, file), opts))
+ stream.open()
+ })
+ return stream
+ }
+
+ exports.get = function (key, opts) {
+ var _key = hash(key)
+ var dir = _key.substring(0, 2)
+ return fs.createReadStream(join(basedir, dir, key), opts)
+ }
+
+ exports.has = function (key, callback) {
+ var _key = hash(key)
+ var dir = _key.substring(0, 2)
+ fs.stat(join(basedir, dir, key), callback)
+ }
+
+ function del(key, cb) {
+ var _key = hash(key)
+ var dir = _key.substring(0, 2)
+ var file = _key.substring(2)
+ }
+
+ return exports
+}
+
View
115 index.js 100755 → 100644
@@ -1,22 +1,13 @@
-#! /usr/bin/env node
/*
very simple kv store setup for to be able to append to each document.
each value is stored in a separate file,
put, get, return streams
*/
-var fs = require('fs')
-var mkdirP = require('mkdirp')
var es = require('event-stream')
-var crypto = require('crypto')
var EventEmitter = require('events').EventEmitter
-var join = require('path').join
-function hash (key) {
- return crypto.createHash('sha1').update(key).digest('hex')
-}
-
-module.exports = kvdb
+module.exports = kv
var formats = {
raw: function (stream) {
@@ -51,7 +42,7 @@ var formats = {
function mkFormat(fn, format) {
return function () {
- return format(fn.apply(null, arguments))
+ return format(fn.apply(this, arguments))
}
}
@@ -62,46 +53,17 @@ function addFormats(fn) {
return f
}
-function kvdb (basedir) {
+function kv (basedir) {
//by default, use newline seperated json.
var emitter = new EventEmitter()
- var keys = []
- function put (key, opts) {
- var _key = hash(key)
- var dir = _key.substring(0, 2)
- var file = key
- var stream = es.gate(true)
- mkdirP(join(basedir, dir), function (err) {
- if(err)
- return stream.emit('error', err)
- stream.pipe(fs.createWriteStream(join(basedir, dir, file), opts))
- stream.open()
- })
- emitter.emit('put', key, Date.now(), stream, opts)
- return stream
- }
-
- function get(key, opts) {
- var _key = hash(key)
- var dir = _key.substring(0, 2)
- return fs.createReadStream(join(basedir, dir, key), opts)
- }
-
- function has(key, callback) {
- var _key = hash(key)
- var dir = _key.substring(0, 2)
- fs.stat(join(basedir, dir, key), callback)
- }
-
- function del(key, cb) {
- var _key = hash(key)
- var dir = _key.substring(0, 2)
- var file = _key.substring(2)
- emitter.emit('del', key, Date.now())
- }
+ var ends = require('./endpoints')(basedir)
+ var keys = {}
function list() {
- return Object.keys(keys)
+ var _keys = []
+ for (var k in keys)
+ _keys.push(k)
+ return _keys
}
function addToKeys (data) {
@@ -111,10 +73,18 @@ function kvdb (basedir) {
delete keys[data[1]]
}
//wrap formats arount get and put, so you can go get.json(key) or get.raw(key)
- emitter.put = addFormats(put)
- emitter.get = addFormats(get)
- emitter.del = del
- emitter.has = has
+
+ emitter.put = addFormats(function (key, opts) {
+ var s = ends.put(key, opts)
+ emitter.emit('put', key, Date.now(), s, opts)
+ return s
+ })
+ emitter.get = addFormats(ends.get)
+ emitter.del = function (key, cb) {
+ emitter.emit('del', key, Date.now())
+ ends.del(key, cb)
+ }
+ emitter.has = ends.has
emitter.list = list
//TODO smarter way to compact the __list, so that can have last update.
@@ -143,46 +113,3 @@ function kvdb (basedir) {
return emitter
}
-
-if(!module.parent) {
- var argv = require('optimist').argv
- var op = (argv._[0] || '').toLowerCase()
- var key = argv._[1]
- var base = argv.base || argv.b || process.env.KV_BASE
-
- if (!~['put', 'get', 'del', 'has'].indexOf(op) &&
- (!key || key.length < 1) && op !== 'list' || !base) {
- var e = console.error
- e('USAGE: kv put|get|del|has $KEY --base $BASEDIR')
- e('')
- e(' source | kv put $KEY # write a record')
- e('')
- e(' kv get $KEY | sink # read a record')
- e('')
- e(' kv del $KEY # delete a record')
- e('')
- e(' kv get __list # get list of changes')
- e('')
- e(' kv has $KEY # test wether $KEY is in db')
- process.exit(1)
- }
-
- var kv = kvdb(base)
-
- if(op == 'get')
- kv.get.raw(key).pipe(process.stdout)
- else if (op == 'put')
- process.stdin.pipe(kv.put.raw(key))
- else if (op == 'del')
- kv.del(key, function () {})
- else if (op == 'has')
- kv.has(key, function (err, has) {
- if(err) console.error(err.message)
- process.exit(has ? 0 : 1)
- })
- else if (op == 'list') {
- kv.on('sync', function () {
- console.log(kv.list().join('\n'))
- })
- }
-}

0 comments on commit 9496653

Please sign in to comment.
Something went wrong with that request. Please try again.