---
title: "Using Aliases with stamp"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Using Aliases with stamp}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(stamp)
old_alias <- st_alias_get()
```

# Overview

Aliases let you work with multiple, independent stamp folders in a single R session.
An alias is a selector for which folder configuration to use; it does not change any on-disk paths.
All artifact paths remain the same regardless of alias; aliases select the folder configuration for the catalog and versions, not the session options.

**Storage model (v0.0.9+):** Artifacts are stored as `<root>/<path>/<filename>/<filename>`
with version history in `<root>/<path>/<filename>/versions/`. The `.stamp/` directory
within each aliased root contains only state (catalog, locks, temp files).

Key properties:

- Aliases select a specific stamp folder initialized via `st_init()`.
- Paths never include alias segments; alias affects catalog/versions only.
- Reusing one alias for different folders errors.
- Different aliases pointing to the same folder warn; they share the same catalog/versions.
- Back-compat alias `"default"` exists and can be re-based with `st_switch()`.

# Setup

Initialize one or more stamp folders and give each an alias:

```{r}
root_a <- tempfile("projA-")
root_b <- tempfile("projB-")
alias_a <- basename(root_a)
alias_b <- basename(root_b)

# Note: st_init() creates the folder if missing
st_init(root_a, alias = alias_a)
st_init(root_b, alias = alias_b)

# Inspect what was created
fs::dir_tree(fs::path(root_a, ".stamp"), recurse = TRUE, all = TRUE)
fs::dir_tree(fs::path(root_b, ".stamp"), recurse = TRUE, all = TRUE)
```

You can inspect registered aliases:

```{r}
st_alias_list()
# Get alias details
st_alias_get(alias_a)
```

# Saving and Loading with Aliases

Use the same artifact path patterns; pass `alias` to select which catalog/versions apply:

```{r}
pA <- fs::path(root_a, "data.qs")
pB <- fs::path(root_b, "data.qs")

# Save different data to A and B to ensure versions are created
st_save(data.frame(id = 1:2), pA, alias = alias_a)
st_save(data.frame(id = 3:4), pB, alias = alias_b)

# Each alias has its own version history
st_versions(pA, alias = alias_a)
st_versions(pB, alias = alias_b)

# Loading respects the alias
objA <- st_load(pA, alias = alias_a)
objB <- st_load(pB, alias = alias_b)
list(A = objA, B = objB)

# Inspect what was created within the stamp folders
# Note: Artifacts are stored as <root>/<path>/<filename>/<filename>
# with versions in <root>/<path>/<filename>/versions/
cat("\nStructure in root_a:\n")
fs::dir_tree(root_a, recurse = 2)

cat("\nStructure in root_b:\n")
fs::dir_tree(root_b, recurse = 2)
```

Version resolution is non-interactive. `NULL` or `0` resolve to the latest version:

```{r}
# Load latest (using default version = NULL for latest)
latestA <- st_load(pA, alias = alias_a)
latestB <- st_load(pB, alias = alias_b)

list(latestA = latestA, latestB = latestB)
```

# Switching the Default Alias

The special alias `"default"` exists for backward compatibility. You can re-base it to point at any initialized folder and keep legacy state in sync:

```{r}
# Re-base default to alias A's folder (silent)
st_switch(alias_a)

# Now calls without alias use whatever folder default points to
st_save(data.frame(id = 5), fs::path(root_a, "more.qs"))
```

# Constraints and Conflicts

- Reusing the same alias for two different folders errors during `st_init()`.
- Pointing two different aliases to the same folder warns; both operate on the same underlying catalog/versions.
- Aliases are case-sensitive and trimmed; prefer simple names (letters, digits, `_`, `-`).

# Best Practices

- Keep per-project aliases (`A`, `B`, or project codes) and avoid reassigning them.
- Store the alias choice alongside project scripts to make intent explicit.
- Use `st_switch()` sparingly; explicit `alias =` in calls is clearer in shared code.
- Version resolution: pass explicit version ids when pinning; use `0` for latest.

# Troubleshooting

- "alias conflict" error: You tried to reuse an alias for a different folder. Pick a new alias or re-point the original alias only if you intend to share catalogs.
- "same folder" warning: Two aliases target the same folder; this is allowed but both will share catalog/versions.
- Latest not loading: Ensure you passed `version = 0` or `NULL`; loading without `version` loads the current version by default.

# Notes

- Internally, `stamp` maintains a catalog with an index for lineage (`parents_index`) to make reverse lookups fast. Aliases do not change filesystem layout; they only select which catalog is used.
- Options like retention, verification, and primary key policies apply to the session. Configure them with `st_opts()`; aliases select folders, not option sets.
- **Storage architecture (v0.0.9+):** Each aliased root stores:
  - `.stamp/` - state only (catalog, locks, temp)
  - `<path>/<filename>/` - artifact directories containing:
    - `<filename>` - the actual file
    - `stmeta/` - sidecar metadata
     - `versions/` - version history for that specific artifact

```{r cleanup, include=FALSE}
if (!is.null(old_alias)) {
  st_init(old_alias$root, state_dir = old_alias$state_dir, verbose = FALSE)
}
unlink(c(root_a, root_b), recursive = TRUE)
```
