/** * Registers {@code DuplicateDataDetectionValidator} validators with the given {@link HollowProducer producer} for * all object schema declared with a primary key. * <p> * This requires that the producer's data model has been initialized * (see {@link HollowProducer#initializeDataModel(Class[])} or a prior run cycle has implicitly initialized * the data model. * <p> * For each {@link HollowTypeWriteState write state} that has a {@link HollowObjectSchema object schema} * declared with a {@link PrimaryKey primary key} a {@code DuplicateDataDetectionValidator} validator * is instantiated, with the primary key type name, and registered with the given producer (if a * {@code DuplicateDataDetectionValidator} validator is not already registered for the same primary key type name). * * @param producer the producer * @apiNote This method registers a {@code DuplicateDataDetectionValidator} validator with only the primary key type * name and not, in addition, the primary key fields. This is to ensure, for the common case, duplicate listeners * are not registered by this method if listeners with the same type names were explicitly registered when * building the producer. * @see HollowProducer#initializeDataModel(Class[]) */ public static void addValidatorsForSchemaWithPrimaryKey(HollowProducer producer) { producer.getWriteEngine().getOrderedTypeStates().stream() .filter(ts -> ts.getSchema().getSchemaType() == SchemaType.OBJECT) .map(ts -> (HollowObjectSchema) ts.getSchema()) .filter(hos -> hos.getPrimaryKey() != null) .map(HollowObjectSchema::getPrimaryKey) .forEach(k -> producer.addListener(new DuplicateDataDetectionValidator(k.getType()))); } }
private HollowProducer createProducer(File tmpFolder, HollowObjectSchema... schemas) { HollowProducer producer = HollowProducer.withPublisher(new FakeBlobPublisher()) .withAnnouncer(new HollowFilesystemAnnouncer(tmpFolder.toPath())).build(); if (schemas != null && schemas.length > 0) { producer.initializeDataModel(schemas); } producer.addListener(new FakeProducerListener()); return producer; }
@Test public void testNotPrimaryProducerVersion() { BasicSingleProducerEnforcer enforcer = new BasicSingleProducerEnforcer(); HollowProducer producer = HollowProducer.withPublisher(new FakeBlobPublisher()) .withSingleProducerEnforcer(enforcer) .withAnnouncer(new HollowFilesystemAnnouncer(tmpFolder.toPath())) .build(); producer.addListener(new FakeProducerListener()); long v1 = producer.runCycle(ws -> { ws.add(1); }); enforcer.disable(); // Run cycle as not the primary producer long v2 = producer.runCycle(ws -> { ws.add(1); }); // Run cycle as the primary producer enforcer.enable(); long v3 = producer.runCycle(ws -> { ws.add(2); }); Assert.assertEquals(v1, v2); Assert.assertTrue(v3 > v2); }