Skip to content

Instantly share code, notes, and snippets.

@gravitystorm
Created March 21, 2014 17:31
Show Gist options
  • Select an option

  • Save gravitystorm/9691382 to your computer and use it in GitHub Desktop.

Select an option

Save gravitystorm/9691382 to your computer and use it in GitHub Desktop.
mbtiles stopWriting not reentrant?
#!/usr/bin/env node
var tilelive = require('tilelive');
var Mbtiles = require('mbtiles');
var Http = require('tilelive-http');
tilelive.protocols['mbtiles:'] = Mbtiles;
tilelive.protocols['http:'] = Http;
var source = new Http("http://tile.openstreetmap.org/{z}/{x}/{y}.png", function(err) { if (err) throw err} );
var sink = new Mbtiles('test.mbtiles?filetype=png', function(err) { if (err) throw err });
// mbtiles is asynchronous at loading, so hang fast for a second
setTimeout(function() {
// This is the basics of copying a tile from source to sink
// sink.startWriting(function(err) {
// if (err) throw err;
// source.getTile(0,0,0, function(err, image, headers) {
// if (err) throw err;
// sink.putTile(0,0,0, image, function(err) {
// if (err) throw err;
// sink.stopWriting(function(err) {
// if (err) throw err;
// });
// });
// });
// });
// starting/stopping writing in parallel doesn't work though, e.g. when one request is waiting for data and another completes first.
sink.startWriting(function(err) {
if (err) throw err;
source.getTile(0,0,0, function(err, image, headers) {
if (err) throw err;
// ensure this has been delayed by a second.
setTimeout(function() {
sink.putTile(0,0,0, image, function(err) {
if (err) throw err;
sink.stopWriting(function(err) {
if (err) throw err;
});
});
}, 1000);
// meanwhile, work on another tile
sink.startWriting(function(err) {
if (err) throw err;
source.getTile(2,2,2, function(err, image, headers) {
if (err) throw err;
sink.putTile(2,2,2, image, function (err) {
if (err) throw err;
sink.stopWriting(function(err) {
if (err) throw err;
});
});
});
});
});
});
}, 1000);
{
"name": "mbtiles-reentrant",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"dependencies": {
"tilelive": "4.5.x",
"tilelive-http": "0.1.2",
"mbtiles": "0.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment