Skip to main content

mz_deploy/project/compiler/
cache.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! SQLite-backed compiler cache shared by the writer and the reader.
11//!
12//! The cache is a single SQLite file per profile namespace. [`BuildArtifact`]
13//! is the read/write handle used during compilation; [`ProjectCache`] is the
14//! read-only handle used by downstream consumers (the LSP, in particular). Both
15//! agree on the file location, schema version, and error vocabulary defined
16//! here.
17
18use std::collections::BTreeMap;
19use std::path::{Path, PathBuf};
20use thiserror::Error;
21
22pub(crate) mod build_artifact;
23pub(crate) mod project_cache;
24pub(crate) mod schema;
25
26pub(crate) use build_artifact::BuildArtifact;
27pub(crate) use project_cache::ProjectCache;
28
29pub(crate) const DB_FILE: &str = "build_artifact.db";
30
31/// Compute the path to the compiler cache database for a given project and profile.
32pub(crate) fn db_path(
33    root: &Path,
34    profile: &str,
35    profile_suffix: Option<&str>,
36    variables: &BTreeMap<String, String>,
37) -> PathBuf {
38    root.join(crate::types::BUILD_DIR)
39        .join(super::COMPILER_DIR)
40        .join(super::profile_namespace(profile, profile_suffix, variables))
41        .join(DB_FILE)
42}
43
44#[derive(Debug, Error)]
45pub enum CacheError {
46    #[error("failed to create compiler cache directory: {path}")]
47    DirectoryCreationFailed {
48        path: PathBuf,
49        #[source]
50        source: std::io::Error,
51    },
52    #[error("failed to open build artifact database: {path}")]
53    DatabaseOpenFailed {
54        path: PathBuf,
55        #[source]
56        source: rusqlite::Error,
57    },
58    #[error("failed to operate on build artifact database: {path}")]
59    DatabaseOperationFailed {
60        path: PathBuf,
61        #[source]
62        source: rusqlite::Error,
63    },
64    #[error("failed to read cached source file: {path}")]
65    FileReadFailed {
66        path: PathBuf,
67        #[source]
68        source: std::io::Error,
69    },
70}