Search in sources :

Example 6 with Edge

use of org.vertexium.Edge in project vertexium by visallo.

the class Elasticsearch5SearchIndex method addExtendedData.

@Override
public void addExtendedData(Graph graph, Iterable<ExtendedDataRow> extendedDatas, Authorizations authorizations) {
    Map<ElementType, Map<String, List<ExtendedDataRow>>> rowsByElementTypeAndId = mapExtendedDatasByElementTypeByElementId(extendedDatas);
    // prefetch the vertices and edges for performance
    Map<String, Vertex> verticesById;
    if (rowsByElementTypeAndId.containsKey(ElementType.VERTEX) && !rowsByElementTypeAndId.get(ElementType.VERTEX).isEmpty()) {
        Iterable<Vertex> vertices = graph.getVertices(rowsByElementTypeAndId.get(ElementType.VERTEX).keySet(), FetchHints.NONE, authorizations);
        verticesById = stream(vertices).collect(Collectors.toMap(Vertex::getId, Function.identity()));
    } else {
        verticesById = new HashMap<>();
    }
    Map<String, Edge> edgesById;
    if (rowsByElementTypeAndId.containsKey(ElementType.EDGE) && !rowsByElementTypeAndId.get(ElementType.EDGE).isEmpty()) {
        edgesById = stream(graph.getEdges(rowsByElementTypeAndId.get(ElementType.EDGE).keySet(), FetchHints.NONE, authorizations)).collect(Collectors.toMap(Edge::getId, Function.identity()));
    } else {
        edgesById = new HashMap<>();
    }
    Set<String> missingElements = new HashSet<>();
    rowsByElementTypeAndId.forEach((elementType, elements) -> {
        elements.forEach((elementId, rows) -> {
            Element element = elementType == ElementType.VERTEX ? verticesById.get(elementId) : edgesById.get(elementId);
            if (element == null) {
                missingElements.add(String.format("%s:%s", elementType == ElementType.VERTEX ? "Vertex" : "Edge", elementId));
                return;
            }
            bulkUpdate(graph, new ConvertingIterable<ExtendedDataRow, UpdateRequest>(rows) {

                @Override
                protected UpdateRequest convert(ExtendedDataRow row) {
                    String tableName = (String) row.getPropertyValue(ExtendedDataRow.TABLE_NAME);
                    String rowId = (String) row.getPropertyValue(ExtendedDataRow.ROW_ID);
                    List<ExtendedDataMutation> columns = stream(row.getProperties()).map(property -> new ExtendedDataMutation(tableName, rowId, property.getName(), property.getKey(), property.getValue(), property.getTimestamp(), property.getVisibility())).collect(Collectors.toList());
                    return prepareUpdate(graph, element, tableName, rowId, columns, authorizations).request();
                }
            });
        });
    });
    if (missingElements.size() > 0) {
        throw new VertexiumException("Could not add all extended data, missing elements: " + Joiner.on(", ").join(missingElements));
    }
}
Also used : ExtendedDataMutation(org.vertexium.mutation.ExtendedDataMutation) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) Edge(org.vertexium.Edge)

Aggregations

Edge (org.vertexium.Edge)6 UpdateRequestBuilder (org.elasticsearch.action.update.UpdateRequestBuilder)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)1 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)1 Vertex (org.vertexium.Vertex)1 VertexiumException (org.vertexium.VertexiumException)1 CypherAstBase (org.vertexium.cypher.ast.model.CypherAstBase)1 VertexiumCypherTypeErrorException (org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException)1 ExtendedDataMutation (org.vertexium.mutation.ExtendedDataMutation)1