Examples of BSONObject


Examples of org.bson.BSONObject

    }
    return bindVariables;
  }

  public static BSONObject batchQueryToBson(BatchQuery batchQuery) {
    BSONObject b = new BasicBSONObject();
    List<Map<String, Object>> queries = new LinkedList<>();
    Iterator<String> sqlIter = batchQuery.getSqls().iterator();
    Iterator<List<BindVariable>> bvIter = batchQuery.getBindVarsList().iterator();
    while (sqlIter.hasNext()) {
      Map<String, Object> query = new HashMap<>();
      query.put("Sql", sqlIter.next());
      List<BindVariable> bindVars = bvIter.next();
      if (bindVars != null) {
        query.put("BindVariables", bindVarsToBSON(bindVars));
      }
      queries.add(query);
    }
    b.put("Queries", queries);
    b.put("Keyspace", batchQuery.getKeyspace());
    b.put("TabletType", batchQuery.getTabletType());
    b.put("KeyspaceIds", batchQuery.getKeyspaceIds());
    if (batchQuery.getSession() != null) {
      b.put("Session", batchQuery.getSession());
    }
    return b;
  }
View Full Code Here

Examples of org.bson.BSONObject

        error = new String(err);
      }
    }

    QueryResult queryResult = null;
    BSONObject result = (BSONObject) reply.get("Result");
    if (result != null) {
      queryResult = bsonToQueryResult(result, null);
    }

    Object session = null;
View Full Code Here

Examples of org.bson.BSONObject

  public static List<Field> bsonToFields(BSONObject result) {
    List<Field> fieldList = new LinkedList<>();
    BasicBSONList fields = (BasicBSONList) result.get("Fields");
    for (Object field : fields) {
      BSONObject fieldBson = (BSONObject) field;
      String fieldName = new String((byte[]) fieldBson.get("Name"));
      int mysqlType = Ints.checkedCast((Long) fieldBson.get("Type"));
      FieldType fieldType = FieldType.get(mysqlType);
      fieldList.add(new Field(fieldName, fieldType));
    }
    return fieldList;
  }
View Full Code Here

Examples of org.bson.BSONObject

    }
    return rowList;
  }

  public static BSONObject splitQueryRequestToBson(SplitQueryRequest request) {
    BSONObject query = new BasicBSONObject();
    query.put("Sql", request.getSql());
    BSONObject b = new BasicBSONObject();
    b.put("Keyspace", request.getKeyspace());
    b.put("Query", query);
    b.put("SplitsPerShard", request.getSplitsPerShard());
    return b;
  }
View Full Code Here

Examples of org.bson.BSONObject

      }
    }
    BasicBSONList result = (BasicBSONList) reply.get("Splits");
    Map<Query, Long> queries = new HashMap<>();
    for (Object split : result) {
      BSONObject splitObj = (BasicBSONObject) split;
      BSONObject query = (BasicBSONObject) (splitObj.get("Query"));
      String sql = new String((byte[]) query.get("Sql"));
      BSONObject bindVars = (BasicBSONObject) query.get("BindVariables");
      List<BindVariable> bindVariables = new LinkedList<>();
      for (String key : bindVars.keySet()) {
        BindVariable bv = null;
        Object val = bindVars.get(key);
        if (val == null) {
          bv = BindVariable.forNull(key);
        }
        if (val instanceof UnsignedLong) {
          bv = BindVariable.forULong(key, (UnsignedLong) val);
        }
        if (val instanceof Long) {
          bv = BindVariable.forLong(key, (Long) val);
        }
        if (val instanceof Double) {
          bv = BindVariable.forDouble(key, (Double) val);
        }
        if (val instanceof byte[]) {
          bv = BindVariable.forBytes(key, (byte[]) val);
        }
        if (bv == null) {
          throw new RuntimeException("invalid bind variable type: " + val.getClass());
        }
        bindVariables.add(bv);
      }
      String keyspace = new String((byte[]) query.get("Keyspace"));
      String tabletType = new String((byte[]) query.get("TabletType"));
      List<KeyRange> keyranges = new ArrayList<>();
      for (Object o : (List<?>) query.get("KeyRanges")) {
        BSONObject keyrange = (BasicBSONObject) o;
        String start = Hex.encodeHexString((byte[]) keyrange.get("Start"));
        String end = Hex.encodeHexString((byte[]) keyrange.get("End"));
        KeyRange kr = new KeyRange(KeyspaceId.valueOf(start), KeyspaceId.valueOf(end));
        keyranges.add(kr);
      }

      Query q = new QueryBuilder(sql, keyspace, tabletType).setKeyRanges(keyranges)
View Full Code Here

Examples of org.bson.BSONObject

  /**
   * Encodes a {@link com.github.youtube.vitess.jdbc.vtocc.QueryService.SqlQuery} request and {@code
   * methodName} into bson.
   */
  public byte[] encode(String methodName, int sequence, Message request) {
    BSONObject bsonHeader = createBsonHeader(methodName, sequence);
    BSONObject bsonBody = sqlQueryBsonRequestBodyFactory.create(request);
    return Bytes.concat(
        pythonicBsonEncoder.encode(bsonHeader), pythonicBsonEncoder.encode(bsonBody));
  }
View Full Code Here

Examples of org.bson.BSONObject

   * Decodes bson byte header and checks for error.
   */
  @SuppressWarnings("UnusedParameters")
  public void checkHeader(String methodName, InputStream inputStream) throws IOException {
    // TODO(gco): Check sequence matches and method matches.
    BSONObject bsonHeader = goRpcBsonDecoder.readObject(inputStream);
    if (LOGGER.isDebugEnabled()) {
      String joinedBsonHeader = Joiner.on(',').withKeyValueSeparator("=").join(bsonHeader.toMap());
      LOGGER.debug("bsonHeader: {}", joinedBsonHeader);
    }
    if (bsonHeader.containsField("Error")) {
      Object error = bsonHeader.get("Error");
      if (error instanceof byte[] && ((byte[]) error).length != 0) {
        throw new IOException(new String((byte[]) error, StandardCharsets.UTF_8));
      }
    }
  }
View Full Code Here

Examples of org.bson.BSONObject

  /**
   * Decodes bson byte body to {@link com.github.youtube.vitess.jdbc.vtocc.QueryService.SqlQuery}
   * response.
   */
  public Message decodeBody(String methodName, InputStream inputStream) throws IOException {
    BSONObject bsonBody = goRpcBsonDecoder.readObject(inputStream);
    return sqlQueryResponseFactory.create(methodName, bsonBody);
  }
View Full Code Here

Examples of org.bson.BSONObject

    return sqlQueryResponseFactory.create(methodName, bsonBody);
  }


  protected BSONObject createBsonHeader(String methodName, int sequence) {
    BSONObject bsonObject = new BasicBSONObject();
    bsonObject.put("ServiceMethod", methodToString(methodName));
    bsonObject.put("Seq", sequence);
    return bsonObject;
  }
View Full Code Here

Examples of org.bson.BSONObject

  }

  protected QueryResult createQueryResult(BSONObject bsonBody) {
    Builder queryResult = QueryResult.newBuilder();
    queryResult.setInsertId((Long) bsonBody.get("InsertId"));
    BSONObject fields = (BSONObject) bsonBody.get("Fields");
    for (String index : fields.keySet()) {
      BSONObject field = (BSONObject) fields.get(index);
      queryResult.addFields(
          Field.newBuilder()
              .setName(new String((byte[]) field.get("Name"), StandardCharsets.UTF_8))
              .setType(Type.valueOf(Ints.checkedCast((Long) field.get("Type"))))
              .build()
      );
    }

    BSONObject rows = (BSONObject) bsonBody.get("Rows");
    for (String rowIndex : rows.keySet()) {
      BSONObject bsonRow = (BSONObject) rows.get(rowIndex);
      Row.Builder queryRow = Row.newBuilder();
      for (String cellIndex : bsonRow.keySet()) {
        queryRow.addValues(Cell.newBuilder()
            .setValue(ByteString.copyFrom((byte[]) bsonRow.get(cellIndex))));
      }
      queryResult.addRows(queryRow);
    }

    queryResult.setRowsAffected((Long) bsonBody.get("RowsAffected"));
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.