Java源码示例:com.baidu.hugegraph.util.E

示例1
public static void main(String[] args) {
    E.checkArgument(args.length == 1, "args: file");
    String input = args[0];
    LOG.info("Prepare to convert mapping file {}", input);

    File file = FileUtils.getFile(input);
    if (!file.exists() || !file.isFile()) {
        LOG.error("The file '{}' doesn't exists or not a file", input);
        throw new IllegalArgumentException(String.format(
                  "The file '%s' doesn't exists or not a file", input));
    }

    LoadMapping mapping = LoadMapping.of(input);
    String outputPath = getOutputPath(file);
    MappingUtil.write(mapping, outputPath);
    LOG.info("Convert mapping file successfuly, stored at {}", outputPath);
}
 
示例2
private HugeElement constructErrorElem(
                    ConditionQuery query, HugeElement element,
                    Map<PropertyKey, Object> incorrectPKs) {
    HugeElement errorElem = element.copyAsFresh();
    Set<Id> propKeys = query.userpropKeys();
    for (Id key : propKeys) {
        Set<Object> conditionValues = query.userpropValues(key);
        E.checkState(!conditionValues.isEmpty(),
                     "Expect user property values for key '%s', " +
                     "but got none", key);
        if (conditionValues.size() > 1) {
            // It's inside/between Query (processed in range index)
            return null;
        }
        HugeProperty<?> prop = element.getProperty(key);
        Object errorValue = conditionValues.iterator().next();
        if (prop == null || !Objects.equals(prop.value(), errorValue)) {
            PropertyKey pkey = this.graph().propertyKey(key);
            errorElem.addProperty(pkey, errorValue);
            incorrectPKs.put(pkey, errorValue);
        }
    }
    return errorElem;
}
 
示例3
@Override
protected List<Object> idColumnValue(Id id) {
    EdgeId edgeId;
    if (id instanceof EdgeId) {
        edgeId = (EdgeId) id;
    } else {
        String[] idParts = EdgeId.split(id);
        if (idParts.length == 1) {
            // Delete edge by label
            return Arrays.asList((Object[]) idParts);
        }
        id = IdUtil.readString(id.asString());
        edgeId = EdgeId.parse(id.asString());
    }

    E.checkState(edgeId.direction() == this.direction,
                 "Can't query %s edges from %s edges table",
                 edgeId.direction(), this.direction);

    return idColumnValue(edgeId);
}
 
示例4
public VertexLabelBuilder(SchemaTransaction transaction,
                          HugeGraph graph, String name) {
    super(transaction, graph);
    E.checkNotNull(name, "name");
    this.id = null;
    this.name = name;
    this.idStrategy = IdStrategy.DEFAULT;
    this.properties = new HashSet<>();
    this.primaryKeys = new ArrayList<>();
    this.nullableKeys = new HashSet<>();
    this.ttl = 0L;
    this.ttlStartTime = null;
    this.enableLabelIndex = null;
    this.userdata = new Userdata();
    this.checkExist = true;
}
 
示例5
private void removeShardsFilesIfExists() {
    File logDir = new File(this.logDir());
    E.checkArgument(logDir.exists() && logDir.isDirectory(),
                    "The log directory '%s' not exists or is file",
                    logDir);
    for (File file : logDir.listFiles()) {
        if (file.getName().endsWith(SHARDS_SUFFIX)) {
            try {
                FileUtils.forceDelete(file);
            } catch (IOException e) {
                throw new ToolsException("Failed to delete shard file " +
                                         "'%s'", file);
            }
        }
    }
}
 
示例6
public CassandraStore(final BackendStoreProvider provider,
                      final String keyspace, final String store) {
    E.checkNotNull(keyspace, "keyspace");
    E.checkNotNull(store, "store");

    this.provider = provider;

    this.keyspace = keyspace;
    this.store = store;
    this.tables = new ConcurrentHashMap<>();

    this.sessions = null;
    this.conf = null;

    this.registerMetaHandlers();
    LOG.debug("Store loaded: {}", store);
}
 
示例7
private void checkPrimaryKeyIndex(SchemaLabel schemaLabel) {
    if (schemaLabel instanceof VertexLabel) {
        VertexLabel vl = (VertexLabel) schemaLabel;
        if (vl.idStrategy().isPrimaryKey()) {
            if (this.indexType.isSecondary() ||
                this.indexType.isUnique() ||
                this.indexType.isShard() &&
                this.allStringIndex(this.indexFields)) {
                List<String> pks = this.graph()
                                       .mapPkId2Name(vl.primaryKeys());
                E.checkArgument(!this.indexFields.containsAll(pks),
                                "No need to build index on properties " +
                                "%s, because they contains all primary " +
                                "keys %s for vertex label '%s'",
                                this.indexFields, pks, vl.name());
            }
        }
    }
}
 
示例8
@Watched(prefix = "tx")
public Number queryNumber(Query query) {
    LOG.debug("Transaction queryNumber: {}", query);

    E.checkArgument(query.aggregate() != null,
                    "The aggregate must be set for number query: %s",
                    query);
    Query squery = this.serializer.writeQuery(query);

    this.beforeRead();
    try {
        return this.store.queryNumber(squery);
    } finally {
        this.afterRead();
    }
}
 
示例9
public WeightedPaths singleSourceShortestPaths(Id sourceV, Directions dir,
                                               String label, String weight,
                                               long degree, long skipDegree,
                                               long capacity, long limit) {
    E.checkNotNull(sourceV, "source vertex id");
    E.checkNotNull(dir, "direction");
    checkDegree(degree);
    checkCapacity(capacity);
    checkSkipDegree(skipDegree, degree, capacity);
    checkLimit(limit);

    Id labelId = this.getEdgeLabelId(label);
    Traverser traverser = new Traverser(sourceV, dir, labelId, weight,
                                        degree, skipDegree, capacity,
                                        limit);
    while (true) {
        // Found, reach max depth or reach capacity, stop searching
        traverser.forward();
        if (traverser.done()) {
            return traverser.shortestPaths();
        }
        checkCapacity(traverser.capacity, traverser.size, "shortest path");
    }
}
 
示例10
protected void parseColumn(BackendColumn col, HugeVertex vertex) {
    BytesBuffer buffer = BytesBuffer.wrap(col.name);
    Id id = this.keyWithIdPrefix ? buffer.readId() : vertex.id();
    E.checkState(buffer.remaining() > 0, "Missing column type");
    byte type = buffer.read();
    // Parse property
    if (type == HugeType.PROPERTY.code()) {
        Id pkeyId = buffer.readId();
        this.parseProperty(pkeyId, BytesBuffer.wrap(col.value), vertex);
    }
    // Parse edge
    else if (type == HugeType.EDGE_IN.code() ||
             type == HugeType.EDGE_OUT.code()) {
        this.parseEdge(col, vertex, vertex.graph());
    }
    // Parse system property
    else if (type == HugeType.SYS_PROPERTY.code()) {
        // pass
    }
    // Invalid entry
    else {
        E.checkState(false, "Invalid entry(%s) with unknown type(%s): 0x%s",
                     id, type & 0xff, Bytes.toHex(col.name));
    }
}
 
示例11
private void checkTtl() {
    E.checkArgument(this.ttl >= 0,
                    "The ttl must be >= 0, but got: %s", this.ttl);
    if (this.ttl == 0L) {
        E.checkArgument(this.ttlStartTime == null,
                        "Can't set ttl start time if ttl is not set");
        return;
    }
    if (this.ttlStartTime == null) {
        return;
    }
    // Check whether the properties contains the specified keys
    E.checkArgument(!this.properties.isEmpty(),
                    "The properties can't be empty when exist " +
                    "ttl start time for edge label '%s'", this.name);
    E.checkArgument(this.properties.contains(this.ttlStartTime),
                    "The ttl start time '%s' must be contained in " +
                    "properties '%s' for vertex label '%s'",
                    this.ttlStartTime, this.name, this.properties);
    PropertyKey pkey = this.graph().propertyKey(this.ttlStartTime);
    E.checkArgument(pkey.dataType().isDate(),
                    "The ttl start time property must be date type," +
                    "but got '%s(%s)'", this.ttlStartTime, pkey.dataType());
}
 
示例12
private PathSet subGraphPaths(Id sourceV, Directions dir, String label,
                              int depth, long degree, long capacity,
                              long limit, boolean rings,
                              boolean sourceInRing) {
    E.checkNotNull(sourceV, "source vertex id");
    E.checkNotNull(dir, "direction");
    checkPositive(depth, "max depth");
    checkDegree(degree);
    checkCapacity(capacity);
    checkLimit(limit);

    Id labelId = this.getEdgeLabelId(label);
    Traverser traverser = new Traverser(sourceV, labelId, depth, degree,
                                        capacity, limit, rings,
                                        sourceInRing);
    PathSet paths = new PathSet();
    while (true) {
        paths.addAll(traverser.forward(dir));
        if (--depth <= 0 || traverser.reachLimit() ||
            traverser.finished()) {
            break;
        }
    }
    return paths;
}
 
示例13
private NeighborRankTraverser.Step jsonToStep(HugeGraph graph) {
    E.checkArgument(this.degree > 0 || this.degree == NO_LIMIT,
                    "The degree must be > 0, but got: %s",
                    this.degree);
    E.checkArgument(this.top > 0 && this.top <= MAX_TOP,
                    "The top of each layer can't exceed %s", MAX_TOP);
    Map<Id, String> labelIds = new HashMap<>();
    if (this.labels != null) {
        for (String label : this.labels) {
            EdgeLabel el = graph.edgeLabel(label);
            labelIds.put(el.id(), label);
        }
    }
    return new NeighborRankTraverser.Step(this.direction, labelIds,
                                          this.degree, this.top);
}
 
示例14
@GET
@Timed
@Compress
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String list(@Context GraphManager manager,
                   @PathParam("graph") String graph,
                   @QueryParam("ids") List<String> stringIds) {
    LOG.debug("Graph [{}] get edges by ids: {}", graph, stringIds);

    E.checkArgument(stringIds != null && !stringIds.isEmpty(),
                    "The ids parameter can't be null or empty");

    Object[] ids = new Id[stringIds.size()];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = HugeEdge.getIdValue(stringIds.get(i), false);
    }

    HugeGraph g = graph(manager, graph);

    Iterator<Edge> edges = g.edges(ids);
    return manager.serializer(g).writeEdges(edges, false);
}
 
示例15
@Override
protected BackendEntry mergeEntries(BackendEntry e1, BackendEntry e2) {
    // Merge edges into vertex
    // TODO: merge rows before calling row2Entry()

    CassandraBackendEntry current = (CassandraBackendEntry) e1;
    CassandraBackendEntry next = (CassandraBackendEntry) e2;

    E.checkState(current == null || current.type().isVertex(),
                 "The current entry must be null or VERTEX");
    E.checkState(next != null && next.type().isEdge(),
                 "The next entry must be EDGE");

    long maxSize = BackendEntryIterator.INLINE_BATCH_SIZE;
    if (current != null && current.subRows().size() < maxSize) {
        Object nextVertexId = next.column(HugeKeys.OWNER_VERTEX);
        if (current.id().equals(IdGenerator.of(nextVertexId))) {
            current.subRow(next.row());
            return current;
        }
    }

    return this.wrapByVertex(next);
}
 
示例16
public Response doGetRequest(String auth,
                             MultivaluedMap<String, String> params) {
    WebTarget target = this.webTarget;
    for (Map.Entry<String, List<String>> entry : params.entrySet()) {
        E.checkArgument(entry.getValue().size() == 1,
                        "Invalid query param '%s', can only accept " +
                        "one value, but got %s",
                        entry.getKey(), entry.getValue());
        target = target.queryParam(entry.getKey(), entry.getValue().get(0));
    }
    return target.request()
                 .header(HttpHeaders.AUTHORIZATION, auth)
                 .accept(MediaType.APPLICATION_JSON)
                 .acceptEncoding(CompressInterceptor.GZIP)
                 .get();
}
 
示例17
@Watched(prefix = "schema")
private static Id asyncRun(HugeGraph graph, SchemaElement schema,
                           boolean sync, SchemaCallable callable,
                           Set<Id> dependencies) {
    E.checkArgument(schema != null, "Schema can't be null");
    String name = SchemaCallable.formatTaskName(schema.type(),
                                                schema.id(),
                                                schema.name());

    JobBuilder<Object> builder = JobBuilder.of(graph).name(name)
                                           .job(callable)
                                           .dependencies(dependencies);
    HugeTask<?> task = builder.schedule();

    // If TASK_SYNC_DELETION is true, wait async thread done before
    // continue. This is used when running tests.
    if (sync) {
        task.syncWait();
    }
    return task.id();
}
 
示例18
public void setExpiredTime() {
    SchemaLabel label = this.schemaLabel();
    if (label.ttl() == 0L) {
        return;
    }
    long now = this.graph.now();
    if (SchemaLabel.NONE_ID.equals(label.ttlStartTime())) {
        this.expiredTime(now + label.ttl());
        return;
    }
    Date date = this.getPropertyValue(label.ttlStartTime());
    if (date == null) {
        this.expiredTime(now + label.ttl());
        return;
    }
    long expired = date.getTime() + label.ttl();
    E.checkArgument(expired > now,
                    "The expired time '%s' of '%s' is prior to now: %s",
                    new Date(expired), this, now);
    this.expiredTime(expired);
}
 
示例19
@Override
public void ensureDirectoryExist(boolean create) {
    FileSystem fs = this.fileSystem();
    Path path = new Path(this.directory());
    try {
        if (fs.exists(path)) {
            E.checkState(fs.getFileStatus(path).isDirectory(),
                         "Can't use directory '%s' because " +
                         "a file with same name exists.", this.directory());
        } else {
            if (create) {
                E.checkState(fs.mkdirs(path),
                             "The directory does not exist and created " +
                             "failed: '%s'", path.toString());
            } else {
                E.checkState(false,
                             "The directory does not exist: '%s'",
                             path.toString());
            }
        }
    } catch (IOException e) {
        throw new ToolsException("Invalid directory '%s'",
                                 e, this.directory());
    }
}
 
示例20
@Override
public VertexLabelBuilder useCustomizeNumberId() {
    E.checkArgument(this.idStrategy == IdStrategy.DEFAULT ||
                    this.idStrategy == IdStrategy.CUSTOMIZE_NUMBER,
                    "Not allowed to change id strategy for " +
                    "vertex label '%s'", this.name);
    this.idStrategy = IdStrategy.CUSTOMIZE_NUMBER;
    return this;
}
 
示例21
/**
 * Add edge with direction OUT
 * @param edge the out edge
 */
public void addOutEdge(HugeEdge edge) {
    if (edge.ownerVertex() == null) {
        edge.sourceVertex(this);
    }
    E.checkState(edge.isDirection(Directions.OUT),
                 "The owner vertex('%s') of OUT edge '%s' should be '%s'",
                 edge.ownerVertex().id(), edge, this.id());
    this.edges.add(edge);
}
 
示例22
@Override
protected GraphTransaction tx() {
    if (this.fresh()) {
        E.checkNotNull(this.tx, "tx");
        return this.tx;
    }
    return null;
}
 
示例23
public CountRequest build() {
    E.checkArgumentNotNull(this.request.source,
                           "The source can't be null");
    for (Step.Builder builder : this.stepBuilders) {
        this.request.steps.add(builder.build());
    }
    E.checkArgument(this.request.steps != null &&
                    !this.request.steps.isEmpty(),
                    "The steps can't be null or empty");
    checkDedupSize(this.request.dedupSize);
    return this.request;
}
 
示例24
@Watched(prefix = "vertex")
@Override
protected boolean ensureFilledProperties(boolean throwIfNotExist) {
    if (this.propLoaded) {
        return true;
    }

    // Skip query if there is no any property key in schema
    if (this.schemaLabel().properties().isEmpty()) {
        this.propLoaded = true;
        return true;
    }

    // NOTE: only adjacent vertex will reach here
    Iterator<Vertex> vertices = this.graph().adjacentVertex(this.id());
    HugeVertex vertex = (HugeVertex) QueryResults.one(vertices);
    if (vertex == null && !throwIfNotExist) {
        return false;
    }
    E.checkState(vertex != null, "Vertex '%s' does not exist", this.id);
    if (vertex.schemaLabel().undefined()) {
        // Update label to undefined
        this.label = vertex.schemaLabel();
    }
    this.copyProperties(vertex);
    return true;
}
 
示例25
protected <R> R queryByCond(HbaseSession<R> session, ConditionQuery query) {
    if (query.containsScanCondition()) {
        E.checkArgument(query.relations().size() == 1,
                        "Invalid scan with multi conditions: %s", query);
        Relation scan = query.relations().iterator().next();
        Shard shard = (Shard) scan.value();
        return this.queryByRange(session, shard, query.page());
    }
    throw new NotSupportException("query: %s", query);
}
 
示例26
@Override
public HugeEdge readEdge(HugeGraph graph, BackendEntry backendEntry) {
    E.checkNotNull(graph, "serializer graph");
    if (backendEntry == null) {
        return null;
    }

    TableBackendEntry entry = this.convertEntry(backendEntry);
    return this.parseEdge(entry.row(), null, graph);
}
 
示例27
private void checkVertexIdLength(String id) {
    this.encoder.reset();
    this.buffer.clear();
    CoderResult r = this.encoder.encode(CharBuffer.wrap(id.toCharArray()),
                                        this.buffer, true);
    E.checkArgument(r.isUnderflow(),
                    "The vertex id length exceeds limit %s : '%s'",
                    Constants.VERTEX_ID_LIMIT, id);
}
 
示例28
@Override
public void removeDirectory() {
    FileSystem fs = this.fileSystem();
    Path path = new Path(this.directory());
    try {
        E.checkState(fs.exists(path) &&
                     fs.getFileStatus(path).isDirectory(),
                     "The directory does not exist: '%s'",
                     this.directory());
        fs.delete(path, true);
    } catch (IOException e) {
        throw new ToolsException("Failed to delete directory '%s'", path);
    }
}
 
示例29
public static int schemaId(Id id) {
    long l = id.asLong();
    // Currently we limit the schema id to within 4 bytes
    E.checkArgument(Integer.MIN_VALUE <= l && l <= Integer.MAX_VALUE,
                    "Schema id is out of bound: %s", l);
    return (int) l;
}
 
示例30
@Override
public Builder by(String... fields) {
    E.checkArgument(this.indexLabel.fields.isEmpty(),
                    "Not allowed to assign index fields multi times");
    List<String> indexFields = Arrays.asList(fields);
    E.checkArgument(CollectionUtil.allUnique(indexFields),
                    "Invalid index fields %s, which contains some " +
                    "duplicate properties", indexFields);
    this.indexLabel.fields.addAll(indexFields);
    return this;
}