|
| 1 | +package engine |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "sync" |
| 7 | + |
| 8 | + "github.com/mudler/localrecall/rag/types" |
| 9 | +) |
| 10 | + |
| 11 | +// MockEngine is a simple in-memory engine for testing. It requires no |
| 12 | +// external dependencies (no LocalAI, no embeddings). |
| 13 | +type MockEngine struct { |
| 14 | + mu sync.Mutex |
| 15 | + docs map[string]types.Result |
| 16 | + index int |
| 17 | +} |
| 18 | + |
| 19 | +func NewMockEngine() *MockEngine { |
| 20 | + return &MockEngine{ |
| 21 | + docs: make(map[string]types.Result), |
| 22 | + index: 1, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +func (m *MockEngine) Store(s string, metadata map[string]string) (Result, error) { |
| 27 | + results, err := m.StoreDocuments([]string{s}, metadata) |
| 28 | + if err != nil { |
| 29 | + return Result{}, err |
| 30 | + } |
| 31 | + return results[0], nil |
| 32 | +} |
| 33 | + |
| 34 | +func (m *MockEngine) StoreDocuments(s []string, metadata map[string]string) ([]Result, error) { |
| 35 | + m.mu.Lock() |
| 36 | + defer m.mu.Unlock() |
| 37 | + |
| 38 | + if len(s) == 0 { |
| 39 | + return nil, fmt.Errorf("empty input") |
| 40 | + } |
| 41 | + |
| 42 | + results := make([]Result, len(s)) |
| 43 | + for i, content := range s { |
| 44 | + id := fmt.Sprintf("%d", m.index) |
| 45 | + // Copy metadata so each doc has its own map |
| 46 | + meta := make(map[string]string, len(metadata)) |
| 47 | + for k, v := range metadata { |
| 48 | + meta[k] = v |
| 49 | + } |
| 50 | + m.docs[id] = types.Result{ |
| 51 | + ID: id, |
| 52 | + Content: content, |
| 53 | + Metadata: meta, |
| 54 | + } |
| 55 | + results[i] = Result{ID: id} |
| 56 | + m.index++ |
| 57 | + } |
| 58 | + return results, nil |
| 59 | +} |
| 60 | + |
| 61 | +func (m *MockEngine) Search(s string, similarEntries int) ([]types.Result, error) { |
| 62 | + m.mu.Lock() |
| 63 | + defer m.mu.Unlock() |
| 64 | + |
| 65 | + var results []types.Result |
| 66 | + for _, doc := range m.docs { |
| 67 | + if strings.Contains(strings.ToLower(doc.Content), strings.ToLower(s)) { |
| 68 | + results = append(results, doc) |
| 69 | + } |
| 70 | + } |
| 71 | + // If no substring match, return all (useful for generic searches) |
| 72 | + if len(results) == 0 { |
| 73 | + for _, doc := range m.docs { |
| 74 | + results = append(results, doc) |
| 75 | + } |
| 76 | + } |
| 77 | + if len(results) > similarEntries { |
| 78 | + results = results[:similarEntries] |
| 79 | + } |
| 80 | + return results, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (m *MockEngine) Delete(where map[string]string, whereDocuments map[string]string, ids ...string) error { |
| 84 | + m.mu.Lock() |
| 85 | + defer m.mu.Unlock() |
| 86 | + |
| 87 | + // Delete by IDs |
| 88 | + if len(ids) > 0 { |
| 89 | + for _, id := range ids { |
| 90 | + delete(m.docs, id) |
| 91 | + } |
| 92 | + return nil |
| 93 | + } |
| 94 | + |
| 95 | + // Delete by metadata where filter |
| 96 | + if len(where) > 0 { |
| 97 | + for id, doc := range m.docs { |
| 98 | + match := true |
| 99 | + for k, v := range where { |
| 100 | + if doc.Metadata[k] != v { |
| 101 | + match = false |
| 102 | + break |
| 103 | + } |
| 104 | + } |
| 105 | + if match { |
| 106 | + delete(m.docs, id) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func (m *MockEngine) GetByID(id string) (types.Result, error) { |
| 115 | + m.mu.Lock() |
| 116 | + defer m.mu.Unlock() |
| 117 | + |
| 118 | + doc, ok := m.docs[id] |
| 119 | + if !ok { |
| 120 | + return types.Result{}, fmt.Errorf("document not found: %s", id) |
| 121 | + } |
| 122 | + return doc, nil |
| 123 | +} |
| 124 | + |
| 125 | +func (m *MockEngine) GetBySource(source string) ([]types.Result, error) { |
| 126 | + m.mu.Lock() |
| 127 | + defer m.mu.Unlock() |
| 128 | + |
| 129 | + var results []types.Result |
| 130 | + for _, doc := range m.docs { |
| 131 | + if doc.Metadata["source"] == source { |
| 132 | + results = append(results, doc) |
| 133 | + } |
| 134 | + } |
| 135 | + return results, nil |
| 136 | +} |
| 137 | + |
| 138 | +func (m *MockEngine) Count() int { |
| 139 | + m.mu.Lock() |
| 140 | + defer m.mu.Unlock() |
| 141 | + |
| 142 | + return len(m.docs) |
| 143 | +} |
| 144 | + |
| 145 | +func (m *MockEngine) Reset() error { |
| 146 | + m.mu.Lock() |
| 147 | + defer m.mu.Unlock() |
| 148 | + |
| 149 | + m.docs = make(map[string]types.Result) |
| 150 | + m.index = 1 |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +func (m *MockEngine) GetEmbeddingDimensions() (int, error) { |
| 155 | + return 384, nil |
| 156 | +} |
0 commit comments