index

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package index wraps Bleve: schema definition, Open/Create, and the Upsert/Delete/Stats surface used by sync, query, and MCP layers.

Index

Constants

View Source
const DefaultIdleTimeout = 30 * time.Second

DefaultIdleTimeout is how long LazyIndex waits with zero refs before closing the underlying handle and releasing the OS file lock.

View Source
const DefaultRetryBudget = 30 * time.Second

DefaultRetryBudget caps how long Acquire will keep retrying when the underlying open hits ErrIndexLocked (i.e. another process holds the writer lock).

View Source
const SchemaVersion = 1

SchemaVersion is bumped when the field mapping changes in a backwards-incompatible way. sync compares it against meta.json and rebuilds the index on mismatch.

Variables

View Source
var ErrIndexLocked = errors.New("index is locked by another process")

ErrIndexLocked is returned when the bbolt file lock cannot be acquired within openLockTimeout. Most commonly this means another codesearch process (typically the `serve` MCP server, or a parallel `sync`) is holding an incompatible lock.

Functions

func BuildMapping

func BuildMapping() *mapping.IndexMappingImpl

BuildMapping returns the Bleve index mapping. The default document mapping has Dynamic enabled so any frontmatter key becomes an indexed field automatically; the named fields below are pinned so they have consistent analyzers regardless of what shows up in dynamic mode.

Types

type Index

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

Index wraps bleve.Index with the Upsert/Delete/Iterate surface that the sync, query, and MCP layers consume.

func Create

func Create(path string) (*Index, error)

Create builds a new index at path using BuildMapping.

func Open

func Open(path string) (*Index, error)

Open opens an existing index at path for read/write access. If the underlying bbolt lock cannot be acquired within openLockTimeout, returns a wrapped ErrIndexLocked.

func OpenReadOnly

func OpenReadOnly(path string) (*Index, error)

OpenReadOnly opens an existing index at path with read-only semantics (shared lock). Multiple read-only handles can coexist; a writer is still blocked while any read-only handle is open.

func (*Index) Bleve

func (i *Index) Bleve() bleve.Index

Bleve returns the underlying bleve.Index for query construction.

func (*Index) Close

func (i *Index) Close() error

Close releases the underlying resources.

func (*Index) Delete

func (i *Index) Delete(id string) error

Delete removes a document by ID (repo-relative path).

func (*Index) DocCount

func (i *Index) DocCount() (uint64, error)

DocCount returns the number of indexed documents.

func (*Index) Iterate

func (i *Index) Iterate(fn func(Stored) error) error

Iterate calls fn once per indexed document, paginating internally. The fn must return nil to continue; any error aborts iteration.

func (*Index) Path

func (i *Index) Path() string

Path is the on-disk location of the index directory.

func (*Index) Upsert

func (i *Index) Upsert(d *doc.Doc) error

Upsert indexes (or replaces) a document under d.Path.

type LazyIndex

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

LazyIndex is a refcounted, idle-closing wrapper around a read-only *Index. The MCP server uses it so concurrent tool requests share a single open handle while sync/query CLI invocations can still acquire the OS file lock during idle windows.

Concurrent Acquire calls are coalesced: the first opens the handle, the rest wait. Once all refs are released, an idle timer closes the handle so other processes are free to write.

func NewLazy

func NewLazy(path string, idle, retry time.Duration) *LazyIndex

NewLazy constructs a LazyIndex over path. idle and retry default to DefaultIdleTimeout and DefaultRetryBudget when zero.

func (*LazyIndex) Acquire

func (l *LazyIndex) Acquire() (*Index, func(), error)

Acquire returns the shared *Index and a release func. Callers must invoke release exactly once (typically with defer). Opens the index lazily on the first uncontended call; subsequent concurrent calls reuse the same handle.

func (*LazyIndex) Close

func (l *LazyIndex) Close() error

Close releases the underlying handle. It errors if any refs are outstanding so a buggy shutdown can't yank the index from under an in-flight request.

type Stored

type Stored struct {
	Path   string
	Digest string
	MTime  float64 // unix nanoseconds
	Size   float64
}

Stored captures the bookkeeping fields the sync layer needs to compare against on-disk files without re-parsing the body.

Jump to

Keyboard shortcuts

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