public static HollowSchema readFrom(InputStream is) throws IOException { int schemaTypeId = is.read(); DataInputStream dis = new DataInputStream(is); String schemaName = dis.readUTF(); switch(SchemaType.fromTypeId(schemaTypeId)) { case OBJECT: return readObjectSchemaFrom(dis, schemaName, SchemaType.hasKey(schemaTypeId)); case LIST: return readListSchemaFrom(dis, schemaName); case SET: return readSetSchemaFrom(dis, schemaName, SchemaType.hasKey(schemaTypeId)); case MAP: return readMapSchemaFrom(dis, schemaName, SchemaType.hasKey(schemaTypeId)); } throw new IOException(); }
@Override public void writeTo(OutputStream os) throws IOException { DataOutputStream dos = new DataOutputStream(os); if (primaryKey != null) dos.write(SchemaType.OBJECT.getTypeIdWithPrimaryKey()); else dos.write(SchemaType.OBJECT.getTypeId()); dos.writeUTF(getName()); if (primaryKey != null) { VarInt.writeVInt(dos, primaryKey.numFields()); for (int i = 0; i < primaryKey.numFields(); i++) { dos.writeUTF(primaryKey.getFieldPath(i)); } } dos.writeShort(size); for(int i=0;i<size;i++) { dos.writeUTF(fieldNames[i]); dos.writeUTF(fieldTypes[i].name()); if(fieldTypes[i] == FieldType.REFERENCE) dos.writeUTF(referencedTypes[i]); } }
@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 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)); } } }
@Test public void fromTypeId() { Assert.assertEquals(SchemaType.OBJECT, SchemaType.fromTypeId(0)); Assert.assertEquals(SchemaType.OBJECT, SchemaType.fromTypeId(6)); Assert.assertNotEquals(SchemaType.OBJECT, SchemaType.fromTypeId(1)); Assert.assertEquals(SchemaType.LIST, SchemaType.fromTypeId(2)); Assert.assertEquals(SchemaType.SET, SchemaType.fromTypeId(1)); Assert.assertEquals(SchemaType.SET, SchemaType.fromTypeId(4)); Assert.assertEquals(SchemaType.MAP, SchemaType.fromTypeId(3)); Assert.assertEquals(SchemaType.MAP, SchemaType.fromTypeId(5)); }
@Override public void writeTo(OutputStream os) throws IOException { DataOutputStream dos = new DataOutputStream(os); dos.write(SchemaType.LIST.getTypeId()); dos.writeUTF(getName()); dos.writeUTF(getElementType()); }