-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_persistence.cpp
More file actions
75 lines (60 loc) · 2.87 KB
/
demo_persistence.cpp
File metadata and controls
75 lines (60 loc) · 2.87 KB
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
#include <iostream>
#include "src/core/database.hpp"
using namespace scalerdb;
int main() {
std::cout << "=== ScalerDB Persistence Demo ===\n\n";
// Create a database and populate it
{
std::cout << "1. Creating database and adding data...\n";
Database db("demo_db");
// Create a table
std::vector<Column> schema;
schema.emplace_back("id", ValueType::Integer32, false, true);
schema.emplace_back("name", ValueType::String, false, false);
schema.emplace_back("score", ValueType::Double, false, false);
Table* table = db.createTable("highscores", std::move(schema), "id");
// Add some data
table->insertRow({Value(1), Value("Alice"), Value(95.5)});
table->insertRow({Value(2), Value("Bob"), Value(87.2)});
table->insertRow({Value(3), Value("Charlie"), Value(92.1)});
std::cout << " Added " << table->getRowCount() << " records to 'highscores' table\n";
// Save to file
std::cout << "2. Saving database to 'demo.json'...\n";
if (db.save("demo.json")) {
std::cout << " ✓ Database saved successfully!\n";
} else {
std::cout << " ✗ Failed to save database\n";
return 1;
}
}
// Database goes out of scope here - all in-memory data is lost
std::cout << "\n3. Original database destroyed (in-memory data gone)\n";
// Load the database back from file
{
std::cout << "4. Loading database from 'demo.json'...\n";
Database restored_db;
if (restored_db.load("demo.json")) {
std::cout << " ✓ Database loaded successfully!\n";
// Verify the data is intact
Table* table = restored_db.getTable("highscores");
if (table) {
std::cout << " Restored table 'highscores' with " << table->getRowCount() << " records:\n";
for (const auto& row : table->getAllRows()) {
std::cout << " ID: " << row.getValue("id").toString()
<< ", Name: " << row.getValue("name").toString()
<< ", Score: " << row.getValue("score").toString() << "\n";
}
}
} else {
std::cout << " ✗ Failed to load database\n";
return 1;
}
}
std::cout << "\n=== Summary ===\n";
std::cout << "ScalerDB is an IN-MEMORY database with OPTIONAL persistence:\n";
std::cout << "• All operations happen in RAM for maximum speed\n";
std::cout << "• Can save/load entire database to/from JSON files\n";
std::cout << "• No automatic persistence - manual save/load required\n";
std::cout << "• Perfect for high-performance applications that need occasional snapshots\n";
return 0;
}