Skip to content

Commit 96c7e40

Browse files
committed
Tuple preformance and functionality improvements
Removed limit of max 16 elements in tuple. Tuple is now storing all values in JavaScript. During serialization from Rust to JavaScript, the tuple is converted to a JavaScript tuple.
1 parent ef0dd0d commit 96c7e40

File tree

4 files changed

+10
-54
lines changed

4 files changed

+10
-54
lines changed

lib/types/tuple.js

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,44 +18,23 @@ const resultWrapper = require("./results-wrapper");
1818
* - If the tuple comes from a ScyllaDB query result, its values remain in Rust memory, and access requires a native call.
1919
*/
2020
class Tuple {
21-
/**
22-
* Rust tuple object if stored in Rust part of the driver.
23-
* @type {rust.TupleWrapper?}
24-
* @private
25-
*/
26-
#internal;
27-
2821
/**
2922
* Elements of the tuple if stored in JS.
3023
* @type {Array?}
3124
* @private
3225
*/
33-
#jsElements;
34-
35-
/**
36-
* True if the tuple is stored in JS, false if it's stored in Rust part of the driver.
37-
* @type {Boolean}
38-
* @private
39-
*/
40-
#isJsStored;
26+
#elements;
4127

4228
/**
4329
* Creates a new instance of Tuple.
4430
* @param {...any} args
4531
*/
4632
constructor(...args) {
47-
this.#isJsStored = true;
48-
this.#internal = null;
49-
this.#jsElements = args;
33+
this.#elements = args;
5034

5135
if (this.elements.length === 0) {
5236
throw new TypeError("Tuple must contain at least one value");
5337
}
54-
55-
// This is Rust driver limitation.
56-
if (this.elements.length > 16) {
57-
throw new TypeError("Tuple must contain at most 16 values");
58-
}
5938
}
6039

6140
/**
@@ -64,8 +43,7 @@ class Tuple {
6443
* @type Array
6544
*/
6645
get elements() {
67-
if (this.#isJsStored) return this.#jsElements;
68-
return this.#internal.getAll();
46+
return this.#elements;
6947
}
7048

7149
set elements(_) {
@@ -78,8 +56,7 @@ class Tuple {
7856
* @type Number
7957
*/
8058
get length() {
81-
if (this.#isJsStored) return this.#jsElements.length;
82-
return this.#internal.getLength();
59+
return this.#elements.length;
8360
}
8461

8562
set length(_) {
@@ -101,8 +78,7 @@ class Tuple {
10178
* @param {Number} index Element index
10279
*/
10380
get(index) {
104-
if (this.#isJsStored) return this.#jsElements[index || 0];
105-
return resultWrapper.getCqlObject(this.#internal.get(index || 0));
81+
return this.#elements[index || 0];
10682
}
10783

10884
/**
@@ -143,9 +119,10 @@ class Tuple {
143119
*/
144120
static fromRust(rustTuple) {
145121
let jsTuple = new Tuple(null);
146-
jsTuple.#internal = rustTuple;
147-
jsTuple.#isJsStored = false;
148-
jsTuple.#jsElements = null;
122+
jsTuple.#elements = rustTuple
123+
.getAll()
124+
.map((e) => resultWrapper.getCqlObject(e))
125+
.map((e) => (e == null ? undefined : e));
149126
return jsTuple;
150127
}
151128
}

src/types/tuple.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,6 @@ impl TupleWrapper {
1515
Ok(TupleWrapper { values: None })
1616
}
1717

18-
#[napi]
19-
pub fn get_length(&self) -> u32 {
20-
self.values.as_ref().map_or(0, |v| v.len() as u32)
21-
}
22-
23-
#[napi]
24-
pub fn get(&self, index: u32) -> napi::Result<Option<CqlValueWrapper>> {
25-
self.values
26-
.as_ref()
27-
.ok_or_else(|| js_error("Values are stored in JS"))
28-
.and_then(|values| {
29-
values
30-
.get(index as usize)
31-
.map(|v| v.as_ref().map(|v| CqlValueWrapper { inner: v.clone() }))
32-
.ok_or_else(|| js_error("Index out of bounds"))
33-
})
34-
}
35-
3618
#[napi]
3719
pub fn get_all(&self) -> napi::Result<Vec<Option<CqlValueWrapper>>> {
3820
self.values

test/unit/cql-value-wrapper-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ describe("Cql value wrapper", function () {
248248
console.log(value.get(0));
249249
assert.strictEqual(value.get(0), "some text");
250250
assert.strictEqual(value.get(1), 1);
251-
assert.strictEqual(value.get(2), null);
251+
assert.strictEqual(value.get(2), undefined);
252252
});
253253

254254
it("should get time uuid type correctly from napi", function () {

test/unit/tuple-tests.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ describe("Tuple", function () {
88
it("should throw an error when no arguments are provided", function () {
99
assert.throws(() => new Tuple(), TypeError);
1010
});
11-
it("should throw an error when more than 16 arguments are provided", function () {
12-
assert.throws(() => new Tuple(...Array(17).fill(0)), TypeError);
13-
});
1411
});
1512
describe("#get()", function () {
1613
it("should return the element at position", function () {

0 commit comments

Comments
 (0)