From 784293d713082967af1e274874ead743ee549ceb Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Thu, 16 Aug 2018 13:08:14 -0700 Subject: [PATCH] fix buffer offset for feature state, more resilient structure --- src/data/feature_position_map.js | 16 ++++++++++------ src/data/program_configuration.js | 5 ++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/data/feature_position_map.js b/src/data/feature_position_map.js index bd83d69cef9..a8c5c9bb586 100644 --- a/src/data/feature_position_map.js +++ b/src/data/feature_position_map.js @@ -1,6 +1,7 @@ // @flow import { register } from '../util/web_worker_transfer'; +import assert from 'assert'; type SerializedFeaturePositionMap = { ids: Float64Array; @@ -17,22 +18,24 @@ type FeaturePosition = { export default class FeaturePositionMap { ids: Array; positions: Array; - _bufferLen: number; + indexed: boolean; constructor() { this.ids = []; this.positions = []; - this._bufferLen = 0; + this.indexed = false; } - add(id: number, index: number, newLength: number) { + add(id: number, index: number, start: number, end: number) { this.ids.push(id); - this.positions.push(index, this._bufferLen, newLength); - this._bufferLen = newLength; + this.positions.push(index, start, end); } getPositions(id: number): Array { - // binary search for the first occurrence of id in this.ids + assert(this.indexed); + + // binary search for the first occurrence of id in this.ids; + // relies on ids/positions being sorted by id, which happens in serialization let i = 0; let j = this.ids.length - 1; while (i < j) { @@ -71,6 +74,7 @@ export default class FeaturePositionMap { // so TypedArray vs Array distinction that flow points out doesn't matter map.ids = (obj.ids: any); map.positions = (obj.positions: any); + map.indexed = true; return map; } } diff --git a/src/data/program_configuration.js b/src/data/program_configuration.js index 3f4c4e84ff7..997aab5784b 100644 --- a/src/data/program_configuration.js +++ b/src/data/program_configuration.js @@ -360,6 +360,7 @@ export default class ProgramConfiguration { _buffers: Array; _featureMap: FeaturePositionMap; + _bufferOffset: number; constructor() { this.binders = {}; @@ -367,6 +368,7 @@ export default class ProgramConfiguration { this._buffers = []; this._featureMap = new FeaturePositionMap(); + this._bufferOffset = 0; } static createDynamic(layer: Layer, zoom: number, filterProperties: (string) => boolean) { @@ -405,8 +407,9 @@ export default class ProgramConfiguration { this.binders[property].populatePaintArray(newLength, feature); } if (feature.id !== undefined) { - this._featureMap.add(+feature.id, index, newLength); + this._featureMap.add(+feature.id, index, this._bufferOffset, newLength); } + this._bufferOffset = newLength; } updatePaintArrays(featureStates: FeatureStates, vtLayer: VectorTileLayer, layer: TypedStyleLayer): boolean {