Search in sources :

Example 1 with MysqlUpdateMutation

use of com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation 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 2 with MysqlUpdateMutation

use of com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation in project SpinalTap by airbnb.

the class UpdateMutationMapper method mapEvent.

@Override
protected List<MysqlMutation> mapEvent(Table table, UpdateEvent event) {
    List<MysqlMutation> mutations = Lists.newArrayList();
    Collection<ColumnMetadata> cols = table.getColumns().values();
    List<Map.Entry<Serializable[], Serializable[]>> rows = event.getRows();
    for (int position = 0; position < rows.size(); position++) {
        MysqlMutationMetadata metadata = createMetadata(table, event, position);
        Row previousRow = new Row(table, zip(rows.get(position).getKey(), cols));
        Row newRow = new Row(table, zip(rows.get(position).getValue(), cols));
        // to retain invariant that a mutation captures changes to a single PK
        if (table.getPrimaryKey().isPresent() && !previousRow.getPrimaryKeyValue().equals(newRow.getPrimaryKeyValue())) {
            mutations.add(new MysqlDeleteMutation(metadata, previousRow));
            mutations.add(new MysqlInsertMutation(metadata, newRow));
        } else {
            mutations.add(new MysqlUpdateMutation(metadata, previousRow, newRow));
        }
    }
    ;
    return mutations;
}
Also used : MysqlDeleteMutation(com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation) ColumnMetadata(com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata) Serializable(java.io.Serializable) MysqlMutationMetadata(com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row)

Example 3 with MysqlUpdateMutation

use of com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation in project SpinalTap by airbnb.

the class UpdateMutationMapper method map.

public Mutation map(MysqlUpdateMutation mutation) {
    MysqlMutationMetadata metadata = mutation.getMetadata();
    Mutation thriftMutation = new Mutation(MutationType.UPDATE, metadata.getTimestamp(), sourceId, metadata.getDataSource().getThriftDataSource(), createBinlogHeader(metadata, mutation.getType().getCode()), metadata.getTable().getThriftTable(), transformToEntity(mutation.getRow()));
    thriftMutation.setPreviousEntity(transformToEntity(mutation.getPreviousRow()));
    return thriftMutation;
}
Also used : MysqlMutationMetadata(com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata) Mutation(com.airbnb.jitney.event.spinaltap.v1.Mutation) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation)

Example 4 with MysqlUpdateMutation

use of com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation in project SpinalTap by airbnb.

the class KafkaDestinationTest method createMutation.

private Mutation createMutation(MutationType type) {
    Mapper<com.airbnb.spinaltap.Mutation<?>, ? extends TBase<?, ?>> thriftMutationMapper = ThriftMutationMapper.create("spinaltap");
    Table table = new Table(0L, TABLE, DATABASE, ImmutableList.of(new ColumnMetadata("id", ColumnDataType.LONGLONG, true, 0)), ImmutableList.of("id"));
    MysqlMutationMetadata metadata = new MysqlMutationMetadata(new DataSource(HOSTNAME, 0, "service"), new BinlogFilePos(), table, 0L, 0L, 0L, null, null, 0L, 0);
    Row row = new Row(table, ImmutableMap.of("id", new Column(new ColumnMetadata("id", ColumnDataType.LONGLONG, true, 0), 1L)));
    MysqlMutation mutation;
    switch(type) {
        case INSERT:
            mutation = new MysqlInsertMutation(metadata, row);
            break;
        case UPDATE:
            mutation = new MysqlUpdateMutation(metadata, row, row);
            break;
        case DELETE:
            mutation = new MysqlDeleteMutation(metadata, row);
            break;
        default:
            mutation = null;
    }
    return (Mutation) (thriftMutationMapper.map(mutation));
}
Also used : MysqlDeleteMutation(com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation) ColumnMetadata(com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata) MysqlMutationMetadata(com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata) Table(com.airbnb.spinaltap.mysql.mutation.schema.Table) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation) DataSource(com.airbnb.spinaltap.mysql.DataSource) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) Column(com.airbnb.spinaltap.mysql.mutation.schema.Column) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) BinlogFilePos(com.airbnb.spinaltap.mysql.BinlogFilePos) MysqlUpdateMutation(com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation) Mutation(com.airbnb.jitney.event.spinaltap.v1.Mutation) MysqlMutation(com.airbnb.spinaltap.mysql.mutation.MysqlMutation) MysqlDeleteMutation(com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation) MysqlInsertMutation(com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row)

Example 5 with MysqlUpdateMutation

use of com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation 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

MysqlUpdateMutation (com.airbnb.spinaltap.mysql.mutation.MysqlUpdateMutation)5 Row (com.airbnb.spinaltap.mysql.mutation.schema.Row)4 MysqlMutationMetadata (com.airbnb.spinaltap.mysql.mutation.MysqlMutationMetadata)3 Serializable (java.io.Serializable)3 Mutation (com.airbnb.jitney.event.spinaltap.v1.Mutation)2 BinlogEvent (com.airbnb.spinaltap.mysql.event.BinlogEvent)2 UpdateEvent (com.airbnb.spinaltap.mysql.event.UpdateEvent)2 MysqlDeleteMutation (com.airbnb.spinaltap.mysql.mutation.MysqlDeleteMutation)2 MysqlInsertMutation (com.airbnb.spinaltap.mysql.mutation.MysqlInsertMutation)2 MysqlMutation (com.airbnb.spinaltap.mysql.mutation.MysqlMutation)2 ColumnMetadata (com.airbnb.spinaltap.mysql.mutation.schema.ColumnMetadata)2 AbstractMap (java.util.AbstractMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 BinlogFilePos (com.airbnb.spinaltap.mysql.BinlogFilePos)1 DataSource (com.airbnb.spinaltap.mysql.DataSource)1 Column (com.airbnb.spinaltap.mysql.mutation.schema.Column)1 Table (com.airbnb.spinaltap.mysql.mutation.schema.Table)1