Package com.youtube.vitess.vtgate.Query

Examples of com.youtube.vitess.vtgate.Query.QueryBuilder


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


  }

  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

  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;
      }
View Full Code Here

  @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 {
View Full Code Here

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

    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    String allRowsSql = "select * from vtgate_test";

    for (String shardName : testEnv.shardKidMap.keySet()) {
      Query shardRows = new QueryBuilder(allRowsSql, testEnv.keyspace, "master").setKeyspaceIds(
          testEnv.getKeyspaceIds(shardName)).build();
      Cursor cursor = vtgate.execute(shardRows);
      Assert.assertEquals(10, cursor.getRowsAffected());
    }
    vtgate.close();
View Full Code Here

  @Test
  public void testDateFieldTypes() throws Exception {
    DateTime dt = DateTime.now().minusDays(2).withMillisOfSecond(0);
    Util.insertRows(testEnv, 10, 1, dt);
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    Query allRowsQuery = new QueryBuilder("select * from vtgate_test", testEnv.keyspace, "master")
        .setKeyspaceIds(testEnv.getAllKeyspaceIds()).build();
    Row row = vtgate.execute(allRowsQuery).next();
    Assert.assertTrue(dt.equals(row.getDateTime("timestamp_col")));
    Assert.assertTrue(dt.equals(row.getDateTime("datetime_col")));
    Assert.assertTrue(dt.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0)
View Full Code Here

    // Insert 10 rows across the shards
    Util.insertRows(testEnv, 1000, 10);
    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    String selectSql = "select * from vtgate_test";
    Query allRowsQuery =
        new QueryBuilder(selectSql, testEnv.keyspace, "master").addKeyRange(KeyRange.ALL).build();
    Cursor cursor = vtgate.execute(allRowsQuery);
    // Verify all rows returned
    Assert.assertEquals(10, cursor.getRowsAffected());
    vtgate.close();
  }
View Full Code Here

    VtGate vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    String selectSql = "select * from vtgate_test";

    // Check ALL KeyRange query returns rows from both shards
    Query allRangeQuery =
        new QueryBuilder(selectSql, testEnv.keyspace, "master").addKeyRange(KeyRange.ALL).build();
    Cursor cursor = vtgate.execute(allRangeQuery);
    Assert.assertEquals(rowsPerShard * 2, cursor.getRowsAffected());

    // Check KeyRange query limited to a single shard returns 10 rows each
    for (String shardName : testEnv.shardKidMap.keySet()) {
      List<KeyspaceId> shardKids = testEnv.getKeyspaceIds(shardName);
      KeyspaceId minKid = Collections.min(shardKids);
      KeyspaceId maxKid = Collections.max(shardKids);
      KeyRange shardKeyRange = new KeyRange(minKid, maxKid);
      Query shardRangeQuery = new QueryBuilder(selectSql, testEnv.keyspace, "master").addKeyRange(
          shardKeyRange).build();
      cursor = vtgate.execute(shardRangeQuery);
      Assert.assertEquals(rowsPerShard, cursor.getRowsAffected());
    }

    // Now make a cross-shard KeyRange and check all rows are returned
    Iterator<String> shardNameIter = testEnv.shardKidMap.keySet().iterator();
    KeyspaceId kidShard1 = testEnv.getKeyspaceIds(shardNameIter.next()).get(2);
    KeyspaceId kidShard2 = testEnv.getKeyspaceIds(shardNameIter.next()).get(2);
    KeyRange crossShardKeyrange;
    if (kidShard1.compareTo(kidShard2) < 0) {
      crossShardKeyrange = new KeyRange(kidShard1, kidShard2);
    } else {
      crossShardKeyrange = new KeyRange(kidShard2, kidShard1);
    }
    Query shardRangeQuery = new QueryBuilder(selectSql, testEnv.keyspace, "master").addKeyRange(
        crossShardKeyrange).build();
    cursor = vtgate.execute(shardRangeQuery);
    Assert.assertEquals(rowsPerShard * 2, cursor.getRowsAffected());
    vtgate.close();
  }
View Full Code Here

      KeyspaceId minKid = Collections.min(kids);
      KeyspaceId maxKid = Collections.max(kids);
      KeyRange kr = new KeyRange(minKid, maxKid);
      for (int i = 0; i < count; i++) {
        KeyspaceId kid = kids.get(i % kids.size());
        Query query = new QueryBuilder(sql, testEnv.keyspace, "master")
            .addBindVar(
                BindVariable.forULong("id", UnsignedLong.valueOf("" + Math.abs(random.nextInt()))))
            .addBindVar(BindVariable.forString("name", ("name_" + i)))
            .addBindVar(BindVariable.forULong("keyspace_id", (UnsignedLong) kid.getId()))
            .addBindVar(BindVariable.forNull("age"))
            .addKeyRange(kr)
            .build();
        vtgate.execute(query);
      }
    }
    vtgate.commit();
    vtgate.close();

    // Check 40 rows exist in total
    vtgate = VtGate.connect("localhost:" + testEnv.port, 0);
    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(count * 2, cursor.getRowsAffected());

    // Check 20 rows exist per shard
    for (String shardName : testEnv.shardKidMap.keySet()) {
      Query shardRows = new QueryBuilder(selectSql, testEnv.keyspace, "master").setKeyspaceIds(
          testEnv.getKeyspaceIds(shardName)).build();
      cursor = vtgate.execute(shardRows);
      Assert.assertEquals(count, cursor.getRowsAffected());
    }

View Full Code Here

TOP

Related Classes of com.youtube.vitess.vtgate.Query.QueryBuilder

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.