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 #1214 from strongloop/refactor/kvao-flush
Refactor/kvao flush to deleteAll
- Loading branch information
Showing
8 changed files
with
178 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
var async = require('async'); | ||
var debug = require('debug')('loopback:kvao:delete-all'); | ||
var utils = require('../utils'); | ||
|
||
/** | ||
* Delete all keys (and values) associated to the current model. | ||
* | ||
* @options {Object} options Unused ATM, placeholder for future options. | ||
* @callback {Function} callback | ||
* @param {Error} err Error object. | ||
* @promise | ||
* | ||
* @header KVAO.prototype.deleteAll([options, ]cb) | ||
*/ | ||
module.exports = function deleteAll(options, callback) { | ||
if (callback == undefined && typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} else if (!options) { | ||
options = {}; | ||
} | ||
|
||
assert(typeof options === 'object', 'options must be an object'); | ||
|
||
callback = callback || utils.createPromiseCallback(); | ||
|
||
var connector = this.getConnector(); | ||
if (typeof connector.deleteAll === 'function') { | ||
connector.deleteAll(this.modelName, options, callback); | ||
} else if (typeof connector.delete === 'function') { | ||
debug('Falling back to unoptimized key-value pair deletion'); | ||
iterateAndDelete(connector, this.modelName, options, callback); | ||
} else { | ||
var errMsg = 'Connector does not support key-value pair deletion'; | ||
debug(errMsg); | ||
process.nextTick(function() { | ||
var err = new Error(errMsg); | ||
err.statusCode = 501; | ||
callback(err); | ||
}); | ||
} | ||
return callback.promise; | ||
}; | ||
|
||
function iterateAndDelete(connector, modelName, options, callback) { | ||
var iter = connector.iterateKeys(modelName, {}); | ||
var keys = []; | ||
iter.next(onNextKey); | ||
|
||
function onNextKey(err, key) { | ||
if (err) return callback(err); | ||
if (key === undefined) return callback(); | ||
connector.delete(modelName, key, options, onDeleted); | ||
} | ||
|
||
function onDeleted(err) { | ||
if (err) return callback(err); | ||
iter.next(onNextKey); | ||
} | ||
} |
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,45 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
var debug = require('debug')('loopback:kvao:delete'); | ||
var utils = require('../utils'); | ||
|
||
/** | ||
* Delete the key-value pair associated to the given key. | ||
* | ||
* @param {String} key Key to use when searching the database. | ||
* @options {Object} options | ||
* @callback {Function} callback | ||
* @param {Error} err Error object. | ||
* @param {*} result Value associated with the given key. | ||
* @promise | ||
* | ||
* @header KVAO.prototype.delete(key[, options], cb) | ||
*/ | ||
module.exports = function keyValueDelete(key, options, callback) { | ||
if (callback == undefined && typeof options === 'function') { | ||
callback = options; | ||
options = {}; | ||
} else if (!options) { | ||
options = {}; | ||
} | ||
|
||
assert(typeof key === 'string' && key, 'key must be a non-empty string'); | ||
|
||
callback = callback || utils.createPromiseCallback(); | ||
|
||
var connector = this.getConnector(); | ||
if (typeof connector.delete === 'function') { | ||
connector.delete(this.modelName, key, options, callback); | ||
} else { | ||
var errMsg = 'Connector does not support key-value pair deletion'; | ||
debug(errMsg); | ||
process.nextTick(function() { | ||
var err = new Error(errMsg); | ||
err.statusCode = 501; | ||
callback(err); | ||
}); | ||
} | ||
|
||
return callback.promise; | ||
}; |
This file was deleted.
Oops, something went wrong.
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,36 @@ | ||
'use strict'; | ||
|
||
const bdd = require('../helpers/bdd-if'); | ||
const helpers = require('./_helpers'); | ||
const should = require('should'); | ||
|
||
module.exports = function(dataSourceFactory, connectorCapabilities) { | ||
var supportsDeleteAll = 'deleteAll' in dataSourceFactory().connector; | ||
|
||
bdd.describeIf(supportsDeleteAll, 'deleteAll', function() { | ||
let CacheItem; | ||
beforeEach(function unpackContext() { | ||
CacheItem = helpers.givenCacheItem(dataSourceFactory); | ||
}); | ||
|
||
it('removes all key-value pairs for the given model', function() { | ||
return helpers.givenKeys(CacheItem, ['key1', 'key2']) | ||
.then(() => CacheItem.deleteAll()) | ||
.then(() => CacheItem.keys()) | ||
.then((keys) => { | ||
should(keys).eql([]); | ||
}); | ||
}); | ||
|
||
it('does not remove data from other existing models', function() { | ||
var AnotherModel = dataSourceFactory().createModel('AnotherModel'); | ||
return helpers.givenKeys(CacheItem, ['key1', 'key2']) | ||
.then(() => helpers.givenKeys(AnotherModel, ['key3', 'key4'])) | ||
.then(() => CacheItem.deleteAll()) | ||
.then(() => AnotherModel.keys()) | ||
.then((keys) => { | ||
should(keys).eql(['key3', 'key4']); | ||
}); | ||
}); | ||
}); | ||
}; |
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