diff --git a/lib/mbtiles.js b/lib/mbtiles.js index 72f1286..9a127a1 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -549,15 +549,12 @@ MBTiles.prototype.putTile = function(z, x, y, data, callback) { // Tilelive may pass us a data.key. If not, generate an md5 // from the image buffer data. - var id; - if (data.key) - id = String(data.key); - else - id = crypto.createHash('md5').update(data).digest('hex'); - if (!this._tileCache[id]) { - // This corresponds to the images table. - this._tileCache[id] = data; - } + var id = data.key + ? String(data.key) + : crypto.createHash('md5').update(data).digest('hex'); + + // This corresponds to the images table. + if (!this._tileCache[id]) this._tileCache[id] = data; // This corresponds to the map table. var coords = hash(z, x, y); @@ -611,32 +608,38 @@ MBTiles.prototype.putGrid = function(z, x, y, data, callback) { // Preprocess grid data. var json = JSON.stringify({ grid: data.grid, keys: data.keys }); - var id = data.id || crypto.createHash('md5').update(json).digest('hex'); - if (!this._gridCache[id]) { - // This corresponds to the grid_utfgrid table. - var that = this; - Step(function() { - zlib.deflate(new Buffer(json, 'utf8'),this); - }, function(err,buffer) { - if (err) return callback(err); - that._gridCache[id] = buffer; - // This corresponds to the grid_key table. - that._keyCache[id] = Object.keys(data.data || {}); - // This corresponds to the keymap table. - _(that._dataCache).extend(data.data || {}); - } - ); - } + // Tilelive may pass us a data.key. If not, generate an md5 + // from the grid data. + var id = data.key + ? String(data.key) + : crypto.createHash('md5').update(json).digest('hex'); // This corresponds to the map table. var coords = hash(z, x, y); if (!this._mapCache[coords]) this._mapCache[coords] = { z: z, x: x, y: y }; this._mapCache[coords].grid_id = id; - // Only commit when we can insert at least batchSize rows. - if (++this._pending >= this._batchSize) return this._commit(callback); - else return callback(null); + var mbtiles = this; + Step(function() { + if (mbtiles._gridCache[id]) return this(); + else zlib.deflate(new Buffer(json, 'utf8'), this); + }, function(err, buffer) { + if (err) throw err; + if (mbtiles._gridCache[id]) return this(); + // grid_utfgrid table. + mbtiles._gridCache[id] = buffer; + // grid_key table. + mbtiles._keyCache[id] = Object.keys(data.data || {}); + // keymap table. + _(mbtiles._dataCache).extend(data.data || {}); + this(); + }, function(err) { + if (err) return callback(err); + // Only commit when we can insert at least batchSize rows. + if (++this._pending >= this._batchSize) return this._commit(callback); + else return callback(null); + }); }; // Inserts a duplicate grid into the MBTiles store. Scheme is XYZ.