-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (45 loc) · 2.43 KB
/
main.py
File metadata and controls
62 lines (45 loc) · 2.43 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
"""
Contact Book — A small FerrumDB example in Python.
Demonstrates: CRUD, secondary indexes, transactions, and key listing.
Install: pip install ferrumdb
Run: python main.py
"""
from ferrumdb import FerrumDB, Transaction
import os
db = FerrumDB.open("contacts.db")
print("=== FerrumDB Contact Book ===\n")
# ── Add contacts ──────────────────────────────────────────────
db.set("contact:1", '{"name": "Alice", "email": "alice@example.com", "role": "engineer"}')
db.set("contact:2", '{"name": "Bob", "email": "bob@example.com", "role": "designer"}')
db.set("contact:3", '{"name": "Charlie", "email": "charlie@example.com", "role": "engineer"}')
db.set("contact:4", '{"name": "Diana", "email": "diana@example.com", "role": "manager"}')
print(f"Added {db.count()} contacts\n")
# ── Read a contact ────────────────────────────────────────────
val = db.get("contact:1")
print(f"Contact 1: {val}\n")
# ── Secondary indexes: query by role ──────────────────────────
db.create_index("role")
engineers = db.find("role", '"engineer"')
print(f"Engineers: {engineers}")
designers = db.find("role", '"designer"')
print(f"Designers: {designers}")
managers = db.find("role", '"manager"')
print(f"Managers: {managers}\n")
# ── Transactions: bulk-add contacts atomically ────────────────
tx = Transaction()
tx.set("contact:5", '{"name": "Eve", "email": "eve@example.com", "role": "engineer"}')
tx.set("contact:6", '{"name": "Frank", "email": "frank@example.com", "role": "designer"}')
db.commit(tx)
print(f"After transaction: {db.count()} contacts")
engineers_after = db.find("role", '"engineer"')
print(f"Engineers now: {engineers_after}\n")
# ── Delete a contact ──────────────────────────────────────────
deleted = db.delete("contact:2")
print(f"Deleted Bob: {deleted}")
print(f"Bob after delete: {db.get('contact:2')}")
print(f"Final count: {db.count()}\n")
# ── List all keys ─────────────────────────────────────────────
keys = sorted(db.keys())
print(f"All keys: {keys}")
# Cleanup
os.remove("contacts.db")