use of com.linkedin.d2.D2ClusterPartitionConfiguration in project rest.li by linkedin.
the class PartitionPropertiesConverter method toConfig.
public static D2ClusterPartitionConfiguration toConfig(PartitionProperties property) {
final D2ClusterPartitionConfiguration config;
final D2ClusterPartitionConfiguration.PartitionTypeSpecificData specificData;
switch(property.getPartitionType()) {
case RANGE:
RangeBasedPartitionProperties range = (RangeBasedPartitionProperties) property;
config = new D2ClusterPartitionConfiguration();
config.setType(PartitionTypeEnum.RANGE);
config.setPartitionKeyRegex(range.getPartitionKeyRegex());
config.setPartitionCount(range.getPartitionCount());
specificData = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
RangedPartitionProperties rangedPartitionProperties = new RangedPartitionProperties();
rangedPartitionProperties.setKeyRangeStart(range.getKeyRangeStart());
rangedPartitionProperties.setPartitionSize(range.getPartitionSize());
specificData.setRangedPartitionProperties(rangedPartitionProperties);
config.setPartitionTypeSpecificData(specificData);
break;
case HASH:
HashBasedPartitionProperties hash = (HashBasedPartitionProperties) property;
config = new D2ClusterPartitionConfiguration();
config.setType(PartitionTypeEnum.HASH);
config.setPartitionKeyRegex(hash.getPartitionKeyRegex());
config.setPartitionCount(hash.getPartitionCount());
specificData = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
specificData.setHashAlgorithm(HashAlgorithm.valueOf(hash.getHashAlgorithm().name()));
config.setPartitionTypeSpecificData(specificData);
break;
case CUSTOM:
{
CustomizedPartitionProperties properties = (CustomizedPartitionProperties) property;
config = new D2ClusterPartitionConfiguration();
config.setType(PartitionTypeEnum.CUSTOM);
config.setPartitionCount(properties.getPartitionCount());
specificData = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
PartitionAccessorList partitionList = new PartitionAccessorList();
partitionList.setClassNames(new StringArray(properties.getPartitionAccessorList()));
specificData.setPartitionAccessorList(partitionList);
config.setPartitionTypeSpecificData(specificData);
break;
}
case NONE:
config = new D2ClusterPartitionConfiguration();
config.setType(PartitionTypeEnum.NONE);
break;
default:
throw new IllegalArgumentException("Unsupported partitionType: " + property.getPartitionType());
}
return config;
}
use of com.linkedin.d2.D2ClusterPartitionConfiguration in project rest.li by linkedin.
the class PartitionPropertiesConverter method toProperties.
public static PartitionProperties toProperties(D2ClusterPartitionConfiguration config) {
final PartitionProperties partitionProperties;
switch(config.getType()) {
case RANGE:
{
RangedPartitionProperties rangedPartitionProperties = config.getPartitionTypeSpecificData().getRangedPartitionProperties();
partitionProperties = new RangeBasedPartitionProperties(config.getPartitionKeyRegex(), rangedPartitionProperties.getKeyRangeStart(), rangedPartitionProperties.getPartitionSize(), config.getPartitionCount());
break;
}
case HASH:
HashBasedPartitionProperties.HashAlgorithm algorithm;
switch(config.getPartitionTypeSpecificData().getHashAlgorithm()) {
case MODULO:
algorithm = HashBasedPartitionProperties.HashAlgorithm.MODULO;
break;
case MD5:
algorithm = HashBasedPartitionProperties.HashAlgorithm.MD5;
break;
case XXHASH:
algorithm = HashBasedPartitionProperties.HashAlgorithm.XXHASH;
break;
default:
throw new IllegalArgumentException("Unsupported hash algorithm: " + config.getPartitionTypeSpecificData().getHashAlgorithm());
}
partitionProperties = new HashBasedPartitionProperties(config.getPartitionKeyRegex(), config.getPartitionCount(), algorithm);
break;
case CUSTOM:
partitionProperties = new CustomizedPartitionProperties(config.getPartitionCount(), config.getPartitionTypeSpecificData().getPartitionAccessorList().getClassNames());
break;
case NONE:
partitionProperties = NullPartitionProperties.getInstance();
break;
default:
throw new IllegalArgumentException("Unsupported partitionType: " + config.getType());
}
return partitionProperties;
}
use of com.linkedin.d2.D2ClusterPartitionConfiguration in project rest.li by linkedin.
the class PartitionPropertiesConverterTest method testHashModuloPartitionProperties.
@Test
public void testHashModuloPartitionProperties() {
final String partitionKeyRegex = "/foo/bar/(\\d+)";
final int partitionCount = 16;
final HashBasedPartitionProperties.HashAlgorithm hashAlgorithm = HashBasedPartitionProperties.HashAlgorithm.MODULO;
PartitionProperties partitionProperties = new HashBasedPartitionProperties(partitionKeyRegex, partitionCount, hashAlgorithm);
D2ClusterPartitionConfiguration.PartitionTypeSpecificData data = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
data.setHashAlgorithm(com.linkedin.d2.HashAlgorithm.MODULO);
D2ClusterPartitionConfiguration partitionConfig = new D2ClusterPartitionConfiguration().setType(PartitionTypeEnum.HASH).setPartitionKeyRegex(partitionKeyRegex).setPartitionCount(partitionCount).setPartitionTypeSpecificData(data);
Assert.assertEquals(PartitionPropertiesConverter.toConfig(partitionProperties), partitionConfig);
Assert.assertEquals(PartitionPropertiesConverter.toProperties(partitionConfig), partitionProperties);
}
use of com.linkedin.d2.D2ClusterPartitionConfiguration in project rest.li by linkedin.
the class PartitionPropertiesConverterTest method testNullPartitionProperties.
@Test
public void testNullPartitionProperties() {
PartitionProperties partitionProperties = NullPartitionProperties.getInstance();
D2ClusterPartitionConfiguration partitionConfig = new D2ClusterPartitionConfiguration().setType(PartitionTypeEnum.NONE);
Assert.assertEquals(PartitionPropertiesConverter.toProperties(partitionConfig), partitionProperties);
Assert.assertEquals(PartitionPropertiesConverter.toConfig(partitionProperties), partitionConfig);
}
use of com.linkedin.d2.D2ClusterPartitionConfiguration in project rest.li by linkedin.
the class PartitionPropertiesConverterTest method testRangePartitionProperties.
@Test
public void testRangePartitionProperties() {
final String partitionKeyRegex = "/foo/bar/(\\d+)";
final long keyRangeStart = 1;
final long paritionSize = 1024;
final int partitionCount = 32;
PartitionProperties partitionProperties = new RangeBasedPartitionProperties(partitionKeyRegex, keyRangeStart, paritionSize, partitionCount);
D2ClusterPartitionConfiguration.PartitionTypeSpecificData data = new D2ClusterPartitionConfiguration.PartitionTypeSpecificData();
data.setRangedPartitionProperties(new RangedPartitionProperties().setKeyRangeStart(keyRangeStart).setPartitionSize(paritionSize));
D2ClusterPartitionConfiguration partitionConfig = new D2ClusterPartitionConfiguration().setType(PartitionTypeEnum.RANGE).setPartitionKeyRegex(partitionKeyRegex).setPartitionCount(partitionCount).setPartitionTypeSpecificData(data);
Assert.assertEquals(PartitionPropertiesConverter.toProperties(partitionConfig), partitionProperties);
Assert.assertEquals(PartitionPropertiesConverter.toConfig(partitionProperties), partitionConfig);
}
Aggregations