storage

package
v0.0.0-...-7f7e7f6 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = StoreOptions{
	InitialSize:  32 << 20,
	MaxSize:      512 << 20,
	SyncInterval: time.Minute,
	Debug:        false,
}

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Value     interface{} `yaml:"value"`
	Timestamp int64       `yaml:"timestamp,omitempty"`
	TTL       int64       `yaml:"ttl,omitempty"`
}

Entry represents a single value in the store with metadata

type FastYAMLEncoder

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

FastYAMLEncoder provides optimized YAML encoding with buffer pooling

func NewFastYAMLEncoder

func NewFastYAMLEncoder() *FastYAMLEncoder

NewFastYAMLEncoder creates a new encoder with initialized buffer pool

func (*FastYAMLEncoder) Decode

func (f *FastYAMLEncoder) Decode(data []byte, v interface{}) error

Decode performs YAML decoding with validation

func (*FastYAMLEncoder) Encode

func (f *FastYAMLEncoder) Encode(v interface{}) ([]byte, error)

Encode performs YAML encoding with buffer reuse

type IndexManager

type IndexManager struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

IndexManager handles multiple index types

func NewIndexManager

func NewIndexManager() *IndexManager

NewIndexManager creates a new index manager

func (*IndexManager) AddIndex

func (im *IndexManager) AddIndex(field string, indexType string) error

AddIndex creates a new index for the specified field

func (*IndexManager) Remove

func (im *IndexManager) Remove(key string)

Remove removes a key from all indexes

func (*IndexManager) RemoveIndex

func (im *IndexManager) RemoveIndex(field string, indexType string) error

RemoveIndex removes an index of the specified type

func (*IndexManager) Search

func (im *IndexManager) Search(query map[string]interface{}) ([]string, error)

Search performs a search across all relevant indexes

func (*IndexManager) Update

func (im *IndexManager) Update(key string, value interface{}) error

Update updates all indexes for a given key-value pair

type SearchQuery

type SearchQuery struct {
	Text       string                 `json:"text,omitempty"`
	Vector     []float32              `json:"vector,omitempty"`
	Filters    map[string]interface{} `json:"filters,omitempty"`
	MaxResults int                    `json:"max_results,omitempty"`
	MinScore   float64                `json:"min_score,omitempty"`
}

SearchQuery represents a combined search query

type SearchResult

type SearchResult struct {
	Key       string      `json:"key"`
	Value     interface{} `json:"value"`
	TextScore float64     `json:"text_score,omitempty"`
	VecScore  float32     `json:"vector_score,omitempty"`
	Combined  float64     `json:"combined_score"`
}

SearchResult represents a combined search result

type Store

type Store struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Store represents an enhanced memory-mapped key-value store

func NewStore

func NewStore(filepath string, opts StoreOptions) (*Store, error)

NewStore creates a new memory-mapped store with the given options

func (*Store) AddToIndex

func (s *Store) AddToIndex(field string, key string, value interface{}) error

AddToIndex adds or updates a value in the specified index

func (*Store) Close

func (s *Store) Close() error

Close ensures all data is synced and resources are released

func (*Store) CreateIndex

func (s *Store) CreateIndex(field string, indexType string) error

CreateIndex creates a new index of the specified type

func (*Store) Delete

func (s *Store) Delete(key string)

func (*Store) Get

func (s *Store) Get(key string) (*Entry, bool)

func (*Store) GetStats

func (s *Store) GetStats() StoreStats

GetStats returns enhanced statistics

func (*Store) RemoveIndex

func (s *Store) RemoveIndex(field string, indexType string) error

RemoveIndex removes an index of the specified type

func (*Store) Search

func (s *Store) Search(query SearchQuery) ([]SearchResult, error)

Search performs a combined search across all indexes

func (*Store) Set

func (s *Store) Set(key string, value interface{}) error

func (*Store) SetWithTTL

func (s *Store) SetWithTTL(key string, value interface{}, ttl time.Duration) error

func (*Store) Sync

func (s *Store) Sync() error

Sync forces a sync to disk

type StoreOptions

type StoreOptions struct {
	InitialSize  int64
	MaxSize      int64
	SyncInterval time.Duration
	Debug        bool
}

StoreOptions configures the store initialization

type StoreStats

type StoreStats struct {
	sync.Mutex
	// Basic Operations
	Reads        uint64    `json:"reads" yaml:"reads"`
	Writes       uint64    `json:"writes" yaml:"writes"`
	Deletes      uint64    `json:"deletes" yaml:"deletes"`
	SyncCount    uint64    `json:"sync_count" yaml:"sync_count"`
	LastSyncTime time.Time `json:"last_sync_time" yaml:"last_sync_time"`

	// Storage Stats
	DataSize     int64  `json:"data_size" yaml:"data_size"`         // Current size of YAML data
	FileSize     int64  `json:"file_size" yaml:"file_size"`         // Total size of mmap file
	EntryCount   uint64 `json:"entry_count" yaml:"entry_count"`     // Number of active entries
	ExpiredCount uint64 `json:"expired_count" yaml:"expired_count"` // Number of expired entries

	// Index Stats
	IndexStats struct {
		TextIndexes struct {
			Count      int `json:"count" yaml:"count"`
			EntryCount int `json:"entry_count" yaml:"entry_count"`
		} `json:"text_indexes" yaml:"text_indexes"`
		VectorIndexes struct {
			Count      int `json:"count" yaml:"count"`
			EntryCount int `json:"entry_count" yaml:"entry_count"`
		} `json:"vector_indexes" yaml:"vector_indexes"`
		BTreeIndexes struct {
			Count      int `json:"count" yaml:"count"`
			EntryCount int `json:"entry_count" yaml:"entry_count"`
		} `json:"btree_indexes" yaml:"btree_indexes"`
	} `json:"index_stats" yaml:"index_stats"`

	// Performance Stats
	PerformanceStats struct {
		AvgReadLatency  float64   `json:"avg_read_latency" yaml:"avg_read_latency"`   // in milliseconds
		AvgWriteLatency float64   `json:"avg_write_latency" yaml:"avg_write_latency"` // in milliseconds
		AvgSyncLatency  float64   `json:"avg_sync_latency" yaml:"avg_sync_latency"`   // in milliseconds
		LastGC          time.Time `json:"last_gc" yaml:"last_gc"`                     // Last time expired entries were cleaned
	} `json:"performance_stats" yaml:"performance_stats"`
}

StoreStats tracks operational statistics for monitoring.

type TextSearchResult

type TextSearchResult struct {
	Key   string
	Score float64
	Text  string
}

TextSearchResult represents a single text search result with score

type TrigramIndex

type TrigramIndex struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

TrigramIndex provides text search using trigram-based indexing

func NewTrigramIndex

func NewTrigramIndex() *TrigramIndex

NewTrigramIndex creates a new trigram-based text index

func (*TrigramIndex) FuzzySearch

func (ti *TrigramIndex) FuzzySearch(query string, minScore float64, maxResults int) []TextSearchResult

FuzzySearch performs fuzzy text search with configurable parameters

func (*TrigramIndex) Remove

func (ti *TrigramIndex) Remove(key string)

Remove deletes a document from the index

func (*TrigramIndex) Search

func (ti *TrigramIndex) Search(query string, maxResults int) []TextSearchResult

Search performs a fuzzy text search using trigrams

func (*TrigramIndex) Update

func (ti *TrigramIndex) Update(key string, text string)

Update adds or updates a document in the index

type VectorIndex

type VectorIndex struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

VectorIndex provides vector similarity search capabilities

func NewVectorIndex

func NewVectorIndex(dimensions int) *VectorIndex

NewVectorIndex creates a new vector index with specified dimensions

func (*VectorIndex) BatchSearch

func (vi *VectorIndex) BatchSearch(queries [][]float32, k int) ([][]VectorSearchResult, error)

BatchSearch performs vector search with multiple query vectors

func (*VectorIndex) Remove

func (vi *VectorIndex) Remove(key string)

Remove deletes a vector from the index

func (*VectorIndex) Search

func (vi *VectorIndex) Search(query []float32, k int) ([]VectorSearchResult, error)

Search performs approximate nearest neighbor search

func (*VectorIndex) Update

func (vi *VectorIndex) Update(key string, vector []float32) error

Update adds or updates a vector for a given key

type VectorSearchResult

type VectorSearchResult struct {
	Key   string
	Score float32
}

VectorSearchResult represents a single search result with score

Jump to

Keyboard shortcuts

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