Documentation
¶
Overview ¶
Package cache provides binary format serialization for fuzzy cache data structures.
Index ¶
- Constants
- func DaysFromTime(t time.Time) uint32
- func IsValidCacheFile(filename string) bool
- func TimeFromDays(days uint32) time.Time
- type BinaryHeader
- type CacheConfig
- type CacheHeader
- type CacheManager
- func (cm *CacheManager) GetCache(ctx context.Context) (*DmenuFuzzyCache, error)
- func (cm *CacheManager) GetTopEntries(ctx context.Context) (*FuzzyResult, error)
- func (cm *CacheManager) InvalidateAndRefresh(ctx context.Context)
- func (cm *CacheManager) OnApplicationExit(ctx context.Context)
- func (cm *CacheManager) Search(ctx context.Context, query string) (*FuzzyResult, error)
- func (cm *CacheManager) Stats() CacheStats
- type CacheStats
- type CertDBOperations
- type CertValidationsCache
- type CompactEntry
- type CosmeticRule
- type DmenuFuzzyCache
- type FavoritesCache
- type FavoritesDBOperations
- type FilterCache
- type FilterData
- type FilterRule
- type FuzzyMatch
- type FuzzyResult
- type FuzzySearcher
- type HistoryQuerier
- type MatchType
- type NetworkRule
- type Trie
- type TrieNode
- type ZoomCache
- type ZoomDBOperations
Constants ¶
const ( CacheMagic = 0x44554d42 // "DUMB" in little-endian CacheVersion = 1 HeaderSize = 28 // Size of binary header )
Binary format constants for cache file structure
Variables ¶
This section is empty.
Functions ¶
func DaysFromTime ¶
DaysFromTime converts a time.Time to days since Unix epoch.
func IsValidCacheFile ¶
IsValidCacheFile checks if a file is a valid cache file without fully loading it.
func TimeFromDays ¶
TimeFromDays converts days since Unix epoch back to time.Time.
Types ¶
type BinaryHeader ¶
type BinaryHeader struct {
Magic uint32 // Magic number for format identification
Version uint32 // Cache format version
EntryCount uint32 // Number of entries in cache
IndexOffset uint64 // Offset to trigram index section
LastModified int64 // Unix timestamp of last modification
}
BinaryHeader represents the cache file header.
type CacheConfig ¶
type CacheConfig struct {
// File paths
CacheFile string // Path to binary cache file
// Performance tuning
MaxEntries uint32 // Maximum entries to cache
TrigramThreshold uint32 // Minimum trigram matches required
ScoreThreshold float64 // Minimum score for results
MaxResults int // Maximum results to return
// Scoring weights
URLWeight float64 // Weight for URL matches
TitleWeight float64 // Weight for title matches
RecencyWeight float64 // Weight for recency
VisitWeight float64 // Weight for visit count
// Cache behavior
TTL time.Duration // Cache time-to-live
WarmupEnabled bool // Enable background cache warming
}
CacheConfig holds configuration for the fuzzy cache.
func DefaultCacheConfig ¶
func DefaultCacheConfig() *CacheConfig
DefaultCacheConfig returns sensible default configuration.
func (*CacheConfig) GetStateDir ¶
func (c *CacheConfig) GetStateDir() (string, error)
GetStateDir returns the state directory using the config package.
type CacheHeader ¶ added in v0.7.0
type CacheHeader struct {
Magic uint64 // Magic bytes for file identification
Version uint32 // Cache format version
DataSize uint64 // Size of data section
RuleCount uint32 // Number of filter rules
Created int64 // Unix timestamp when cache was created
Checksum uint64 // CRC64 checksum of data section
}
CacheHeader represents the binary cache file header
type CacheManager ¶
type CacheManager struct {
// contains filtered or unexported fields
}
CacheManager handles the lifecycle of the fuzzy cache with smart invalidation.
func NewCacheManager ¶
func NewCacheManager(queries HistoryQuerier, config *CacheConfig) *CacheManager
NewCacheManager creates a new cache manager.
func (*CacheManager) GetCache ¶
func (cm *CacheManager) GetCache(ctx context.Context) (*DmenuFuzzyCache, error)
GetCache returns the current cache, loading from filesystem or DB as needed.
func (*CacheManager) GetTopEntries ¶
func (cm *CacheManager) GetTopEntries(ctx context.Context) (*FuzzyResult, error)
GetTopEntries returns the top entries without a search query.
func (*CacheManager) InvalidateAndRefresh ¶
func (cm *CacheManager) InvalidateAndRefresh(ctx context.Context)
InvalidateAndRefresh forces cache invalidation and refresh. This should be called when we know the DB has changed (e.g., after adding history).
func (*CacheManager) OnApplicationExit ¶
func (cm *CacheManager) OnApplicationExit(ctx context.Context)
OnApplicationExit should be called when the application is about to exit. This triggers a background cache refresh for the next startup.
func (*CacheManager) Search ¶
func (cm *CacheManager) Search(ctx context.Context, query string) (*FuzzyResult, error)
Search performs a fuzzy search using the cached data.
func (*CacheManager) Stats ¶
func (cm *CacheManager) Stats() CacheStats
Stats returns cache statistics.
type CacheStats ¶
type CacheStats struct {
EntryCount int // Number of cached entries
TrigramCount int // Number of trigrams in index
FileSize int64 // Size of cache file on disk
LastModified time.Time // When cache was last built
FileModTime time.Time // When cache file was last modified
}
CacheStats provides information about the cache state.
func (CacheStats) String ¶
func (cs CacheStats) String() string
String returns a string representation of cache stats.
type CertDBOperations ¶ added in v0.13.0
type CertDBOperations struct {
// contains filtered or unexported fields
}
CertDBOperations implements DatabaseOperations for certificate validations cache. Handles loading, persisting, and deleting certificate validations from the database.
func NewCertDBOperations ¶ added in v0.13.0
func NewCertDBOperations(queries db.DatabaseQuerier) *CertDBOperations
NewCertDBOperations creates a new CertDBOperations instance.
func (*CertDBOperations) Delete ¶ added in v0.13.0
func (c *CertDBOperations) Delete(ctx context.Context, hostname string) error
Delete removes a certificate validation from the database.
func (*CertDBOperations) LoadAll ¶ added in v0.13.0
func (c *CertDBOperations) LoadAll(ctx context.Context) (map[string]db.CertificateValidation, error)
LoadAll loads all certificate validations from the database. Returns a map of hostname -> CertificateValidation. Only loads non-expired validations.
func (*CertDBOperations) Persist ¶ added in v0.13.0
func (c *CertDBOperations) Persist(ctx context.Context, hostname string, validation db.CertificateValidation) error
Persist saves a certificate validation to the database. Uses INSERT OR REPLACE to handle both new and existing entries.
type CertValidationsCache ¶ added in v0.13.0
type CertValidationsCache struct {
*generic.GenericCache[string, db.CertificateValidation]
}
CertValidationsCache is a specialized cache for certificate validations. It wraps GenericCache with hostname-specific helper methods. Key insight: Caches hostname -> validation decision to avoid DB queries on HTTPS cert errors.
func NewCertValidationsCache ¶ added in v0.13.0
func NewCertValidationsCache(queries db.DatabaseQuerier) *CertValidationsCache
NewCertValidationsCache creates a new certificate validations cache.
type CompactEntry ¶
type CompactEntry struct {
URL string // Original URL
Title string // Page title
FaviconURL string // URL to favicon for this entry
VisitCount uint16 // Max 65535 visits (sufficient for most use cases)
LastVisit uint32 // Days since Unix epoch (saves 4 bytes vs time.Time)
Score uint16 // Pre-computed base score (0-65535)
}
CompactEntry represents a history entry optimized for memory usage.
func NewCompactEntry ¶
func NewCompactEntry(url, title, faviconURL string, visitCount int64, lastVisit time.Time) CompactEntry
NewCompactEntry creates a CompactEntry from database values.
func (*CompactEntry) Display ¶
func (e *CompactEntry) Display() string
Display returns a formatted string for dmenu display.
type CosmeticRule ¶ added in v0.7.0
CosmeticRule represents a compiled cosmetic filtering rule
type DmenuFuzzyCache ¶
type DmenuFuzzyCache struct {
// contains filtered or unexported fields
}
DmenuFuzzyCache provides high-performance fuzzy search for dmenu history.
func (*DmenuFuzzyCache) LoadFromBinary ¶
func (c *DmenuFuzzyCache) LoadFromBinary(filename string) error
LoadFromBinary loads cache from a binary file using memory mapping.
func (*DmenuFuzzyCache) SaveToBinary ¶
func (c *DmenuFuzzyCache) SaveToBinary(filename string) error
SaveToBinary writes the cache to a binary file using memory mapping for performance.
func (*DmenuFuzzyCache) Search ¶
func (c *DmenuFuzzyCache) Search(query string, config *CacheConfig) *FuzzyResult
Search performs a fuzzy search on the cache and returns ranked results.
type FavoritesCache ¶ added in v0.14.0
type FavoritesCache struct {
*generic.GenericCache[string, db.Favorite]
}
FavoritesCache is a specialized cache for favorites. It wraps GenericCache with favorites-specific helper methods.
func NewFavoritesCache ¶ added in v0.14.0
func NewFavoritesCache(queries db.DatabaseQuerier) *FavoritesCache
NewFavoritesCache creates a new favorites cache.
func (*FavoritesCache) GetAll ¶ added in v0.14.0
func (f *FavoritesCache) GetAll() []db.Favorite
GetAll returns all favorites as a slice, ordered by position. This is a convenience method for the frontend.
type FavoritesDBOperations ¶ added in v0.14.0
type FavoritesDBOperations struct {
// contains filtered or unexported fields
}
FavoritesDBOperations implements DatabaseOperations for favorites cache. Handles loading, persisting, and deleting favorites from the database.
func NewFavoritesDBOperations ¶ added in v0.14.0
func NewFavoritesDBOperations(queries db.DatabaseQuerier) *FavoritesDBOperations
NewFavoritesDBOperations creates a new FavoritesDBOperations instance.
func (*FavoritesDBOperations) Delete ¶ added in v0.14.0
func (f *FavoritesDBOperations) Delete(ctx context.Context, url string) error
Delete removes a favorite from the database.
type FilterCache ¶ added in v0.7.0
type FilterCache struct {
// contains filtered or unexported fields
}
FilterCache provides high-performance binary filter caching with memory mapping
func NewFilterCache ¶ added in v0.7.0
func NewFilterCache(path string) *FilterCache
NewFilterCache creates a new filter cache instance
func (*FilterCache) IsValid ¶ added in v0.7.0
func (fc *FilterCache) IsValid() bool
IsValid checks if the cache file exists and has valid format
func (*FilterCache) LoadMapped ¶ added in v0.7.0
func (fc *FilterCache) LoadMapped() (*FilterData, error)
LoadMapped loads the cache file using memory mapping for zero-copy access
func (*FilterCache) Remove ¶ added in v0.7.0
func (fc *FilterCache) Remove() error
Remove deletes the cache file
func (*FilterCache) Size ¶ added in v0.7.0
func (fc *FilterCache) Size() int64
Size returns the size of the cache file in bytes
func (*FilterCache) Unmap ¶ added in v0.7.0
func (fc *FilterCache) Unmap() error
Unmap releases the memory-mapped data
func (*FilterCache) Write ¶ added in v0.7.0
func (fc *FilterCache) Write(data *FilterData) error
Write saves filter data to cache in binary format
type FilterData ¶ added in v0.7.0
type FilterData struct {
Version uint32
Created time.Time
NetworkRules []NetworkRule
CosmeticRules []CosmeticRule
}
FilterData represents the compiled filter data for caching
type FilterRule ¶ added in v0.7.0
type FilterRule struct {
Type uint8 // Rule type (0=network, 1=cosmetic)
Priority uint8 // Rule priority
Action uint8 // Action to take (0=block, 1=allow, etc.)
Flags uint8 // Additional flags
PatternLen uint16 // Length of pattern string
DomainLen uint16 // Length of domain string
ResourceType uint32 // Bitmask of resource types
}
FilterRule represents a compiled filter rule in binary format
type FuzzyMatch ¶
type FuzzyMatch struct {
Entry *CompactEntry // Matched entry
Score float64 // Final fuzzy match score (0.0-1.0)
URLScore float64 // URL similarity score
TitleScore float64 // Title similarity score
RecencyScore float64 // Recency boost score
VisitScore float64 // Visit count boost score
MatchType MatchType // Type of match found
}
FuzzyMatch represents a single fuzzy search match.
type FuzzyResult ¶
type FuzzyResult struct {
Matches []FuzzyMatch // Matching entries
QueryTime time.Duration // Time taken to execute query
}
FuzzyResult contains the results of a fuzzy search query.
type FuzzySearcher ¶
type FuzzySearcher struct {
// contains filtered or unexported fields
}
FuzzySearcher provides high-performance fuzzy search capabilities.
func NewFuzzySearcher ¶
func NewFuzzySearcher(config *CacheConfig) *FuzzySearcher
NewFuzzySearcher creates a new fuzzy searcher with the given config.
type HistoryQuerier ¶
type HistoryQuerier interface {
GetHistory(ctx context.Context, limit int64) ([]db.History, error)
}
HistoryQuerier defines the interface for querying history data.
type MatchType ¶
type MatchType uint8
MatchType indicates how the query matched the entry.
type NetworkRule ¶ added in v0.7.0
type NetworkRule struct {
Pattern string
Domain string
Action uint8 // 0=block, 1=allow
ResourceType uint32 // Bitmask of resource types
Priority uint8
}
NetworkRule represents a compiled network filtering rule
type Trie ¶
type Trie struct {
Root *TrieNode
}
Trie represents a compressed prefix tree for fast prefix matching.
func (*Trie) PrefixSearch ¶
PrefixSearch finds all entries that start with any word in the query.
type TrieNode ¶
type TrieNode struct {
Children map[rune]*TrieNode // Child nodes
Entries []uint32 // Entry IDs that end at this node
IsEnd bool // Marks end of a prefix
}
TrieNode represents a node in the prefix trie.
type ZoomCache ¶ added in v0.13.0
type ZoomCache struct {
*generic.GenericCache[string, float64]
}
ZoomCache is a specialized cache for zoom levels. It wraps GenericCache with domain-specific helper methods.
func NewZoomCache ¶ added in v0.13.0
func NewZoomCache(queries db.DatabaseQuerier) *ZoomCache
NewZoomCache creates a new zoom level cache.
type ZoomDBOperations ¶ added in v0.13.0
type ZoomDBOperations struct {
// contains filtered or unexported fields
}
ZoomDBOperations implements DatabaseOperations for zoom level cache. Handles loading, persisting, and deleting zoom levels from the database.
func NewZoomDBOperations ¶ added in v0.13.0
func NewZoomDBOperations(queries db.DatabaseQuerier) *ZoomDBOperations
NewZoomDBOperations creates a new ZoomDBOperations instance.
func (*ZoomDBOperations) Delete ¶ added in v0.13.0
func (z *ZoomDBOperations) Delete(ctx context.Context, domain string) error
Delete removes a zoom level from the database.