Documentation
¶
Overview ¶
Package index is the rebuildable SQLite index of a vault: notes, tags (nested a/b included), frontmatter properties, links and full-text search. The .md files stay the source of truth; the index can be deleted at any time and is rebuilt from disk.
It uses modernc.org/sqlite, a pure-Go SQLite, so every target (desktop, server, phones) builds without cgo. There are no migrations: when the schema version changes the database is dropped and rebuilt.
Index ¶
- Constants
- Variables
- func Path(v *vault.Vault) string
- type Cond
- type Entry
- type Index
- func (ix *Index) Backlinks(p string) ([]Link, error)
- func (ix *Index) Close() error
- func (ix *Index) Count() (int, error)
- func (ix *Index) DB() *sql.DB
- func (ix *Index) Get(p string) (Entry, error)
- func (ix *Index) Links(p string) ([]Link, error)
- func (ix *Index) Lookup(ref string) ([]string, error)
- func (ix *Index) Query(q Query) ([]Entry, error)
- func (ix *Index) Rebuild(v *vault.Vault) (Stats, error)
- func (ix *Index) Remove(rel string) error
- func (ix *Index) Resolve(target, from string) (string, error)
- func (ix *Index) Search(q string, filter Query) ([]Entry, error)
- func (ix *Index) Sync(v *vault.Vault) (Stats, error)
- func (ix *Index) Tags() ([]TagCount, error)
- func (ix *Index) Update(v *vault.Vault, rel string) error
- type Link
- type Query
- type Sort
- type Stats
- type TagCount
Constants ¶
const FileName = "index.db"
FileName is the index file inside the vault's .nota directory.
const SchemaVersion = 1
SchemaVersion is bumped on every schema change; a database with another version is dropped and rebuilt.
Variables ¶
var ErrEmptyQuery = errors.New("index: empty search query")
ErrEmptyQuery is returned by Search for a query without search terms.
Functions ¶
Types ¶
type Cond ¶
type Cond struct {
Key string
Value string
// Not inverts the condition: the key is missing or has another value.
Not bool
}
Cond is one property condition of a query.
type Entry ¶
type Entry struct {
Path string `json:"path"`
Folder string `json:"folder"`
Title string `json:"title"`
Tags []string `json:"tags"`
Props map[string]any `json:"properties"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Modified time.Time `json:"modified"`
Size int64 `json:"size"`
// Snippet is the matching text, for search results.
Snippet string `json:"snippet,omitempty"`
}
Entry is one note as the index knows it.
type Index ¶
type Index struct {
// Recreated is true when Open found no index or an incompatible one.
// The caller should Sync before trusting query results.
Recreated bool
// contains filtered or unexported fields
}
Index wraps the SQLite database.
func Open ¶
Open opens (or creates) the index at path. Use ":memory:" in tests. A database with another schema version, or one that is not readable as an index, is deleted and created again.
func (*Index) Backlinks ¶
Backlinks returns the links from other notes (or the note itself) that resolve to p.
func (*Index) Lookup ¶
Lookup finds notes for a user-supplied reference such as a CLI argument: an exact path (".md" optional), else a title or file name (any case), else a prefix of a path, title or file name. It returns every candidate of the first rule that matches, sorted by path.
func (*Index) Search ¶
Search runs a full-text search over titles and bodies, best match first; title matches weigh more. q is plain text: words must all occur (the last one as a prefix), "quoted phrases" match as phrases, -word excludes and OR between words means either. The filters in filter (folder, tag, where) apply as in Query; its Sort is ignored.
func (*Index) Sync ¶
Sync brings the index up to date with the vault: new and changed notes (by mtime and size, then content hash) are indexed, missing ones removed.
type Link ¶
type Link struct {
// From is the linking note, To the linked one ("" when unresolved).
From string `json:"from"`
To string `json:"to"`
Target string `json:"target"`
Heading string `json:"heading,omitempty"`
Alias string `json:"alias,omitempty"`
Embed bool `json:"embed,omitempty"`
}
Link is a resolved link between two notes.
type Query ¶
type Query struct {
// Folder limits results to a folder and its subfolders.
Folder string
// Tag matches the tag and its nested tags: "a" matches "a" and "a/b".
Tag string
Where []Cond
Sort Sort
Limit int
}
Query selects notes. Empty fields do not filter.
type Sort ¶
type Sort struct {
// Field is one of "path", "title", "created", "updated", "modified".
Field string
Desc bool
}
Sort orders query results.