Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import v5x8 from './bolt-protocol-v5x8.transformer'
import { TypeTransformer } from './transformer'
import { structure } from '../packstream'
import { Vector, newError } from 'neo4j-driver-core'
import { Vector, isVector, newError } from 'neo4j-driver-core'
const VECTOR = 0x56
const FLOAT_32 = 0xc6
const FLOAT_64 = 0xc1
Expand All @@ -39,7 +39,7 @@ const typeToTypeMarker = {
function createVectorTransformer () {
return new TypeTransformer({
signature: VECTOR,
isTypeInstance: object => object instanceof Vector,
isTypeInstance: object => isVector(object),
toStructure: vector => {
const typeMarker = typeToTypeMarker[vector.getType()]
if (typeMarker === undefined) {
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ import * as json from './json'
import resultTransformers, { ResultTransformer } from './result-transformers'
import ClientCertificate, { clientCertificateProviders, ClientCertificateProvider, ClientCertificateProviders, RotatingClientCertificateProvider, resolveCertificateProvider } from './client-certificate'
import * as internal from './internal' // todo: removed afterwards
import Vector, { VectorType, vector } from './vector'
import Vector, { VectorType, vector, isVector } from './vector'

/**
* Object containing string constants representing predefined {@link Neo4jError} codes.
Expand Down Expand Up @@ -189,7 +189,9 @@ const forExport = {
notificationFilterDisabledClassification,
notificationFilterMinimumSeverityLevel,
clientCertificateProviders,
resolveCertificateProvider
resolveCertificateProvider,
isVector,
vector
}

export {
Expand Down Expand Up @@ -268,6 +270,7 @@ export {
notificationFilterMinimumSeverityLevel,
clientCertificateProviders,
resolveCertificateProvider,
isVector,
Vector,
vector
}
Expand Down
41 changes: 41 additions & 0 deletions packages/core/src/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import { newError } from './error'

const VECTOR_IDENTIFIER_PROPERTY = '__isVector__'

type EnumRecord<T extends string | symbol> = { [key in T]: key }

export type VectorType = 'INT8' | 'INT16' | 'INT32' | 'INT64' | 'FLOAT32' | 'FLOAT64'
Expand Down Expand Up @@ -87,8 +89,38 @@ export default class Vector<K extends Float32Array | Float64Array | Int8Array |
getType (): VectorType {
return this._type
}

toString (): string {
return `vector([${this._typedArray.join(', ')}], ${this._typedArray.length}, ${getTypeString(this._type)})`
}
}

function getTypeString (type: VectorType): string {
switch (type) {
case 'INT8':
return 'INTEGER8 NOT NULL'
case 'INT16':
return 'INTEGER16 NOT NULL'
case 'INT32':
return 'INTEGER32 NOT NULL'
case 'INT64':
return 'INTEGER NOT NULL'
case 'FLOAT32':
return 'FLOAT32 NOT NULL'
case 'FLOAT64':
return 'FLOAT NOT NULL'
default:
throw newError(`Cannot stringify vector with unsupported type. Got type: ${type as string}`)
}
}

Object.defineProperty(Vector.prototype, VECTOR_IDENTIFIER_PROPERTY, {
value: true,
enumerable: false,
configurable: false,
writable: false
})

/**
* Cast a TypedArray to a {@link Vector}
* @access public
Expand All @@ -98,3 +130,12 @@ export default class Vector<K extends Float32Array | Float64Array | Int8Array |
export function vector<K extends Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | BigInt64Array> (typedArray: K): Vector<K> {
return new Vector(typedArray)
}

/**
* Test if given object is an instance of the {@link Vector} class.
* @param {Object} obj the object to test.
* @return {boolean} `true` if given object is a {@link Vector}, `false` otherwise.
*/
export function isVector<K extends Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | BigInt64Array> (obj: any): obj is Vector<K> {
return obj != null && obj[VECTOR_IDENTIFIER_PROPERTY] === true
}
16 changes: 16 additions & 0 deletions packages/core/test/vector-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,20 @@ describe('Vector', () => {
expect(vec.asTypedArray()).toEqual(typedArray)
})
})
describe('.toString()', () => {
it.each([
['Int8Array', Int8Array.from([1]), 'vector([1], 1, INTEGER8 NOT NULL)'],
['Int16Array', Int16Array.from([1, 2, 3]), 'vector([1, 2, 3], 3, INTEGER16 NOT NULL)'],
['Int32Array', Int32Array.from([3]), 'vector([3], 1, INTEGER32 NOT NULL)'],
['BigInt64Array', BigInt64Array.from([BigInt(0)]), 'vector([0], 1, INTEGER NOT NULL)'],
['Float32Array', Float32Array.from([0.5, 1.875]), 'vector([0.5, 1.875], 2, FLOAT32 NOT NULL)'],
['Float32Array with special values', Float32Array.from([-Infinity, Infinity, NaN, -NaN, 1.401298464324817e-45, 3.4028235e38]), 'vector([-Infinity, Infinity, NaN, NaN, 1.401298464324817e-45, 3.4028234663852886e+38], 6, FLOAT32 NOT NULL)'],
['Float64Array', Float64Array.from([2]), 'vector([2], 1, FLOAT NOT NULL)'],
['Float64Array with special values', Float64Array.from([-Infinity, Infinity, NaN, -NaN, 2.2250738585072014e-308, 1.7976931348623157e308]), 'vector([-Infinity, Infinity, NaN, NaN, 2.2250738585072014e-308, 1.7976931348623157e+308], 6, FLOAT NOT NULL)'],
['Float64Array of huge size', Float64Array.from([...Array(10000).keys()]), `vector([${[...Array(10000).keys()].join(', ')}], 10000, FLOAT NOT NULL)`]
])('should correctly stringify (%s)', (_, typedArray, expectedString) => {
const vec = vector(typedArray)
expect(vec.toString()).toEqual(expectedString)
})
})
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions packages/neo4j-driver-deno/lib/core/index.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions packages/neo4j-driver-deno/lib/core/vector.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/neo4j-driver/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
staticAuthTokenManager,
clientCertificateProviders,
resolveCertificateProvider,
isVector,
Vector,
VectorType,
vector
Expand Down Expand Up @@ -410,6 +411,7 @@ const forExport = {
notificationFilterDisabledCategory,
notificationFilterMinimumSeverityLevel,
clientCertificateProviders,
isVector,
vector
}

Expand Down Expand Up @@ -484,6 +486,7 @@ export {
notificationFilterDisabledClassification,
notificationFilterMinimumSeverityLevel,
clientCertificateProviders,
isVector,
vector,
Vector,
VectorType
Expand Down
7 changes: 5 additions & 2 deletions packages/neo4j-driver/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ import {
ClientCertificateProviders,
RotatingClientCertificateProvider,
clientCertificateProviders,
types as coreTypes
types as coreTypes,
isVector
} from 'neo4j-driver-core'
import {
AuthToken,
Expand Down Expand Up @@ -299,6 +300,7 @@ declare const forExport: {
notificationFilterMinimumSeverityLevel: typeof notificationFilterMinimumSeverityLevel
logging: typeof logging
clientCertificateProviders: typeof clientCertificateProviders
isVector: typeof isVector
}

export {
Expand Down Expand Up @@ -379,7 +381,8 @@ export {
notificationFilterDisabledClassification,
notificationFilterMinimumSeverityLevel,
logging,
clientCertificateProviders
clientCertificateProviders,
isVector
}

export type {
Expand Down