public HollowMapJavaGenerator(String packageName, String apiClassname, HollowMapSchema schema, HollowDataset dataset, Set<String> parameterizedTypes, boolean parameterizeClassNames, CodeGeneratorConfig config) { super(packageName, apiClassname, schema, dataset, config); this.schema = schema; this.keyClassName = hollowImplClassname(schema.getKeyType()); this.valueClassName = hollowImplClassname(schema.getValueType()); this.parameterizeKey = parameterizeClassNames || parameterizedTypes.contains(schema.getKeyType()); this.parameterizeValue = parameterizeClassNames || parameterizedTypes.contains(schema.getValueType()); }
public HollowDiffMapCountingNode(HollowDiff diff, HollowTypeDiff topLevelTypeDiff, HollowDiffNodeIdentifier nodeId, HollowMapTypeReadState fromState, HollowMapTypeReadState toState) { super(diff, topLevelTypeDiff, nodeId); this.fromState = fromState; this.toState = toState; HollowTypeReadState keyFromState = fromState == null ? null : fromState.getSchema().getKeyTypeState(); HollowTypeReadState keyToState = toState == null ? null : toState.getSchema().getKeyTypeState(); this.keyNode = getHollowDiffCountingNode(keyFromState, keyToState, "key"); HollowTypeReadState valueFromState = fromState == null ? null : fromState.getSchema().getValueTypeState(); HollowTypeReadState valueToState = toState == null ? null : toState.getSchema().getValueTypeState(); this.valueNode = getHollowDiffCountingNode(valueFromState, valueToState, "value"); String keyType = fromState != null ? fromState.getSchema().getKeyType() : toState.getSchema().getKeyType(); String valueType = fromState != null ? fromState.getSchema().getValueType() : toState.getSchema().getValueType(); this.keyFilter = new DiffEqualOrdinalFilter(equalityMapping.getEqualOrdinalMap(keyType)); this.valueFilter = new DiffEqualOrdinalFilter(equalityMapping.getEqualOrdinalMap(valueType)); this.keyRequiresTraversalForMissingFields = equalityMapping.requiresMissingFieldTraversal(keyType); this.valueRequiresTraversalForMissingFields = equalityMapping.requiresMissingFieldTraversal(valueType); }
@Override public boolean equalsValue(int valueOrdinal, Object testObject) { return GenericHollowRecordHelper.equalObject(getSchema().getValueType(), valueOrdinal, testObject); }
@Override public String toString() { StringBuilder builder = new StringBuilder(getName()); builder.append(" Map<").append(getKeyType()).append(",").append(getValueType()).append(">"); if(hashKey != null) { builder.append(" @HashKey("); if(hashKey.numFields() > 0) { builder.append(hashKey.getFieldPath(0)); for(int i=1;i<hashKey.numFields();i++) { builder.append(", ").append(hashKey.getFieldPath(i)); } } builder.append(")"); } builder.append(";"); return builder.toString(); }
@Override public boolean equals(Object other) { if(!(other instanceof HollowMapSchema)) return false; HollowMapSchema otherSchema = (HollowMapSchema)other; if(!getName().equals(otherSchema.getName())) return false; if(!getKeyType().equals(otherSchema.getKeyType())) return false; if(!getValueType().equals(otherSchema.getValueType())) return false; return isNullableObjectEquals(hashKey, otherSchema.getHashKey()); }
public DiffEqualityMapMapper(DiffEqualityMapping mapping, HollowMapTypeReadState fromState, HollowMapTypeReadState toState, boolean oneToOne) { super(fromState, toState, oneToOne); HollowMapSchema schema = fromState.getSchema(); this.keyEqualOrdinalMap = mapping.getEqualOrdinalMap(schema.getKeyType()); this.valueEqualOrdinalMap = mapping.getEqualOrdinalMap(schema.getValueType()); this.requiresTraversalForMissingFields = mapping.requiresMissingFieldTraversal(schema.getKeyType()) || mapping.requiresMissingFieldTraversal(schema.getValueType()); }
private void appendMapStringify(Writer writer, HollowDataAccess dataAccess, HollowMapTypeDataAccess typeDataAccess, int ordinal, int indentation) throws IOException { HollowMapSchema schema = typeDataAccess.getSchema(); if(showTypes) writer.append("(").append(schema.getName()).append(")"); if(showOrdinals) writer.append(" (ordinal ").append(Integer.toString(ordinal)).append(")"); indentation++; String keyType = schema.getKeyType(); String valueType = schema.getValueType(); HollowMapEntryOrdinalIterator iter = typeDataAccess.ordinalIterator(ordinal); while(iter.next()) { writer.append(NEWLINE); appendIndentation(writer, indentation); writer.append("k: "); appendStringify(writer, dataAccess, keyType, iter.getKey(), indentation); writer.append(NEWLINE); appendIndentation(writer, indentation); writer.append("v: "); appendStringify(writer, dataAccess, valueType, iter.getValue(), indentation); } }
@Override public HollowRecord instantiateValue(int valueOrdinal) { return GenericHollowRecordHelper.instantiate(getTypeDataAccess().getDataAccess(), getSchema().getValueType(), valueOrdinal); }
@Override public void writeTo(OutputStream os) throws IOException { DataOutputStream dos = new DataOutputStream(os); if(getHashKey() != null) dos.write(SchemaType.MAP.getTypeIdWithPrimaryKey()); else dos.write(SchemaType.MAP.getTypeId()); dos.writeUTF(getName()); dos.writeUTF(getKeyType()); dos.writeUTF(getValueType()); if(getHashKey() != null) { VarInt.writeVInt(dos, getHashKey().numFields()); for(int i=0;i<getHashKey().numFields();i++) { dos.writeUTF(getHashKey().getFieldPath(i)); } } }
private String expectedCollectionClassName(HollowSchema referencedSchema) { if (referencedSchema instanceof HollowObjectSchema) { return referencedSchema.getName(); } else if (referencedSchema instanceof HollowListSchema) { importClasses.add(List.class); HollowSchema elementSchema = dataset.getSchema(((HollowListSchema)referencedSchema).getElementType()); return "ListOf" + expectedCollectionClassName(elementSchema); } else if (referencedSchema instanceof HollowSetSchema) { importClasses.add(Set.class); HollowSchema elementSchema = dataset.getSchema(((HollowSetSchema)referencedSchema).getElementType()); return "SetOf" + expectedCollectionClassName(elementSchema); } else if (referencedSchema instanceof HollowMapSchema) { importClasses.add(Map.class); HollowSchema keySchema = dataset.getSchema(((HollowMapSchema)referencedSchema).getKeyType()); HollowSchema valueSchema = dataset.getSchema(((HollowMapSchema)referencedSchema).getValueType()); return "MapOf" + expectedCollectionClassName(keySchema) + "To" + expectedCollectionClassName(valueSchema); } throw new IllegalArgumentException("Expected HollowCollectionSchema or HollowMapSchema but got " + referencedSchema.getClass().getSimpleName()); }
private static void addTransitiveMatches(HollowReadStateEngine stateEngine, HollowMapTypeReadState typeState, Map<String, BitSet> matches) { HollowMapSchema schema = typeState.getSchema(); BitSet matchingOrdinals = getOrCreateBitSet(matches, schema.getName(), typeState.maxOrdinal()); HollowTypeReadState keyTypeState = stateEngine.getTypeState(schema.getKeyType()); HollowTypeReadState valueTypeState = stateEngine.getTypeState(schema.getValueType()); BitSet keyOrdinals = getOrCreateBitSet(matches, schema.getKeyType(), keyTypeState.maxOrdinal()); BitSet valueOrdinals = getOrCreateBitSet(matches, schema.getValueType(), valueTypeState.maxOrdinal()); int ordinal = matchingOrdinals.nextSetBit(0); while(ordinal != -1) { HollowMapEntryOrdinalIterator iter = typeState.ordinalIterator(ordinal); while(iter.next()) { keyOrdinals.set(iter.getKey()); valueOrdinals.set(iter.getValue()); } ordinal = matchingOrdinals.nextSetBit(ordinal + 1); } }
private HollowTypeDataAccess getChildDataAccess(HollowTypeDataAccess typeDataAccess, String childName) { if(typeDataAccess instanceof HollowObjectTypeDataAccess) { HollowObjectSchema schema = (HollowObjectSchema) typeDataAccess.getSchema(); int fieldIdx = schema.getPosition(childName); String childType = schema.getReferencedType(fieldIdx); return dataAccess.getTypeDataAccess(childType); } else if(typeDataAccess instanceof HollowCollectionTypeDataAccess) { HollowCollectionSchema schema = (HollowCollectionSchema) typeDataAccess.getSchema(); return dataAccess.getTypeDataAccess(schema.getElementType()); } else if(typeDataAccess instanceof HollowMapTypeDataAccess) { HollowMapSchema schema = (HollowMapSchema) typeDataAccess.getSchema(); String childType = "key".equals(childName) ? schema.getKeyType() : schema.getValueType(); return dataAccess.getTypeDataAccess(childType); } throw new IllegalArgumentException("I can't create a child node for a " + typeDataAccess.getClass()); }
private String buildFieldType(HollowSchema referencedSchema) { if (referencedSchema instanceof HollowObjectSchema) { return buildClassName(referencedSchema.getName(), classNameSuffix); } else if (referencedSchema instanceof HollowListSchema) { importClasses.add(List.class); HollowSchema elementSchema = dataset.getSchema(((HollowListSchema)referencedSchema).getElementType()); return "List<" + buildFieldType(elementSchema) + ">"; } else if (referencedSchema instanceof HollowSetSchema) { importClasses.add(Set.class); HollowSchema elementSchema = dataset.getSchema(((HollowSetSchema)referencedSchema).getElementType()); return "Set<" + buildFieldType(elementSchema) + ">"; } else if (referencedSchema instanceof HollowMapSchema) { importClasses.add(Map.class); HollowSchema keySchema = dataset.getSchema(((HollowMapSchema)referencedSchema).getKeyType()); HollowSchema valueSchema = dataset.getSchema(((HollowMapSchema)referencedSchema).getValueType()); return "Map<" + buildFieldType(keySchema) + ", " + buildFieldType(valueSchema) + ">"; } throw new IllegalArgumentException("Schema is unrecognized type " + referencedSchema.getClass().getSimpleName()); }
private List<Field> createMapFields(HollowEffigy effigy) { List<Field> fields = new ArrayList<Field>(); HollowMapTypeDataAccess typeDataAccess = (HollowMapTypeDataAccess)effigy.dataAccess; HollowMapSchema schema = typeDataAccess.getSchema(); HollowMapEntryOrdinalIterator iter = typeDataAccess.ordinalIterator(effigy.ordinal); while(iter.next()) { HollowEffigy entryEffigy = new HollowEffigy("Map.Entry"); entryEffigy.add(new Field("key", effigy(typeDataAccess.getDataAccess(), schema.getKeyType(), iter.getKey()))); entryEffigy.add(new Field("value", effigy(typeDataAccess.getDataAccess(), schema.getValueType(), iter.getValue()))); fields.add(new Field("entry", "Map.Entry", entryEffigy)); } return fields; }
public static HollowSchema withoutKeys(HollowSchema schema) { switch(schema.getSchemaType()) { case SET: HollowSetSchema setSchema = (HollowSetSchema)schema; if(setSchema.getHashKey() != null) setSchema = new HollowSetSchema(setSchema.getName(), setSchema.getElementType()); return setSchema; case MAP: HollowMapSchema mapSchema = (HollowMapSchema)schema; if(mapSchema.getHashKey() != null) mapSchema = new HollowMapSchema(mapSchema.getName(), mapSchema.getKeyType(), mapSchema.getValueType()); return mapSchema; default: return schema; } }
@Override public HollowWriteRecord copy(int ordinal) { HollowMapWriteRecord rec = rec(); rec.reset(); HollowMapEntryOrdinalIterator iter = readState().ordinalIterator(ordinal); String keyType = readState().getSchema().getKeyType(); String valueType = readState().getSchema().getValueType(); while(iter.next()) { int remappedKeyOrdinal = ordinalRemapper.getMappedOrdinal(keyType, iter.getKey()); int remappedValueOrdinal = ordinalRemapper.getMappedOrdinal(valueType, iter.getValue()); int hashCode = preserveHashPositions ? ((HollowMapEntryOrdinalIteratorImpl)iter).getCurrentBucket() : remappedKeyOrdinal; rec.addEntry(remappedKeyOrdinal, remappedValueOrdinal, hashCode); } return rec; }
@Test public void parsesMapSchema() throws IOException { String listSchema = "MapOfStringToTypeA Map<String, TypeA>;\n"; HollowMapSchema schema = (HollowMapSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("MapOfStringToTypeA", schema.getName()); Assert.assertEquals("String", schema.getKeyType()); Assert.assertEquals("TypeA", schema.getValueType()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
private void indexSchema(HollowSchema schema, Collection<HollowSchema> allSchemas) { if(schema instanceof HollowCollectionSchema) { String elementType = ((HollowCollectionSchema) schema).getElementType(); addDependency(schema.getName(), elementType, allSchemas); } else if(schema instanceof HollowMapSchema) { String keyType = ((HollowMapSchema)schema).getKeyType(); String valueType = ((HollowMapSchema)schema).getValueType(); addDependency(schema.getName(), keyType, allSchemas); addDependency(schema.getName(), valueType, allSchemas); } else if(schema instanceof HollowObjectSchema) { HollowObjectSchema objectSchema = (HollowObjectSchema) schema; for(int i=0;i<objectSchema.numFields();i++) { if(objectSchema.getFieldType(i) == FieldType.REFERENCE) { String refType = objectSchema.getReferencedType(i); addDependency(schema.getName(), refType, allSchemas); } } } getList(schema.getName(), dependencyIndex); getList(schema.getName(), reverseDependencyIndex); }
@Test public void parsesMapSchemaWithPrimaryKey() throws IOException { String listSchema = "MapOfStringToTypeA Map<String, TypeA> @HashKey(value);\n"; HollowMapSchema schema = (HollowMapSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("MapOfStringToTypeA", schema.getName()); Assert.assertEquals("String", schema.getKeyType()); Assert.assertEquals("TypeA", schema.getValueType()); Assert.assertEquals(new PrimaryKey("String", "value"), schema.getHashKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
@Test public void parsesMapSchemaWithMultiFieldPrimaryKey() throws IOException { String listSchema = "MapOfStringToTypeA Map<String, TypeA> @HashKey(id.value, region.country.id, key);\n"; HollowMapSchema schema = (HollowMapSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("MapOfStringToTypeA", schema.getName()); Assert.assertEquals("String", schema.getKeyType()); Assert.assertEquals("TypeA", schema.getValueType()); Assert.assertEquals(new PrimaryKey("String", "id.value", "region.country.id", "key"), schema.getHashKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }