bevy_persistence_database 0.3.0

A persistence and database integration solution for the Bevy game engine
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
use crate::bevy::components::Guid;
use crate::core::db::read_version;
use crate::core::query::{
    EdgeQuerySpecification, FilterExpression, PaginationConfig, PersistenceQuerySpecification,
};
use crate::core::session::PersistenceSession;
use crate::core::versioning::version_manager::VersionKey;
use super::cache::CachePolicy;
use super::persistence_query_system_param::PersistentQuery;
use super::query_thread_local::{RelationshipLoadSpec, take_pagination_config};
use bevy::ecs::query::{QueryData, QueryFilter};
use bevy::prelude::{Entity, Mut, World};
use rayon::prelude::*;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};

impl<'w, 's, Q: QueryData + 'static, F: QueryFilter + 'static> PersistentQuery<'w, 's, Q, F> {
    #[inline]
    pub(crate) fn apply_one_document(
        world: &mut World,
        session: &mut PersistenceSession,
        doc: &serde_json::Value,
        comps: &[&'static str],
        allow_overwrite: bool,
        key_field: &str,
    ) {
        let Some(key) = doc
            .get(key_field)
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
        else {
            bevy::log::trace!(
                "apply_one_document: skipping doc missing key '{}'",
                key_field
            );
            return;
        };
        let version = read_version(doc).unwrap_or(1);

        // Try to find an existing entity by Guid first.
        let existing_entity = world
            .query::<(Entity, &Guid)>()
            .iter(world)
            .find(|(_, g)| g.id() == key)
            .map(|(e, _)| e);

        // Resolve or spawn the entity for this key
        let (entity, existed) = if let Some(existing) = existing_entity {
            if !session.entity_keys().contains_key(&existing) {
                session.insert_entity_key(existing, key.clone());
            }
            (existing, true)
        } else if let Some((candidate, _)) = session
            .entity_keys()
            .iter()
            .find(|(_, k)| **k == key)
            .map(|(e, k)| (*e, k.clone()))
        {
            if world.get_entity(candidate).is_ok() {
                (candidate, true)
            } else {
                let e = world.spawn(Guid::new(key.clone())).id();
                session.insert_entity_key(e, key.clone());
                (e, false)
            }
        } else {
            let e = world.spawn(Guid::new(key.clone())).id();
            session.insert_entity_key(e, key.clone());
            (e, false)
        };

        if existed && !allow_overwrite {
            bevy::log::trace!(
                "apply_one_document: skip overwrite entity={:?} key={}",
                entity,
                key
            );
            return;
        }

        // Cache/refresh version
        session
            .version_manager_mut()
            .set_version(VersionKey::Entity(key.clone()), version);

        // Insert components - optimized for common case
        if !comps.is_empty() {
            for &comp_name in comps {
                if let Some(val) = doc.get(comp_name) {
                    if let Some(deser) = session.component_deserializer(comp_name) {
                        if let Err(e) = deser(world, entity, val.clone()) {
                            bevy::log::error!(
                                "Failed to deserialize component {}: {}",
                                comp_name,
                                e
                            );
                        }
                    }
                }
            }
        } else {
            for (registered_name, deser) in session.component_deserializers() {
                if let Some(val) = doc.get(registered_name) {
                    if let Err(e) = deser(world, entity, val.clone()) {
                        bevy::log::error!(
                            "Failed to deserialize component {}: {}",
                            registered_name,
                            e
                        );
                    }
                }
            }
        }
    }

    pub(crate) fn process_documents(
        &mut self,
        documents: Vec<serde_json::Value>,
        comp_names: &[&'static str],
        allow_overwrite: bool,
    ) {
        let key_field = self.db.connection.document_key_field();
        let explicit_components = comp_names.to_vec();
        bevy::log::trace!(
            "PQ::process_documents: deferring {} docs; comps={:?}; allow_overwrite={}",
            documents.len(),
            explicit_components,
            allow_overwrite
        );

        // For large document sets, use parallel processing with chunks
        if documents.len() > 1000 {
            // Process documents in parallel chunks
            const CHUNK_SIZE: usize = 250;
            let chunks: Vec<Vec<serde_json::Value>> = documents
                .chunks(CHUNK_SIZE)
                .map(|chunk| chunk.to_vec())
                .collect();

            chunks.into_par_iter().for_each(|chunk| {
                // Create a single operation that processes a chunk of documents
                let comps = explicit_components.clone();
                let allow = allow_overwrite;
                let key_field = key_field.to_string();

                self.ops.push(Box::new(move |world: &mut World| {
                    bevy::log::trace!(
                        "PQ::process_documents/op: applying deferred chunk of {} documents",
                        chunk.len()
                    );
                    world.resource_scope(|world, mut session: Mut<PersistenceSession>| {
                        for doc in &chunk {
                            Self::apply_one_document(
                                world,
                                &mut session,
                                doc,
                                &comps,
                                allow,
                                &key_field,
                            );
                        }
                    });
                }));
            });
        } else {
            // Batch the entire small set in a single scope to avoid per-doc locking
            let docs = documents;
            let comps = explicit_components.clone();
            let allow = allow_overwrite;
            let key_field = key_field.to_string();

            self.ops.push(Box::new(move |world: &mut World| {
                bevy::log::trace!(
                    "PQ::process_documents/op: applying deferred batch of {} documents",
                    docs.len()
                );
                world.resource_scope(|world, mut session: Mut<PersistenceSession>| {
                    for doc in &docs {
                        Self::apply_one_document(
                            world,
                            &mut session,
                            doc,
                            &comps,
                            allow,
                            &key_field,
                        );
                    }
                });
            }));
        }
    }

    pub(crate) fn execute_combined_load(
        &mut self,
        cache_policy: CachePolicy,
        presence_with: Vec<&'static str>,
        presence_without: Vec<&'static str>,
        fetch_only: Vec<&'static str>,
        value_filters: Option<FilterExpression>,
        hash_salts: &[&'static str],
        force_full_docs: bool,
        store: String,
        relationship_spec: RelationshipLoadSpec,
    ) {
        if store.is_empty() {
            bevy::log::error!("PQ::execute_combined_load: store is required");
            return;
        }
        // Compute cache hash
        let mut hasher = std::collections::hash_map::DefaultHasher::new();
        std::any::type_name::<Q>().hash(&mut hasher);
        for &salt in hash_salts {
            salt.hash(&mut hasher);
        }
        for &name in &presence_with {
            name.hash(&mut hasher);
        }
        for &name in &presence_without {
            name.hash(&mut hasher);
        }
        for &name in &fetch_only {
            name.hash(&mut hasher);
        }
        if let Some(expr) = &value_filters {
            format!("{:?}", expr).hash(&mut hasher);
        }
        for (type_id, depth) in relationship_spec.per_type.iter() {
            type_id.hash(&mut hasher);
            depth.hash(&mut hasher);
        }
        if let Some(depth) = relationship_spec.all {
            depth.hash(&mut hasher);
        }
        let query_hash = hasher.finish();

        bevy::log::debug!(
            "PQ::execute_combined_load enter: type={} hash={:#x} cache_policy={:?} salts={:?}",
            std::any::type_name::<Q>(),
            query_hash,
            cache_policy,
            hash_salts
        );

        let should_query_db = match cache_policy {
            CachePolicy::ForceRefresh => true,
            CachePolicy::UseCache => !self.cache.contains(query_hash),
        };

        bevy::log::debug!(
            "PQ::execute_combined_load: should_query_db={} presence_with={:?} presence_without={:?} fetch_only={:?} expr={:?}",
            should_query_db,
            presence_with,
            presence_without,
            fetch_only,
            value_filters
        );

        if should_query_db {
            let spec = PersistenceQuerySpecification {
                store: store.clone(),
                kind: crate::core::db::connection::DocumentKind::Entity,
                presence_with: presence_with.clone(),
                presence_without: presence_without.clone(),
                fetch_only: fetch_only.clone(),
                value_filters: value_filters.clone(),
                return_full_docs: force_full_docs
                    || (presence_with.is_empty() && presence_without.is_empty()),
                pagination: take_pagination_config(),
            };

            // Check if pagination is active
            if let Some(page_size) = spec.pagination.as_ref().map(|p| p.page_size) {
                // Determine allow_overwrite based on cache policy or presence filters
                let allow_overwrite =
                    matches!(cache_policy, CachePolicy::ForceRefresh) || !presence_with.is_empty();
                self.process_paginated_load(&spec, page_size, allow_overwrite);
            } else {
                // Original single-load code
                match self
                    .runtime
                    .block_on(self.db.connection.execute_documents(&spec))
                {
                    Ok(documents) => {
                        let key_field = self.db.connection.document_key_field();
                        let loaded_keys: Vec<String> = documents
                            .iter()
                            .filter_map(|doc| {
                                doc.get(key_field)
                                    .and_then(|v| v.as_str())
                                    .map(|s| s.to_string())
                            })
                            .collect();

                        bevy::log::debug!(
                            "PQ::execute_combined_load: backend returned {} documents; immediate_world_ptr={} ",
                            documents.len(),
                            self.world_ptr.is_some()
                        );
                        let allow_overwrite = matches!(cache_policy, CachePolicy::ForceRefresh)
                            || !presence_with.is_empty();

                        let comps_to_deser: Vec<&'static str> =
                            if presence_with.is_empty() && presence_without.is_empty() {
                                Vec::new()
                            } else {
                                fetch_only.clone()
                            };

                        if let Some(ptr_res) = &self.world_ptr {
                            let world: &mut World = ptr_res.as_world_mut();

                            // Apply all documents in a single scope to minimize world locking overhead.
                            world.resource_scope(|world, mut session: Mut<PersistenceSession>| {
                                for doc in &documents {
                                    Self::apply_one_document(
                                        world,
                                        &mut session,
                                        doc,
                                        &comps_to_deser,
                                        allow_overwrite,
                                        key_field,
                                    );
                                }

                                // Fetch resources once alongside the entity loads.
                                let rt = world
                                    .resource::<crate::bevy::plugins::persistence_plugin::TokioRuntime>()
                                    .runtime
                                    .clone();
                                let db = self.db.connection.clone();
                                bevy::log::trace!("PQ::immediate_apply: fetching resources");
                                rt.block_on(
                                    session.fetch_and_insert_resources(&*db, &store, world),
                                )
                                .ok();
                            });

                            if !relationship_spec.is_empty() {
                                world.resource_scope(|world, mut session: Mut<PersistenceSession>| {
                                    self.load_relationships_for_keys(
                                        world,
                                        &mut session,
                                        &store,
                                        &loaded_keys,
                                        relationship_spec,
                                    );
                                });
                            }

                            bevy::log::trace!("PQ::immediate_apply: world.flush()");
                            world.flush();

                            // Note: inner query (self.query) has a stale view of the world
                            // Pass-through methods like get_many() use fresh world queries instead

                            // Compare what the inner Query vs a fresh QueryState sees after immediate apply.
                            let inner_cnt = self.query.iter().count();
                            bevy::log::trace!(
                                "PQ::immediate_apply: inner_query_iter_count={}",
                                inner_cnt
                            );

                            // Warm-up current archetypes count for diagnostics via fresh QueryState
                            let lhs_cnt = {
                                let mut qs: bevy::ecs::query::QueryState<(Entity, Q), F> =
                                    bevy::ecs::query::QueryState::new(world);
                                qs.iter(&*world).count()
                            };
                            bevy::log::trace!(
                                "PQ::immediate_apply: fresh_qstate_iter_count={}",
                                lhs_cnt
                            );
                        } else {
                            bevy::log::trace!(
                                "PQ::execute_combined_load: deferring {} docs",
                                documents.len()
                            );
                            self.process_documents(documents, &comps_to_deser, allow_overwrite);

                            // Also fetch resources alongside any query (deferred)
                            let db = self.db.connection.clone();
                            self.ops.push(Box::new(move |world: &mut World| {
                                world.resource_scope(|world, mut session: Mut<PersistenceSession>| {
                                    let rt = world
                                        .resource::<crate::bevy::plugins::persistence_plugin::TokioRuntime>()
                                        .runtime
                                        .clone();
                                    bevy::log::trace!("PQ::execute_combined_load: fetching resources");
                                    // Use db directly rather than dereferencing it
                                    rt.block_on(session.fetch_and_insert_resources(&*db, &store, world)).ok();
                                });
                            }));

                            if !relationship_spec.is_empty() {
                                bevy::log::warn!(
                                    "PersistentQuery relationship loading requested without ImmediateWorldPtr; deferred relationship loading is not yet supported for this path"
                                );
                            }
                        }
                    }
                    Err(e) => {
                        bevy::log::error!("Error fetching documents: {}", e);
                        return;
                    }
                }
            }

            // Cache the query hash if successful
            if !matches!(cache_policy, CachePolicy::ForceRefresh) {
                bevy::log::trace!("PQ::execute_combined_load: caching hash {:#x}", query_hash);
                self.cache.insert(query_hash);
            }
        } else {
            bevy::log::trace!(
                "Skipping DB query - using cached results for hash={:#x}",
                query_hash
            );
        }
    }

    fn process_paginated_load(
        &mut self,
        spec: &PersistenceQuerySpecification,
        page_size: usize,
        allow_overwrite: bool,
    ) {
        bevy::log::debug!("Processing paginated load with page_size={}", page_size);

        // First get total count
        let total = match self
            .runtime
            .block_on(self.db.connection.count_documents(spec))
        {
            Ok(count) => count,
            Err(e) => {
                bevy::log::error!("Error counting documents: {}", e);
                return;
            }
        };

        let pages = (total + page_size - 1) / page_size; // Ceiling division
        bevy::log::debug!("Paginated load: total={} pages={}", total, pages);

        // Process each page
        let mut processed = 0;
        for page in 0..pages {
            let mut page_spec = spec.clone();
            page_spec.pagination = Some(PaginationConfig {
                page_size,
                page_number: page,
            });

            match self
                .runtime
                .block_on(self.db.connection.execute_documents(&page_spec))
            {
                Ok(documents) => {
                    processed += documents.len();
                    bevy::log::debug!(
                        "Loaded page {}/{} with {} documents (total processed: {}/{})",
                        page + 1,
                        pages,
                        documents.len(),
                        processed,
                        total
                    );

                    let comps_to_deser =
                        if spec.presence_with.is_empty() && spec.presence_without.is_empty() {
                            Vec::new()
                        } else {
                            spec.fetch_only.clone()
                        };

                    self.process_documents(documents, &comps_to_deser, allow_overwrite);
                }
                Err(e) => {
                    bevy::log::error!("Error fetching documents page {}: {}", page, e);
                    break;
                }
            }
        }
    }

    fn ensure_entity_loaded_by_key(
        &self,
        world: &mut World,
        session: &mut PersistenceSession,
        store: &str,
        key_field: &str,
        key: &str,
    ) -> Option<Entity> {
        if let Some(existing) = session.entity_by_key(key) {
            if world.get_entity(existing).is_ok() {
                return Some(existing);
            }
        }

        match self.runtime.block_on(self.db.connection.fetch_document(store, key)) {
            Ok(Some((doc, _))) => {
                Self::apply_one_document(world, session, &doc, &[], true, key_field);
                session.entity_by_key(key)
            }
            Ok(None) => None,
            Err(e) => {
                bevy::log::error!("Failed to fetch relationship target doc {}: {}", key, e);
                None
            }
        }
    }

    fn load_relationships_for_keys(
        &self,
        world: &mut World,
        session: &mut PersistenceSession,
        store: &str,
        source_keys: &[String],
        relationship_spec: RelationshipLoadSpec,
    ) {
        if source_keys.is_empty() {
            return;
        }

        let requested_depths = relationship_spec.resolve(session.relationship_type_entries());
        if requested_depths.is_empty() {
            return;
        }

        let key_field = self.db.connection.document_key_field();

        for (type_id, depth) in requested_depths {
            let Some(rel_name) = session.relationship_type_name(&type_id) else {
                continue;
            };

            let spec = EdgeQuerySpecification {
                store: store.to_string(),
                relationship_types: vec![rel_name.to_string()],
                from_guids: source_keys.to_vec(),
                to_guids: Vec::new(),
                depth,
            };

            let edges = match self.runtime.block_on(self.db.connection.query_edges(&spec)) {
                Ok(edges) => edges,
                Err(e) => {
                    bevy::log::error!(
                        "Failed loading relationship edges for {}: {}",
                        rel_name,
                        e
                    );
                    continue;
                }
            };

            let mut grouped: HashMap<String, Vec<(String, Option<serde_json::Value>)>> =
                HashMap::new();
            for edge in edges {
                grouped
                    .entry(edge.from_guid)
                    .or_default()
                    .push((edge.to_guid, edge.payload));
            }

            for source_key in source_keys {
                let Some(source_entity) =
                    self.ensure_entity_loaded_by_key(world, session, store, key_field, source_key)
                else {
                    continue;
                };

                let raw_targets = grouped.remove(source_key).unwrap_or_default();
                let mut resolved_targets = Vec::with_capacity(raw_targets.len());
                for (target_key, payload) in raw_targets {
                    if let Some(target_entity) =
                        self.ensure_entity_loaded_by_key(world, session, store, key_field, &target_key)
                    {
                        resolved_targets.push((target_entity, payload));
                    }
                }

                if let Err(e) = session.apply_relationship_targets(
                    type_id,
                    world,
                    source_entity,
                    resolved_targets,
                ) {
                    bevy::log::error!(
                        "Failed to materialize relationship {} on {:?}: {}",
                        rel_name,
                        source_entity,
                        e
                    );
                }
            }
        }
    }
}