Package com.youtube.vitess.vtgate

Examples of com.youtube.vitess.vtgate.VtGate


  @Test
  public void testInts() throws Exception {
    String createTable =
        "create table vtocc_ints(tiny tinyint, tinyu tinyint unsigned, small smallint, smallu smallint unsigned, medium mediumint, mediumu mediumint unsigned, normal int, normalu int unsigned, big bigint, bigu bigint unsigned, year year, primary key(tiny)) comment 'vtocc_nocache'\n" + "";
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    vtgate.begin();
    vtgate.execute(getQuery(createTable));
    vtgate.commit();

    String insertSql =
        "insert into vtocc_ints values(:tiny, :tinyu, :small, :smallu, :medium, :mediumu, :normal, :normalu, :big, :bigu, :year)";
    Query insertQuery = new QueryBuilder(insertSql, testEnv.keyspace, "master")
        .addBindVar(BindVariable.forInt("tiny", -128))
        .addBindVar(BindVariable.forInt("tinyu", 255))
        .addBindVar(BindVariable.forInt("small", -32768))
        .addBindVar(BindVariable.forInt("smallu", 65535))
        .addBindVar(BindVariable.forInt("medium", -8388608))
        .addBindVar(BindVariable.forInt("mediumu", 16777215))
        .addBindVar(BindVariable.forLong("normal", -2147483648L))
        .addBindVar(BindVariable.forLong("normalu", 4294967295L))
        .addBindVar(BindVariable.forLong("big", -9223372036854775808L))
        .addBindVar(BindVariable.forULong("bigu", UnsignedLong.valueOf("18446744073709551615")))
        .addBindVar(BindVariable.forShort("year", (short) 2012))
        .addKeyRange(KeyRange.ALL)
        .build();
    vtgate.begin();
    vtgate.execute(insertQuery);
    vtgate.commit();

    String selectSql = "select * from vtocc_ints where tiny = -128";
    Query selectQuery =
        new QueryBuilder(selectSql, testEnv.keyspace, "master").addKeyRange(KeyRange.ALL).build();
    Cursor cursor = vtgate.execute(selectQuery);
    Assert.assertEquals(1, cursor.getRowsAffected());
    Row row = cursor.next();
    Assert.assertTrue(row.getInt("tiny").equals(-128));
    Assert.assertTrue(row.getInt("tinyu").equals(255));
    Assert.assertTrue(row.getInt("small").equals(-32768));
    Assert.assertTrue(row.getInt("smallu").equals(65535));
    Assert.assertTrue(row.getInt("medium").equals(-8388608));
    Assert.assertTrue(row.getInt("mediumu").equals(16777215));
    Assert.assertTrue(row.getLong("normal").equals(-2147483648L));
    Assert.assertTrue(row.getLong("normalu").equals(4294967295L));
    Assert.assertTrue(row.getLong("big").equals(-9223372036854775808L));
    Assert.assertTrue(row.getULong("bigu").equals(UnsignedLong.valueOf("18446744073709551615")));
    Assert.assertTrue(row.getShort("year").equals((short) 2012));
    vtgate.close();
  }
View Full Code Here


  @Test
  public void testFracts() throws Exception {
    String createTable =
        "create table vtocc_fracts(id int, deci decimal(5,2), num numeric(5,2), f float, d double, primary key(id)) comment 'vtocc_nocache'\n" + "";
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    vtgate.begin();
    vtgate.execute(getQuery(createTable));
    vtgate.commit();

    String insertSql = "insert into vtocc_fracts values(:id, :deci, :num, :f, :d)";
    Query insertQuery = new QueryBuilder(insertSql, testEnv.keyspace, "master")
        .addBindVar(BindVariable.forInt("id", 1))
        .addBindVar(BindVariable.forDouble("deci", 1.99))
        .addBindVar(BindVariable.forDouble("num", 2.99))
        .addBindVar(BindVariable.forFloat("f", 3.99F))
        .addBindVar(BindVariable.forDouble("d", 4.99))
        .addKeyRange(KeyRange.ALL)
        .build();
    vtgate.begin();
    vtgate.execute(insertQuery);
    vtgate.commit();

    String selectSql = "select * from vtocc_fracts where id = 1";
    Query selectQuery =
        new QueryBuilder(selectSql, testEnv.keyspace, "master").addKeyRange(KeyRange.ALL).build();
    Cursor cursor = vtgate.execute(selectQuery);
    Assert.assertEquals(1, cursor.getRowsAffected());
    Row row = cursor.next();
    Assert.assertTrue(row.getLong("id").equals(1L));
    Assert.assertTrue(row.getBigDecimal("deci").equals(new BigDecimal("1.99")));
    Assert.assertTrue(row.getBigDecimal("num").equals(new BigDecimal("2.99")));
    Assert.assertTrue(row.getFloat("f").equals(3.99F));
    Assert.assertTrue(row.getDouble("d").equals(4.99));
    vtgate.close();
  }
View Full Code Here

  @Test
  public void testStrings() throws Exception {
    String createTable =
        "create table vtocc_strings(vb varbinary(16), c char(16), vc varchar(16), b binary(4), tb tinyblob, bl blob, ttx tinytext, tx text, en enum('a','b'), s set('a','b'), primary key(vb)) comment 'vtocc_nocache'\n" + "";
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    vtgate.begin();
    vtgate.execute(getQuery(createTable));
    vtgate.commit();

    String insertSql =
        "insert into vtocc_strings values(:vb, :c, :vc, :b, :tb, :bl, :ttx, :tx, :en, :s)";
    Query insertQuery = new QueryBuilder(insertSql, testEnv.keyspace, "master")
        .addBindVar(BindVariable.forBytes("vb", "a".getBytes()))
        .addBindVar(BindVariable.forBytes("c", "b".getBytes()))
        .addBindVar(BindVariable.forBytes("vc", "c".getBytes()))
        .addBindVar(BindVariable.forBytes("b", "d".getBytes()))
        .addBindVar(BindVariable.forBytes("tb", "e".getBytes()))
        .addBindVar(BindVariable.forBytes("bl", "f".getBytes()))
        .addBindVar(BindVariable.forBytes("ttx", "g".getBytes()))
        .addBindVar(BindVariable.forBytes("tx", "h".getBytes()))
        .addBindVar(BindVariable.forString("en", "a"))
        .addBindVar(BindVariable.forString("s", "a,b"))
        .addKeyRange(KeyRange.ALL)
        .build();
    vtgate.begin();
    vtgate.execute(insertQuery);
    vtgate.commit();

    String selectSql = "select * from vtocc_strings where vb = 'a'";
    Query selectQuery =
        new QueryBuilder(selectSql, testEnv.keyspace, "master").addKeyRange(KeyRange.ALL).build();
    Cursor cursor = vtgate.execute(selectQuery);
    Assert.assertEquals(1, cursor.getRowsAffected());
    Row row = cursor.next();
    Assert.assertTrue(Arrays.equals(row.getBytes("vb"), "a".getBytes()));
    Assert.assertTrue(Arrays.equals(row.getBytes("c"), "b".getBytes()));
    Assert.assertTrue(Arrays.equals(row.getBytes("vc"), "c".getBytes()));
    // binary(4) column will be suffixed with three 0 bytes
    Assert.assertTrue(
        Arrays.equals(row.getBytes("b"), ByteBuffer.allocate(4).put((byte) 'd').array()));
    Assert.assertTrue(Arrays.equals(row.getBytes("tb"), "e".getBytes()));
    Assert.assertTrue(Arrays.equals(row.getBytes("bl"), "f".getBytes()));
    Assert.assertTrue(Arrays.equals(row.getBytes("ttx"), "g".getBytes()));
    Assert.assertTrue(Arrays.equals(row.getBytes("tx"), "h".getBytes()));
    // Assert.assertTrue(row.getString("en").equals("a"));
    // Assert.assertTrue(row.getString("s").equals("a,b"));
    vtgate.close();
  }
View Full Code Here

  @Test
  public void testMisc() throws Exception {
    String createTable =
        "create table vtocc_misc(id int, b bit(8), d date, dt datetime, t time, primary key(id)) comment 'vtocc_nocache'\n" + "";
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    vtgate.begin();
    vtgate.execute(getQuery(createTable));
    vtgate.commit();

    String insertSql = "insert into vtocc_misc values(:id, :b, :d, :dt, :t)";
    Query insertQuery = new QueryBuilder(insertSql, testEnv.keyspace, "master")
        .addBindVar(BindVariable.forInt("id", 1))
        .addBindVar(BindVariable.forBytes("b", ByteBuffer.allocate(1).put((byte) 1).array()))
        .addBindVar(BindVariable.forDate("d", DateTime.parse("2012-01-01")))
        .addBindVar(BindVariable.forDateTime("dt", DateTime.parse("2012-01-01T15:45:45")))
        .addBindVar(BindVariable.forTime("t",
            DateTime.parse("15:45:45", ISODateTimeFormat.timeElementParser())))
        .addKeyRange(KeyRange.ALL)
        .build();
    vtgate.begin();
    vtgate.execute(insertQuery);
    vtgate.commit();

    String selectSql = "select * from vtocc_misc where id = 1";
    Query selectQuery =
        new QueryBuilder(selectSql, testEnv.keyspace, "master").addKeyRange(KeyRange.ALL).build();
    Cursor cursor = vtgate.execute(selectQuery);
    Assert.assertEquals(1, cursor.getRowsAffected());
    Row row = cursor.next();
    Assert.assertTrue(row.getLong("id").equals(1L));
    Assert.assertTrue(
        Arrays.equals(row.getBytes("b"), ByteBuffer.allocate(1).put((byte) 1).array()));
    Assert.assertTrue(row.getDateTime("d").equals(DateTime.parse("2012-01-01")));
    Assert.assertTrue(row.getDateTime("dt").equals(DateTime.parse("2012-01-01T15:45:45")));
    Assert.assertTrue(row.getDateTime("t").equals(
        DateTime.parse("15:45:45", ISODateTimeFormat.timeElementParser())));
    vtgate.close();
  }
View Full Code Here

    insertRows(testEnv, startId, count, new DateTime());
  }

  public static void insertRows(TestEnv testEnv, int startId, int count, DateTime dateTime)
      throws ConnectionException, DatabaseException {
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);

    vtgate.begin();
    String insertSql = "insert into vtgate_test "
        + "(id, name, age, percent, datetime_col, timestamp_col, date_col, time_col, keyspace_id) "
        + "values (:id, :name, :age, :percent, :datetime_col, :timestamp_col, :date_col, :time_col, :keyspace_id)";
    for (int i = startId; i < startId + count; i++) {
      KeyspaceId kid = testEnv.getAllKeyspaceIds().get(i % testEnv.getAllKeyspaceIds().size());
      Query query = new QueryBuilder(insertSql, testEnv.keyspace, "master")
          .addBindVar(BindVariable.forULong("id", UnsignedLong.valueOf("" + i)))
          .addBindVar(BindVariable.forBytes("name", ("name_" + i).getBytes()))
          .addBindVar(BindVariable.forInt("age", i * 2))
          .addBindVar(BindVariable.forDouble("percent", new Double(i / 100.0)))
          .addBindVar(BindVariable.forULong("keyspace_id", (UnsignedLong) kid.getId()))
          .addBindVar(BindVariable.forDateTime("datetime_col", dateTime))
          .addBindVar(BindVariable.forDateTime("timestamp_col", dateTime))
          .addBindVar(BindVariable.forDate("date_col", dateTime))
          .addBindVar(BindVariable.forTime("time_col", dateTime))
          .addKeyspaceId(kid)
          .build();
      vtgate.execute(query);
    }
    vtgate.commit();
    vtgate.close();
  }
View Full Code Here

  /**
   * Insert rows to a specific shard using ExecuteKeyspaceIds
   */
  public static void insertRowsInShard(TestEnv testEnv, String shardName, int count)
      throws DatabaseException, ConnectionException {
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    vtgate.begin();
    String sql = "insert into vtgate_test " + "(id, name, keyspace_id) "
        + "values (:id, :name, :keyspace_id)";
    List<KeyspaceId> kids = testEnv.getKeyspaceIds(shardName);
    for (int i = 1; i <= count; i++) {
      KeyspaceId kid = kids.get(i % kids.size());
      Query query = new QueryBuilder(sql, testEnv.keyspace, "master")
          .addBindVar(BindVariable.forULong("id", UnsignedLong.valueOf("" + i)))
          .addBindVar(BindVariable.forBytes("name", ("name_" + i).getBytes()))
          .addBindVar(BindVariable.forULong("keyspace_id", (UnsignedLong) kid.getId()))
          .addKeyspaceId(kid)
          .build();
      vtgate.execute(query);
    }
    vtgate.commit();
    vtgate.close();
  }
View Full Code Here

    vtgate.commit();
    vtgate.close();
  }

  public static void createTable(TestEnv testEnv) throws Exception {
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    vtgate.begin();
    vtgate.execute(new QueryBuilder("drop table if exists vtgate_test", testEnv.keyspace, "master")
        .setKeyspaceIds(testEnv.getAllKeyspaceIds()).build());
    String createTable = "create table vtgate_test (id bigint auto_increment,"
        + " name varchar(64), age SMALLINT,  percent DECIMAL(5,2),"
        + " keyspace_id bigint(20) unsigned NOT NULL, datetime_col DATETIME,"
        + " timestamp_col TIMESTAMP,  date_col DATE, time_col TIME, primary key (id))"
        + " Engine=InnoDB";
    vtgate.execute(new QueryBuilder(createTable, testEnv.keyspace, "master").setKeyspaceIds(
        testEnv.getAllKeyspaceIds()).build());
    vtgate.commit();
    vtgate.close();
  }
View Full Code Here

   * exception.
   */
  public static void waitForTablet(String tabletType, int rowCount, int attempts, TestEnv testEnv)
      throws Exception {
    String sql = "select * from vtgate_test";
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    for (int i = 0; i < attempts; i++) {
      Cursor cursor = vtgate.execute(new QueryBuilder(sql, testEnv.keyspace, tabletType)
          .setKeyspaceIds(testEnv.getAllKeyspaceIds()).build());
      if (cursor.getRowsAffected() >= rowCount) {
        vtgate.close();
        return;
      }
      Thread.sleep(1000);
    }
    vtgate.close();
    throw new Exception(tabletType + " fails to catch up");
  }
View Full Code Here

  /**
   * Test DMLs are not allowed outside a transaction
   */
  @Test
  public void testDMLOutsideTransaction() throws ConnectionException {
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    String deleteSql = "delete from vtgate_test";
    try {
      vtgate.execute(new QueryBuilder(deleteSql, testEnv.keyspace, "master").addKeyspaceId(
          testEnv.getAllKeyspaceIds().get(0)).build());
      Assert.fail("did not raise DatabaseException");
    } catch (DatabaseException e) {
      Assert.assertTrue(e.getMessage().contains("not_in_tx"));
    } finally {
      vtgate.close();
    }
  }
View Full Code Here

  /**
   * 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();
  }
View Full Code Here

TOP

Related Classes of com.youtube.vitess.vtgate.VtGate

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.