|
| 1 | +Advanced Usage |
| 2 | +============== |
| 3 | + |
| 4 | +This guide covers advanced features and performance optimization techniques for ProllyTree. |
| 5 | + |
| 6 | +Performance Optimization |
| 7 | +------------------------- |
| 8 | + |
| 9 | +Batch Operations |
| 10 | +~~~~~~~~~~~~~~~~ |
| 11 | + |
| 12 | +For better performance when inserting many items, use batch operations: |
| 13 | + |
| 14 | +.. code-block:: python |
| 15 | +
|
| 16 | + from prollytree import ProllyTree |
| 17 | +
|
| 18 | + tree = ProllyTree() |
| 19 | +
|
| 20 | + # Instead of individual inserts |
| 21 | + for i in range(1000): |
| 22 | + tree.insert(f"key_{i}".encode(), f"value_{i}".encode()) |
| 23 | +
|
| 24 | + # Use batch insert (much faster) |
| 25 | + batch_data = [ |
| 26 | + (f"key_{i}".encode(), f"value_{i}".encode()) |
| 27 | + for i in range(1000) |
| 28 | + ] |
| 29 | + tree.insert_batch(batch_data) |
| 30 | +
|
| 31 | +Storage Backends |
| 32 | +~~~~~~~~~~~~~~~~ |
| 33 | + |
| 34 | +Choose the appropriate storage backend for your use case: |
| 35 | + |
| 36 | +.. code-block:: python |
| 37 | +
|
| 38 | + from prollytree import ProllyTree, VersionedKvStore |
| 39 | +
|
| 40 | + # In-memory (fastest, not persistent) |
| 41 | + tree = ProllyTree() |
| 42 | +
|
| 43 | + # File-based storage (persistent) |
| 44 | + tree = ProllyTree(storage_type="file", path="/path/to/data") |
| 45 | +
|
| 46 | + # Versioned storage with Git-like history |
| 47 | + store = VersionedKvStore("/path/to/versioned_data") |
| 48 | +
|
| 49 | +Tree Configuration |
| 50 | +~~~~~~~~~~~~~~~~~~ |
| 51 | + |
| 52 | +Tune tree parameters for your workload: |
| 53 | + |
| 54 | +.. code-block:: python |
| 55 | +
|
| 56 | + from prollytree import ProllyTree, TreeConfig |
| 57 | +
|
| 58 | + # Default configuration |
| 59 | + config = TreeConfig() |
| 60 | +
|
| 61 | + # Custom configuration for specific workloads |
| 62 | + config = TreeConfig( |
| 63 | + base=8, # Higher base for wider trees (good for read-heavy) |
| 64 | + modulus=128, # Higher modulus for deeper trees (good for write-heavy) |
| 65 | + ) |
| 66 | +
|
| 67 | + tree = ProllyTree(config=config) |
| 68 | +
|
| 69 | +Concurrent Access |
| 70 | +----------------- |
| 71 | + |
| 72 | +Thread Safety |
| 73 | +~~~~~~~~~~~~~ |
| 74 | + |
| 75 | +For multi-threaded applications: |
| 76 | + |
| 77 | +.. code-block:: python |
| 78 | +
|
| 79 | + import threading |
| 80 | + from prollytree import ProllyTree |
| 81 | +
|
| 82 | + # Create a thread-safe tree |
| 83 | + tree = ProllyTree(thread_safe=True) |
| 84 | +
|
| 85 | + def worker(thread_id): |
| 86 | + for i in range(100): |
| 87 | + key = f"thread_{thread_id}_key_{i}".encode() |
| 88 | + value = f"value_{i}".encode() |
| 89 | + tree.insert(key, value) |
| 90 | +
|
| 91 | + # Start multiple threads |
| 92 | + threads = [] |
| 93 | + for i in range(4): |
| 94 | + t = threading.Thread(target=worker, args=(i,)) |
| 95 | + threads.append(t) |
| 96 | + t.start() |
| 97 | +
|
| 98 | + for t in threads: |
| 99 | + t.join() |
| 100 | +
|
| 101 | +Memory Management |
| 102 | +----------------- |
| 103 | + |
| 104 | +LRU Cache |
| 105 | +~~~~~~~~~ |
| 106 | + |
| 107 | +Enable LRU caching for read-heavy workloads: |
| 108 | + |
| 109 | +.. code-block:: python |
| 110 | +
|
| 111 | + from prollytree import ProllyTree, CacheConfig |
| 112 | +
|
| 113 | + cache_config = CacheConfig( |
| 114 | + max_size=10000, # Cache up to 10k nodes |
| 115 | + eviction_policy="lru" |
| 116 | + ) |
| 117 | +
|
| 118 | + tree = ProllyTree(cache_config=cache_config) |
| 119 | +
|
| 120 | +Memory Monitoring |
| 121 | +~~~~~~~~~~~~~~~~~ |
| 122 | + |
| 123 | +Monitor memory usage: |
| 124 | + |
| 125 | +.. code-block:: python |
| 126 | +
|
| 127 | + tree = ProllyTree() |
| 128 | +
|
| 129 | + # Insert data |
| 130 | + for i in range(10000): |
| 131 | + tree.insert(f"key_{i}".encode(), f"value_{i}".encode()) |
| 132 | +
|
| 133 | + # Get memory statistics |
| 134 | + stats = tree.get_memory_stats() |
| 135 | + print(f"Nodes in memory: {stats['node_count']}") |
| 136 | + print(f"Memory usage: {stats['memory_bytes']} bytes") |
| 137 | + print(f"Cache hit rate: {stats['cache_hit_rate']}%") |
| 138 | +
|
| 139 | +Data Serialization |
| 140 | +------------------- |
| 141 | + |
| 142 | +Custom Serialization |
| 143 | +~~~~~~~~~~~~~~~~~~~~~ |
| 144 | + |
| 145 | +For complex data types: |
| 146 | + |
| 147 | +.. code-block:: python |
| 148 | +
|
| 149 | + import json |
| 150 | + import pickle |
| 151 | + from prollytree import ProllyTree |
| 152 | +
|
| 153 | + tree = ProllyTree() |
| 154 | +
|
| 155 | + # JSON serialization |
| 156 | + def store_json(tree, key, data): |
| 157 | + serialized = json.dumps(data).encode('utf-8') |
| 158 | + tree.insert(key.encode('utf-8'), serialized) |
| 159 | +
|
| 160 | + def load_json(tree, key): |
| 161 | + data = tree.find(key.encode('utf-8')) |
| 162 | + return json.loads(data.decode('utf-8')) if data else None |
| 163 | +
|
| 164 | + # Usage |
| 165 | + complex_data = { |
| 166 | + "user": "alice", |
| 167 | + "scores": [95, 87, 92], |
| 168 | + "metadata": {"premium": True, "last_login": "2023-01-01"} |
| 169 | + } |
| 170 | +
|
| 171 | + store_json(tree, "user:alice", complex_data) |
| 172 | + retrieved = load_json(tree, "user:alice") |
| 173 | +
|
| 174 | +SQL Advanced Queries |
| 175 | +--------------------- |
| 176 | + |
| 177 | +Complex Joins and Aggregations |
| 178 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 179 | + |
| 180 | +.. code-block:: python |
| 181 | +
|
| 182 | + from prollytree import ProllySQLStore |
| 183 | +
|
| 184 | + sql_store = ProllySQLStore("/path/to/sql_data") |
| 185 | +
|
| 186 | + # Create tables |
| 187 | + sql_store.execute(""" |
| 188 | + CREATE TABLE users ( |
| 189 | + id INTEGER PRIMARY KEY, |
| 190 | + name TEXT, |
| 191 | + department_id INTEGER, |
| 192 | + salary REAL |
| 193 | + ) |
| 194 | + """) |
| 195 | +
|
| 196 | + sql_store.execute(""" |
| 197 | + CREATE TABLE departments ( |
| 198 | + id INTEGER PRIMARY KEY, |
| 199 | + name TEXT, |
| 200 | + budget REAL |
| 201 | + ) |
| 202 | + """) |
| 203 | +
|
| 204 | + # Complex aggregation query |
| 205 | + result = sql_store.execute(""" |
| 206 | + SELECT |
| 207 | + d.name as department, |
| 208 | + COUNT(u.id) as employee_count, |
| 209 | + AVG(u.salary) as avg_salary, |
| 210 | + MAX(u.salary) as max_salary, |
| 211 | + SUM(u.salary) as total_salary |
| 212 | + FROM departments d |
| 213 | + LEFT JOIN users u ON d.id = u.department_id |
| 214 | + GROUP BY d.id, d.name |
| 215 | + HAVING COUNT(u.id) > 0 |
| 216 | + ORDER BY avg_salary DESC |
| 217 | + """) |
| 218 | +
|
| 219 | +Error Handling and Debugging |
| 220 | +----------------------------- |
| 221 | + |
| 222 | +Exception Handling |
| 223 | +~~~~~~~~~~~~~~~~~~~ |
| 224 | + |
| 225 | +.. code-block:: python |
| 226 | +
|
| 227 | + from prollytree import ProllyTree, ProllyTreeError, StorageError |
| 228 | +
|
| 229 | + try: |
| 230 | + tree = ProllyTree(storage_type="file", path="/invalid/path") |
| 231 | + tree.insert(b"key", b"value") |
| 232 | + except StorageError as e: |
| 233 | + print(f"Storage error: {e}") |
| 234 | + except ProllyTreeError as e: |
| 235 | + print(f"Tree operation error: {e}") |
| 236 | + except Exception as e: |
| 237 | + print(f"Unexpected error: {e}") |
| 238 | +
|
| 239 | +Debug Mode |
| 240 | +~~~~~~~~~~ |
| 241 | + |
| 242 | +.. code-block:: python |
| 243 | +
|
| 244 | + # Enable debug logging |
| 245 | + tree = ProllyTree(debug=True, log_level="DEBUG") |
| 246 | +
|
| 247 | + # Validate tree structure |
| 248 | + is_valid = tree.validate() |
| 249 | + if not is_valid: |
| 250 | + print("Tree structure is corrupted!") |
| 251 | +
|
| 252 | + # Get detailed statistics |
| 253 | + stats = tree.get_debug_stats() |
| 254 | + print(f"Tree height: {stats['height']}") |
| 255 | + print(f"Node distribution: {stats['node_distribution']}") |
| 256 | + print(f"Rebalancing events: {stats['rebalance_count']}") |
| 257 | +
|
| 258 | +Migration and Backup |
| 259 | +--------------------- |
| 260 | + |
| 261 | +Data Export/Import |
| 262 | +~~~~~~~~~~~~~~~~~~~ |
| 263 | + |
| 264 | +.. code-block:: python |
| 265 | +
|
| 266 | + # Export tree data |
| 267 | + tree.export_to_file("/path/to/backup.json", format="json") |
| 268 | + tree.export_to_file("/path/to/backup.bin", format="binary") |
| 269 | +
|
| 270 | + # Import tree data |
| 271 | + new_tree = ProllyTree() |
| 272 | + new_tree.import_from_file("/path/to/backup.json", format="json") |
| 273 | +
|
| 274 | +This advanced guide covers performance optimization, concurrent access patterns, memory management, complex data operations, and debugging techniques for ProllyTree. |
0 commit comments