Search in sources :

Example 1 with UpdateEvent

use of com.airbnb.spinaltap.mysql.event.UpdateEvent 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;
        }
    }
}
Also used : WriteEvent(com.airbnb.spinaltap.mysql.event.WriteEvent) QueryEventData(com.github.shyiko.mysql.binlog.event.QueryEventData) TableMapEvent(com.airbnb.spinaltap.mysql.event.TableMapEvent) WriteRowsEventData(com.github.shyiko.mysql.binlog.event.WriteRowsEventData) EventType(com.github.shyiko.mysql.binlog.event.EventType) UpdateRowsEventData(com.github.shyiko.mysql.binlog.event.UpdateRowsEventData) DeleteRowsEventData(com.github.shyiko.mysql.binlog.event.DeleteRowsEventData) XidEventData(com.github.shyiko.mysql.binlog.event.XidEventData) QueryEvent(com.airbnb.spinaltap.mysql.event.QueryEvent) DeleteEvent(com.airbnb.spinaltap.mysql.event.DeleteEvent) TableMapEventData(com.github.shyiko.mysql.binlog.event.TableMapEventData) StartEvent(com.airbnb.spinaltap.mysql.event.StartEvent) EventHeaderV4(com.github.shyiko.mysql.binlog.event.EventHeaderV4) XidEvent(com.airbnb.spinaltap.mysql.event.XidEvent) UpdateEvent(com.airbnb.spinaltap.mysql.event.UpdateEvent)

Example 2 with UpdateEvent

use of com.airbnb.spinaltap.mysql.event.UpdateEvent in project SpinalTap by airbnb.

the class MysqlMutationMapperTest method testUpdateMutation.

@Test
public void testUpdateMutation() throws Exception {
    Serializable[] old = new Serializable[4];
    old[0] = 12131L;
    old[1] = "test_user";
    old[2] = 25;
    old[3] = 0;
    Serializable[] current = new Serializable[4];
    current[0] = old[0];
    current[1] = old[1];
    current[2] = 26;
    current[3] = old[3];
    Serializable[] old2 = new Serializable[4];
    old2[0] = 12334L;
    old2[1] = "test_user2";
    old2[2] = 30;
    old2[3] = 1;
    Serializable[] current2 = new Serializable[4];
    current2[0] = old2[0];
    current2[1] = old2[1];
    current2[2] = 31;
    current2[3] = old2[3];
    Map.Entry<Serializable[], Serializable[]> change = new AbstractMap.SimpleEntry<>(old, current);
    Map.Entry<Serializable[], Serializable[]> change2 = new AbstractMap.SimpleEntry<>(old2, current2);
    BinlogEvent event = new UpdateEvent(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 MysqlUpdateMutation);
    MysqlUpdateMutation mutation = (MysqlUpdateMutation) mutations.get(0);
    validateMetadata(mutation, 0);
    Row oldRow = mutation.getPreviousRow();
    Row newRow = mutation.getRow();
    assertEquals(12131L, oldRow.getColumns().get("id").getValue());
    assertEquals("test_user", oldRow.getColumns().get("name").getValue());
    assertEquals(25, oldRow.getColumns().get("age").getValue());
    assertEquals(0, oldRow.getColumns().get("sex").getValue());
    assertEquals(12131L, newRow.getColumns().get("id").getValue());
    assertEquals("test_user", newRow.getColumns().get("name").getValue());
    assertEquals(26, newRow.getColumns().get("age").getValue());
    assertEquals(0, newRow.getColumns().get("sex").getValue());
    assertTrue(mutations.get(1) instanceof MysqlUpdateMutation);
    mutation = (MysqlUpdateMutation) mutations.get(1);
    validateMetadata(mutation, 1);
    oldRow = mutation.getPreviousRow();
    newRow = mutation.getRow();
    assertEquals(12334L, oldRow.getColumns().get("id").getValue());
    assertEquals("test_user2", oldRow.getColumns().get("name").getValue());
    assertEquals(30, oldRow.getColumns().get("age").getValue());
    assertEquals(1, oldRow.getColumns().get("sex").getValue());
    assertEquals(12334L, newRow.getColumns().get("id").getValue());
    assertEquals("test_user2", newRow.getColumns().get("name").getValue());
    assertEquals(31, newRow.getColumns().get("age").getValue());
    assertEquals(1, newRow.getColumns().get("sex").getValue());
}
Also used : Serializable(java.io.Serializable) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation) BinlogEvent(com.airbnb.spinaltap.mysql.event.BinlogEvent) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Map(java.util.Map) AbstractMap(java.util.AbstractMap) UpdateEvent(com.airbnb.spinaltap.mysql.event.UpdateEvent) Test(org.junit.Test)

Example 3 with UpdateEvent

use of com.airbnb.spinaltap.mysql.event.UpdateEvent in project SpinalTap by airbnb.

the class MysqlMutationMapperTest method testUpdateMutationWithDifferentPK.

@Test
public void testUpdateMutationWithDifferentPK() throws Exception {
    Serializable[] old = new Serializable[4];
    old[0] = 12131L;
    old[1] = "test_user";
    old[2] = 25;
    old[3] = 0;
    Serializable[] current = new Serializable[4];
    current[0] = 12334L;
    current[1] = old[1];
    current[2] = 26;
    current[3] = old[3];
    Map.Entry<Serializable[], Serializable[]> change = new AbstractMap.SimpleEntry<>(old, current);
    BinlogEvent event = new UpdateEvent(TABLE_ID, SERVER_ID, TIMESTAMP, BINLOG_FILE_POS, ImmutableList.of(change));
    List<? extends Mutation> mutations = eventMapper.map(event);
    assertEquals(2, mutations.size());
    assertTrue(mutations.get(0) instanceof MysqlDeleteMutation);
    MysqlDeleteMutation deleteMutation = (MysqlDeleteMutation) mutations.get(0);
    validateMetadata(deleteMutation, 0);
    Row row = deleteMutation.getRow();
    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 MysqlInsertMutation);
    MysqlInsertMutation insertMutation = (MysqlInsertMutation) mutations.get(1);
    validateMetadata(insertMutation, 0);
    row = insertMutation.getRow();
    assertEquals(12334L, row.getColumns().get("id").getValue());
    assertEquals("test_user", row.getColumns().get("name").getValue());
    assertEquals(26, row.getColumns().get("age").getValue());
    assertEquals(0, row.getColumns().get("sex").getValue());
}
Also used : MysqlDeleteMutation(com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation) Serializable(java.io.Serializable) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) BinlogEvent(com.airbnb.spinaltap.mysql.event.BinlogEvent) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Map(java.util.Map) AbstractMap(java.util.AbstractMap) UpdateEvent(com.airbnb.spinaltap.mysql.event.UpdateEvent) Test(org.junit.Test)

Example 4 with UpdateEvent

use of com.airbnb.spinaltap.mysql.event.UpdateEvent 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)));
}
Also used : WriteEvent(com.airbnb.spinaltap.mysql.event.WriteEvent) SourceState(com.airbnb.spinaltap.common.source.SourceState) TableMapEvent(com.airbnb.spinaltap.mysql.event.TableMapEvent) TableCache(com.airbnb.spinaltap.mysql.TableCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) BinlogEvent(com.airbnb.spinaltap.mysql.event.BinlogEvent) QueryEvent(com.airbnb.spinaltap.mysql.event.QueryEvent) DeleteEvent(com.airbnb.spinaltap.mysql.event.DeleteEvent) BinlogFilePos(com.airbnb.spinaltap.mysql.BinlogFilePos) StartEvent(com.airbnb.spinaltap.mysql.event.StartEvent) XidEvent(com.airbnb.spinaltap.mysql.event.XidEvent) UpdateEvent(com.airbnb.spinaltap.mysql.event.UpdateEvent) Test(org.junit.Test)

Example 5 with UpdateEvent

use of com.airbnb.spinaltap.mysql.event.UpdateEvent in project SpinalTap by airbnb.

the class MysqlMutationMapperTest method testUpdateMutationWithNullPK.

@Test
public void testUpdateMutationWithNullPK() throws Exception {
    Serializable[] old = new Serializable[4];
    old[0] = null;
    old[1] = "test_user";
    old[2] = 25;
    old[3] = 0;
    Serializable[] current = new Serializable[4];
    current[0] = null;
    current[1] = old[1];
    current[2] = 26;
    current[3] = old[3];
    Map.Entry<Serializable[], Serializable[]> change = new AbstractMap.SimpleEntry<>(old, current);
    BinlogEvent event = new UpdateEvent(TABLE_ID, SERVER_ID, TIMESTAMP, BINLOG_FILE_POS, ImmutableList.of(change));
    List<? extends Mutation> mutations = eventMapper.map(event);
    assertEquals(1, mutations.size());
    assertTrue(mutations.get(0) instanceof MysqlUpdateMutation);
    MysqlUpdateMutation mutation = (MysqlUpdateMutation) mutations.get(0);
    validateMetadata(mutation, 0);
    Row oldRow = mutation.getPreviousRow();
    Row newRow = mutation.getRow();
    assertEquals(null, oldRow.getColumns().get("id").getValue());
    assertEquals("test_user", oldRow.getColumns().get("name").getValue());
    assertEquals(25, oldRow.getColumns().get("age").getValue());
    assertEquals(0, oldRow.getColumns().get("sex").getValue());
    assertEquals(null, newRow.getColumns().get("id").getValue());
    assertEquals("test_user", newRow.getColumns().get("name").getValue());
    assertEquals(26, newRow.getColumns().get("age").getValue());
    assertEquals(0, newRow.getColumns().get("sex").getValue());
}
Also used : Serializable(java.io.Serializable) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation) BinlogEvent(com.airbnb.spinaltap.mysql.event.BinlogEvent) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Map(java.util.Map) AbstractMap(java.util.AbstractMap) UpdateEvent(com.airbnb.spinaltap.mysql.event.UpdateEvent) Test(org.junit.Test)

Aggregations

UpdateEvent (com.airbnb.spinaltap.mysql.event.UpdateEvent)5 BinlogEvent (com.airbnb.spinaltap.mysql.event.BinlogEvent)4 Test (org.junit.Test)4 Row (com.airbnb.spinaltap.mysql.mutation.schema.Row)3 Serializable (java.io.Serializable)3 AbstractMap (java.util.AbstractMap)3 Map (java.util.Map)3 DeleteEvent (com.airbnb.spinaltap.mysql.event.DeleteEvent)2 QueryEvent (com.airbnb.spinaltap.mysql.event.QueryEvent)2 StartEvent (com.airbnb.spinaltap.mysql.event.StartEvent)2 TableMapEvent (com.airbnb.spinaltap.mysql.event.TableMapEvent)2 WriteEvent (com.airbnb.spinaltap.mysql.event.WriteEvent)2 XidEvent (com.airbnb.spinaltap.mysql.event.XidEvent)2 MysqlUpdateMutation (com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation)2 SourceState (com.airbnb.spinaltap.common.source.SourceState)1 BinlogFilePos (com.airbnb.spinaltap.mysql.BinlogFilePos)1 TableCache (com.airbnb.spinaltap.mysql.TableCache)1 MysqlDeleteMutation (com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation)1 MysqlInsertMutation (com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation)1 DeleteRowsEventData (com.github.shyiko.mysql.binlog.event.DeleteRowsEventData)1