use of com.linkedin.databus2.core.filter.KeyFilterConfigHolder in project databus by linkedin.
the class TestDbusKeyCompositeFilter method testDbusKeyModFilterWithStringKeys1.
@Test
public // Case when string keys have numeric values
void testDbusKeyModFilterWithStringKeys1() throws Exception {
KeyFilterConfigHolder.Config partConf = new KeyFilterConfigHolder.Config();
partConf.setType("MOD");
KeyModFilterConfig.Config modConf = new KeyModFilterConfig.Config();
modConf.setNumBuckets(100);
modConf.setBuckets("[0,3-4]");
partConf.setMod(modConf);
DbusKeyFilter filter = new DbusKeyFilter(new KeyFilterConfigHolder(partConf.build()));
// String Keys with numeric values
List<DbusEvent> dbusEvents = new ArrayList<DbusEvent>();
List<String> keys = new ArrayList<String>();
for (long i = 0; i < 1000; ++i) {
keys.add(new Long(i).toString());
}
generateStringEvents(1000, (short) 1, keys, dbusEvents);
List<DbusEvent> expPassedEvents = new ArrayList<DbusEvent>();
List<DbusEvent> expFailedEvents = new ArrayList<DbusEvent>();
for (DbusEvent event : dbusEvents) {
long bkt = new Long(new String(event.keyBytes())) % 100;
if ((bkt == 0) || ((bkt >= 3) && (bkt < 5)))
expPassedEvents.add(event);
else
expFailedEvents.add(event);
}
List<DbusEvent> passedEvents = new ArrayList<DbusEvent>();
List<DbusEvent> failedEvents = new ArrayList<DbusEvent>();
for (DbusEvent event : dbusEvents) {
if (filter.allow(event)) {
passedEvents.add(event);
} else {
failedEvents.add(event);
}
}
System.out.println("Passed Event Size :" + passedEvents.size());
System.out.println("Failed Event Size :" + failedEvents.size());
assertEquals("Passed Size", expPassedEvents.size(), passedEvents.size());
assertEquals("Failed Size", expFailedEvents.size(), failedEvents.size());
for (int i = 0; i < passedEvents.size(); ++i) {
assertEquals("Passed Element " + i, expPassedEvents.get(i), passedEvents.get(i));
}
for (int i = 0; i < passedEvents.size(); ++i) {
assertEquals("Failed Element " + i, expFailedEvents.get(i), failedEvents.get(i));
}
ObjectMapper objMapper = new ObjectMapper();
String objStr = objMapper.writeValueAsString(filter);
System.out.println("KeyModFilter :" + objStr);
DbusKeyFilter filter2 = KeyFilterConfigJSONFactory.parseDbusKeyFilter(objStr);
String objStr2 = objMapper.writeValueAsString(filter2);
System.out.println("KeyModFilter2 :" + objStr2);
assertEquals("KeyModFilter JSON Serialization Test", objStr, objStr2);
}
use of com.linkedin.databus2.core.filter.KeyFilterConfigHolder in project databus by linkedin.
the class TestDbusKeyCompositeFilter method testDbusKeyModFilterWithStringKeys2.
@Test
public // Case when string keys have non-numeric values
void testDbusKeyModFilterWithStringKeys2() throws Exception {
KeyFilterConfigHolder.Config partConf = new KeyFilterConfigHolder.Config();
partConf.setType("MOD");
KeyModFilterConfig.Config modConf = new KeyModFilterConfig.Config();
modConf.setNumBuckets(100);
modConf.setBuckets("[0,3-4]");
partConf.setMod(modConf);
DbusKeyFilter filter = new DbusKeyFilter(new KeyFilterConfigHolder(partConf.build()));
// String Keys with numeric values
List<DbusEvent> dbusEvents = new ArrayList<DbusEvent>();
List<String> keys = new ArrayList<String>();
for (long i = 0; i < 1000; ++i) {
//contains non-numeric char
keys.add(i + "_1000");
}
generateStringEvents(1000, (short) 1, keys, dbusEvents);
List<DbusEvent> expPassedEvents = new ArrayList<DbusEvent>();
List<DbusEvent> expFailedEvents = new ArrayList<DbusEvent>();
for (DbusEvent event : dbusEvents) {
long bkt = new Long(Math.abs(new String(event.keyBytes()).hashCode())) % 100;
if ((bkt == 0) || ((bkt >= 3) && (bkt < 5)))
expPassedEvents.add(event);
else
expFailedEvents.add(event);
}
List<DbusEvent> passedEvents = new ArrayList<DbusEvent>();
List<DbusEvent> failedEvents = new ArrayList<DbusEvent>();
for (DbusEvent event : dbusEvents) {
if (filter.allow(event)) {
passedEvents.add(event);
} else {
failedEvents.add(event);
}
}
System.out.println("Passed Event Size :" + passedEvents.size());
System.out.println("Failed Event Size :" + failedEvents.size());
assertEquals("Passed Size", expPassedEvents.size(), passedEvents.size());
assertEquals("Failed Size", expFailedEvents.size(), failedEvents.size());
for (int i = 0; i < passedEvents.size(); ++i) {
assertEquals("Passed Element " + i, expPassedEvents.get(i), passedEvents.get(i));
}
for (int i = 0; i < passedEvents.size(); ++i) {
assertEquals("Failed Element " + i, expFailedEvents.get(i), failedEvents.get(i));
}
ObjectMapper objMapper = new ObjectMapper();
String objStr = objMapper.writeValueAsString(filter);
System.out.println("KeyModFilter :" + objStr);
DbusKeyFilter filter2 = KeyFilterConfigJSONFactory.parseDbusKeyFilter(objStr);
String objStr2 = objMapper.writeValueAsString(filter2);
System.out.println("KeyModFilter2 :" + objStr2);
assertEquals("KeyModFilter JSON Serialization Test", objStr, objStr2);
}
use of com.linkedin.databus2.core.filter.KeyFilterConfigHolder in project databus by linkedin.
the class TestDbusKeyCompositeFilter method testDbusKeyModFilter.
@Test
public void testDbusKeyModFilter() throws Exception {
KeyFilterConfigHolder.Config partConf = new KeyFilterConfigHolder.Config();
partConf.setType("MOD");
KeyModFilterConfig.Config modConf = new KeyModFilterConfig.Config();
modConf.setNumBuckets(100);
modConf.setBuckets("[0,3-4]");
partConf.setMod(modConf);
DbusKeyFilter filter = new DbusKeyFilter(new KeyFilterConfigHolder(partConf.build()));
List<DbusEvent> dbusEvents = new ArrayList<DbusEvent>();
List<Long> keys = new ArrayList<Long>();
for (long i = 0; i < 1000; i++) {
keys.add(i);
}
generateEvents(1000, (short) 1, keys, dbusEvents);
List<DbusEvent> expPassedEvents = new ArrayList<DbusEvent>();
List<DbusEvent> expFailedEvents = new ArrayList<DbusEvent>();
for (DbusEvent event : dbusEvents) {
long bkt = event.key() % 100;
if ((bkt == 0) || ((bkt >= 3) && (bkt < 5)))
expPassedEvents.add(event);
else
expFailedEvents.add(event);
}
List<DbusEvent> passedEvents = new ArrayList<DbusEvent>();
List<DbusEvent> failedEvents = new ArrayList<DbusEvent>();
for (DbusEvent event : dbusEvents) {
if (filter.allow(event)) {
passedEvents.add(event);
} else {
failedEvents.add(event);
}
}
System.out.println("Passed Event Size :" + passedEvents.size());
System.out.println("Failed Event Size :" + failedEvents.size());
assertEquals("Passed Size", expPassedEvents.size(), passedEvents.size());
assertEquals("Failed Size", expFailedEvents.size(), failedEvents.size());
for (int i = 0; i < passedEvents.size(); ++i) {
assertEquals("Passed Element " + i, expPassedEvents.get(i), passedEvents.get(i));
}
for (int i = 0; i < passedEvents.size(); ++i) {
assertEquals("Failed Element " + i, expFailedEvents.get(i), failedEvents.get(i));
}
ObjectMapper objMapper = new ObjectMapper();
String objStr = objMapper.writeValueAsString(filter);
System.out.println("KeyModFilter :" + objStr);
DbusKeyFilter filter2 = KeyFilterConfigJSONFactory.parseDbusKeyFilter(objStr);
String objStr2 = objMapper.writeValueAsString(filter2);
System.out.println("KeyModFilter2 :" + objStr2);
assertEquals("KeyModFilter JSON Serialization Test", objStr, objStr2);
}
use of com.linkedin.databus2.core.filter.KeyFilterConfigHolder in project databus by linkedin.
the class TestDbusKeyCompositeFilter method testDbusKeyNoneFilter.
@Test
public void testDbusKeyNoneFilter() throws Exception {
KeyFilterConfigHolder.Config partConf = new KeyFilterConfigHolder.Config();
partConf.setType("NONE");
DbusKeyFilter filter = new DbusKeyFilter(new KeyFilterConfigHolder(partConf.build()));
List<DbusEvent> dbusEvents = new ArrayList<DbusEvent>();
List<Long> keys = new ArrayList<Long>();
for (long i = 0; i < 1000; i++) {
keys.add(i);
}
generateEvents(1000, (short) 1, keys, dbusEvents);
List<DbusEvent> passedEvents = new ArrayList<DbusEvent>();
List<DbusEvent> failedEvents = new ArrayList<DbusEvent>();
for (DbusEvent event : dbusEvents) {
if (filter.allow(event)) {
passedEvents.add(event);
} else {
failedEvents.add(event);
}
}
System.out.println("Passed Event Size :" + passedEvents.size());
System.out.println("Failed Event Size :" + failedEvents.size());
assertEquals("Passed Size", dbusEvents.size(), passedEvents.size());
assertEquals("Failed Size", 0, failedEvents.size());
for (int i = 0; i < passedEvents.size(); i++) {
assertEquals("Passed Element " + i, dbusEvents.get(i), passedEvents.get(i));
}
ObjectMapper objMapper = new ObjectMapper();
String objStr = objMapper.writeValueAsString(filter);
System.out.println("KeyFilter :" + objStr);
DbusKeyFilter filter2 = KeyFilterConfigJSONFactory.parseDbusKeyFilter(objStr);
String objStr2 = objMapper.writeValueAsString(filter2);
System.out.println("KeyFilter2 :" + objStr2);
assertEquals("KeyNoneFilter: JSON Serialization Test", objStr, objStr2);
}
Aggregations