private List<ImmutableBytesWritable> sortKeys() { // This will throw InsufficientMemoryException if necessary memoryChunk.resize(memoryChunk.getSize() + SizedUtil.sizeOfArrayList(hash.size())); keyList = new ArrayList<ImmutableBytesWritable>(hash.size()); keyList.addAll(hash.keySet()); Comparator<ImmutableBytesWritable> comp = new ImmutableBytesWritable.Comparator(); if (orderBy == OrderBy.REV_ROW_KEY_ORDER_BY) { comp = Collections.reverseOrder(comp); } Collections.sort(keyList, comp); return keyList; } }
private void doComparisonsOnRaw(ImmutableBytesWritable a, ImmutableBytesWritable b, int expectedSignum) throws IOException { ImmutableBytesWritable.Comparator comparator = new ImmutableBytesWritable.Comparator(); ByteArrayOutputStream baosA = new ByteArrayOutputStream(); ByteArrayOutputStream baosB = new ByteArrayOutputStream(); a.write(new DataOutputStream(baosA)); b.write(new DataOutputStream(baosB)); assertEquals( "Comparing " + a + " and " + b + " as raw", signum(comparator.compare(baosA.toByteArray(), 0, baosA.size(), baosB.toByteArray(), 0, baosB.size())), expectedSignum); assertEquals( "Comparing " + a + " and " + b + " as raw (inverse)", -signum(comparator.compare(baosB.toByteArray(), 0, baosB.size(), baosA.toByteArray(), 0, baosA.size())), expectedSignum); }
/** * Builds a comparator from the list of columns in ORDER BY clause. * @param orderByExpressions the columns in ORDER BY clause. * @return the comparator built from the list of columns in ORDER BY clause. */ // ImmutableBytesWritable.Comparator doesn't implement generics @SuppressWarnings("unchecked") private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) { Ordering<ResultEntry> ordering = null; int pos = 0; for (OrderByExpression col : orderByExpressions) { Expression e = col.getExpression(); Comparator<ImmutableBytesWritable> comparator = e.getSortOrder() == SortOrder.DESC && !e.getDataType().isFixedWidth() ? buildDescVarLengthComparator() : new ImmutableBytesWritable.Comparator(); Ordering<ImmutableBytesWritable> o = Ordering.from(comparator); if(!col.isAscending()) o = o.reverse(); o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst(); Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++)); ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering); } return ordering; }
/** * Builds a comparator from the list of columns in ORDER BY clause. * @param orderByExpressions the columns in ORDER BY clause. * @return the comparator built from the list of columns in ORDER BY clause. */ // ImmutableBytesWritable.Comparator doesn't implement generics @SuppressWarnings("unchecked") private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) { Ordering<ResultEntry> ordering = null; int pos = 0; for (OrderByExpression col : orderByExpressions) { Expression e = col.getExpression(); Comparator<ImmutableBytesWritable> comparator = e.getSortOrder() == SortOrder.DESC && !e.getDataType().isFixedWidth() ? buildDescVarLengthComparator() : new ImmutableBytesWritable.Comparator(); Ordering<ImmutableBytesWritable> o = Ordering.from(comparator); if(!col.isAscending()) o = o.reverse(); o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst(); Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++)); ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering); } return ordering; }
private void doComparisonsOnRaw(ImmutableBytesWritable a, ImmutableBytesWritable b, int expectedSignum) throws IOException { ImmutableBytesWritable.Comparator comparator = new ImmutableBytesWritable.Comparator(); ByteArrayOutputStream baosA = new ByteArrayOutputStream(); ByteArrayOutputStream baosB = new ByteArrayOutputStream(); a.write(new DataOutputStream(baosA)); b.write(new DataOutputStream(baosB)); assertEquals( "Comparing " + a + " and " + b + " as raw", signum(comparator.compare(baosA.toByteArray(), 0, baosA.size(), baosB.toByteArray(), 0, baosB.size())), expectedSignum); assertEquals( "Comparing " + a + " and " + b + " as raw (inverse)", -signum(comparator.compare(baosB.toByteArray(), 0, baosB.size(), baosA.toByteArray(), 0, baosA.size())), expectedSignum); }
private List<ImmutableBytesWritable> sortKeys() { // This will throw InsufficientMemoryException if necessary memoryChunk.resize(memoryChunk.getSize() + SizedUtil.sizeOfArrayList(hash.size())); keyList = new ArrayList<ImmutableBytesWritable>(hash.size()); keyList.addAll(hash.keySet()); Comparator<ImmutableBytesWritable> comp = new ImmutableBytesWritable.Comparator(); if (orderBy == OrderBy.REV_ROW_KEY_ORDER_BY) { comp = Collections.reverseOrder(comp); } Collections.sort(keyList, comp); return keyList; } }
private static Comparator<ImmutableBytesWritable> buildDescVarLengthComparator() { return new Comparator<ImmutableBytesWritable>() { @Override public int compare(ImmutableBytesWritable o1, ImmutableBytesWritable o2) { return DescVarLengthFastByteComparisons.compareTo( o1.get(), o1.getOffset(), o1.getLength(), o2.get(), o2.getOffset(), o2.getLength()); } }; }
private static Comparator<ImmutableBytesWritable> buildDescVarLengthComparator() { return new Comparator<ImmutableBytesWritable>() { @Override public int compare(ImmutableBytesWritable o1, ImmutableBytesWritable o2) { return DescVarLengthFastByteComparisons.compareTo( o1.get(), o1.getOffset(), o1.getLength(), o2.get(), o2.getOffset(), o2.getLength()); } }; }
private void doComparisonsOnObjects(ImmutableBytesWritable a, ImmutableBytesWritable b, int expectedSignum) { ImmutableBytesWritable.Comparator comparator = new ImmutableBytesWritable.Comparator(); assertEquals( "Comparing " + a + " and " + b + " as objects", signum(comparator.compare(a, b)), expectedSignum); assertEquals( "Comparing " + a + " and " + b + " as objects (inverse)", -signum(comparator.compare(b, a)), expectedSignum); }
public void testSpecificCompare() { ImmutableBytesWritable ibw1 = new ImmutableBytesWritable(new byte[]{0x0f}); ImmutableBytesWritable ibw2 = new ImmutableBytesWritable(new byte[]{0x00, 0x00}); ImmutableBytesWritable.Comparator c = new ImmutableBytesWritable.Comparator(); assertFalse("ibw1 < ibw2", c.compare( ibw1, ibw2 ) < 0 ); }
/** * Builds a comparator from the list of columns in ORDER BY clause. * @param orderByExpressions the columns in ORDER BY clause. * @return the comparator built from the list of columns in ORDER BY clause. */ // ImmutableBytesWritable.Comparator doesn't implement generics @SuppressWarnings("unchecked") private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) { Ordering<ResultEntry> ordering = null; int pos = 0; for (OrderByExpression col : orderByExpressions) { Expression e = col.getExpression(); Comparator<ImmutableBytesWritable> comparator = e.getSortOrder() == SortOrder.DESC && !e.getDataType().isFixedWidth() ? buildDescVarLengthComparator() : new ImmutableBytesWritable.Comparator(); Ordering<ImmutableBytesWritable> o = Ordering.from(comparator); if(!col.isAscending()) o = o.reverse(); o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst(); Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++)); ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering); } return ordering; }
/** * Builds a comparator from the list of columns in ORDER BY clause. * @param orderByExpressions the columns in ORDER BY clause. * @return the comparator built from the list of columns in ORDER BY clause. */ // ImmutableBytesWritable.Comparator doesn't implement generics @SuppressWarnings("unchecked") private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) { Ordering<ResultEntry> ordering = null; int pos = 0; for (OrderByExpression col : orderByExpressions) { Ordering<ImmutableBytesWritable> o = Ordering.from(new ImmutableBytesWritable.Comparator()); if(!col.isAscending()) o = o.reverse(); o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst(); Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++)); ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering); } return ordering; }
private static Comparator<ImmutableBytesWritable> buildDescVarLengthComparator() { return new Comparator<ImmutableBytesWritable>() { @Override public int compare(ImmutableBytesWritable o1, ImmutableBytesWritable o2) { return DescVarLengthFastByteComparisons.compareTo( o1.get(), o1.getOffset(), o1.getLength(), o2.get(), o2.getOffset(), o2.getLength()); } }; }
public void testSpecificCompare() { ImmutableBytesWritable ibw1 = new ImmutableBytesWritable(new byte[]{0x0f}); ImmutableBytesWritable ibw2 = new ImmutableBytesWritable(new byte[]{0x00, 0x00}); ImmutableBytesWritable.Comparator c = new ImmutableBytesWritable.Comparator(); assertFalse("ibw1 < ibw2", c.compare( ibw1, ibw2 ) < 0 ); }
private void doComparisonsOnObjects(ImmutableBytesWritable a, ImmutableBytesWritable b, int expectedSignum) { ImmutableBytesWritable.Comparator comparator = new ImmutableBytesWritable.Comparator(); assertEquals( "Comparing " + a + " and " + b + " as objects", signum(comparator.compare(a, b)), expectedSignum); assertEquals( "Comparing " + a + " and " + b + " as objects (inverse)", -signum(comparator.compare(b, a)), expectedSignum); }