Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

updating truncate to use batch write item

  • Loading branch information...
commit 44236b8ae9fc8adc2aa304d234902b7f2759ff5c 1 parent 38f9de0
Ryan Fitzgerald ryanfitz authored
34 lib/dynode/client.js
@@ -185,7 +185,7 @@ Client.prototype.batchWriteItem = function(options, cb) {
185 185
186 186 return memo;
187 187 }, {});
188   -
  188 +
189 189 this._request("BatchWriteItem", {RequestItems: request}, cb);
190 190 };
191 191
@@ -206,24 +206,28 @@ Client.prototype.truncate = function(tableName, options, cb) {
206 206 rangeKey;
207 207
208 208 if(table.KeySchema.RangeKeyElement) rangeKey = table.KeySchema.RangeKeyElement.AttributeName;
209   -
210   - var deleteQueue = async.queue(function (key, callback) {
211   - self.deleteItem(tableName, key, callback);
212   - }, throughput);
213   -
214   - deleteQueue.drain = function() {
215   - return cb();
216   - };
217 209
218 210 self.scan(tableName, function (err, items, stats) {
219 211 if(!items || items.length === 0) return cb();
220 212
221   - items.forEach(function(item) {
222   - var key = {hash: item[hashKey]};
223   - if(rangeKey) key.range = item[rangeKey];
224   -
225   - deleteQueue.push(key, function (err) {});
226   - });
  213 + var batchWrites = [];
  214 + var i,j,chunk = 25;
  215 +
  216 + for (i=0,j=items.length; i<j; i+=chunk) {
  217 + var chunks = items.slice(i,i+chunk);
  218 +
  219 + var writes = {};
  220 + writes[tableName] = chunks.map(function(item){
  221 + var key = {hash: item[hashKey]};
  222 + if(rangeKey) key.range = item[rangeKey];
  223 +
  224 + return {del : key};
  225 + });
  226 +
  227 + batchWrites.push(async.apply(self.batchWriteItem.bind(self), writes));
  228 + }
  229 +
  230 + async.parallel(batchWrites, cb);
227 231 });
228 232 });
229 233 };
5 test/integration/dynode-test.js
@@ -201,13 +201,12 @@ describe('Dynode Integration Tests', function() {
201 201 this.timeout(0);
202 202
203 203 dynode.truncate(DynamoDB.TestTable, function(err){
204   -
205   - dynode.getItem(DynamoDB.TestTable, 'prod-1', function(err, item){
  204 +
  205 + dynode.getItem(DynamoDB.TestTable, 'prod-1', {ConsistentRead : true}, function(err, item){
206 206 should.not.exist(err);
207 207 should.not.exist(item);
208 208 done();
209 209 });
210   -
211 210 });
212 211 });
213 212 });
16 test/test-helper.js
@@ -14,7 +14,21 @@ DB.start = function(callback) {
14 14 DB.createProducts = function(products, callback) {
15 15 if(!Array.isArray(products)) products = [products];
16 16
17   - async.forEach(products, DB.createProduct, callback);
  17 + var batchWrites = [];
  18 + var i,j,chunk = 25;
  19 +
  20 + for (i=0,j=products.length; i<j; i+=chunk) {
  21 + var chunks = products.slice(i,i+chunk);
  22 +
  23 + var writes = {};
  24 + writes[DB.TestTable] = chunks.map(function(p){
  25 + return {put : p};
  26 + });
  27 +
  28 + batchWrites.push(async.apply(client.batchWriteItem.bind(client), writes));
  29 + }
  30 +
  31 + async.parallel(batchWrites, callback);
18 32 };
19 33
20 34 DB.createProduct = function(product, cb) {

0 comments on commit 44236b8

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