com.aliyun.openservices.paifeaturestore.domain.Model Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of paifeaturestore-sdk Show documentation
Show all versions of paifeaturestore-sdk Show documentation
SDK for PAI-FeatureStore service
package com.aliyun.openservices.paifeaturestore.domain;
import com.aliyun.openservices.paifeaturestore.constants.FSType;
import com.aliyun.openservices.paifeaturestore.model.ModelFeatures;
import com.aliyun.openservices.paifeaturestore.model.SeqConfig;
import com.aliyun.tea.utils.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Model {
private final com.aliyun.openservices.paifeaturestore.model.Model model;
private final Project project;
private final Map featureViewMap = new HashMap<>();
private final Map featureEntityMap = new HashMap<>();
// featureview : feature names
private final Map> featureNamesMap = new HashMap<>();
// featureview : alias names
private final Map> aliasNamesMap = new HashMap<>();
// feature entity joinid : featureviews
private final Map> featureEntityJoinIdMap = new HashMap<>();
List featureEntityJoinIdList = new ArrayList<>();
static ExecutorService executorService;
static {
executorService = Executors.newCachedThreadPool(r -> {
Thread thread = new Thread(r);
thread.setName("modelfeature-processor");
thread.setDaemon(true);
return thread;
});
}
public Model(com.aliyun.openservices.paifeaturestore.model.Model model, Project project) {
this.model = model;
this.project = project;
for (ModelFeatures feature : this.model.getFeatures()) {
//IFeatureView featureView = project.getFeatureView(feature.getFeatureViewName());
IFeatureView featureView = project.getFeatureViewMap().get(feature.getFeatureViewName());
FeatureEntity featureEntity = project.getFeatureEntity(featureView.getFeatureView().getFeatureEntityName());
this.featureViewMap.put(feature.getFeatureViewName(), featureView);
this.featureEntityMap.put(featureView.getFeatureView().getFeatureEntityName(), featureEntity);
if (this.featureNamesMap.containsKey(feature.getFeatureViewName())) {
if (featureView instanceof SequenceFeatureView) {
SequenceFeatureView sequenceFeatureView = (SequenceFeatureView) featureView;
for (SeqConfig config : sequenceFeatureView.getSeqConfigs()) {
if (config.getOfflineSeqName().equals(feature.getName())) {
this.featureNamesMap.get(feature.getFeatureViewName()).add(config.getOnlineSeqName());
}
}
} else {
this.featureNamesMap.get(feature.getFeatureViewName()).add(feature.getName());
}
} else {
List names = new ArrayList<>();
if (featureView instanceof SequenceFeatureView) {
SequenceFeatureView sequenceFeatureView = (SequenceFeatureView) featureView;
for (SeqConfig config : sequenceFeatureView.getSeqConfigs()) {
if (config.getOfflineSeqName().equals(feature.getName())) {
names.add(config.getOnlineSeqName());
}
}
} else {
names.add(feature.getName());
}
this.featureNamesMap.put(feature.getFeatureViewName(), names);
}
if (!StringUtils.isEmpty(feature.getAliasName())) {
if (this.aliasNamesMap.containsKey(feature.getFeatureViewName())) {
this.aliasNamesMap.get(feature.getFeatureViewName()).put(feature.getName(), feature.getAliasName());
} else {
Map names = new HashMap<>();
names.put(feature.getName(), feature.getAliasName());
this.aliasNamesMap.put(feature.getFeatureViewName(), names);
}
}
if (this.featureEntityJoinIdMap.containsKey(featureEntity.getFeatureEntity().getFeatureEntityJoinid())) {
this.featureEntityJoinIdMap.get(featureEntity.getFeatureEntity().getFeatureEntityJoinid()).put(feature.getFeatureViewName(), featureView);
} else {
Map featureViewMap1 = new HashMap<>();
featureViewMap1.put(feature.getFeatureViewName(), featureView);
this.featureEntityJoinIdMap.put(featureEntity.getFeatureEntity().getFeatureEntityJoinid(), featureViewMap1);
}
for (String joinId : this.featureEntityJoinIdMap.keySet()) {
this.featureEntityJoinIdList.add(joinId);
}
}
}
public com.aliyun.openservices.paifeaturestore.model.Model getModel() {
return model;
}
public FeatureResult getOnlineFeatures(Map> joinIds) throws Exception {
int size = -1;
for (String joinId : this.featureEntityJoinIdList) {
if (!joinIds.containsKey(joinId)) {
throw new RuntimeException(String.format("join id:%s not found", joinId));
}
if (-1 == size) {
size = joinIds.get(joinId).size();
} else {
if (size != joinIds.get(joinId).size()) {
throw new RuntimeException(String.format("join id:%s length not equal", joinId));
}
}
}
CountDownLatch countDownLatch = new CountDownLatch(joinIds.size());
// thread safe map
Map> joinIdFeaturesMap = new ConcurrentHashMap<>();
for (Map.Entry> entry : joinIds.entrySet()) {
executorService.execute(() -> {
try {
Map featureViewMap = this.featureEntityJoinIdMap.get(entry.getKey());
Stream> completableFutureStream = featureViewMap.values().stream().map(featureView -> CompletableFuture.supplyAsync(() -> {
try {
return featureView.getOnlineFeatures(entry.getValue().toArray(new String[0]),
this.featureNamesMap.get(featureView.getFeatureView().getName()).toArray(new String[0]), this.aliasNamesMap.get(featureView.getFeatureView().getName()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
List featureResults = completableFutureStream.map(CompletableFuture::join).collect(Collectors.toList());
// add to map
joinIdFeaturesMap.put(entry.getKey(), featureResults);
} finally {
countDownLatch.countDown();
}
});
}
// wait all featureview get features
countDownLatch.await();
FeatureStoreResult featureStoreResult = new FeatureStoreResult();
List featureFields = new ArrayList<>();
Map featureFieldTypeMap = new HashMap<>();
List