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
// Copyright 2025 Stoolap Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::fs;
use stoolap::Database;
use tempfile::tempdir;
#[test]
fn test_persistence_trace() {
let dir = tempdir().unwrap();
let db_path = dir.path().join("test.db");
let dsn = format!("file://{}", db_path.display());
eprintln!("=== PHASE 1: Create and Insert ===");
// Phase 1: Create table and insert data
{
let db = Database::open(&dsn).unwrap();
db.execute(
"CREATE TABLE test (id INTEGER PRIMARY KEY, value INTEGER)",
(),
)
.unwrap();
db.execute("INSERT INTO test (id, value) VALUES (1, 100)", ())
.unwrap();
db.execute("INSERT INTO test (id, value) VALUES (2, 200)", ())
.unwrap();
let count: i64 = db.query_one("SELECT COUNT(*) FROM test", ()).unwrap();
eprintln!("Phase 1 count: {}", count);
// Print WAL content in a more detailed way
let wal_dir = db_path.join("wal");
if wal_dir.exists() {
for entry in fs::read_dir(&wal_dir).unwrap() {
let entry = entry.unwrap();
let content = fs::read(entry.path()).unwrap();
eprintln!(
"\nWAL file: {:?} ({} bytes)",
entry.file_name(),
content.len()
);
// Parse WAL entries manually
let mut pos = 0;
let mut entry_num = 0;
while pos + 16 <= content.len() {
let lsn = u64::from_le_bytes(content[pos..pos + 8].try_into().unwrap());
let size =
u64::from_le_bytes(content[pos + 8..pos + 16].try_into().unwrap()) as usize;
pos += 16;
if pos + size > content.len() {
eprintln!(
" Entry {}: LSN={}, size={} (truncated)",
entry_num, lsn, size
);
break;
}
// Parse entry
let data = &content[pos..pos + size];
if data.len() >= 32 {
let txn_id = i64::from_le_bytes(data[0..8].try_into().unwrap());
let row_id = i64::from_le_bytes(data[8..16].try_into().unwrap());
let _timestamp = i64::from_le_bytes(data[16..24].try_into().unwrap());
// Table name length
if data.len() >= 26 {
let table_len =
u16::from_le_bytes(data[24..26].try_into().unwrap()) as usize;
if data.len() >= 26 + table_len {
let table_name = String::from_utf8_lossy(&data[26..26 + table_len]);
let op_pos = 26 + table_len;
if data.len() > op_pos {
let op = data[op_pos];
let op_name = match op {
1 => "CreateTable",
2 => "DropTable",
3 => "Insert",
4 => "Update",
5 => "Delete",
6 => "Commit",
7 => "Rollback",
9 => "CreateIndex",
10 => "DropIndex",
_ => "Unknown",
};
eprintln!(" Entry {}: LSN={}, txn={}, row={}, table='{}', op={} ({})",
entry_num, lsn, txn_id, row_id, table_name, op, op_name);
}
}
}
}
pos += size;
entry_num += 1;
}
}
}
db.close().unwrap();
}
eprintln!("\n=== PHASE 2: Reopen and verify ===");
// Phase 2: Reopen and verify
{
let db = Database::open(&dsn).unwrap();
// Check if table exists
let table_result = db.query("SELECT * FROM test", ());
match table_result {
Ok(rows) => {
let collected: Vec<_> = rows.collect();
eprintln!("Table 'test' exists with {} rows:", collected.len());
for row in collected {
match row {
Ok(r) => {
let id: i64 = r.get(0).unwrap();
let value: i64 = r.get(1).unwrap();
eprintln!(" id={}, value={}", id, value);
}
Err(e) => eprintln!(" Error reading row: {}", e),
}
}
}
Err(e) => eprintln!("Table 'test' query failed: {}", e),
}
let count: i64 = db.query_one("SELECT COUNT(*) FROM test", ()).unwrap();
eprintln!("\nPhase 2 count: {}", count);
if count == 2 {
eprintln!("✅ SUCCESS!");
} else {
eprintln!("❌ FAILURE: Expected 2, got {}", count);
}
assert_eq!(count, 2);
}
}