Search in sources :

Example 1 with Row

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

use of com.airbnb.spinaltap.mysql.mutation.schema.Row 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 3 with Row

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

the class MysqlKeyProviderTest method testGetKey.

@Test
public void testGetKey() throws Exception {
    Row row = new Row(TABLE, ImmutableMap.of(ID_COLUMN, new Column(TABLE.getColumns().get(ID_COLUMN), 1234)));
    MysqlMutation mutation = new MysqlInsertMutation(MUTATION_METADATA, row);
    assertEquals("test:users:1234", MysqlKeyProvider.INSTANCE.get(mutation));
}
Also used : Column(com.airbnb.spinaltap.mysql.mutation.schema.Column) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Test(org.junit.Test)

Example 4 with Row

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

the class MysqlKeyProvider method get.

/**
 * This is currently a replication of the logic to get the partition for a Mysql table mutation in
 * {@link com.airbnb.jitney.helpers.SpinaltapHelper}
 */
@Override
public String get(Mutation<?> mutation) {
    Preconditions.checkState(mutation instanceof MysqlMutation);
    MysqlMutation mysqlMutation = (MysqlMutation) mutation;
    Table table = mysqlMutation.getMetadata().getTable();
    Row row = mysqlMutation.getRow();
    return String.format("%s:%s:%s", table.getDatabase(), table.getName(), row.getPrimaryKeyValue());
}
Also used : Table(com.airbnb.spinaltap.mysql.mutation.schema.Table) Row(com.airbnb.spinaltap.mysql.mutation.schema.Row)

Example 5 with Row

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

the class MutationSchemaValidatorTest method testMissingColumn.

@Test(expected = IllegalStateException.class)
public void testMissingColumn() throws Exception {
    Row row = new Row(TABLE, ImmutableMap.of(ID_COLUMN, createColumn(ID_COLUMN, ColumnDataType.LONGLONG, true, 1L, 0), NAME_COLUMN, createColumn(NAME_COLUMN, ColumnDataType.VARCHAR, false, "bob", 1)));
    validator.validate(createMutation(row));
}
Also used : Row(com.airbnb.spinaltap.mysql.mutation.schema.Row) Test(org.junit.Test)

Aggregations

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