Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support for merged options, emitters with limits

  • Loading branch information...
commit 4009f8ad5d82f34da2b0d4f1fb9e7034264f78aa 1 parent d97090a
@pofallon authored
View
73 lib/blob.js
@@ -19,22 +19,22 @@ function Container (b,options) {
Container.listContainers = function(prefix, options, callback) {
- var params = {};
- var containerList = [];
var containerCounter = 0;
- var emitter = new EventEmitter();
-
var processResults = function(err, theseContainers, continuation) {
if (err) {
emitter.emit('error', err);
} else {
theseContainers.forEach(function(container) {
- emitter.emit('data', container.name);
- containerCounter++;
+ if (!options.limit || (containerCounter < options.limit)) {
+ emitter.emit('data', container.name);
+ containerCounter++;
+ }
});
if (continuation && continuation.hasNextPage()) {
- fetchContainers(continuation);
+ if (!options.limit || (containerCounter < options.limit)) {
+ fetchContainers(continuation);
+ }
} else {
emitter.emit('end', containerCounter);
}
@@ -46,33 +46,22 @@ Container.listContainers = function(prefix, options, callback) {
if (more) {
more.getNextPage(processResults);
} else {
- if (params.prefix) {
- params.options.blobService.listContainers({prefix:params.prefix},processResults);
+ if (prefix) {
+ options.blobService.listContainers({prefix:prefix},processResults);
} else {
- params.options.blobService.listContainers(processResults);
+ options.blobService.listContainers(processResults);
}
}
};
-
- skyutil.prepFetch({
- prefix: prefix,
- options: options,
- callback: callback,
- emitter: emitter
- }, function(p) {
- params = p;
- fetchContainers();
- }
- );
-
+
+ var emitter = skyutil.getEmitter(callback);
+ fetchContainers();
return(emitter);
};
Container.createContainer = function(name,options,callback) {
- callback = skyutil.safeCallback(callback);
-
options.blobService.createContainerIfNotExists(name, function(err) {
if (err) {
callback(err);
@@ -85,8 +74,6 @@ Container.createContainer = function(name,options,callback) {
Container.removeContainer = function(name,options,callback) {
- callback = skyutil.safeCallback(callback);
-
options.blobService.deleteContainer(name, function(err) {
if (err) {
callback(err);
@@ -97,25 +84,26 @@ Container.removeContainer = function(name,options,callback) {
};
-Container.prototype.list = function(prefix,callback) {
+Container.prototype.list = function(prefix,options,callback) {
var params = {};
- var blobList = [];
var that = this;
var blobCounter = 0;
- var emitter = new EventEmitter();
-
var processResults = function(err, theseBlobs, continuation) {
if (err) {
emitter.emit('error',err);
} else {
theseBlobs.forEach(function(blob) {
- emitter.emit('data', blob.name);
- blobCounter++;
+ if (!params.options.limit || (blobCounter < params.options.limit)) {
+ emitter.emit('data', blob.name);
+ blobCounter++;
+ }
});
if (continuation && continuation.hasNextPage()) {
- fetchBlobs(continuation);
+ if (!params.options.limit || (blobCounter < params.options.limit)) {
+ fetchBlobs(continuation);
+ }
} else {
emitter.emit('end',blobCounter);
}
@@ -136,16 +124,17 @@ Container.prototype.list = function(prefix,callback) {
};
- skyutil.prepFetch({
+ var emitter;
+
+ skyutil.parseArgs({
prefix: prefix,
- options: null,
- callback: callback,
- emitter: emitter
- }, function(p) {
- params = p;
- fetchBlobs();
- }
- );
+ options: options,
+ callback: callback
+ }, function(p) {
+ params = p;
+ emitter = skyutil.getEmitter(params.callback);
+ fetchBlobs();
+ });
return(emitter);
View
37 lib/queue.js
@@ -17,22 +17,22 @@ util.inherits(Queue,EventEmitter);
Queue.listQueues = function(prefix, options, callback) {
- var params = {};
- var queueList = [];
var queueCounter = 0;
- var emitter = new EventEmitter();
-
var processResults = function(err, theseQueues, continuation) {
if (err) {
emitter.emit('error', err);
} else {
theseQueues.forEach(function(queue) {
- emitter.emit('data', queue.name);
- queueCounter++;
+ if (!options.limit || (queueCounter < options.limit)) {
+ emitter.emit('data', queue.name);
+ queueCounter++;
+ }
});
if (continuation && continuation.hasNextPage()) {
- fetchQueues(continuation);
+ if (!options.limit || (queueCounter < options.limit)) {
+ fetchQueues(continuation);
+ }
} else {
emitter.emit('end',queueCounter);
}
@@ -43,33 +43,22 @@ Queue.listQueues = function(prefix, options, callback) {
if (more) {
more.getNextPage(processResults);
} else {
- if (params.prefix) {
- params.options.queueService.listQueues({prefix:params.prefix},processResults);
+ if (prefix) {
+ options.queueService.listQueues({prefix:prefix},processResults);
} else {
- params.options.queueService.listQueues(processResults);
+ options.queueService.listQueues(processResults);
}
}
};
- skyutil.prepFetch({
- prefix: prefix,
- options: options,
- callback: callback,
- emitter: emitter
- }, function(p) {
- params = p;
- fetchQueues();
- }
- );
-
+ var emitter = skyutil.getEmitter(callback);
+ fetchQueues();
return(emitter);
};
Queue.createQueue = function(name,options,callback) {
- callback = skyutil.safeCallback(callback);
-
options.queueService.createQueueIfNotExists(name, function(err) {
if (err) {
callback(err);
@@ -82,8 +71,6 @@ Queue.createQueue = function(name,options,callback) {
Queue.removeQueue = function(name,options,callback) {
- callback = skyutil.safeCallback(callback);
-
options.queueService.deleteQueue(name, function(err) {
if (err) {
callback(err);
View
125 lib/storage.js
@@ -10,6 +10,8 @@ var Queue = require("./queue");
var azure = require('azure');
+var skyutil = require('./util');
+
module.exports = function(options) {
function Storage () {
@@ -23,25 +25,53 @@ module.exports = function(options) {
return (new Table(t,options));
};
- Storage.listTables = function(prefix,callback) {
+ Storage.listTables = function(prefix,theseOpts,callback) {
if (!options.tableService) {
options.tableService = azure.createTableService(options.account, options.key);
}
- return Table.listTables(prefix,options,callback);
+
+ var results;
+
+ skyutil.parseArgs({
+ prefix: prefix,
+ options: theseOpts,
+ callback: callback,
+ mergeOpts: options
+ }, function(params) {
+ results = Table.listTables(params.prefix,params.options,params.callback);
+ });
+
+ return results;
};
- Storage.createTable = function(t,callback) {
+ Storage.createTable = function(t,theseOpts,callback) {
if (!options.tableService) {
options.tableService = azure.createTableService(options.account, options.key);
}
- Table.createTable(t,options,callback);
+ skyutil.parseArgs({
+ options: theseOpts,
+ callback: callback,
+ mergeOpts: options,
+ safeCallback: true
+ }, function(params) {
+ Table.createTable(t,params.options,params.callback);
+ })
+
};
- Storage.removeTable = function(t,callback) {
+ Storage.removeTable = function(t,theseOpts,callback) {
if (!options.tableService) {
options.tableService = azure.createTableService(options.account, options.key);
}
- Table.removeTable(t,options,callback);
+ skyutil.parseArgs({
+ options: theseOpts,
+ callback: callback,
+ mergeOpts: options,
+ safeCallback: true
+ }, function(params) {
+ Table.removeTable(t,params.options,params.callback);
+ });
+
};
Storage.container = function(b) {
@@ -51,25 +81,53 @@ module.exports = function(options) {
return(new Container(b,options));
};
- Storage.listContainers = function(prefix,callback) {
+ Storage.listContainers = function(prefix,theseOpts,callback) {
if (!options.blobService) {
options.blobService = azure.createBlobService(options.account, options.key);
}
- return Container.listContainers(prefix,options,callback);
+
+ var result;
+
+ skyutil.parseArgs({
+ prefix: prefix,
+ options: theseOpts,
+ callback: callback,
+ mergeOpts: options
+ }, function(params) {
+ result = Container.listContainers(params.prefix,params.options,params.callback);
+ });
+
+ return result;
};
- Storage.createContainer = function(b,callback) {
+ Storage.createContainer = function(b,theseOpts,callback) {
if (!options.blobService) {
options.blobService = azure.createBlobService(options.account, options.key);
}
- Container.createContainer(b,options,callback);
+ skyutil.parseArgs({
+ options: theseOpts,
+ callback: callback,
+ mergeOpts: options,
+ safeCallback: true
+ }, function(params) {
+ Container.createContainer(b,params.options,params.callback);
+ });
+
};
- Storage.removeContainer = function(b,callback) {
+ Storage.removeContainer = function(b,theseOpts, callback) {
if (!options.blobService) {
options.blobService = azure.createBlobService(options.account, options.key);
}
- Container.removeContainer(b,options,callback);
+ skyutil.parseArgs({
+ options: theseOpts,
+ callback: callback,
+ mergeOpts: options,
+ safeCallback: true
+ }, function(params) {
+ Container.removeContainer(b,params.options,params.callback);
+ });
+
};
Storage.queue = function(q) {
@@ -79,25 +137,56 @@ module.exports = function(options) {
return(new Queue(q,options));
};
- Storage.listQueues = function(prefix,callback) {
+ Storage.listQueues = function(prefix,theseOpts,callback) {
if (!options.queueService) {
options.queueService = azure.createQueueService(options.account, options.key);
}
- return Queue.listQueues(prefix,options,callback);
+
+ var result;
+
+ skyutil.parseArgs({
+ prefix: prefix,
+ options: theseOpts,
+ callback: callback,
+ mergeOpts: options
+ }, function(params) {
+ result = Queue.listQueues(params.prefix,params.options,params.callback);
+ });
+
+ return result;
+
};
- Storage.createQueue = function(q,callback) {
+ Storage.createQueue = function(q,theseOpts,callback) {
if (!options.queueService) {
options.queueService = azure.createQueueService(options.account, options.key);
}
- Queue.createQueue(q,options,callback);
+
+ skyutil.parseArgs({
+ options: theseOpts,
+ callback: callback,
+ mergeOpts: options,
+ safeCallback: true
+ }, function(params) {
+ Queue.createQueue(q,params.options,params.callback);
+ });
+
};
- Storage.removeQueue = function(q,callback) {
+ Storage.removeQueue = function(q,theseOpts,callback) {
if (!options.queueService) {
options.queueService = azure.createQueueService(options.account, options.key);
}
- Queue.removeQueue(q,options,callback);
+
+ skyutil.parseArgs({
+ options: theseOpts,
+ callback: callback,
+ mergeOpts: options,
+ safeCallback: true
+ }, function(params) {
+ Queue.removeQueue(q,params.options,params.callback);
+ })
+
};
return Storage;
View
58 lib/table.js
@@ -18,29 +18,29 @@ function Table (t,options) {
Table.listTables = function(prefix,options,callback) {
- var params = {};
- var tableList = [];
var tableCounter = 0;
- var emitter = new EventEmitter();
-
var processResults = function(err, theseTables, continuation) {
if (err) {
emitter.emit('error',err);
} else {
theseTables.forEach(function(table) {
- if (params.prefix) {
- if (table.TableName.indexOf(params.prefix) === 0) {
+ if (!options.limit || (tableCounter < options.limit)) {
+ if (prefix) {
+ if (table.TableName.indexOf(prefix) === 0) {
+ emitter.emit('data',table.TableName);
+ tableCounter++;
+ }
+ } else {
emitter.emit('data',table.TableName);
tableCounter++;
}
- } else {
- emitter.emit('data',table.TableName);
- tableCounter++;
}
});
if (continuation && continuation.hasNextPage()) {
- fetchTables(continuation);
+ if (!options.limit || (tableCounter < options.limit)) {
+ fetchTables(continuation);
+ }
} else {
emitter.emit('end', tableCounter);
}
@@ -57,25 +57,14 @@ Table.listTables = function(prefix,options,callback) {
};
- skyutil.prepFetch({
- prefix: prefix,
- options: options,
- callback: callback,
- emitter: emitter
- }, function(p) {
- params = p;
- fetchTables();
- }
- );
-
+ var emitter = skyutil.getEmitter(callback);
+ fetchTables();
return(emitter);
};
Table.createTable = function(name,options,callback) {
- callback = skyutil.safeCallback(callback);
-
options.tableService.createTableIfNotExists(name, function(err) {
if (err) {
callback(err);
@@ -88,8 +77,6 @@ Table.createTable = function(name,options,callback) {
Table.removeTable = function(name,options,callback) {
- callback = skyutil.safeCallback(callback);
-
options.tableService.deleteTable(name, function(err) {
if (err) {
callback(err);
@@ -229,16 +216,17 @@ Table.prototype.or = function() {
return this;
}
+Table.prototype.top = function() {
+ this._query.top.apply(this._query, arguments);
+ return this;
+}
+
Table.prototype.rows = function(callback) {
var that = this;
- var params = {};
- var rowList = [];
var rowCount = 0;
- var emitter = new EventEmitter();
-
var processResults = function(err, theseRows, continuation) {
if (err) {
emitter.emit('error',err);
@@ -269,16 +257,8 @@ Table.prototype.rows = function(callback) {
};
- skyutil.prepFetch({
- prefix: null,
- options: null,
- callback: callback,
- emitter: emitter
- }, function(p) {
- params = p;
- fetchRows();
- }
- );
+ var emitter = skyutil.getEmitter(callback);
+ fetchRows();
return(emitter);
View
52 lib/util.js
@@ -5,6 +5,7 @@
*/
var EventEmitter = require('events').EventEmitter;
+var u_ = require('underscore');
var safeCallback = function(cb) {
if ((typeof cb) === 'function') {
@@ -14,12 +15,10 @@ var safeCallback = function(cb) {
}
}
-var prepFetch = function(args, callback) {
- // prefix, options, callback, doFetch
+var parseArgs = function(args, callback) {
var params = {};
- var theList = [];
-
+
[args.prefix,args.options,args.callback].forEach(function(elem, idx, a) {
switch (typeof elem) {
case "string":
@@ -34,18 +33,18 @@ var prepFetch = function(args, callback) {
}
if ((idx+1) === a.length) {
- // If we have a callback, trap emissions ourselves
- // and pass them back all at once in the callback
- if (params.callback) {
- args.emitter.on('data', function(item) {
- theList.push(item);
- });
- args.emitter.on('end', function() {
- params.callback(null,theList);
- });
- args.emitter.on('error', function(err) {
- params.callback(err);
- });
+ if (!params.options) {
+ params.options = {};
+ };
+
+ // Merge options if we have 'em
+ if (args.mergeOpts) {
+ u_.extend(params.options,args.mergeOpts)
+ }
+
+ // Provide a safe callback if required
+ if (!params.callback && args.safeCallback) {
+ params.callback = function() {};
}
callback(params);
@@ -55,5 +54,24 @@ var prepFetch = function(args, callback) {
}
-exports.prepFetch = prepFetch;
+var getEmitter = function(cb) {
+ var emitter = new EventEmitter();
+ var theList = [];
+
+ if (cb) {
+ emitter.on('data', function(item) {
+ theList.push(item);
+ });
+ emitter.on('end', function() {
+ cb(null, theList);
+ });
+ emitter.on('error', function(err) {
+ cb(err);
+ });
+ }
+ return emitter;
+}
+
+exports.parseArgs = parseArgs;
+exports.getEmitter = getEmitter;
exports.safeCallback = safeCallback;
View
19 test/storage-blob.js
@@ -77,6 +77,15 @@ module.exports = testCase({
});
},
+ getContainersWithLimit: function (test) {
+ storage.listContainers({limit:1}, function(err, containers) {
+ test.equals(err,null);
+ test.notEqual(containers,null);
+ test.strictEqual(containers.length,1);
+ test.done();
+ });
+ },
+
blobPut: function (test) {
var c = storage.container(this.containerName);
var memStream = new MemoryStream();
@@ -215,6 +224,16 @@ module.exports = testCase({
},
+ listBlobsWithLimit: function(test) {
+ var c = storage.container(this.containerName);
+ c.list({limit:1}, function(err,blobs) {
+ test.equals(err,null);
+ test.notEqual(blobs,null);
+ test.strictEqual(blobs.length,1);
+ test.done();
+ });
+ },
+
deleteBlob: function(test) {
var c = storage.container(this.containerName);
c.del('blob2.txt', function(err) {
View
11 test/storage-queue.js
@@ -75,6 +75,17 @@ module.exports = testCase({
});
},
+ getQueuesWithLimit: function(test) {
+ storage.listQueues({limit:1},function(err,queues) {
+ test.equals(err,null);
+ test.notEqual(queues,null);
+ if (queues) {
+ test.strictEqual(queues.length,1);
+ }
+ test.done();
+ });
+ },
+
queuePutMessage: function (test) {
var queue = storage.queue(this.queueName);
queue.put('Queue Test Message', function(err) {
View
19 test/storage-table.js
@@ -75,6 +75,15 @@ module.exports = testCase({
});
},
+ getTablesWithLimit: function(test) {
+ storage.listTables({limit:1}, function(err, tables) {
+ test.equals(err,null);
+ test.notEqual(tables,null);
+ test.strictEqual(tables.length,1);
+ test.done();
+ });
+ },
+
tableInsertRow: function (test) {
var t = storage.table(this.tableName);
t.insert(this.partitionKey,this.rowKey,{'one':'uno', 'two': 2, 'three': true, 'four': new Date('2010-12-23T23:12:11.234Z') }, function(err) {
@@ -217,6 +226,16 @@ module.exports = testCase({
});
},
+ /* tableTopRows: function(test) {
+ var t = storage.table(this.tableName);
+ t.top(2).rows(function(err,rows) {
+ test.equals(err,null);
+ test.notEqual(rows,null);
+ test.strictEqual(rows.length,2);
+ test.done();
+ });
+ }, */
+
tableDeleteRow: function (test) {
var t = storage.table(this.tableName);
t.del(this.partitionKey,this.rowKey,function(err) {
Please sign in to comment.
Something went wrong with that request. Please try again.