Skip to content

Commit 41d4f6e

Browse files
committed
chore: codefmt
1 parent 5c79f87 commit 41d4f6e

File tree

6 files changed

+169
-34
lines changed

6 files changed

+169
-34
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ target/
1616
/.obsidian
1717
.DS_Store
1818

19-
/hello_world
20-
/transaction
19+
/example_data
2120

2221
kitesql_data
2322
kitesql_bench

examples/hello_world.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ implement_from_tuple!(
2828

2929
#[cfg(feature = "macros")]
3030
fn main() -> Result<(), DatabaseError> {
31-
let database = DataBaseBuilder::path("./hello_world").build()?;
31+
let database = DataBaseBuilder::path("./example_data/hello_world").build()?;
3232

3333
// 1) Create table and insert multiple rows with mixed types.
3434
database

examples/transaction.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,34 @@
22

33
use kite_sql::db::{DataBaseBuilder, ResultIter};
44
use kite_sql::errors::DatabaseError;
5+
use kite_sql::types::tuple::Tuple;
6+
use kite_sql::types::value::DataValue;
57

68
fn main() -> Result<(), DatabaseError> {
7-
let database = DataBaseBuilder::path("./transaction").build_optimistic()?;
8-
let mut transaction = database.new_transaction()?;
9-
10-
// Scenario: create table and insert rows inside a transaction; visible after commit.
11-
transaction
9+
let database = DataBaseBuilder::path("./example_data/transaction").build_optimistic()?;
10+
database
1211
.run("create table if not exists t1 (c1 int primary key, c2 int)")?
1312
.done()?;
13+
let mut transaction = database.new_transaction()?;
14+
1415
transaction
1516
.run("insert into t1 values(0, 0), (1, 1)")?
1617
.done()?;
1718

18-
assert!(database.run("select * from t1").is_err());
19+
assert!(database.run("select * from t1")?.next().is_none());
1920

2021
transaction.commit()?;
2122

22-
assert!(database.run("select * from t1").is_ok());
23+
let mut iter = database.run("select * from t1")?;
24+
assert_eq!(
25+
iter.next().unwrap()?,
26+
Tuple::new(None, vec![DataValue::Int32(0), DataValue::Int32(0)])
27+
);
28+
assert_eq!(
29+
iter.next().unwrap()?,
30+
Tuple::new(None, vec![DataValue::Int32(1), DataValue::Int32(1)])
31+
);
32+
assert!(iter.next().is_none());
2333

2434
// Scenario: another transaction updates but does not commit; changes stay invisible.
2535
let mut tx2 = database.new_transaction()?;

examples/wasm_index_usage.test.mjs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Verify index usage in wasm build: create table, build indexes, analyze, and query with filters.
22
import assert from "node:assert/strict";
33
import { createRequire } from "module";
4-
import fs from "node:fs";
54

65
// Minimal localStorage polyfill for wasm code paths (statistics persistence).
76
class LocalStorage {
@@ -36,22 +35,18 @@ global.window = windowShim;
3635
const require = createRequire(import.meta.url);
3736
const { WasmDatabase } = require("../pkg/kite_sql.js");
3837

39-
// Load the shared test dataset for richer coverage.
40-
function loadTestData() {
41-
const csvPath = new URL("../tests/data/row_20000.csv", import.meta.url);
42-
return fs.readFileSync(csvPath, "utf8").trim();
43-
}
44-
4538
async function main() {
4639
const db = new WasmDatabase();
4740

4841
await db.execute("drop table if exists t1");
4942
await db.execute("create table t1(id int primary key, c1 int, c2 int)");
5043

51-
// Insert data in bulk
52-
const data = loadTestData();
53-
for (const line of data.split("\n")) {
54-
const [id, c1, c2] = line.split("|").map((v) => parseInt(v, 10));
44+
// Insert data in bulk (20k rows) without reading from disk.
45+
// Each row matches the old CSV pattern: id = i*3, c1 = i*3+1, c2 = i*3+2.
46+
for (let i = 0; i < 20_000; i += 1) {
47+
const id = i * 3;
48+
const c1 = id + 1;
49+
const c2 = id + 2;
5550
await db.execute(`insert into t1 values(${id}, ${c1}, ${c2})`);
5651
}
5752

src/db.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ impl DataBaseBuilder {
103103
Self::_build::<RocksStorage>(storage, self.scala_functions, self.table_functions)
104104
}
105105

106-
#[cfg(not(target_arch = "wasm32"))]
107106
pub fn build_in_memory(self) -> Result<Database<MemoryStorage>, DatabaseError> {
108107
let storage = MemoryStorage::new();
109108

src/storage/memory.rs

Lines changed: 144 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,153 @@ impl Transaction for MemoryTransaction {
9999
}
100100
}
101101

102-
#[cfg(test)]
103-
mod test {
102+
#[cfg(all(test, target_arch = "wasm32"))]
103+
mod wasm_tests {
104+
use super::*;
105+
use crate::catalog::{ColumnCatalog, ColumnDesc, ColumnRef, TableName};
106+
use crate::db::{DataBaseBuilder, ResultIter};
107+
use crate::expression::range_detacher::Range;
104108
use crate::storage::Iter;
109+
use crate::types::tuple::Tuple;
110+
use crate::types::value::DataValue;
111+
use crate::types::LogicalType;
112+
use crate::utils::lru::SharedLruCache;
113+
use itertools::Itertools;
105114
use std::collections::{BTreeMap, Bound};
106115
use std::hash::RandomState;
107116
use std::sync::Arc;
108-
use itertools::Itertools;
117+
use wasm_bindgen_test::*;
118+
119+
#[wasm_bindgen_test]
120+
fn memory_storage_roundtrip() -> Result<(), DatabaseError> {
121+
let storage = MemoryStorage::new();
122+
let mut transaction = storage.transaction()?;
123+
let table_cache = Arc::new(SharedLruCache::new(4, 1, RandomState::new())?);
124+
let columns = Arc::new(vec![
125+
ColumnRef::from(ColumnCatalog::new(
126+
"c1".to_string(),
127+
false,
128+
ColumnDesc::new(LogicalType::Integer, Some(0), false, None).unwrap(),
129+
)),
130+
ColumnRef::from(ColumnCatalog::new(
131+
"c2".to_string(),
132+
false,
133+
ColumnDesc::new(LogicalType::Boolean, None, false, None).unwrap(),
134+
)),
135+
]);
136+
137+
let source_columns = columns
138+
.iter()
139+
.map(|col_ref| ColumnCatalog::clone(col_ref))
140+
.collect_vec();
141+
transaction.create_table(
142+
&table_cache,
143+
"test".to_string().into(),
144+
source_columns,
145+
false,
146+
)?;
147+
148+
transaction.append_tuple(
149+
&"test".to_string(),
150+
Tuple::new(
151+
Some(DataValue::Int32(1)),
152+
vec![DataValue::Int32(1), DataValue::Boolean(true)],
153+
),
154+
&[
155+
LogicalType::Integer.serializable(),
156+
LogicalType::Boolean.serializable(),
157+
],
158+
false,
159+
)?;
160+
transaction.append_tuple(
161+
&"test".to_string(),
162+
Tuple::new(
163+
Some(DataValue::Int32(2)),
164+
vec![DataValue::Int32(2), DataValue::Boolean(true)],
165+
),
166+
&[
167+
LogicalType::Integer.serializable(),
168+
LogicalType::Boolean.serializable(),
169+
],
170+
false,
171+
)?;
172+
173+
let mut read_columns = BTreeMap::new();
174+
read_columns.insert(0, columns[0].clone());
175+
176+
let mut iter = transaction.read(
177+
&table_cache,
178+
"test".to_string().into(),
179+
(Some(1), Some(1)),
180+
read_columns,
181+
true,
182+
)?;
183+
184+
let option_1 = iter.next_tuple()?;
185+
assert_eq!(option_1.unwrap().pk, Some(DataValue::Int32(2)));
186+
187+
let option_2 = iter.next_tuple()?;
188+
assert_eq!(option_2, None);
189+
190+
Ok(())
191+
}
192+
193+
#[wasm_bindgen_test]
194+
fn memory_storage_read_by_index() -> Result<(), DatabaseError> {
195+
let kite_sql = DataBaseBuilder::path("./memory").build_in_memory()?;
196+
kite_sql
197+
.run("create table t1 (a int primary key, b int)")?
198+
.done()?;
199+
kite_sql
200+
.run("insert into t1 (a, b) values (0, 0), (1, 1), (2, 2), (3, 4)")?
201+
.done()?;
202+
203+
let transaction = kite_sql.storage.transaction()?;
204+
let table_name: TableName = "t1".to_string().into();
205+
let table = transaction
206+
.table(kite_sql.state.table_cache(), table_name.clone())?
207+
.unwrap()
208+
.clone();
209+
let pk_index = table.indexes().next().unwrap().clone();
210+
let mut iter = transaction.read_by_index(
211+
kite_sql.state.table_cache(),
212+
table_name,
213+
(Some(0), None),
214+
table.columns().cloned().enumerate().collect(),
215+
pk_index,
216+
vec![Range::Scope {
217+
min: Bound::Excluded(DataValue::Int32(0)),
218+
max: Bound::Included(DataValue::Int32(2)),
219+
}],
220+
true,
221+
)?;
222+
223+
let mut result = Vec::new();
224+
while let Some(tuple) = iter.next_tuple()? {
225+
result.push(tuple.pk.unwrap());
226+
}
227+
228+
assert_eq!(result, vec![DataValue::Int32(1), DataValue::Int32(2)]);
229+
230+
Ok(())
231+
}
232+
}
233+
234+
#[cfg(all(test, not(target_arch = "wasm32")))]
235+
mod native_tests {
236+
use super::*;
109237
use crate::catalog::{ColumnCatalog, ColumnDesc, ColumnRef, TableName};
110238
use crate::db::{DataBaseBuilder, ResultIter};
111-
use crate::errors::DatabaseError;
112239
use crate::expression::range_detacher::Range;
113-
use crate::storage::memory::MemoryStorage;
114-
use crate::storage::{Storage, Transaction};
115-
use crate::types::LogicalType;
240+
use crate::storage::Iter;
116241
use crate::types::tuple::Tuple;
117242
use crate::types::value::DataValue;
243+
use crate::types::LogicalType;
118244
use crate::utils::lru::SharedLruCache;
245+
use itertools::Itertools;
246+
use std::collections::{BTreeMap, Bound};
247+
use std::hash::RandomState;
248+
use std::sync::Arc;
119249

120250
#[test]
121251
fn memory_storage_roundtrip() -> Result<(), DatabaseError> {
@@ -139,7 +269,12 @@ mod test {
139269
.iter()
140270
.map(|col_ref| ColumnCatalog::clone(col_ref))
141271
.collect_vec();
142-
transaction.create_table(&table_cache, "test".to_string().into(), source_columns, false)?;
272+
transaction.create_table(
273+
&table_cache,
274+
"test".to_string().into(),
275+
source_columns,
276+
false,
277+
)?;
143278

144279
transaction.append_tuple(
145280
&"test".to_string(),
@@ -221,10 +356,7 @@ mod test {
221356
result.push(tuple.pk.unwrap());
222357
}
223358

224-
assert_eq!(
225-
result,
226-
vec![DataValue::Int32(1), DataValue::Int32(2)]
227-
);
359+
assert_eq!(result, vec![DataValue::Int32(1), DataValue::Int32(2)]);
228360

229361
Ok(())
230362
}

0 commit comments

Comments
 (0)