/** * Parse a collection of {@link HollowSchema}s from the provided String. * * @param schemas the schemas as a string * @return the list of schema * @throws IOException if the schema cannot be parsed */ public static List<HollowSchema> parseCollectionOfSchemas(String schemas) throws IOException { return parseCollectionOfSchemas(new StringReader(schemas)); }
/** * Parse a single {@link HollowSchema} from the provided String. * * @param schema the schema as a string * @return the schema * @throws IOException if the schema cannot be parsed */ public static HollowSchema parseSchema(String schema) throws IOException { StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(schema)); configureTokenizer(tokenizer); return parseSchema(tokenizer); }
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); }
private static HollowSchema parseSchema(StreamTokenizer tokenizer) throws IOException { int tok = tokenizer.nextToken(); while(tok != StreamTokenizer.TT_WORD) { if(tok == StreamTokenizer.TT_EOF) return null; tok = tokenizer.nextToken(); } String typeName = tokenizer.sval; tok = tokenizer.nextToken(); if(tok == StreamTokenizer.TT_WORD) { if("List".equals(tokenizer.sval)) { return parseListSchema(typeName, tokenizer); } else if("Set".equals(tokenizer.sval)) { return parseSetSchema(typeName, tokenizer); } else if("Map".equals(tokenizer.sval)) { return parseMapSchema(typeName, tokenizer); } else { throw new IOException("Invalid syntax: expected one of '{', 'List', 'Set', or 'Map' after type declaration for '" + typeName + "'"); } } return parseObjectSchema(typeName, tokenizer); }
@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())); }
private static String[] parseHashKey(StreamTokenizer tokenizer) throws IOException { return parseKeyFieldPaths(tokenizer, "HashKey"); }
private static HollowObjectSchema parseObjectSchema(String typeName, StreamTokenizer tokenizer) throws IOException { String keyFieldPaths[] = parsePrimaryKey(tokenizer); if (tokenizer.ttype != '{') { throw new IOException("Invalid syntax: expecting '{' for '" + typeName + "'");
@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())); }
private static String[] parsePrimaryKey(StreamTokenizer tokenizer) throws IOException { return parseKeyFieldPaths(tokenizer, "PrimaryKey"); }
@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())); }
/** * Reads a schema file into the provided HollowWriteStateEngine. The schema file must be on the classpath. * * @param schemaFilePath the path to the schema * @param engine the write state engine * @throws IOException if the schema could not be read */ public static void readSchemaFileIntoWriteState(String schemaFilePath, HollowWriteStateEngine engine) throws IOException { InputStream input = null; try { input = HollowWriteStateCreator.class.getClassLoader().getResourceAsStream(schemaFilePath); Collection<HollowSchema> schemas = HollowSchemaParser.parseCollectionOfSchemas(new BufferedReader(new InputStreamReader(input))); populateStateEngineWithTypeWriteStates(engine, schemas); } finally { if (input != null) { input.close(); } } }
/** * Parse a collection of {@link HollowSchema}s from the provided Reader. * * @param reader the reader * @return the list of schema * @throws IOException if the schema cannot be parsed */ public static List<HollowSchema> parseCollectionOfSchemas(Reader reader) throws IOException { StreamTokenizer tokenizer = new StreamTokenizer(reader); configureTokenizer(tokenizer); List<HollowSchema> schemaList = new ArrayList<HollowSchema>(); HollowSchema schema = parseSchema(tokenizer); while (schema != null) { schemaList.add(schema); schema = parseSchema(tokenizer); } return schemaList; }
private static HollowMapSchema parseMapSchema(String typeName, StreamTokenizer tokenizer) throws IOException { int tok = tokenizer.nextToken(); if(tokenizer.ttype != '<') throw new IOException("Invalid Syntax: Expected '<' after 'Map' for type " + typeName); tok = tokenizer.nextToken(); if(tok != StreamTokenizer.TT_WORD) { log.warning("Invalid Syntax: Expected element type declaration: " + typeName); } String keyType = tokenizer.sval; tok = tokenizer.nextToken(); if(tokenizer.ttype != ',') throw new IOException("Invalid Syntax: Expected ',' after key type declaration: " + typeName); tok = tokenizer.nextToken(); if(tok != StreamTokenizer.TT_WORD) { log.warning("Invalid Syntax: Expected value type declaration: " + typeName); } String valueType = tokenizer.sval; tok = tokenizer.nextToken(); if(tokenizer.ttype != '>') throw new IOException("Invalid Syntax: Expected '>' after value type declaration: " + typeName); tok = tokenizer.nextToken(); String hashKeyPaths[] = parseHashKey(tokenizer); if(tokenizer.ttype != ';') throw new IOException("Invalid Syntax: Expected semicolon after Map schema declaration: " + typeName); return new HollowMapSchema(typeName, keyType, valueType, hashKeyPaths); }
@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())); }
@Test public void parsesManySchemas() throws IOException { String manySchemas = "/* This is a comment\n" + " consisting of multiple lines */\n" + " TypeA {\n" + " int a1;\n" + " \tstring a2; //This is a comment\n" + " String a3;\n" + "}\n\n"+ "MapOfStringToTypeA Map<String, TypeA>;\n"+ "ListOfTypeA List<TypeA>;\n"+ "TypeB { float b1; double b2; boolean b3; }"; List<HollowSchema> schemas = HollowSchemaParser.parseCollectionOfSchemas(manySchemas); Assert.assertEquals(4, schemas.size()); }
@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())); }
@Test public void testParseCollectionOfSchemas_reader() throws Exception { InputStream input = null; try { input = getClass().getResourceAsStream("/schema1.txt"); List<HollowSchema> schemas = HollowSchemaParser.parseCollectionOfSchemas(new BufferedReader(new InputStreamReader(input))); Assert.assertEquals("Should have two schemas", 2, schemas.size()); Assert.assertEquals("Should have Minion schema", "Minion", schemas.get(0).getName()); Assert.assertEquals("Should have String schema", "String", schemas.get(1).getName()); } finally { if (input != null) { input.close(); } } } }
@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())); }
@Test public void sortsSchemasEvenIfDependencyTypesNotPresent() throws IOException { String schemasText = "TypeA { TypeB b; }" + "TypeB { TypeC c; }"; List<HollowSchema> schemas = HollowSchemaParser.parseCollectionOfSchemas(schemasText); List<HollowSchema> sortedSchemas = HollowSchemaSorter.dependencyOrderedSchemaList(schemas); Assert.assertEquals(2, sortedSchemas.size()); Assert.assertEquals("TypeB", sortedSchemas.get(0).getName()); Assert.assertEquals("TypeA", sortedSchemas.get(1).getName()); }
@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())); }