Skip to content

Commit 877ddb5

Browse files
zhangfengcdtclaude
andcommitted
Add Python documentation auto-generation with Read the Docs integration
- Add complete Sphinx documentation framework in python/docs/ - Add .readthedocs.yaml for automated documentation hosting - Add comprehensive API reference with autodoc integration - Add quickstart guide, examples, and advanced usage documentation - Add build script for local documentation generation - Update .gitignore to exclude documentation build artifacts 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 5331862 commit 877ddb5

11 files changed

Lines changed: 1216 additions & 0 deletions

File tree

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,14 @@ build/
6262
.mypy_cache/
6363
.tox/
6464
*.whl
65+
66+
# Documentation build files
67+
python/docs/_build/
68+
python/docs/.doctrees/
69+
python/docs/_static/
70+
python/docs/_templates/
71+
72+
# Sphinx temporary files
73+
.buildinfo
74+
*.inv
75+
searchindex.js

.readthedocs.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# .readthedocs.yaml
2+
# Read the Docs configuration file
3+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
4+
5+
# Required
6+
version: 2
7+
8+
# Set the OS, Python version and other tools you might need
9+
build:
10+
os: ubuntu-22.04
11+
tools:
12+
python: "3.11"
13+
rust: "1.75"
14+
jobs:
15+
pre_create_environment:
16+
# Install system dependencies
17+
- apt-get update
18+
- apt-get install -y build-essential pkg-config libssl-dev curl
19+
pre_build:
20+
# Install Rust and build the Python package
21+
- python -m pip install --upgrade pip setuptools wheel
22+
- python -m pip install maturin
23+
# Build and install the ProllyTree Python package with SQL features
24+
- cd $READTHEDOCS_PROJECT_PATH
25+
- maturin build --release --features "python sql" --out target/wheels
26+
- python -m pip install target/wheels/prollytree-*.whl
27+
28+
# Build documentation in the docs/ directory with Sphinx
29+
sphinx:
30+
configuration: python/docs/conf.py
31+
fail_on_warning: false
32+
33+
# Python requirements for documentation
34+
python:
35+
install:
36+
- requirements: python/docs/requirements.txt
37+
38+
# Output formats
39+
formats:
40+
- htmlzip
41+
- pdf

python/docs/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# ProllyTree Python Documentation
2+
3+
This directory contains the Sphinx documentation for ProllyTree Python bindings.
4+
5+
## Local Development
6+
7+
### Prerequisites
8+
9+
```bash
10+
pip install sphinx sphinx_rtd_theme sphinx-autodoc-typehints myst-parser maturin
11+
```
12+
13+
### Building Documentation Locally
14+
15+
```bash
16+
# Build Python bindings and documentation
17+
./build_docs.sh
18+
19+
# Or build documentation only (requires prollytree to be installed)
20+
sphinx-build -b html . _build/html
21+
```
22+
23+
### Viewing Documentation
24+
25+
```bash
26+
# Open in browser
27+
open _build/html/index.html
28+
29+
# Or serve locally
30+
cd _build/html && python -m http.server 8000
31+
# Then visit: http://localhost:8000
32+
```
33+
34+
## Read the Docs Integration
35+
36+
This documentation is configured to be built automatically on Read the Docs using the `.readthedocs.yaml` file in the project root.
37+
38+
### File Structure
39+
40+
- `conf.py` - Sphinx configuration
41+
- `index.rst` - Main documentation page
42+
- `quickstart.rst` - Getting started guide
43+
- `api.rst` - Auto-generated API reference
44+
- `examples.rst` - Comprehensive examples
45+
- `advanced.rst` - Advanced usage patterns
46+
- `requirements.txt` - Documentation dependencies
47+
- `build_docs.sh` - Local build script
48+
49+
### Auto-Generated Content
50+
51+
The API documentation is automatically generated from the Python bindings using Sphinx autodoc. This includes:
52+
53+
- ProllyTree class and methods
54+
- VersionedKvStore for Git-like version control
55+
- ProllySQLStore for SQL query support
56+
- AgentMemorySystem for AI agent memory
57+
- All supporting classes and enums
58+
59+
### Adding New Documentation
60+
61+
1. Add new `.rst` files to this directory
62+
2. Update `index.rst` to include them in the toctree
63+
3. Rebuild documentation with `./build_docs.sh`

python/docs/advanced.rst

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
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

Comments
 (0)