diff --git a/src/components/sqliteStream.js b/src/components/sqliteStream.js index 32e4a461..f53ed48c 100644 --- a/src/components/sqliteStream.js +++ b/src/components/sqliteStream.js @@ -2,16 +2,28 @@ const Readable = require('stream').Readable; const Sqlite3 = require('better-sqlite3'); const logger = require('pelias-logger').get('whosonfirst:sqliteStream'); +// attempt to create index to improve read performance +// +// catch all failures since the PIP service will have several processes all +// trying to acquire a write lock on the DB, and only one will succeed +// +// note: can be removed once the upstream PR is merged and all data on +// dist.whosonfirst.org is updated: +// https://github.com/whosonfirst/go-whosonfirst-sqlite-features/pull/4 +function createIndex(dbPath) { + try { + new Sqlite3(dbPath) + .exec('CREATE INDEX IF NOT EXISTS spr_obsolete ON spr (is_deprecated, is_superseded)') + .close(); + } catch (e){ + } +} + class SQLiteStream extends Readable { constructor(dbPath, sql) { super({ objectMode: true, autoDestroy: true, highWaterMark: 32 }); - // ensure indices exist - // note: this can be removed once the upstream PR is merged: - // https://github.com/whosonfirst/go-whosonfirst-sqlite-features/pull/4 - new Sqlite3(dbPath) - .exec('CREATE INDEX IF NOT EXISTS spr_obsolete ON spr (is_deprecated, is_superseded)') - .close(); + createIndex(dbPath); this._db = new Sqlite3(dbPath, { readonly: true }); this._iterator = this._db.prepare(sql).iterate(); @@ -81,4 +93,4 @@ function findGeoJSONByPlacetypeAndWOFId(placetypes, wofids) { module.exports = SQLiteStream; module.exports.findGeoJSON = findGeoJSON; module.exports.findGeoJSONByPlacetype = findGeoJSONByPlacetype; -module.exports.findGeoJSONByPlacetypeAndWOFId = findGeoJSONByPlacetypeAndWOFId; \ No newline at end of file +module.exports.findGeoJSONByPlacetypeAndWOFId = findGeoJSONByPlacetypeAndWOFId;