|
17 | 17 |
|
18 | 18 | package feast.ingestion.model; |
19 | 19 |
|
20 | | -import java.io.Serializable; |
21 | | -import java.util.List; |
22 | | -import java.util.Map; |
23 | | -import feast.ingestion.service.SpecRetrievalException; |
| 20 | +import com.google.common.base.Preconditions; |
| 21 | +import feast.ingestion.service.SpecService; |
24 | 22 | import feast.specs.EntitySpecProto.EntitySpec; |
25 | 23 | import feast.specs.FeatureSpecProto.FeatureSpec; |
| 24 | +import feast.specs.ImportSpecProto.Field; |
26 | 25 | import feast.specs.ImportSpecProto.ImportSpec; |
27 | 26 | import feast.specs.StorageSpecProto.StorageSpec; |
| 27 | +import java.io.Serializable; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Map.Entry; |
| 32 | +import lombok.Builder; |
| 33 | +import lombok.Getter; |
| 34 | + |
| 35 | +@Builder |
| 36 | +@Getter |
| 37 | +public class Specs implements Serializable { |
| 38 | + private String jobName; |
| 39 | + private ImportSpec importSpec; |
| 40 | + private Map<String, EntitySpec> entitySpecs; |
| 41 | + private Map<String, FeatureSpec> featureSpecs; |
| 42 | + private Map<String, StorageSpec> storageSpecs; |
| 43 | + private transient SpecService specService; |
| 44 | + private RuntimeException error; |
| 45 | + |
| 46 | + public static Specs of(String jobName, ImportSpec importSpec, SpecService specService) { |
| 47 | + try { |
| 48 | + Specs.SpecsBuilder specsBuilder = Specs.builder().jobName(jobName).importSpec(importSpec); |
| 49 | + |
| 50 | + List<Field> fields = importSpec.getSchema().getFieldsList(); |
| 51 | + List<String> featureIds = new ArrayList<>(); |
| 52 | + for (Field field : fields) { |
| 53 | + if (!field.getFeatureId().isEmpty()) { |
| 54 | + featureIds.add(field.getFeatureId()); |
| 55 | + } |
| 56 | + } |
| 57 | + specsBuilder.featureSpecs(specService.getFeatureSpecs(featureIds)); |
| 58 | + |
| 59 | + List<String> entityNames = importSpec.getEntitiesList(); |
| 60 | + for (FeatureSpec featureSpec : specsBuilder.featureSpecs.values()) { |
| 61 | + Preconditions.checkArgument( |
| 62 | + entityNames.contains(featureSpec.getEntity()), |
| 63 | + "Feature has entity not listed in import spec featureSpec=" + featureSpec.toString()); |
| 64 | + } |
| 65 | + specsBuilder.entitySpecs(specService.getEntitySpecs(entityNames)); |
| 66 | + |
| 67 | + specsBuilder.storageSpecs(specService.getAllStorageSpecs()); |
| 68 | + |
| 69 | + return specsBuilder.build(); |
| 70 | + } catch (RuntimeException e) { |
| 71 | + return Specs.builder().error(e).build(); |
| 72 | + } |
| 73 | + } |
28 | 74 |
|
29 | | -public interface Specs extends Serializable { |
30 | | - FeatureSpec getFeatureSpec(String featureId); |
| 75 | + public void validate() { |
| 76 | + if (error != null) { |
| 77 | + throw error; |
| 78 | + } |
31 | 79 |
|
32 | | - List<FeatureSpec> getFeatureSpecByServingStoreId(String storeId) throws SpecRetrievalException; |
| 80 | + // Sanity checks that our maps are built correctly |
| 81 | + for (Entry<String, FeatureSpec> entry : featureSpecs.entrySet()) { |
| 82 | + Preconditions.checkArgument(entry.getKey().equals(entry.getValue().getId())); |
| 83 | + } |
| 84 | + for (Entry<String, EntitySpec> entry : entitySpecs.entrySet()) { |
| 85 | + Preconditions.checkArgument(entry.getKey().equals(entry.getValue().getName())); |
| 86 | + } |
| 87 | + for (Entry<String, StorageSpec> entry : storageSpecs.entrySet()) { |
| 88 | + Preconditions.checkArgument(entry.getKey().equals(entry.getValue().getId())); |
| 89 | + } |
33 | 90 |
|
34 | | - EntitySpec getEntitySpec(String entityName) throws SpecRetrievalException; |
| 91 | + for (FeatureSpec featureSpec : featureSpecs.values()) { |
| 92 | + // Check that feature has a matching entity |
| 93 | + Preconditions.checkArgument( |
| 94 | + entitySpecs.containsKey(featureSpec.getEntity()), |
| 95 | + String.format( |
| 96 | + "Feature %s references unknown entity %s", |
| 97 | + featureSpec.getId(), featureSpec.getEntity())); |
| 98 | + // Check that feature has a matching serving store |
| 99 | + Preconditions.checkArgument( |
| 100 | + storageSpecs.containsKey(featureSpec.getDataStores().getServing().getId()), |
| 101 | + String.format( |
| 102 | + "Feature %s references unknown serving store %s", |
| 103 | + featureSpec.getId(), featureSpec.getDataStores().getServing().getId())); |
| 104 | + // Check that feature has a matching warehouse store |
| 105 | + Preconditions.checkArgument( |
| 106 | + storageSpecs.containsKey(featureSpec.getDataStores().getWarehouse().getId()), |
| 107 | + String.format( |
| 108 | + "Feature %s references unknown warehouse store %s", |
| 109 | + featureSpec.getId(), featureSpec.getDataStores().getWarehouse().getId())); |
| 110 | + } |
| 111 | + } |
35 | 112 |
|
36 | | - ImportSpec getImportSpec() throws SpecRetrievalException; |
| 113 | + public EntitySpec getEntitySpec(String entityName) { |
| 114 | + Preconditions.checkArgument( |
| 115 | + entitySpecs.containsKey(entityName), |
| 116 | + String.format("Unknown entity %s, spec was not initialized", entityName)); |
| 117 | + return entitySpecs.get(entityName); |
| 118 | + } |
37 | 119 |
|
38 | | - Map<String, StorageSpec> getStorageSpecs() throws SpecRetrievalException; |
| 120 | + public FeatureSpec getFeatureSpec(String featureId) { |
| 121 | + Preconditions.checkArgument( |
| 122 | + featureSpecs.containsKey(featureId), |
| 123 | + String.format("Unknown feature %s, spec was not initialized", featureId)); |
| 124 | + return featureSpecs.get(featureId); |
| 125 | + } |
39 | 126 |
|
40 | | - StorageSpec getStorageSpec(String storeId); |
| 127 | + public List<FeatureSpec> getFeatureSpecByServingStoreId(String storeId) { |
| 128 | + List<FeatureSpec> out = new ArrayList<>(); |
| 129 | + for (FeatureSpec featureSpec : featureSpecs.values()) { |
| 130 | + if (featureSpec.getDataStores().getServing().getId().equals(storeId)) { |
| 131 | + out.add(featureSpec); |
| 132 | + } |
| 133 | + } |
| 134 | + return out; |
| 135 | + } |
41 | 136 |
|
42 | | - String getJobName(); |
| 137 | + public StorageSpec getStorageSpec(String storeId) { |
| 138 | + Preconditions.checkArgument( |
| 139 | + storageSpecs.containsKey(storeId), |
| 140 | + String.format("Unknown store %s, spec was not initialized", storeId)); |
| 141 | + return storageSpecs.get(storeId); |
| 142 | + } |
43 | 143 | } |
0 commit comments