geotile_sdk
Version:
A tile sdk for Cloud Optimised Geotiff and shapefile
141 lines (129 loc) • 3.9 kB
JavaScript
/*
* @Description: 将当前瓦片请求的纵向相关区域加入slowList(慢队列), 空闲时(无瓦片请求时)加入orderList, 获取瓦片
* @Author: luojun1
* @Date: 2021-10-21 17:25:12
* @LastEditTime: 2021-12-14 15:35:49
*/
// 缓存大小
var capability = 200 // 默认缓存大小,缓存大小为100时,大约占用74.8MB内存
// 双向链表, 存放paddingRegion, 为了实现定期淘汰无用瓦片,对paddingRegion进行排序(不存放order而是存放paddingRegion)
// 记录添加的顺序, 将后添加和最近访问的paddingRegion排在前面,定期删除链表尾部的paddingRegion
class RegionNode {
constructor (paddingRegion, z, xmlUUID) {
this.pre = null
this.next = null
this.Region = paddingRegion
this.z = z
this.xmlUUID = xmlUUID
}
}
class LinkedList {
constructor (size) {
this.capability = size || capability // 默认缓存大小,缓存大小为100时,大约占用74.8MB内存
this.length = 0
this.head = null
this.tail = null
}
addNode (cur) {
if (!cur) {
console.log('addNode:null')
return
}
cur.next = this.head
if (this.head !== null) {
this.head.pre = cur
}
this.head = cur // 更新head
if (!this.tail) {
this.tail = this.head
}
this.length++
}
rmNode (cur) {
if (!cur) {
console.log('rmNode:null')
return
}
if (cur === this.head && cur === this.tail) {
this.head = null
this.tail = null
} else if (cur === this.tail) {
this.tail = cur.pre
cur.pre.next = null
} else if (cur === this.head) {
this.head = cur.next
cur.next.pre = null
} else {
cur.pre.next = cur.next
cur.next.pre = cur.pre
}
this.length--
return cur
}
}
/** ******** 事件驱动的地图瓦片区域缓存************/
/** hashmap + 双向链表 作为瓦片区域缓存的基础结构 */
var regionCache = new LinkedList()
function addRegion (paddingRegion, z, xmlUUID) {
var cur = new RegionNode(paddingRegion, z, xmlUUID)
regionCache.addNode(cur)
return cur
}
// 将最近访问过的region排到链表前
function updateRegion (Node) {
// console.log('updateRegion:')
regionCache.rmNode(Node)
regionCache.addNode(Node)
}
function rmRegion (Node) {
regionCache.rmNode(Node)
}
// 检查缓存利用率
function checkRegionCache (cancelOrders) {
// console.log('regionCache.#length:', regionCache.length)
// var curNode = regionCache.head
// while (curNode) {
// console.log('paddingRegion:', curNode)
// curNode = curNode.next
// }
while (regionCache.length >= regionCache.capability) {
cancelOrders(regionCache.tail.Region, regionCache.tail.z, regionCache.tail.xmlUUID)
regionCache.rmNode(regionCache.tail)
}
}
// 慢队列(stack), 存放瓦片请求的纵向相关区域, 空闲时加入orderList请求瓦片
var slowList = []
// 预加载队列的容量,如队列满载,则删除旧的region
// 0.8是低级别region占所有region缓存的比例,当前级别region:3×4×图层数量,低级别region:3×2×4×2×图层数,当前级别:低级别=1:4
var slowListCap = capability * 0.8
class Region {
constructor (lowLevelRegion, z, xmlUUID) {
this.Region = lowLevelRegion
this.z = z
this.xmlUUID = xmlUUID
}
}
function addVerticalRegion (region, z, xmlUUID) {
slowList.push(new Region(region, z, xmlUUID))
while (slowList.length >= slowListCap) {
slowList.shift()
}
console.log('slowList.length:', slowList.length)
}
function preloadVerticalRegion (recall) {
var region = slowList.pop()
if (region) {
recall(region.Region, region.z, region.xmlUUID)
return null
} else {
return new Error('slowList is empty!!')
}
}
module.exports = {
addRegion,
updateRegion,
rmRegion,
checkRegionCache,
addVerticalRegion,
preloadVerticalRegion
}