use of com.spotify.ffwd.v1.Value in project grpc-gcp-java by GoogleCloudPlatform.
the class MakeFieldsMap method makeFieldsMap.
public HashMap<String, Value> makeFieldsMap() {
String fieldName = "initial";
String fieldValue = "initial";
Scanner sc = new Scanner(System.in);
HashMap<String, Value> inputFields = new HashMap<>();
while (!fieldName.matches("DONE")) {
System.out.print("Field Name (Enter DONE to quit): ");
fieldName = sc.next();
if (!fieldName.matches("DONE")) {
System.out.print("Field Value: ");
fieldValue = sc.next();
Value fsValue = Value.newBuilder().setStringValue(fieldValue).build();
inputFields.put(fieldName, fsValue);
}
}
return inputFields;
}
use of com.spotify.ffwd.v1.Value in project meveo by meveo-org.
the class Neo4jService method deleteEntity.
public void deleteEntity(String neo4jConfiguration, String cetCode, Map<String, Object> values) throws BusinessException {
/* Get entity template */
final CustomEntityTemplate customEntityTemplate = customFieldsCache.getCustomEntityTemplate(cetCode);
/* Extract unique fields values for node */
final Map<String, Object> uniqueFields = getNodeKeys(customEntityTemplate.getAppliesTo(), values);
/* No unique fields has been found */
if (uniqueFields.isEmpty()) {
throw new BusinessException("At least one unique field must be provided for cet to delete");
}
final String uniqueFieldStatement = neo4jDao.getFieldsString(uniqueFields.keySet());
/* Map the variables declared in the statement */
Map<String, Object> valuesMap = new HashMap<>();
valuesMap.put("cetCode", cetCode);
valuesMap.put("uniqueFields", uniqueFieldStatement);
String deleteStatement = getStatement(new StrSubstitutor(valuesMap), Neo4JRequests.deleteCet);
final Transaction transaction = crossStorageTransaction.getNeo4jTransaction(neo4jConfiguration);
InternalNode internalNode = null;
try {
/* Delete the node and all its associated relationships and fire node deletion event */
// Execute query
final StatementResult result = transaction.run(deleteStatement, values);
for (Record record : result.list()) {
// Fire deletion event
// Parse properties
final Map<String, Value> properties = record.get("properties").asMap(e -> e);
// Parse labels
final List<String> labels = record.get("labels").asList(Value::asString);
// Parse id
final long id = record.get("id").asLong();
// Create Node object
internalNode = new InternalNode(id, labels, properties);
}
transaction.success();
} catch (Exception e) {
log.error("Cannot delete node with code {} and values {}", cetCode, values, e);
transaction.failure();
throw new BusinessException(e);
}
if (internalNode != null) {
// Fire notification
nodeRemovedEvent.fire(new Neo4jEntity(internalNode, neo4jConfiguration));
}
}
use of com.spotify.ffwd.v1.Value in project meveo by meveo-org.
the class Neo4jService method addSourceNodeUniqueCrt.
/**
* Persist a source node of an unique relationship.
* If a relationship that targets the target node exists, then we merge the fields of the start in parameter to
* the fields of the source node of the relationship.
* If such a relation does not exists, we create the source node with it fields.
*
* @param neo4JConfiguration Neo4J coordinates
* @param crtCode Code of the unique relation
* @param startNodeValues Values to assign to the start node
* @param endNodeValues Filters on the target node values
*/
@JpaAmpNewTx
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public PersistenceActionResult addSourceNodeUniqueCrt(String neo4JConfiguration, String crtCode, Map<String, Object> startNodeValues, Map<String, Object> endNodeValues) throws BusinessException, ELException {
// Get relationship template
final CustomRelationshipTemplate customRelationshipTemplate = customFieldsCache.getCustomRelationshipTemplate(crtCode);
final CustomEntityTemplate endNode = customRelationshipTemplate.getEndNode();
// Extract unique fields values for the start node
Map<String, CustomFieldTemplate> endNodeCfts = customFieldTemplateService.findByAppliesTo(endNode.getAppliesTo());
Map<String, CustomFieldTemplate> startNodeCfts = customFieldTemplateService.findByAppliesTo(customRelationshipTemplate.getStartNode().getAppliesTo());
final Map<String, Object> endNodeUniqueFields = new HashMap<>();
Map<String, Object> endNodeConvertedValues = validateAndConvertCustomFields(endNodeCfts, endNodeValues, endNodeUniqueFields, true);
Map<String, Object> startNodeConvertedValues = validateAndConvertCustomFields(startNodeCfts, startNodeValues, null, true);
// Map the variables declared in the statement
Map<String, Object> valuesMap = new HashMap<>();
final String cetCode = customRelationshipTemplate.getStartNode().getCode();
valuesMap.put("cetCode", cetCode);
valuesMap.put("crtCode", crtCode);
valuesMap.put("endCetcode", customRelationshipTemplate.getEndNode().getCode());
// Prepare the key maps for unique fields and start node fields
final String uniqueFieldStatements = neo4jDao.getFieldsString(endNodeConvertedValues.keySet());
final String startNodeValuesStatements = neo4jDao.getFieldsString(startNodeConvertedValues.keySet());
// No unique fields has been found
if (endNodeUniqueFields.isEmpty()) {
// If no unique fields are provided / defined, retrieve the meveo_uuid of the target node using unicity rules
Set<String> ids = endNode.getNeo4JStorageConfiguration().getUniqueConstraints().stream().filter(uniqueConstraint -> uniqueConstraint.getTrustScore() == 100).filter(uniqueConstraint -> isApplicableConstraint(endNodeValues, uniqueConstraint)).sorted(Neo4jService.CONSTRAINT_COMPARATOR).map(uniqueConstraint -> neo4jDao.executeUniqueConstraint(neo4JConfiguration, uniqueConstraint, endNodeValues, endNode.getCode())).findFirst().orElse(Set.of());
if (ids.isEmpty()) {
log.error("At least one unique field must be provided for target entity [code = {}, fields = {}]. " + "Unique fields are : {}", customRelationshipTemplate.getEndNode().getCode(), endNodeValues, endNodeUniqueFields);
throw new BusinessException("Unique field must be provided");
}
if (ids.size() > 1) {
throw new BusinessException(String.format("Multiple targets for unique relationship %s : %s.", crtCode, ids));
}
String id = ids.iterator().next();
endNodeValues.put("meveo_uuid", id);
endNodeUniqueFields.put("meveo_uuid", id);
}
// Assign the keys names
valuesMap.put(FIELD_KEYS, uniqueFieldStatements);
valuesMap.put(FIELDS, startNodeValuesStatements);
// Create the substitutor
StrSubstitutor sub = new StrSubstitutor(valuesMap);
// Values of the keys defined in valuesMap
Map<String, Object> parametersValues = new HashMap<>();
parametersValues.putAll(startNodeConvertedValues);
parametersValues.putAll(endNodeConvertedValues);
final Transaction transaction = crossStorageTransaction.getNeo4jTransaction(neo4JConfiguration);
// Try to find the id of the source node
String findStartNodeStatement = getStatement(sub, Neo4JRequests.findStartNodeId);
final StatementResult run = transaction.run(findStartNodeStatement, parametersValues);
Neo4jEntity startNode = null;
try {
try {
/* Update the source node with the found id */
// Alias to use in queries
final String startNodeAlias = "startNode";
// Retrieve ID of the node
final Record idRecord = run.single();
final Value id = idRecord.get(0);
parametersValues.put(NODE_ID, id);
// Create statement
CustomEntityTemplate startCet = customRelationshipTemplate.getStartNode();
List<String> additionalLabels = getAdditionalLabels(startCet);
final Map<String, Object> updatableValues = valuesMap.entrySet().stream().filter(s -> startNodeCfts.get(s.getKey()).isAllowEdit()).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
StringBuffer statement = neo4jDao.appendAdditionalLabels(Neo4JRequests.updateNodeWithId, additionalLabels, startNodeAlias, updatableValues);
statement = neo4jDao.appendReturnStatement(statement, startNodeAlias, valuesMap);
String updateStatement = getStatement(sub, statement);
// Execute query
final StatementResult result = transaction.run(updateStatement, parametersValues);
// Fire node update event
startNode = new Neo4jEntity(result.single().get(startNodeAlias).asNode(), neo4JConfiguration);
} catch (NoSuchRecordException e) {
/* Create the source node */
addCetNode(neo4JConfiguration, customRelationshipTemplate.getStartNode(), startNodeValues, null);
}
transaction.success();
} catch (Exception e) {
transaction.failure();
log.error("Transaction for persisting entity with code {} and fields {} was rolled back", cetCode, startNodeValues, e);
throw new BusinessException(e);
}
if (startNode != null) {
nodeUpdatedEvent.fire(startNode);
return new PersistenceActionResult(startNode.get("meveo_uuid").asString());
} else {
return null;
}
}
use of com.spotify.ffwd.v1.Value in project meveo by meveo-org.
the class Neo4jService method createEntityReferences.
/**
* @param neo4JConfiguration
* @param cet
* @param fieldValues
* @param cetFields
* @param fields
* @return
* @throws BusinessException
*/
private Map<EntityRef, String> createEntityReferences(String neo4JConfiguration, CustomEntityTemplate cet, Map<String, Object> fieldValues, Map<String, CustomFieldTemplate> cetFields, Map<String, Object> fields) throws BusinessException {
/* Collect entity references */
final List<CustomFieldTemplate> entityReferences = cetFields.values().stream().filter(// Entity references
customFieldTemplate -> customFieldTemplate.getFieldType().equals(CustomFieldTypeEnum.ENTITY)).filter(// Value is provided
customFieldTemplate -> fieldValues.get(customFieldTemplate.getCode()) != null).collect(Collectors.toList());
/* Create referenced nodes and collect relationships to create */
// Map where the id of the target node is the key and the label of relationship is the value
Map<EntityRef, String> relationshipsToCreate = new HashMap<>();
for (CustomFieldTemplate entityReference : entityReferences) {
Object referencedCetValue = fieldValues.get(entityReference.getCode());
String referencedCetCode = entityReference.getEntityClazzCetCode();
CustomEntityTemplate referencedCet = customFieldsCache.getCustomEntityTemplate(referencedCetCode);
if (referencedCetValue instanceof EntityReferenceWrapper) {
EntityReferenceWrapper wrapper = (EntityReferenceWrapper) referencedCetValue;
if (wrapper.getUuid() == null) {
continue;
}
}
Collection<Object> values;
if (entityReference.getStorageType().equals(CustomFieldStorageTypeEnum.LIST)) {
if (!(referencedCetValue instanceof Collection)) {
throw new BusinessException("Value for CFT " + entityReference.getCode() + " of CET " + cet.getCode() + " should be a collection");
}
values = ((Collection<Object>) referencedCetValue);
if (referencedCet.getNeo4JStorageConfiguration() != null && referencedCet.getNeo4JStorageConfiguration().isPrimitiveEntity()) {
fields.put(entityReference.getCode(), new ArrayList<>());
}
} else {
values = Collections.singletonList(referencedCetValue);
}
for (Object value : values) {
Set<EntityRef> relatedPersistedEntities = new HashSet<>();
if (referencedCet.getNeo4JStorageConfiguration() != null && referencedCet.getNeo4JStorageConfiguration().isPrimitiveEntity()) {
Map<String, Object> valueMap = new HashMap<>();
valueMap.put("value", value);
// If there is no unique constraints defined, directly merge node
if (referencedCet.getNeo4JStorageConfiguration().getUniqueConstraints().isEmpty()) {
List<String> additionalLabels = getAdditionalLabels(referencedCet);
executePrePersist(neo4JConfiguration, referencedCet, valueMap);
String createdNodeId = neo4jDao.mergeNode(neo4JConfiguration, referencedCetCode, valueMap, valueMap, valueMap, additionalLabels, null);
if (createdNodeId != null) {
relatedPersistedEntities.add(new EntityRef(createdNodeId, referencedCet.getCode()));
}
} else {
PersistenceActionResult persistenceResult = addCetNode(neo4JConfiguration, referencedCetCode, valueMap);
relatedPersistedEntities.addAll(persistenceResult.getPersistedEntities());
}
if (entityReference.getStorageType().equals(CustomFieldStorageTypeEnum.LIST)) {
((List<Object>) fields.get(entityReference.getCode())).add(valueMap.get("value"));
} else {
fields.put(entityReference.getCode(), valueMap.get("value"));
}
} else {
// Referenced CET is not primitive
if (value instanceof Map && referencedCet.getAvailableStorages().contains(DBStorageType.NEO4J)) {
Map<String, Object> valueMap = (Map<String, Object>) value;
PersistenceActionResult persistenceResult = addCetNode(neo4JConfiguration, referencedCet, valueMap);
relatedPersistedEntities.addAll(persistenceResult.getPersistedEntities());
} else if (value instanceof String) {
// If entity reference's value is a string and the entity reference is not primitive, then the value is likely the UUID of the referenced node
handleUuidReference(neo4JConfiguration, cet, relationshipsToCreate, entityReference, referencedCet, value);
} else if (value instanceof EntityReferenceWrapper) {
handleUuidReference(neo4JConfiguration, cet, relationshipsToCreate, entityReference, referencedCet, ((EntityReferenceWrapper) value).getUuid());
} else if (value instanceof Collection) {
for (Object item : (Collection<?>) value) {
if (item instanceof String) {
handleUuidReference(neo4JConfiguration, cet, relationshipsToCreate, entityReference, referencedCet, value);
}
}
} else if (referencedCet.getAvailableStorages().contains(DBStorageType.NEO4J)) {
throw new IllegalArgumentException("CET " + referencedCetCode + " should be a primitive entity");
}
}
if (relatedPersistedEntities != null) {
String relationshipName = Optional.ofNullable(entityReference.getRelationshipName()).orElseGet(() -> entityReference.getRelationship() != null ? entityReference.getRelationship().getName() : null);
if (relationshipName == null) {
throw new BusinessException(entityReference.getAppliesTo() + "#" + entityReference.getCode() + ": Relationship name must be provided !");
}
for (EntityRef entityRef : relatedPersistedEntities) {
relationshipsToCreate.put(entityRef, relationshipName);
}
}
}
}
return relationshipsToCreate;
}
use of com.spotify.ffwd.v1.Value in project semantic-metrics by spotify.
the class FastForwardReporter method reportDistribution.
private void reportDistribution(final com.spotify.ffwd.v1.Metric metric, final Distribution distribution) {
ByteString byteString = distribution.getValueAndFlush();
Value value = Value.distributionValue(byteString);
send(metric.value(value));
}
Aggregations