use of com.airbnb.spinaltap.mysql.event.DeleteEvent in project SpinalTap by airbnb.
the class MysqlSource method toBinlogEvent.
public static BinlogEvent toBinlogEvent(Event event, BinlogFilePos filePos) {
EventHeaderV4 header = event.getHeader();
EventType eventType = header.getEventType();
long serverId = header.getServerId();
long timestamp = header.getTimestamp();
if (EventType.isWrite(eventType)) {
WriteRowsEventData data = event.getData();
return new WriteEvent(data.getTableId(), serverId, timestamp, filePos, data.getRows());
} else if (EventType.isUpdate(eventType)) {
UpdateRowsEventData data = event.getData();
return new UpdateEvent(data.getTableId(), serverId, timestamp, filePos, data.getRows());
} else if (EventType.isDelete(eventType)) {
DeleteRowsEventData data = event.getData();
return new DeleteEvent(data.getTableId(), serverId, timestamp, filePos, data.getRows());
} else {
switch(eventType) {
case TABLE_MAP:
TableMapEventData tableMapData = event.getData();
return new TableMapEvent(tableMapData.getTableId(), serverId, timestamp, filePos, tableMapData.getDatabase(), tableMapData.getTable(), tableMapData.getColumnTypes());
case XID:
XidEventData xidData = event.getData();
return new XidEvent(serverId, timestamp, filePos, xidData.getXid());
case QUERY:
QueryEventData queryData = event.getData();
return new QueryEvent(serverId, timestamp, filePos, queryData.getDatabase(), queryData.getSql());
case FORMAT_DESCRIPTION:
return new StartEvent(serverId, timestamp, filePos);
default:
return null;
}
}
}
use of com.airbnb.spinaltap.mysql.event.DeleteEvent in project SpinalTap by airbnb.
the class MysqlEventFilterTest method testEventFilter.
@Test
public void testEventFilter() throws Exception {
TableCache tableCache = mock(TableCache.class);
BinlogEvent lastEvent = new XidEvent(0l, 0l, BINLOG_FILE_POS, 0l);
BinlogFilePos nextPosition = new BinlogFilePos("test.123", 15, 100);
SourceState state = new SourceState(0l, lastEvent.getOffset(), 0l, BINLOG_FILE_POS);
Filter<BinlogEvent> filter = MysqlEventFilter.create(tableCache, TABLE_NAMES, new AtomicReference(state));
when(tableCache.contains(TABLE_ID)).thenReturn(true);
assertTrue(filter.apply(new TableMapEvent(TABLE_ID, 0l, 0l, nextPosition, DATABASE_NAME, TABLE_NAME, new byte[1])));
assertTrue(filter.apply(new WriteEvent(TABLE_ID, 0l, 0l, nextPosition, Collections.emptyList())));
assertTrue(filter.apply(new DeleteEvent(TABLE_ID, 0l, 0l, nextPosition, Collections.emptyList())));
assertTrue(filter.apply(new UpdateEvent(TABLE_ID, 0l, 0l, nextPosition, Collections.emptyList())));
assertTrue(filter.apply(new XidEvent(0l, 0l, BINLOG_FILE_POS, 12l)));
assertTrue(filter.apply(new QueryEvent(0l, 0l, BINLOG_FILE_POS, DATABASE_NAME, "")));
assertTrue(filter.apply(new StartEvent(0l, 0l, BINLOG_FILE_POS)));
assertFalse(filter.apply(new TableMapEvent(TABLE_ID, 0l, 0l, BINLOG_FILE_POS, "", "", new byte[1])));
assertFalse(filter.apply(new WriteEvent(0l, 0l, 0l, BINLOG_FILE_POS, Collections.emptyList())));
assertFalse(filter.apply(new WriteEvent(TABLE_ID, 0l, 0l, BINLOG_FILE_POS, Collections.emptyList())));
assertFalse(filter.apply(mock(BinlogEvent.class)));
}
use of com.airbnb.spinaltap.mysql.event.DeleteEvent in project SpinalTap by airbnb.
the class MysqlMutationMapperTest method testDeleteMutation.
@Test
public void testDeleteMutation() throws Exception {
Serializable[] change = new Serializable[4];
change[0] = 12131L;
change[1] = "test_user";
change[2] = 25;
change[3] = 0;
Serializable[] change2 = new Serializable[4];
change2[0] = 12334L;
change2[1] = "test_user2";
change2[2] = 12;
change2[3] = 1;
BinlogEvent event = new DeleteEvent(TABLE_ID, SERVER_ID, TIMESTAMP, BINLOG_FILE_POS, ImmutableList.of(change, change2));
List<? extends Mutation> mutations = eventMapper.map(event);
assertEquals(2, mutations.size());
assertTrue(mutations.get(0) instanceof MysqlDeleteMutation);
MysqlDeleteMutation mutation = (MysqlDeleteMutation) mutations.get(0);
validateMetadata(mutation, 0);
Row row = mutation.getEntity();
assertEquals(12131L, row.getColumns().get("id").getValue());
assertEquals("test_user", row.getColumns().get("name").getValue());
assertEquals(25, row.getColumns().get("age").getValue());
assertEquals(0, row.getColumns().get("sex").getValue());
assertTrue(mutations.get(1) instanceof MysqlDeleteMutation);
mutation = (MysqlDeleteMutation) mutations.get(1);
validateMetadata(mutation, 1);
row = mutation.getEntity();
assertEquals(12334L, row.getColumns().get("id").getValue());
assertEquals("test_user2", row.getColumns().get("name").getValue());
assertEquals(12, row.getColumns().get("age").getValue());
assertEquals(1, row.getColumns().get("sex").getValue());
}
Aggregations