Search in sources :

Example 1 with VertexBuilder

use of com.baidu.hugegraph.loader.builder.VertexBuilder in project incubator-hugegraph-toolchain by apache.

the class ParseTaskBuilder method buildTask.

private ParseTask buildTask(ElementBuilder builder, List<Line> lines) {
    final LoadMetrics metrics = this.context.summary().metrics(this.struct);
    final int batchSize = this.context.options().batchSize;
    final ElementMapping mapping = builder.mapping();
    final boolean needRemoveId = builder instanceof VertexBuilder && ((VertexLabel) builder.schemaLabel()).idStrategy().isPrimaryKey();
    return new ParseTask(mapping, () -> {
        List<List<Record>> batches = new ArrayList<>();
        // One batch record
        List<Record> records = new ArrayList<>(batchSize);
        int count = 0;
        for (Line line : lines) {
            try {
                // NOTE: don't remove entry in keyValues
                @SuppressWarnings("unchecked") List<GraphElement> elements = builder.build(line.names(), line.values());
                E.checkState(elements.size() <= batchSize, "The number of columns in a line cannot " + "exceed the size of a batch, but got %s > %s", elements.size(), batchSize);
                // Prevent batch size from exceeding limit
                if (records.size() + elements.size() > batchSize) {
                    LOG.debug("Create a new batch for {}", mapping);
                    // Add current batch and create a new batch
                    batches.add(records);
                    records = new ArrayList<>(batchSize);
                }
                for (GraphElement element : elements) {
                    if (needRemoveId) {
                        ((Vertex) element).id(null);
                    }
                    records.add(new Record(line.rawLine(), element));
                    count++;
                }
            } catch (IllegalArgumentException e) {
                metrics.increaseParseFailure(mapping);
                ParseException pe = new ParseException(line.rawLine(), e);
                this.handleParseFailure(mapping, pe);
            }
        }
        if (!records.isEmpty()) {
            batches.add(records);
        }
        metrics.plusParseSuccess(mapping, count);
        return batches;
    });
}
Also used : ElementMapping(com.baidu.hugegraph.loader.mapping.ElementMapping) Vertex(com.baidu.hugegraph.structure.graph.Vertex) ArrayList(java.util.ArrayList) LoadMetrics(com.baidu.hugegraph.loader.metrics.LoadMetrics) Line(com.baidu.hugegraph.loader.reader.line.Line) VertexBuilder(com.baidu.hugegraph.loader.builder.VertexBuilder) VertexLabel(com.baidu.hugegraph.structure.schema.VertexLabel) GraphElement(com.baidu.hugegraph.structure.GraphElement) ArrayList(java.util.ArrayList) List(java.util.List) Record(com.baidu.hugegraph.loader.builder.Record) ParseException(com.baidu.hugegraph.loader.exception.ParseException)

Aggregations

Record (com.baidu.hugegraph.loader.builder.Record)1 VertexBuilder (com.baidu.hugegraph.loader.builder.VertexBuilder)1 ParseException (com.baidu.hugegraph.loader.exception.ParseException)1 ElementMapping (com.baidu.hugegraph.loader.mapping.ElementMapping)1 LoadMetrics (com.baidu.hugegraph.loader.metrics.LoadMetrics)1 Line (com.baidu.hugegraph.loader.reader.line.Line)1 GraphElement (com.baidu.hugegraph.structure.GraphElement)1 Vertex (com.baidu.hugegraph.structure.graph.Vertex)1 VertexLabel (com.baidu.hugegraph.structure.schema.VertexLabel)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1