Merge pull request #7327 from tschaub/tile-management

Prune the tile cache after updating a source's URL
This commit is contained in:
Tim Schaub
2017-10-09 17:01:12 -06:00
committed by GitHub
18 changed files with 308 additions and 79 deletions
+64 -2
View File
@@ -1,8 +1,11 @@
goog.require('ol.Image');
goog.require('ol.Map');
goog.require('ol.View');
goog.require('ol.layer.Layer');
goog.require('ol.layer.Tile');
goog.require('ol.renderer.Layer');
goog.require('ol.source.XYZ');
goog.require('ol.tilecoord');
describe('ol.renderer.Layer', function() {
@@ -80,4 +83,63 @@ describe('ol.renderer.Layer', function() {
});
});
describe('manageTilePyramid behavior', function() {
var target, map, view, source;
beforeEach(function(done) {
target = document.createElement('div');
Object.assign(target.style, {
position: 'absolute',
left: '-1000px',
top: '-1000px',
width: '360px',
height: '180px'
});
document.body.appendChild(target);
view = new ol.View({
center: [0, 0],
zoom: 0
});
source = new ol.source.XYZ({
url: '#{x}/{y}/{z}'
});
map = new ol.Map({
target: target,
view: view,
layers: [
new ol.layer.Tile({
source: source
})
]
});
map.once('postrender', function() {
done();
});
});
afterEach(function() {
map.dispose();
document.body.removeChild(target);
});
it('accesses tiles from current zoom level last', function(done) {
// expect most recent tile in the cache to be from zoom level 0
var key = source.tileCache.peekFirstKey();
var tileCoord = ol.tilecoord.fromKey(key);
expect(tileCoord[0]).to.be(0);
map.once('moveend', function() {
// expect most recent tile in the cache to be from zoom level 4
var key = source.tileCache.peekFirstKey();
var tileCoord = ol.tilecoord.fromKey(key);
expect(tileCoord[0]).to.be(4);
done();
});
view.setZoom(4);
});
});
});
+3 -1
View File
@@ -5,8 +5,10 @@ goog.require('ol.proj');
goog.require('ol.proj.Projection');
goog.require('ol.source.Source');
goog.require('ol.source.Tile');
goog.require('ol.tilecoord');
goog.require('ol.tilegrid.TileGrid');
/**
* Tile source for tests that uses a EPSG:4326 based grid with 4 resolutions and
* 256x256 tiles.
@@ -40,7 +42,7 @@ ol.inherits(MockTile, ol.source.Tile);
* @inheritDoc
*/
MockTile.prototype.getTile = function(z, x, y) {
var key = this.getKeyZXY(z, x, y);
var key = ol.tilecoord.getKeyZXY(z, x, y);
if (this.tileCache.containsKey(key)) {
return /** @type {!ol.Tile} */ (this.tileCache.get(key));
} else {
+2 -3
View File
@@ -1,5 +1,3 @@
goog.require('ol.ImageTile');
goog.require('ol.TileState');
goog.require('ol.TileUrlFunction');
@@ -9,6 +7,7 @@ goog.require('ol.proj.EPSG3857');
goog.require('ol.proj.Projection');
goog.require('ol.reproj.Tile');
goog.require('ol.source.TileImage');
goog.require('ol.tilecoord');
goog.require('ol.tilegrid');
@@ -52,7 +51,7 @@ describe('ol.source.TileImage', function() {
expect(source.getKey()).to.be('');
source.getTileInternal(0, 0, -1, 1, ol.proj.get('EPSG:3857'));
expect(source.tileCache.getCount()).to.be(1);
tile = source.tileCache.get(source.getKeyZXY(0, 0, -1));
tile = source.tileCache.get(ol.tilecoord.getKeyZXY(0, 0, -1));
});
it('gets the tile from the cache', function() {
+6 -8
View File
@@ -275,15 +275,13 @@ describe('ol.source.TileWMS', function() {
});
describe('#setUrls()', function() {
it ('resets coordKeyPrefix_', function() {
var urls = ['u1', 'u2'];
var source1 = new ol.source.TileWMS({
urls: urls
it ('updates the source key', function() {
var source = new ol.source.TileWMS({
urls: ['u1', 'u2']
});
var source2 = new ol.source.TileWMS({});
expect(source2.coordKeyPrefix_).to.be.empty();
source2.setUrls(urls);
expect(source2.coordKeyPrefix_).to.equal(source1.coordKeyPrefix_);
var originalKey = source.getKey();
source.setUrls(['u3', 'u4']);
expect(source.getKey() !== originalKey).to.be(true);
});
});
});
+84
View File
@@ -164,6 +164,30 @@ describe('ol.structs.LRUCache', function() {
});
});
describe('#peekFirstKey()', function() {
it('returns the newest key in the cache', function() {
var cache = new ol.structs.LRUCache();
cache.set('oldest', 'oldest');
cache.set('oldish', 'oldish');
cache.set('newish', 'newish');
cache.set('newest', 'newest');
expect(cache.peekFirstKey()).to.eql('newest');
});
it('works if the cache has one item', function() {
var cache = new ol.structs.LRUCache();
cache.set('key', 'value');
expect(cache.peekFirstKey()).to.eql('key');
});
it('throws if the cache is empty', function() {
var cache = new ol.structs.LRUCache();
expect(function() {
cache.peekFirstKey();
}).to.throwException();
});
});
describe('peeking at the last value', function() {
it('returns the last key', function() {
fillLRUCache(lruCache);
@@ -188,6 +212,66 @@ describe('ol.structs.LRUCache', function() {
});
});
describe('#remove()', function() {
it('removes an item from the cache', function() {
var cache = new ol.structs.LRUCache();
cache.set('oldest', 'oldest');
cache.set('oldish', 'oldish');
cache.set('newish', 'newish');
cache.set('newest', 'newest');
cache.remove('oldish');
expect(cache.getCount()).to.eql(3);
expect(cache.getValues()).to.eql(['newest', 'newish', 'oldest']);
});
it('works when removing the oldest item', function() {
var cache = new ol.structs.LRUCache();
cache.set('oldest', 'oldest');
cache.set('oldish', 'oldish');
cache.set('newish', 'newish');
cache.set('newest', 'newest');
cache.remove('oldest');
expect(cache.getCount()).to.eql(3);
expect(cache.peekLastKey()).to.eql('oldish');
expect(cache.getValues()).to.eql(['newest', 'newish', 'oldish']);
});
it('works when removing the newest item', function() {
var cache = new ol.structs.LRUCache();
cache.set('oldest', 'oldest');
cache.set('oldish', 'oldish');
cache.set('newish', 'newish');
cache.set('newest', 'newest');
cache.remove('newest');
expect(cache.getCount()).to.eql(3);
expect(cache.peekFirstKey()).to.eql('newish');
expect(cache.getValues()).to.eql(['newish', 'oldish', 'oldest']);
});
it('returns the removed item', function() {
var cache = new ol.structs.LRUCache();
var item = {};
cache.set('key', item);
var returned = cache.remove('key');
expect(returned).to.be(item);
});
it('throws if the key does not exist', function() {
var cache = new ol.structs.LRUCache();
cache.set('foo', 'foo');
cache.set('bar', 'bar');
var call = function() {
cache.remove('bam');
};
expect(call).to.throwException();
});
});
describe('clearing the cache', function() {
it('clears the cache', function() {
fillLRUCache(lruCache);
+39
View File
@@ -0,0 +1,39 @@
goog.require('ol.Tile');
goog.require('ol.TileCache');
goog.require('ol.tilecoord');
describe('ol.TileCache', function() {
describe('#pruneExceptNewestZ()', function() {
it('gets rid of all entries that are not at the newest z', function() {
var tiles = [
new ol.Tile([0, 0, 0]),
new ol.Tile([1, 0, 0]),
new ol.Tile([1, 1, 0]),
new ol.Tile([2, 0, 0]),
new ol.Tile([2, 1, 0]),
new ol.Tile([2, 2, 0]),
new ol.Tile([2, 3, 0]) // newest tile at z: 2
];
var cache = new ol.TileCache();
sinon.spy(tiles[0], 'dispose');
tiles.forEach(function(tile) {
cache.set(ol.tilecoord.getKey(tile.tileCoord), tile);
});
cache.pruneExceptNewestZ();
expect(cache.getKeys()).to.eql([
'2/3/0',
'2/2/0',
'2/1/0',
'2/0/0'
]);
expect(tiles[0].dispose.calledOnce).to.be(true);
});
});
});
+17 -2
View File
@@ -1,5 +1,3 @@
goog.require('ol.tilecoord');
goog.require('ol.tilegrid.TileGrid');
@@ -23,6 +21,23 @@ describe('ol.TileCoord', function() {
});
});
describe('getKey()', function() {
it('returns a key for a tile coord', function() {
var key = ol.tilecoord.getKey([1, 2, 3]);
expect(key).to.eql('1/2/3');
});
});
describe('fromKey()', function() {
it('returns a tile coord given a key', function() {
var tileCoord = [1, 2, 3];
var key = ol.tilecoord.getKey(tileCoord);
var returned = ol.tilecoord.fromKey(key);
expect(returned).to.eql(tileCoord);
});
});
describe('hash', function() {
it('produces different hashes for different tile coords', function() {
var tileCoord1 = [3, 2, 1];