public static boolean hasGenericDataType(PropertyKey key) { return key.dataType().equals(Object.class); } }
public StandardKeyInformation(PropertyKey key, Parameter... parameters) { Preconditions.checkNotNull(key); Preconditions.checkNotNull(parameters); this.dataType = key.dataType(); this.parameters = parameters; this.cardinality = key.cardinality(); }
private static long[] checkSignature(List<PropertyKey> sig) { Preconditions.checkArgument(sig.size() == (Sets.newHashSet(sig)).size(), "Signature and sort key cannot contain duplicate types"); long[] signature = new long[sig.size()]; for (int i = 0; i < sig.size(); i++) { PropertyKey key = sig.get(i); Preconditions.checkNotNull(key); Preconditions.checkArgument(!((PropertyKey) key).dataType().equals(Object.class), "Signature and sort keys must have a proper declared datatype: %s", key.name()); signature[i] = key.longId(); } return signature; }
private long[] checkSortKey(List<PropertyKey> sig) { for (PropertyKey key : sig) { Preconditions.checkArgument(attributeHandler.isOrderPreservingDatatype(key.dataType()), "Key must have an order-preserving data type to be used as sort key: " + key); } return checkSignature(sig); }
private void writePropertyValue(DataOutput out, PropertyKey key, Object value, InlineType inlineType) { if (AttributeUtil.hasGenericDataType(key)) { assert !inlineType.writeByteOrdered(); out.writeClassAndObject(value); } else { assert value==null || value.getClass().equals(key.dataType()); if (inlineType.writeByteOrdered()) out.writeObjectByteOrder(value, key.dataType()); else out.writeObject(value, key.dataType()); } }
private String printPropertyKeys(boolean calledDirectly) { StringBuilder sb = new StringBuilder(); String pattern = "%-30s | %-11s | %-50s |\n"; Iterable<PropertyKey> keys = getRelationTypes(PropertyKey.class); boolean hasResults = false; if (calledDirectly) { sb.append(FIRSTDASH); } else { sb.append(DASHBREAK); } sb.append(String.format(pattern, "Property Key Name", "Cardinality", "Data Type")); sb.append(DASHBREAK); for (PropertyKey key: keys) { hasResults = true; sb.append(String.format(pattern, key.name(), key.cardinality(), key.dataType())); } if (hasResults && calledDirectly) { sb.append(DASHBREAK); } return sb.toString(); }
@Override public Q orderBy(String keyName, org.apache.tinkerpop.gremlin.process.traversal.Order order) { Preconditions.checkArgument(schemaInspector.containsPropertyKey(keyName), "Provided key does not exist: %s", keyName); PropertyKey key = schemaInspector.getPropertyKey(keyName); Preconditions.checkArgument(key != null && order != null, "Need to specify and key and an order"); Preconditions.checkArgument(Comparable.class.isAssignableFrom(key.dataType()), "Can only order on keys with comparable data type. [%s] has datatype [%s]", key.name(), key.dataType()); Preconditions.checkArgument(!(key instanceof SystemRelationType), "Cannot use system types in ordering: %s", key); Preconditions.checkArgument(!orders.containsKey(key)); Preconditions.checkArgument(orders.isEmpty(), "Only a single sort order is supported on vertex queries"); orders.add(key, Order.convert(order)); return getThis(); }
@Override public GraphCentricQueryBuilder orderBy(String keyName, org.apache.tinkerpop.gremlin.process.traversal.Order order) { Preconditions.checkArgument(tx.containsPropertyKey(keyName),"Provided key does not exist: %s",keyName); final PropertyKey key = tx.getPropertyKey(keyName); Preconditions.checkArgument(key!=null && order!=null,"Need to specify and key and an order"); Preconditions.checkArgument(Comparable.class.isAssignableFrom(key.dataType()), "Can only order on keys with comparable data type. [%s] has datatype [%s]", key.name(), key.dataType()); Preconditions.checkArgument(key.cardinality()== Cardinality.SINGLE, "Ordering is undefined on multi-valued key [%s]", key.name()); Preconditions.checkArgument(!orders.containsKey(key)); orders.add(key, Order.convert(order)); return this; }
private Object readPropertyValue(ReadBuffer read, PropertyKey key, InlineType inlineType) { if (AttributeUtil.hasGenericDataType(key)) { return serializer.readClassAndObject(read); } else { if (inlineType.writeByteOrdered()) return serializer.readObjectByteOrder(read, key.dataType()); else return serializer.readObject(read, key.dataType()); } }
@Test public void testAutoSchemaMakerForVertexPropertyDataType(){ JanusGraphVertex v1 = tx.addVertex("user"); v1.property("id", 10); v1.property("created", new Date()); PropertyKey idPropertyKey = tx.getPropertyKey("id"); assertEquals("Data type not identified correctly by auto schema maker", Integer.class, idPropertyKey.dataType()); PropertyKey createdPropertyKey = tx.getPropertyKey("created"); assertEquals("Data type not identified properly by auto schema maker", Date.class, createdPropertyKey.dataType()); }
@Test public void testAutoSchemaMakerForEdgePropertyDataType(){ JanusGraphVertex v1 = tx.addVertex("user"); JanusGraphVertex v2 = tx.addVertex("user"); v1.addEdge("knows", v2, "id", 10, "created", new Date()); PropertyKey idPropertyKey = tx.getPropertyKey("id"); assertEquals("Data type not identified correctly by auto schema maker", Integer.class, idPropertyKey.dataType()); PropertyKey createdPropertyKey = tx.getPropertyKey("created"); assertEquals("Data type not identified correctly by auto schema maker", Date.class, createdPropertyKey.dataType()); }
public PropertyKeyDefinition(PropertyKey key) { this(key.name(),key.longId(),key.cardinality(),key.dataType()); }
public final Object verifyAttribute(PropertyKey key, Object attribute) { if (attribute==null) throw new SchemaViolationException("Property value cannot be null"); Class<?> datatype = key.dataType(); if (datatype.equals(Object.class)) { if (!attributeHandler.validDataType(attribute.getClass())) { throw Property.Exceptions.dataTypeOfPropertyValueNotSupported(attribute); } return attribute; } else { if (!attribute.getClass().equals(datatype)) { Object converted = null; try { converted = attributeHandler.convert(datatype, attribute); } catch (IllegalArgumentException e) { //Just means that data could not be converted } if (converted == null) throw new SchemaViolationException( "Value [%s] is not an instance of the expected data type for property key [%s] and cannot be converted. Expected: %s, found: %s", attribute, key.name(), datatype, attribute.getClass()); attribute = converted; } assert (attribute.getClass().equals(datatype)); attributeHandler.verifyAttribute(datatype, attribute); return attribute; } }
public static GraphCentricQueryBuilder getQuery(CompositeIndexType index, Object[] values, StandardJanusGraphTx tx) { Preconditions.checkArgument(index != null && values != null && values.length > 0 && tx != null); Preconditions.checkArgument(values.length==index.getFieldKeys().length); GraphCentricQueryBuilder gb = tx.query(); IndexField[] fields = index.getFieldKeys(); for (int i = 0; i <fields.length; i++) { IndexField f = fields[i]; Object value = values[i]; Preconditions.checkNotNull(value); PropertyKey key = f.getFieldKey(); Preconditions.checkArgument(key.dataType().equals(value.getClass()),"Incompatible data types for: " + value); gb.has(key, Cmp.EQUAL,value); } if (index.hasSchemaTypeConstraint()) { gb.has(ImplicitKey.LABEL,Cmp.EQUAL,index.getSchemaTypeConstraint().name()); } return gb; } }
private StaticBuffer getIndexKey(CompositeIndexType index, Object[] values) { final DataOutput out = serializer.getDataOutput(8*DEFAULT_OBJECT_BYTELEN + 8); VariableLong.writePositive(out, index.getID()); final IndexField[] fields = index.getFieldKeys(); Preconditions.checkArgument(fields.length>0 && fields.length==values.length); for (int i = 0; i < fields.length; i++) { final IndexField f = fields[i]; final Object value = values[i]; Preconditions.checkNotNull(value); if (AttributeUtil.hasGenericDataType(f.getFieldKey())) { out.writeClassAndObject(value); } else { assert value.getClass().equals(f.getFieldKey().dataType()) : value.getClass() + " - " + f.getFieldKey().dataType(); out.writeObjectNotNull(value); } } StaticBuffer key = out.getStaticBuffer(); if (hashKeys) key = HashingUtil.hashPrefixKey(hashLength,key); return key; }
public IndexQuery getQuery(final MixedIndexType index, final Condition condition, final OrderList orders) { final Condition newCondition = ConditionUtil.literalTransformation(condition, new Function<Condition<JanusGraphElement>, Condition<JanusGraphElement>>() { @Nullable @Override public Condition<JanusGraphElement> apply(final Condition<JanusGraphElement> condition) { Preconditions.checkArgument(condition instanceof PredicateCondition); final PredicateCondition pc = (PredicateCondition) condition; final PropertyKey key = (PropertyKey) pc.getKey(); return new PredicateCondition<>(key2Field(index, key), pc.getPredicate(), pc.getValue()); } }); ImmutableList<IndexQuery.OrderEntry> newOrders = IndexQuery.NO_ORDER; if (!orders.isEmpty() && GraphCentricQueryBuilder.indexCoversOrder(index,orders)) { final ImmutableList.Builder<IndexQuery.OrderEntry> lb = ImmutableList.builder(); for (int i = 0; i < orders.size(); i++) { lb.add(new IndexQuery.OrderEntry(key2Field(index,orders.getKey(i)), orders.getOrder(i), orders.getKey(i).dataType())); } newOrders = lb.build(); } return new IndexQuery(index.getStoreName(), newCondition, newOrders); }
assert direction == Direction.OUT; StandardPropertyKeyMaker lm = (StandardPropertyKeyMaker) transaction.makePropertyKey(composedName); lm.dataType(((PropertyKey) type).dataType()); maker = lm;
assert key.dataType().equals(p.value().getClass()) : key + " -> " + p; values.add(new RecordEntry(p));
if (!o1.equals(o2)) { final int objectCompare; if (Comparable.class.isAssignableFrom(((PropertyKey) t1).dataType())) { objectCompare = ((Comparable) o1).compareTo(o2); } else {
static boolean validJanusGraphOrder(OrderGlobalStep orderGlobalStep, Traversal rootTraversal, boolean isVertexOrder) { final List<Pair<Traversal.Admin, Object>> comparators = orderGlobalStep.getComparators(); for(final Pair<Traversal.Admin, Object> comp : comparators) { final String key; if (comp.getValue0() instanceof ElementValueTraversal && comp.getValue1() instanceof Order) { key = ((ElementValueTraversal) comp.getValue0()).getPropertyKey(); } else if (comp.getValue1() instanceof ElementValueComparator) { final ElementValueComparator evc = (ElementValueComparator) comp.getValue1(); if (!(evc.getValueComparator() instanceof Order)) return false; key = evc.getPropertyKey(); } else { // do not fold comparators that include nested traversals that are not simple ElementValues return false; } final JanusGraphTransaction tx = JanusGraphTraversalUtil.getTx(rootTraversal.asAdmin()); final PropertyKey pKey = tx.getPropertyKey(key); if (pKey == null || !(Comparable.class.isAssignableFrom(pKey.dataType())) || (isVertexOrder && pKey.cardinality() != Cardinality.SINGLE)) { return false; } } return true; }