@@ -33,6 +33,7 @@ const util = require('util');
33
33
const dbTypeByNum = new Map ( ) ;
34
34
const dbTypeByOraTypeNum = new Map ( ) ;
35
35
const dbTypeByColumnTypeName = new Map ( ) ;
36
+ const MAX_UINT32 = Math . pow ( 2 , 32 ) - 1 ;
36
37
37
38
// define class used for database types
38
39
class DbType {
@@ -343,8 +344,6 @@ class JsonId extends Uint8Array {
343
344
// indices must be an regular Array
344
345
// values can be regular or typedArray.
345
346
class SparseVector {
346
- // It also takes skipChecks which is made true for avoiding checks if
347
- // the SparseVector is constructed internally.
348
347
constructor ( input ) {
349
348
this . _indices = new Uint32Array ( 0 ) ;
350
349
this . _values = new Float64Array ( 0 ) ;
@@ -353,25 +352,29 @@ class SparseVector {
353
352
if ( ! input ) {
354
353
return ;
355
354
}
356
- if ( typeof input === 'string' ) {
355
+ if ( typeof input === 'object' &&
356
+ "numDimensions" in input &&
357
+ "indices" in input &&
358
+ "values" in input
359
+ ) {
360
+ // Object has valid properties for Sparse.
361
+ this . _fromObject ( input ) ;
362
+ } else if ( typeof input === 'string' ) {
357
363
// initialize from string.
358
364
this . _fromString ( input ) ;
359
365
} else if ( this . _validDenseArray ( input ) ) {
360
366
// dense array
361
367
this . _fromDense ( input ) ;
362
- } else if ( typeof input === 'object' ) {
363
- this . _fromObject ( input ) ;
364
368
} else {
365
369
errors . throwErr ( errors . ERR_VECTOR_SPARSE_INVALID_INPUT ) ;
366
370
}
367
371
}
368
372
369
- _validDenseArray ( value , isIndices = false ) {
370
- const valid = ( value instanceof Float32Array ||
373
+ _validDenseArray ( value ) {
374
+ return ( value instanceof Float32Array ||
371
375
value instanceof Float64Array ||
372
376
value instanceof Int8Array || ( Object . getPrototypeOf ( value )
373
377
=== Uint8Array . prototype ) || Array . isArray ( value ) ) ;
374
- return ! isIndices ? valid : valid || value instanceof Uint32Array ;
375
378
}
376
379
377
380
// Check if indexArray and valuesArray have the same length
@@ -388,7 +391,7 @@ class SparseVector {
388
391
if ( ! this . _validDenseArray ( values ) ) {
389
392
errors . throwErr ( errors . ERR_VECTOR_SPARSE_VALUES_IS_NOT_ARRAY ) ;
390
393
}
391
- if ( ! ( this . _validDenseArray ( indices , true ) ) ) {
394
+ if ( ! ( indices instanceof Uint32Array ) && ! Array . isArray ( indices ) ) {
392
395
errors . throwErr ( errors . ERR_VECTOR_SPARSE_INDICES_IS_NOT_ARRAY ) ;
393
396
}
394
397
SparseVector . _validateLengths ( indices , values ) ;
@@ -402,14 +405,27 @@ class SparseVector {
402
405
this . _convertToTypedArrays ( ) ;
403
406
}
404
407
405
- _fromObject ( input , skipChecks = false ) {
406
- this . _updateProperties ( input . numDimensions , input . indices , input . values , skipChecks ) ;
408
+ _fromObject ( input ) {
409
+ this . _updateProperties ( input . numDimensions , input . indices , input . values ) ;
407
410
}
408
411
409
412
_convertToTypedArrays ( ) {
410
413
// convert to typed arrays.
411
- this . _indices = this . _indices instanceof Uint32Array ? this . _indices :
412
- new Uint32Array ( this . _indices ) ;
414
+ if ( ! ( this . _indices instanceof Uint32Array ) ) {
415
+ // validate elements are valid uint32 type.
416
+ const indices = new Uint32Array ( this . _indices . map ( ( x , index ) => {
417
+ if ( typeof x !== 'number' || ! Number . isInteger ( x ) ) {
418
+ errors . throwErr ( errors . ERR_VECTOR_SPARSE_INDICES_ELEM_IS_NOT_VALID , index ) ;
419
+ }
420
+ if ( x < 0 || x > MAX_UINT32 || x > ( this . numDimensions - 1 ) ) {
421
+ errors . throwErr ( errors . ERR_VECTOR_SPARSE_INDICES_ELEM_IS_NOT_VALID ,
422
+ index ) ;
423
+ }
424
+ return x ;
425
+ } ) ) ;
426
+ this . _indices = indices ;
427
+ }
428
+
413
429
this . _values = Array . isArray ( this . _values ) ? new Float64Array ( this . _values )
414
430
: this . _values ;
415
431
}
@@ -428,8 +444,8 @@ class SparseVector {
428
444
this . _values . push ( input [ i ] ) ;
429
445
}
430
446
}
431
- this . _convertToTypedArrays ( ) ;
432
447
this . _numDimensions = input . length ;
448
+ this . _convertToTypedArrays ( ) ;
433
449
}
434
450
435
451
// parse a string input into a sparse vector
0 commit comments