示例#1
0
  /*
   * (non-Javadoc)
   *
   * @see com.dianping.puma.parser.mysql.event.AbstractBinlogEvent#doParse(java .nio.ByteBuffer,
   * com.dianping.puma.common.bo.PumaContext)
   */
  @Override
  public void doParse(ByteBuffer buf, PumaContext context) throws IOException {
    tableId = PacketUtils.readLong(buf, 6);
    reserved = PacketUtils.readInt(buf, 2);
    databaseNameLength = buf.get();
    databaseName = PacketUtils.readNullTerminatedString(buf);
    tableNameLength = buf.get();
    tableName = PacketUtils.readNullTerminatedString(buf);
    columnCount = PacketUtils.readLengthCodedUnsignedLong(buf);
    columnTypes = PacketUtils.readBytes(buf, columnCount.intValue());
    columnMetadataCount = PacketUtils.readLengthCodedUnsignedLong(buf);

    columnMetadata =
        Metadata.valueOf(columnTypes, PacketUtils.readBytes(buf, columnMetadataCount.intValue()));
    columnNullabilities = PacketUtils.readBitSet(buf, columnCount.intValue());

    context.getTableMaps().put(tableId, this);
  }
示例#2
0
  /** Test selects using ExecuteKeyspaceIds */
  @Test
  public void testExecuteKeyspaceIds() throws Exception {
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);

    // Ensure empty table
    String selectSql = "select * from vtgate_test";
    Query allRowsQuery =
        new QueryBuilder(selectSql, testEnv.keyspace, "master")
            .setKeyspaceIds(testEnv.getAllKeyspaceIds())
            .build();
    Cursor cursor = vtgate.execute(allRowsQuery);
    Assert.assertEquals(CursorImpl.class, cursor.getClass());
    Assert.assertEquals(0, cursor.getRowsAffected());
    Assert.assertEquals(0, cursor.getLastRowId());
    Assert.assertFalse(cursor.hasNext());
    vtgate.close();

    // Insert 10 rows
    Util.insertRows(testEnv, 1000, 10);

    vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    cursor = vtgate.execute(allRowsQuery);
    Assert.assertEquals(10, cursor.getRowsAffected());
    Assert.assertEquals(0, cursor.getLastRowId());
    Assert.assertTrue(cursor.hasNext());

    // Fetch all rows from the first shard
    KeyspaceId firstKid = testEnv.getAllKeyspaceIds().get(0);
    Query query =
        new QueryBuilder(selectSql, testEnv.keyspace, "master").addKeyspaceId(firstKid).build();
    cursor = vtgate.execute(query);

    // Check field types and values
    Row row = cursor.next();
    Cell idCell = row.next();
    Assert.assertEquals("id", idCell.getName());
    Assert.assertEquals(UnsignedLong.class, idCell.getType());
    UnsignedLong id = row.getULong(idCell.getName());

    Cell nameCell = row.next();
    Assert.assertEquals("name", nameCell.getName());
    Assert.assertEquals(byte[].class, nameCell.getType());
    Assert.assertTrue(
        Arrays.equals(("name_" + id.toString()).getBytes(), row.getBytes(nameCell.getName())));

    Cell ageCell = row.next();
    Assert.assertEquals("age", ageCell.getName());
    Assert.assertEquals(Integer.class, ageCell.getType());
    Assert.assertEquals(Integer.valueOf(2 * id.intValue()), row.getInt(ageCell.getName()));

    vtgate.close();
  }