Skip to content

Commit b45ff74

Browse files
authored
Add RDFVariant bindings for C, C++, and Java APIs (#2815)
1 parent 0f17b29 commit b45ff74

2 files changed

Lines changed: 35 additions & 42 deletions

File tree

src_cpp/node_util.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,71 +4,70 @@
44
#include "common/types/blob.h"
55
#include "common/types/value/nested.h"
66
#include "common/types/value/node.h"
7+
#include "common/types/value/rdf_variant.h"
78
#include "common/types/value/recursive_rel.h"
89
#include "common/types/value/rel.h"
910
#include "common/types/value/value.h"
1011

1112
Napi::Value ConvertRdfVariantToNapiObject(const Value& value, Napi::Env env) {
12-
auto typeVal = NestedVal::getChildVal(&value, 0);
13-
auto type = static_cast<LogicalTypeID>(typeVal->getValue<uint8_t>());
14-
auto blobData = NestedVal::getChildVal(&value, 1)->strVal.data();
13+
auto type = RdfVariant::getLogicalTypeID(&value);
1514
switch (type) {
1615
case LogicalTypeID::STRING: {
17-
auto str = NestedVal::getChildVal(&value, 1)->strVal;
16+
auto str = RdfVariant::getValue<std::string>(&value);
1817
return Napi::String::New(env, str.data(), str.size());
1918
}
2019
case LogicalTypeID::BLOB: {
21-
auto blobStr = Blob::getValue<blob_t>(blobData).value.getAsString();
20+
auto blobStr = RdfVariant::getValue<blob_t>(&value).value.getAsString();
2221
return Napi::Buffer<char>::Copy(env, blobStr.c_str(), blobStr.size());
2322
}
2423
case LogicalTypeID::INT64: {
25-
return Napi::Number::New(env, Blob::getValue<int64_t>(blobData));
24+
return Napi::Number::New(env, RdfVariant::getValue<int64_t>(&value));
2625
}
2726
case LogicalTypeID::INT32: {
28-
return Napi::Number::New(env, Blob::getValue<int32_t>(blobData));
27+
return Napi::Number::New(env, RdfVariant::getValue<int32_t>(&value));
2928
}
3029
case LogicalTypeID::INT16: {
31-
return Napi::Number::New(env, Blob::getValue<int16_t>(blobData));
30+
return Napi::Number::New(env, RdfVariant::getValue<int16_t>(&value));
3231
}
3332
case LogicalTypeID::INT8: {
34-
return Napi::Number::New(env, Blob::getValue<int8_t>(blobData));
33+
return Napi::Number::New(env, RdfVariant::getValue<int8_t>(&value));
3534
}
3635
case LogicalTypeID::UINT64: {
37-
return Napi::Number::New(env, Blob::getValue<uint64_t>(blobData));
36+
return Napi::Number::New(env, RdfVariant::getValue<uint64_t>(&value));
3837
}
3938
case LogicalTypeID::UINT32: {
40-
return Napi::Number::New(env, Blob::getValue<uint32_t>(blobData));
39+
return Napi::Number::New(env, RdfVariant::getValue<uint32_t>(&value));
4140
}
4241
case LogicalTypeID::UINT16: {
43-
return Napi::Number::New(env, Blob::getValue<uint16_t>(blobData));
42+
return Napi::Number::New(env, RdfVariant::getValue<uint16_t>(&value));
4443
}
4544
case LogicalTypeID::UINT8: {
46-
return Napi::Number::New(env, Blob::getValue<uint8_t>(blobData));
45+
return Napi::Number::New(env, RdfVariant::getValue<uint8_t>(&value));
4746
}
4847
case LogicalTypeID::DOUBLE: {
49-
return Napi::Number::New(env, Blob::getValue<double>(blobData));
48+
return Napi::Number::New(env, RdfVariant::getValue<double>(&value));
5049
}
5150
case LogicalTypeID::FLOAT: {
52-
return Napi::Number::New(env, Blob::getValue<float>(blobData));
51+
return Napi::Number::New(env, RdfVariant::getValue<float>(&value));
5352
}
5453
case LogicalTypeID::BOOL: {
55-
return Napi::Boolean::New(env, Blob::getValue<bool>(blobData));
54+
return Napi::Boolean::New(env, RdfVariant::getValue<bool>(&value));
5655
}
5756
case LogicalTypeID::DATE: {
58-
auto dateVal = Blob::getValue<date_t>(blobData);
57+
auto dateVal = RdfVariant::getValue<date_t>(&value);
5958
// Javascript Date type contains both date and time information. This returns the Date at
6059
// 00:00:00 in UTC timezone.
6160
auto milliseconds = Date::getEpochNanoSeconds(dateVal) / Interval::NANOS_PER_MICRO /
6261
Interval::MICROS_PER_MSEC;
6362
return Napi::Date::New(env, milliseconds);
6463
}
6564
case LogicalTypeID::TIMESTAMP: {
66-
auto timestampVal = Blob::getValue<timestamp_t>(blobData);
65+
auto timestampVal = RdfVariant::getValue<timestamp_t>(&value);
6766
auto milliseconds = timestampVal.value / Interval::MICROS_PER_MSEC;
6867
return Napi::Date::New(env, milliseconds);
6968
}
7069
case LogicalTypeID::INTERVAL: {
71-
auto intervalVal = Blob::getValue<interval_t>(blobData);
70+
auto intervalVal = RdfVariant::getValue<interval_t>(&value);
7271
// TODO: By default, Node.js returns the difference in milliseconds between two dates, so we
7372
// follow the convention here, but it might not be the best choice in terms of usability.
7473
auto microseconds = intervalVal.micros;

test/common.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,33 +51,27 @@ const initTests = async () => {
5151
}
5252

5353
await conn.query(
54-
"create node table moviesSerial (ID SERIAL, name STRING, length INT32, note STRING, PRIMARY KEY (ID))"
54+
"create node table moviesSerial (ID SERIAL, name STRING, length INT32, note STRING, PRIMARY KEY (ID))"
5555
);
5656
await conn.query(
57-
'copy moviesSerial from "../../dataset/tinysnb-serial/vMovies.csv"'
57+
'copy moviesSerial from "../../dataset/tinysnb-serial/vMovies.csv"'
5858
);
5959

60-
await conn.query("CREATE RDFGraph T;");
61-
await conn.query(
62-
`
63-
CREATE (:T_l {val:cast(12, "INT64")}),
64-
(:T_l {val:cast(43, "INT32")}),
65-
(:T_l {val:cast(33, "INT16")}),
66-
(:T_l {val:cast(2, "INT8")}),
67-
(:T_l {val:cast(90, "UINT64")}),
68-
(:T_l {val:cast(77, "UINT32")}),
69-
(:T_l {val:cast(12, "UINT16")}),
70-
(:T_l {val:cast(1, "UINT8")}),
71-
(:T_l {val:cast(4.4, "DOUBLE")}),
72-
(:T_l {val:cast(1.2, "FLOAT")}),
73-
(:T_l {val:true}),
74-
(:T_l {val:"hhh"}),
75-
(:T_l {val:cast("2024-01-01", "DATE")}),
76-
(:T_l {val:cast("2024-01-01 11:25:30Z+00:00", "TIMESTAMP")}),
77-
(:T_l {val:cast("2 day", "INTERVAL")}),
78-
(:T_l {val:cast("\\\\xB2", "BLOB")})
79-
`
80-
);
60+
const rdfSchema = await fs.readFile(
61+
"../../dataset/rdf/rdf_variant/schema.cypher"
62+
);
63+
const rdfCopy = await fs.readFile(
64+
"../../dataset/rdf/rdf_variant/copy.cypher"
65+
);
66+
for (const file of [rdfSchema, rdfCopy]) {
67+
const lines = file.toString().split("\n");
68+
for (const line of lines) {
69+
if (line.trim().length === 0) {
70+
continue;
71+
}
72+
await conn.query(line);
73+
}
74+
}
8175

8276
global.dbPath = dbPath;
8377
global.db = db;

0 commit comments

Comments
 (0)