-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy path02-basic-sharding.php
More file actions
141 lines (114 loc) · 3.87 KB
/
02-basic-sharding.php
File metadata and controls
141 lines (114 loc) · 3.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
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
<?php
declare(strict_types=1);
require_once __DIR__ . '/../../vendor/autoload.php';
use tommyknocker\pdodb\PdoDb;
$driver = $_ENV['PDODB_DRIVER'] ?? 'sqlite';
// Create main database connection
$db = match ($driver) {
'mysql' => new PdoDb('mysql', [
'host' => $_ENV['MYSQL_HOST'] ?? 'localhost',
'dbname' => $_ENV['MYSQL_DATABASE'] ?? 'test',
'user' => $_ENV['MYSQL_USER'] ?? 'root',
'password' => $_ENV['MYSQL_PASSWORD'] ?? '',
]),
'mariadb' => new PdoDb('mariadb', [
'host' => $_ENV['MARIADB_HOST'] ?? 'localhost',
'dbname' => $_ENV['MARIADB_DATABASE'] ?? 'test',
'user' => $_ENV['MARIADB_USER'] ?? 'root',
'password' => $_ENV['MARIADB_PASSWORD'] ?? '',
]),
'pgsql' => new PdoDb('pgsql', [
'host' => $_ENV['POSTGRES_HOST'] ?? 'localhost',
'dbname' => $_ENV['POSTGRES_DATABASE'] ?? 'test',
'user' => $_ENV['POSTGRES_USER'] ?? 'postgres',
'password' => $_ENV['POSTGRES_PASSWORD'] ?? '',
]),
default => new PdoDb('sqlite', ['path' => ':memory:']),
};
// For SQLite, we'll use multiple in-memory databases to simulate sharding
if ($driver === 'sqlite') {
// Create shard connections
$db->addConnection('shard1', ['driver' => 'sqlite', 'path' => ':memory:']);
$db->addConnection('shard2', ['driver' => 'sqlite', 'path' => ':memory:']);
$db->addConnection('shard3', ['driver' => 'sqlite', 'path' => ':memory:']);
// Create tables in each shard
foreach (['shard1', 'shard2', 'shard3'] as $shard) {
$db->connection($shard);
$db->rawQuery('
CREATE TABLE users (
user_id INTEGER PRIMARY KEY,
name TEXT,
email TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
');
}
// Configure range-based sharding using existing connections
$db->shard('users')
->shardKey('user_id')
->strategy('range')
->ranges([
'shard1' => [0, 1000],
'shard2' => [1001, 2000],
'shard3' => [2001, 3000],
])
->useConnections(['shard1', 'shard2', 'shard3'])
->register();
}
echo "=== Basic Sharding Example ===\n\n";
// Insert data - automatically routed to appropriate shard
echo "Inserting users...\n";
$db->find()->table('users')->insert([
'user_id' => 500,
'name' => 'Alice',
'email' => '[email protected]'
]);
$db->find()->table('users')->insert([
'user_id' => 1500,
'name' => 'Bob',
'email' => '[email protected]'
]);
$db->find()->table('users')->insert([
'user_id' => 2500,
'name' => 'Charlie',
'email' => '[email protected]'
]);
echo "Users inserted successfully!\n\n";
// Query data - automatically routed to appropriate shard
echo "Querying users by user_id:\n";
$user1 = $db->find()
->from('users')
->where('user_id', 500)
->getOne();
echo "User 500: {$user1['name']} ({$user1['email']})\n";
$user2 = $db->find()
->from('users')
->where('user_id', 1500)
->getOne();
echo "User 1500: {$user2['name']} ({$user2['email']})\n";
$user3 = $db->find()
->from('users')
->where('user_id', 2500)
->getOne();
echo "User 2500: {$user3['name']} ({$user3['email']})\n\n";
// Update data - automatically routed to appropriate shard
echo "Updating user 500...\n";
$affected = $db->find()
->table('users')
->where('user_id', 500)
->update(['name' => 'Alice Updated']);
echo "Updated {$affected} row(s)\n\n";
// Verify update
$updatedUser = $db->find()
->from('users')
->where('user_id', 500)
->getOne();
echo "Updated user: {$updatedUser['name']}\n\n";
// Delete data - automatically routed to appropriate shard
echo "Deleting user 2500...\n";
$deleted = $db->find()
->table('users')
->where('user_id', 2500)
->delete();
echo "Deleted {$deleted} row(s)\n\n";
echo "Sharding example completed successfully!\n";