Minor optimization for binary vectors

This commit is contained in:
Sharad Chandran R 2024-07-23 17:04:07 +05:30
parent bc01781ec0
commit f31af6bdd0
5 changed files with 30 additions and 18 deletions

View File

@ -13,6 +13,8 @@ node-oracledb `v6.6.0 <https://github.com/oracle/node-oracledb/compare/v6.5.1...
Common Changes
++++++++++++++
#) Added support for binary vector datatype (Oracle Database 23ai feature)
#) Added support for Centralized Configuration Providers (Azure App
Configuration Store and OCI Object Storage). Node-oracledb extracts
configuration information from the supported provider and uses it to

View File

@ -73,7 +73,10 @@ class VectorDecoder extends BaseBuffer {
} else {
errors.throwErr(errors.ERR_VECTOR_FORMAT_NOT_SUPPORTED, vectorFormat);
}
if (flags & constants.TNS_VECTOR_FLAG_NORM)
// For binary vector, NORM will never be present but space for NORM is reserved to keep
// same header lengh across the different vector formats.
if (vectorFormat === constants.VECTOR_FORMAT_BINARY || flags & constants.TNS_VECTOR_FLAG_NORM)
this.skipBytes(8);
// parse data
@ -107,6 +110,8 @@ class VectorEncoder extends GrowableBuffer {
let writeFn = this.writeBinaryFloat.bind(this);
let numElements = value.length;
let vectorVersion = constants.TNS_VECTOR_VERSION_BASE;
let flags = constants.TNS_VECTOR_FLAG_NORMSRC
| constants.TNS_VECTOR_FLAG_NORM; // NORM is present and reserve space.
if (Array.isArray(value) || value instanceof Float64Array) {
vectorFormat = constants.VECTOR_FORMAT_FLOAT64;
@ -119,13 +124,10 @@ class VectorEncoder extends GrowableBuffer {
// The number of dimensions are assumed to be multiple of 8.
numElements = numElements * 8;
vectorVersion = constants.TNS_VECTOR_VERSION_WITH_BINARY;
flags = constants.TNS_VECTOR_FLAG_NORMSRC; // only space is reserved.
writeFn = this.writeUInt8.bind(this);
}
// Let server generate the norm (TNS_VECTOR_FLAG_NORMSRC)
const flags = constants.TNS_VECTOR_FLAG_NORM
| constants.TNS_VECTOR_FLAG_NORMSRC;
// write header
this.writeUInt8(constants.TNS_VECTOR_MAGIC_BYTE);
this.writeUInt8(vectorVersion);

View File

@ -1060,6 +1060,7 @@ module.exports = {
VECTOR_FORMAT_FLOAT32: constants.VECTOR_FORMAT_FLOAT32,
VECTOR_FORMAT_FLOAT64: constants.VECTOR_FORMAT_FLOAT64,
VECTOR_FORMAT_INT8: constants.VECTOR_FORMAT_INT8,
VECTOR_FORMAT_BINARY: constants.VECTOR_FORMAT_BINARY,
// database type aliases
BLOB: types.DB_TYPE_BLOB,

View File

@ -224,21 +224,26 @@ static bool njsJsonBuffer_populateNode(njsJsonBuffer *buf, dpiJsonNode *node,
NJS_CHECK_NAPI(env, napi_is_typedarray(env, value, &isTyped))
if (isTyped) {
NJS_CHECK_NAPI(env, napi_get_global(env, &global))
NJS_CHECK_NAPI(env, napi_get_named_property(env, global, "Uint8Array",
&uint8Val))
NJS_CHECK_NAPI(env, napi_get_named_property(env, uint8Val, "prototype",
&uint8Proto))
NJS_CHECK_NAPI(env, napi_get_prototype(env, value, &valProto))
// See if the value prototype matches with Uint8Array.
// The Buffer and JsonId return true for Uint8Array instance, so
// using prototype check to see if the value is instance of Uint8Array.
NJS_CHECK_NAPI(env, napi_strict_equals(env, uint8Proto, valProto,
&check))
NJS_CHECK_NAPI(env, napi_get_typedarray_info(env, value, &type,
NULL, NULL, NULL, NULL))
NULL, NULL, NULL, NULL))
if ((type == napi_float64_array) || (type == napi_float32_array)
|| (type == napi_int8_array) || (check)) {
|| (type == napi_int8_array)) {
check = true;
} else {
NJS_CHECK_NAPI(env, napi_get_named_property(env, global,
"Uint8Array", &uint8Val))
NJS_CHECK_NAPI(env, napi_get_named_property(env, uint8Val,
"prototype", &uint8Proto))
NJS_CHECK_NAPI(env, napi_get_prototype(env, value, &valProto))
// See if the value prototype matches with Uint8Array.
// The Buffer and JsonId return true for Uint8Array instance, so
// using prototype check to see if the value is
// instance of Uint8Array.
NJS_CHECK_NAPI(env, napi_strict_equals(env, uint8Proto, valProto,
&check))
}
if (check) {
node->oracleTypeNum = DPI_ORACLE_TYPE_VECTOR;
node->nativeTypeNum = DPI_NATIVE_TYPE_BYTES;
NJS_CHECK_NAPI(env, napi_call_function(env, global,

View File

@ -205,6 +205,7 @@ describe('244.dataTypeJson.js', function() {
KeyBinary: new Uint8Array([240, 120]),
keyBuf: Buffer.from("A Raw")
};
const jsonVal22 = { "key22": [new Uint8Array([20, 10])]};
const binds = [
[1, jsonVal1],
[2, jsonVal2],
@ -234,6 +235,7 @@ describe('244.dataTypeJson.js', function() {
}
if (await testsUtil.isVectorBinaryRunnable()) {
binds.push([21, jsonVal21]);
binds.push([22, jsonVal22]);
}
binds.forEach((element, index) => {
binds[index].push(connection.encodeOSON(element[1]));