HandleBatchNotification processes a batch of items and indexes them in dependency order. It first indexes all dependencies (e.g., balances), then indexes the primary item (e.g., transaction). This ensures referential integrity in the search index.
(ctx context.Context, batch *IndexBatch)
| 250 | // It first indexes all dependencies (e.g., balances), then indexes the primary item (e.g., transaction). |
| 251 | // This ensures referential integrity in the search index. |
| 252 | func (t *TypesenseClient) HandleBatchNotification(ctx context.Context, batch *IndexBatch) error { |
| 253 | // Deduplicate dependencies to avoid redundant indexing |
| 254 | batch.Deduplicate() |
| 255 | |
| 256 | // Step 1: Index all dependencies first (in order) |
| 257 | for _, dep := range batch.Dependencies { |
| 258 | data, err := toMap(dep.Data) |
| 259 | if err != nil { |
| 260 | return fmt.Errorf("failed to convert dependency %s/%s to map: %w", dep.Collection, dep.DocumentID, err) |
| 261 | } |
| 262 | if err := t.HandleNotification(ctx, dep.Collection, data); err != nil { |
| 263 | return fmt.Errorf("failed to index dependency %s/%s: %w", dep.Collection, dep.DocumentID, err) |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // Step 2: Index primary entity after dependencies exist |
| 268 | if batch.Primary != nil { |
| 269 | data, err := toMap(batch.Primary.Data) |
| 270 | if err != nil { |
| 271 | return fmt.Errorf("failed to convert primary %s/%s to map: %w", batch.Primary.Collection, batch.Primary.DocumentID, err) |
| 272 | } |
| 273 | if err := t.HandleNotification(ctx, batch.Primary.Collection, data); err != nil { |
| 274 | return fmt.Errorf("failed to index primary %s/%s: %w", batch.Primary.Collection, batch.Primary.DocumentID, err) |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return nil |
| 279 | } |
| 280 | |
| 281 | // toMap converts an interface{} to map[string]interface{} via JSON marshaling. |
| 282 | func toMap(data interface{}) (map[string]interface{}, error) { |
no test coverage detected