public HollowObjectSchema findUnionSchema(HollowObjectSchema otherSchema) { if(!getName().equals(otherSchema.getName())) { throw new IllegalArgumentException("Cannot find common schema of two schemas with different names!"); } int totalFields = otherSchema.numFields(); for (String fieldName : fieldNames) { if(otherSchema.getPosition(fieldName) == -1) totalFields++; } PrimaryKey primaryKey = isNullableObjectEquals(this.primaryKey, otherSchema.getPrimaryKey()) ? this.primaryKey : null; HollowObjectSchema unionSchema = new HollowObjectSchema(getName(), totalFields, primaryKey); for(int i=0;i<fieldNames.length;i++) { unionSchema.addField(fieldNames[i], fieldTypes[i], referencedTypes[i]); } for(int i=0;i<otherSchema.numFields();i++) { if(getPosition(otherSchema.getFieldName(i)) == -1) { unionSchema.addField(otherSchema.getFieldName(i), otherSchema.getFieldType(i), otherSchema.getReferencedType(i)); } } return unionSchema; }
public String getReferencedType(String fieldName) { int fieldPosition = getPosition(fieldName); if(fieldPosition == -1) return null; return getReferencedType(fieldPosition); }
private int[] createFieldMapping(HollowObjectSchema unionSchema, HollowObjectSchema individualSchema) { int mapping[] = new int[unionSchema.numFields()]; for(int i=0;i<unionSchema.numFields();i++) { String fieldName = unionSchema.getFieldName(i); mapping[i] = individualSchema.getPosition(fieldName); } return mapping; }
public FieldType getFieldType(String fieldName) { int fieldPosition = getPosition(fieldName); if(fieldPosition == -1) return null; return getFieldType(fieldPosition); }
@Before public void setUp() { typeASchema = new HollowObjectSchema("TypeA", 2); typeASchema.addField("a1", FieldType.INT); typeASchema.addField("a2", FieldType.REFERENCE, "TypeB"); typeBSchema_state1 = new HollowObjectSchema("TypeB", 2); typeBSchema_state1.addField("b1", FieldType.INT); typeBSchema_state1.addField("b2", FieldType.BOOLEAN); typeBSchema_state2 = new HollowObjectSchema("TypeB", 1); typeBSchema_state2.addField("b1", FieldType.INT); super.setUp(); }
public void writeDataTo(ByteDataBuffer buf, HollowObjectSchema translate) { for(int i=0; i < translate.numFields(); i++) { int fieldIndex = schema.getPosition(translate.getFieldName(i)); if(fieldIndex != -1) { writeField(buf, fieldIndex); } else { writeNull(buf, translate.getFieldType(i)); } } }
@Override public boolean equals(Object other) { if(!(other instanceof HollowObjectSchema)) return false; HollowObjectSchema otherSchema = (HollowObjectSchema) other; if(!getName().equals(otherSchema.getName())) return false; if(otherSchema.numFields() != numFields()) return false; if (!isNullableObjectEquals(primaryKey, otherSchema.getPrimaryKey())) return false; for(int i=0;i<numFields();i++) { if(getFieldType(i) != otherSchema.getFieldType(i)) return false; if(!getFieldName(i).equals(otherSchema.getFieldName(i))) return false; } return true; }
@Test public void filterSchema() { HollowObjectSchema s1 = new HollowObjectSchema("Test", 2, "F2"); s1.addField("F1", FieldType.INT); s1.addField("F2", FieldType.LONG); Assert.assertEquals(2, s1.numFields()); HollowFilterConfig filter = new HollowFilterConfig(); filter.addField("Test", "F2"); HollowObjectSchema s2 = s1.filterSchema(filter); Assert.assertEquals(1, s2.numFields()); Assert.assertEquals("F2", s2.getFieldName(0)); Assert.assertEquals(s1.getPrimaryKey(), s2.getPrimaryKey()); }
private void addSchemaField(HollowObjectSchema schema, HollowObjectSchema keySchema, String keyField, String[] keyFieldParts, int keyFieldPartPosition) { int schemaPosition = schema.getPosition(keyFieldParts[keyFieldPartPosition]); if(keyFieldPartPosition < keyFieldParts.length - 1) { HollowObjectSchema nextPartSchema = (HollowObjectSchema) schema.getReferencedTypeState(schemaPosition).getSchema(); addSchemaField(nextPartSchema, keySchema, keyField, keyFieldParts, keyFieldPartPosition + 1); } else { keySchema.addField(keyField, schema.getFieldType(schemaPosition), schema.getReferencedType(schemaPosition)); } }
public HollowObjectSampler(HollowObjectSchema schema, HollowSamplingDirector director) { this.typeName = schema.getName(); this.sampleCounts = new long[schema.numFields()]; HollowSamplingDirector[] samplingDirectors = new HollowSamplingDirector[schema.numFields()]; Arrays.fill(samplingDirectors, director); String fieldNames[] = new String[schema.numFields()]; for(int i=0;i<fieldNames.length;i++) { fieldNames[i] = schema.getFieldName(i); } this.fieldNames = fieldNames; this.samplingDirectors = samplingDirectors; }
private String fieldType(int i) { switch (schema.getFieldType(i)) { case BOOLEAN: case BYTES: case DOUBLE: case FLOAT: case INT: case LONG: case STRING: return getJavaScalarType(schema.getFieldType(i)); case REFERENCE: return buildFieldType(dataset.getSchema(schema.getReferencedType(i))); default: throw new IllegalArgumentException("Field type is unrecognized: " + schema.getFieldType(i)); } }
@Override public boolean isNull(int ordinal, String fieldName) { try { HollowObjectTypeDataAccess dataAccess = getTypeDataAccess(); int fieldIndex = getSchema().getPosition(fieldName); if(fieldIndex == -1) return missingDataHandler().handleIsNull(getSchema().getName(), ordinal, fieldName); return dataAccess.isNull(ordinal, fieldIndex); } catch(Exception ex) { throw new RuntimeException(String.format("Unable to handle ordinal=%s, fieldName=%s", ordinal, fieldName), ex); } }
private boolean isSearchable(HollowObjectSchema schema, int fieldNumber) { if(schema.getFieldType(fieldNumber) == FieldType.REFERENCE) { if(schema.getReferencedTypeState(fieldNumber).getSchema().getSchemaType() != SchemaType.OBJECT) return false; HollowObjectSchema refObjSchema = (HollowObjectSchema)schema.getReferencedTypeState(fieldNumber).getSchema(); if(refObjSchema.numFields() != 1) return false; return isSearchable(refObjSchema, 0); } return true; }
private String generateReferenceFieldAccessors(int fieldNum) { StringBuilder builder = new StringBuilder(); String fieldName = substituteInvalidChars(objectSchema.getFieldName(fieldNum)); String referencedType = substituteInvalidChars(objectSchema.getReferencedType(fieldNum)); builder.append(" public int get"+ uppercase(fieldName) + "Ordinal(int ordinal) {\n"); builder.append(" if(fieldIndex[" + fieldNum +"] == -1)\n"); builder.append(" return missingDataHandler().handleReferencedOrdinal(\"").append(objectSchema.getName()).append("\", ordinal, \"").append(fieldName).append("\");\n"); builder.append(" return getTypeDataAccess().readOrdinal(ordinal, fieldIndex[" + fieldNum + "]);\n"); builder.append(" }\n\n"); builder.append(" public " + typeAPIClassname(referencedType) + " get" + uppercase(fieldName) + "TypeAPI() {\n"); builder.append(" return getAPI().get").append(uppercase(referencedType)).append("TypeAPI();\n"); builder.append(" }"); return builder.toString(); }
public SchemaDisplayField(String fieldPath, HollowObjectSchema parentSchema, int fieldNumber) { this.fieldPath = fieldPath; this.fieldName = parentSchema.getFieldName(fieldNumber); this.fieldType = parentSchema.getFieldType(fieldNumber); this.isSearchable = isSearchable(parentSchema, fieldNumber); this.referencedType = fieldType == FieldType.REFERENCE ? new SchemaDisplay(parentSchema.getReferencedTypeState(fieldNumber).getSchema(), fieldPath) : null; }
private String getKeyFieldType(String fieldPath) { try { HollowObjectSchema keySchema = (HollowObjectSchema)dataset.getSchema(schema.getKeyType()); String fieldPathElements[] = fieldPath.split("\\."); int idx = 0; while(idx < fieldPathElements.length-1) { keySchema = (HollowObjectSchema)dataset.getSchema(keySchema.getReferencedType(fieldPathElements[idx])); idx++; } FieldType fieldType = keySchema.getFieldType(keySchema.getPosition(fieldPathElements[idx])); return HollowCodeGenerationUtils.getJavaBoxedType(fieldType); } catch(Throwable th) { } throw new IllegalArgumentException("Field path '" + fieldPath + "' specified incorrectly for type: " + schema.getName()); }
private static boolean referencesGenericStringSchema(HollowSchema schema) { if(schema instanceof HollowObjectSchema) { HollowObjectSchema objSchema = (HollowObjectSchema)schema; for(int i=0;i<objSchema.numFields();i++) { if("String".equals(objSchema.getReferencedType(i))) { return true; } } } return false; }
private ObjectMappedFieldPath addPathMapping(String fieldName, String[] fieldPaths, RemappingBuilderInstruction instruction, int idx) { if(idx < fieldPaths.length - 1) { RemappingBuilderInstruction childInstruction = instruction.childrenRecs.get(fieldPaths[idx]); HollowObjectSchema schema = instruction.typeState.getSchema(); String referencedType = schema.getReferencedType(fieldPaths[idx]); if(childInstruction == null) { HollowObjectTypeWriteState childTypeState = (HollowObjectTypeWriteState) stateEngine.getTypeState(referencedType); HollowObjectWriteRecord childWriteRec = getWriteRecord(childTypeState.getSchema()); childInstruction = new RemappingBuilderInstruction(childWriteRec, childTypeState); instruction.addChildInstruction(fieldPaths[idx], childInstruction); } return addPathMapping(fieldName, fieldPaths, childInstruction, idx+1); } HollowObjectSchema schema = instruction.rec.getSchema(); String remappedFieldName = fieldPaths[idx]; return new ObjectMappedFieldPath(instruction.rec, remappedFieldName, typeName, fieldName, schema.getPosition(remappedFieldName), findFieldProcessor(typeName, fieldName, schema.getName(), remappedFieldName)); }
/** * Returns a field name that is same for use as a java variable. */ private String getFieldName(int index) { return substituteInvalidChars(schema.getFieldName(index)); }
private void addNullRecord(int ordinal, long recordBitOffset, FixedLengthElementArray fixedLengthLongArray, ByteDataBuffer varLengthByteArrays[]) { for(int fieldIndex=0; fieldIndex < getSchema().numFields(); fieldIndex++) { if(getSchema().getFieldType(fieldIndex) == FieldType.STRING || getSchema().getFieldType(fieldIndex) == FieldType.BYTES) { long fieldBitOffset = recordBitOffset + fieldStats.getFieldBitOffset(fieldIndex); int bitsPerElement = fieldStats.getMaxBitsForField(fieldIndex); long currentPointer = varLengthByteArrays[fieldIndex] == null ? 0 : varLengthByteArrays[fieldIndex].length(); fixedLengthLongArray.setElementValue(fieldBitOffset, bitsPerElement, currentPointer); } } }