private static HollowListSchema readListSchemaFrom(DataInputStream is, String schemaName) throws IOException { String elementType = is.readUTF(); return new HollowListSchema(schemaName, elementType); }
@Override public String getTypeName() { return schema.getName(); }
@Override public String toString() { return getName() + " List<" + getElementType() + ">;"; }
@Override protected void applyToChecksum(HollowChecksum checksum, HollowSchema withSchema) { if(!getSchema().equals(withSchema)) throw new IllegalArgumentException("HollowListTypeReadState cannot calculate checksum with unequal schemas: " + getSchema().getName()); BitSet populatedOrdinals = getListener(PopulatedOrdinalListener.class).getPopulatedOrdinals(); for(int i=0; i<shards.length; i++) shards[i].applyToChecksum(checksum, populatedOrdinals, i, shards.length); }
@Test public void parsesListSchema() throws IOException { String listSchema = "ListOfTypeA List<TypeA>;\n"; HollowListSchema schema = (HollowListSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("ListOfTypeA", schema.getName()); Assert.assertEquals("TypeA", schema.getElementType()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
public HollowListJavaGenerator(String packageName, String apiClassname, HollowListSchema schema, Set<String> parameterizedTypes, boolean parameterizeClassNames, HollowDataset dataset, CodeGeneratorConfig config) { super(packageName, apiClassname, schema, dataset, config); this.schema = schema; this.elementClassName = hollowImplClassname(schema.getElementType()); this.parameterize = parameterizeClassNames || parameterizedTypes.contains(schema.getElementType()); }
void wireTypeStatesToSchemas() { for(HollowTypeReadState state : typeStates.values()) { switch(state.getSchema().getSchemaType()) { case OBJECT: HollowObjectSchema objSchema = (HollowObjectSchema)state.getSchema(); for(int i=0;i<objSchema.numFields();i++) { if(objSchema.getReferencedType(i) != null) objSchema.setReferencedTypeState(i, typeStates.get(objSchema.getReferencedType(i))); } break; case LIST: HollowListSchema listSchema = (HollowListSchema)state.getSchema(); listSchema.setElementTypeState(typeStates.get(listSchema.getElementType())); break; case SET: HollowSetSchema setSchema = (HollowSetSchema)state.getSchema(); setSchema.setElementTypeState(typeStates.get(setSchema.getElementType())); ((HollowSetTypeReadState)state).buildKeyDeriver(); break; case MAP: HollowMapSchema mapSchema = (HollowMapSchema)state.getSchema(); mapSchema.setKeyTypeState(typeStates.get(mapSchema.getKeyType())); mapSchema.setValueTypeState(typeStates.get(mapSchema.getValueType())); ((HollowMapTypeReadState)state).buildKeyDeriver(); break; } } }
private void traverseList(HollowListTypeReadState typeState, int ordinal) { HollowListSchema schema = typeState.getSchema(); int size = typeState.size(ordinal); for(int i=0;i<size;i++) { int refOrdinal = typeState.getElementOrdinal(ordinal, i); if(refOrdinal != HollowConstants.ORDINAL_NONE) extractHollowRecord(schema.getElementTypeState(), refOrdinal); } }
@Override public String generate() { StringBuilder builder = new StringBuilder(); appendPackageAndCommonImports(builder, apiClassname); builder.append("import " + HollowListTypeAPI.class.getName() + ";\n\n"); builder.append("import " + HollowListTypeDataAccess.class.getName() + ";\n"); builder.append("import " + HollowListLookupDelegate.class.getName() + ";\n"); builder.append("\n@SuppressWarnings(\"all\")\n"); builder.append("public class ").append(className).append(" extends HollowListTypeAPI {\n\n"); builder.append(" private final ").append(delegateLookupClassname(schema)).append(" delegateLookupImpl;\n\n"); builder.append(" public ").append(className).append("(").append(apiClassname).append(" api, HollowListTypeDataAccess dataAccess) {\n"); builder.append(" super(api, dataAccess);\n"); builder.append(" this.delegateLookupImpl = new ").append(delegateLookupClassname(schema)).append("(this);\n"); builder.append(" }\n\n"); builder.append(" public ").append(typeAPIClassname(schema.getElementType())).append(" getElementAPI() {\n"); builder.append(" return getAPI().get").append(typeAPIClassname(schema.getElementType())).append("();\n"); builder.append(" }\n\n"); builder.append(" public ").append(delegateLookupClassname(schema)).append(" getDelegateLookupImpl() {\n"); builder.append(" return delegateLookupImpl;\n"); builder.append(" }\n\n"); builder.append(" public ").append(apiClassname).append(" getAPI() {\n"); builder.append(" return (").append(apiClassname).append(")api;\n"); builder.append(" }\n\n"); builder.append("}"); return builder.toString(); }
private static HollowListSchema parseListSchema(String typeName, StreamTokenizer tokenizer) throws IOException { int tok = tokenizer.nextToken(); if(tokenizer.ttype != '<') throw new IOException("Invalid Syntax: Expected '<' after 'List' for type " + typeName); tok = tokenizer.nextToken(); if(tok != StreamTokenizer.TT_WORD) { log.warning("Invalid Syntax: Expected element type declaration: " + typeName); } String elementType = tokenizer.sval; tok = tokenizer.nextToken(); if(tokenizer.ttype != '>') throw new IOException("Invalid Syntax: Expected '>' element type declaration: " + typeName); tok = tokenizer.nextToken(); if(tokenizer.ttype != ';') throw new IOException("Invalid Syntax: Expected semicolon after List schema declaration: " + typeName); return new HollowListSchema(typeName, elementType); }
private void appendTypeAPIAccessor(StringBuilder classBuilder) { String typeAPIClassname = typeAPIClassname(schema.getName()); classBuilder.append(" public " + typeAPIClassname + " typeApi() {\n"); classBuilder.append(" return (").append(typeAPIClassname).append(") delegate.getTypeAPI();\n"); classBuilder.append(" }\n\n"); } }
@Override public boolean equals(Object other) { if(!(other instanceof HollowListSchema)) return false; HollowListSchema otherSchema = (HollowListSchema)other; if(!getName().equals(otherSchema.getName())) return false; return getElementType().equals(otherSchema.getElementType()); }
@Override public boolean equalsElement(int elementOrdinal, Object testObject) { return GenericHollowRecordHelper.equalObject(getSchema().getElementType(), elementOrdinal, testObject); }
public HollowSchema toHollowSchema() { switch(type) { case LIST: return new HollowListSchema(schemaName, subType); case MAP: return new HollowMapSchema(schemaName, "MapKey", subType); case OBJECT: HollowObjectSchema schema = new HollowObjectSchema(schemaName, fields.size()); for(Map.Entry<String, HollowDiscoveredField> entry : fields.entrySet()) { if(entry.getValue().fieldType == FieldType.STRING) { schema.addField(entry.getKey(), FieldType.REFERENCE, "String"); } else { schema.addField(entry.getKey(), entry.getValue().fieldType, entry.getValue().referencedType); } } return schema; } throw new IllegalStateException("HollowDiscoveredSchema type must be one of LIST,MAP,OBJECT. Was " + type); } }
public HollowListTypeReadState(HollowReadStateEngine stateEngine, HollowListSchema schema, int numShards) { super(stateEngine, schema); this.sampler = new HollowListSampler(schema.getName(), DisabledSamplingDirector.INSTANCE); this.shardNumberMask = numShards - 1; this.shardOrdinalShift = 31 - Integer.numberOfLeadingZeros(numShards); if(numShards < 1 || 1 << shardOrdinalShift != numShards) throw new IllegalArgumentException("Number of shards must be a power of 2!"); HollowListTypeReadStateShard shards[] = new HollowListTypeReadStateShard[numShards]; for(int i=0;i<shards.length;i++) shards[i] = new HollowListTypeReadStateShard(); this.shards = shards; }
@Override public void writeTo(OutputStream os) throws IOException { DataOutputStream dos = new DataOutputStream(os); dos.write(SchemaType.LIST.getTypeId()); dos.writeUTF(getName()); dos.writeUTF(getElementType()); }
private void generateChainableAddForSetAndList(StringBuilder classBodyBuilder) { for (int i = 0; i < schema.numFields(); i++) { if (schema.getFieldType(i) != FieldType.REFERENCE) { continue; } HollowSchema referencedSchema = dataset.getSchema(schema.getReferencedType(i)); if (referencedSchema instanceof HollowListSchema || referencedSchema instanceof HollowSetSchema) { HollowSchema elementSchema = dataset.getSchema(referencedSchema instanceof HollowListSchema ? ((HollowListSchema) referencedSchema).getElementType() : ((HollowSetSchema) referencedSchema).getElementType()); String elementType = buildFieldType(elementSchema); Class fieldImplementationType = referencedSchema instanceof HollowListSchema ? ArrayList.class : HashSet.class; importClasses.add(fieldImplementationType); classBodyBuilder.append(" public ").append(getClassName()).append(" addTo") .append(uppercase(getFieldName(i))).append("(") .append(elementType).append(" ").append(lowercase(elementType)).append(") {\n"); classBodyBuilder.append(" if (this.").append(getFieldName(i)).append(" == null) {\n"); classBodyBuilder.append(" this.").append(getFieldName(i)).append(" = new ") .append(fieldImplementationType.getSimpleName()).append("<").append(elementType).append(">();\n"); classBodyBuilder.append(" }\n"); classBodyBuilder.append(" this.").append(getFieldName(i)).append(".add(") .append(lowercase(elementType)).append(");\n"); classBodyBuilder.append(" return this;\n"); classBodyBuilder.append(" }\n"); } } }
@Override protected void initializeTypeStates() { HollowListTypeWriteState writeState = new HollowListTypeWriteState(new HollowListSchema("TestList", "TestObject")); writeStateEngine.addTypeState(writeState); }
private int copyListRecord(HollowListSchema engineSchema, int currentRecordPointer, int currentRecordOrdinal) { HollowListWriteRecord rec = engineSchema != null ? (HollowListWriteRecord)getWriteRecord(engineSchema) : null; int numElements = VarInt.readVInt(record.data, currentRecordPointer); currentRecordPointer += VarInt.sizeOfVInt(numElements); for(int i=0;i<numElements;i++) { int unmappedElementOrdinal = VarInt.readVInt(record.data, currentRecordPointer); currentRecordPointer += VarInt.sizeOfVInt(unmappedElementOrdinal); if(rec != null) { int mappedElementOrdinal = ordinalMapping.get(unmappedElementOrdinal); rec.addElement(mappedElementOrdinal); } } if(engineSchema != null) { int stateEngineOrdinal = stateEngine.add(engineSchema.getName(), rec); ordinalMapping.put(currentRecordOrdinal, stateEngineOrdinal); } return currentRecordPointer; }
private void appendListStringify(Writer writer, HollowDataAccess dataAccess, HollowListTypeDataAccess typeDataAccess, int ordinal, int indentation) throws IOException { HollowListSchema schema = typeDataAccess.getSchema(); if(showTypes) writer.append("(").append(schema.getName()).append(")"); if(showOrdinals) writer.append(" (ordinal ").append(Integer.toString(ordinal)).append(")"); indentation++; int size = typeDataAccess.size(ordinal); String elementType = schema.getElementType(); for(int i=0;i<size;i++) { writer.append(NEWLINE); int elementOrdinal = typeDataAccess.getElementOrdinal(ordinal, i); appendIndentation(writer, indentation); writer.append("e" + i + ": "); appendStringify(writer, dataAccess, elementType, elementOrdinal, indentation); } }