allsource-core 0.7.3

High-performance event store core built in Rust
Documentation
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
# AllSource Core - High-Performance Event Store

> AI-native event store built in Rust with columnar storage, schema validation, event replay, and stream processing

[![crates.io](https://img.shields.io/crates/v/allsource-core.svg)](https://crates.io/crates/allsource-core)
[![docs.rs](https://docs.rs/allsource-core/badge.svg)](https://docs.rs/allsource-core)
[![Rust](https://img.shields.io/badge/rust-1.75%2B-orange.svg)](https://www.rust-lang.org/)
[![Tests](https://img.shields.io/badge/tests-250%20passing-brightgreen.svg)]()
[![Performance](https://img.shields.io/badge/throughput-469K%20events%2Fsec-blue.svg)]()
[![Architecture](https://img.shields.io/badge/clean%20architecture-Phase%203%20Infrastructure-success.svg)]()
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

## ๐ŸŽฏ What is AllSource?

AllSource is a high-performance event store designed for modern event-sourcing and CQRS architectures. Built with a **polyglot architecture**:

- **Rust Core** (this service): High-performance event store engine with columnar storage
- **Go Control Plane** (`services/control-plane`): Orchestration, monitoring, and management layer

The Rust core provides blazing-fast event ingestion (469K events/sec) and sub-microsecond queries, while the Go control plane handles cluster coordination and operational tasks.

**Current Version**: v0.7.1 ยท [crates.io](https://crates.io/crates/allsource-core) ยท [docs.rs](https://docs.rs/allsource-core)

## Installation

`allsource-core` is distributed as a **library crate** via [crates.io](https://crates.io/crates/allsource-core).

### Add to Your Project

```bash
cargo add allsource-core@0.7
```

Or add to your `Cargo.toml` (pin to minor version for stability):

```toml
[dependencies]
# allsource-core: High-performance event store
# Pin to minor version - allows patch updates only
allsource-core = "0.7"
```

> **Version Pinning Best Practice**: We recommend `"0.7"` (minor version) rather than `"0.7.1"` (exact) or `"0"` (major only). This allows automatic patch updates while avoiding breaking changes.

### Usage Patterns

1. **Embedded Library** - Import directly into your Rust application
2. **Standalone Server** - Build your own binary using the library
3. **Serverless** - Deploy in AWS Lambda or similar environments

## โœจ Features

### ๐Ÿš€ Core Event Store (v0.1)

- **Immutable Event Log**: Append-only storage with complete audit trail
- **Time-Travel Queries**: Query entity state as of any timestamp
- **Concurrent Indexing**: Lock-free indexing using `DashMap` for O(1) lookups
- **Real-time Projections**: Built-in materialized views with custom projection support
- **High Performance**: 469K+ events/sec throughput, sub-millisecond queries
- **Type-Safe API**: Strong typing with Rust's ownership system

### ๐Ÿ’พ Persistence & Durability (v0.2)

- **Parquet Columnar Storage**: Apache Arrow-based storage for analytics
- **Write-Ahead Log (WAL)**: Crash recovery with full durability guarantees
- **Snapshot System**: Point-in-time snapshots with automatic optimization
- **Automatic Compaction**: Background file merging for storage efficiency
- **WebSocket Streaming**: Real-time event broadcasting to connected clients
- **Advanced Analytics**: Event frequency, correlation analysis, statistical summaries

### ๐Ÿ“‹ Schema Registry (v0.5)

- **JSON Schema Validation**: Enforce event contracts at ingestion time
- **Schema Versioning**: Automatic version management with compatibility checking
- **Compatibility Modes**: Backward, Forward, Full, or None
- **Breaking Change Prevention**: Validate schema evolution before deployment
- **Subject Organization**: Group schemas by domain or event type

### ๐Ÿ”„ Event Replay & Projections (v0.5)

- **Point-in-Time Replay**: Replay events from any timestamp
- **Projection Rebuilding**: Reconstruct materialized views with progress tracking
- **Batch Processing**: Configurable batch sizes for optimal performance
- **Async Execution**: Non-blocking background replay operations
- **Cancellable Operations**: Stop replays gracefully with proper cleanup
- **Progress Metrics**: Real-time statistics (events/sec, percentage complete)

### ๐Ÿ” Security & Multi-tenancy (v0.7)

- **Authentication**: JWT tokens with RBAC, API keys for services
- **Authorization**: Role-based access control (Admin, Operator, Reader)
- **Rate Limiting**: Per-tenant configurable limits
- **IP Filtering**: Global allowlist/blocklist support
- **Audit Logging**: Comprehensive audit trail with PostgreSQL support
- **Tenant Isolation**: Repository-level isolation with quotas
- **Serverless Ready**: Graceful shutdown, PORT env var, optimized containers

### โšก Stream Processing (v0.5)

- **6 Built-in Operators**:
  - **Filter**: eq, ne, gt, lt, contains operations
  - **Map**: Transform field values (uppercase, lowercase, trim, math)
  - **Reduce**: Aggregations (count, sum, avg, min, max) with grouping
  - **Window**: Time-based aggregations (tumbling, sliding, session)
  - **Enrich**: External data lookup and enrichment
  - **Branch**: Conditional event routing
- **Stateful Processing**: Thread-safe state management for aggregations
- **Window Buffers**: Automatic time-based event eviction
- **Pipeline Statistics**: Track processing metrics per pipeline
- **Integrated Processing**: Events flow through pipelines during ingestion

### ๐Ÿ—๏ธ Clean Architecture

The codebase follows a strict layered architecture for maintainability and testability:

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Infrastructure Layer                     โ”‚
โ”‚  (HTTP handlers, WebSocket, persistence, security)          โ”‚
โ”‚  infrastructure::web, infrastructure::persistence,          โ”‚
โ”‚  infrastructure::security, infrastructure::repositories     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                    Application Layer                        โ”‚
โ”‚  (Use cases, services, DTOs)                                โ”‚
โ”‚  application::use_cases, application::services,             โ”‚
โ”‚  application::dto                                           โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                      Domain Layer                           โ”‚
โ”‚  (Entities, value objects, repository traits)               โ”‚
โ”‚  domain::entities, domain::value_objects,                   โ”‚
โ”‚  domain::repositories                                       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

**Module Organization:**

| Layer | Path | Contents |
|-------|------|----------|
| Domain | `domain::entities` | Event, Tenant, Schema, Projection, AuditEvent, EventStream |
| Domain | `domain::value_objects` | EntityId, TenantId, EventType, PartitionKey |
| Domain | `domain::repositories` | Repository trait definitions (no implementations) |
| Application | `application::use_cases` | IngestEvent, QueryEvents, ManageTenant, ManageSchema |
| Application | `application::services` | AuditLogger, Pipeline, Replay, Analytics |
| Application | `application::dto` | Request/Response DTOs for API boundaries |
| Infrastructure | `infrastructure::web` | HTTP handlers, WebSocket, API routes |
| Infrastructure | `infrastructure::persistence` | Storage, WAL, Snapshots, Compaction, Index |
| Infrastructure | `infrastructure::security` | Auth, Middleware, Rate Limiting |
| Infrastructure | `infrastructure::repositories` | In-memory, PostgreSQL, RocksDB implementations |

**Dependency Rule:** Inner layers never depend on outer layers. Domain has zero external dependencies.

### ๐Ÿ”๏ธ SierraDB-Inspired Production Patterns (NEW)

Based on battle-tested patterns from [SierraDB](https://github.com/cablehead/xs), a production-grade event store:

**PartitionKey** - Fixed Partition Architecture
- โœ… 32 fixed partitions for single-node deployment (scalable to 1024+)
- โœ… Consistent hashing ensures same entity always maps to same partition
- โœ… Sequential writes within partitions for ordering guarantees
- โœ… Ready for horizontal scaling with node assignment
- โœ… 6 comprehensive tests covering distribution and consistency

**EventStream** - Gapless Version Guarantees
- โœ… Watermark system tracks "highest continuously confirmed sequence"
- โœ… Prevents gaps that would break event sourcing guarantees
- โœ… Optimistic locking prevents concurrent modification conflicts
- โœ… Version numbers start at 1 and increment sequentially
- โœ… 9 tests covering versioning, concurrency, and gap detection

**EventStreamRepository** - Infrastructure Implementation
- โœ… Thread-safe in-memory implementation with parking_lot RwLock
- โœ… Partition-aware stream storage and retrieval
- โœ… Watermark tracking and gapless verification
- โœ… Optimistic locking enforcement at repository level
- โœ… 8 comprehensive tests covering all operations

**StorageIntegrity** - Corruption Prevention
- โœ… SHA-256 checksums for data integrity
- โœ… WAL segment verification
- โœ… Parquet file integrity checking
- โœ… Batch verification with progress reporting
- โœ… 8 tests covering checksums and file verification

**7-Day Stress Tests** - Production Hardening
- โœ… Continuous ingestion tests (7 days, 1 hour, 5 minutes configs)
- โœ… Memory leak detection
- โœ… Corruption detection over time
- โœ… Partition balance monitoring
- โœ… 4 tests + configurable long-running tests

**Why These Patterns?**
| Pattern | SierraDB's Lesson | Our Benefit |
|---------|-------------------|-------------|
| Fixed Partitions | Sequential writes enable gapless sequences | Horizontal scaling without complex coordination |
| Gapless Versions | Watermark prevents data gaps | Consistent event sourcing guarantees |
| Optimistic Locking | Detect concurrent modifications | Safe concurrent access without heavy locks |

**Implemented**:
- โœ… Storage integrity checksums (prevent silent corruption)
- โœ… 7-day continuous stress tests (production confidence)

**Coming Next**:
- โšก Zero-copy deserialization (performance optimization)
- ๐Ÿ”„ Batch processing optimizations
- ๐Ÿ”’ Lock-free data structures

## ๐Ÿ“Š Performance Benchmarks

Measured on Apple Silicon M-series (release build):

| Operation | Throughput/Latency | Details |
|-----------|-------------------|---------|
| Event Ingestion | 442-469K events/sec | Single-threaded |
| Entity Query | 11.9 ฮผs | Indexed lookup |
| Type Query | 2.4 ms | Cross-entity scan |
| State Reconstruction | 3.5 ฮผs | With snapshots |
| State Reconstruction | 3.8 ฮผs | Without snapshots |
| Concurrent Writes (8 workers) | 8.0 ms/batch | 100 events/batch |
| Parquet Batch Write | 3.5 ms | 1000 events |
| Snapshot Creation | 130 ฮผs | Per entity |
| WAL Sync Writes | 413 ms | 100 syncs |

**Test Coverage**: 250 tests - 100% passing
- Domain Layer: 177 tests (Value Objects, Entities, Business Rules, **SierraDB Patterns**)
  - **PartitionKey**: 6 tests (consistent hashing, distribution, node assignment)
  - **EventStream**: 9 tests (gapless versioning, optimistic locking, watermarks)
- Application Layer: 20 tests (Use Cases, DTOs)
- Infrastructure Layer: 53 tests (API, Storage, Services, **Repository Implementations**)
  - **InMemoryEventStreamRepository**: 8 tests (SierraDB pattern implementation)
  - **StorageIntegrity**: 8 tests (checksum verification, WAL/Parquet integrity)
  - **7-Day Stress Tests**: 4 tests (long-running corruption detection)

## ๐Ÿ”ง API Endpoints (38 Total)

### Core Event Store

```bash
# Health check
GET /health

# Ingest event
POST /api/v1/events

# Query events
GET /api/v1/events/query?entity_id=user-123
GET /api/v1/events/query?event_type=user.created
GET /api/v1/events/query?since=2024-01-15T00:00:00Z&limit=100

# Entity state
GET /api/v1/entities/:entity_id/state
GET /api/v1/entities/:entity_id/state?as_of=2024-01-15T10:00:00Z
GET /api/v1/entities/:entity_id/snapshot

# Statistics
GET /api/v1/stats
```

### WebSocket Streaming (v0.2)

```bash
# Real-time event stream
WS /api/v1/events/stream
```

### Analytics (v0.2)

```bash
# Event frequency analysis
GET /api/v1/analytics/frequency?event_type=user.created&bucket_size=3600

# Statistical summary
GET /api/v1/analytics/summary?entity_id=user-123

# Event correlation
GET /api/v1/analytics/correlation?event_a=user.created&event_b=order.placed
```

### Snapshots (v0.2)

```bash
# Create snapshot
POST /api/v1/snapshots

# List snapshots
GET /api/v1/snapshots
GET /api/v1/snapshots?entity_id=user-123

# Get latest snapshot
GET /api/v1/snapshots/:entity_id/latest
```

### Compaction (v0.2)

```bash
# Trigger manual compaction
POST /api/v1/compaction/trigger

# Get compaction stats
GET /api/v1/compaction/stats
```

### Schema Registry (v0.5)

```bash
# Register schema
POST /api/v1/schemas

# List subjects
GET /api/v1/schemas

# Get schema
GET /api/v1/schemas/:subject
GET /api/v1/schemas/:subject?version=2

# List versions
GET /api/v1/schemas/:subject/versions

# Validate event
POST /api/v1/schemas/validate

# Set compatibility mode
PUT /api/v1/schemas/:subject/compatibility
```

### Event Replay (v0.5)

```bash
# Start replay
POST /api/v1/replay

# List replays
GET /api/v1/replay

# Get progress
GET /api/v1/replay/:replay_id

# Cancel replay
POST /api/v1/replay/:replay_id/cancel

# Delete replay
DELETE /api/v1/replay/:replay_id
```

### Stream Processing Pipelines (v0.5)

```bash
# Register pipeline
POST /api/v1/pipelines

# List pipelines
GET /api/v1/pipelines

# Get pipeline
GET /api/v1/pipelines/:pipeline_id

# Remove pipeline
DELETE /api/v1/pipelines/:pipeline_id

# Get all stats
GET /api/v1/pipelines/stats

# Get pipeline stats
GET /api/v1/pipelines/:pipeline_id/stats

# Reset pipeline state
PUT /api/v1/pipelines/:pipeline_id/reset
```

## ๐Ÿ“ Project Structure (Clean Architecture)

Following **Clean Architecture** principles with clear separation of concerns:

```
services/core/src/
โ”œโ”€โ”€ main.rs                    # Application entry point
โ”œโ”€โ”€ lib.rs                     # Library exports
โ”œโ”€โ”€ error.rs                   # Error types and Result
โ”‚
โ”œโ”€โ”€ domain/                    # ๐Ÿ›๏ธ DOMAIN LAYER (Business Logic)
โ”‚   โ”œโ”€โ”€ entities/              # Core business entities
โ”‚   โ”‚   โ”œโ”€โ”€ event.rs          # Event entity (162 tests)
โ”‚   โ”‚   โ”œโ”€โ”€ tenant.rs         # Multi-tenancy entity
โ”‚   โ”‚   โ”œโ”€โ”€ schema.rs         # Schema registry entity
โ”‚   โ”‚   โ”œโ”€โ”€ projection.rs     # Projection entity
โ”‚   โ”‚   โ””โ”€โ”€ event_stream.rs   # ๐Ÿ†• Gapless event stream (9 tests)
โ”‚   โ””โ”€โ”€ value_objects/         # Self-validating value objects
โ”‚       โ”œโ”€โ”€ tenant_id.rs      # Tenant identifier
โ”‚       โ”œโ”€โ”€ event_type.rs     # Event type validation
โ”‚       โ”œโ”€โ”€ entity_id.rs      # Entity identifier
โ”‚       โ””โ”€โ”€ partition_key.rs  # ๐Ÿ†• Fixed partitioning (6 tests)
โ”‚
โ”œโ”€โ”€ application/               # ๐ŸŽฏ APPLICATION LAYER (Use Cases)
โ”‚   โ”œโ”€โ”€ dto/                   # Data Transfer Objects
โ”‚   โ”‚   โ”œโ”€โ”€ event_dto.rs      # Event request/response DTOs
โ”‚   โ”‚   โ”œโ”€โ”€ tenant_dto.rs     # Tenant DTOs
โ”‚   โ”‚   โ”œโ”€โ”€ schema_dto.rs     # Schema DTOs
โ”‚   โ”‚   โ””โ”€โ”€ projection_dto.rs # Projection DTOs
โ”‚   โ””โ”€โ”€ use_cases/             # Application business logic
โ”‚       โ”œโ”€โ”€ ingest_event.rs   # Event ingestion (3 tests)
โ”‚       โ”œโ”€โ”€ query_events.rs   # Event queries (4 tests)
โ”‚       โ”œโ”€โ”€ manage_tenant.rs  # Tenant management (5 tests)
โ”‚       โ”œโ”€โ”€ manage_schema.rs  # Schema operations (4 tests)
โ”‚       โ””โ”€โ”€ manage_projection.rs # Projection ops (4 tests)
โ”‚
โ””โ”€โ”€ infrastructure/            # ๐Ÿ”ง INFRASTRUCTURE LAYER (Technical)
    โ”œโ”€โ”€ repositories/         # ๐Ÿ†• Repository implementations (SierraDB)
    โ”‚   โ””โ”€โ”€ in_memory_event_stream_repository.rs  # Thread-safe, partitioned (8 tests)
    โ”œโ”€โ”€ persistence/          # ๐Ÿ†• Storage integrity (SierraDB)
    โ”‚   โ””โ”€โ”€ storage_integrity.rs  # Checksum verification (8 tests)
    โ”œโ”€โ”€ api.rs                # REST API endpoints (38 endpoints)
    โ”œโ”€โ”€ store.rs              # Event store implementation
    โ”œโ”€โ”€ storage.rs            # Parquet columnar storage
    โ”œโ”€โ”€ wal.rs                # Write-ahead log
    โ”œโ”€โ”€ snapshot.rs           # Snapshot management
    โ”œโ”€โ”€ compaction.rs         # Storage compaction
    โ”œโ”€โ”€ index.rs              # High-performance indexing
    โ”œโ”€โ”€ projection.rs         # Real-time projections
    โ”œโ”€โ”€ analytics.rs          # Analytics engine
    โ”œโ”€โ”€ websocket.rs          # WebSocket streaming
    โ”œโ”€โ”€ schema.rs             # Schema validation service
    โ”œโ”€โ”€ replay.rs             # Event replay engine
    โ”œโ”€โ”€ pipeline.rs           # Stream processing
    โ”œโ”€โ”€ backup.rs             # Backup management
    โ”œโ”€โ”€ auth.rs               # Authentication/Authorization
    โ”œโ”€โ”€ rate_limit.rs         # Rate limiting
    โ”œโ”€โ”€ tenant.rs             # Tenant service
    โ”œโ”€โ”€ metrics.rs            # Metrics collection
    โ”œโ”€โ”€ middleware.rs         # HTTP middleware
    โ”œโ”€โ”€ config.rs             # Configuration
    โ”œโ”€โ”€ tenant_api.rs         # Tenant API handlers
    โ””โ”€โ”€ auth_api.rs           # Auth API handlers

tests/
โ”œโ”€โ”€ integration_tests.rs      # End-to-end tests
โ””โ”€โ”€ stress_tests/             # ๐Ÿ†• Long-running stress tests (SierraDB)
    โ””โ”€โ”€ seven_day_stress.rs   # 7-day corruption detection (4 tests)

benches/
โ””โ”€โ”€ performance_benchmarks.rs # Performance benchmarks
```

### ๐Ÿ—๏ธ Clean Architecture Benefits

**Domain Layer** (Core Business Logic)
- โœ… Pure business rules with zero external dependencies
- โœ… Value Objects enforce invariants at construction time
- โœ… Entities contain rich domain behavior
- โœ… **SierraDB patterns** for production-grade event streaming
- โœ… 177 comprehensive domain tests (including 15 SierraDB tests)

**Application Layer** (Orchestration)
- โœ… Use Cases coordinate domain entities
- โœ… DTOs isolate external contracts from domain
- โœ… Clear input/output boundaries
- โœ… 20 use case tests covering all scenarios

**Infrastructure Layer** (Technical Details)
- โœ… Pluggable implementations (can swap storage, APIs)
- โœ… Framework and library dependencies isolated
- โœ… Domain and Application layers remain pure
- โœ… 37 infrastructure integration tests

## ๐Ÿš€ Quick Start

### Option 1: Use as a Library (Recommended)

Add to your `Cargo.toml`:

```toml
[dependencies]
allsource-core = "0.7"  # Pin to minor version
```

Or:

```bash
cargo add allsource-core@0.7
```

### Option 2: Build from Source

```bash
# Clone the repository
git clone https://github.com/all-source-os/chronos-monorepo.git
cd chronos-monorepo/apps/core

# Build
cargo build --release

# Run tests
cargo test

# Run benchmarks
cargo bench
```

### Running the Server

```bash
# Development mode
cargo run

# Production mode (optimized)
cargo run --release

# With debug logging
RUST_LOG=debug cargo run

# Custom port (modify main.rs)
# Default: 0.0.0.0:8080
```

### Example: Ingest Events

```bash
curl -X POST http://localhost:3900/api/v1/events \
  -H "Content-Type: application/json" \
  -d '{
    "event_type": "user.created",
    "entity_id": "user-123",
    "payload": {
      "name": "Alice",
      "email": "alice@example.com"
    }
  }'
```

### Example: Query Events

```bash
# Get all events for an entity
curl "http://localhost:3900/api/v1/events/query?entity_id=user-123"

# Time-travel query
curl "http://localhost:3900/api/v1/events/query?entity_id=user-123&as_of=2024-01-15T10:00:00Z"

# Query by type
curl "http://localhost:3900/api/v1/events/query?event_type=user.created&limit=10"
```

### Example: Register Schema (v0.5)

```bash
curl -X POST http://localhost:3900/api/v1/schemas \
  -H "Content-Type: application/json" \
  -d '{
    "subject": "user.created",
    "schema": {
      "type": "object",
      "required": ["name", "email"],
      "properties": {
        "name": {"type": "string"},
        "email": {"type": "string", "format": "email"}
      }
    }
  }'
```

### Example: Start Replay (v0.5)

```bash
curl -X POST http://localhost:3900/api/v1/replay \
  -H "Content-Type: application/json" \
  -d '{
    "projection_name": "user_snapshot",
    "from_timestamp": "2024-01-01T00:00:00Z",
    "config": {
      "batch_size": 1000,
      "emit_progress": true
    }
  }'
```

### Example: Create Pipeline (v0.5)

```bash
curl -X POST http://localhost:3900/api/v1/pipelines \
  -H "Content-Type: application/json" \
  -d '{
    "name": "user_analytics",
    "source_event_types": ["user.created", "user.updated"],
    "operators": [
      {
        "type": "filter",
        "field": "country",
        "value": "US",
        "op": "eq"
      },
      {
        "type": "reduce",
        "field": "id",
        "function": "count",
        "group_by": "country"
      }
    ],
    "enabled": true
  }'
```

## โœ… Quality Gates

AllSource Core enforces strict quality gates to maintain code quality, consistency, and reliability.

**See**: [QUALITY_GATES.md](../../docs/current/QUALITY_GATES.md) for complete documentation.

### Quick Start

```bash
# Run all quality gates (recommended before commit)
make check

# Auto-fix formatting and sorting
make format
make format-sort

# Full CI pipeline locally
make ci
```

### Quality Checks Enforced

- โœ… **Code Formatting** (rustfmt) - Consistent style across codebase
- โœ… **Code Quality** (clippy) - Zero warnings, catch anti-patterns
- โœ… **Dependency Sorting** (cargo-sort) - Alphabetically sorted Cargo.toml
- โœ… **Test Coverage** - All tests must pass
- โœ… **Build Verification** - Release build must succeed

### CI/CD Integration

Quality gates run automatically on:
- Every push to `main` or `develop`
- Every pull request
- Enforced before merge

**Workflow**: `.github/workflows/rust-quality.yml`

### Configuration Files

- `.clippy.toml` - Clippy linter configuration (MSRV: 1.70.0)
- `rustfmt.toml` - Code formatting rules (max width: 100)
- `cargo-sort.toml` - Dependency sorting configuration
- `Makefile` - Quality gate commands

## ๐Ÿงช Testing

```bash
# Run all tests
cargo test

# Run unit tests only
cargo test --lib

# Run integration tests
cargo test --test integration_tests

# Run specific test
cargo test test_replay_progress_tracking

# Run with output
cargo test -- --nocapture

# Run with logging
RUST_LOG=debug cargo test
```

## ๐Ÿ“Š Benchmarking

```bash
# Run all benchmarks
cargo bench

# Run specific benchmark suite
cargo bench ingestion_throughput
cargo bench query_performance
cargo bench state_reconstruction
cargo bench concurrent_writes

# View results
open target/criterion/report/index.html
```

## ๐Ÿ—๏ธ Architecture

### System Architecture

AllSource uses a **polyglot architecture** with specialized services:

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Go Control Plane (Port 8081)         โ”‚
โ”‚   โ€ข Cluster Management                  โ”‚
โ”‚   โ€ข Metrics Aggregation                 โ”‚
โ”‚   โ€ข Operation Orchestration             โ”‚
โ”‚   โ€ข Health Monitoring                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
              โ”‚ HTTP Client
              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Rust Event Store (Port 8080)         โ”‚
โ”‚   โ€ข Event Ingestion (469K/sec)          โ”‚
โ”‚   โ€ข Query Engine (<12ฮผs)                โ”‚
โ”‚   โ€ข Schema Registry                     โ”‚
โ”‚   โ€ข Stream Processing                   โ”‚
โ”‚   โ€ข Event Replay                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
              โ”‚
              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Storage Layer                         โ”‚
โ”‚   โ€ข Parquet (Columnar)                  โ”‚
โ”‚   โ€ข WAL (Durability)                    โ”‚
โ”‚   โ€ข Snapshots (Point-in-time)           โ”‚
โ”‚   โ€ข In-Memory Indexes                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

### Event Flow

When an event is ingested:

1. **Validation** - Check event integrity
2. **WAL Write** - Durable write-ahead log (v0.2)
3. **Indexing** - Update entity/type indexes
4. **Projections** - Update materialized views
5. **Pipelines** - Real-time stream processing (v0.5)
6. **Parquet Storage** - Columnar persistence (v0.2)
7. **In-Memory Store** - Fast access layer
8. **WebSocket Broadcast** - Real-time streaming (v0.2)
9. **Auto-Snapshots** - Create snapshots if needed (v0.2)

### Storage Architecture

```
Storage Layer:
โ”œโ”€โ”€ In-Memory Events (Vec<Event>)
โ”œโ”€โ”€ DashMap Indexes (entity_id, event_type)
โ”œโ”€โ”€ Parquet Files (columnar storage)
โ”œโ”€โ”€ WAL Segments (append-only logs)
โ””โ”€โ”€ Snapshots (point-in-time state)
```

### Concurrency Model

- **Lock-free Indexes**: DashMap for entity/type lookups
- **RwLock**: parking_lot RwLock for event list
- **Atomic Counters**: Lock-free statistics tracking
- **Async Runtime**: Tokio for background tasks (replay, compaction)

## ๐ŸŽฏ Roadmap

### โœ… v0.1 - Core Event Store (COMPLETED)
- [x] In-memory event storage
- [x] DashMap-based indexing
- [x] Entity state reconstruction
- [x] Basic projections
- [x] REST API
- [x] Query by entity/type/time

### โœ… v0.2 - Persistence & Durability (COMPLETED)
- [x] Parquet columnar storage
- [x] Write-ahead log (WAL)
- [x] Snapshot system
- [x] Automatic compaction
- [x] WebSocket streaming
- [x] Advanced analytics

### โœ… v0.5 - Schema & Processing (COMPLETED)
- [x] Schema registry with versioning
- [x] Event replay engine
- [x] Projection rebuilding
- [x] Stream processing pipelines
- [x] Stateful aggregations
- [x] Window operations

### โœ… v0.6 - Clean Architecture Refactoring (PHASE 2 COMPLETED)
- [x] **Phase 1**: Domain Layer (162 tests)
  - [x] Value Objects (TenantId, EventType, EntityId)
  - [x] Domain Entities (Event, Tenant, Schema, Projection)
  - [x] Business rule enforcement
  - [x] Self-validating types
- [x] **Phase 2**: Application Layer (20 tests)
  - [x] DTOs for all operations
  - [x] Tenant management use cases
  - [x] Schema management use cases
  - [x] Projection management use cases
  - [x] Clean separation from domain
- [ ] **Phase 3**: Infrastructure Layer (IN PROGRESS)
  - [ ] Repository pattern implementation
  - [ ] API layer refactoring
  - [ ] Service layer extraction
  - [ ] Dependency injection

### โœ… v0.7 - Security & Cloud-Native (COMPLETED)
- [x] **Security Infrastructure**
  - [x] JWT authentication with role-based access control (RBAC)
  - [x] API key authentication for service-to-service
  - [x] Rate limiting per tenant
  - [x] IP filtering (global allowlist/blocklist)
  - [x] Request ID tracking for audit trail
- [x] **Multi-tenancy with Quotas**
  - [x] Tenant isolation at repository level
  - [x] Configurable quotas (events/day, storage, rate limits)
  - [x] Tenant activation/deactivation
- [x] **Audit Logging**
  - [x] Comprehensive audit events for all operations
  - [x] PostgreSQL audit repository
  - [x] In-memory audit repository for testing
- [x] **Serverless & Cloud Support**
  - [x] Graceful shutdown (SIGTERM handling)
  - [x] PORT environment variable support
  - [x] Fly.io deployment configurations
  - [x] Google Cloud Run configurations
  - [x] Helm charts for Kubernetes
- [x] **Performance Optimizations**
  - [x] Arena-based memory pooling (2-5ns allocations)
  - [x] SIMD JSON parsing
  - [x] Lock-free batch processing

### ๐Ÿ“‹ v0.8 - Advanced Features (PLANNED)
- [x] Multi-tenancy support (Complete)
- [x] Audit logging (Complete)
- [ ] Event encryption at rest
- [ ] Retention policies
- [ ] Data archival
- [ ] Backup/restore enhancements

### ๐ŸŒ v1.0 - Distributed & Cloud-Native (PLANNED)
- [ ] Distributed replication
- [ ] Multi-region support
- [ ] Consensus protocol (Raft)
- [ ] Arrow Flight RPC
- [ ] Kubernetes operators
- [ ] Cloud-native deployment
- [ ] Horizontal scaling
- [ ] Load balancing

### ๐Ÿ”ฎ Future Enhancements (BACKLOG)
- [ ] GraphQL API
- [ ] WASM plugin system
- [ ] Change Data Capture (CDC)
- [ ] Time-series optimization
- [ ] Machine learning integrations
- [ ] Real-time anomaly detection
- [ ] Event sourcing templates
- [ ] Visual query builder

## ๐Ÿ”ฌ Technical Decisions

### Why Rust?

- **Performance**: Zero-cost abstractions, no GC pauses
- **Safety**: Ownership model prevents data races
- **Concurrency**: Fearless concurrency with Send/Sync
- **Ecosystem**: Excellent libraries (Tokio, Axum, Arrow)

### Why DashMap?

- Lock-free concurrent HashMap
- Better than `RwLock<HashMap>` for reads
- Sharded internally for minimal contention
- O(1) lookups with concurrent access

### Why Apache Arrow/Parquet?

- Industry-standard columnar format
- Zero-copy data access
- SIMD-accelerated operations
- Excellent compression ratios
- Interoperable with DataFusion, Polars, DuckDB

### Why Tokio + Axum?

- High-performance async runtime
- Type-safe request handlers
- Excellent ecosystem integration
- Low overhead, fast routing

### Why parking_lot?

- Smaller and faster than std::sync::RwLock
- No poisoning - simpler error handling
- Better performance under contention
- Widely used in production Rust

## ๐Ÿ“š Usage Examples

### Programmatic API

```rust
use allsource_core::{EventStore, Event, QueryEventsRequest};
use serde_json::json;

// Create store
let store = EventStore::new();

// Ingest events
let event = Event::new(
    "user.created".to_string(),
    "user-123".to_string(),
    json!({
        "name": "Alice",
        "email": "alice@example.com"
    })
);
store.ingest(event)?;

// Query events
let request = QueryEventsRequest {
    entity_id: Some("user-123".to_string()),
    ..Default::default()
};
let events = store.query(request)?;

// Reconstruct state
let state = store.reconstruct_state("user-123", None)?;
println!("Current state: {}", state);

// Time-travel query
let timestamp = chrono::Utc::now() - chrono::Duration::hours(1);
let past_state = store.reconstruct_state("user-123", Some(timestamp))?;
println!("State 1 hour ago: {}", past_state);
```

### Custom Projection

```rust
use allsource_core::projection::Projection;
use allsource_core::event::Event;
use serde_json::Value;
use parking_lot::RwLock;
use std::collections::HashMap;

struct RevenueProjection {
    totals: RwLock<HashMap<String, f64>>,
}

impl Projection for RevenueProjection {
    fn name(&self) -> &str {
        "revenue_by_customer"
    }

    fn process(&self, event: &Event) -> allsource_core::Result<()> {
        if event.event_type == "order.completed" {
            if let Some(amount) = event.payload["amount"].as_f64() {
                let mut totals = self.totals.write();
                *totals.entry(event.entity_id.clone()).or_insert(0.0) += amount;
            }
        }
        Ok(())
    }

    fn get_state(&self, entity_id: &str) -> Option<Value> {
        self.totals.read()
            .get(entity_id)
            .map(|total| json!({ "total_revenue": total }))
    }

    fn clear(&self) {
        self.totals.write().clear();
    }
}
```

## ๐Ÿ› Troubleshooting

### Port Already in Use

```bash
# Find process using port 8080
lsof -i :8080

# Kill process
kill -9 <PID>
```

### Slow Performance

```bash
# Always use release mode for benchmarks
cargo run --release
cargo bench

# Check system resources
top -o cpu
```

### Memory Issues

```bash
# Monitor memory usage
RUST_LOG=allsource_core=debug cargo run --release

# Reduce batch sizes in config
# Adjust snapshot_config.max_events_before_snapshot
```

### Test Failures

```bash
# Clean build
cargo clean
cargo test

# Check for stale processes
killall allsource-core

# Verbose test output
cargo test -- --nocapture --test-threads=1
```

## ๐Ÿ“– Resources

- [Event Sourcing Pattern]https://martinfowler.com/eaaDev/EventSourcing.html
- [CQRS Pattern]https://martinfowler.com/bliki/CQRS.html
- [Apache Arrow]https://arrow.apache.org/
- [Parquet Format]https://parquet.apache.org/
- [Tokio Async Runtime]https://tokio.rs/
- [Axum Web Framework]https://docs.rs/axum

## ๐Ÿค Contributing

Contributions are welcome! Areas of interest:

- Performance optimizations
- Additional projection types
- Query optimization
- Documentation improvements
- Bug fixes and tests

## ๐Ÿ“„ License

MIT License - see LICENSE file for details

---

<div align="center">

**AllSource Core** - *Event sourcing, done right*

Built with ๐Ÿฆ€ Rust | Clean Architecture | Made for Production

Version 0.7.1 | 469K events/sec | 470+ tests passing | Security & Cloud-Native

</div>