in asterix-graphix/src/main/java/org/apache/asterix/graphix/metadata/entitytupletranslators/GraphTupleTranslator.java [118:226]
private Schema readGraphSchema(ARecord graph, GraphIdentifier graphIdentifier) throws AsterixException {
Schema.Builder schemaBuilder = new Schema.Builder(graphIdentifier);
// Read in the vertex list.
IAObject verticesObj = GRAPH_RECORD_DETAIL.getObjectForField(graph, FIELD_NAME_VERTICES);
IACursor verticesCursor = ((AOrderedList) verticesObj).getCursor();
while (verticesCursor.next()) {
ARecord vertex = (ARecord) verticesCursor.get();
// Read in the label name.
IAObject labelNameObj = VERTEX_RECORD_DETAIL.getObjectForField(vertex, FIELD_NAME_LABEL);
ElementLabel elementLabel = new ElementLabel(((AString) labelNameObj).getStringValue(), false);
// Read in the primary key fields.
List<List<String>> primaryKeyFields = new ArrayList<>();
IAObject primaryKeyObj = VERTEX_RECORD_DETAIL.getObjectForField(vertex, FIELD_NAME_PRIMARY_KEY);
IACursor primaryKeyCursor = ((AOrderedList) primaryKeyObj).getCursor();
while (primaryKeyCursor.next()) {
IACursor nameCursor = ((AOrderedList) primaryKeyCursor.get()).getCursor();
primaryKeyFields.add(readNameList(nameCursor));
}
// Read in the definition body.
IAObject bodyObj = VERTEX_RECORD_DETAIL.getObjectForField(vertex, FIELD_NAME_BODY);
String definitionBody = ((AString) bodyObj).getStringValue();
// Read in the vertex definition, and perform validation of the metadata record.
schemaBuilder.addVertex(elementLabel, primaryKeyFields, definitionBody);
switch (schemaBuilder.getLastError()) {
case NO_ERROR:
break;
case VERTEX_LABEL_CONFLICT:
throw new AsterixException(ErrorCode.METADATA_ERROR,
"Conflicting vertex label found: " + elementLabel);
default:
throw new AsterixException(ErrorCode.METADATA_ERROR,
"Constructor vertex was not returned, but the error is not a conflicting vertex label!");
}
}
// Read in the edge list.
IAObject edgesObj = GRAPH_RECORD_DETAIL.getObjectForField(graph, FIELD_NAME_EDGES);
IACursor edgesCursor = ((AOrderedList) edgesObj).getCursor();
while (edgesCursor.next()) {
ARecord edge = (ARecord) edgesCursor.get();
// Read in the label name.
IAObject labelNameObj = EDGE_RECORD_DETAIL.getObjectForField(edge, FIELD_NAME_LABEL);
ElementLabel edgeLabel = new ElementLabel(((AString) labelNameObj).getStringValue(), false);
// Read in the destination label name.
IAObject destinationLabelNameObj = EDGE_RECORD_DETAIL.getObjectForField(edge, FIELD_NAME_DESTINATION_LABEL);
AString destinationLabelString = (AString) destinationLabelNameObj;
ElementLabel destinationLabel = new ElementLabel(destinationLabelString.getStringValue(), false);
// Read in the source label name.
IAObject sourceLabelNameObj = EDGE_RECORD_DETAIL.getObjectForField(edge, FIELD_NAME_SOURCE_LABEL);
ElementLabel sourceLabel = new ElementLabel(((AString) sourceLabelNameObj).getStringValue(), false);
// Read in the source key fields.
List<List<String>> sourceKeyFields = new ArrayList<>();
IAObject sourceKeyObj = EDGE_RECORD_DETAIL.getObjectForField(edge, FIELD_NAME_SOURCE_KEY);
IACursor sourceKeyCursor = ((AOrderedList) sourceKeyObj).getCursor();
while (sourceKeyCursor.next()) {
IACursor nameCursor = ((AOrderedList) sourceKeyCursor.get()).getCursor();
sourceKeyFields.add(readNameList(nameCursor));
}
// Read in the destination key fields (this is common to all edge definition records).
List<List<String>> destinationKeyFields = new ArrayList<>();
IAObject destinationKeyObj = EDGE_RECORD_DETAIL.getObjectForField(edge, FIELD_NAME_DESTINATION_KEY);
IACursor destinationKeyCursor = ((AOrderedList) destinationKeyObj).getCursor();
while (destinationKeyCursor.next()) {
IACursor nameCursor = ((AOrderedList) destinationKeyCursor.get()).getCursor();
destinationKeyFields.add(readNameList(nameCursor));
}
// Read in the definition body.
IAObject bodyObj = EDGE_RECORD_DETAIL.getObjectForField(edge, FIELD_NAME_BODY);
String definitionBody = ((AString) bodyObj).getStringValue();
// Finally, read in the edge definition and perform validation of the metadata record.
schemaBuilder.addEdge(edgeLabel, destinationLabel, sourceLabel, destinationKeyFields, sourceKeyFields,
definitionBody);
switch (schemaBuilder.getLastError()) {
case NO_ERROR:
break;
case SOURCE_VERTEX_NOT_FOUND:
throw new AsterixException(ErrorCode.METADATA_ERROR,
"Source vertex " + sourceLabel + " not found in the edge " + edgeLabel + ".");
case DESTINATION_VERTEX_NOT_FOUND:
throw new AsterixException(ErrorCode.METADATA_ERROR,
"Destination vertex " + destinationLabel + " not found in the edge " + edgeLabel + ".");
case EDGE_LABEL_CONFLICT:
throw new AsterixException(ErrorCode.METADATA_ERROR, "Conflicting edge label found: " + edgeLabel);
default:
throw new AsterixException(ErrorCode.METADATA_ERROR,
"Edge constructor was not returned, and an unexpected error encountered");
}
}
return schemaBuilder.build();
}