Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/ve.dm.HashValueStore.js

raw symbols SCIP
1 /*!
2 * VisualEditor HashValueStore class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /* global SparkMD5 */
8
9 /**
10 * Ordered append-only hash store, whose values once inserted are immutable
11 *
12 * Values are objects, strings or Arrays, and are hashed using an algorithm with low collision
13 * probability: values with the same hash can be assumed equal.
14 *
15 * Values are stored in insertion order, and the store can be sliced to get a subset of values
16 * inserted consecutively.
17 *
18 * Two stores can be merged even if they have differently computed hashes, so long as two values
19 * will (with high probability) have the same hash only if equal. In this case, equivalent
20 * values can have two different hashes.
21 *
22 * @class
23 * @constructor
24 * @param {Object[]} [values] Values to insert
25 */
26 ve.dm.HashValueStore = function VeDmHashValueStore( values ) {
27 // Maps hashes to values
28 this.hashStore = {};
29 // Hashes in order of insertion (used for slicing)
30 this.hashes = [];
31 if ( values ) {
32 this.hashAll( values );
33 }
34 };
35
36 /* Inheritance */
37
38 OO.initClass( ve.dm.HashValueStore );
39
40 /* Static Methods */
41
42 /**
43 * Deserialize a store from a JSONable object
44 *
45 * @param {Function} deserializeValue Deserializer for arbitrary store values
46 * @param {Object|null} data Store serialized as a JSONable object
47 * @return {ve.dm.HashValueStore} Deserialized store
48 */
49 ve.dm.HashValueStore.static.deserialize = function ( deserializeValue, data ) {
50 const store = new ve.dm.HashValueStore();
51
52 if ( !data ) {
53 return store;
54 }
55
56 store.hashes = data.hashes.slice();
57 store.hashStore = {};
58 for ( const hash in data.hashStore ) {
59 store.hashStore[ hash ] = deserializeValue( data.hashStore[ hash ] );
60 }
61 return store;
62 };
63
64 /* Methods */
65
66 /**
67 * Serialize the store into a JSONable object
68 *
69 * @param {Function} serializeValue Serializer for arbitrary store values
70 * @return {Object|null} Serialized store, null if empty
71 */
72 ve.dm.HashValueStore.prototype.serialize = function ( serializeValue ) {
73 const serialized = {};
74
75 for ( const hash in this.hashStore ) {
76 serialized[ hash ] = serializeValue( this.hashStore[ hash ] );
77 }
78 return this.getLength() ? {
79 hashes: this.hashes.slice(),
80 hashStore: serialized
81 } : null;
82 };
83
84 /**
85 * Get the number of values in the store
86 *
87 * @return {number} Number of values in the store
88 */
89 ve.dm.HashValueStore.prototype.getLength = function () {
90 return this.hashes.length;
91 };
92
93 ve.dm.HashValueStore.prototype.truncate = function ( start ) {
94 const removedHashes = this.hashes.splice( start );
95 for ( let i = 0, len = removedHashes.length; i < len; i++ ) {
96 delete this.hashStore[ removedHashes[ i ] ];
97 }
98 };
99
100 /**
101 * Return a new store containing a slice of the values in insertion order
102 *
103 * @param {number} [start] Include values from position start onwards (default: 0)
104 * @param {number} [end] Include values to position end exclusive (default: slice to end)
105 * @return {ve.dm.HashValueStore} Slice of the current store (with non-cloned value references)
106 */
107 ve.dm.HashValueStore.prototype.slice = function ( start, end ) {
108 const sliced = new this.constructor();
109
110 sliced.hashes = this.hashes.slice( start, end );
111 for ( let i = 0, len = sliced.hashes.length; i < len; i++ ) {
112 const hash = sliced.hashes[ i ];
113 sliced.hashStore[ hash ] = this.hashStore[ hash ];
114 }
115 return sliced;
116 };
117
118 /**
119 * Clone a store.
120 *
121 * @deprecated Use #slice with no arguments.
122 * @return {ve.dm.HashValueStore} New store with the same contents as this one
123 */
124 ve.dm.HashValueStore.prototype.clone = function () {
125 return this.slice();
126 };
127
128 /**
129 * Insert a value into the store
130 *
131 * @param {Object|string|Array} value Value to store
132 * @param {string} [stringified] Stringified version of value; default OO.getHash( value )
133 * @return {string} Hash value with low collision probability
134 */
135 ve.dm.HashValueStore.prototype.hash = function ( value, stringified ) {
136 const hash = this.hashOfValue( value, stringified );
137
138 if ( !this.hashStore[ hash ] ) {
139 if ( Array.isArray( value ) ) {
140 this.hashStore[ hash ] = ve.copy( value );
141 } else if ( value !== null && typeof value === 'object' ) {
142 this.hashStore[ hash ] = ve.cloneObject( value );
143 } else {
144 this.hashStore[ hash ] = value;
145 }
146 this.hashes.push( hash );
147 }
148
149 return hash;
150 };
151
152 /**
153 * Replace a value's stored hash, e.g. if the value has changed and you want to discard the old one.
154 *
155 * @param {string} oldHash The value's previously stored hash
156 * @param {Object|string|Array} value New value
157 * @throws {Error} Old hash not found
158 * @return {string} New hash
159 */
160 ve.dm.HashValueStore.prototype.replaceHash = function ( oldHash, value ) {
161 const newHash = this.hashOfValue( value );
162
163 if ( !Object.prototype.hasOwnProperty.call( this.hashStore, oldHash ) ) {
164 throw new Error( 'Old hash not found: ' + oldHash );
165 }
166
167 delete this.hashStore[ oldHash ];
168
169 if ( this.hashStore[ newHash ] === undefined ) {
170 this.hashStore[ newHash ] = value;
171 this.hashes.splice( this.hashes.indexOf( oldHash ), 1, newHash );
172 }
173
174 return newHash;
175 };
176
177 /**
178 * Get the hash of a value without inserting it in the store
179 *
180 * @param {Object|string|Array} value Value to hash
181 * @param {string} [stringified] Stringified version of value; default OO.getHash( value )
182 * @return {string} Hash value with low collision probability
183 */
184 ve.dm.HashValueStore.prototype.hashOfValue = function ( value, stringified ) {
185 if ( typeof stringified !== 'string' ) {
186 stringified = OO.getHash( value );
187 }
188
189 // We don't need cryptographically strong hashes, just low collision probability. Given
190 // effectively random hash distribution, for n values hashed into a space of m hash
191 // strings, the probability of a collision is roughly n^2 / (2m). We use 16 hex digits
192 // of MD5 i.e. 2^64 possible hash strings, so given 2^16 stored values the collision
193 // probability is about 2^-33 =~ 0.0000000001 , i.e. negligible.
194 //
195 // Prefix with a letter to prevent all numeric hashes, and to constrain the space of
196 // possible object property values.
197 return 'h' + SparkMD5.hash( stringified ).slice( 0, 16 );
198 };
199
200 /**
201 * Get the hashes of values in the store
202 *
203 * Same as hash but with arrays.
204 *
205 * @param {Object[]} values Values to lookup or store
206 * @return {string[]} The hashes of the values in the store
207 */
208 ve.dm.HashValueStore.prototype.hashAll = function ( values ) {
209 const hashes = [];
210 for ( let i = 0, length = values.length; i < length; i++ ) {
211 hashes.push( this.hash( values[ i ] ) );
212 }
213 return hashes;
214 };
215
216 /**
217 * Get the value stored for a particular hash
218 *
219 * @param {string} hash Hash to look up
220 * @return {Object|undefined} Value stored for this hash if present, else undefined
221 */
222 ve.dm.HashValueStore.prototype.value = function ( hash ) {
223 return this.hashStore[ hash ];
224 };
225
226 /**
227 * Get the values stored for a list of hashes
228 *
229 * Same as value but with arrays.
230 *
231 * @param {string[]} hashes Hashes to lookup
232 * @return {Array} Values for these hashes (undefined for any not present)
233 */
234 ve.dm.HashValueStore.prototype.values = function ( hashes ) {
235 const values = [];
236 for ( let i = 0, length = hashes.length; i < length; i++ ) {
237 values.push( this.value( hashes[ i ] ) );
238 }
239 return values;
240 };
241
242 /**
243 * Merge another store into this store.
244 *
245 * It is allowed for the two stores to have differently computed hashes, so long as two values
246 * will (with high probability) have the same hash only if equal. In this case, equivalent
247 * values can have two different hashes.
248 *
249 * Values are added in the order they appear in the other store. Objects added to the store are
250 * added by reference, not cloned, unlike in .hash()
251 *
252 * @param {ve.dm.HashValueStore} other Store to merge into this one
253 */
254 ve.dm.HashValueStore.prototype.merge = function ( other ) {
255 if ( other === this ) {
256 return;
257 }
258
259 for ( let i = 0, len = other.hashes.length; i < len; i++ ) {
260 const hash = other.hashes[ i ];
261 if ( !Object.prototype.hasOwnProperty.call( this.hashStore, hash ) ) {
262 this.hashStore[ hash ] = other.hashStore[ hash ];
263 this.hashes.push( hash );
264 }
265 }
266 };
267
268 /**
269 * Clone this store excluding certain values, like a set difference operation
270 *
271 * @param {ve.dm.HashValueStore|Object} omit Store of values to omit, or object whose keys are hashes to emit
272 * @return {ve.dm.HashValueStore} All values in this that do not appear in other
273 */
274 ve.dm.HashValueStore.prototype.difference = function ( omit ) {
275 const store = new this.constructor();
276
277 if ( omit instanceof ve.dm.HashValueStore ) {
278 omit = omit.hashStore;
279 }
280 for ( let i = 0, len = this.hashes.length; i < len; i++ ) {
281 const hash = this.hashes[ i ];
282 if ( !Object.prototype.hasOwnProperty.call( omit, hash ) ) {
283 store.hashes.push( hash );
284 store.hashStore[ hash ] = this.hashStore[ hash ];
285 }
286 }
287 return store;
288 };