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,34 +62,26 @@
this.outstanding_requests = new Map()
}
getLeaf = tilestr => {
getLeaf = (offset, len) => {
return new Promise((resolve,reject) => {
if (this.leaves.has(tilestr)) {
resolve(this.leaves.get(tilestr))
} else if (this.outstanding_requests.has(tilestr)) {
this.outstanding_requests.get(tilestr).push(resolve)
if (this.leaves.has(offset)) {
resolve(this.leaves.get(offset))
} else if (this.outstanding_requests.has(offset)) {
this.outstanding_requests.get(offset).push(resolve)
} else {
this.outstanding_requests.set(tilestr,[])
this.rootdir.then(rootdir => {
if (rootdir.has(tilestr)) {
var val = rootdir.get(tilestr)
if (val[2] == 1) { // it is a directory
fetch(this.url, {headers:{'range':'bytes=' + val[0] + '-' + (val[0] + val[1]-1)}}).then(resp => {
this.outstanding_requests.set(offset,[])
fetch(this.url, {headers:{'range':'bytes=' + offset + '-' + (offset + len-1)}}).then(resp => {
return resp.arrayBuffer()
}).then(buf => {
var map = bytesToMap(new DataView(buf),val[1]/17)
this.leaves.set(tilestr,map)
var map = bytesToMap(new DataView(buf),len/17)
this.leaves.set(offset,map)
resolve(map)
this.outstanding_requests.get(tilestr).forEach(f => {
this.outstanding_requests.get(offset).forEach(f => {
f(map)
})
// TODO fix duplication of multiple leaves in same directory
console.log("leaves: ", this.leaves.size)
})
}
}
})
}
})
}
@@ -103,7 +95,9 @@
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_str = z7_tile[0] + "_" + z7_tile[1] + "_" + z7_tile[2]
return this.getLeaf(z7_tile_str).then(leafdir => {
if (rootdir.has(z7_tile_str)) {
const val = rootdir.get(z7_tile_str)
return this.getLeaf(val[0],val[1]).then(leafdir => {
if (leafdir.has(strid)) {
return leafdir.get(strid)
}
@@ -111,6 +105,7 @@
})
}
}
}
return null
})
}