Esempio n. 1
0
  public void collectionSerDeserTest(int version) throws Exception {
    // Lists
    ListType<?> lt = ListType.getInstance(Int32Type.instance, true);
    List<Integer> l = Arrays.asList(2, 6, 1, 9);

    List<ByteBuffer> lb = new ArrayList<>(l.size());
    for (Integer i : l) lb.add(Int32Type.instance.decompose(i));

    assertEquals(
        l,
        lt.getSerializer()
            .deserializeForNativeProtocol(
                CollectionSerializer.pack(lb, lb.size(), version), version));

    // Sets
    SetType<?> st = SetType.getInstance(UTF8Type.instance, true);
    Set<String> s = new LinkedHashSet<>();
    s.addAll(Arrays.asList("bar", "foo", "zee"));

    List<ByteBuffer> sb = new ArrayList<>(s.size());
    for (String t : s) sb.add(UTF8Type.instance.decompose(t));

    assertEquals(
        s,
        st.getSerializer()
            .deserializeForNativeProtocol(
                CollectionSerializer.pack(sb, sb.size(), version), version));

    // Maps
    MapType<?, ?> mt = MapType.getInstance(UTF8Type.instance, LongType.instance, true);
    Map<String, Long> m = new LinkedHashMap<>();
    m.put("bar", 12L);
    m.put("foo", 42L);
    m.put("zee", 14L);

    List<ByteBuffer> mb = new ArrayList<>(m.size() * 2);
    for (Map.Entry<String, Long> entry : m.entrySet()) {
      mb.add(UTF8Type.instance.decompose(entry.getKey()));
      mb.add(LongType.instance.decompose(entry.getValue()));
    }

    assertEquals(
        m,
        mt.getSerializer()
            .deserializeForNativeProtocol(
                CollectionSerializer.pack(mb, m.size(), version), version));
  }
Esempio n. 2
0
  static int compareListOrSet(AbstractType<?> elementsComparator, ByteBuffer o1, ByteBuffer o2) {
    // Note that this is only used if the collection is inside an UDT
    if (!o1.hasRemaining() || !o2.hasRemaining())
      return o1.hasRemaining() ? 1 : o2.hasRemaining() ? -1 : 0;

    ByteBuffer bb1 = o1.duplicate();
    ByteBuffer bb2 = o2.duplicate();

    int size1 = CollectionSerializer.readCollectionSize(bb1, 3);
    int size2 = CollectionSerializer.readCollectionSize(bb2, 3);

    for (int i = 0; i < Math.min(size1, size2); i++) {
      ByteBuffer v1 = CollectionSerializer.readValue(bb1, 3);
      ByteBuffer v2 = CollectionSerializer.readValue(bb2, 3);
      int cmp = elementsComparator.compare(v1, v2);
      if (cmp != 0) return cmp;
    }

    return size1 == size2 ? 0 : (size1 < size2 ? -1 : 1);
  }