Skip to content

Commit e840c79

Browse files
authored
Add getCachedBulletRecordProvider to BulletConfig (#95)
1 parent 910e720 commit e840c79

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

src/main/java/com/yahoo/bullet/common/BulletConfig.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ public class BulletConfig extends Config {
337337
public BulletConfig(String file) {
338338
super(file, DEFAULT_CONFIGURATION_NAME);
339339
VALIDATOR.validate(this);
340-
provider = BulletRecordProvider.from(getAs(RECORD_PROVIDER_CLASS_NAME, String.class));
341340
}
342341

343342
/**
@@ -346,19 +345,36 @@ public BulletConfig(String file) {
346345
public BulletConfig() {
347346
super(DEFAULT_CONFIGURATION_NAME);
348347
VALIDATOR.validate(this);
349-
provider = BulletRecordProvider.from(getAs(RECORD_PROVIDER_CLASS_NAME, String.class));
350348
}
351349

352350
/**
353-
* Get the {@link BulletRecordProvider} stored in this BulletConfig instance. This BulletRecordProvider is
354-
* created when this BulletConfig is constructed.
351+
* Construct a {@link BulletRecordProvider} and store it in this BulletConfig instance.
355352
*
356353
* @return The BulletRecordProvider instance.
357354
*/
358355
public BulletRecordProvider getBulletRecordProvider() {
356+
provider = BulletRecordProvider.from(getAs(RECORD_PROVIDER_CLASS_NAME, String.class));
357+
return provider;
358+
}
359+
360+
/**
361+
* Get the {@link BulletRecordProvider} stored in this BulletConfig instance, or construct and store one first if
362+
* there is none.
363+
*
364+
* @return The BulletRecordProvider instance.
365+
*/
366+
public BulletRecordProvider getCachedBulletRecordProvider() {
367+
if (provider == null) {
368+
return getBulletRecordProvider();
369+
}
359370
return provider;
360371
}
361372

373+
/**
374+
* Construct a {@link Schema} if configured.
375+
*
376+
* @return The Schema instance if configured; otherwise, null.
377+
*/
362378
public Schema getSchema() {
363379
String schemaFile = getAs(RECORD_SCHEMA_FILE_NAME, String.class);
364380
if (schemaFile == null) {

src/test/java/com/yahoo/bullet/common/BulletConfigTest.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.yahoo.bullet.querying.partitioning.MockPartitioner;
99
import com.yahoo.bullet.record.BulletRecord;
1010
import com.yahoo.bullet.record.BulletRecordProvider;
11+
import com.yahoo.bullet.record.avro.TypedAvroBulletRecordProvider;
12+
import com.yahoo.bullet.record.simple.TypedSimpleBulletRecordProvider;
1113
import com.yahoo.bullet.result.Meta;
1214
import com.yahoo.bullet.result.Meta.Concept;
1315
import com.yahoo.bullet.typesystem.Type;
@@ -461,11 +463,13 @@ public void testGetBulletRecordProvider() {
461463
BulletConfig config = new BulletConfig();
462464
BulletRecordProvider providerA = config.getBulletRecordProvider();
463465
BulletRecordProvider providerB = config.getBulletRecordProvider();
464-
Assert.assertEquals(providerA, providerB);
466+
467+
// Creates a new provider each time
468+
Assert.assertNotEquals(providerA, providerB);
465469

466470
// Ensure the provider generates new records each time
467471
BulletRecord recordA = providerA.getInstance();
468-
BulletRecord recordB = providerB.getInstance();
472+
BulletRecord recordB = providerA.getInstance();
469473

470474
Assert.assertNotNull(recordA);
471475
Assert.assertNotNull(recordB);
@@ -475,6 +479,31 @@ public void testGetBulletRecordProvider() {
475479
Assert.assertTrue(recordA.typedGet("someField").isNull());
476480
}
477481

482+
@Test
483+
public void testGetCachedBulletRecordProvider() {
484+
BulletConfig config = new BulletConfig();
485+
BulletRecordProvider providerA = config.getCachedBulletRecordProvider();
486+
BulletRecordProvider providerB = config.getCachedBulletRecordProvider();
487+
488+
// Uses the same provider
489+
Assert.assertSame(providerA, providerB);
490+
}
491+
492+
@Test
493+
public void testSettingDifferentBulletRecordProvider() {
494+
BulletConfig config = new BulletConfig();
495+
496+
// Default record provider is TypedAvroBulletRecordProvider
497+
Assert.assertTrue(config.getBulletRecordProvider() instanceof TypedAvroBulletRecordProvider);
498+
499+
config.set(BulletConfig.RECORD_PROVIDER_CLASS_NAME, TypedSimpleBulletRecordProvider.class.getName());
500+
501+
// Cached record provider doesn't change with new setting
502+
Assert.assertTrue(config.getCachedBulletRecordProvider() instanceof TypedAvroBulletRecordProvider);
503+
504+
Assert.assertTrue(config.getBulletRecordProvider() instanceof TypedSimpleBulletRecordProvider);
505+
}
506+
478507
@Test(expectedExceptions = IllegalStateException.class)
479508
public void testEqualityPartitioningWithNoFieldsValidation() {
480509
BulletConfig config = new BulletConfig();

0 commit comments

Comments
 (0)