1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
//! NodeGroup — a fixed-size collection of ColumnChunks (one per column).
//!
//! A `NodeGroup` holds up to `NODE_GROUP_SIZE` rows of data across all
//! columns of a table. When the group is full it can be flushed to a set
//! of persistent `Column` instances. This mirrors the C++ Akar
//! `ChunkedNodeGroup` / `NodeGroup` concept.
//!
//! # Row → Column mapping
//!
//! Each row is a `Vec<Value>` with one element per column. The NodeGroup
//! distributes values column-wise: `columns[col_idx].append(values[col_idx])`.
use crate::column::Column;
use crate::column_chunk::{ColumnChunk, NODE_GROUP_SIZE};
use crate::spiller::{MultiWayStreamMerge, SpillFile, Spiller};
use crate::version_info::VersionInfo;
use akar_common::error::StorageError;
use akar_common::types::Value;
use std::sync::Arc;
/// A node group stores up to `NODE_GROUP_SIZE` rows in columnar format.
///
/// `start_offset` is the global row index within the owning table where
/// this group's data begins. `num_nodes` counts how many rows have been
/// appended so far (≤ `NODE_GROUP_SIZE`).
///
/// `version_info` tracks MVCC insert/delete visibility for concurrent
/// writers. It is `None` for single-writer mode (backward compat).
///
/// # Disk Spilling
///
/// When `spiller` is set and the memory threshold is exceeded, the group
/// automatically spills its contents to temp files during `append_row()`.
/// After ingestion is complete, call `flush_with_spiller()` instead of
/// `flush()` to merge all spills + final in-memory data into the columns.
#[derive(Debug, Clone)]
pub struct NodeGroup {
/// One in-memory ColumnChunk per column of the table.
pub columns: Vec<ColumnChunk>,
/// Global row offset within the owning table.
pub start_offset: u64,
/// Number of rows currently stored in this group.
pub num_nodes: u64,
/// Optional MVCC version tracker for this group.
pub version_info: Option<VersionInfo>,
/// Optional spiller for disk-based memory management.
spiller: Option<Arc<Spiller>>,
/// List of spill files created during append operations.
spill_files: Vec<SpillFile>,
}
impl NodeGroup {
/// Create a new empty node group for `num_columns` columns.
///
/// All columns start with the default `NODE_GROUP_SIZE` capacity.
/// `start_offset` is the global row index in the owning table where
/// this group begins.
pub fn new(num_columns: usize, start_offset: u64) -> Self {
let columns = (0..num_columns).map(|_| ColumnChunk::new()).collect();
Self {
columns,
start_offset,
num_nodes: 0,
version_info: None,
spiller: None,
spill_files: Vec::new(),
}
}
/// Create a new node group with a custom chunk capacity per column.
pub fn with_capacity(num_columns: usize, start_offset: u64, capacity: usize) -> Self {
let columns = (0..num_columns).map(|_| ColumnChunk::with_capacity(capacity)).collect();
Self {
columns,
start_offset,
num_nodes: 0,
version_info: None,
spiller: None,
spill_files: Vec::new(),
}
}
/// Attach a spiller to this node group for disk-based memory management.
///
/// When a spiller is attached, `append_row()` automatically spills the
/// current buffer to disk when the memory threshold is exceeded, then
/// continues appending. Call `flush_with_spiller()` instead of `flush()`
/// to merge all spill files + final in-memory data.
pub fn with_spiller(mut self, spiller: Arc<Spiller>) -> Self {
self.spiller = Some(spiller);
self
}
/// Set the spiller on an existing node group.
pub fn set_spiller(&mut self, spiller: Arc<Spiller>) {
self.spiller = Some(spiller);
}
/// Enable MVCC version tracking for this node group.
/// Must be called before any inserts if concurrent writes are expected.
pub fn enable_version_info(&mut self) {
if self.version_info.is_none() {
self.version_info = Some(VersionInfo::new(NODE_GROUP_SIZE));
}
}
// ------------------------------------------------------------------
// Public API
// ------------------------------------------------------------------
/// Append a single row (one value per column) to the group.
///
/// Returns an error if the number of values does not match the number
/// of columns, or if the group is already full.
///
/// If `txn_id` is `Some(...)`, the insert is recorded in the version
/// info for MVCC visibility tracking.
pub fn append_row(&mut self, row: Vec<Value>) -> Result<(), StorageError> {
self.append_row_with_txn(row, None)
}
/// Append a row with an optional transaction ID for MVCC tracking.
///
/// If a spiller is attached and the in-memory data exceeds the configured
/// memory threshold, the current buffer is automatically spilled to disk
/// before appending the new row. This keeps memory usage bounded during
/// large batch operations like `COPY FROM`.
pub fn append_row_with_txn(&mut self, row: Vec<Value>, txn_id: Option<u64>) -> Result<(), StorageError> {
if row.len() != self.columns.len() {
return Err(StorageError::Page(format!(
"column count mismatch: expected {} values, got {}",
self.columns.len(),
row.len()
)));
}
if self.is_full() {
return Err(StorageError::Page("node group is already full".to_string()));
}
// Auto-spill if the memory threshold is exceeded
if let Some(ref spiller) = self.spiller
&& !self.columns.is_empty()
&& spiller.should_spill(&self.columns[0])
{
self.spill_and_clear()?;
}
for (col_idx, value) in row.into_iter().enumerate() {
self.columns[col_idx].append(value);
}
// Record insert in version info if MVCC tracking is enabled
if let Some(ref vi) = self.version_info
&& let Some(txn) = txn_id
{
vi.insert(txn, self.num_nodes as u32);
}
self.num_nodes += 1;
Ok(())
}
/// Spill all column chunks to disk and reset the group to empty.
///
/// The spill file is tracked so that `flush_with_spiller()` can later
/// merge all spilled data back into the persistent columns.
pub fn spill_and_clear(&mut self) -> Result<(), StorageError> {
let spiller = self
.spiller
.as_ref()
.ok_or_else(|| StorageError::Spiller("No spiller attached to NodeGroup".to_string()))?;
if self.is_empty() {
return Ok(());
}
let spill = spiller.spill_columns(&mut self.columns)?;
if let Some(sf) = spill {
self.spill_files.push(sf);
}
self.num_nodes = 0;
Ok(())
}
/// Restore all spilled rows back into the in-memory columns.
///
/// Merges every tracked spill file (in creation order) followed by the
/// rows appended since the last spill, so the group's columns again hold
/// the complete row set. Spill files are cleaned up on success. This is
/// the ingest-time counterpart of `flush_with_spiller()`: it keeps the
/// in-memory node group authoritative for scans and the column mirror
/// after a memory-bounded bulk ingest (P51.44).
pub fn restore_spilled(&mut self) -> Result<(), StorageError> {
if self.spill_files.is_empty() {
return Ok(());
}
let spiller = self
.spiller
.clone()
.ok_or_else(|| StorageError::Spiller("No spiller attached to NodeGroup".to_string()))?;
let num_cols = self.columns.len();
let mut rows: Vec<Vec<Value>> = Vec::new();
let files = std::mem::take(&mut self.spill_files);
for sf in &files {
let chunks = spiller.restore_columns(sf, num_cols)?;
let n = chunks.first().map(|c| c.num_values()).unwrap_or(0);
for r in 0..n {
let mut row = Vec::with_capacity(num_cols);
for c in &chunks {
row.push(c.get(r).cloned().unwrap_or(Value::Null));
}
rows.push(row);
}
}
for row in self.scan() {
rows.push(row);
}
let mut columns: Vec<ColumnChunk> = (0..num_cols).map(|_| ColumnChunk::new()).collect();
for row in &rows {
for (ci, value) in row.iter().enumerate() {
columns[ci].append(value.clone());
}
}
self.columns = columns;
self.num_nodes = rows.len() as u64;
for sf in &files {
let _ = spiller.cleanup(sf);
}
Ok(())
}
/// Flush all data to persistent columns, merging any spilled data.
///
/// This is the spill-aware alternative to `flush()`. It merges all
/// previously spilled files + the current in-memory buffer into the
/// target columns using a streaming merge. If no spilling occurred,
/// this falls back to the regular `flush()`.
///
/// The optional `sort_key_column` is the column index to use for
/// merge ordering and PK deduplication. Pass `None` for unordered
/// append (no dedup).
pub fn flush_with_spiller(
&mut self,
columns: &mut [Column],
sort_key_column: Option<usize>,
dedup: bool,
) -> std::io::Result<usize> {
if self.spill_files.is_empty() {
// No spilling occurred — regular flush
return self.flush(columns);
}
assert_eq!(
columns.len(),
self.columns.len(),
"NodeGroup::flush_with_spiller: column count mismatch"
);
// Capture in-memory rows before clearing
let in_memory_rows = self.scan();
self.clear();
// Build the merger
let sort_col = sort_key_column.unwrap_or(0);
let mut merger = MultiWayStreamMerge::new(&self.spill_files, Some(in_memory_rows), sort_col, dedup)
.map_err(std::io::Error::other)?;
// Stream all merged rows into the target columns
let mut total: usize = 0;
while let Some(row) = merger.next_tuple() {
for (col_idx, value) in row.into_iter().enumerate() {
if col_idx < columns.len() {
columns[col_idx].append_value(&value)?;
}
}
total += 1;
}
// Clean up spill files
if let Some(ref spiller) = self.spiller {
let files = std::mem::take(&mut self.spill_files);
for sf in &files {
let _ = spiller.cleanup(sf);
}
}
Ok(total)
}
/// Whether the group has reached capacity.
pub fn is_full(&self) -> bool {
self.num_nodes as usize >= NODE_GROUP_SIZE
}
/// Whether the group is empty.
pub fn is_empty(&self) -> bool {
self.num_nodes == 0
}
/// Number of columns in this group.
pub fn num_columns(&self) -> usize {
self.columns.len()
}
/// Whether any spill files are still pending merge-back into memory.
pub fn has_spill_files(&self) -> bool {
!self.spill_files.is_empty()
}
/// Remaining capacity (number of additional rows that can be appended).
pub fn remaining(&self) -> usize {
NODE_GROUP_SIZE.saturating_sub(self.num_nodes as usize)
}
/// Flush all buffered data to persistent `Column` instances.
///
/// Each `ColumnChunk` is flushed to the corresponding `Column` in the
/// slice via `flush_to_column()`. After flushing, the chunks are
/// cleared and ready for reuse.
///
/// Returns the total number of rows flushed.
///
/// # Panics
///
/// Panics if `columns.len() != self.columns.len()`.
pub fn flush(&mut self, columns: &mut [Column]) -> std::io::Result<usize> {
assert_eq!(
columns.len(),
self.columns.len(),
"NodeGroup::flush: column count mismatch"
);
let mut total = 0;
for (chunk, col) in self.columns.iter_mut().zip(columns.iter_mut()) {
let n = chunk.flush_to_column(col)?;
// All chunks should flush the same number of values.
if total == 0 {
total = n;
}
debug_assert!(n == 0 || n == total, "inconsistent flush count");
}
self.num_nodes = 0;
Ok(total)
}
/// Flush data to columns but keep the in-memory buffer intact.
pub fn flush_copy(&self, columns: &mut [Column]) -> std::io::Result<usize> {
assert_eq!(
columns.len(),
self.columns.len(),
"NodeGroup::flush_copy: column count mismatch"
);
let mut total = 0;
for (chunk, col) in self.columns.iter().zip(columns.iter_mut()) {
let n = chunk.flush_copy_to_column(col)?;
if total == 0 {
total = n;
}
}
Ok(total)
}
/// Scan all rows currently buffered in the group.
///
/// Returns a `Vec<Vec<Value>>` where `result[row][col]` is the value
/// at the given row and column.
pub fn scan(&self) -> Vec<Vec<Value>> {
let n_rows = self.num_nodes as usize;
let n_cols = self.columns.len();
let mut result = Vec::with_capacity(n_rows);
for row in 0..n_rows {
let mut row_data = Vec::with_capacity(n_cols);
for chunk in &self.columns {
match chunk.get(row) {
Some(v) => row_data.push(v.clone()),
None => row_data.push(Value::Null),
}
}
result.push(row_data);
}
result
}
/// Scan a range of buffered rows `[start, start + count)`.
///
/// Returns `Vec<Vec<Value>>` in row-major order.
pub fn scan_range(&self, start: usize, count: usize) -> Vec<Vec<Value>> {
let end = (start + count).min(self.num_nodes as usize);
if start >= end {
return Vec::new();
}
let n_cols = self.columns.len();
let mut result = Vec::with_capacity(end - start);
for row in start..end {
let mut row_data = Vec::with_capacity(n_cols);
for chunk in &self.columns {
match chunk.get(row) {
Some(v) => row_data.push(v.clone()),
None => row_data.push(Value::Null),
}
}
result.push(row_data);
}
result
}
/// Access a single value at the given local row and column index.
pub fn get_value(&self, local_row: usize, col_idx: usize) -> Option<&Value> {
self.columns.get(col_idx).and_then(|chunk| chunk.get(local_row))
}
/// Access a single value with MVCC snapshot isolation.
///
/// Checks `VersionInfo` for insert/delete visibility first. If the row
/// is not visible at `snapshot_ts`, returns `None`. Then checks
/// `UpdateInfo` version chain on the column chunk for versioned updates.
pub fn get_value_with_snapshot(
&self,
local_row: usize,
col_idx: usize,
snapshot_ts: Option<u64>,
commit_history: &[(u64, u64)],
) -> Option<&Value> {
// Check version info visibility (inserts/deletes)
if let Some(ts) = snapshot_ts
&& !self.is_row_visible(local_row, ts, commit_history)
{
return None;
}
// Get value with update version chain check
self.columns
.get(col_idx)
.and_then(|chunk| chunk.get_value_with_snapshot(local_row, snapshot_ts, commit_history))
}
/// Access a single value with MVCC snapshot isolation (owned variant).
///
/// Like `get_value_with_snapshot` but returns `Option<Value>` instead of
/// `Option<&Value>`, enabling proper version chain traversal with
/// deserialized old values from `UpdateInfo`.
pub fn get_value_owned_with_snapshot(
&self,
local_row: usize,
col_idx: usize,
snapshot_ts: Option<u64>,
commit_history: &[(u64, u64)],
) -> Option<Value> {
// Check version info visibility (inserts/deletes)
if let Some(ts) = snapshot_ts
&& !self.is_row_visible(local_row, ts, commit_history)
{
return None;
}
// Get value with update version chain check (owned)
self.columns
.get(col_idx)
.and_then(|chunk| chunk.get_value_owned_with_snapshot(local_row, snapshot_ts, commit_history))
}
/// Check whether a row is visible at the given snapshot timestamp.
/// Returns `true` if no version tracking is active (backward compat).
pub fn is_row_visible(&self, local_row: usize, snapshot_ts: u64, commit_history: &[(u64, u64)]) -> bool {
match &self.version_info {
Some(vi) => vi.is_visible(local_row as u32, snapshot_ts, commit_history),
None => true, // No version tracking → always visible
}
}
/// Reset the group to empty without flushing.
pub fn clear(&mut self) {
for chunk in &mut self.columns {
chunk.clear();
}
self.num_nodes = 0;
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
use crate::buffer_manager::BufferManagerConfig;
use crate::page::DEFAULT_PAGE_SIZE;
use akar_common::memory::MemoryManager;
use akar_common::types::LogicalTypeID;
use std::sync::{Arc, Mutex};
fn setup_columns(num_cols: usize, db_path: &std::path::Path) -> Vec<Column> {
let mm = Arc::new(MemoryManager::new(64 * 1024 * 1024));
let config = BufferManagerConfig::default();
let bm = Arc::new(Mutex::new(crate::buffer_manager::BufferManager::new(
db_path.to_path_buf(),
mm,
config,
)));
(0..num_cols)
.map(|i| {
Column::new(
LogicalTypeID::Int64,
0,
i as u32,
db_path,
bm.clone(),
DEFAULT_PAGE_SIZE,
)
})
.collect()
}
#[test]
fn test_empty_group() {
let group = NodeGroup::new(3, 0);
assert_eq!(group.num_columns(), 3);
assert_eq!(group.num_nodes, 0);
assert!(group.is_empty());
assert!(!group.is_full());
assert_eq!(group.start_offset, 0);
}
#[test]
fn test_append_row() {
let mut group = NodeGroup::new(2, 100);
group.append_row(vec![Value::Int64(1), Value::Int64(2)]).unwrap();
assert_eq!(group.num_nodes, 1);
assert!(!group.is_empty());
group.append_row(vec![Value::Int64(3), Value::Int64(4)]).unwrap();
assert_eq!(group.num_nodes, 2);
}
#[test]
fn test_append_wrong_column_count() {
let mut group = NodeGroup::new(2, 0);
let result = group.append_row(vec![Value::Int64(1)]);
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("column count mismatch"));
}
#[test]
fn test_append_when_full() {
// NodeGroup fullness is based on NODE_GROUP_SIZE (4096).
// Test that is_full returns false when not full:
let mut group = NodeGroup::with_capacity(1, 0, 5);
for _ in 0..10 {
group.append_row(vec![Value::Int64(1)]).unwrap();
}
assert!(!group.is_full());
assert_eq!(group.num_nodes, 10);
}
#[test]
fn test_scan() {
let mut group = NodeGroup::new(3, 0);
group
.append_row(vec![Value::Int64(10), Value::Int64(20), Value::Int64(30)])
.unwrap();
group
.append_row(vec![Value::Int64(11), Value::Int64(21), Value::Int64(31)])
.unwrap();
let data = group.scan();
assert_eq!(data.len(), 2);
assert_eq!(data[0][0], Value::Int64(10));
assert_eq!(data[0][1], Value::Int64(20));
assert_eq!(data[1][2], Value::Int64(31));
}
#[test]
fn test_scan_range() {
let mut group = NodeGroup::new(2, 0);
for i in 0..10 {
group.append_row(vec![Value::Int64(i), Value::Int64(i * 10)]).unwrap();
}
let slice = group.scan_range(3, 4);
assert_eq!(slice.len(), 4);
assert_eq!(slice[0][0], Value::Int64(3));
assert_eq!(slice[3][0], Value::Int64(6));
}
#[test]
fn test_get_value() {
let mut group = NodeGroup::new(2, 50);
group.append_row(vec![Value::Int64(100), Value::Int64(200)]).unwrap();
assert_eq!(group.get_value(0, 0), Some(&Value::Int64(100)));
assert_eq!(group.get_value(0, 1), Some(&Value::Int64(200)));
assert_eq!(group.get_value(1, 0), None);
}
#[test]
fn test_flush_to_columns() {
let dir = tempfile::tempdir().unwrap();
let mut cols = setup_columns(2, dir.path());
let mut group = NodeGroup::new(2, 0);
for i in 0i64..50 {
group.append_row(vec![Value::Int64(i), Value::Int64(i * 10)]).unwrap();
}
let flushed = group.flush(&mut cols).unwrap();
assert_eq!(flushed, 50);
assert_eq!(group.num_nodes, 0);
assert!(group.is_empty());
// Verify data persisted in columns
for i in 0i64..50 {
assert_eq!(cols[0].get_value(i as u64).unwrap(), Value::Int64(i));
assert_eq!(cols[1].get_value(i as u64).unwrap(), Value::Int64(i * 10));
}
}
#[test]
fn test_flush_copy_preserves_buffer() {
let dir = tempfile::tempdir().unwrap();
let mut cols = setup_columns(2, dir.path());
let mut group = NodeGroup::new(2, 0);
group.append_row(vec![Value::Int64(1), Value::Int64(2)]).unwrap();
let flushed = group.flush_copy(&mut cols).unwrap();
assert_eq!(flushed, 1);
// Buffer should still be intact
assert_eq!(group.num_nodes, 1);
assert_eq!(cols[0].get_value(0).unwrap(), Value::Int64(1));
}
#[test]
fn test_restore_spilled_reconstructs_full_group() {
let dir = tempfile::tempdir().unwrap();
let spiller = Arc::new(crate::spiller::Spiller::new(dir.path(), 64));
let mut group = NodeGroup::new(2, 0);
group.set_spiller(spiller.clone());
// Append enough rows that the group spills mid-way, then more rows.
for i in 0i64..20 {
group.append_row(vec![Value::Int64(i), Value::Int64(i * 10)]).unwrap();
}
assert!(!group.spill_files.is_empty(), "low threshold must spill");
assert!(group.num_nodes < 20, "spill must have evicted rows from memory");
group.restore_spilled().unwrap();
assert_eq!(group.num_nodes, 20, "restore must recover the full row set");
assert!(group.spill_files.is_empty(), "spill files cleaned up after restore");
for i in 0i64..20 {
assert_eq!(group.get_value(i as usize, 0), Some(&Value::Int64(i)));
assert_eq!(group.get_value(i as usize, 1), Some(&Value::Int64(i * 10)));
}
}
#[test]
fn test_clear() {
let mut group = NodeGroup::new(2, 0);
group.append_row(vec![Value::Int64(1), Value::Int64(2)]).unwrap();
group.clear();
assert_eq!(group.num_nodes, 0);
assert!(group.is_empty());
}
#[test]
fn test_remaining() {
// `remaining()` is based on NODE_GROUP_SIZE (4096), not chunk capacity.
let mut group = NodeGroup::with_capacity(3, 0, 10);
assert_eq!(group.remaining(), NODE_GROUP_SIZE);
group
.append_row(vec![Value::Int64(1), Value::Int64(2), Value::Int64(3)])
.unwrap();
assert_eq!(group.remaining(), NODE_GROUP_SIZE - 1);
group
.append_row(vec![Value::Int64(4), Value::Int64(5), Value::Int64(6)])
.unwrap();
assert_eq!(group.remaining(), NODE_GROUP_SIZE - 2);
}
#[test]
fn test_start_offset() {
let group = NodeGroup::new(2, 12345);
assert_eq!(group.start_offset, 12345);
}
#[test]
fn test_multi_column_scan() {
let mut group = NodeGroup::new(4, 0);
group
.append_row(vec![
Value::String("Alice".into()),
Value::Int64(30),
Value::Double(1.65),
Value::Bool(true),
])
.unwrap();
let data = group.scan();
assert_eq!(data.len(), 1);
assert_eq!(data[0][0], Value::String("Alice".into()));
assert_eq!(data[0][1], Value::Int64(30));
assert_eq!(data[0][3], Value::Bool(true));
}
#[test]
fn test_multiple_flush_cycles() {
let dir = tempfile::tempdir().unwrap();
let mut cols = setup_columns(2, dir.path());
let mut group = NodeGroup::with_capacity(2, 0, 20);
// Flush cycle 1
for i in 0i64..15 {
group.append_row(vec![Value::Int64(i), Value::Int64(-i)]).unwrap();
}
assert_eq!(group.flush(&mut cols).unwrap(), 15);
// Flush cycle 2
for i in 15i64..30 {
group.append_row(vec![Value::Int64(i), Value::Int64(-i)]).unwrap();
}
assert_eq!(group.flush(&mut cols).unwrap(), 15);
// Verify all 30 rows
assert_eq!(cols[0].num_values, 30);
for i in 0i64..30 {
assert_eq!(cols[0].get_value(i as u64).unwrap(), Value::Int64(i));
assert_eq!(cols[1].get_value(i as u64).unwrap(), Value::Int64(-i));
}
}
}