/** * @param schemas The schemas from the data model * @return a write state engine which is pre-populated with the specified data model. */ public static HollowWriteStateEngine createWithSchemas(Collection<HollowSchema> schemas) { HollowWriteStateEngine stateEngine = new HollowWriteStateEngine(); populateStateEngineWithTypeWriteStates(stateEngine, schemas); return stateEngine; }
/** * 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(); } } }
/** * Initializes the producer data model for the given schemas. * <p> * Data model initialization is required prior to {@link #restore(long, HollowConsumer.BlobRetriever) restoring} * the producer. * This ensures that restoration can correctly compare the producer's current data model * with the data model of the restored data state and manage any differences in those models * (such as not restoring state for any types in the restoring data model not present in the * producer's current data model). * <p> * After initialization a data model initialization event will be emitted * to all registered data model initialization * {@link com.netflix.hollow.api.producer.listener.DataModelInitializationListener listeners}. * * @param schemas the data model classes * @throws IllegalArgumentException if {@code schemas} is empty * @see #restore(long, HollowConsumer.BlobRetriever) */ public void initializeDataModel(HollowSchema... schemas) { Objects.requireNonNull(schemas); if (schemas.length == 0) { throw new IllegalArgumentException("classes is empty"); } long start = currentTimeMillis(); HollowWriteStateCreator.populateStateEngineWithTypeWriteStates(getWriteEngine(), Arrays.asList(schemas)); listeners.listeners().fireProducerInit(currentTimeMillis() - start); isInitialized = true; }
/** * Recreate a {@link HollowWriteStateEngine} which can be used to write a snapshot of or continue * a delta chain from the supplied {@link HollowReadStateEngine}. * <p> * The returned state engine will be ready to write a snapshot which will exactly recreate the data in the supplied {@link HollowReadStateEngine}. * A delta chain may be continued from this state by calling {@link HollowWriteStateEngine#prepareForNextCycle()}. * * @param readEngine the read state engine * @return the write state engine */ public static HollowWriteStateEngine recreateAndPopulateUsingReadEngine(final HollowReadStateEngine readEngine) { final HollowWriteStateEngine writeEngine = new HollowWriteStateEngine(); populateStateEngineWithTypeWriteStates(writeEngine, readEngine.getSchemas()); populateUsingReadEngine(writeEngine, readEngine); return writeEngine; }
@Test public void determinesIfSchemasAreTransitivelyDependent() throws IOException { String schemasText = "TypeA { TypeB b; }" + "TypeB { TypeC c; }" + "TypeC { TypeD d; }"; List<HollowSchema> schemas = HollowSchemaParser.parseCollectionOfSchemas(schemasText); HollowWriteStateEngine stateEngine = new HollowWriteStateEngine(); HollowWriteStateCreator.populateStateEngineWithTypeWriteStates(stateEngine, schemas); Assert.assertTrue(HollowSchemaSorter.typeIsTransitivelyDependent(stateEngine, "TypeA", "TypeB")); Assert.assertTrue(HollowSchemaSorter.typeIsTransitivelyDependent(stateEngine, "TypeA", "TypeC")); Assert.assertTrue(HollowSchemaSorter.typeIsTransitivelyDependent(stateEngine, "TypeB", "TypeC")); Assert.assertFalse(HollowSchemaSorter.typeIsTransitivelyDependent(stateEngine, "TypeC", "TypeB")); Assert.assertFalse(HollowSchemaSorter.typeIsTransitivelyDependent(stateEngine, "TypeB", "TypeA")); Assert.assertFalse(HollowSchemaSorter.typeIsTransitivelyDependent(stateEngine, "TypeC", "TypeA")); }