Documentation
¶
Overview ¶
Package libs loads SysML/KerML standard-library files (bundled via embed.FS or overridden on disk) and maintains a persistent cache of their indexed symbols. See spec section 10.
Index ¶
- Constants
- Variables
- func BuildSnapshot(src Source) ([]byte, error)
- func DecodeSnapshot(data []byte, digest string) (*symbols.Index, error)
- func FrozenLibrary() *symbols.Index
- func LoadInto(idx *symbols.Index)
- func NewModelIndex() *symbols.Index
- func SharedBase() *symbols.Index
- func SnapshotIndex() (*symbols.Index, error)
- func TierOf(name string) symbols.LibraryTier
- type Cache
- type IndexRecord
- type Loader
- type Source
Constants ¶
const LibraryPathEnvVar = "OPENSYSML_LIBRARY_PATH"
LibraryPathEnvVar names the directory to load the standard library from instead of the embedded copy. The legacy SYSML_LIBRARY_PATH name remains accepted; the OPENSYSML_ name wins when both are set.
Variables ¶
var ErrSnapshotStale = errors.New("libs: library snapshot does not match the library files")
ErrSnapshotStale reports a snapshot built from different library files, or in a different format, than the ones in hand.
Functions ¶
func BuildSnapshot ¶ added in v0.4.3
BuildSnapshot loads src into a fresh index the way a process without a snapshot would, freezes it, and returns the snapshot bytes for it. The result is a function of the files alone: the same files give the same bytes.
func DecodeSnapshot ¶ added in v0.4.3
DecodeSnapshot rebuilds the frozen index a snapshot holds, provided it was built from files with the given set digest (see Loader.setDigest) in the current format; otherwise it reports ErrSnapshotStale.
func FrozenLibrary ¶ added in v0.4.3
FrozenLibrary returns a frozen index holding the standard library: the embedded snapshot decoded when it matches the library files, else the files loaded and frozen. A snapshot that fails to decode is logged, since only a build defect makes one.
func LoadInto ¶ added in v0.1.2
LoadInto loads every standard library file into idx, using the symbol cache when it is available. A failure is non-fatal: it is logged, and the index stays usable for a model that does not depend on the file that failed.
func NewModelIndex ¶ added in v0.2.0
NewModelIndex returns an index for one model to add its documents to: an overlay over the shared standard library, which the model reads but cannot write to.
func SharedBase ¶ added in v0.2.0
SharedBase returns the frozen index holding the standard library, built on first use and shared by every model afterwards: the library is the same for every model and immutable once loaded, so one copy serves them all.
func SnapshotIndex ¶ added in v0.4.3
SnapshotIndex returns the frozen library index decoded from the embedded snapshot, when the snapshot was built from exactly the files DefaultSource yields. It fails with ErrSnapshotStale when it was not — an edited bundled file, a LibraryPathEnvVar override with other content — and the caller loads the files instead.
func TierOf ¶ added in v0.5.0
func TierOf(name string) symbols.LibraryTier
TierOf classifies a library file by the directory of the bundle it is under, as a Source names it; a file outside the bundle's layout is of no stated tier.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache persists reduced library IndexRecords to disk keyed by content hash and format version. A hit lets the loader skip lexing/parsing entirely. The same mechanism serves git dependencies (Plan 5c) since it keys purely on content.
func NewCache ¶
NewCache resolves the cache directory ($XDG_CACHE_HOME or os.UserCacheDir), appends "sysml-ls/libs", creates it, and returns a ready Cache.
func (*Cache) Load ¶
func (c *Cache) Load(key string) (*IndexRecord, bool)
Load returns the cached record for key, or (nil, false) on any miss (absent file, read error, or decode error — all treated as a benign miss).
func (*Cache) Prune ¶
func (c *Cache) Prune()
Prune removes records that no load has hit for maxIdleAge, and the temp files a crashed store left behind. Cache entries are otherwise never invalidated explicitly — a key that no longer matches is simply missed — so without this the directory keeps every record any library version ever produced.
func (*Cache) Store ¶
func (c *Cache) Store(key string, rec *IndexRecord) error
Store gob-encodes rec and writes it to <dir>/<key>.idx atomically: it writes a temp file of its own then renames it into place, so a concurrent Load or a crash never observes a partially written file, and two writers of one key cannot truncate or publish each other's temp. A failed encode/write removes the temp and leaves any existing final file untouched.
type IndexRecord ¶
type IndexRecord struct {
Name string
Facts []factRecord
}
IndexRecord is the derived analysis persisted for one library document.
type Loader ¶
type Loader struct {
// RequireResolved makes LoadAll cache no document with an unresolved
// specialization target, since a key does not describe the index a record was
// built in.
RequireResolved bool
// contains filtered or unexported fields
}
Loader loads library files from a Source and registers them into a target index, using a Cache to skip the derivation a load would otherwise repeat. Every file is parsed and indexed on every load path, so a library contributes its declarations whether or not the cache held anything for it; a record carries only the derived facts whose derivation dominates a cold load.
func (*Loader) Hits ¶ added in v0.2.0
Hits reports how many documents of the last LoadAll had their facts installed from a cache record instead of derived, which is how a caller tells a warm load from a cold one.
func (*Loader) LoadAll ¶ added in v0.2.0
LoadAll registers every file of the source into idx as library content and installs the derived facts of each. It loads the whole set at once because a record holds supertype names a sibling file may declare; an incomplete library is reported and not cached.
Hashing and parsing are per file and pure, so the files are hashed and parsed concurrently; the index is then written in source order, so the result is the one a serial load produces.
type Source ¶
type Source interface {
// List returns the relative paths of available library files, sorted.
List() []string
// Read returns the bytes of the named library file, or an error.
Read(name string) ([]byte, error)
}
Source yields standard-library file contents by relative path (e.g. "Kernel Libraries/Kernel Data Type Library/ScalarValues.kerml").
func DefaultSource ¶
func DefaultSource() Source
DefaultSource returns a dirSource rooted at LibraryPathEnvVar when that environment variable is set and non-empty, otherwise the embedded source.
func EmbeddedSource ¶ added in v0.4.3
func EmbeddedSource() Source
EmbeddedSource returns the bundled copy of the standard library, whatever LibraryPathEnvVar says: it is what the embedded snapshot is generated from.
func NewDirSource ¶
NewDirSource returns a Source that reads .kerml/.sysml files from dir.
func SharedLibrary ¶ added in v0.5.0
SharedLibrary returns the shared frozen index (see SharedBase) together with the source serving the bytes it was built from. The source answers only for the files the index holds, and with the text they had when it was built, so a span the index carries always addresses the text the source serves — even when a LibraryPathEnvVar override is edited while the process runs.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Command gensnapshot writes the bundled library's snapshot, the derived artifact package libs embeds; run through `go generate ./internal/core/libs`.
|
Command gensnapshot writes the bundled library's snapshot, the derived artifact package libs embeds; run through `go generate ./internal/core/libs`. |