fix leaf duplication, index by offset

This commit is contained in:
Brandon Liu
2021-02-18 18:50:59 +08:00
parent f309d81aa2
commit 0c55b7e115

View File

@@ -62,32 +62,24 @@
this.outstanding_requests = new Map() this.outstanding_requests = new Map()
} }
getLeaf = tilestr => { getLeaf = (offset, len) => {
return new Promise((resolve,reject) => { return new Promise((resolve,reject) => {
if (this.leaves.has(tilestr)) { if (this.leaves.has(offset)) {
resolve(this.leaves.get(tilestr)) resolve(this.leaves.get(offset))
} else if (this.outstanding_requests.has(tilestr)) { } else if (this.outstanding_requests.has(offset)) {
this.outstanding_requests.get(tilestr).push(resolve) this.outstanding_requests.get(offset).push(resolve)
} else { } else {
this.outstanding_requests.set(tilestr,[]) this.outstanding_requests.set(offset,[])
this.rootdir.then(rootdir => { fetch(this.url, {headers:{'range':'bytes=' + offset + '-' + (offset + len-1)}}).then(resp => {
if (rootdir.has(tilestr)) { return resp.arrayBuffer()
var val = rootdir.get(tilestr) }).then(buf => {
if (val[2] == 1) { // it is a directory var map = bytesToMap(new DataView(buf),len/17)
fetch(this.url, {headers:{'range':'bytes=' + val[0] + '-' + (val[0] + val[1]-1)}}).then(resp => { this.leaves.set(offset,map)
return resp.arrayBuffer() resolve(map)
}).then(buf => { this.outstanding_requests.get(offset).forEach(f => {
var map = bytesToMap(new DataView(buf),val[1]/17) f(map)
this.leaves.set(tilestr,map) })
resolve(map) console.log("leaves: ", this.leaves.size)
this.outstanding_requests.get(tilestr).forEach(f => {
f(map)
})
// TODO fix duplication of multiple leaves in same directory
console.log("leaves: ", this.leaves.size)
})
}
}
}) })
} }
}) })
@@ -103,12 +95,15 @@
var z7_tile_diff = (z - 7) var z7_tile_diff = (z - 7)
var z7_tile = [7,Math.trunc(x / (1 << z7_tile_diff)), Math.trunc(y / (1 << z7_tile_diff))] var z7_tile = [7,Math.trunc(x / (1 << z7_tile_diff)), Math.trunc(y / (1 << z7_tile_diff))]
var z7_tile_str = z7_tile[0] + "_" + z7_tile[1] + "_" + z7_tile[2] var z7_tile_str = z7_tile[0] + "_" + z7_tile[1] + "_" + z7_tile[2]
return this.getLeaf(z7_tile_str).then(leafdir => { if (rootdir.has(z7_tile_str)) {
if (leafdir.has(strid)) { const val = rootdir.get(z7_tile_str)
return leafdir.get(strid) return this.getLeaf(val[0],val[1]).then(leafdir => {
} if (leafdir.has(strid)) {
return null return leafdir.get(strid)
}) }
return null
})
}
} }
} }
return null return null