Throw an exception if an ol.structs.RBush is modified while reading

This commit is contained in:
Tom Payne
2013-11-27 14:44:05 +01:00
parent fab0fe5e7a
commit 665781ee03
2 changed files with 100 additions and 1 deletions

View File

@@ -189,6 +189,12 @@ ol.structs.RBush = function(opt_maxEntries) {
*/ */
this.valueExtent_ = {}; this.valueExtent_ = {};
/**
* @private
* @type {number}
*/
this.readers_ = 0;
}; };
@@ -387,7 +393,12 @@ ol.structs.RBush.prototype.condense_ = function(path) {
* @template S * @template S
*/ */
ol.structs.RBush.prototype.forEach = function(callback, opt_obj) { ol.structs.RBush.prototype.forEach = function(callback, opt_obj) {
++this.readers_;
try {
return this.forEach_(this.root_, callback, opt_obj); return this.forEach_(this.root_, callback, opt_obj);
} finally {
--this.readers_;
}
}; };
@@ -432,6 +443,25 @@ ol.structs.RBush.prototype.forEach_ = function(node, callback, opt_obj) {
*/ */
ol.structs.RBush.prototype.forEachInExtent = ol.structs.RBush.prototype.forEachInExtent =
function(extent, callback, opt_obj) { function(extent, callback, opt_obj) {
++this.readers_;
try {
return this.forEachInExtent_(extent, callback, opt_obj);
} finally {
--this.readers_;
}
};
/**
* @param {ol.Extent} extent Extent.
* @param {function(this: S, T): *} callback Callback.
* @param {S=} opt_obj Scope.
* @private
* @return {*} Callback return value.
* @template S
*/
ol.structs.RBush.prototype.forEachInExtent_ =
function(extent, callback, opt_obj) {
/** @type {Array.<ol.structs.RBushNode.<T>>} */ /** @type {Array.<ol.structs.RBushNode.<T>>} */
var toVisit = [this.root_]; var toVisit = [this.root_];
var result; var result;
@@ -496,6 +526,9 @@ ol.structs.RBush.prototype.getKey_ = function(value) {
* @param {T} value Value. * @param {T} value Value.
*/ */
ol.structs.RBush.prototype.insert = function(extent, value) { ol.structs.RBush.prototype.insert = function(extent, value) {
if (this.readers_) {
throw Error('cannot insert value while reading');
}
var key = this.getKey_(value); var key = this.getKey_(value);
goog.asserts.assert(!this.valueExtent_.hasOwnProperty(key)); goog.asserts.assert(!this.valueExtent_.hasOwnProperty(key));
this.insert_(extent, value, this.root_.height - 1); this.insert_(extent, value, this.root_.height - 1);
@@ -535,6 +568,9 @@ ol.structs.RBush.prototype.insert_ = function(extent, value, level) {
* @param {T} value Value. * @param {T} value Value.
*/ */
ol.structs.RBush.prototype.remove = function(value) { ol.structs.RBush.prototype.remove = function(value) {
if (this.readers_) {
throw Error('cannot remove value while reading');
}
var key = this.getKey_(value); var key = this.getKey_(value);
goog.asserts.assert(this.valueExtent_.hasOwnProperty(key)); goog.asserts.assert(this.valueExtent_.hasOwnProperty(key));
var extent = this.valueExtent_[key]; var extent = this.valueExtent_[key];
@@ -629,6 +665,9 @@ ol.structs.RBush.prototype.splitRoot_ = function(node1, node2) {
* @param {T} value Value. * @param {T} value Value.
*/ */
ol.structs.RBush.prototype.update = function(extent, value) { ol.structs.RBush.prototype.update = function(extent, value) {
if (this.readers_) {
throw Error('cannot update value while reading');
}
var key = this.getKey_(value); var key = this.getKey_(value);
var currentExtent = this.valueExtent_[key]; var currentExtent = this.valueExtent_[key];
goog.asserts.assert(goog.isDef(currentExtent)); goog.asserts.assert(goog.isDef(currentExtent));

View File

@@ -55,6 +55,27 @@ describe('ol.structs.RBush', function() {
}); });
describe('#insert', function() {
it('throws an exception if called while iterating over all values',
function() {
expect(function() {
rBush.forEach(function(value) {
rBush.insert([0, 0, 1, 1], {});
});
}).to.throwException();
});
it('throws an exception if called while iterating over an extent',
function() {
expect(function() {
rBush.forEachInExtent([-10, -10, 10, 10], function(value) {
rBush.insert([0, 0, 1, 1], {});
});
}).to.throwException();
});
});
describe('#remove', function() { describe('#remove', function() {
it('can remove each object', function() { it('can remove each object', function() {
@@ -66,6 +87,45 @@ describe('ol.structs.RBush', function() {
} }
}); });
it('throws an exception if called while iterating over all values',
function() {
expect(function() {
rBush.forEach(function(value) {
rBush.remove(value);
});
}).to.throwException();
});
it('throws an exception if called while iterating over an extent',
function() {
expect(function() {
rBush.forEachInExtent([-10, -10, 10, 10], function(value) {
rBush.remove(value);
});
}).to.throwException();
});
});
describe('#update', function() {
it('throws an exception if called while iterating over all values',
function() {
expect(function() {
rBush.forEach(function(value) {
rBush.update([0, 0, 1, 1], objs[1]);
});
}).to.throwException();
});
it('throws an exception if called while iterating over an extent',
function() {
expect(function() {
rBush.forEachInExtent([-10, -10, 10, 10], function(value) {
rBush.update([0, 0, 1, 1], objs[1]);
});
}).to.throwException();
});
}); });
}); });