This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from gordonwritescode/master
[WIP] v0.6.14
- Loading branch information
Showing
13 changed files
with
478 additions
and
30 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
var StorageAdapter = require('./adapter'); | ||
var inherits = require('util').inherits; | ||
var EventEmitter = require('events').EventEmitter; | ||
var StorageItem = require('./item'); | ||
|
||
/** | ||
* Migrates data stored with one {@link StorageAdapter} to another | ||
* @constructor | ||
* @param {StorageAdapter} source - The source adapter | ||
* @param {StorageAdapter} target - The migration destination | ||
*/ | ||
function StorageMigration(source, target) { | ||
if (!(this instanceof StorageMigration)) { | ||
return new StorageMigration(source, target); | ||
} | ||
|
||
assert(source instanceof StorageAdapter, 'Invalid storage adapter supplied'); | ||
assert(target instanceof StorageAdapter, 'Invalid storage adapter supplied'); | ||
|
||
this.source = source; | ||
this.target = target; | ||
this.readyState = StorageMigration.STOPPED; | ||
|
||
EventEmitter.call(this); | ||
} | ||
|
||
inherits(StorageMigration, EventEmitter); | ||
|
||
StorageMigration.STOPPED = 0; | ||
StorageMigration.STARTED = 1; | ||
|
||
/** | ||
* Starts the migration process | ||
*/ | ||
StorageMigration.prototype.start = function() { | ||
assert( | ||
this.readyState === StorageMigration.STOPPED, | ||
'Migration has already started' | ||
); | ||
|
||
this.readyState = StorageMigration.STARTED; | ||
this._sourceStream = this.source.createReadStream(); | ||
|
||
this._sourceStream.on('data', this._handleSourceObject.bind(this)); | ||
this._sourceStream.on('end', this._handleSourceFinished.bind(this)); | ||
this._sourceStream.on('error', this._handleSourceError.bind(this)); | ||
|
||
return this._sourceStream; | ||
}; | ||
|
||
/** | ||
* Stops the migration process | ||
*/ | ||
StorageMigration.prototype.stop = function() { | ||
assert( | ||
this.readyState === StorageMigration.STARTED, | ||
'Migration has already stopped' | ||
); | ||
|
||
this._sourceStream.removeAllListeners(); | ||
|
||
this.readyState = StorageMigration.STOPPED; | ||
this._sourceStream = null; | ||
}; | ||
|
||
/** | ||
* Handles a data event from the source read stream and inserts it into the | ||
* the target adapter | ||
* @private | ||
* @param {StorageItem} sourceItem - Storage item from the source read stream | ||
*/ | ||
StorageMigration.prototype._handleSourceObject = function(sourceItem) { | ||
var self = this; | ||
|
||
self._sourceStream.pause(); | ||
|
||
self.target.put(StorageItem(sourceItem), function(err) { | ||
if (err) { | ||
return self.emit('error', err); | ||
} | ||
|
||
self.target.get(sourceItem.hash, function(err, targetItem) { | ||
if (err) { | ||
return self.emit('error', err); | ||
} | ||
|
||
if (typeof sourceItem.shard.read === 'function') { | ||
return sourceItem.shard.pipe(targetItem.shard) | ||
.on('error', self._handleSourceError.bind(this)) | ||
.on('finish', self._sourceStream.resume.bind(self._sourceStream)); | ||
} | ||
|
||
self._sourceStream.resume(); | ||
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* Handles the completion of the source stream read | ||
* @private | ||
*/ | ||
StorageMigration.prototype._handleSourceFinished = function() { | ||
this.readyState = StorageMigration.STOPPED; | ||
this._sourceStream = null; | ||
|
||
this.emit('finish'); | ||
}; | ||
|
||
/** | ||
* Handles errors received from the underyling source stream | ||
* @private | ||
* @param {Error} error | ||
*/ | ||
StorageMigration.prototype._handleSourceError = function(err) { | ||
this.readyState = StorageMigration.STOPPED; | ||
this._sourceStream = null; | ||
|
||
this.emit('error', err); | ||
}; | ||
|
||
module.exports = StorageMigration; |
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
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
Oops, something went wrong.