Documentation
¶
Index ¶
- Constants
- Variables
- func BuildLetterFilterSQL(letter *string, column string) (clauses []string, args []any)
- func BuildTagFilterSQL(filters []zapscript.TagFilter) (clauses []string, args []any)
- type BatchInserter
- type MediaDB
- func (db *MediaDB) Allocate() error
- func (db *MediaDB) Analyze() error
- func (db *MediaDB) BackgroundOperationDone()
- func (db *MediaDB) BeginTransaction(batchEnabled bool) error
- func (db *MediaDB) BrowseDirectories(ctx context.Context, pathPrefix string) ([]database.BrowseDirectoryResult, error)
- func (db *MediaDB) BrowseFileCount(ctx context.Context, pathPrefix string, letter *string) (int, error)
- func (db *MediaDB) BrowseFiles(ctx context.Context, opts *database.BrowseFilesOptions) ([]database.SearchResultWithCursor, error)
- func (db *MediaDB) BrowseRootCounts(ctx context.Context, rootDirs []string) (map[string]*int, error)
- func (db *MediaDB) BrowseVirtualSchemes(ctx context.Context) ([]database.BrowseVirtualScheme, error)
- func (db *MediaDB) BulkSetMediaMissing(dbids map[int]struct{}) error
- func (db *MediaDB) CheckForDuplicateMediaTitles() ([]string, error)
- func (db *MediaDB) Close() error
- func (db *MediaDB) CommitTransaction() error
- func (*MediaDB) CreateIndexes() error
- func (db *MediaDB) CreateSecondaryIndexes() error
- func (db *MediaDB) DeleteMediaTag(mediaDBID, tagDBID int64) error
- func (db *MediaDB) DeleteMediaTags(mediaDBID int64) error
- func (db *MediaDB) DropSecondaryIndexes() error
- func (db *MediaDB) Exists() bool
- func (db *MediaDB) FindMedia(row database.Media) (database.Media, error)
- func (db *MediaDB) FindMediaTag(row database.MediaTag) (database.MediaTag, error)
- func (db *MediaDB) FindMediaTitle(row *database.MediaTitle) (database.MediaTitle, error)
- func (db *MediaDB) FindOrInsertMedia(row database.Media) (database.Media, error)
- func (db *MediaDB) FindOrInsertMediaTag(row database.MediaTag) (database.MediaTag, error)
- func (db *MediaDB) FindOrInsertMediaTitle(row *database.MediaTitle) (database.MediaTitle, error)
- func (db *MediaDB) FindOrInsertSystem(row database.System) (database.System, error)
- func (db *MediaDB) FindOrInsertTag(row database.Tag) (database.Tag, error)
- func (db *MediaDB) FindOrInsertTagType(row database.TagType) (database.TagType, error)
- func (db *MediaDB) FindSystem(row database.System) (database.System, error)
- func (db *MediaDB) FindSystemBySystemID(systemID string) (database.System, error)
- func (db *MediaDB) FindTag(row database.Tag) (database.Tag, error)
- func (db *MediaDB) FindTagType(row database.TagType) (database.TagType, error)
- func (db *MediaDB) GetAllMedia() ([]database.Media, error)
- func (db *MediaDB) GetAllMediaTitles() ([]database.MediaTitle, error)
- func (db *MediaDB) GetAllSystems() ([]database.System, error)
- func (db *MediaDB) GetAllTagTypes() ([]database.TagType, error)
- func (db *MediaDB) GetAllTags() ([]database.Tag, error)
- func (db *MediaDB) GetAllUsedTags(ctx context.Context) ([]database.TagInfo, error)
- func (db *MediaDB) GetCachedSlugResolution(ctx context.Context, systemID, slug string, tagFilters []zapscript.TagFilter) (mediaDBID int64, strategy string, found bool)
- func (db *MediaDB) GetCachedStats(ctx context.Context, query *database.MediaQuery) (MediaStats, bool)
- func (db *MediaDB) GetDBPath() string
- func (db *MediaDB) GetIndexingStatus() (string, error)
- func (db *MediaDB) GetIndexingSystems() ([]string, error)
- func (db *MediaDB) GetLastGenerated() (time.Time, error)
- func (db *MediaDB) GetLastIndexedSystem() (string, error)
- func (db *MediaDB) GetLaunchCommandForMedia(ctx context.Context, systemID, path string) (string, error)
- func (db *MediaDB) GetMaxMediaID() (int64, error)
- func (db *MediaDB) GetMaxMediaTagID() (int64, error)
- func (db *MediaDB) GetMaxSystemID() (int64, error)
- func (db *MediaDB) GetMaxTagID() (int64, error)
- func (db *MediaDB) GetMaxTagTypeID() (int64, error)
- func (db *MediaDB) GetMaxTitleID() (int64, error)
- func (db *MediaDB) GetMediaByDBID(ctx context.Context, mediaDBID int64) (database.SearchResultWithCursor, error)
- func (db *MediaDB) GetMediaBySystemID(systemID string) ([]database.MediaWithFullPath, error)
- func (db *MediaDB) GetMediaTagsBySystemID(systemID string) ([]database.MediaTagLink, error)
- func (db *MediaDB) GetMediaWithFullPath() ([]database.MediaWithFullPath, error)
- func (db *MediaDB) GetMediaWithFullPathExcluding(excludeSystemIDs []string) ([]database.MediaWithFullPath, error)
- func (db *MediaDB) GetOptimizationStatus() (string, error)
- func (db *MediaDB) GetOptimizationStep() (string, error)
- func (db *MediaDB) GetSystemTagsCached(ctx context.Context, systems []systemdefs.System) ([]database.TagInfo, error)
- func (db *MediaDB) GetSystemsExcluding(excludeSystemIDs []string) ([]database.System, error)
- func (db *MediaDB) GetTags(ctx context.Context, systems []systemdefs.System) ([]database.TagInfo, error)
- func (db *MediaDB) GetTitlesBySystemID(systemID string) ([]database.TitleWithSystem, error)
- func (db *MediaDB) GetTitlesWithPreFilter(ctx context.Context, systemID string, ...) ([]database.MediaTitle, error)
- func (db *MediaDB) GetTitlesWithSystems() ([]database.TitleWithSystem, error)
- func (db *MediaDB) GetTitlesWithSystemsExcluding(excludeSystemIDs []string) ([]database.TitleWithSystem, error)
- func (db *MediaDB) GetTotalMediaCount() (int, error)
- func (db *MediaDB) GetZapScriptTagsBySystemAndPath(ctx context.Context, systemID, path string) ([]database.TagInfo, error)
- func (db *MediaDB) IndexedSystems() ([]string, error)
- func (db *MediaDB) InsertMedia(row database.Media) (database.Media, error)
- func (db *MediaDB) InsertMediaTag(row database.MediaTag) (database.MediaTag, error)
- func (db *MediaDB) InsertMediaTitle(row *database.MediaTitle) (database.MediaTitle, error)
- func (db *MediaDB) InsertSystem(row database.System) (database.System, error)
- func (db *MediaDB) InsertTag(row database.Tag) (database.Tag, error)
- func (db *MediaDB) InsertTagType(row database.TagType) (database.TagType, error)
- func (db *MediaDB) InvalidateCountCache() error
- func (db *MediaDB) InvalidateSlugCache(ctx context.Context) error
- func (db *MediaDB) InvalidateSlugCacheForSystems(ctx context.Context, systemIDs []string) error
- func (db *MediaDB) InvalidateSystemTagsCache(ctx context.Context, systems []systemdefs.System) error
- func (db *MediaDB) MigrateUp() error
- func (db *MediaDB) Open() error
- func (db *MediaDB) PopulateBrowseCache(ctx context.Context) error
- func (db *MediaDB) PopulateSystemTagsCache(ctx context.Context) error
- func (db *MediaDB) PopulateSystemTagsCacheForSystems(ctx context.Context, systems []systemdefs.System) error
- func (db *MediaDB) RandomGame(systems []systemdefs.System) (database.SearchResult, error)
- func (db *MediaDB) RandomGameWithQuery(query *database.MediaQuery) (database.SearchResult, error)
- func (db *MediaDB) RebuildSlugSearchCache() error
- func (db *MediaDB) RebuildTagCache() error
- func (db *MediaDB) RefreshSlugSearchCacheForSystems(ctx context.Context, systemIDs []string) error
- func (db *MediaDB) ResetMissingFlags(systemDBIDs []int) error
- func (db *MediaDB) RollbackTransaction() error
- func (db *MediaDB) RunBackgroundOptimization(statusCallback func(optimizing bool), pauser *syncutil.Pauser)
- func (db *MediaDB) SearchMediaBySecondarySlug(ctx context.Context, systemID string, secondarySlug string, ...) ([]database.SearchResultWithCursor, error)
- func (db *MediaDB) SearchMediaBySlug(ctx context.Context, systemID string, slug string, tags []zapscript.TagFilter) ([]database.SearchResultWithCursor, error)
- func (db *MediaDB) SearchMediaBySlugIn(ctx context.Context, systemID string, slugList []string, ...) ([]database.SearchResultWithCursor, error)
- func (db *MediaDB) SearchMediaBySlugPrefix(ctx context.Context, systemID string, slugPrefix string, ...) ([]database.SearchResultWithCursor, error)
- func (db *MediaDB) SearchMediaPathExact(systems []systemdefs.System, query string) ([]database.SearchResult, error)
- func (db *MediaDB) SearchMediaPathGlob(systems []systemdefs.System, query string) ([]database.SearchResult, error)
- func (db *MediaDB) SearchMediaWithFilters(ctx context.Context, filters *database.SearchFilters) ([]database.SearchResultWithCursor, error)
- func (db *MediaDB) SetCachedSlugResolution(ctx context.Context, systemID, slug string, tagFilters []zapscript.TagFilter, ...) error
- func (db *MediaDB) SetCachedStats(ctx context.Context, query *database.MediaQuery, stats MediaStats) error
- func (db *MediaDB) SetDBPathForTesting(dbPath string)
- func (db *MediaDB) SetIndexingCacheSize(enable bool)
- func (db *MediaDB) SetIndexingStatus(status string) error
- func (db *MediaDB) SetIndexingSystems(systemIDs []string) error
- func (db *MediaDB) SetLastIndexedSystem(systemID string) error
- func (db *MediaDB) SetOptimizationStatus(status string) error
- func (db *MediaDB) SetOptimizationStep(step string) error
- func (db *MediaDB) SetSQLForTesting(ctx context.Context, sqlDB *sql.DB, platform platforms.Platform) error
- func (db *MediaDB) SystemIndexed(system *systemdefs.System) bool
- func (db *MediaDB) TrackBackgroundOperation()
- func (db *MediaDB) Truncate() error
- func (db *MediaDB) TruncateSystems(systemIDs []string) error
- func (db *MediaDB) UnsafeGetSQLDb() *sql.DB
- func (db *MediaDB) UpdateLastGenerated() error
- func (db *MediaDB) UpdateMediaTitle(mediaDBID, mediaTitleDBID int64) error
- func (db *MediaDB) Vacuum() error
- func (db *MediaDB) WALCheckpoint() error
- func (db *MediaDB) WaitForBackgroundOperations()
- type MediaStats
- type PreFilterQuery
- type SlugMetadata
- type SlugSearchCache
- func (c *SlugSearchCache) CanServeSystems(systemIDs []string) bool
- func (c *SlugSearchCache) ExactSecondarySlugMatch(systemDBIDs []int64, secSlug []byte) []int64
- func (c *SlugSearchCache) ExactSlugMatch(systemDBIDs []int64, slug []byte) []int64
- func (c *SlugSearchCache) ExactSlugMatchAny(systemDBIDs []int64, slugList [][]byte) []int64
- func (c *SlugSearchCache) PrefixSlugMatch(systemDBIDs []int64, prefix []byte) []int64
- func (c *SlugSearchCache) RandomEntry(systemDBIDs []int64) (int64, bool)
- func (c *SlugSearchCache) ResolveSystemDBIDs(systemIDs []string) []int64
- func (c *SlugSearchCache) Search(systemDBIDs []int64, variantGroups [][][]byte) []int64
- func (c *SlugSearchCache) Size() int
- func (c *SlugSearchCache) TrigramIndexSize() int
Constants ¶
const ( IndexingStatusRunning = "running" IndexingStatusPending = "pending" IndexingStatusCompleted = "completed" IndexingStatusFailed = "failed" IndexingStatusCancelled = "cancelled" )
Indexing status constants
const ( DBConfigLastGeneratedAt = "LastGeneratedAt" DBConfigOptimizationStatus = "OptimizationStatus" DBConfigOptimizationStep = "OptimizationStep" DBConfigIndexingStatus = "IndexingStatus" DBConfigLastIndexedSystem = "LastIndexedSystem" DBConfigIndexingSystems = "IndexingSystems" )
Variables ¶
var ErrDependencyFlush = errors.New("dependency flush failed")
ErrDependencyFlush is returned by Flush when a dependency batch inserter fails. Callers can detect this via errors.Is to distinguish a parent-table flush failure from a local constraint violation.
var ErrNullSQL = errors.New("MediaDB is not connected")
Functions ¶
func BuildLetterFilterSQL ¶ added in v2.10.0
BuildLetterFilterSQL constructs SQL WHERE clauses for filtering by the first character of a column. Supports single letters (A-Z), "0-9" for numeric starts, and "#" for symbols (non-alphanumeric).
The column parameter is the SQL expression to filter on, e.g., "MediaTitles.Name" or "mt.Name".
Returns clauses and args in the same format as BuildTagFilterSQL.
func BuildTagFilterSQL ¶ added in v2.7.0
BuildTagFilterSQL constructs SQL WHERE clauses and arguments for tag filtering using a hybrid strategy optimized for SQLite performance:
- AND filters: INTERSECT pattern
- NOT filters: NOT EXISTS pattern
- OR filters: EXISTS with OR conditions
Returns a slice of WHERE clause strings and corresponding arguments. Clauses should be joined with " AND " and appended to the main query's WHERE conditions.
Types ¶
type BatchInserter ¶ added in v2.7.0
type BatchInserter struct {
// contains filtered or unexported fields
}
BatchInserter manages batched multi-row inserts for a specific table
func NewBatchInserter ¶ added in v2.7.0
func NewBatchInserter( ctx context.Context, tx *sql.Tx, tableName string, columns []string, batchSize int, ) (*BatchInserter, error)
NewBatchInserter creates a batch inserter for the given table
func NewBatchInserterWithOptions ¶ added in v2.7.0
func NewBatchInserterWithOptions( ctx context.Context, tx *sql.Tx, tableName string, columns []string, batchSize int, orIgnore bool, ) (*BatchInserter, error)
NewBatchInserterWithOptions creates a batch inserter with OR IGNORE option
func (*BatchInserter) Add ¶ added in v2.7.0
func (b *BatchInserter) Add(values ...any) error
Add appends a row to the current batch and auto-flushes when the batch size is reached. Dependency ordering ensures parent tables are flushed before children, preventing FK violations.
func (*BatchInserter) Close ¶ added in v2.7.0
func (b *BatchInserter) Close() error
Close flushes remaining items and closes all cached statements
func (*BatchInserter) Flush ¶ added in v2.7.0
func (b *BatchInserter) Flush() error
Flush executes the current batch and resets the buffer
func (*BatchInserter) SetDependencies ¶ added in v2.7.0
func (b *BatchInserter) SetDependencies(deps ...*BatchInserter)
SetDependencies sets the parent batch inserters that must flush before this one
type MediaDB ¶
type MediaDB struct {
// contains filtered or unexported fields
}
func (*MediaDB) BackgroundOperationDone ¶ added in v2.7.1
func (db *MediaDB) BackgroundOperationDone()
BackgroundOperationDone decrements the background operations counter. This should be called when an operation started with TrackBackgroundOperation completes.
func (*MediaDB) BeginTransaction ¶
func (*MediaDB) BrowseDirectories ¶ added in v2.10.0
func (db *MediaDB) BrowseDirectories( ctx context.Context, pathPrefix string, ) ([]database.BrowseDirectoryResult, error)
BrowseDirectories returns distinct immediate subdirectory names under the given path prefix.
func (*MediaDB) BrowseFileCount ¶ added in v2.10.0
func (db *MediaDB) BrowseFileCount( ctx context.Context, pathPrefix string, letter *string, ) (int, error)
BrowseFileCount returns the total number of immediate child files under a path prefix.
func (*MediaDB) BrowseFiles ¶ added in v2.10.0
func (db *MediaDB) BrowseFiles( ctx context.Context, opts *database.BrowseFilesOptions, ) ([]database.SearchResultWithCursor, error)
BrowseFiles returns indexed media files that are immediate children of the given path prefix.
func (*MediaDB) BrowseRootCounts ¶ added in v2.10.0
func (db *MediaDB) BrowseRootCounts( ctx context.Context, rootDirs []string, ) (map[string]*int, error)
BrowseRootCounts returns a map of root directory to count of indexed media under each root. A nil *int means the count is not yet available (cache not populated). A non-nil *int is the actual count (which may be 0).
func (*MediaDB) BrowseVirtualSchemes ¶ added in v2.10.0
func (db *MediaDB) BrowseVirtualSchemes( ctx context.Context, ) ([]database.BrowseVirtualScheme, error)
BrowseVirtualSchemes returns distinct URI schemes present in indexed media.
func (*MediaDB) BulkSetMediaMissing ¶ added in v2.11.0
func (*MediaDB) CheckForDuplicateMediaTitles ¶ added in v2.7.0
CheckForDuplicateMediaTitles returns any MediaTitle records that have duplicate (SystemDBID, Slug) combinations. Used in tests to validate data integrity after selective updates.
func (*MediaDB) CommitTransaction ¶
func (*MediaDB) CreateIndexes ¶ added in v2.7.0
func (*MediaDB) CreateSecondaryIndexes ¶ added in v2.10.0
CreateSecondaryIndexes recreates dropped secondary indexes after bulk inserts and self-heals any required indexes missing from existing databases. Called synchronously at the end of indexing so the database is fully searchable when indexing completes.
func (*MediaDB) DeleteMediaTag ¶ added in v2.11.0
func (*MediaDB) DeleteMediaTags ¶ added in v2.11.0
func (*MediaDB) DropSecondaryIndexes ¶ added in v2.10.0
DropSecondaryIndexes drops all secondary indexes to speed up bulk inserts. Call before a full reindex, then call CreateSecondaryIndexes after.
func (*MediaDB) FindMediaTag ¶
func (*MediaDB) FindMediaTitle ¶
func (db *MediaDB) FindMediaTitle(row *database.MediaTitle) (database.MediaTitle, error)
func (*MediaDB) FindOrInsertMedia ¶
func (*MediaDB) FindOrInsertMediaTag ¶
func (*MediaDB) FindOrInsertMediaTitle ¶
func (db *MediaDB) FindOrInsertMediaTitle(row *database.MediaTitle) (database.MediaTitle, error)
func (*MediaDB) FindOrInsertSystem ¶
func (*MediaDB) FindOrInsertTag ¶
func (*MediaDB) FindOrInsertTagType ¶
func (*MediaDB) FindSystem ¶
func (*MediaDB) FindSystemBySystemID ¶ added in v2.7.0
func (*MediaDB) FindTagType ¶
func (*MediaDB) GetAllMedia ¶ added in v2.7.0
func (*MediaDB) GetAllMediaTitles ¶ added in v2.7.0
func (db *MediaDB) GetAllMediaTitles() ([]database.MediaTitle, error)
func (*MediaDB) GetAllSystems ¶ added in v2.7.0
func (*MediaDB) GetAllTagTypes ¶ added in v2.7.0
func (*MediaDB) GetAllTags ¶ added in v2.7.0
func (*MediaDB) GetAllUsedTags ¶ added in v2.7.0
GetAllUsedTags returns all tags that are actually in use (have media associated) This is optimized for the "all systems" case and avoids expensive system filtering
func (*MediaDB) GetCachedSlugResolution ¶ added in v2.7.0
func (db *MediaDB) GetCachedSlugResolution( ctx context.Context, systemID, slug string, tagFilters []zapscript.TagFilter, ) (mediaDBID int64, strategy string, found bool)
GetCachedSlugResolution retrieves a cached slug resolution result. Returns the MediaDBID, strategy name, and true if found; otherwise returns 0, "", false.
func (*MediaDB) GetCachedStats ¶ added in v2.7.0
func (db *MediaDB) GetCachedStats(ctx context.Context, query *database.MediaQuery) (MediaStats, bool)
GetCachedStats returns cached statistics for the given media query, if available. Returns the stats and true if found, or empty stats and false if not cached.
func (*MediaDB) GetIndexingStatus ¶ added in v2.7.0
func (*MediaDB) GetIndexingSystems ¶ added in v2.7.0
func (*MediaDB) GetLastIndexedSystem ¶ added in v2.7.0
func (*MediaDB) GetLaunchCommandForMedia ¶ added in v2.7.0
func (db *MediaDB) GetLaunchCommandForMedia(ctx context.Context, systemID, path string) (string, error)
GetLaunchCommandForMedia generates a title-based launch command for the given media.
func (*MediaDB) GetMaxMediaID ¶ added in v2.7.0
func (*MediaDB) GetMaxMediaTagID ¶ added in v2.7.0
func (*MediaDB) GetMaxSystemID ¶ added in v2.7.0
GetMax*ID methods for resume functionality
func (*MediaDB) GetMaxTagID ¶ added in v2.7.0
func (*MediaDB) GetMaxTagTypeID ¶ added in v2.7.0
func (*MediaDB) GetMaxTitleID ¶ added in v2.7.0
func (*MediaDB) GetMediaByDBID ¶ added in v2.7.0
func (db *MediaDB) GetMediaByDBID(ctx context.Context, mediaDBID int64) (database.SearchResultWithCursor, error)
GetMediaByDBID retrieves a single SearchResultWithCursor by Media DBID. This is used to reconstruct the full result from a cached Media DBID.
func (*MediaDB) GetMediaBySystemID ¶ added in v2.9.1
func (db *MediaDB) GetMediaBySystemID(systemID string) ([]database.MediaWithFullPath, error)
GetMediaBySystemID retrieves all media for a specific system with their associated title and system information. This is used for lazy loading during resume to avoid loading ALL media upfront.
func (*MediaDB) GetMediaTagsBySystemID ¶ added in v2.11.0
func (db *MediaDB) GetMediaTagsBySystemID(systemID string) ([]database.MediaTagLink, error)
GetMediaTagsBySystemID retrieves all media-tag links for a specific system.
func (*MediaDB) GetMediaWithFullPath ¶ added in v2.7.0
func (db *MediaDB) GetMediaWithFullPath() ([]database.MediaWithFullPath, error)
GetMediaWithFullPath retrieves all media with their associated title and system information using JOIN queries. This eliminates the need for nested loops to match media with titles and systems.
func (*MediaDB) GetMediaWithFullPathExcluding ¶ added in v2.7.0
func (db *MediaDB) GetMediaWithFullPathExcluding(excludeSystemIDs []string) ([]database.MediaWithFullPath, error)
GetMediaWithFullPathExcluding retrieves all media with their associated title and system information, excluding those belonging to systems in the excludeSystemIDs list. This is optimized for selective indexing to avoid loading data for systems being reindexed.
func (*MediaDB) GetOptimizationStatus ¶ added in v2.7.0
func (*MediaDB) GetOptimizationStep ¶ added in v2.7.0
func (*MediaDB) GetSystemTagsCached ¶ added in v2.7.0
func (db *MediaDB) GetSystemTagsCached(ctx context.Context, systems []systemdefs.System) ([]database.TagInfo, error)
GetSystemTagsCached retrieves tags for specific systems using the cache table Falls back to the optimized subquery approach if cache is empty
func (*MediaDB) GetSystemsExcluding ¶ added in v2.7.0
GetSystemsExcluding retrieves all systems except those in the excludeSystemIDs list. This is optimized for selective indexing to avoid loading data for systems being reindexed.
func (*MediaDB) GetTitlesBySystemID ¶ added in v2.9.1
func (db *MediaDB) GetTitlesBySystemID(systemID string) ([]database.TitleWithSystem, error)
GetTitlesBySystemID retrieves all media titles for a specific system with their associated system information. This is used for lazy loading during resume to avoid loading ALL titles upfront.
func (*MediaDB) GetTitlesWithPreFilter ¶ added in v2.7.0
func (db *MediaDB) GetTitlesWithPreFilter( ctx context.Context, systemID string, minLength, maxLength, minWordCount, maxWordCount int, ) ([]database.MediaTitle, error)
GetTitlesWithPreFilter retrieves media titles filtered by slug length and word count ranges. This dramatically reduces the candidate set for fuzzy matching by using indexed pre-filter columns. Uses the composite index idx_media_prefilter (SlugLength, SlugWordCount) for efficient range queries.
func (*MediaDB) GetTitlesWithSystems ¶ added in v2.7.0
func (db *MediaDB) GetTitlesWithSystems() ([]database.TitleWithSystem, error)
GetTitlesWithSystems retrieves all media titles with their associated system IDs using a JOIN query. This is more efficient than fetching titles and systems separately and matching them in application code.
func (*MediaDB) GetTitlesWithSystemsExcluding ¶ added in v2.7.0
func (db *MediaDB) GetTitlesWithSystemsExcluding(excludeSystemIDs []string) ([]database.TitleWithSystem, error)
GetTitlesWithSystemsExcluding retrieves all media titles with their associated system IDs, excluding those belonging to systems in the excludeSystemIDs list. This is optimized for selective indexing to avoid loading data for systems being reindexed.
func (*MediaDB) GetTotalMediaCount ¶ added in v2.7.0
GetTotalMediaCount returns the total number of media entries in the database.
func (*MediaDB) GetZapScriptTagsBySystemAndPath ¶ added in v2.10.0
func (db *MediaDB) GetZapScriptTagsBySystemAndPath( ctx context.Context, systemID, path string, ) ([]database.TagInfo, error)
GetZapScriptTagsBySystemAndPath retrieves disambiguating tags for a media item. A tag is disambiguating when sibling media entries (same title) have different values for that tag type (e.g., 2-player vs 4-player variants of the same game). Returns only the target media's tags for tag types that differ across siblings. Returns empty slice if no disambiguating tags exist or media not found.
func (*MediaDB) IndexedSystems ¶
IndexedSystems returns all systems indexed in the media database.
func (*MediaDB) InsertMedia ¶
func (*MediaDB) InsertMediaTag ¶
func (*MediaDB) InsertMediaTitle ¶
func (db *MediaDB) InsertMediaTitle(row *database.MediaTitle) (database.MediaTitle, error)
func (*MediaDB) InsertSystem ¶
func (*MediaDB) InsertTagType ¶
InsertTagType inserts a new TagType into the database.
func (*MediaDB) InvalidateCountCache ¶ added in v2.7.0
InvalidateCountCache clears all cached media counts. This should be called after any operation that changes the media database content.
func (*MediaDB) InvalidateSlugCache ¶ added in v2.7.0
InvalidateSlugCache clears all cached slug resolutions. This should be called after any operation that changes the media database content.
func (*MediaDB) InvalidateSlugCacheForSystems ¶ added in v2.7.0
InvalidateSlugCacheForSystems clears cached slug resolutions for specific systems. This is used during selective system reindexing to avoid invalidating the entire cache.
func (*MediaDB) InvalidateSystemTagsCache ¶ added in v2.7.0
func (db *MediaDB) InvalidateSystemTagsCache(ctx context.Context, systems []systemdefs.System) error
InvalidateSystemTagsCache removes cache entries for specific systems Useful for incremental cache updates when only certain systems change If no systems are provided, this is a no-op and returns success.
func (*MediaDB) PopulateBrowseCache ¶ added in v2.10.0
PopulateBrowseCache rebuilds the BrowseCache table from the current Media data.
func (*MediaDB) PopulateSystemTagsCache ¶ added in v2.7.0
PopulateSystemTagsCache rebuilds the cache table for fast tag lookups by system This should be called after media indexing completes
func (*MediaDB) PopulateSystemTagsCacheForSystems ¶ added in v2.7.0
func (db *MediaDB) PopulateSystemTagsCacheForSystems(ctx context.Context, systems []systemdefs.System) error
PopulateSystemTagsCacheForSystems rebuilds cache for specific systems only Used for incremental cache updates after individual system changes
func (*MediaDB) RandomGame ¶
func (db *MediaDB) RandomGame(systems []systemdefs.System) (database.SearchResult, error)
RandomGame returns a random game from specified systems.
func (*MediaDB) RandomGameWithQuery ¶ added in v2.7.0
func (db *MediaDB) RandomGameWithQuery(query *database.MediaQuery) (database.SearchResult, error)
RandomGameWithQuery returns a random game matching the specified MediaQuery.
func (*MediaDB) RebuildSlugSearchCache ¶ added in v2.10.0
RebuildSlugSearchCache builds or rebuilds the in-memory slug search cache.
func (*MediaDB) RebuildTagCache ¶ added in v2.10.0
RebuildTagCache builds or rebuilds the in-memory tag cache from the SystemTagsCache SQL table. Should be called after media indexing completes.
func (*MediaDB) RefreshSlugSearchCacheForSystems ¶ added in v2.11.0
func (*MediaDB) ResetMissingFlags ¶ added in v2.11.0
func (*MediaDB) RollbackTransaction ¶
RollbackTransaction rolls back the current transaction and cleans up resources
func (*MediaDB) RunBackgroundOptimization ¶ added in v2.7.0
func (db *MediaDB) RunBackgroundOptimization(statusCallback func(optimizing bool), pauser *syncutil.Pauser)
RunBackgroundOptimization performs database optimization operations in the background. This includes creating indexes, running ANALYZE, and vacuuming the database. It can be safely interrupted and resumed later.
func (*MediaDB) SearchMediaBySecondarySlug ¶ added in v2.7.0
func (*MediaDB) SearchMediaBySlug ¶ added in v2.7.0
func (*MediaDB) SearchMediaBySlugIn ¶ added in v2.7.0
func (db *MediaDB) SearchMediaBySlugIn( ctx context.Context, systemID string, slugList []string, tags []zapscript.TagFilter, ) ([]database.SearchResultWithCursor, error)
SearchMediaBySlugIn searches for media items matching any of the provided slugs using an IN clause. This is optimized for searching multiple slug candidates in a single query.
func (*MediaDB) SearchMediaBySlugPrefix ¶ added in v2.7.0
func (*MediaDB) SearchMediaPathExact ¶
func (db *MediaDB) SearchMediaPathExact(systems []systemdefs.System, query string) ([]database.SearchResult, error)
SearchMediaPathExact returns indexed names matching an exact query (case-insensitive).
func (*MediaDB) SearchMediaPathGlob ¶
func (db *MediaDB) SearchMediaPathGlob(systems []systemdefs.System, query string) ([]database.SearchResult, error)
func (*MediaDB) SearchMediaWithFilters ¶ added in v2.7.0
func (db *MediaDB) SearchMediaWithFilters( ctx context.Context, filters *database.SearchFilters, ) ([]database.SearchResultWithCursor, error)
func (*MediaDB) SetCachedSlugResolution ¶ added in v2.7.0
func (db *MediaDB) SetCachedSlugResolution( ctx context.Context, systemID, slug string, tagFilters []zapscript.TagFilter, mediaDBID int64, strategy string, ) error
SetCachedSlugResolution stores a successful slug resolution in the cache.
func (*MediaDB) SetCachedStats ¶ added in v2.7.0
func (db *MediaDB) SetCachedStats(ctx context.Context, query *database.MediaQuery, stats MediaStats) error
SetCachedStats stores statistics for the given media query in the cache.
func (*MediaDB) SetDBPathForTesting ¶ added in v2.7.0
SetDBPathForTesting explicitly sets the DB path so test memory DBs can reload.
func (*MediaDB) SetIndexingCacheSize ¶ added in v2.10.0
SetIndexingCacheSize temporarily increases SQLite cache_size for bulk indexing. Call with enable=true before indexing starts, and enable=false after it completes. When enabled, sets 32MB cache (vs default 8MB) to reduce page eviction during heavy insert workloads with non-sequential index keys.
func (*MediaDB) SetIndexingStatus ¶ added in v2.7.0
func (*MediaDB) SetIndexingSystems ¶ added in v2.7.0
func (*MediaDB) SetLastIndexedSystem ¶ added in v2.7.0
func (*MediaDB) SetOptimizationStatus ¶ added in v2.7.0
func (*MediaDB) SetOptimizationStep ¶ added in v2.7.0
func (*MediaDB) SetSQLForTesting ¶
func (db *MediaDB) SetSQLForTesting(ctx context.Context, sqlDB *sql.DB, platform platforms.Platform) error
SetSQLForTesting allows injection of a sql.DB instance for testing purposes. This method should only be used in tests to set up in-memory databases.
func (*MediaDB) SystemIndexed ¶
func (db *MediaDB) SystemIndexed(system *systemdefs.System) bool
SystemIndexed returns true if a specific system is indexed in the media database.
func (*MediaDB) TrackBackgroundOperation ¶ added in v2.7.1
func (db *MediaDB) TrackBackgroundOperation()
TrackBackgroundOperation increments the background operations counter. Call BackgroundOperationDone when the operation completes. This allows external code (like the indexing goroutine) to be tracked.
func (*MediaDB) TruncateSystems ¶ added in v2.7.0
func (*MediaDB) UnsafeGetSQLDb ¶
func (*MediaDB) UpdateLastGenerated ¶
func (*MediaDB) UpdateMediaTitle ¶ added in v2.11.0
func (*MediaDB) WALCheckpoint ¶ added in v2.10.0
WALCheckpoint forces a WAL checkpoint to flush pending writes to the main database file.
func (*MediaDB) WaitForBackgroundOperations ¶ added in v2.7.0
func (db *MediaDB) WaitForBackgroundOperations()
WaitForBackgroundOperations waits for all background operations to complete. This should be called before closing the database to ensure clean shutdown.
type MediaStats ¶ added in v2.7.0
MediaStats represents cached statistics for a media query
type PreFilterQuery ¶ added in v2.7.0
PreFilterQuery represents pre-filter parameters for efficient fuzzy matching candidate reduction.
type SlugMetadata ¶ added in v2.7.0
SlugMetadata contains pre-filter columns computed from a slug. This metadata enables efficient fuzzy matching by reducing the candidate set before applying expensive string similarity algorithms.
func GenerateSlugMetadataFromTokens ¶ added in v2.10.0
func GenerateSlugMetadataFromTokens( mediaType slugs.MediaType, input string, slug string, tokens []string, ) SlugMetadata
GenerateSlugMetadataFromTokens computes slug metadata from a pre-computed slug and tokens. This avoids redundant slugification when the slug and tokens have already been computed (e.g., during GetPathFragments). The secondary slug still requires slugification since it is derived from the original title, not the pre-computed slug.
func GenerateSlugWithMetadata ¶ added in v2.7.0
func GenerateSlugWithMetadata(mediaType slugs.MediaType, input string) SlugMetadata
GenerateSlugWithMetadata normalizes input and computes all prefilter metadata. CRITICAL: Uses SlugifyWithTokens to ensure metadata matches the actual slug.
The metadata is computed from the EXACT tokens extracted during the 14-stage normalization pipeline, not from re-tokenization. This ensures consistency between the slug and its metadata.
The mediaType parameter should match the MediaType of the system being indexed, ensuring consistent slugification between indexing and resolution.
If the title contains a secondary title separator (: or -), the secondary slug is also extracted and stored separately to enable efficient secondary title matching.
Example:
metadata := GenerateSlugWithMetadata(slugs.MediaTypeGame, "The Legend of Zelda: Ocarina of Time") metadata.Slug → "legendofzeldaocarinaoftime" metadata.SlugLength → 26 (character count) metadata.SlugWordCount → 6 (token count: legend, of, zelda, ocarina, of, time) metadata.SecondarySlug → "ocarinaoftime" (secondary title after colon)
type SlugSearchCache ¶ added in v2.10.0
type SlugSearchCache struct {
// contains filtered or unexported fields
}
SlugSearchCache holds all slug data in memory for fast substring matching. Entries are sorted by systemDBID for contiguous system-filtered scans. A trigram inverted index accelerates unfiltered substring searches.
func (*SlugSearchCache) CanServeSystems ¶ added in v2.11.0
func (c *SlugSearchCache) CanServeSystems(systemIDs []string) bool
func (*SlugSearchCache) ExactSecondarySlugMatch ¶ added in v2.10.0
func (c *SlugSearchCache) ExactSecondarySlugMatch(systemDBIDs []int64, secSlug []byte) []int64
ExactSecondarySlugMatch returns title DBIDs where the secondary slug exactly matches the given bytes.
func (*SlugSearchCache) ExactSlugMatch ¶ added in v2.10.0
func (c *SlugSearchCache) ExactSlugMatch(systemDBIDs []int64, slug []byte) []int64
ExactSlugMatch returns title DBIDs where the slug exactly matches the given bytes.
func (*SlugSearchCache) ExactSlugMatchAny ¶ added in v2.10.0
func (c *SlugSearchCache) ExactSlugMatchAny(systemDBIDs []int64, slugList [][]byte) []int64
ExactSlugMatchAny returns title DBIDs where the slug exactly matches any of the given slugs.
func (*SlugSearchCache) PrefixSlugMatch ¶ added in v2.10.0
func (c *SlugSearchCache) PrefixSlugMatch(systemDBIDs []int64, prefix []byte) []int64
PrefixSlugMatch returns title DBIDs where the slug starts with the given prefix.
func (*SlugSearchCache) RandomEntry ¶ added in v2.10.0
func (c *SlugSearchCache) RandomEntry(systemDBIDs []int64) (int64, bool)
RandomEntry picks a random title DBID from entries matching the system filter.
func (*SlugSearchCache) ResolveSystemDBIDs ¶ added in v2.10.0
func (c *SlugSearchCache) ResolveSystemDBIDs(systemIDs []string) []int64
ResolveSystemDBIDs converts system ID strings to database IDs using the cache's lookup map.
func (*SlugSearchCache) Search ¶ added in v2.10.0
func (c *SlugSearchCache) Search(systemDBIDs []int64, variantGroups [][][]byte) []int64
Search finds all title DBIDs matching the given system filter and variant groups. systemDBIDs is the system filter (empty = no filter). variantGroups is AND-of-ORs: each group is a set of byte variants for one query word (OR'd together), and all groups must match (AND'd together).
func (*SlugSearchCache) Size ¶ added in v2.10.0
func (c *SlugSearchCache) Size() int
Size returns the approximate memory footprint of the cache in bytes.
func (*SlugSearchCache) TrigramIndexSize ¶ added in v2.10.0
func (c *SlugSearchCache) TrigramIndexSize() int
TrigramIndexSize returns the memory footprint of the trigram index in bytes.
Source Files
¶
- batch_inserter.go
- letterfilter.go
- mediadb.go
- slug_cache.go
- slug_metadata.go
- slug_search_cache.go
- sql_browse.go
- sql_browse_cache.go
- sql_cache.go
- sql_config.go
- sql_helpers.go
- sql_maintenance.go
- sql_media.go
- sql_media_tags.go
- sql_media_titles.go
- sql_migrations.go
- sql_prefetch.go
- sql_search.go
- sql_systems.go
- sql_tags.go
- tag_cache.go
- tagfilter.go