-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24_batch_operations.py
More file actions
123 lines (96 loc) · 4.03 KB
/
24_batch_operations.py
File metadata and controls
123 lines (96 loc) · 4.03 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
#!/usr/bin/env python3
"""
SochDB Python SDK - Example: Batch Operations (Embedded FFI)
Atomic batch operations ensure all-or-nothing semantics.
If any operation fails, the entire batch is rolled back.
No server required - uses embedded FFI.
"""
from sochdb import Database
def main():
print("=" * 60)
print("SochDB - Batch Operations Example (Embedded FFI)")
print("=" * 60)
print("Note: This uses embedded Database - no server required!\n")
# Open embedded database
db = Database.open("./example_batch_db")
print("--- Single Batch Put ---")
# Atomic multi-key write using transaction
with db.transaction() as txn:
# All these writes happen atomically
txn.put(b"user:alice:email", b"alice@example.com")
txn.put(b"user:alice:age", b"30")
txn.put(b"user:alice:created", b"2026-01-07")
txn.put(b"user:alice:verified", b"true")
print("✓ Atomically stored 4 keys for user:alice")
# Verify
email = db.get(b"user:alice:email")
print(f" Verification: email = {email.decode()}")
print("\n--- Batch Put with Rollback ---")
try:
with db.transaction() as txn:
txn.put(b"user:bob:email", b"bob@example.com")
txn.put(b"user:bob:age", b"25")
# Simulate an error - none of the writes should persist
raise ValueError("Simulated failure!")
txn.put(b"user:bob:verified", b"true") # Never reached
except ValueError as e:
print(f"✓ Transaction aborted: {e}")
# Verify rollback - bob's data should NOT exist
bob_email = db.get(b"user:bob:email")
print(f" Verification: bob's email = {bob_email}") # Should be None
print("\n--- Batch Scan and Update ---")
# Add some products
products = [
(b"product:001:price", b"99.99"),
(b"product:001:stock", b"50"),
(b"product:002:price", b"149.99"),
(b"product:002:stock", b"30"),
(b"product:003:price", b"79.99"),
(b"product:003:stock", b"100"),
]
with db.transaction() as txn:
for key, value in products:
txn.put(key, value)
print(f"✓ Added {len(products)} product entries")
# Atomic inventory update - decrease all stock by 10%
with db.transaction() as txn:
stock_updates = []
for key, value in txn.scan_prefix(b"product:"):
if b":stock" in key:
current = int(value.decode())
new_stock = int(current * 0.9) # 10% decrease
stock_updates.append((key, str(new_stock).encode()))
for key, value in stock_updates:
txn.put(key, value)
print(f" Updated {key.decode()}: {value.decode()}")
print("\n--- Conditional Batch Updates ---")
# Only update if condition is met
with db.transaction() as txn:
# Check current stock before update
stock = txn.get(b"product:003:stock")
if stock and int(stock.decode()) > 80:
txn.put(b"product:003:status", b"high_stock")
txn.put(b"product:003:reorder", b"false")
print("✓ Product 003 marked as high_stock")
else:
txn.put(b"product:003:status", b"low_stock")
txn.put(b"product:003:reorder", b"true")
print("✓ Product 003 marked as low_stock")
print("\n--- Benefits of Atomic Batches ---")
print("""
1. Consistency: All writes succeed or all fail
2. Isolation: Other readers see old or new state, never partial
3. Durability: Committed batches survive crashes
4. Performance: Single disk sync for many operations
Use cases:
- User registration (email + profile + settings)
- E-commerce orders (inventory + order + payment)
- Agent memory updates (context + facts + embeddings)
""")
# Cleanup
db.close()
import shutil
shutil.rmtree("./example_batch_db", ignore_errors=True)
print("✅ Batch operations example complete!")
if __name__ == "__main__":
main()