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/// Decode a `typecheck_columns.column_type` value.
32///
33/// The column holds JSON rather than a type name because a record's structure
34/// has to survive the round trip; see [`crate::types::data_type`].
35fn decode_column_type(json: String) -> rusqlite::Result<crate::types::DataType> {
36    crate::types::DataType::from_json(&json).map_err(|err| {
37        rusqlite::Error::FromSqlConversionFailure(2, rusqlite::types::Type::Text, Box::new(err))
38    })
39}
40
41/// Compute the path to the compiler cache database for a given project and profile.
42pub(crate) fn db_path(
43    root: &Path,
44    profile: &str,
45    profile_suffix: Option<&str>,
46    variables: &BTreeMap<String, String>,
47) -> PathBuf {
48    root.join(crate::types::BUILD_DIR)
49        .join(super::COMPILER_DIR)
50        .join(super::profile_namespace(profile, profile_suffix, variables))
51        .join(DB_FILE)
52}
53
54#[derive(Debug, Error)]
55pub enum CacheError {
56    #[error("failed to create compiler cache directory: {path}")]
57    DirectoryCreationFailed {
58        path: PathBuf,
59        #[source]
60        source: std::io::Error,
61    },
62    #[error("failed to open build artifact database: {path}")]
63    DatabaseOpenFailed {
64        path: PathBuf,
65        #[source]
66        source: rusqlite::Error,
67    },
68    #[error("failed to operate on build artifact database: {path}")]
69    DatabaseOperationFailed {
70        path: PathBuf,
71        #[source]
72        source: rusqlite::Error,
73    },
74    #[error("failed to read cached source file: {path}")]
75    FileReadFailed {
76        path: PathBuf,
77        #[source]
78        source: std::io::Error,
79    },
80}