Ejemplo n.º 1
0
  public void udtSerDeserTest(int version) throws Exception {
    ListType<?> lt = ListType.getInstance(Int32Type.instance, true);
    SetType<?> st = SetType.getInstance(UTF8Type.instance, true);
    MapType<?, ?> mt = MapType.getInstance(UTF8Type.instance, LongType.instance, true);

    UserType udt =
        new UserType(
            "ks",
            bb("myType"),
            Arrays.asList(bb("f1"), bb("f2"), bb("f3"), bb("f4")),
            Arrays.asList(LongType.instance, lt, st, mt));

    Map<ColumnIdentifier, Term.Raw> value = new HashMap<>();
    value.put(ci("f1"), lit(42));
    value.put(ci("f2"), new Lists.Literal(Arrays.<Term.Raw>asList(lit(3), lit(1))));
    value.put(ci("f3"), new Sets.Literal(Arrays.<Term.Raw>asList(lit("foo"), lit("bar"))));
    value.put(
        ci("f4"),
        new Maps.Literal(
            Arrays.<Pair<Term.Raw, Term.Raw>>asList(
                Pair.<Term.Raw, Term.Raw>create(lit("foo"), lit(24)),
                Pair.<Term.Raw, Term.Raw>create(lit("bar"), lit(12)))));

    UserTypes.Literal u = new UserTypes.Literal(value);
    Term t = u.prepare("ks", columnSpec("myValue", udt));

    QueryOptions options = QueryOptions.DEFAULT;
    if (version == 2)
      options =
          QueryOptions.fromProtocolV2(ConsistencyLevel.ONE, Collections.<ByteBuffer>emptyList());
    else if (version != 3) throw new AssertionError("Invalid protocol version for test");

    ByteBuffer serialized = t.bindAndGet(options);

    ByteBuffer[] fields = udt.split(serialized);

    assertEquals(4, fields.length);

    assertEquals(bytes(42L), fields[0]);

    // Note that no matter what the protocol version has been used in bindAndGet above, the
    // collections inside
    // a UDT should alway be serialized with version 3 of the protocol. Which is why we don't use
    // 'version'
    // on purpose below.

    assertEquals(
        Arrays.asList(3, 1), lt.getSerializer().deserializeForNativeProtocol(fields[1], 3));

    LinkedHashSet<String> s = new LinkedHashSet<>();
    s.addAll(Arrays.asList("bar", "foo"));
    assertEquals(s, st.getSerializer().deserializeForNativeProtocol(fields[2], 3));

    LinkedHashMap<String, Long> m = new LinkedHashMap<>();
    m.put("bar", 12L);
    m.put("foo", 24L);
    assertEquals(m, mt.getSerializer().deserializeForNativeProtocol(fields[3], 3));
  }
Ejemplo n.º 2
0
 public QueryMessage decode(ByteBuf body, int version) {
   String query = CBUtil.readLongString(body);
   if (version == 1) {
     ConsistencyLevel consistency = CBUtil.readConsistencyLevel(body);
     return new QueryMessage(
         query,
         QueryOptions.fromProtocolV1(consistency, Collections.<ByteBuffer>emptyList()));
   } else {
     return new QueryMessage(query, QueryOptions.codec.decode(body, version));
   }
 }
Ejemplo n.º 3
0
  public Message.Response execute(QueryState state) {
    try {
      if (options.getPageSize() == 0) throw new ProtocolException("The page size cannot be 0");

      UUID tracingId = null;
      if (isTracingRequested()) {
        tracingId = UUIDGen.getTimeUUID();
        state.prepareTracingSession(tracingId);
      }

      if (state.traceNextQuery()) {
        state.createTracingSession();

        ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
        builder.put("query", query);
        if (options.getPageSize() > 0)
          builder.put("page_size", Integer.toString(options.getPageSize()));

        Tracing.instance.begin("Execute CQL3 query", builder.build());
      }

      Message.Response response =
          state.getClientState().getCQLQueryHandler().process(query, state, options);
      if (options.skipMetadata() && response instanceof ResultMessage.Rows)
        ((ResultMessage.Rows) response).result.metadata.setSkipMetadata();

      if (tracingId != null) response.setTracingId(tracingId);

      return response;
    } catch (Exception e) {
      if (!((e instanceof RequestValidationException) || (e instanceof RequestExecutionException)))
        logger.error("Unexpected error during query", e);
      return ErrorMessage.fromException(e);
    } finally {
      Tracing.instance.stopSession();
    }
  }