Skip to content

Engine → Assets → asset_database.h

asset_database

Syntax

class CONJURE_API asset_database

Constructors

AccessSignatureDescription
publicasset_database( core::vfs::file_system &fileSystem, importer_registry &importers, const asset_type_registry &types, core::jobs::job_system *jobs = nullptr);jobs (optional) parallelises source hashing during scan(); without it hashing runs inline. Imports take their job system as an argument.

Important

Copy constructor and assignment has been deleted.

Destructors

AccessSignatureDescription
public~asset_database();

Variables

AccessNameTypeDefaultDescription
privatemCachestd::unique_ptr<asset_cache>nullptr
privatemCacheRootstd::string{}
privatemChildrenstd::vector<std::vector<usize>>{}
privatemEntriesstd::vector<entry>{}
privatemFileSystemcore::vfs::file_system *nullptr
privatemForwardEdgesstd::vector<usize>{}
privatemImportersimporter_registry *nullptr
privatemIndexByIdcore::flat_hash_map<asset_id, usize>{}
privatemIndexByPathcore::flat_hash_map<std::string, usize>{}
privatemJobscore::jobs::job_system *nullptr
privatemMountsstd::vector<asset_mount>{}
privatemMutexmutable std::shared_mutex{}
privatemPlatformstd::string{}
privatemReverseEdgesstd::vector<usize>{}
privatemSideFileOwnerscore::flat_hash_map<std::string, std::vector<asset_id>>{}
privatemStalestd::vector<asset_id>{}
privatemTypesconst asset_type_registry *nullptr

Functions

Public

add_file

[[nodiscard]] core::result<asset_id> add_file(
    std::string_view assetPath);

Registers a source that exists on disk but was not scanned yet, creating its .meta. NOT_FOUND when the file is missing, ALREADY_EXISTS when it is known, INVALID_ARGUMENT when the path leaves its mount ("assets://../x", an absolute path).

add_mount

[[nodiscard]] core::result<void> add_mount(
    std::string_view mountName,
    std::string_view rootPath);

Adds a mount. ALREADY_EXISTS for a repeated name, INVALID_ARGUMENT for an empty one. Mounts are scanned in the order they were added.

asset_path_of

[[nodiscard]] std::optional<std::string> asset_path_of(
    const asset_id &id) const;

"mount://relative#sub" of a record.

cache

[[nodiscard]] const asset_cache *cache() const noexcept;

The cache opened by open(), or nullptr.

collect_dependents

void collect_dependents(
    std::span<const asset_id> roots,
    std::vector<asset_id> &outOrdered) const;

Transitive dependents including the roots, depth ascending, deduplicated.

contains

[[nodiscard]] bool contains(
    const asset_id &id) const;

Allocation-free lookups for the load path (cache_asset_source asks on every job): whether a record exists, its cooked type (nullopt when unknown or never imported) and its status.

create_asset

[[nodiscard]] core::result<asset_id> create_asset(
    std::string_view assetPath,
    std::string_view importerId,
    std::span<const std::byte> initialBytes);

Writes a new source and its .meta with the given importer, then registers it. The source is removed again when its .meta cannot be written; INVALID_ARGUMENT when the path leaves its mount or one of its names is not portable (core::vfs::path_utils::is_portable_name), before anything is written.

delete_asset

[[nodiscard]] core::result<void> delete_asset(
    const asset_id &id);

Deletes the source, its .meta, its blobs and its records.

dependencies_of

[[nodiscard]] std::vector<asset_id> dependencies_of(
    const asset_id &id) const;

dependency_closure

[[nodiscard]] core::result<std::vector<asset_id>> dependency_closure(
    std::span<const asset_id> roots) const;

The roots plus everything they depend on, transitively, in uuid order. NOT_FOUND (assets) when a root is unknown.

dependents_of

[[nodiscard]] std::vector<asset_id> dependents_of(
    const asset_id &id) const;

file_path_of

[[nodiscard]] std::optional<std::string> file_path_of(
    const asset_id &id) const;

File-system path of a record's source.

find

[[nodiscard]] std::optional<asset_record> find(
    const asset_id &id) const;

find_by_file_path

[[nodiscard]] std::optional<asset_id> find_by_file_path(
    std::string_view filePath) const;

The primary whose source is the given file-system path ("assets/a.png").

find_by_path

[[nodiscard]] std::optional<asset_id> find_by_path(
    std::string_view assetPath) const;

import

[[nodiscard]] core::result<import_report> import(
    std::span<const asset_id> ids,
    core::jobs::job_system &jobs);

Imports the sources of the given ids (sub-asset ids import their primary), one job per source, writes blobs into the cache and merges the results. A failing source is recorded IMPORT_FAILED and keeps its previous blob; the call itself fails only with INVALID_STATE before open().

import_stale

[[nodiscard]] core::result<import_report> import_stale(
    core::jobs::job_system &jobs);

Imports every stale primary.

is_open

[[nodiscard]] bool is_open() const noexcept;

mark_source_missing

[[nodiscard]] core::result<void> mark_source_missing(
    const asset_id &id);

Marks a primary (and its sub-assets) MISSING_SOURCE without a full scan: the hot reload controller calls it when the watcher reports the source file removed, so later loads fail with NOT_FOUND while an already loaded payload stays loaded. NOT_FOUND for an unknown id; a sub-asset id marks its primary.

mark_source_present

[[nodiscard]] core::result<void> mark_source_present(
    const asset_id &id);

Reverses mark_source_missing() once the source file exists again (the file was moved back or re-created): the status returns to OK (or NO_IMPORTER), the size and write time are refreshed and the source is stale if its content changed. NOT_FOUND for an unknown id or when the file is still absent.

mounts

[[nodiscard]] std::span<const asset_mount> mounts() const noexcept;

move_asset

[[nodiscard]] core::result<void> move_asset(
    const asset_id &id,
    std::string_view newAssetPath);

Renames the source and its .meta; the uuid and every sub-asset uuid are kept. INVALID_ARGUMENT when the new path leaves its mount or one of its names is not portable (core::vfs::path_utils::is_portable_name), before anything is moved.

open

[[nodiscard]] core::result<void> open(
    std::string_view cacheRoot,
    std::string_view platform);

Opens the cache under cacheRoot for a platform and loads the db.bin snapshot when it is valid. Must precede scan().

owners_of_side_file

[[nodiscard]] std::vector<asset_id> owners_of_side_file(
    std::string_view assetPath) const;

platform

[[nodiscard]] std::string_view platform() const noexcept;

read_meta

[[nodiscard]] core::result<asset_meta> read_meta(
    const asset_id &id) const;

Reads the .meta of a primary (or of a sub-asset's primary).

record_count

[[nodiscard]] usize record_count() const;

records

[[nodiscard]] std::vector<asset_record> records() const;

save_snapshot

[[nodiscard]] core::result<void> save_snapshot() const;

Persists the records to <cacheRoot>/db.bin. Purely a cache; rebuilt when missing.

scan

[[nodiscard]] core::result<scan_report> scan();

Walks every mount in sorted order, creates missing .meta files, applies the duplicate-uuid policy, hashes changed sources, marks removed sources MISSING_SOURCE, computes the stale set, deletes leftover *.tmp blobs and saves the snapshot. A .meta that cannot be written leaves its record META_INVALID (retried by the next scan) instead of failing the scan. INVALID_STATE before open().

set_importer

[[nodiscard]] core::result<void> set_importer(
    const asset_id &id,
    std::string_view importerId);

Makes a registered importer the one a primary's .meta names ("import as"): the way to import a file with an importer that reads its format without claiming its extension, such as a .glb as collision geometry instead of a render model. The meta takes the importer's version and default settings, since the previous importer's settings mean nothing to it, and the asset is stale until its next import; its uuid is kept. Naming the importer the meta already has changes nothing. NO_IMPORTER for an id that is not registered; otherwise the errors of read_meta and write_meta.

stale_ids

[[nodiscard]] std::vector<asset_id> stale_ids() const;

The stale primaries computed by the last scan() or import().

status_of

[[nodiscard]] std::optional<asset_record_status> status_of(
    const asset_id &id) const;

type_of

[[nodiscard]] std::optional<asset_type_id> type_of(
    const asset_id &id) const;

types

[[nodiscard]] const asset_type_registry &types() const noexcept;

vfs

[[nodiscard]] core::vfs::file_system &vfs() const noexcept;

write_meta

[[nodiscard]] core::result<void> write_meta(
    const asset_id &id,
    const asset_meta &meta);

Writes the .meta of a primary and refreshes the record (settings hash, importer). A changed uuid is refused with INVALID_ARGUMENT and logged: it would orphan references. A sub-asset the last import dropped (no blob) gets no record back from the meta. An imported primary switched to another importer is stale until its next import, even when both importers share a version and settings.

Private

apply_import_results

[[nodiscard]] core::result<void> apply_import_results(
    std::vector<source_job> &jobs,
    import_report &report);

detect_cycles_locked

void detect_cycles_locked();

ensure_open

[[nodiscard]] core::result<void> ensure_open() const;

file_path_for

[[nodiscard]] std::string file_path_for(
    u32 mountIndex,
    std::string_view relativePath) const;

find_entry_locked

[[nodiscard]] const entry *find_entry_locked(
    const asset_id &id) const noexcept;
[[nodiscard]] entry *find_entry_locked(
    const asset_id &id) noexcept;

hash_source

[[nodiscard]] u64 hash_source(
    u32 mountIndex,
    std::string_view relativePath,
    std::span<const std::string> sideFiles) const;

hash_source_bytes

[[nodiscard]] u64 hash_source_bytes(
    std::span<const std::byte> primaryBytes,
    std::span<const std::string> sideFiles) const;

load_snapshot

[[nodiscard]] core::result<void> load_snapshot();

mount_index

[[nodiscard]] std::optional<u32> mount_index(
    std::string_view mountName) const noexcept;

rebuild_graphs_locked

void rebuild_graphs_locked();

rebuild_indexes_locked

void rebuild_indexes_locked();

recompute_stale

void recompute_stale();

register_source

[[nodiscard]] core::result<asset_id> register_source(
    u32 mountIndex,
    std::string_view relativePath,
    bool &outMetaCreated);

set_source_status

[[nodiscard]] core::result<void> set_source_status(
    const asset_id &id,
    bool present);

write_import_log

[[nodiscard]] core::result<void> write_import_log(
    const import_report &report) const;