Skip to content

Commit 7191248

Browse files
author
MPCoreDeveloper
committed
demo read/write and radonly combo
1 parent fb434f0 commit 7191248

File tree

3 files changed

+82
-16
lines changed

3 files changed

+82
-16
lines changed

SharpCoreDB.Demo/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ static void Main(string[] args)
6464

6565
// Read/write connection (default)
6666
var dbReadWrite = db;
67-
// Create a readonly connection to the same database path
68-
var dbReadOnly = factory.Create(dbPath, masterPassword, isReadOnly: true);
6967

7068
// Step 3: Create Tables
7169
Console.WriteLine("--- Creating Tables ---");
@@ -99,6 +97,9 @@ static void Main(string[] args)
9997
dbReadWrite.ExecuteSQL("INSERT INTO test (id, name) VALUES (?, ?)", new Dictionary<string, object?> { { "0", 3 }, { "1", "AutoTest" } });
10098
Console.WriteLine("Inserted data");
10199

100+
// Create a readonly connection AFTER schema/data creation so it can load metadata
101+
var dbReadOnly = factory.Create(dbPath, masterPassword, isReadOnly: true);
102+
102103
// Step 5: Query Data from readonly connection
103104
Console.WriteLine("\n--- Querying Data (readonly connection) ---");
104105
dbReadOnly.ExecuteSQL("SELECT * FROM users");

SharpCoreDB/docs/ROADMAP.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# SharpCoreDB Roadmap
2+
3+
## 1) Read-only instances and schema changes
4+
5+
- Problem
6+
- A read-only database instance loads the metadata (tables/indexes) once during construction.
7+
- New tables created later by a read-write instance are invisible to already opened read-only instances.
8+
- Result: queries against newly created tables fail on those read-only instances until they are recreated.
9+
10+
- Current guidance (to document in guides/demos)
11+
- Open read-only instances after completing schema changes; or
12+
- Dispose and recreate read-only instances after CREATE/ALTER; or
13+
- Run reporting/read-only processes that restart on schema changes.
14+
15+
- Plan (non-breaking; v1.1.x)
16+
1. API: `IDatabase.ReloadMetadata()`
17+
- Forces a reload of `meta.json` and rebinds in-memory tables/indexes.
18+
- Allowed for read-only instances (no write locks needed).
19+
2. Opt-in auto-refresh for read-only
20+
- `DatabaseConfig` flag: `AutoReloadMetadataOnChange` (default: false).
21+
- File watcher on `meta.json` → debounce → `ReloadMetadata()`.
22+
3. Schema versioning
23+
- Extend meta with `SchemaVersion` (monotonic counter).
24+
- Validate at query start: mismatch → (optional) auto-reload when the flag is active.
25+
4. Events
26+
- Read-write: `OnSchemaChanged` after `CREATE/DROP/ALTER` (increments `SchemaVersion`).
27+
5. Connection pool integration
28+
- Invalidate/refresh read-only pooled instances on schema changes (when pooling is enabled).
29+
30+
- Testing
31+
- Unit: `ReloadMetadata` reflects a newly created table; existing queries keep working.
32+
- Integration: Read-write → CREATE TABLE; read-only with AutoReload flag sees the new table without recreation.
33+
34+
- Documentation
35+
- Add a section in `docs/guides/EXAMPLES.md`: timing of read-only instances, `ReloadMetadata()` usage, and the opt-in auto-reload flag.
36+
37+
- Expected impact
38+
- Backwards compatible; no breaking changes.
39+
- Minimal runtime overhead when auto-reload is disabled (default).
40+
41+
- Target versions
42+
- v1.1.0: API + docs
43+
- v1.1.1: opt-in auto-reload + tests
44+
45+
---
46+
47+
## 2) Users & permissions demo (completed)
48+
- Demo shows:
49+
- Two users (`writer`, `reader`) and two connections (RW/RO).
50+
- RW performs schema and data mutations; RO performs selects and blocks writes.
51+
- Included in `SharpCoreDB.Demo` and referenced from docs.
52+
53+
---
54+
55+
## 3) Quality-of-life improvements
56+
- More specific exception for write attempts on read-only connections (include hint to recreate or reload).
57+
- `IDatabaseInfo` surface: `IsReadOnly`, `SchemaVersion`, `Tables` (for diagnostics/telemetry).
58+
59+
---
60+
61+
## Git compliance
62+
- Follow Conventional Commits for changes related to this roadmap:
63+
- `feat(db): add ReloadMetadata API to refresh catalog on read-only instances`
64+
- `feat(config): add AutoReloadMetadataOnChange flag`
65+
- `docs(guide): document read-only behavior and metadata reload`
66+
- `test(db): add integration tests for metadata reload`
67+
- Ensure updates include unit/integration tests and documentation changes in the same PR when feasible.
68+

SharpCoreDB/docs/guides/EXAMPLES.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
6. [Adaptive WAL Batching](#adaptive-wal-batching) - ⚡ **NEW!**
1313
7. [Entity Framework Core](#ef-core)
1414
8. [Advanced Queries](#advanced-queries)
15+
9. [Read-only Instances & Schema Changes](#read-only-instances--schema-changes) <!-- NEW -->
1516

1617
---
1718

@@ -489,20 +490,16 @@ var results = db.Query(@"
489490

490491
---
491492

492-
## Performance Tips
493+
## Read-only Instances & Schema Changes
493494

494-
1. **Use indexes** for frequently queried columns
495-
2. **Enable caching** for read-heavy workloads
496-
3. **Batch inserts** instead of individual inserts
497-
4. **Use transactions** for multiple operations
498-
5. **Choose correct index type** (B-Tree for ranges, Hash for equality)
499-
6. **Enable adaptive WAL batching** for variable/concurrent workloads ⚡ **NEW!**
500-
7. **Use HighPerformance or Concurrent config** for production deployments
501-
8. **Monitor batch size adjustments** to verify optimal scaling
495+
Read-only instances load the catalog (tables/indexes) once on construction. If a read-write instance creates a new table later, already opened read-only instances do not automatically “see” the new table.
502496

503-
---
497+
- Effect: queries against newly created tables fail on those read-only instances until they are recreated.
498+
- Cost: recreating a read-only instance only reloads small metadata files (O(#tables)) and has negligible overhead. It does not affect regular query performance.
499+
500+
### Recommended patterns
504501

505-
For more examples, see:
506-
- [Benchmarks](../SharpCoreDB.Benchmarks/)
507-
- [Demo Project](../SharpCoreDB.Demo/)
508-
- [Unit Tests](../SharpCoreDB.Tests/)
502+
```csharp
503+
// 1) Create schema with a read-write connection
504+
var rw = factory.Create(dbPath, password);
505+
rw.ExecuteSQL("CREATE TABLE users (id INTEGER, name TEXT)");

0 commit comments

Comments
 (0)