|
| 1 | +# Connector Development Guide |
| 2 | + |
| 3 | +This guide explains how to add a new backend connector under `stores/<backend>`. |
| 4 | + |
| 5 | +## 1) Scope and Contract |
| 6 | + |
| 7 | +Every connector must implement the shared interfaces in `vectordata`: |
| 8 | + |
| 9 | +- `vectordata.VectorStore` |
| 10 | +- `vectordata.Collection` |
| 11 | + |
| 12 | +The connector must preserve shared behavior: |
| 13 | + |
| 14 | +- validate collection specs (`name`, `dimension`, `metric`, `mode`) |
| 15 | +- enforce vector dimension on write and search |
| 16 | +- return `vectordata.ErrNotFound` for missing records |
| 17 | +- return `vectordata.ErrDimensionMismatch`, `vectordata.ErrSchemaMismatch`, and `vectordata.ErrInvalidFilter` where applicable |
| 18 | +- compute score via `vectordata.ScoreFromDistance` |
| 19 | + |
| 20 | +## 2) Recommended Layout |
| 21 | + |
| 22 | +Create a new directory: |
| 23 | + |
| 24 | +- `stores/<backend>/doc.go` |
| 25 | +- `stores/<backend>/store.go` |
| 26 | +- `stores/<backend>/collection.go` |
| 27 | +- `stores/<backend>/schema.go` |
| 28 | +- `stores/<backend>/helpers.go` |
| 29 | +- `stores/<backend>/<backend>_integration_test.go` |
| 30 | + |
| 31 | +Add extra files only when needed (for example filter compiler/evaluator files). |
| 32 | + |
| 33 | +## 3) Store Implementation Checklist |
| 34 | + |
| 35 | +Implement a store type that owns connection/resources and options: |
| 36 | + |
| 37 | +1. Add `StoreOptions` and `DefaultStoreOptions()`. |
| 38 | +2. Add `NewVectorStore(...)` constructor with option normalization. |
| 39 | +3. Implement `EnsureCollection(ctx, spec)`: |
| 40 | + - normalize and validate spec |
| 41 | + - ensure schema/table/metadata structures |
| 42 | + - create or validate collection storage |
| 43 | +4. Implement `Collection(name, dimension, metric)` as a lightweight handle constructor. |
| 44 | + |
| 45 | +## 4) Collection Implementation Checklist |
| 46 | + |
| 47 | +Implement collection operations with strict validation: |
| 48 | + |
| 49 | +1. `Insert` and `Upsert` |
| 50 | + - validate ID and vector dimension |
| 51 | + - normalize nil metadata to empty object |
| 52 | + - use batching/chunking for bulk writes |
| 53 | +2. `Get` and `Delete` |
| 54 | + - return `ErrNotFound` from `Get` when missing |
| 55 | +3. `Count` |
| 56 | + - support nil filter and filter predicates |
| 57 | +4. `SearchByVector` |
| 58 | + - validate `topK > 0` |
| 59 | + - validate query vector dimension |
| 60 | + - apply filter (SQL pushdown or in-process evaluation) |
| 61 | + - apply optional threshold |
| 62 | + - honor projection (`Metadata`, `Content`, `Vector`) |
| 63 | + - return results ordered by best match first |
| 64 | + - compute `Score` from `Distance` |
| 65 | +5. `EnsureIndexes` |
| 66 | + - create backend-specific indexes when supported |
| 67 | + - return explicit error if unsupported options are requested |
| 68 | + |
| 69 | +## 5) Filter Strategy |
| 70 | + |
| 71 | +Choose one strategy: |
| 72 | + |
| 73 | +- SQL pushdown: compile `vectordata.Filter` to parameterized backend SQL |
| 74 | +- In-process: load records and evaluate filter AST in Go |
| 75 | + |
| 76 | +Requirements: |
| 77 | + |
| 78 | +- never interpolate raw values into SQL |
| 79 | +- return `vectordata.ErrInvalidFilter` for invalid AST/input |
| 80 | + |
| 81 | +## 6) Schema Safety Modes |
| 82 | + |
| 83 | +Respect `CollectionSpec.Mode`: |
| 84 | + |
| 85 | +- `EnsureStrict`: fail on mismatches |
| 86 | +- `EnsureAutoMigrate`: add/fix optional schema parts where possible |
| 87 | + |
| 88 | +When mode is unset, use connector defaults (`StrictByDefault` behavior). |
| 89 | + |
| 90 | +## 7) Testing Requirements |
| 91 | + |
| 92 | +Add both unit and integration coverage. |
| 93 | + |
| 94 | +Unit tests: |
| 95 | + |
| 96 | +- spec validation |
| 97 | +- schema mismatch behavior |
| 98 | +- filter behavior (happy path + invalid filters) |
| 99 | +- dimension mismatch and error mapping |
| 100 | +- projection and threshold behavior in search |
| 101 | + |
| 102 | +Integration tests: |
| 103 | + |
| 104 | +- put in `stores/<backend>/<backend>_integration_test.go` |
| 105 | +- use `//go:build integration` |
| 106 | +- start backend with Testcontainers when DSN env var is absent |
| 107 | +- allow DSN override via `<BACKEND>_TEST_DSN` |
| 108 | + |
| 109 | +Run commands: |
| 110 | + |
| 111 | +```bash |
| 112 | +go test ./... |
| 113 | +go test -tags=integration ./stores/<backend> |
| 114 | +``` |
| 115 | + |
| 116 | +## 8) Repository Wiring |
| 117 | + |
| 118 | +When adding a connector, also update: |
| 119 | + |
| 120 | +1. `README.md`: |
| 121 | + - scope |
| 122 | + - requirements |
| 123 | + - project layout |
| 124 | + - integration test notes |
| 125 | +2. `docs/architecture.md` and `docs/stores-implementation.md` |
| 126 | +3. `docs/README.md` |
| 127 | +4. `Makefile` integration targets (if per-backend targets are used) |
| 128 | +5. `docker-compose.yml` only if manual local backend compose is required |
| 129 | +6. `go.mod` and `go.sum` dependencies |
| 130 | + |
| 131 | +## 9) Minimal Skeleton |
| 132 | + |
| 133 | +```go |
| 134 | +package mybackend |
| 135 | + |
| 136 | +import ( |
| 137 | + "context" |
| 138 | + |
| 139 | + "github.com/gabisonia/go-vectorstore/vectordata" |
| 140 | +) |
| 141 | + |
| 142 | +type StoreOptions struct{} |
| 143 | + |
| 144 | +func DefaultStoreOptions() StoreOptions { return StoreOptions{} } |
| 145 | + |
| 146 | +type VectorStore struct{} |
| 147 | + |
| 148 | +func NewVectorStore(opts StoreOptions) (*VectorStore, error) { |
| 149 | + return &VectorStore{}, nil |
| 150 | +} |
| 151 | + |
| 152 | +func (s *VectorStore) EnsureCollection(ctx context.Context, spec vectordata.CollectionSpec) (vectordata.Collection, error) { |
| 153 | + return nil, nil |
| 154 | +} |
| 155 | + |
| 156 | +func (s *VectorStore) Collection(name string, dimension int, metric vectordata.DistanceMetric) vectordata.Collection { |
| 157 | + return nil |
| 158 | +} |
| 159 | +``` |
0 commit comments