|
| 1 | +package datastore |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | +) |
| 8 | + |
| 9 | +// LoadDataStoreFromCatalog fetches all data from a remote CatalogStore and creates a local |
| 10 | +// in-memory DataStore populated with that data. After loading, all operations are performed |
| 11 | +// on the local DataStore without any remote calls. |
| 12 | +func LoadDataStoreFromCatalog(ctx context.Context, catalog CatalogStore) (DataStore, error) { |
| 13 | + // Create a new mutable in-memory datastore |
| 14 | + memoryDS := NewMemoryDataStore() |
| 15 | + |
| 16 | + // Fetch all address references from the catalog |
| 17 | + addressRefs, err := catalog.Addresses().Fetch(ctx) |
| 18 | + if err != nil { |
| 19 | + // If no address refs found, treat as empty catalog (valid state) |
| 20 | + if !errors.Is(err, ErrAddressRefNotFound) { |
| 21 | + return nil, fmt.Errorf("failed to fetch address references from catalog: %w", err) |
| 22 | + } |
| 23 | + addressRefs = []AddressRef{} // Empty catalog is valid |
| 24 | + } |
| 25 | + |
| 26 | + // Populate the address ref store |
| 27 | + for _, ref := range addressRefs { |
| 28 | + if addErr := memoryDS.Addresses().Add(ref); addErr != nil { |
| 29 | + return nil, fmt.Errorf("failed to add address reference to local store: %w", addErr) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // Fetch all chain metadata from the catalog |
| 34 | + chainMetadata, err := catalog.ChainMetadata().Fetch(ctx) |
| 35 | + if err != nil { |
| 36 | + // If no chain metadata found, treat as empty catalog (valid state) |
| 37 | + if !errors.Is(err, ErrChainMetadataNotFound) { |
| 38 | + return nil, fmt.Errorf("failed to fetch chain metadata from catalog: %w", err) |
| 39 | + } |
| 40 | + chainMetadata = []ChainMetadata{} // Empty catalog is valid |
| 41 | + } |
| 42 | + |
| 43 | + // Populate the chain metadata store |
| 44 | + for _, metadata := range chainMetadata { |
| 45 | + if addErr := memoryDS.ChainMetadata().Add(metadata); addErr != nil { |
| 46 | + return nil, fmt.Errorf("failed to add chain metadata to local store: %w", addErr) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Fetch all contract metadata from the catalog |
| 51 | + contractMetadata, err := catalog.ContractMetadata().Fetch(ctx) |
| 52 | + if err != nil { |
| 53 | + // If no contract metadata found, treat as empty catalog (valid state) |
| 54 | + if !errors.Is(err, ErrContractMetadataNotFound) { |
| 55 | + return nil, fmt.Errorf("failed to fetch contract metadata from catalog: %w", err) |
| 56 | + } |
| 57 | + contractMetadata = []ContractMetadata{} // Empty catalog is valid |
| 58 | + } |
| 59 | + |
| 60 | + // Populate the contract metadata store |
| 61 | + for _, metadata := range contractMetadata { |
| 62 | + if addErr := memoryDS.ContractMetadata().Add(metadata); addErr != nil { |
| 63 | + return nil, fmt.Errorf("failed to add contract metadata to local store: %w", addErr) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Fetch environment metadata from the catalog |
| 68 | + envMetadata, err := catalog.EnvMetadata().Get(ctx) |
| 69 | + if err != nil { |
| 70 | + // EnvMetadata might not exist, which is okay - ignore ErrEnvMetadataNotSet |
| 71 | + if !errors.Is(err, ErrEnvMetadataNotSet) { |
| 72 | + return nil, fmt.Errorf("failed to fetch environment metadata from catalog: %w", err) |
| 73 | + } |
| 74 | + // If it's ErrEnvMetadataNotSet, continue without error |
| 75 | + } else { |
| 76 | + // Populate the environment metadata store |
| 77 | + if setErr := memoryDS.EnvMetadata().Set(envMetadata); setErr != nil { |
| 78 | + return nil, fmt.Errorf("failed to set environment metadata in local store: %w", setErr) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Seal the datastore to make it read-only and return |
| 83 | + return memoryDS.Seal(), nil |
| 84 | +} |
0 commit comments