private static HollowSetSchema readSetSchemaFrom(DataInputStream is, String schemaName, boolean hasHashKey) throws IOException { String elementType = is.readUTF(); String hashKeyFields[] = null; if(hasHashKey) { int numFields = VarInt.readVInt(is); hashKeyFields = new String[numFields]; for(int i=0;i<numFields;i++) { hashKeyFields[i] = is.readUTF(); } } return new HollowSetSchema(schemaName, elementType, hashKeyFields); }
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 protected void applyToChecksum(HollowChecksum checksum, HollowSchema withSchema) { if(!getSchema().equals(withSchema)) throw new IllegalArgumentException("HollowSetTypeReadState 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); }
@Override public boolean equals(Object other) { if(!(other instanceof HollowSetSchema)) return false; HollowSetSchema otherSchema = (HollowSetSchema)other; if(!getName().equals(otherSchema.getName())) return false; if(!getElementType().equals(otherSchema.getElementType())) return false; return isNullableObjectEquals(hashKey, otherSchema.getHashKey()); }
@Override public void writeTo(OutputStream os) throws IOException { DataOutputStream dos = new DataOutputStream(os); if(getHashKey() != null) dos.write(SchemaType.SET.getTypeIdWithPrimaryKey()); else dos.write(SchemaType.SET.getTypeId()); dos.writeUTF(getName()); dos.writeUTF(getElementType()); if(getHashKey() != null) { VarInt.writeVInt(dos, getHashKey().numFields()); for(int i=0;i<getHashKey().numFields();i++) { dos.writeUTF(getHashKey().getFieldPath(i)); } } }
@Override public String toString() { StringBuilder builder = new StringBuilder(getName()); builder.append(" Set<").append(getElementType()).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 protected String getTypeName() { return schema.getName(); }
@Test public void parsesSetSchemaWithMultiFieldKey() throws IOException { String listSchema = "SetOfTypeA Set<TypeA> @HashKey(id.value, region.country.id, key);\n"; HollowSetSchema schema = (HollowSetSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("SetOfTypeA", schema.getName()); Assert.assertEquals("TypeA", schema.getElementType()); Assert.assertEquals(new PrimaryKey("TypeA", "id.value", "region.country.id", "key"), schema.getHashKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
public HollowSetJavaGenerator(String packageName, String apiClassname, HollowSetSchema 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()); }
@Test public void testEqualsWithKeys() { HollowSetSchema s1 = new HollowSetSchema("Test", "TypeA", "f1"); HollowSetSchema s2 = new HollowSetSchema("Test", "TypeA", "f1"); Assert.assertEquals(s1.getHashKey(), s2.getHashKey()); Assert.assertEquals(new PrimaryKey("TypeA", "f1"), s2.getHashKey()); HollowSetSchema s1 = new HollowSetSchema("Test", "TypeA", "f1", "f2"); HollowSetSchema s2 = new HollowSetSchema("Test", "TypeA", "f1", "f2"); Assert.assertEquals(s1.getHashKey(), s2.getHashKey()); Assert.assertEquals(new PrimaryKey("TypeA", "f1", "f2"), s2.getHashKey()); HollowSetSchema s1 = new HollowSetSchema("Test", "TypeA"); HollowSetSchema s2 = new HollowSetSchema("Test", "TypeA", "f1"); Assert.assertNotEquals(s1.getHashKey(), s2.getHashKey()); HollowSetSchema s1 = new HollowSetSchema("Test", "TypeA", "f1"); HollowSetSchema s2 = new HollowSetSchema("Test", "TypeA", "f1", "f2"); Assert.assertNotEquals(s1.getHashKey(), s2.getHashKey());
@Test public void parsesSetSchema() throws IOException { String listSchema = "SetOfTypeA Set<TypeA>;\n"; HollowSetSchema schema = (HollowSetSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("SetOfTypeA", schema.getName()); Assert.assertEquals("TypeA", schema.getElementType()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
public void buildKeyDeriver() { if(getSchema().getHashKey() != null) this.keyDeriver = new HollowPrimaryKeyValueDeriver(getSchema().getHashKey(), getStateEngine()); for(int i=0;i<shards.length;i++) shards[i].setKeyDeriver(keyDeriver); }
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 traverseSet(HollowSetTypeReadState typeState, int ordinal) { HollowSetSchema schema = typeState.getSchema(); HollowOrdinalIterator iter = typeState.ordinalIterator(ordinal); int refOrdinal = iter.next(); while(refOrdinal != HollowOrdinalIterator.NO_MORE_ORDINALS) { if(refOrdinal != HollowConstants.ORDINAL_NONE) extractHollowRecord(schema.getElementTypeState(), refOrdinal); refOrdinal = iter.next(); } }
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"); } }
@Test public void parsesSetSchemaWithKey() throws IOException { String listSchema = "SetOfTypeA Set<TypeA> @HashKey(id.value);\n"; HollowSetSchema schema = (HollowSetSchema) HollowSchemaParser.parseSchema(listSchema); Assert.assertEquals("SetOfTypeA", schema.getName()); Assert.assertEquals("TypeA", schema.getElementType()); Assert.assertEquals(new PrimaryKey("TypeA", "id.value"), schema.getHashKey()); Assert.assertEquals(schema, HollowSchemaParser.parseSchema(schema.toString())); }
private boolean isDefinedHashCode(HollowSchema schema) { if(schema instanceof HollowSetSchema) return typeNamesWithDefinedHashCodes.contains(((HollowSetSchema)schema).getElementType()); if(schema instanceof HollowMapSchema) return typeNamesWithDefinedHashCodes.contains(((HollowMapSchema)schema).getKeyType()); return false; }
private void appendSetStringify(Writer writer, HollowDataAccess dataAccess, HollowSetTypeDataAccess typeDataAccess, int ordinal, int indentation) throws IOException { HollowSetSchema schema = typeDataAccess.getSchema(); if(showTypes) writer.append("(").append(schema.getName()).append(")"); if(showOrdinals) writer.append(" (ordinal ").append(Integer.toString(ordinal)).append(")"); indentation++; String elementType = schema.getElementType(); HollowOrdinalIterator iter = typeDataAccess.ordinalIterator(ordinal); int elementOrdinal = iter.next(); while(elementOrdinal != NO_MORE_ORDINALS) { writer.append(NEWLINE); appendIndentation(writer, indentation); writer.append("e: "); appendStringify(writer, dataAccess, elementType, elementOrdinal, indentation); elementOrdinal = iter.next(); } }
void buildKeyMatcher() { PrimaryKey hashKey = getSchema().getHashKey(); if(hashKey != null) this.keyMatcher = new HistoricalPrimaryKeyMatcher(getDataAccess(), hashKey); }
private static HollowSetSchema parseSetSchema(String typeName, StreamTokenizer tokenizer) throws IOException { int tok = tokenizer.nextToken(); if(tokenizer.ttype != '<') throw new IOException("Invalid Syntax: Expected '<' after 'Set' 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(); String hashKeyPaths[] = parseHashKey(tokenizer); if(tokenizer.ttype != ';') throw new IOException("Invalid Syntax: Expected semicolon after Set schema declaration: " + typeName); return new HollowSetSchema(typeName, elementType, hashKeyPaths); }