Skip to content

Creating Element Schema

sbarzilay edited this page Jan 3, 2018 · 2 revisions

Creating Element Schemas

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(); }
}
  1. fromFields(Map<String, Object> fields) - Returns a collection of elements from source data fields.
  2. toFields(E element) - Converts an element to a source data fields.
  3. toFields(Set<String> propertyKeys) - Converts property keys to fields.
  4. toPredicates(PredicatesHolder predicatesHolder) - Converts predicates to match source data.
  5. getFieldByPropertyKey(String key) - Returns a field name.
  6. 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.

  1. createElement(Map<String, Object> fields) - Creates an element from source.
  2. toPredicates(List<? extends Vertex> vertices) - Returns a predicate from a list of vertices.

Loading Your Element Schemas

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();
}
  1. init(UniGraph graph, JSONObject configuration) - returns a set of query controllers that each query controller holds element schemas.
  2. providerBuilders() - Returns a list of property schema builders that are specific for this provider.
  3. close() - Closes any resources used.

A good example for this is ElasticSourceProvider.