| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
ve.dm.HashValueStore = function VeDmHashValueStore( values ) { |
| 27 |
|
| 28 |
this.hashStore = {}; |
| 29 |
|
| 30 |
this.hashes = []; |
| 31 |
if ( values ) { |
| 32 |
this.hashAll( values ); |
| 33 |
} |
| 34 |
}; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
OO.initClass( ve.dm.HashValueStore ); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 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 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 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 |
|
| 86 |
|
| 87 |
|
| 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 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 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 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
ve.dm.HashValueStore.prototype.clone = function () { |
| 125 |
return this.slice(); |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 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 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 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 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
ve.dm.HashValueStore.prototype.hashOfValue = function ( value, stringified ) { |
| 185 |
if ( typeof stringified !== 'string' ) { |
| 186 |
stringified = OO.getHash( value ); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
return 'h' + SparkMD5.hash( stringified ).slice( 0, 16 ); |
| 198 |
}; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 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 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
ve.dm.HashValueStore.prototype.value = function ( hash ) { |
| 223 |
return this.hashStore[ hash ]; |
| 224 |
}; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 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 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 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 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 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 |
}; |