SymbolRecord carries the optional compiler signature and the snapshot schema moves to 3; restore accepts schemas 1-3, verifies the digest for 3 and persists the upgrade. Registered index tests cover signature persistence, a schema-2 migration keeping records and oversized rejection. Signature help merges every observed signature for the name (document occurrences and index) with the compiler-selected one first; a real stdio session with two called overloads returned both labels. This is a candidate list, not overload resolution.
165 lines
6.2 KiB
C++
165 lines
6.2 KiB
C++
#pragma once
|
|
|
|
#include <compare>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <filesystem>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace vls::index {
|
|
|
|
// Zero-based LSP-style character coordinates within one physical line.
|
|
struct Location {
|
|
std::string path;
|
|
std::uint32_t line{}, character{};
|
|
std::uint32_t endLine{}, endCharacter{};
|
|
auto operator<=>(const Location &) const = default;
|
|
};
|
|
|
|
enum class SymbolKind { unknown, function, type, variable, field, namespace_, module };
|
|
|
|
struct SymbolRecord {
|
|
std::string name;
|
|
SymbolKind kind{SymbolKind::unknown};
|
|
Location location; // declaration or definition
|
|
std::optional<Location> definition;
|
|
std::vector<Location> references;
|
|
std::string provenance;
|
|
// Compiler-provided call signature, when the observer reported one.
|
|
std::optional<std::string> signature;
|
|
auto operator<=>(const SymbolRecord &) const = default;
|
|
};
|
|
|
|
struct Limits {
|
|
std::size_t contexts{64};
|
|
std::size_t records{65536};
|
|
std::size_t recordsPerContext{16384};
|
|
std::size_t referencesPerRecord{256};
|
|
std::size_t nameBytes{256};
|
|
std::size_t signatureBytes{320};
|
|
std::size_t pathBytes{4096};
|
|
std::size_t snapshotBytes{32 * 1024 * 1024};
|
|
};
|
|
|
|
struct ContextInput {
|
|
std::string context; // Build::ContextKey alternative
|
|
std::vector<std::string> dependencies;
|
|
// Path and content identity of every source this context consumed.
|
|
std::vector<std::pair<std::string, std::string>> sources;
|
|
std::vector<SymbolRecord> records;
|
|
bool complete{true};
|
|
std::string reason;
|
|
};
|
|
|
|
struct Coverage {
|
|
bool indexed{}; // at least one context or overlay was considered
|
|
bool complete{};
|
|
std::vector<std::string> incomplete; // context names, sorted
|
|
auto operator<=>(const Coverage &) const = default;
|
|
};
|
|
|
|
struct Query {
|
|
std::string name;
|
|
bool substring{};
|
|
std::optional<std::string> context;
|
|
std::size_t limit{100};
|
|
};
|
|
|
|
struct Result {
|
|
std::vector<SymbolRecord> symbols;
|
|
Coverage coverage;
|
|
bool truncated{};
|
|
};
|
|
|
|
// Immutable published snapshot. Readers keep it alive across later publications
|
|
// and evictions.
|
|
class Generation {
|
|
public:
|
|
[[nodiscard]] std::uint64_t number() const noexcept { return number_; }
|
|
[[nodiscard]] const std::vector<SymbolRecord> &symbols() const noexcept { return records_; }
|
|
[[nodiscard]] const std::vector<std::pair<std::string, Coverage>> &contexts() const noexcept {
|
|
return contexts_;
|
|
}
|
|
|
|
private:
|
|
friend class SymbolIndex;
|
|
Generation(std::uint64_t number, std::vector<SymbolRecord> records,
|
|
std::vector<std::pair<std::string, Coverage>> contexts)
|
|
: number_(number), records_(std::move(records)), contexts_(std::move(contexts)) {}
|
|
std::uint64_t number_;
|
|
std::vector<SymbolRecord> records_;
|
|
std::vector<std::pair<std::string, Coverage>> contexts_;
|
|
};
|
|
|
|
// Context-aware, bounded symbol index. Committed records belong to explicit
|
|
// analysis contexts; unsaved document overlays shadow committed records for
|
|
// the same path and are reconciled on save/close. A changed, deleted or
|
|
// renamed source marks dependent contexts incomplete without discarding the
|
|
// last-good records (degraded navigation, never an authoritative rename).
|
|
class SymbolIndex {
|
|
public:
|
|
explicit SymbolIndex(Limits = Limits{});
|
|
~SymbolIndex();
|
|
SymbolIndex(const SymbolIndex &) = delete;
|
|
SymbolIndex &operator=(const SymbolIndex &) = delete;
|
|
|
|
// Atomically replaces one context and publishes a new generation.
|
|
[[nodiscard]] bool publish(const ContextInput &);
|
|
[[nodiscard]] bool remove(std::string_view context);
|
|
|
|
// Unsaved document records. The newest overlay for a path shadows the
|
|
// committed records of that path until it is cleared.
|
|
[[nodiscard]] bool set_overlay(std::string path, std::string content,
|
|
std::vector<SymbolRecord> records);
|
|
[[nodiscard]] bool clear_overlay(std::string_view path);
|
|
|
|
// A new content (or absence) invalidates every context that consumed the
|
|
// path. Matching content is a no-op.
|
|
[[nodiscard]] std::size_t invalidate(std::string_view path,
|
|
const std::optional<std::string> &content);
|
|
|
|
[[nodiscard]] Result find(const Query &) const;
|
|
// Records whose use location is in PATH, with the same overlay shadowing,
|
|
// deduplication and coverage reporting as find().
|
|
[[nodiscard]] Result find_by_path(std::string_view path, std::size_t limit = 4096) const;
|
|
[[nodiscard]] Coverage coverage() const;
|
|
[[nodiscard]] std::shared_ptr<const Generation> current() const;
|
|
[[nodiscard]] std::uint64_t generation() const;
|
|
[[nodiscard]] std::size_t records() const;
|
|
[[nodiscard]] const Limits &limits() const noexcept;
|
|
|
|
// Versioned persistence. restore() classifies the snapshot; incompatible or
|
|
// corrupt bytes are quarantined and the index rebuilds into a new generation.
|
|
enum class LoadStatus { loaded, missing, incompatible, corrupt, oversized };
|
|
struct LoadResult {
|
|
LoadStatus status{LoadStatus::missing};
|
|
std::uint64_t generation{};
|
|
std::optional<std::filesystem::path> quarantine;
|
|
// True when a schema-1 snapshot was accepted and upgraded in memory.
|
|
bool migrated{};
|
|
};
|
|
// Writes the versioned snapshot (schema 2 with a canonical payload digest).
|
|
// restore() also accepts schema 1 and reports the upgrade as migrated.
|
|
[[nodiscard]] bool save(const std::filesystem::path &) const;
|
|
[[nodiscard]] std::expected<LoadResult, std::string> restore(const std::filesystem::path &);
|
|
// True only when a compatible snapshot was loaded.
|
|
[[nodiscard]] bool load(const std::filesystem::path &);
|
|
// Generation-directory backend: each save keeps the previous generation for
|
|
// crash recovery, CURRENT selects the newest, and a legacy index.json is
|
|
// migrated on the first restore.
|
|
[[nodiscard]] bool save_generation(const std::filesystem::path &);
|
|
[[nodiscard]] std::expected<LoadResult, std::string>
|
|
restore_generation(const std::filesystem::path &);
|
|
|
|
private:
|
|
struct State;
|
|
std::shared_ptr<State> state_;
|
|
};
|
|
|
|
} // namespace vls::index
|