Java Code Examples for com.baidu.hugegraph.util.E#checkState()

The following examples show how to use com.baidu.hugegraph.util.E#checkState() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinarySerializer.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
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));
    }
}
 
Example 2
Source File: HugeVertex.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Watched(prefix = "vertex")
public List<Object> primaryValues() {
    E.checkArgument(this.label.idStrategy() == IdStrategy.PRIMARY_KEY,
                    "The id strategy '%s' don't have primary keys",
                    this.label.idStrategy());
    List<Id> primaryKeys = this.label.primaryKeys();
    E.checkArgument(!primaryKeys.isEmpty(),
                    "Primary key can't be empty for id strategy '%s'",
                    IdStrategy.PRIMARY_KEY);

    List<Object> propValues = new ArrayList<>(primaryKeys.size());
    for (Id pk : primaryKeys) {
        HugeProperty<?> property = this.getProperty(pk);
        E.checkState(property != null,
                     "The value of primary key '%s' can't be null",
                     this.graph().propertyKey(pk).name());
        propValues.add(property.serialValue());
    }
    return propValues;
}
 
Example 3
Source File: ConditionQuery.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
/**
 * This method is only used for secondary index scenario,
 * its relation must be EQ
 * @param fields the user property fields
 * @return the corresponding user property serial values of fields
 */
public String userpropValuesString(List<Id> fields) {
    List<Object> values = new ArrayList<>(fields.size());
    for (Id field : fields) {
        boolean got = false;
        for (Relation r : this.userpropRelations()) {
            if (r.key().equals(field) && !r.isSysprop()) {
                E.checkState(r.relation == RelationType.EQ,
                             "Method userpropValues(List<String>) only " +
                             "used for secondary index, " +
                             "relation must be EQ, but got %s",
                             r.relation());
                values.add(r.serialValue());
                got = true;
            }
        }
        if (!got) {
            throw new BackendException(
                      "No such userprop named '%s' in the query '%s'",
                      field, this);
        }
    }
    return concatValues(values);
}
 
Example 4
Source File: FileLineFetcher.java    From hugegraph-loader with Apache License 2.0 6 votes vote down vote up
public void skipOffset(Readable readable, long offset) {
    if (offset <= 0) {
        return;
    }
    E.checkState(this.reader != null, "The reader shouldn't be null");

    try {
        for (long i = 0L; i < offset; i++) {
            this.reader.readLine();
        }
    } catch (IOException e) {
        throw new LoadException("Failed to skip the first %s lines " +
                                "of file %s, please ensure the file " +
                                "must have at least %s lines",
                                e, offset, readable, offset);
    }
    this.addOffset(offset);
}
 
Example 5
Source File: PaloTables.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Override
protected BackendEntry mergeEntries(BackendEntry e1, BackendEntry e2) {
    MysqlBackendEntry current = (MysqlBackendEntry) e1;
    MysqlBackendEntry next = (MysqlBackendEntry) e2;

    E.checkState(current == null || current.type().isIndex(),
                 "The current entry must be null or INDEX");
    E.checkState(next != null && next.type().isIndex(),
                 "The next entry must be INDEX");

    long maxSize = BackendEntryIterator.INLINE_BATCH_SIZE;
    if (current != null && current.subRows().size() < maxSize) {
        String currentId = this.entryId(current);
        String nextId = this.entryId(next);
        if (currentId.equals(nextId)) {
            current.subRow(next.row());
            return current;
        }
    }
    return next;
}
 
Example 6
Source File: BytesBuffer.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
private void require(int size) {
    // Does need to resize?
    if (this.buffer.limit() - this.buffer.position() >= size) {
        return;
    }
    // Can't resize for wrapped buffer since will change the origin ref
    E.checkState(this.resize, "Can't resize for wrapped buffer");

    // Extra capacity as buffer
    int newcapacity = size + this.buffer.limit() + DEFAULT_CAPACITY;
    E.checkArgument(newcapacity <= MAX_BUFFER_CAPACITY,
                    "Capacity exceeds max buffer capacity: %s",
                    MAX_BUFFER_CAPACITY);
    ByteBuffer newBuffer = ByteBuffer.allocate(newcapacity);
    this.buffer.flip();
    newBuffer.put(this.buffer);
    this.buffer = newBuffer;
}
 
Example 7
Source File: HugeTarget.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
@Override
public Map<String, Object> asMap() {
    E.checkState(this.name != null, "Target name can't be null");
    E.checkState(this.url != null, "Target url can't be null");

    Map<String, Object> map = new HashMap<>();

    map.put(Hidden.unHide(P.NAME), this.name);
    map.put(Hidden.unHide(P.GRAPH), this.graph);
    map.put(Hidden.unHide(P.URL), this.url);

    if (this.resources != null && this.resources != EMPTY) {
        map.put(Hidden.unHide(P.RESS), this.resources);
    }

    return super.asMap(map);
}
 
Example 8
Source File: ConditionQuery.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public void unsetCondition(Object key) {
    for (Iterator<Condition> iter = this.conditions.iterator();
         iter.hasNext();) {
        Condition c = iter.next();
        E.checkState(c.isRelation(), "Can't unset condition '%s'", c);
        if (((Condition.Relation) c).key().equals(key)) {
            iter.remove();
        }
    }
}
 
Example 9
Source File: GraphTransaction.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
@Watched(prefix = "graph")
public <V> void addVertexProperty(HugeVertexProperty<V> prop) {
    // NOTE: this method can also be used to update property

    HugeVertex vertex = prop.element();
    E.checkState(vertex != null,
                 "No owner for updating property '%s'", prop.key());

    // Add property in memory for new created vertex
    if (vertex.fresh()) {
        // The owner will do property update
        vertex.setProperty(prop);
        return;
    }
    // Check is updating property of added/removed vertex
    E.checkArgument(!this.addedVertices.containsKey(vertex.id()) ||
                    this.updatedVertices.containsKey(vertex.id()),
                    "Can't update property '%s' for adding-state vertex",
                    prop.key());
    E.checkArgument(!vertex.removed() &&
                    !this.removedVertices.containsKey(vertex.id()),
                    "Can't update property '%s' for removing-state vertex",
                    prop.key());
    // Check is updating primary key
    List<Id> primaryKeyIds = vertex.schemaLabel().primaryKeys();
    E.checkArgument(!primaryKeyIds.contains(prop.propertyKey().id()),
                    "Can't update primary key: '%s'", prop.key());

    // Do property update
    this.lockForUpdateProperty(vertex.schemaLabel(), prop, () -> {
        // Update old vertex to remove index (without new property)
        this.indexTx.updateVertexIndex(vertex, true);
        // Update(add) vertex property
        this.propertyUpdated(vertex, prop, vertex.setProperty(prop));
    });
}
 
Example 10
Source File: IndexLabel.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public Id indexField() {
    E.checkState(this.indexType.isRange() || this.indexType.isSearch(),
                 "Can't call indexField() for %s index label",
                 this.indexType.string());
    E.checkState(this.indexFields.size() == 1,
                 "There should be only one field in %s index label, " +
                 "but got: %s", this.indexType.string(), this.indexFields);
    return this.indexFields.get(0);
}
 
Example 11
Source File: LocalDirectory.java    From hugegraph-tools with Apache License 2.0 5 votes vote down vote up
private static void removeDirectory(String directory) {
    File dir = new File(directory);
    E.checkState(dir.exists() && dir.isDirectory(),
                 "The directory does not exist: '%s'",
                 dir.getAbsolutePath());
    try {
        FileUtils.deleteDirectory(dir);
    } catch (IOException e) {
        throw new ToolsException("Failed to delete directory '%s'",
                                 dir.getAbsolutePath());
    }
}
 
Example 12
Source File: DataTypeUtil.java    From hugegraph-loader with Apache License 2.0 5 votes vote down vote up
private static Number parseNumber(String key, Object value,
                                  DataType dataType) {
    E.checkState(dataType.isNumber(), "The target data type must be number");

    if (dataType.clazz().isInstance(value)) {
        return (Number) value;
    }
    try {
        switch (dataType) {
            case BYTE:
                return Byte.valueOf(value.toString());
            case INT:
                return Integer.valueOf(value.toString());
            case LONG:
                return parseLong(value.toString());
            case FLOAT:
                return Float.valueOf(value.toString());
            case DOUBLE:
                return Double.valueOf(value.toString());
            default:
                throw new AssertionError(String.format(
                          "Number type only contains Byte, Integer, " +
                          "Long, Float, Double, but got %s",
                          dataType.clazz()));
        }
    } catch (NumberFormatException e) {
        throw new IllegalArgumentException(String.format(
                  "Failed to convert value(key='%s') '%s'(%s) to Number",
                  key, value, value.getClass()), e);
    }
}
 
Example 13
Source File: DataTypeUtil.java    From hugegraph-loader with Apache License 2.0 5 votes vote down vote up
private static Object parseSingleValue(String key, Object rawValue,
                                       DataType dataType,
                                       InputSource source) {
    // Trim space if raw value is string
    Object value = rawValue;
    if (rawValue instanceof String) {
        value = ((String) rawValue).trim();
    }
    if (dataType.isNumber()) {
        return parseNumber(key, value, dataType);
    } else if (dataType.isBoolean()) {
        return parseBoolean(key, value);
    } else if (dataType.isDate()) {
        E.checkState(source instanceof FileSource,
                     "Only accept FileSource when convert String value " +
                     "to Date, but got '%s'", source.getClass().getName());
        String dateFormat = ((FileSource) source).dateFormat();
        String timeZone = ((FileSource) source).timeZone();
        return parseDate(key, value, dateFormat, timeZone);
    } else if (dataType.isUUID()) {
        return parseUUID(key, value);
    }
    E.checkArgument(checkDataType(key, value, dataType),
                    "The value(key='%s') '%s'(%s) is not match with " +
                    "data type %s and can't convert to it",
                    key, value, value.getClass(), dataType);
    return value;
}
 
Example 14
Source File: JsonUtil.java    From hugegraph-loader with Apache License 2.0 5 votes vote down vote up
public static <T> T fromJson(String json, TypeReference<T> typeRef) {
    E.checkState(json != null, "Json value can't be null for '%s'",
                 typeRef.getType());
    try {
        return MAPPER.readValue(json, typeRef);
    } catch (IOException e) {
        LOG.error("Failed to deserialize json", e);
        throw new DeserializeException("Failed to deserialize json", e);
    }
}
 
Example 15
Source File: HugeTask.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
public Map<String, Object> asMap(boolean withDetails) {
    E.checkState(this.type != null, "Task type can't be null");
    E.checkState(this.name != null, "Task name can't be null");

    Map<String, Object> map = new HashMap<>();

    map.put(Hidden.unHide(P.ID), this.id);

    map.put(Hidden.unHide(P.TYPE), this.type);
    map.put(Hidden.unHide(P.NAME), this.name);
    map.put(Hidden.unHide(P.STATUS), this.status.string());
    map.put(Hidden.unHide(P.PROGRESS), this.progress);
    map.put(Hidden.unHide(P.CREATE), this.create);
    map.put(Hidden.unHide(P.RETRIES), this.retries);

    if (this.description != null) {
        map.put(Hidden.unHide(P.DESCRIPTION), this.description);
    }
    if (this.update != null) {
        map.put(Hidden.unHide(P.UPDATE), this.update);
    }
    if (this.dependencies != null) {
        Set<Long> value = this.dependencies.stream().map(Id::asLong)
                                                    .collect(toOrderSet());
        map.put(Hidden.unHide(P.DEPENDENCIES), value);
    }

    if (withDetails) {
        map.put(Hidden.unHide(P.CALLABLE),
                this.callable.getClass().getName());
        if (this.input != null) {
            map.put(Hidden.unHide(P.INPUT), this.input);
        }
        if (this.result != null) {
            map.put(Hidden.unHide(P.RESULT), this.result);
        }
    }

    return map;
}
 
Example 16
Source File: PerfUtil.java    From hugegraph-common with Apache License 2.0 4 votes vote down vote up
public void clear() {
    E.checkState(this.callStack.empty(),
                 "Can't be cleared when the call has not ended yet");
    this.stopwatches.clear();
}
 
Example 17
Source File: StandardHugeGraph.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
private void checkGraphNotClosed() {
    E.checkState(!this.closed, "Graph '%s' has been closed", this);
}
 
Example 18
Source File: RocksDBStdSessions.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
public synchronized ColumnFamilyHandle get() {
    E.checkState(this.handle.isOwningHandle(),
                 "It seems CF has been closed");
    return this.handle;
}
 
Example 19
Source File: RocksDBStore.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
private final void checkDbOpened() {
    E.checkState(this.sessions != null && !this.sessions.closed(),
                 "RocksDB has not been opened");
}
 
Example 20
Source File: CassandraSessionPool.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Override
protected Session newSession() {
    E.checkState(this.cluster != null,
                 "Cassandra cluster has not been initialized");
    return new Session();
}