From 0ec2fbb15797bbef6803d22b1a71b2d30b0faaa9 Mon Sep 17 00:00:00 2001 From: Benjamin Anderson Date: Tue, 19 Jan 2016 12:17:19 -0800 Subject: [PATCH] Add signatures for value-based type defaults The previous implementation only provided property key names to DefaultSchemaMaker instances, which limits users' ability to customize behavior. This changeset also passes the property key value down to the DefaultSchemaMaker, allowing more fine-grained analysis and potentially better decisions around default types. --- .../titan/core/schema/DefaultSchemaMaker.java | 4 ++++ .../titan/core/schema/SchemaInspector.java | 4 ++++ .../titan/graphdb/transaction/StandardTitanTx.java | 11 +++++++++++ .../titan/graphdb/vertices/AbstractVertex.java | 4 ++-- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/titan-core/src/main/java/com/thinkaurelius/titan/core/schema/DefaultSchemaMaker.java b/titan-core/src/main/java/com/thinkaurelius/titan/core/schema/DefaultSchemaMaker.java index 8245cbd4ea..13f2b1da25 100644 --- a/titan-core/src/main/java/com/thinkaurelius/titan/core/schema/DefaultSchemaMaker.java +++ b/titan-core/src/main/java/com/thinkaurelius/titan/core/schema/DefaultSchemaMaker.java @@ -47,6 +47,10 @@ public default PropertyKey makePropertyKey(PropertyKeyMaker factory) { return factory.cardinality(defaultPropertyCardinality(factory.getName())).dataType(Object.class).make(); } + public default PropertyKey makePropertyKey(PropertyKeyMaker factory, Object value) { + return makePropertyKey(factory); + } + /** * Creates a new vertex label with the default settings against the provided {@link VertexLabelMaker}. * diff --git a/titan-core/src/main/java/com/thinkaurelius/titan/core/schema/SchemaInspector.java b/titan-core/src/main/java/com/thinkaurelius/titan/core/schema/SchemaInspector.java index 1acaf6e869..cb56562f85 100644 --- a/titan-core/src/main/java/com/thinkaurelius/titan/core/schema/SchemaInspector.java +++ b/titan-core/src/main/java/com/thinkaurelius/titan/core/schema/SchemaInspector.java @@ -53,6 +53,10 @@ public interface SchemaInspector { */ public PropertyKey getOrCreatePropertyKey(String name); + public default PropertyKey getOrCreatePropertyKey(String name, Object value) { + return getOrCreatePropertyKey(name); + } + /** * Returns the property key with the given name. If it does not exist, NULL is returned * diff --git a/titan-core/src/main/java/com/thinkaurelius/titan/graphdb/transaction/StandardTitanTx.java b/titan-core/src/main/java/com/thinkaurelius/titan/graphdb/transaction/StandardTitanTx.java index 7f6f96fd54..63fc981332 100644 --- a/titan-core/src/main/java/com/thinkaurelius/titan/graphdb/transaction/StandardTitanTx.java +++ b/titan-core/src/main/java/com/thinkaurelius/titan/graphdb/transaction/StandardTitanTx.java @@ -927,6 +927,17 @@ public PropertyKey getOrCreatePropertyKey(String name) { throw new IllegalArgumentException("The type of given name is not a key: " + name); } + @Override + public PropertyKey getOrCreatePropertyKey(String name, Object value) { + RelationType et = getRelationType(name); + if (et == null) { + return config.getAutoSchemaMaker().makePropertyKey(makePropertyKey(name), value); + } else if (et.isPropertyKey()) { + return (PropertyKey) et; + } else + throw new IllegalArgumentException("The type of given name is not a key: " + name); + } + @Override public EdgeLabel getEdgeLabel(String name) { RelationType el = getRelationType(name); diff --git a/titan-core/src/main/java/com/thinkaurelius/titan/graphdb/vertices/AbstractVertex.java b/titan-core/src/main/java/com/thinkaurelius/titan/graphdb/vertices/AbstractVertex.java index 01981956b4..a621deb3c8 100644 --- a/titan-core/src/main/java/com/thinkaurelius/titan/graphdb/vertices/AbstractVertex.java +++ b/titan-core/src/main/java/com/thinkaurelius/titan/graphdb/vertices/AbstractVertex.java @@ -132,14 +132,14 @@ public O valueOrNull(PropertyKey key) { */ public TitanVertexProperty property(final String key, final V value, final Object... keyValues) { - TitanVertexProperty p = tx().addProperty(it(), tx().getOrCreatePropertyKey(key), value); + TitanVertexProperty p = tx().addProperty(it(), tx().getOrCreatePropertyKey(key, value), value); ElementHelper.attachProperties(p,keyValues); return p; } @Override public TitanVertexProperty property(final VertexProperty.Cardinality cardinality, final String key, final V value, final Object... keyValues) { - TitanVertexProperty p = tx().addProperty(cardinality, it(), tx().getOrCreatePropertyKey(key), value); + TitanVertexProperty p = tx().addProperty(cardinality, it(), tx().getOrCreatePropertyKey(key, value), value); ElementHelper.attachProperties(p,keyValues); return p; }