public HollowPrimaryKeyIndexGenerator(HollowDataset dataset, String packageName, String apiClassname, HollowObjectSchema schema, CodeGeneratorConfig config) { super(packageName, apiClassname, schema, dataset, config); this.pk = schema.getPrimaryKey(); isGenSimpleConstructor = true; isParameterizedConstructorPublic = false; isAutoListenToDataRefresh = false; isImplementsUniqueKeyIndex = false; }
private int[][] calculatePrimaryKeyFieldPathIdx(int[][] primaryKeyFieldPathIdx) { if(schema.getPrimaryKey() == null) throw new IllegalArgumentException("Type " + typeName + " does not have a primary key defined"); primaryKeyFieldPathIdx = new int[schema.getPrimaryKey().numFields()][]; for(int i=0;i<primaryKeyFieldPathIdx.length;i++) primaryKeyFieldPathIdx[i] = schema.getPrimaryKey().getFieldPathIndex(parentMapper.getStateEngine(), i); return primaryKeyFieldPathIdx; }
private PrimaryKey getPrimaryKey(HollowSchema schema) { if(schema.getSchemaType() == SchemaType.OBJECT) return ((HollowObjectSchema)schema).getPrimaryKey(); return null; }
private void generateHollowPrimaryKeyAnnotation(StringBuilder classBodyBuilder) { PrimaryKey primaryKey = schema.getPrimaryKey(); if (primaryKey == null) { return; } importClasses.add(HollowPrimaryKey.class); classBodyBuilder.append("@HollowPrimaryKey(fields={"); for (int i = 0; i < primaryKey.numFields(); i++) { if (i > 0) { classBodyBuilder.append(", "); } classBodyBuilder.append("\"").append(primaryKey.getFieldPath(i)).append("\""); } classBodyBuilder.append("})\n"); }
String[] getDefaultElementHashKey() { PrimaryKey pKey = schema.getPrimaryKey(); if (pKey != null) return pKey.getFieldPaths(); if(mappedFields.size() == 1) { MappedField singleField = mappedFields.get(0); if(singleField.getFieldType() != MappedFieldType.REFERENCE) return new String[] { singleField.getFieldName() }; } return null; }
private static PrimaryKey createPrimaryKey(HollowReadStateEngine stateEngine, String type, String... fieldPaths) { if (fieldPaths != null && fieldPaths.length != 0) { return new PrimaryKey(type, fieldPaths); } HollowSchema schema = stateEngine.getSchema(type); if (schema instanceof HollowObjectSchema) { return ((HollowObjectSchema) schema).getPrimaryKey(); } return null; }
private void initializePrimaryKeys() { List<PrimaryKey> keys = new ArrayList<PrimaryKey>(); for (HollowSchema schema : output.getSchemas()) { if (schema.getSchemaType() == SchemaType.OBJECT) { PrimaryKey pk = ((HollowObjectSchema) schema).getPrimaryKey(); if (pk != null) keys.add(pk); } } this.primaryKeys = sortPrimaryKeys(keys); }
/** * Binds the field paths with those of the primary key associated with the schema of the unique type. * * @throws com.netflix.hollow.api.error.SchemaNotFoundException if there is no schema for the unique * type * @throws IllegalArgumentException if there is no primary key associated with the unique type */ public Builder<T> bindToPrimaryKey() { String primaryTypeName = uniqueType.getSimpleName(); HollowSchema schema = consumer.getStateEngine().getNonNullSchema(primaryTypeName); assert schema.getSchemaType() == HollowSchema.SchemaType.OBJECT; this.primaryTypeKey = ((HollowObjectSchema) schema).getPrimaryKey(); if (primaryTypeKey == null) { throw new IllegalArgumentException( String.format("No primary key associated with primary type %s", uniqueType)); } return this; }
public AbstractHollowDataAccessor(HollowReadStateEngine rStateEngine, String type, PrimaryKey primaryKey) { this.rStateEngine = rStateEngine; HollowSchema schema = rStateEngine.getTypeState(type).getSchema(); if (schema instanceof HollowObjectSchema) { this.type = type; if (primaryKey == null) { HollowObjectSchema oSchema = ((HollowObjectSchema) schema); this.primaryKey = oSchema.getPrimaryKey(); } else { this.primaryKey = primaryKey; } if (this.primaryKey == null) throw new RuntimeException(String.format("Unsupported DataType=%s with SchemaType=%s : %s", type, schema.getSchemaType(), "PrimaryKey is missing")); } else { throw new RuntimeException(String.format("Unsupported DataType=%s with SchemaType=%s : %s", type, schema.getSchemaType(), "Only supported type=" + SchemaType.OBJECT)); } }
private List<PrimaryKey> extractPrimaryKeys(HollowDataset dataset) { List<PrimaryKey> keys = new ArrayList<PrimaryKey>(); for (HollowSchema schema : dataset.getSchemas()) { if (schema.getSchemaType() == SchemaType.OBJECT) { PrimaryKey pk = ((HollowObjectSchema) schema).getPrimaryKey(); if (pk != null) keys.add(pk); } } return keys; }
public TypeAPrimaryKeyIndex(HollowConsumer consumer, boolean isListenToDataRefresh) { this(consumer, isListenToDataRefresh, ((HollowObjectSchema) consumer.getStateEngine().getNonNullSchema("TypeA")).getPrimaryKey() .getFieldPaths()); }
@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; }
private void setupKeyIndex(HollowReadStateEngine stateEngine, HollowHistory history) { HollowHistoryKeyIndex keyIndex = history.getKeyIndex(); for (String type : stateEngine.getAllTypes()) { HollowTypeReadState typeState = stateEngine.getTypeState(type); HollowSchema schema = typeState.getSchema(); if (schema instanceof HollowObjectSchema) { HollowObjectSchema oSchema = (HollowObjectSchema) schema; PrimaryKey pKey = oSchema.getPrimaryKey(); if (pKey == null) continue; keyIndex.indexTypeField(pKey, stateEngine); System.out.println("Setup KeyIndex: type=" + type + "\t" + pKey); } } }
@Override public FieldType[] getPrimaryKeyFieldTypes(int identifier) { HollowSchema schema = getSchema(identifier); if (schema.getSchemaType() == SchemaType.OBJECT) { PrimaryKey primaryKey = ((HollowObjectSchema) schema).getPrimaryKey(); if (primaryKey != null) { FieldType fieldTypes[] = new FieldType[primaryKey.numFields()]; for (int i = 0; i < fieldTypes.length; i++) { fieldTypes[i] = primaryKey.getFieldType(dataset, i); } return fieldTypes; } } return null; }
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; }
@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 HollowObjectSchema getFilteredObjectSchema(HollowObjectSchema schema, HollowFilterConfig filterConfig) { ObjectFilterConfig typeConfig = filterConfig.getObjectTypeConfig(schema.getName()); int numIncludedFields = 0; for(int i=0;i<schema.numFields();i++) { if(typeConfig.includesField(schema.getFieldName(i))) numIncludedFields++; } if(numIncludedFields == schema.numFields()) return schema; HollowObjectSchema filteredSchema = new HollowObjectSchema(schema.getName(), numIncludedFields, schema.getPrimaryKey()); for(int i=0;i<schema.numFields();i++) { if(typeConfig.includesField(schema.getFieldName(i))) filteredSchema.addField(schema.getFieldName(i), schema.getFieldType(i), schema.getReferencedType(i)); } return filteredSchema; }
@Test public void automaticallyTraversesSomeIncompletelyDefinedFieldPaths() { HollowObjectSchema schema = (HollowObjectSchema) writeEngine.getTypeState("TypeWithTraversablePrimaryKey").getSchema(); PrimaryKey traversablePrimaryKey = schema.getPrimaryKey(); Assert.assertEquals(2, traversablePrimaryKey.getFieldPathIndex(writeEngine, 0).length); Assert.assertEquals(3, traversablePrimaryKey.getFieldPathIndex(writeEngine, 1).length); Assert.assertEquals(1, traversablePrimaryKey.getFieldPathIndex(writeEngine, 2).length); PrimaryKey anotherTraversablePrimaryKey = new PrimaryKey("TypeWithTraversablePrimaryKey", "subType.id"); Assert.assertEquals(3, anotherTraversablePrimaryKey.getFieldPathIndex(writeEngine, 0).length); PrimaryKey hardStopPrimaryKey = new PrimaryKey("TypeWithTraversablePrimaryKey", "subType.id!"); Assert.assertEquals(2, hardStopPrimaryKey.getFieldPathIndex(writeEngine, 0).length); PrimaryKey hardStopPrimaryKey2 = new PrimaryKey("TypeWithTraversablePrimaryKey", "subType2!"); Assert.assertEquals(1, hardStopPrimaryKey2.getFieldPathIndex(writeEngine, 0).length); PrimaryKey hardStopPrimaryKey3 = new PrimaryKey("TypeWithTraversablePrimaryKey", "strList!"); Assert.assertEquals(1, hardStopPrimaryKey3.getFieldPathIndex(writeEngine, 0).length); }
@Test public void parsesObjectSchemaMultipleWithKey() throws IOException { String objectSchema = " TypeA @PrimaryKey(a1, a3.value) {\n" + " int a1;\n" + " string a2;\n" + " String a3;\n" + "}\n"; HollowObjectSchema schema = (HollowObjectSchema) HollowSchemaParser.parseSchema(objectSchema); Assert.assertEquals("TypeA", schema.getName()); Assert.assertEquals(3, schema.numFields()); Assert.assertEquals(FieldType.INT, schema.getFieldType(0)); Assert.assertEquals("a1", schema.getFieldName(0)); Assert.assertEquals(FieldType.STRING, schema.getFieldType(1)); Assert.assertEquals("a2", schema.getFieldName(1)); Assert.assertEquals(FieldType.REFERENCE, schema.getFieldType(2)); Assert.assertEquals("String", schema.getReferencedType(2)); Assert.assertEquals("a3", schema.getFieldName(2)); // Make sure primary key and HollowObjectSchame.toString is parsed properly Assert.assertEquals(new PrimaryKey("TypeA", "a1", "a3.value"), schema.getPrimaryKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesObjectSchemaWithKey() throws IOException { String objectSchema = " TypeA @PrimaryKey(a1) {\n" + " int a1;\n" + " string a2;\n" + " String a3;\n" + "}\n"; HollowObjectSchema schema = (HollowObjectSchema) HollowSchemaParser.parseSchema(objectSchema); Assert.assertEquals("TypeA", schema.getName()); Assert.assertEquals(3, schema.numFields()); Assert.assertEquals(FieldType.INT, schema.getFieldType(0)); Assert.assertEquals("a1", schema.getFieldName(0)); Assert.assertEquals(FieldType.STRING, schema.getFieldType(1)); Assert.assertEquals("a2", schema.getFieldName(1)); Assert.assertEquals(FieldType.REFERENCE, schema.getFieldType(2)); Assert.assertEquals("String", schema.getReferencedType(2)); Assert.assertEquals("a3", schema.getFieldName(2)); // Make sure primary key and HollowObjectSchame.toString is parsed properly Assert.assertEquals(new PrimaryKey("TypeA", "a1"), schema.getPrimaryKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }