-
Notifications
You must be signed in to change notification settings - Fork 35
Creating Element Schema
sbarzilay edited this page Jan 3, 2018
·
2 revisions
In order to create an element schema you have to implement the ElementSchema
interface.
Which consists of:
public interface ElementSchema<E extends Element> {
Collection<E> fromFields(Map<String, Object> fields);
Map<String, Object> toFields(E element);
Set<String> toFields(Set<String> propertyKeys);
PredicatesHolder toPredicates(PredicatesHolder predicatesHolder);
String getFieldByPropertyKey(String key);
default Set<ElementSchema> getChildSchemas() { return Collections.emptySet(); }
}
-
fromFields(Map<String, Object> fields)
- Returns a collection of elements from source data fields. -
toFields(E element)
- Converts an element to a source data fields. -
toFields(Set<String> propertyKeys)
- Converts property keys to fields. -
toPredicates(PredicatesHolder predicatesHolder)
- Converts predicates to match source data. -
getFieldByPropertyKey(String key)
- Returns a field name. -
Set<ElementSchema> getChildSchemas()
- Returns all child schemas.
And either VertexSchema
or EdgeSchema
.
public interface VertexSchema extends ElementSchema<Vertex> {
@Override
default Collection<Vertex> fromFields(Map<String, Object> fields) {
Vertex element = createElement(fields);
if(element == null) return null;
return Collections.singleton(element);
}
Vertex createElement(Map<String, Object> fields);
default PredicatesHolder toPredicates(List<? extends Vertex> vertices) {
if(vertices == null || vertices.size() == 0) return PredicatesHolderFactory.abort();
HashSet<PredicatesHolder> predicates = new HashSet<>();
vertices.stream().collect(Collectors.groupingBy(Vertex::label)).forEach((label, labelVertices) -> {
HasContainer labelPredicate = new HasContainer(T.label.getAccessor(), P.eq(label));
HasContainer ids = new HasContainer(T.id.getAccessor(),
P.within(labelVertices.stream().map(Vertex::id).collect(Collectors.toSet())));
PredicatesHolder labelPredicates = PredicatesHolderFactory.and(ids, labelPredicate);
PredicatesHolder toPredicates = toPredicates(labelPredicates);
predicates.add(toPredicates);
});
return PredicatesHolderFactory.or(predicates);
}
}
Examples: DocVertexSchema, DocEdgeSchema, RowVertexSchema, RowEdgeSchema.
-
createElement(Map<String, Object> fields)
- Creates an element from source. -
toPredicates(List<? extends Vertex> vertices)
- Returns a predicate from a list of vertices.
To load your schemas you need to implement the SourceProvider
interface.
Which consists of:
public interface SourceProvider {
Set<UniQueryController> init(UniGraph graph, JSONObject configuration) throws Exception;
default List<PropertySchema.PropertySchemaBuilder> providerBuilders() {return Collections.emptyList();}
void close();
}
-
init(UniGraph graph, JSONObject configuration)
- returns a set of query controllers that each query controller holds element schemas. -
providerBuilders()
- Returns a list of property schema builders that are specific for this provider. -
close()
- Closes any resources used.
A good example for this is ElasticSourceProvider.