allsource-core 0.29.0

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
//! Event search over a live [`EventStore`]: semantic search through
//! [`SemanticSearchUseCase`] and hybrid search through [`HybridSearchEngine`],
//! both fed from the store's ingest broadcast.
//!
//! Reads never index. [`EventSearch::catch_up`] does, and refuses until an
//! embedder is installed; searches answer [`EventSearchError::Unavailable`]
//! until the first catch-up has completed.

use super::{
    EmbeddedEvent, HybridSearchEngine, KeywordSearchEngine, SearchQuery, VectorSearchEngine,
    VectorSearchEngineConfig, hybrid_search_engine::SearchMode,
};
use crate::{
    application::{
        dto::QueryEventsRequest,
        services::{IndexEventRequest, VectorSearchConfig, VectorSearchService},
        use_cases::{SemanticSearchUseCase, SemanticSearchUseCaseRequest},
    },
    domain::{entities::Event, repositories::EventRepository, value_objects::EmbeddingVector},
    error::{AllSourceError, Result},
    infrastructure::{
        memory::ledger::{Owner, Tally},
        repositories::InMemoryVectorSearchRepository,
    },
    store::EventStore,
};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use std::{
    collections::HashSet,
    sync::{
        Arc, OnceLock,
        atomic::{AtomicBool, Ordering},
    },
};
use tokio::sync::{Mutex, broadcast};
use uuid::Uuid;

pub const MAX_SEARCH_LIMIT: usize = 100;
const OVER_FETCH: usize = 5;
const MAX_FETCH: usize = 1000;
const RESCAN_PAGE: usize = 1000;

/// Turns text into the vector space events are indexed in.
pub trait QueryEmbedder: Send + Sync {
    fn embed(&self, text: &str) -> Result<Vec<f32>>;
}

#[cfg(feature = "prime-vectors")]
impl QueryEmbedder for crate::prime::vectors::TextEmbedder {
    fn embed(&self, text: &str) -> Result<Vec<f32>> {
        crate::prime::vectors::TextEmbedder::embed(self, text)
            .map_err(|e| AllSourceError::InternalError(e.to_string()))
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum EventSearchError {
    /// No embedder, index not built yet, or the embedder failed on the query.
    Unavailable(String),
    InvalidInput(String),
    Internal(String),
}

impl std::fmt::Display for EventSearchError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Unavailable(m) | Self::InvalidInput(m) | Self::Internal(m) => f.write_str(m),
        }
    }
}

impl From<AllSourceError> for EventSearchError {
    fn from(err: AllSourceError) -> Self {
        match err {
            AllSourceError::InvalidInput(m) | AllSourceError::InvalidQuery(m) => {
                Self::InvalidInput(m)
            }
            other => Self::Internal(other.to_string()),
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct SemanticSearchParams {
    pub query: String,
    pub limit: Option<usize>,
    pub event_type: Option<String>,
    pub min_similarity: Option<f32>,
}

#[derive(Debug, Clone, Default)]
pub struct HybridSearchParams {
    pub query: String,
    pub limit: Option<usize>,
    pub event_type: Option<String>,
    pub entity_id: Option<String>,
    pub semantic_weight: Option<f32>,
    pub keyword_weight: Option<f32>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct EventSearchHit {
    pub event_id: Uuid,
    pub score: f32,
    pub semantic_score: Option<f32>,
    pub keyword_score: Option<f32>,
    pub event_type: String,
    pub entity_id: String,
    pub timestamp: DateTime<Utc>,
}

struct IndexerState {
    events: broadcast::Receiver<Arc<Event>>,
    indexed: HashSet<Uuid>,
    needs_rescan: bool,
    /// Events the store should hold if every ingest reached the broadcast.
    expected_total: usize,
}

pub struct EventSearch {
    store: Arc<EventStore>,
    embedder: OnceLock<Arc<dyn QueryEmbedder>>,
    vector_service: Arc<VectorSearchService>,
    semantic: SemanticSearchUseCase,
    hybrid: HybridSearchEngine,
    indexer: Mutex<IndexerState>,
    ready: AtomicBool,
    /// Indexed ids, the event payloads the keyword and hybrid indexes keep, and
    /// the embeddings: charged to `search` as they are added.
    charged: Tally,
}

impl EventSearch {
    /// Subscribes to the store's ingest broadcast immediately, so nothing
    /// ingested between now and the first catch-up is missed.
    pub fn new(store: Arc<EventStore>) -> Result<Arc<Self>> {
        let vector_service = Arc::new(
            VectorSearchService::new(Arc::new(InMemoryVectorSearchRepository::new())).with_config(
                VectorSearchConfig {
                    max_k: MAX_FETCH,
                    ..VectorSearchConfig::default()
                },
            ),
        );
        let event_repository: Arc<dyn EventRepository> =
            Arc::new(StoreEventRepository(Arc::clone(&store)));
        let semantic = SemanticSearchUseCase::new(Arc::clone(&vector_service), event_repository);
        let hybrid = HybridSearchEngine::new(
            Arc::new(VectorSearchEngine::with_external_embeddings(
                VectorSearchEngineConfig::default(),
            )),
            Arc::new(KeywordSearchEngine::new()?),
        );
        let events = store.subscribe_events();
        Ok(Arc::new(Self {
            store,
            embedder: OnceLock::new(),
            vector_service,
            semantic,
            hybrid,
            indexer: Mutex::new(IndexerState {
                events,
                indexed: HashSet::new(),
                needs_rescan: true,
                expected_total: 0,
            }),
            ready: AtomicBool::new(false),
            charged: Tally::new(Owner::Search),
        }))
    }

    /// A second embedder is ignored: the index must stay in one vector space.
    pub fn set_embedder(&self, embedder: Arc<dyn QueryEmbedder>) {
        if self.embedder.set(embedder).is_err() {
            tracing::warn!("event search embedder installed twice; keeping the first");
        }
    }

    pub fn is_ready(&self) -> bool {
        self.ready.load(Ordering::Acquire)
    }

    /// Indexes every event the store holds that is not indexed yet.
    /// Returns how many events were newly indexed.
    /// Without an embedder only the keyword index is built, so hybrid search
    /// answers keyword-only and semantic search stays unavailable.
    pub async fn catch_up(&self) -> std::result::Result<usize, EventSearchError> {
        let embedder = self.embedder.get().cloned();
        let mut state = self.indexer.lock().await;

        let total_before_drain = self.store.total_events();
        let mut pending = Vec::new();
        loop {
            match state.events.try_recv() {
                Ok(event) => {
                    state.expected_total += 1;
                    pending.push(event);
                }
                Err(broadcast::error::TryRecvError::Lagged(missed)) => {
                    tracing::warn!("event search missed {missed} ingest notifications; rescanning");
                    state.needs_rescan = true;
                }
                Err(_) => break,
            }
        }
        if total_before_drain > state.expected_total {
            state.needs_rescan = true;
        }

        let mut indexed = 0;
        if state.needs_rescan {
            let total = self.store.total_events();
            let mut offset = 0u64;
            loop {
                let page = self.store.events_after_offset(offset, &[], RESCAN_PAGE);
                let Some((last, _)) = page.last() else { break };
                offset = *last;
                let events: Vec<Arc<Event>> = page.into_iter().map(|(_, e)| Arc::new(e)).collect();
                indexed += self.index(&mut state, embedder.as_ref(), events).await?;
            }
            state.expected_total = state.expected_total.max(total);
            state.needs_rescan = false;
        }
        indexed += self.index(&mut state, embedder.as_ref(), pending).await?;

        self.ready.store(true, Ordering::Release);
        Ok(indexed)
    }

    pub async fn semantic(
        &self,
        tenant_id: &str,
        params: SemanticSearchParams,
    ) -> std::result::Result<Vec<EventSearchHit>, EventSearchError> {
        let limit = validate_limit(params.limit)?;
        let query = validate_query(&params.query)?;
        if let Some(min) = params.min_similarity
            && !(min.is_finite() && (-1.0..=1.0).contains(&min))
        {
            return Err(EventSearchError::InvalidInput(
                "min_similarity must be a number between -1 and 1".to_string(),
            ));
        }
        let embedding = self.embed_query(query)?;
        let fetch = if params.event_type.is_some() {
            (limit * OVER_FETCH).min(MAX_FETCH)
        } else {
            limit
        };

        let response = self
            .semantic
            .execute(SemanticSearchUseCaseRequest {
                query_embedding: Some(embedding),
                k: Some(fetch),
                tenant_id: Some(tenant_id.to_string()),
                event_type: params.event_type.clone(),
                min_similarity: params.min_similarity,
                ..SemanticSearchUseCaseRequest::default()
            })
            .await?;

        let mut hits = Vec::with_capacity(limit);
        for result in response.results {
            let Some(event) = self.tenant_event(tenant_id, result.event_id)? else {
                continue;
            };
            if params
                .event_type
                .as_deref()
                .is_some_and(|t| t != event.event_type_str())
            {
                continue;
            }
            hits.push(hit(&event, result.score, None, None));
            if hits.len() == limit {
                break;
            }
        }
        Ok(hits)
    }

    pub fn hybrid(
        &self,
        tenant_id: &str,
        params: HybridSearchParams,
    ) -> std::result::Result<Vec<EventSearchHit>, EventSearchError> {
        let limit = validate_limit(params.limit)?;
        let query = validate_query(&params.query)?;
        for (name, weight) in [
            ("semantic_weight", params.semantic_weight),
            ("keyword_weight", params.keyword_weight),
        ] {
            if let Some(w) = weight
                && !(w.is_finite() && w >= 0.0)
            {
                return Err(EventSearchError::InvalidInput(format!(
                    "{name} must be a non-negative number"
                )));
            }
        }
        let mut search = SearchQuery::new(query)
            .with_limit(limit)
            .with_tenant(tenant_id)
            .with_weights(params.semantic_weight, params.keyword_weight);
        if self.embedder.get().is_some() {
            search = search.with_query_embedding(self.embed_query(query)?);
        } else {
            self.require_ready()?;
            search = search.with_mode(SearchMode::KeywordOnly);
        }
        if let Some(event_type) = params.event_type {
            search = search.with_event_type(event_type);
        }
        if let Some(entity_id) = params.entity_id {
            search = search.with_entity_id(entity_id);
        }

        let mut hits = Vec::new();
        for result in self.hybrid.search(&search)? {
            if let Some(event) = self.tenant_event(tenant_id, result.event_id)? {
                hits.push(hit(
                    &event,
                    result.score,
                    result.semantic_score,
                    result.keyword_score,
                ));
            }
        }
        Ok(hits)
    }

    fn embedder(&self) -> std::result::Result<Arc<dyn QueryEmbedder>, EventSearchError> {
        self.embedder.get().cloned().ok_or_else(|| {
            EventSearchError::Unavailable(
                "event search is unavailable: no embedding model is loaded in this Core"
                    .to_string(),
            )
        })
    }

    fn require_ready(&self) -> std::result::Result<(), EventSearchError> {
        if self.is_ready() {
            Ok(())
        } else {
            Err(EventSearchError::Unavailable(
                "event search index is still being built; retry shortly".to_string(),
            ))
        }
    }

    fn embed_query(&self, query: &str) -> std::result::Result<Vec<f32>, EventSearchError> {
        let embedder = self.embedder()?;
        self.require_ready()?;
        embedder.embed(query).map_err(|e| {
            EventSearchError::Unavailable(format!("could not embed the search query: {e}"))
        })
    }

    /// Defence in depth: a hit is returned only when the store still holds the
    /// event and it belongs to the caller's tenant.
    fn tenant_event(
        &self,
        tenant_id: &str,
        event_id: Uuid,
    ) -> std::result::Result<Option<Event>, EventSearchError> {
        Ok(self
            .store
            .get_event_by_id(&event_id)?
            .filter(|e| e.tenant_id_str() == tenant_id))
    }

    async fn index(
        &self,
        state: &mut IndexerState,
        embedder: Option<&Arc<dyn QueryEmbedder>>,
        events: Vec<Arc<Event>>,
    ) -> std::result::Result<usize, EventSearchError> {
        let fresh: Vec<Arc<Event>> = events
            .into_iter()
            .filter(|e| !state.indexed.contains(&e.id()))
            .collect();
        if fresh.is_empty() {
            return Ok(0);
        }

        let embeddings: Vec<Option<Vec<f32>>> = match embedder {
            Some(embedder) => self.embed_batch(embedder, &fresh).await?,
            None => vec![None; fresh.len()],
        };

        let mut documents = Vec::with_capacity(fresh.len());
        let mut charge = 0u64;
        for (event, embedding) in fresh.iter().zip(embeddings) {
            charge += event.resident_bytes();
            if let Some(values) = &embedding {
                charge += (values.len() * size_of::<f32>()) as u64;
                let source_text = VectorSearchEngine::extract_text_from_payload(event.payload());
                self.vector_service
                    .index_event(IndexEventRequest {
                        event_id: event.id(),
                        tenant_id: event.tenant_id_str().to_string(),
                        embedding: EmbeddingVector::new(values.clone())?,
                        source_text: Some(source_text),
                    })
                    .await?;
            }
            documents.push(EmbeddedEvent {
                event_id: event.id(),
                tenant_id: event.tenant_id_str().to_string(),
                event_type: event.event_type_str().to_string(),
                entity_id: Some(event.entity_id_str().to_string()),
                payload: event.payload().clone(),
                timestamp: event.timestamp(),
                embedding,
            });
        }
        self.hybrid.index_embedded(documents)?;

        self.charged.add(charge);
        let slots_before = state.indexed.capacity();
        state.indexed.extend(fresh.iter().map(|e| e.id()));
        self.charged
            .grow(slots_before, state.indexed.capacity(), size_of::<Uuid>());
        Ok(fresh.len())
    }

    async fn embed_batch(
        &self,
        embedder: &Arc<dyn QueryEmbedder>,
        events: &[Arc<Event>],
    ) -> std::result::Result<Vec<Option<Vec<f32>>>, EventSearchError> {
        let to_embed = Arc::clone(embedder);
        let batch = events.to_vec();
        tokio::task::spawn_blocking(move || {
            batch
                .iter()
                .map(|event| {
                    let text = VectorSearchEngine::extract_text_from_payload(event.payload());
                    if text.is_empty() {
                        return None;
                    }
                    to_embed
                        .embed(&text)
                        .inspect_err(|e| {
                            tracing::warn!(
                                "event search could not embed event {}: {e}",
                                event.id()
                            );
                        })
                        .ok()
                })
                .collect()
        })
        .await
        .map_err(|e| EventSearchError::Internal(format!("embedding task failed: {e}")))
    }
}

fn validate_limit(limit: Option<usize>) -> std::result::Result<usize, EventSearchError> {
    match limit.unwrap_or(10) {
        0 => Err(EventSearchError::InvalidInput(
            "limit must be at least 1".to_string(),
        )),
        n if n > MAX_SEARCH_LIMIT => Err(EventSearchError::InvalidInput(format!(
            "limit cannot exceed {MAX_SEARCH_LIMIT}"
        ))),
        n => Ok(n),
    }
}

fn validate_query(query: &str) -> std::result::Result<&str, EventSearchError> {
    let trimmed = query.trim();
    if trimmed.is_empty() {
        return Err(EventSearchError::InvalidInput(
            "query must not be empty".to_string(),
        ));
    }
    Ok(trimmed)
}

fn hit(
    event: &Event,
    score: f32,
    semantic_score: Option<f32>,
    keyword_score: Option<f32>,
) -> EventSearchHit {
    EventSearchHit {
        event_id: event.id(),
        score,
        semantic_score,
        keyword_score,
        event_type: event.event_type_str().to_string(),
        entity_id: event.entity_id_str().to_string(),
        timestamp: event.timestamp(),
    }
}

/// [`EventRepository`] over the live store, for the semantic search use case.
struct StoreEventRepository(Arc<EventStore>);

impl StoreEventRepository {
    fn query(&self, request: &QueryEventsRequest) -> Result<Vec<Event>> {
        self.0.query(request)
    }
}

#[async_trait]
impl EventRepository for StoreEventRepository {
    async fn save(&self, event: &Event) -> Result<()> {
        self.0.ingest(event)
    }

    async fn save_batch(&self, events: &[Event]) -> Result<()> {
        self.0.ingest_batch(events.to_vec())
    }

    async fn find_by_id(&self, id: Uuid) -> Result<Option<Event>> {
        self.0.get_event_by_id(&id)
    }

    async fn find_by_entity(&self, entity_id: &str, tenant_id: &str) -> Result<Vec<Event>> {
        self.query(&QueryEventsRequest {
            entity_id: Some(entity_id.to_string()),
            tenant_id: Some(tenant_id.to_string()),
            ..QueryEventsRequest::default()
        })
    }

    async fn find_by_type(&self, event_type: &str, tenant_id: &str) -> Result<Vec<Event>> {
        self.query(&QueryEventsRequest {
            event_type: Some(event_type.to_string()),
            tenant_id: Some(tenant_id.to_string()),
            ..QueryEventsRequest::default()
        })
    }

    async fn find_by_time_range(
        &self,
        tenant_id: &str,
        start: DateTime<Utc>,
        end: DateTime<Utc>,
    ) -> Result<Vec<Event>> {
        self.query(&QueryEventsRequest {
            tenant_id: Some(tenant_id.to_string()),
            since: Some(start),
            until: Some(end),
            ..QueryEventsRequest::default()
        })
    }

    async fn find_by_entity_as_of(
        &self,
        entity_id: &str,
        tenant_id: &str,
        as_of: DateTime<Utc>,
    ) -> Result<Vec<Event>> {
        self.query(&QueryEventsRequest {
            entity_id: Some(entity_id.to_string()),
            tenant_id: Some(tenant_id.to_string()),
            as_of: Some(as_of),
            ..QueryEventsRequest::default()
        })
    }

    async fn count(&self, tenant_id: &str) -> Result<usize> {
        Ok(self
            .query(&QueryEventsRequest {
                tenant_id: Some(tenant_id.to_string()),
                ..QueryEventsRequest::default()
            })?
            .len())
    }

    async fn health_check(&self) -> Result<()> {
        Ok(())
    }
}