Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
85 lines (64 loc) · 3.5 KB
/
Copy pathmain.py
File metadata and controls
85 lines (64 loc) · 3.5 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
"""
Contact Book — A small FerrumDB example in Python.
Demonstrates: CRUD, secondary indexes, transactions, and new v0.2.0 features:
exists, stats, prefix scanning, native dicts, backup/restore.
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")
# ── Check if key exists (O(1), no disk read) ──────────────
print(f"contact:1 exists: {db.exists('contact:1')}")
print(f"contact:99 exists: {db.exists('contact:99')}\n")
# ── Read a contact ────────────────────────────────────────────
# Note: get() now returns native Python dict!
val = db.get("contact:1")
print(f"Contact 1: {val}")
print(f"Type: {type(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")
# ── Prefix scanning ────────────────────────────────────────────
contact_keys = db.keys_with_prefix("contact:")
print(f"Contact keys: {contact_keys}")
# ── Database statistics ────────────────────────────────────────
stats = db.stats()
print(f"\nDatabase stats:")
print(f" Key count: {stats['key_count']}")
print(f" Disk size: {stats['disk_size_bytes']} bytes")
print(f" Index memory: ~{stats['index_memory_estimate']} bytes")
print(f" Expired keys: {stats['expired_keys']}")
# ── Backup/Restore ─────────────────────────────────────────────
backup = db.export_json()
print(f"\nExported {len(backup)} entries for backup")
# List all keys
keys = sorted(db.keys())
print(f"All keys: {keys}")
# Cleanup
os.remove("contacts.db")