@VisibleForTesting
  void insertRecordIntoSorter(Product2<K, V> record) throws IOException {
    assert (sorter != null);
    final K key = record._1();
    final int partitionId = partitioner.getPartition(key);
    serBuffer.reset();
    serOutputStream.writeKey(key, OBJECT_CLASS_TAG);
    serOutputStream.writeValue(record._2(), OBJECT_CLASS_TAG);
    serOutputStream.flush();

    final int serializedRecordSize = serBuffer.size();
    assert (serializedRecordSize > 0);

    sorter.insertRecord(
        serBuffer.getBuf(), Platform.BYTE_ARRAY_OFFSET, serializedRecordSize, partitionId);
  }
Exemple #2
0
    public byte[] getNext() throws IOException {
      // System.err.println("getNext() called");
      if (LOG.isLoggable(Level.FINEST)) {
        LOG.finest("getNext() called");
      }
      int c;

      while ((c = inputStream.read()) != -1) {
        if (!(pattern == 2 && c == 3)) {
          next.write(c);

          if (pattern == 0 && c == 0) {
            pattern = 1;
          } else if (pattern == 1 && c == 0) {
            pattern = 2;
          } else if (pattern == 2 && c == 0) {
            byte[] s = next.toByteArrayLess3();
            next.reset();
            if (s != null) {
              return s;
            }
          } else if (pattern == 2 && c == 1) {
            byte[] s = next.toByteArrayLess3();
            next.reset();
            pattern = 0;
            if (s != null) {
              return s;
            }
          } else if (pattern != 0) {
            pattern = 0;
          }
        } else {
          pattern = 0;
        }
      }
      byte[] s = next.toByteArray();
      next.reset();
      if (s.length > 0) {
        return s;
      } else {
        return null;
      }
    }
  public void testIt() throws Exception {
    MyByteArrayOutputStream myByteArrayOutputStream = new MyByteArrayOutputStream();
    myByteArrayOutputStream.write(new byte[] {4, 10});
    assertEquals(2, myByteArrayOutputStream.size());
    assertEquals(4, myByteArrayOutputStream.deleteFirst());

    assertEquals(1, myByteArrayOutputStream.size());
    assertEquals(10, myByteArrayOutputStream.deleteFirst());

    assertEquals(0, myByteArrayOutputStream.size());

    myByteArrayOutputStream.write(new byte[] {1, 2, 3, 4});
    byte[] b = new byte[2];
    assertEquals(2, myByteArrayOutputStream.delete(b, 0, 2));
    assertEquals(1, b[0]);
    assertEquals(2, b[1]);
    assertEquals(2, myByteArrayOutputStream.size());

    assertEquals(2, myByteArrayOutputStream.delete(b, 0, 2));
    assertEquals(3, b[0]);
    assertEquals(4, b[1]);
    assertEquals(0, myByteArrayOutputStream.size());

    assertEquals(0, myByteArrayOutputStream.delete(b, 0, 2));
    myByteArrayOutputStream.write(new byte[] {7});

    assertEquals(1, myByteArrayOutputStream.delete(b, 0, 2));
    assertEquals(7, b[0]);

    b = new byte[1024];
    myByteArrayOutputStream.write(new byte[] {1, 2, 3, 4});
    assertEquals(4, myByteArrayOutputStream.delete(b, 0, 1024));

    myByteArrayOutputStream.write(new byte[] {1, 2});
    assertEquals(2, myByteArrayOutputStream.delete(b, 0, 1024));

    myByteArrayOutputStream = new MyByteArrayOutputStream(5);
    myByteArrayOutputStream.write(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
    assertEquals(10, myByteArrayOutputStream.delete(b, 512, 1024));
  }