Revert "cache file access"
This reverts commit fde3538493.
Conflicts:
lib/directoryindex.js
This commit is contained in:
@@ -1,66 +0,0 @@
|
|||||||
var _ = require('underscore');
|
|
||||||
var fs = require('fs');
|
|
||||||
var path = require('path');
|
|
||||||
var util = require('util');
|
|
||||||
var EventEmitter = require('events').EventEmitter;
|
|
||||||
|
|
||||||
module.exports = DirectoryIndex;
|
|
||||||
util.inherits(DirectoryIndex, EventEmitter)
|
|
||||||
function DirectoryIndex(dir) {
|
|
||||||
this.filepath = dir;
|
|
||||||
|
|
||||||
// Initialize and continually update the directory index.
|
|
||||||
this.update = _(this.update).bind(this);
|
|
||||||
process.nextTick(this.update)
|
|
||||||
fs.watchFile(dir, this.update);
|
|
||||||
}
|
|
||||||
|
|
||||||
DirectoryIndex.prototype.getList = function(callback) {
|
|
||||||
if (!this.list) this.once('updated', done);
|
|
||||||
else done.call(this);
|
|
||||||
|
|
||||||
function done() {
|
|
||||||
if (this.err) callback(this.err);
|
|
||||||
else callback(null, this.list);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
DirectoryIndex.prototype.getID = function(id, callback) {
|
|
||||||
if (!this.list) this.once('updated', done);
|
|
||||||
else done.call(this);
|
|
||||||
|
|
||||||
function done() {
|
|
||||||
if (this.err) callback(this.err);
|
|
||||||
else if (!this.list[id]) callback(new Error('Tileset not found'));
|
|
||||||
else callback(null, this.list[id]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DirectoryIndex.prototype.update = function() {
|
|
||||||
var index = this;
|
|
||||||
fs.readdir(index.filepath, function(err, files) {
|
|
||||||
index.err = err;
|
|
||||||
index.list = {};
|
|
||||||
if (!err) for (var i = 0; i < files.length; i++) {
|
|
||||||
var name = files[i].match(/^([\w-]+)\.mbtiles$/);
|
|
||||||
if (name) {
|
|
||||||
index.list[name[1]] = 'mbtiles://' + path.join(index.filepath, name[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
index.emit('updated');
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var cache = DirectoryIndex.cache = {};
|
|
||||||
DirectoryIndex.create = function(filepath) {
|
|
||||||
if (!cache[filepath]) {
|
|
||||||
var oldpath = filepath;
|
|
||||||
filepath = path.resolve(filepath);
|
|
||||||
if (!cache[filepath]) {
|
|
||||||
cache[filepath] = new DirectoryIndex(filepath);
|
|
||||||
}
|
|
||||||
// Avoid path.resolve() calls.
|
|
||||||
cache[oldpath] = cache[filepath];
|
|
||||||
}
|
|
||||||
return cache[filepath];
|
|
||||||
};
|
|
||||||
+15
-5
@@ -10,9 +10,6 @@ var _ = require('underscore'),
|
|||||||
sm = new (require('sphericalmercator')),
|
sm = new (require('sphericalmercator')),
|
||||||
sqlite3 = require('sqlite3');
|
sqlite3 = require('sqlite3');
|
||||||
|
|
||||||
var DirectoryIndex = require('./directoryindex');
|
|
||||||
|
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'test') sqlite3.verbose();
|
if (process.env.NODE_ENV === 'test') sqlite3.verbose();
|
||||||
|
|
||||||
function noop(err) {
|
function noop(err) {
|
||||||
@@ -112,13 +109,26 @@ MBTiles.registerProtocols = function(tilelive) {
|
|||||||
|
|
||||||
// Finds all mbtiles file in the filepath and returns their tilesource URI.
|
// Finds all mbtiles file in the filepath and returns their tilesource URI.
|
||||||
MBTiles.list = function(filepath, callback) {
|
MBTiles.list = function(filepath, callback) {
|
||||||
DirectoryIndex.create(filepath).getList(callback);
|
filepath = path.resolve(filepath);
|
||||||
|
fs.readdir(filepath, function(err, files) {
|
||||||
|
if (err) return callback(err);
|
||||||
|
for (var result = {}, i = 0; i < files.length; i++) {
|
||||||
|
var name = files[i].match(/^([\w-]+)\.mbtiles$/);
|
||||||
|
if (name) result[name[1]] = 'mbtiles://' + path.join(filepath, name[0]);
|
||||||
|
}
|
||||||
|
return callback(null, result);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Finds an mbtiles file with the given ID in the filepath and returns a
|
// Finds an mbtiles file with the given ID in the filepath and returns a
|
||||||
// tilesource URI.
|
// tilesource URI.
|
||||||
MBTiles.findID = function(filepath, id, callback) {
|
MBTiles.findID = function(filepath, id, callback) {
|
||||||
DirectoryIndex.create(filepath).getID(id, callback);
|
filepath = path.resolve(filepath);
|
||||||
|
var file = path.join(filepath, id + '.mbtiles');
|
||||||
|
fs.stat(file, function(err, stats) {
|
||||||
|
if (err) return callback(err);
|
||||||
|
else return callback(null, 'mbtiles://' + file);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Retrieve the schema of the current mbtiles database and inform the caller of
|
// Retrieve the schema of the current mbtiles database and inform the caller of
|
||||||
|
|||||||
Reference in New Issue
Block a user