distill-daemon 0.0.3

Daemon component of the asset pipeline `distill`.
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
use std::{
    collections::{HashMap, HashSet},
    path,
    rc::Rc,
    sync::Arc,
};

use capnp_rpc::{pry, rpc_twoparty_capnp, twoparty, RpcSystem};
use distill_core::utils::{self, canonicalize_path};
use distill_importer::SerializedAsset;
use distill_schema::{
    build_artifact_metadata,
    data::{
        artifact, asset_change_log_entry,
        asset_metadata::{self, latest_artifact},
        AssetSource,
    },
    parse_artifact_metadata, parse_db_asset_ref,
    service::asset_hub,
};
use futures::{AsyncReadExt, TryFutureExt};

use crate::{
    artifact_cache::ArtifactCache,
    asset_hub::{AssetBatchEvent, AssetHub},
    capnp_db::{CapnpCursor as _, Environment, RoTransaction},
    error::Error,
    file_asset_source::FileAssetSource,
    file_tracker::FileTracker,
};

// crate::Error has `impl From<crate::Error> for capnp::Error`
type Promise<T> = capnp::capability::Promise<T, capnp::Error>;
type Result<T> = std::result::Result<T, Error>;

struct ServiceContext {
    hub: Arc<AssetHub>,
    file_source: Arc<FileAssetSource>,
    file_tracker: Arc<FileTracker>,
    artifact_cache: Arc<ArtifactCache>,
    db: Arc<Environment>,
}

pub(crate) struct AssetHubService {
    ctx: Arc<ServiceContext>,
}

struct SnapshotTxn {
    ctx: Arc<ServiceContext>,
    // txn is owned by service context, so it's lifetime is bound to this object's lifetime
    txn: RoTransaction<'static>,
}

impl SnapshotTxn {
    async fn new(ctx: Arc<ServiceContext>) -> Self {
        let txn = ctx.db.ro_txn().await.unwrap();
        // The transaction can live at least as long as ServiceContext, which this object holds onto.
        // It is only ever borrowed for a lifetime bound to the self reference.
        let txn = unsafe { std::mem::transmute::<RoTransaction<'_>, RoTransaction<'static>>(txn) };
        Self { ctx, txn }
    }

    fn txn(&self) -> &RoTransaction<'_> {
        &self.txn
    }

    fn ctx(&self) -> &Arc<ServiceContext> {
        &self.ctx
    }
}

// RPC interface implementations

struct AssetHubSnapshotImpl {
    txn: Arc<SnapshotTxn>,
}

struct AssetHubImpl {
    ctx: Arc<ServiceContext>,
}

fn build_artifact_message<T: AsRef<[u8]>>(
    artifact: &SerializedAsset<T>,
) -> capnp::message::Builder<capnp::message::HeapAllocator> {
    let mut value_builder = capnp::message::Builder::new_default();
    {
        let mut m = value_builder.init_root::<artifact::Builder<'_>>();
        let mut metadata = m.reborrow().init_metadata();
        build_artifact_metadata(&artifact.metadata, &mut metadata);
        let slice: &[u8] = artifact.data.as_ref();
        m.reborrow().set_data(slice);
    }
    value_builder
}

fn artifact_to_serialized_asset<'a>(
    artifact: &artifact::Reader<'a>,
) -> Result<SerializedAsset<&'a [u8]>> {
    let metadata = parse_artifact_metadata(&artifact.get_metadata()?);
    Ok(SerializedAsset {
        metadata,
        data: artifact.get_data()?,
    })
}

impl AssetHubSnapshotImpl {
    async fn new(ctx: Arc<ServiceContext>) -> Self {
        Self {
            txn: Arc::new(SnapshotTxn::new(ctx).await),
        }
    }

    fn get_asset_metadata(
        &mut self,
        params: asset_hub::snapshot::GetAssetMetadataParams,
        mut results: asset_hub::snapshot::GetAssetMetadataResults,
    ) -> Result<()> {
        let params = params.get()?;
        let ctx = self.txn.ctx();
        let txn = self.txn.txn();
        let mut metadatas = Vec::new();
        for id in params.get_assets()? {
            let id = utils::uuid_from_slice(id.get_id()?).ok_or(Error::UuidLength)?;
            let value = ctx.hub.get_metadata(txn, &id);
            if let Some(metadata) = value {
                metadatas.push(metadata);
            }
        }
        let mut results_builder = results.get();
        let assets = results_builder
            .reborrow()
            .init_assets(metadatas.len() as u32);
        for (idx, metadata) in metadatas.iter().enumerate() {
            let metadata = metadata.get()?;
            assets.set_with_caveats(idx as u32, metadata)?;
        }
        Ok(())
    }

    fn get_asset_metadata_with_dependencies(
        &mut self,
        params: asset_hub::snapshot::GetAssetMetadataWithDependenciesParams,
        mut results: asset_hub::snapshot::GetAssetMetadataWithDependenciesResults,
    ) -> Result<()> {
        let params = params.get()?;
        let ctx = self.txn.ctx();
        let txn = self.txn.txn();
        let mut metadatas = HashMap::new();
        for id in params.get_assets()? {
            let id = utils::uuid_from_slice(id.get_id()?).ok_or(Error::UuidLength)?;
            let value = ctx.hub.get_metadata(txn, &id);
            if let Some(metadata) = value {
                metadatas.insert(id, metadata);
            }
        }
        let mut missing_metadata = HashSet::new();
        for metadata in metadatas.values() {
            if let latest_artifact::Artifact(Ok(artifact)) =
                metadata.get()?.get_latest_artifact().which()?
            {
                for dep in artifact.get_load_deps()? {
                    let dep = *parse_db_asset_ref(&dep).expect_uuid();
                    if !metadatas.contains_key(&dep) {
                        missing_metadata.insert(dep);
                    }
                }
            }
        }
        for id in missing_metadata {
            let value = ctx.hub.get_metadata(txn, &id);
            if let Some(metadata) = value {
                metadatas.insert(id, metadata);
            }
        }
        let mut results_builder = results.get();
        let assets = results_builder
            .reborrow()
            .init_assets(metadatas.len() as u32);
        for (idx, metadata) in metadatas.values().enumerate() {
            let metadata = metadata.get()?;
            assets.set_with_caveats(idx as u32, metadata)?;
        }
        Ok(())
    }

    fn get_all_asset_metadata(
        &mut self,
        _params: asset_hub::snapshot::GetAllAssetMetadataParams,
        mut results: asset_hub::snapshot::GetAllAssetMetadataResults,
    ) -> Result<()> {
        let ctx = self.txn.ctx();
        let txn = self.txn.txn();
        let mut metadatas = Vec::new();
        for (_, value) in ctx.hub.get_metadata_iter(txn)?.capnp_iter_start() {
            let value = value?;
            let metadata = value.into_typed::<asset_metadata::Owned>();
            metadatas.push(metadata);
        }
        let mut results_builder = results.get();
        let assets = results_builder
            .reborrow()
            .init_assets(metadatas.len() as u32);
        for (idx, metadata) in metadatas.iter().enumerate() {
            let metadata = metadata.get()?;
            assets.set_with_caveats(idx as u32, metadata)?;
        }
        Ok(())
    }

    async fn get_import_artifacts(
        snapshot: Arc<SnapshotTxn>,
        params: asset_hub::snapshot::GetImportArtifactsParams,
        mut results: asset_hub::snapshot::GetImportArtifactsResults,
    ) -> Result<()> {
        let params = params.get()?;
        let ctx = snapshot.ctx();
        let txn = snapshot.txn();
        let mut regen_artifacts = Vec::new();
        let mut cached_artifacts = Vec::new();
        let mut scratch_buf = Vec::new();
        let cache_txn = ctx.artifact_cache.ro_txn().await?;

        let request_uuid = uuid::Uuid::new_v4();
        log::trace!(
            "get_import_artifacts Gathering artifacts {:?}",
            request_uuid
        );

        for id in params.get_assets()? {
            let id = utils::uuid_from_slice(id.get_id()?).ok_or(Error::UuidLength)?;
            log::trace!("{:?} get_import_artifacts for id {:?}", request_uuid, id);
            let value = ctx.hub.get_metadata(txn, &id);
            if let Some(metadata) = value {
                log::trace!("metadata available for id {:?}", id);

                // retreive artifact data from cache if available
                let mut need_regen = true;
                if let latest_artifact::Artifact(Ok(artifact)) =
                    metadata.get()?.get_latest_artifact().which()?
                {
                    let hash = u64::from_le_bytes(utils::make_array(artifact.get_hash()?));
                    if let Some(artifact) = ctx.artifact_cache.get(&cache_txn, hash).await {
                        cached_artifacts.push(artifact);
                        need_regen = false;
                    } else {
                        log::trace!("cache miss for asset {:?} with hash {:?}", id, hash);
                    }
                }

                if need_regen {
                    let metadata = metadata.get()?;
                    match metadata.get_source()? {
                        AssetSource::File => {
                            log::trace!("regenerating import artifact from file for {:?}", id);
                            let (_, artifact) = ctx
                                .file_source
                                .regenerate_import_artifact(txn, &id, &mut scratch_buf)
                                .await?;
                            log::trace!("finished regenerating import artifact for {:?}", id);
                            let capnp_artifact = build_artifact_message(&artifact);
                            log::trace!("built artifact message for {:?}", id);
                            regen_artifacts.push(capnp_artifact);
                        }
                    }
                } else {
                    log::trace!("using cached import artifact for {:?}", id);
                }
            } else {
                log::trace!("metadata not available for id {:?}", id);
            }
        }

        log::trace!("get_import_artifacts Building Response {:?}", request_uuid);
        let mut results_builder = results.get();
        let mut artifact_results = results_builder
            .reborrow()
            .init_artifacts(cached_artifacts.len() as u32 + regen_artifacts.len() as u32);
        for (idx, artifact) in cached_artifacts.iter().enumerate() {
            artifact_results
                .reborrow()
                .set_with_caveats(idx as u32, artifact.get()?)?;
        }
        for (idx, artifact) in regen_artifacts.iter().enumerate() {
            artifact_results.reborrow().set_with_caveats(
                idx as u32,
                artifact.get_root_as_reader::<artifact::Reader<'_>>()?,
            )?;
        }
        Ok(())
    }

    fn get_latest_asset_change(
        &mut self,
        _params: asset_hub::snapshot::GetLatestAssetChangeParams,
        mut results: asset_hub::snapshot::GetLatestAssetChangeResults,
    ) -> Result<()> {
        let ctx = self.txn.ctx();
        let txn = self.txn.txn();
        let change_num = ctx.hub.get_latest_asset_change(txn)?;
        results.get().set_num(change_num);
        Ok(())
    }

    fn get_asset_changes(
        &mut self,
        params: asset_hub::snapshot::GetAssetChangesParams,
        mut results: asset_hub::snapshot::GetAssetChangesResults,
    ) -> Result<()> {
        let params = params.get()?;
        let ctx = self.txn.ctx();
        let txn = self.txn.txn();
        let mut changes = Vec::new();
        let iter = ctx.hub.get_asset_changes_iter(txn)?;
        let iter = iter.capnp_iter_from(&params.get_start().to_le_bytes());
        let mut count = params.get_count() as usize;
        if count == 0 {
            count = std::usize::MAX;
        }
        for (_, value) in iter.take(count) {
            let value = value?;
            let change = value.into_typed::<asset_change_log_entry::Owned>();
            changes.push(change);
        }
        let mut results_builder = results.get();
        let changes_results = results_builder
            .reborrow()
            .init_changes(changes.len() as u32);
        for (idx, change) in changes.iter().enumerate() {
            let change = change.get()?;
            changes_results.set_with_caveats(idx as u32, change)?;
        }
        Ok(())
    }

    fn get_path_for_assets(
        &mut self,
        params: asset_hub::snapshot::GetPathForAssetsParams,
        mut results: asset_hub::snapshot::GetPathForAssetsResults,
    ) -> Result<()> {
        let params = params.get()?;
        let ctx = self.txn.ctx();
        let txn = self.txn.txn();
        let mut asset_paths = Vec::new();
        for id in params.get_assets()? {
            let asset_uuid = utils::uuid_from_slice(id.get_id()?).ok_or(Error::UuidLength)?;
            let path = ctx.file_source.get_asset_path(txn, &asset_uuid);
            if let Some(path) = path {
                for dir in ctx.file_tracker.get_watch_dirs() {
                    let canonicalized_dir = canonicalize_path(&dir);
                    if path.starts_with(&canonicalized_dir) {
                        let relative_path = path
                            .strip_prefix(canonicalized_dir)
                            .expect("error stripping prefix")
                            .to_path_buf();
                        let relative_path = canonicalize_path(&relative_path)
                            .to_string_lossy()
                            .replace("\\", "/");
                        asset_paths.push((id, relative_path));
                    }
                }
            }
        }
        let mut results_builder = results.get();
        let mut assets = results_builder
            .reborrow()
            .init_paths(asset_paths.len() as u32);
        for (idx, (asset, path)) in asset_paths.iter().enumerate() {
            assets.reborrow().get(idx as u32).set_path(path.as_bytes());
            assets
                .reborrow()
                .get(idx as u32)
                .init_id()
                .set_id(asset.get_id()?);
        }
        Ok(())
    }

    fn get_assets_for_paths(
        &mut self,
        params: asset_hub::snapshot::GetAssetsForPathsParams,
        mut results: asset_hub::snapshot::GetAssetsForPathsResults,
    ) -> Result<()> {
        let params = params.get()?;
        let ctx = self.txn.ctx();
        let txn = self.txn.txn();
        let mut metadatas = Vec::new();
        for request_path in params.get_paths()? {
            let request_path = request_path?;
            let path_str = std::str::from_utf8(request_path)?.to_string();
            let path = path::PathBuf::from(path_str);
            let mut metadata = None;
            if path.is_relative() {
                for dir in ctx.file_tracker.get_watch_dirs() {
                    let canonicalized = canonicalize_path(&dir.join(&path));
                    metadata = ctx.file_source.get_metadata(txn, &canonicalized);
                    if metadata.is_some() {
                        break;
                    }
                }
            } else {
                let canonicalized = canonicalize_path(&path);
                metadata = ctx.file_source.get_metadata(txn, &canonicalized)
            }
            if let Some(metadata) = metadata {
                metadatas.push((request_path, metadata));
            }
        }
        let mut results_builder = results.get();
        let mut results = results_builder
            .reborrow()
            .init_assets(metadatas.len() as u32);
        for (idx, (path, assets)) in metadatas.iter().enumerate() {
            let assets = assets.get()?.get_assets()?;
            let num_assets = assets.len();
            let mut asset_results = results.reborrow().get(idx as u32).init_assets(num_assets);
            for (idx, asset) in assets.iter().enumerate() {
                asset_results
                    .reborrow()
                    .get(idx as u32)
                    .set_id(asset.get_id()?.get_id()?);
            }
            results.reborrow().get(idx as u32).set_path(path);
        }
        Ok(())
    }

    async fn update_asset(
        snapshot: Arc<SnapshotTxn>,
        params: asset_hub::snapshot::UpdateAssetParams,
        mut results: asset_hub::snapshot::UpdateAssetResults,
    ) -> Result<()> {
        let params = params.get()?;
        let txn = &snapshot.txn;
        let ctx = &snapshot.ctx;
        // TODO move the below parts into FileAssetSource
        let new_artifact = artifact_to_serialized_asset(&params.get_asset()?)?.to_vec();
        let asset_uuid = new_artifact.metadata.asset_id;
        let asset_metadata = ctx.hub.get_metadata(txn, &asset_uuid);
        let mut scratch_buf = Vec::new();
        if let Some(asset_metadata) = asset_metadata {
            match asset_metadata.get()?.get_source()? {
                AssetSource::File => {
                    let path = ctx.file_source.get_asset_path(txn, &asset_uuid);
                    if let Some(path) = path {
                        let source_metadata = ctx
                            .file_source
                            .get_metadata(txn, &path)
                            .expect("inconsistent source metadata");
                        let source_metadata = source_metadata.get()?;
                        let mut assets = vec![new_artifact];
                        for asset in source_metadata.get_assets()? {
                            let source_asset_id = utils::uuid_from_slice(asset.get_id()?.get_id()?)
                                .ok_or(Error::UuidLength)?;
                            if source_asset_id != asset_uuid {
                                // TODO maybe extract into a function, and use the cache in the future
                                let (_, artifact) = ctx
                                    .file_source
                                    .regenerate_import_artifact(
                                        txn,
                                        &source_asset_id,
                                        &mut scratch_buf,
                                    )
                                    .await?;
                                assets.push(artifact);
                            }
                        }
                        let export_results = ctx.file_source.export_source(path, assets).await?;
                        let updated_asset_metadata =
                            export_results.iter().find(|a| a.id == asset_uuid);
                        if let Some(metadata) = updated_asset_metadata {
                            if let Some(artifact) = &metadata.artifact {
                                let mut results_builder = results.get();
                                results_builder.set_new_import_hash(&artifact.id.0.to_le_bytes());
                                Ok(())
                            } else {
                                Err(Error::Custom(
                                    "Metadata for the updated asset does not contain artifact metadata after exporting"
                                        .into(),
                                ))
                            }
                        } else {
                            Err(Error::Custom(
                                "Metadata for the updated asset doesn't exist after exporting"
                                    .into(),
                            ))
                        }
                    } else {
                        Err(Error::Custom("Source file does not exist for asset".into()))
                    }
                }
            }
        } else {
            Err(Error::Custom(
                "Unable to find asset metadata for asset".into(),
            ))
        }
    }
}

#[allow(clippy::unit_arg)]
impl asset_hub::Server for AssetHubImpl {
    fn register_listener(
        &mut self,
        params: asset_hub::RegisterListenerParams,
        results: asset_hub::RegisterListenerResults,
    ) -> Promise<()> {
        log::trace!("asset_hub::Server::register_listener");
        Promise::ok(pry!(AssetHubImpl::register_listener(self, params, results)))
    }

    fn get_snapshot(
        &mut self,
        params: asset_hub::GetSnapshotParams,
        results: asset_hub::GetSnapshotResults,
    ) -> Promise<()> {
        log::trace!("asset_hub::Server::get_snapshot");
        let fut = AssetHubImpl::get_snapshot(self.ctx.clone(), params, results);
        Promise::from_future(async { fut.await.map_err(|e| e.into()) })
    }
}
impl AssetHubImpl {
    fn register_listener(
        &mut self,
        params: asset_hub::RegisterListenerParams,
        _results: asset_hub::RegisterListenerResults,
    ) -> Result<()> {
        let params = params.get()?;
        let listener = Rc::new(params.get_listener()?);
        let ctx = self.ctx.clone();
        let (tx, rx) = async_channel::bounded(16);
        tx.try_send(AssetBatchEvent::Commit).unwrap();

        let tx = self.ctx.hub.register_listener(tx);

        tokio::task::spawn_local(async move {
            while rx.recv().await.is_ok() {
                let mut request = listener.update_request();
                let snapshot = AssetHubSnapshotImpl::new(ctx.clone()).await;
                let latest_change = ctx
                    .hub
                    .get_latest_asset_change(snapshot.txn.txn())
                    .expect("failed to get latest change");
                request.get().set_latest_change(latest_change);
                request.get().set_snapshot(capnp_rpc::new_client(snapshot));
                if request.send().promise.await.is_err() {
                    ctx.hub.drop_listener(tx);
                    break;
                }
            }
        });
        Ok(())
    }

    async fn get_snapshot(
        ctx: Arc<ServiceContext>,
        _params: asset_hub::GetSnapshotParams,
        mut results: asset_hub::GetSnapshotResults,
    ) -> Result<()> {
        let snapshot = AssetHubSnapshotImpl::new(ctx).await;
        results.get().set_snapshot(capnp_rpc::new_client(snapshot));
        Ok(())
    }
}

impl AssetHubService {
    pub fn new(
        db: Arc<Environment>,
        hub: Arc<AssetHub>,
        file_source: Arc<FileAssetSource>,
        file_tracker: Arc<FileTracker>,
        artifact_cache: Arc<ArtifactCache>,
    ) -> AssetHubService {
        AssetHubService {
            ctx: Arc::new(ServiceContext {
                hub,
                db,
                file_source,
                file_tracker,
                artifact_cache,
            }),
        }
    }

    pub async fn run(&self, addr: std::net::SocketAddr) {
        let result: std::result::Result<(), Box<dyn std::error::Error>> = async {
            let listener = tokio::net::TcpListener::bind(&addr).await?;

            loop {
                let (stream, _) = listener.accept().await?;
                log::info!("tokio::net::TcpListener accepted");
                stream.set_nodelay(true).unwrap();
                use tokio_util::compat::*;
                let (reader, writer) = stream.compat().split();

                let service_impl = AssetHubImpl {
                    ctx: self.ctx.clone(),
                };
                let hub_impl: asset_hub::Client = capnp_rpc::new_client(service_impl);

                let network = twoparty::VatNetwork::new(
                    reader,
                    writer,
                    rpc_twoparty_capnp::Side::Server,
                    Default::default(),
                );

                let rpc_system = RpcSystem::new(Box::new(network), Some(hub_impl.client));
                tokio::task::spawn_local(rpc_system.map_err(|_| ()));
            }
        }
        .await;
        // NOTE(happens): This will only fail if we can't set the stream
        // parameters on startup, which is a cause for panic in any case.
        // NOTE(kabergstrom): It also seems to happen when the main thread
        // is aborted and this is run on a background thread
        result.expect("Failed to run tcp listener");
    }
}

#[allow(clippy::unit_arg)]
impl asset_hub::snapshot::Server for AssetHubSnapshotImpl {
    fn get_asset_metadata(
        &mut self,
        params: asset_hub::snapshot::GetAssetMetadataParams,
        results: asset_hub::snapshot::GetAssetMetadataResults,
    ) -> Promise<()> {
        log::trace!("asset_hub::snapshot::Server::get_asset_metadata");
        Promise::ok(pry!(AssetHubSnapshotImpl::get_asset_metadata(
            self, params, results
        )))
    }

    fn get_asset_metadata_with_dependencies(
        &mut self,
        params: asset_hub::snapshot::GetAssetMetadataWithDependenciesParams,
        results: asset_hub::snapshot::GetAssetMetadataWithDependenciesResults,
    ) -> Promise<()> {
        log::trace!("asset_hub::snapshot::Server::get_asset_metadata_with_dependencies");
        Promise::ok(pry!(
            AssetHubSnapshotImpl::get_asset_metadata_with_dependencies(self, params, results)
        ))
    }

    fn get_all_asset_metadata(
        &mut self,
        params: asset_hub::snapshot::GetAllAssetMetadataParams,
        results: asset_hub::snapshot::GetAllAssetMetadataResults,
    ) -> Promise<()> {
        log::trace!("asset_hub::snapshot::Server::get_all_asset_metadata");
        Promise::ok(pry!(AssetHubSnapshotImpl::get_all_asset_metadata(
            self, params, results
        )))
    }

    fn get_import_artifacts(
        &mut self,
        params: asset_hub::snapshot::GetImportArtifactsParams,
        results: asset_hub::snapshot::GetImportArtifactsResults,
    ) -> Promise<()> {
        log::trace!("asset_hub::snapshot::Server::get_import_artifacts");
        let fut = AssetHubSnapshotImpl::get_import_artifacts(self.txn.clone(), params, results);
        Promise::from_future(async { fut.await.map_err(|e| e.into()) })
    }

    fn get_latest_asset_change(
        &mut self,
        params: asset_hub::snapshot::GetLatestAssetChangeParams,
        results: asset_hub::snapshot::GetLatestAssetChangeResults,
    ) -> Promise<()> {
        log::trace!("asset_hub::snapshot::Server::get_latest_asset_change");
        Promise::ok(pry!(AssetHubSnapshotImpl::get_latest_asset_change(
            self, params, results
        )))
    }

    fn get_asset_changes(
        &mut self,
        params: asset_hub::snapshot::GetAssetChangesParams,
        results: asset_hub::snapshot::GetAssetChangesResults,
    ) -> Promise<()> {
        log::trace!("asset_hub::snapshot::Server::get_asset_changes");
        Promise::ok(pry!(AssetHubSnapshotImpl::get_asset_changes(
            self, params, results
        )))
    }

    fn get_path_for_assets(
        &mut self,
        params: asset_hub::snapshot::GetPathForAssetsParams,
        results: asset_hub::snapshot::GetPathForAssetsResults,
    ) -> Promise<()> {
        log::trace!("asset_hub::snapshot::Server::get_path_for_assets");
        Promise::ok(pry!(AssetHubSnapshotImpl::get_path_for_assets(
            self, params, results
        )))
    }

    fn get_assets_for_paths(
        &mut self,
        params: asset_hub::snapshot::GetAssetsForPathsParams,
        results: asset_hub::snapshot::GetAssetsForPathsResults,
    ) -> Promise<()> {
        log::trace!("asset_hub::snapshot::Server::get_assets_for_paths");
        Promise::ok(pry!(AssetHubSnapshotImpl::get_assets_for_paths(
            self, params, results
        )))
    }

    fn update_asset(
        &mut self,
        params: asset_hub::snapshot::UpdateAssetParams,
        results: asset_hub::snapshot::UpdateAssetResults,
    ) -> Promise<()> {
        log::trace!("asset_hub::snapshot::Server::update_asset");
        let fut = AssetHubSnapshotImpl::update_asset(self.txn.clone(), params, results);
        Promise::from_future(async { fut.await.map_err(|e| e.into()) })
    }
}