private static int compare(Relation first, Relation second) { Object firstValue = first.value(); Object secondValue = second.value(); if (firstValue instanceof Number && secondValue instanceof Number) { return NumericUtil.compareNumber(firstValue, (Number) secondValue); } else if (firstValue instanceof Date && secondValue instanceof Date) { return ((Date) firstValue).compareTo((Date) secondValue); } else { throw new IllegalArgumentException(String.format( "Can't compare between %s and %s", first, second)); } }
public Object condition(Object key) { this.checkFlattened(); List<Object> values = new ArrayList<>(); for (Condition c : this.conditions) { if (c.isRelation()) { Condition.Relation r = (Condition.Relation) c; if (r.key().equals(key) && r.relation() == RelationType.EQ) { values.add(r.value()); } } } if (values.isEmpty()) { return null; } E.checkState(values.size() == 1, "Illegal key '%s' with more than one value", key); return values.get(0); }
protected BackendColumnIterator queryByCond(Session 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); } throw new NotSupportException("query: %s", query); }
assert condition instanceof Condition.Relation; Condition.Relation r = (Condition.Relation) condition; Number value = NumericUtil.convertToNumber(r.value()); Condition.Relation sys = new Condition.SyspropRelation( HugeKeys.FIELD_VALUES,
@Override protected Query writeQueryCondition(Query query) { ConditionQuery result = (ConditionQuery) query; // No user-prop when serialize assert result.allSysprop(); for (Condition.Relation r : result.relations()) { // Serialize key if (query.resultType().isSchema()) { r.serialKey(((HugeKeys) r.key()).string()); } else { r.serialKey(formatSyspropName((HugeKeys) r.key())); } if (r.value() instanceof Id) { // Serialize id value r.serialValue(writeId((Id) r.value())); } else { // Serialize other type value r.serialValue(JsonUtil.toJson(r.value())); } if (r.relation() == Condition.RelationType.CONTAINS_KEY) { // Serialize has-key String key = (String) r.serialValue(); r.serialValue(formatPropertyName(key)); } } return result; }
@Override protected Query writeQueryEdgeCondition(Query query) { ConditionQuery result = (ConditionQuery) query; for (Condition.Relation r : result.relations()) { Object value = r.value(); if (value instanceof Id) { if (r.key() == HugeKeys.OWNER_VERTEX || r.key() == HugeKeys.OTHER_VERTEX) { // Serialize vertex id r.serialValue(IdUtil.writeString((Id) value)); } else { // Serialize label id r.serialValue(((Id) value).asObject()); } } else if (value instanceof Directions) { r.serialValue(((Directions) value).code()); } } return null; }
private static Condition convIn2Or(Relation relation) { assert relation.relation() == Condition.RelationType.IN; Object key = relation.key(); @SuppressWarnings("unchecked") List<Object> values = (List<Object>) relation.value(); Condition cond, conds = null; for (Object value : values) { if (key instanceof HugeKeys) { cond = Condition.eq((HugeKeys) key, value); } else { cond = Condition.eq((Id) key, value); } conds = conds == null ? cond : Condition.or(conds, cond); } return conds; }
private static Condition convNotin2And(Relation relation) { assert relation.relation() == Condition.RelationType.NOT_IN; Object key = relation.key(); @SuppressWarnings("unchecked") List<Object> values = (List<Object>) relation.value(); Condition cond; Condition conds = null; for (Object value : values) { if (key instanceof HugeKeys) { cond = Condition.neq((HugeKeys) key, value); } else { cond = Condition.neq((Id) key, value); } conds = conds == null ? cond : Condition.and(conds, cond); } return conds; }
public Object serialValue() { return this.serialValue != null ? this.serialValue : this.value(); }
public ConditionQuery query(Condition condition) { // Query by id (HugeGraph-259) if (condition instanceof Relation) { Relation relation = (Relation) condition; if (relation.key().equals(HugeKeys.ID) && relation.relation() == RelationType.EQ) { E.checkArgument(relation.value() instanceof Id, "Invalid id value '%s'", relation.value()); super.query((Id) relation.value()); return this; } } this.conditions.add(condition); return this; }
@Override protected Query writeQueryCondition(Query query) { if (query.resultType().isGraph()) { ConditionQuery result = (ConditionQuery) query; // No user-prop when serialize assert result.allSysprop(); for (Condition.Relation r : result.relations()) { if (r.relation() == Condition.RelationType.CONTAINS) { r.serialValue(JsonUtil.toJson(r.value())); } } } return query; }
protected RowIterator queryByCond(Session 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); } throw new NotSupportException("query: %s", query); }
@Override public int hashCode() { return this.type().hashCode() ^ this.relation().hashCode() ^ this.key().hashCode() ^ this.value().hashCode(); }
private static Condition convTextContainsAny2Or(Relation relation) { assert relation.relation() == Condition.RelationType.TEXT_CONTAINS_ANY; @SuppressWarnings("unchecked") Collection<String> words = (Collection<String>) relation.value(); Condition cond, conds = null; for (String word : words) { assert relation.key() instanceof Id; cond = Condition.textContains((Id) relation.key(), word); conds = conds == null ? cond : Condition.or(conds, cond); } return conds; }