mapscache

package
v0.13.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2026 License: GPL-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package mapscache manages on-disk PMTiles archives for offline use.

Index

Constants

View Source
const DefaultMapsBaseURL = "https://maps.nw5w.com"

DefaultMapsBaseURL is the production tile/download host. Override per env via New if you ever spin up a staging endpoint.

Variables

View Source
var ErrAlreadyInflight = errors.New("download already in flight")

ErrAlreadyInflight is returned by Start when another download for the same slug is already running. Callers may ignore this and tell the user the download is already happening.

Functions

This section is empty.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager orchestrates per-state PMTiles downloads. Active downloads run in goroutines; the in-memory `inflight` map exposes live byte counters so the HTTP handler can report progress without hammering the store. Completion (or failure) writes the final state to the store and removes the inflight entry. At most maxConcurrent downloads run at once.

func New

func New(cacheDir string, store *configstore.Store, tokenProvider func(context.Context) string, mapsBaseURL string, maxConcurrent int) *Manager

New constructs a Manager. tokenProvider is called per-download to fetch the current bearer token (it may change if the user re-registers); mapsBaseURL should be DefaultMapsBaseURL in production; maxConcurrent caps how many downloads run in parallel (defaults to 2 if non-positive — be polite to the upstream).

func (*Manager) Delete

func (m *Manager) Delete(ctx context.Context, slug string) error

Delete cancels any in-flight download for slug, removes the persisted row, and best-effort removes the on-disk file. Idempotent on absent slugs.

func (*Manager) List

func (m *Manager) List(ctx context.Context) ([]Status, error)

List returns the status of every state with a persisted row plus any in-flight downloads not yet persisted (first-time downloads before the upstream Content-Length comes back). Inflight entries override persisted ones so re-downloads show live progress.

func (*Manager) MigrateLegacyArchives added in v0.12.1

func (m *Manager) MigrateLegacyArchives(ctx context.Context) error

MigrateLegacyArchives moves legacy bare-slug files (<cache>/colorado.pmtiles) into the new namespaced state subdir (<cache>/state/colorado.pmtiles). Idempotent: skips files already in subdirs and skips non-pmtiles files.

Collision policy: if the namespaced target already exists, the legacy file is removed rather than overwriting the (presumably newer) namespaced file. Otherwise os.Rename clobbers a file an operator may have already redownloaded under the new layout. Designed to run once on startup; safe to re-run.

func (*Manager) PathFor

func (m *Manager) PathFor(slug string) string

PathFor returns the on-disk path of slug's PMTiles archive. For namespaced slugs, the slashes become subdirectory separators:

state/colorado            -> <cache>/state/colorado.pmtiles
country/de                -> <cache>/country/de.pmtiles
province/ca/british-...   -> <cache>/province/ca/british-...pmtiles

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, slug string) error

Start kicks off a download for slug. Returns ErrAlreadyInflight if another download for the same slug is already running. Idempotent otherwise — re-downloads succeed by replacing the file atomically. The caller does not block: this returns as soon as the goroutine is spawned.

func (*Manager) Status

func (m *Manager) Status(ctx context.Context, slug string) (Status, error)

Status returns a snapshot for slug. Reads in-memory live counters when an active download is in progress; falls back to the persisted row otherwise. Returns State=="absent" with an empty Status if no row exists.

type Status

type Status struct {
	Slug            string    `json:"slug"`
	State           string    `json:"state"` // "absent" | "pending" | "downloading" | "complete" | "error"
	BytesTotal      int64     `json:"bytes_total"`
	BytesDownloaded int64     `json:"bytes_downloaded"`
	DownloadedAt    time.Time `json:"downloaded_at,omitempty"`
	ErrorMessage    string    `json:"error_message,omitempty"`
}

Status is a snapshot of one slug's download state.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL