/** * See PIG-2936. The purpose of this test is to ensure that Tuples are being serialized in the * specific way that we expect. */ @Test public void testTupleSerializationSpecific() throws Exception { byte[] flags = { BinInterSedes.TUPLE_0, BinInterSedes.TUPLE_1, BinInterSedes.TUPLE_2, BinInterSedes.TUPLE_3, BinInterSedes.TUPLE_4, BinInterSedes.TUPLE_5, BinInterSedes.TUPLE_6, BinInterSedes.TUPLE_7, BinInterSedes.TUPLE_8, BinInterSedes.TUPLE_9, }; for (int i = 0; i < flags.length; i++) { Tuple t = mTupleFactory.newTuple(i); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutput out = new DataOutputStream(baos); out.writeByte(flags[i]); for (int j = 0; j < i; j++) { Integer val = Integer.valueOf(random.nextInt()); bis.writeDatum(out, val); t.set(j, val); } testSerTuple(t, baos.toByteArray()); } }
private void testSerTuple(Tuple t, byte[] expected) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutput out = new DataOutputStream(baos); bis.writeDatum(out, t); Tuple t2 = (Tuple) bis.readDatum(new DataInputStream(new ByteArrayInputStream(baos.toByteArray()))); assertEquals(t, t2); }
/** * Compare two NullableTuples as raw bytes. If neither are null, then IntWritable.compare() is * used. If both are null then the indices are compared. Otherwise the null one is defined to be * less. */ public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) { int rc = 0; mHasNullField = false; Tuple t1; Tuple t2; // This can't be done on the raw data. Users are allowed to // implement their own versions of tuples, which means we have no // idea what the underlying representation is. So step one is to // instantiate each object as a tuple. try { t1 = bis.readTuple(new DataInputStream(new ByteArrayInputStream(b1, s1, l1))); t2 = bis.readTuple(new DataInputStream(new ByteArrayInputStream(b2, s2, l2))); } catch (IOException ioe) { mLog.error("Unable to instantiate tuples for comparison: " + ioe.getMessage()); throw new RuntimeException(ioe.getMessage(), ioe); } rc = compareTuple(t1, t2); // TODO think about how SchemaTuple could speed this up return rc; }