Skip to content

Commit 69b3c38

Browse files
authored
Merge pull request #109 from vcon-dev/silly-goldberg
Modernize file storage with UUID-based naming, compression, and tests
2 parents 19aee91 + 28010fb commit 69b3c38

File tree

5 files changed

+904
-79
lines changed

5 files changed

+904
-79
lines changed

example_config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ tracers:
105105
# dlq_vcon_on_error: True
106106

107107
storages:
108+
file:
109+
module: storage.file
110+
options:
111+
path: /data/vcons
112+
organize_by_date: true
113+
compression: false
114+
max_file_size: 10485760
115+
file_permissions: 0o644
116+
dir_permissions: 0o755
108117
mongo:
109118
module: storage.mongo
110119
options:

example_docker-compose.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ services:
1111
command: "watchmedo auto-restart -p '*.py' -R python -- ./server/main.py"
1212
volumes:
1313
- .:/app
14+
- vcon_files:/data/vcons
1415
depends_on:
1516
- redis
1617
env_file:
@@ -30,6 +31,7 @@ services:
3031
command: /bin/bash -c "poetry run uvicorn server.api:app --host 0.0.0.0 --port 8000"
3132
volumes:
3233
- .:/app
34+
- vcon_files:/data/vcons
3335
ports:
3436
- "${CONSERVER_EXTERNAL_PORT:-8000}:8000"
3537
depends_on:
@@ -249,3 +251,4 @@ services:
249251
volumes:
250252
es_data: {}
251253
redis_data: {}
254+
vcon_files: {}

server/storage/file/README.md

Lines changed: 115 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,151 @@ This module implements local file system storage for the vCon server.
44

55
## Overview
66

7-
File storage provides simple, local file system storage capabilities, making it ideal for development, testing, and small-scale deployments of vCon data.
7+
File storage provides local file system storage capabilities, making it ideal for development, testing, and small-scale deployments. Files are stored using the vCon UUID as the filename, with optional date-based directory organization and gzip compression.
88

99
## Configuration
1010

11-
Required configuration options:
11+
Configuration options in `config.yml`:
1212

1313
```yaml
1414
storages:
1515
file:
1616
module: storage.file
1717
options:
18-
base_path: /path/to/storage # Base directory for file storage
19-
file_format: json # File format (json/txt)
20-
compression: false # Enable compression
18+
path: /data/vcons # Base directory for storage
19+
organize_by_date: true # Store in YYYY/MM/DD subdirectories
20+
compression: false # Enable gzip compression
2121
max_file_size: 10485760 # Max file size in bytes (10MB)
22-
file_permissions: 0644 # File permissions
22+
file_permissions: 0o644 # Unix file permissions (octal)
23+
dir_permissions: 0o755 # Unix directory permissions (octal)
2324
```
2425
26+
### Configuration Options
27+
28+
| Option | Type | Default | Description |
29+
|--------|------|---------|-------------|
30+
| `path` | string | `/data/vcons` | Base directory for vCon file storage |
31+
| `organize_by_date` | boolean | `true` | Organize files in YYYY/MM/DD subdirectories based on vCon creation date |
32+
| `compression` | boolean | `false` | Enable gzip compression (files saved as `.json.gz`) |
33+
| `max_file_size` | integer | `10485760` | Maximum file size in bytes (10MB default) |
34+
| `file_permissions` | integer | `0o644` | Unix permissions for created files |
35+
| `dir_permissions` | integer | `0o755` | Unix permissions for created directories |
36+
2537
## Features
2638

27-
- Local file storage
28-
- Multiple file formats
29-
- Compression support
30-
- File size limits
31-
- Automatic metrics logging
32-
- File organization
33-
- Permission management
39+
- **UUID-based filenames**: Files are named `{uuid}.json` or `{uuid}.json.gz`
40+
- **Date-based organization**: Optional YYYY/MM/DD directory structure based on vCon creation date
41+
- **Gzip compression**: Reduce storage space with optional compression
42+
- **File size limits**: Prevent oversized files from consuming disk space
43+
- **Permission management**: Configure Unix file and directory permissions
44+
- **Automatic cleanup**: Empty directories are removed when vCons are deleted
45+
- **Metrics logging**: All operations are automatically timed and logged
46+
47+
## Docker Volume Configuration
48+
49+
When using Docker, mount a volume for persistent file storage:
50+
51+
```yaml
52+
services:
53+
conserver:
54+
volumes:
55+
- vcon_files:/data/vcons
56+
57+
volumes:
58+
vcon_files: {}
59+
```
3460

3561
## Usage
3662

3763
```python
3864
from storage import Storage
3965
40-
# Initialize File storage
66+
# Initialize file storage
4167
file_storage = Storage("file")
4268
43-
# Save vCon data
44-
file_storage.save(vcon_id)
69+
# Save vCon data (retrieves from Redis and writes to file)
70+
file_storage.save(vcon_uuid)
4571
4672
# Retrieve vCon data
47-
vcon_data = file_storage.get(vcon_id)
73+
vcon_data = file_storage.get(vcon_uuid)
74+
75+
# Delete vCon file
76+
file_storage.delete(vcon_uuid)
77+
```
78+
79+
### Direct Module Usage
80+
81+
For more control, you can use the module functions directly:
82+
83+
```python
84+
from server.storage.file import save, get, delete, exists, list_vcons
85+
86+
# Check if vCon exists
87+
if exists("my-uuid", opts):
88+
data = get("my-uuid", opts)
89+
90+
# List all vCons with pagination
91+
uuids = list_vcons(opts, limit=100, offset=0)
92+
93+
# Delete a vCon
94+
deleted = delete("my-uuid", opts)
95+
```
96+
97+
## File Organization
98+
99+
### Flat Structure (`organize_by_date: false`)
100+
```
101+
/data/vcons/
102+
├── abc123.json
103+
├── def456.json
104+
└── ghi789.json.gz
48105
```
49106
50-
## Implementation Details
107+
### Date-Based Structure (`organize_by_date: true`)
108+
```
109+
/data/vcons/
110+
├── 2024/
111+
│ ├── 03/
112+
│ │ ├── 14/
113+
│ │ │ ├── abc123.json
114+
│ │ │ └── def456.json
115+
│ │ └── 15/
116+
│ │ └── ghi789.json
117+
│ └── 04/
118+
│ └── 01/
119+
│ └── jkl012.json
120+
```
121+
122+
## API Reference
123+
124+
### `save(vcon_uuid: str, opts: dict = None) -> None`
125+
Save a vCon to file storage. Retrieves the vCon from Redis and writes it to a file.
126+
127+
### `get(vcon_uuid: str, opts: dict = None) -> Optional[dict]`
128+
Retrieve a vCon from file storage by UUID. Returns `None` if not found.
129+
130+
### `delete(vcon_uuid: str, opts: dict = None) -> bool`
131+
Delete a vCon file. Returns `True` if deleted, `False` if not found.
132+
133+
### `exists(vcon_uuid: str, opts: dict = None) -> bool`
134+
Check if a vCon exists in file storage.
51135
52-
The File storage implementation:
53-
- Uses standard file system operations
54-
- Implements file compression
55-
- Supports multiple file formats
56-
- Provides file organization
57-
- Includes automatic metrics logging
136+
### `list_vcons(opts: dict = None, limit: int = 100, offset: int = 0) -> list[str]`
137+
List vCon UUIDs in storage with pagination support. Returns UUIDs sorted by modification time (newest first).
58138
59139
## Dependencies
60140
61-
- json
62-
- gzip
63-
- pathlib
141+
- `json` - JSON serialization
142+
- `gzip` - Compression support
143+
- `pathlib` - Path manipulation
144+
- `glob` - File pattern matching
64145
65146
## Best Practices
66147
67-
1. Regular file cleanup
68-
2. Implement file rotation
69-
3. Use appropriate file formats
70-
4. Monitor disk space
71-
5. Implement proper error handling
72-
6. Use compression for large files
73-
7. Regular backup
74-
8. Implement file size limits
75-
9. Use appropriate file permissions
76-
10. Monitor file system performance
77-
11. Implement proper directory structure
78-
12. Handle file locking
148+
1. **Use compression** for large vCons to save disk space
149+
2. **Enable date organization** for easier manual browsing and archival
150+
3. **Set appropriate permissions** for security (default 0o644 for files)
151+
4. **Monitor disk space** - implement cleanup policies for old files
152+
5. **Configure volume mounts** in Docker for data persistence
153+
6. **Set reasonable file size limits** to prevent runaway storage
154+
7. **Use S3 or other cloud storage** for production deployments with large volumes

0 commit comments

Comments
 (0)