Skip to content

Commit

Permalink
fix: repair dangling refs repeatedly until all are resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
hackergrrl committed Oct 13, 2017
1 parent 75950c9 commit dc0679c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
29 changes: 16 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = function (osm, xml, opts, cb) {
.pipe(osm2Obj({coerceIds: false}))
.pipe(t)

t.once('finish', cb)
t.once('end', cb)

var ids = {}

Expand Down Expand Up @@ -59,32 +59,35 @@ module.exports = function (osm, xml, opts, cb) {
}

function write (change, enc, next) {
if (hasDanglingRefs(change)) {
pending.push(change)
return next()
}

processChange(change, next)
}

function flush (cb) {
function flush (fin) {
var self = this
next(null)
function next (err) {
if (err) return cb(err)
if (pending.length === 0) return cb()
var change = pending.shift()
processChange(change, next)
console.log('p', pending.length)
if (err) return fin(err)
else if (pending.length === 0) {
self.resume()
return fin()
}
else processChange(pending.shift(), next)
}
}

function processChange (change, cb) {
if (hasDanglingRefs(change)) {
pending.push(change)
return process.nextTick(cb)
}

change = replaceIds(change)
var cid = change.id
delete change.id
osm.create(change, function (err, id, node) {
ids[cid] = id
if (err) throw err
cb()
cb(err)
})
}
}
23 changes: 11 additions & 12 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@ var fs = require('fs')
var importer = require('../')
var OsmMem = require('osm-p2p-mem')

test('smaller.xml', function (t) {
var xml = fs.createReadStream('./test/smaller.xml')
test('ways before nodes', function (t) {
var xml = fs.createReadStream('./test/ways_first.xml')

var osm = OsmMem()

var pending = 7

importer(osm, xml, function (err) {
t.error(err)
t.end()
var rs = osm.kv.createReadStream()
rs.on('data', function () {
if(!--pending) t.end()
})
})
})

test('ways before nodes', function (t) {
var xml = fs.createReadStream('./test/ways_first.xml')
test('smaller.xml', function (t) {
var xml = fs.createReadStream('./test/smaller.xml')

var osm = OsmMem()

importer(osm, xml, function (err) {
t.error(err)
osm.ready(function () {
osm.query([[-85,85],[-85,85]], function (err, res) {
t.error(err)
t.equal(5, res.length)
t.end()
})
})
t.end()
})
})
5 changes: 5 additions & 0 deletions test/ways_first.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
<tag k="name" v="Boulevard Kamanyola"/>
</way>

<relation id="119180" version="5" timestamp="2016-08-24T15:38:40Z" changeset="41666103">
<member type="way" ref="-1" role="outer"/>
<tag k="name" v="Volcano CDP"/>
</relation>

<node id="-2" lat="-11.6572028" lon="27.4691154" visible="true"/>
<node id="-3" lat="-11.6570032" lon="27.4688649" visible="true"/>
<node id="-4" lat="-11.6567879" lon="27.4684879" visible="true"/>
Expand Down

0 comments on commit dc0679c

Please sign in to comment.