[Pkg-javascript-commits] [node-leveldown] 16/492: copy() using pipe() with test
Andrew Kelley
andrewrk-guest at moszumanska.debian.org
Sun Jul 6 17:13:40 UTC 2014
This is an automated email from the git hooks/post-receive script.
andrewrk-guest pushed a commit to annotated tag rocksdb-0.10.1
in repository node-leveldown.
commit d5592a5c4f6d784a7b2a93cac7ab9f83660bc77a
Author: Rod Vagg <rod at vagg.org>
Date: Tue Aug 14 21:19:25 2012 +1000
copy() using pipe() with test
---
lib/levelup.js | 5 +++-
lib/util.js | 5 ++++
lib/write-stream.js | 2 +-
test/common.js | 33 ++++++++++++---------
test/copy-test.js | 74 +++++++++++++++++++++++++++++++++++++++++++++++
test/read-stream-test.js | 40 +++++++++++++------------
test/write-stream-test.js | 5 +++-
7 files changed, 129 insertions(+), 35 deletions(-)
diff --git a/lib/levelup.js b/lib/levelup.js
index 4cc9dd6..0d0fc16 100644
--- a/lib/levelup.js
+++ b/lib/levelup.js
@@ -264,4 +264,7 @@ var bridge = require('bindings')('levelup.node')
return database
}
-module.exports.createDatabase = createDatabase
\ No newline at end of file
+module.exports = {
+ createDatabase : createDatabase
+ , copy : require('./util').copy
+}
\ No newline at end of file
diff --git a/lib/util.js b/lib/util.js
index 3bc5594..4653460 100644
--- a/lib/util.js
+++ b/lib/util.js
@@ -8,7 +8,12 @@ var toBuffer = function (data, encoding) {
return encoding == 'binary' ? buffer : buffer.toString(encoding)
}
+ , copy = function (srcdb, dstdb, callback) {
+ srcdb.readStream().pipe(dstdb.writeStream().on('close', callback))
+ }
+
module.exports = {
toBuffer : toBuffer
, toEncoding : toEncoding
+ , copy : copy
}
\ No newline at end of file
diff --git a/lib/write-stream.js b/lib/write-stream.js
index e897913..d8f58cb 100644
--- a/lib/write-stream.js
+++ b/lib/write-stream.js
@@ -88,7 +88,7 @@ WriteStream.prototype._process = function() {
}
}
- if (this._end) {
+ if (this._end && this._status != 'closed') {
this._status = 'closed'
this.writable = false
this.emit('close')
diff --git a/test/common.js b/test/common.js
index f3768af..54e4535 100644
--- a/test/common.js
+++ b/test/common.js
@@ -7,6 +7,7 @@ var ba = require('buster').assertions
, path = require('path')
, levelup = require('../lib/levelup.js')
, child_process = require('child_process')
+ , dbidx = 0
ba.add('isInstanceOf', {
assert: function (actual, expected) {
@@ -30,19 +31,25 @@ ba.add('isUndefined', {
, refuteMessage: '${0} expected not to be undefined'
})
-global.openTestDatabase = function (callback) {
- var db = levelup.createDatabase(
- this.cleanupDirs[0] = '/tmp/levelup_test_db'
- , {
- createIfMissing: true
- , errorIfExists: true
- }
- )
- this.closeableDatabases.push(db)
- db.open(function (err) {
+global.openTestDatabase = function (location, callback) {
+ callback = typeof location == 'function' ? location : callback
+ location = typeof location == 'string' ? location : path.join(__dirname, 'levelup_test_db_' + dbidx++)
+ rimraf(location, function (err) {
refute(err)
- callback(db)
- })
+ this.cleanupDirs.push(location)
+ var db = levelup.createDatabase(
+ location
+ , {
+ createIfMissing: true
+ , errorIfExists: true
+ }
+ )
+ this.closeableDatabases.push(db)
+ db.open(function (err) {
+ refute(err)
+ callback(db)
+ })
+ }.bind(this))
}
global.cleanUp = function (closeableDatabases, cleanupDirs, callback) {
@@ -67,7 +74,7 @@ global.checkBinaryTestData = function (testData, callback) {
var fname = '__tst.dat.' + Math.random()
fs.writeFile(fname, testData, function (err) {
refute(err)
- child_process.exec('which md5sum', function (err, stdout, stderr) {
+ child_process.exec('which md5sum', function (err, stdout) {
child_process.exec((stdout !== '' ? 'md5sum ' : 'md5 -r ') + fname, function (err, stdout, stderr) {
refute(err)
refute(stderr)
diff --git a/test/copy-test.js b/test/copy-test.js
new file mode 100644
index 0000000..3b6b536
--- /dev/null
+++ b/test/copy-test.js
@@ -0,0 +1,74 @@
+/* Copyright (c) 2012 Rod Vagg <@rvagg> */
+
+/*global cleanUp:true, openTestDatabase:true*/
+
+var buster = require('buster')
+ , assert = buster.assert
+ , levelup = require('../lib/levelup.js')
+ , errors = require('../lib/errors.js')
+ , rimraf = require('rimraf')
+ , async = require('async')
+ , fs = require('fs')
+
+buster.testCase('Copy', {
+ 'setUp': function () {
+ this.cleanupDirs = []
+ this.closeableDatabases = []
+ this.openTestDatabase = openTestDatabase.bind(this)
+ }
+
+ , 'tearDown': function (done) {
+ cleanUp(this.closeableDatabases, this.cleanupDirs, done)
+ }
+
+ , 'copy full database': function (done) {
+ var sourceData = []
+
+ for (var i = 0; i < 100; i++) {
+ sourceData.push({
+ type : 'put'
+ , key : i
+ , value : Math.random()
+ })
+ }
+
+ var opensrc = function (callback) {
+ this.openTestDatabase(function (db) {
+ db.batch(sourceData.slice(), function (err) {
+ callback(err, db)
+ })
+ })
+ }.bind(this)
+
+ , opendst = function (callback) {
+ this.openTestDatabase(function (db) {
+ callback(null, db)
+ })
+ }.bind(this)
+
+ , verify = function (dstdb) {
+ async.forEach(
+ sourceData
+ , function (data, callback) {
+ dstdb.get(data.key, function (err, value) {
+ refute(err)
+ assert.equals(value, data.value, 'Destination data #' + data.key + ' has correct value')
+ callback()
+ })
+ }
+ , done
+ )
+ }.bind(this)
+
+ async.parallel(
+ { src: opensrc, dst: opendst }
+ , function (err, dbs) {
+ refute(err)
+ levelup.copy(dbs.src, dbs.dst, function (err) {
+ refute(err)
+ verify(dbs.dst)
+ })
+ }
+ )
+ }
+})
\ No newline at end of file
diff --git a/test/read-stream-test.js b/test/read-stream-test.js
index 99652f8..98e844c 100644
--- a/test/read-stream-test.js
+++ b/test/read-stream-test.js
@@ -9,6 +9,7 @@ var buster = require('buster')
, rimraf = require('rimraf')
, async = require('async')
, fs = require('fs')
+ , path = require('path')
buster.testCase('ReadStream', {
'setUp': function () {
@@ -73,27 +74,28 @@ buster.testCase('ReadStream', {
}
, 'test delayed open': function (done) {
- var execute = function () {
- var db = levelup.createDatabase(
- this.cleanupDirs[0] = '/tmp/levelup_test_db'
- , { createIfMissing: true, errorIfExists: false }
- )
- this.closeableDatabases.push(db)
- db.open(function (err) {
- refute(err)
-
- var rs = db.readStream()
- assert.isFalse(rs.writable)
- assert.isTrue(rs.readable)
- rs.on('ready', this.readySpy)
- rs.on('data' , this.dataSpy)
- rs.on('end' , this.endSpy)
- rs.on('close', this.verify.bind(this, rs, done))
- }.bind(this))
- }.bind(this)
+ var location = path.join(__dirname, 'levelup_test_db_delayed_open')
+ , execute = function () {
+ this.cleanupDirs.push(location)
+ var db = levelup.createDatabase(location
+ , { createIfMissing: true, errorIfExists: false }
+ )
+ this.closeableDatabases.push(db)
+ db.open(function (err) {
+ refute(err)
+
+ var rs = db.readStream()
+ assert.isFalse(rs.writable)
+ assert.isTrue(rs.readable)
+ rs.on('ready', this.readySpy)
+ rs.on('data' , this.dataSpy)
+ rs.on('end' , this.endSpy)
+ rs.on('close', this.verify.bind(this, rs, done))
+ }.bind(this))
+ }.bind(this)
// setup -- open db, write stuff to it, close it again so we can reopen it
- this.openTestDatabase(function (db) {
+ this.openTestDatabase(location, function (db) {
db.batch(this.sourceData.slice(), function (err) {
refute(err)
db.close(function () {
diff --git a/test/write-stream-test.js b/test/write-stream-test.js
index d29a108..7822c6b 100644
--- a/test/write-stream-test.js
+++ b/test/write-stream-test.js
@@ -9,6 +9,7 @@ var buster = require('buster')
, rimraf = require('rimraf')
, async = require('async')
, fs = require('fs')
+ , path = require('path')
buster.testCase('WriteStream', {
'setUp': function () {
@@ -94,8 +95,10 @@ buster.testCase('WriteStream', {
}
, 'test delayed open with maxBufferLength': function (done) {
+ var location = path.join(__dirname, 'levelup_test_db_delayed_open')
+ this.cleanupDirs.push(location)
var db = levelup.createDatabase(
- this.cleanupDirs[0] = '/tmp/levelup_test_db'
+ location
, { createIfMissing: true, errorIfExists: false }
)
, ws = db.writeStream({ maxBufferLength: 1 })
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-leveldown.git
More information about the Pkg-javascript-commits
mailing list