public RegexTemplate(String template) { this.template = requireNonNull(template, "template is null"); try { this.pattern = Pattern.compile(template); } catch (Exception e) { throw new IllegalArgumentException("Invalid template: " + template, e); } Map<String, Integer> namedGroups; try { namedGroups = (Map<String, Integer>) NAMED_GROUPS_METHOD.invoke(pattern); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } ImmutableSortedMap<Integer, String> sortedGroups = ImmutableSortedMap.copyOf(ImmutableBiMap.copyOf(namedGroups).inverse()); this.fieldNames = ImmutableList.copyOf(sortedGroups.values()); }
static <K, V> ImmutableSortedMap<K, V> emptyMap(Comparator<? super K> comparator) { if (Ordering.natural().equals(comparator)) { return of(); } else { return new ImmutableSortedMap<>( ImmutableSortedSet.emptySet(comparator), ImmutableList.<V>of()); } }
@Override public List<Entry<String, Integer>> create(Object... elements) { ImmutableSortedMap.Builder<String, Integer> builder = ImmutableSortedMap.naturalOrder(); for (Object o : elements) { @SuppressWarnings("unchecked") Entry<String, Integer> entry = (Entry<String, Integer>) o; builder.put(entry); } return builder.build().entrySet().asList(); } }
private ImmutableSortedMap<K, V> getSubMap(int fromIndex, int toIndex) { if (fromIndex == 0 && toIndex == size()) { return this; } else if (fromIndex == toIndex) { return emptyMap(comparator()); } else { return new ImmutableSortedMap<>( keySet.getSubSet(fromIndex, toIndex), valueList.subList(fromIndex, toIndex)); } }
@Override public ImmutableSortedMap<K, V> descendingMap() { // TODO(kevinb): the descendingMap is never actually cached at all. Either it should be or the // code below simplified. ImmutableSortedMap<K, V> result = descendingMap; if (result == null) { if (isEmpty()) { return result = emptyMap(Ordering.from(comparator()).reverse()); } else { return result = new ImmutableSortedMap<>( (RegularImmutableSortedSet<K>) keySet.descendingSet(), valueList.reverse(), this); } } return result; }
newInput.getRowType().getFieldList(); continue; int newInputPos = frame.oldToNewOutputs.get(i); projects.add(RexInputRef.of2(newInputPos, newInputOutput)); mapNewInputToProjOutputs.put(newInputPos, newPos); if (!frame.corDefOutputs.isEmpty()) { : frame.corDefOutputs.entrySet()) { projects.add(RexInputRef.of2(entry.getValue(), newInputOutput)); corDefOutputs.put(entry.getKey(), newPos); mapNewInputToProjOutputs.put(entry.getValue(), newPos); newPos++; Map<Integer, Integer> combinedMap = Maps.newHashMap(); for (Integer oldInputPos : frame.oldToNewOutputs.keySet()) { combinedMap.put(oldInputPos, mapNewInputToProjOutputs.get( frame.oldToNewOutputs.get(oldInputPos))); for (Map.Entry<Integer, RexLiteral> entry : omittedConstants.descendingMap().entrySet()) { postProjects.add(entry.getKey() + frame.corDefOutputs.size(), entry.getValue());
Map<Integer, Integer> mapOldToNewOutputs = Maps.newHashMap(); int oldLeftFieldCount = oldLeft.getRowType().getFieldCount(); int newLeftFieldCount = leftFrame.r.getRowType().getFieldCount(); int oldRightFieldCount = oldRight.getRowType().getFieldCount(); assert rel.getRowType().getFieldCount() == oldLeftFieldCount + oldRightFieldCount; rightFrame.oldToNewOutputs.get(i) + newLeftFieldCount); : rightFrame.corDefOutputs.entrySet()) { corDefOutputs.put(entry.getKey(), entry.getValue() + newLeftFieldCount);
RexUtil.composeConjunction(rexBuilder, conditions, false); RelNode newRel = HiveSemiJoin.getSemiJoin(frame.r.getCluster(), frame.r.getTraitSet(), join.getLeft(), join.getRight(), condition, join.getLeftKeys(), join.getRightKeys()); if (rightFrame.corDefOutputs.isEmpty()) { return null; <= rightFrame.corDefOutputs.keySet().size(); rightFrame.r.getRowType().getFieldList(); final int newLeftPos = leftFrame.oldToNewOutputs.get(corDef.field); final int newRightPos = rightOutput.getValue(); SqlOperator callOp = corDef.getPredicateKind() == null ? corDefOutputs.remove(corDef); int oldLeftFieldCount = oldLeft.getRowType().getFieldCount(); int oldRightFieldCount = oldRight.getRowType().getFieldCount(); mapOldToNewOutputs.put( i + oldLeftFieldCount, rightFrame.oldToNewOutputs.get(i) + newLeftFieldCount);
private void testImmutableXXX() { assertEquals(5, ImmutableList.of(1, 2, 3, 4, 5).size()); assertEquals(5, ImmutableSet.of(1, 2, 3, 4, 5).size()); assertEquals(5, ImmutableSortedSet.of(1, 2, 3, 4, 5).size()); assertEquals(3, ImmutableMap.of(1, 2, 3, 4, 5, 6).size()); assertEquals(3, ImmutableSortedMap.of(1, 2, 3, 4, 5, 6).size()); assertEquals(9, ImmutableMultiset.of(1, 1, 2, 2, 3, 3, 4, 5, 6).size()); assertEquals(6, ImmutableMultiset.of(1, 1, 2, 2, 3, 3, 4, 5, 6).elementSet().size()); assertEquals(2, ImmutableMultiset.of(1, 1, 2, 2, 3, 3, 4, 5, 6).count(1)); } }
public void testGet_collections() { assertEquals(ImmutableSet.of().iterator(), ArbitraryInstances.get(Iterator.class)); assertFalse(ArbitraryInstances.get(PeekingIterator.class).hasNext()); assertFalse(ArbitraryInstances.get(ListIterator.class).hasNext()); assertEquals(ImmutableSet.of(), ArbitraryInstances.get(Iterable.class)); assertEquals(ImmutableSet.of(), ArbitraryInstances.get(Set.class)); assertEquals(ImmutableSet.of(), ArbitraryInstances.get(ImmutableSet.class)); assertEquals(ImmutableSortedSet.of(), ArbitraryInstances.get(SortedSet.class)); assertEquals(ImmutableSortedSet.of(), ArbitraryInstances.get(ImmutableSortedSet.class)); assertEquals(ImmutableList.of(), ArbitraryInstances.get(Collection.class)); assertEquals(ImmutableList.of(), ArbitraryInstances.get(ImmutableCollection.class)); assertEquals(ImmutableList.of(), ArbitraryInstances.get(List.class)); assertEquals(ImmutableList.of(), ArbitraryInstances.get(ImmutableList.class)); assertEquals(ImmutableMap.of(), ArbitraryInstances.get(Map.class)); assertEquals(ImmutableMap.of(), ArbitraryInstances.get(ImmutableMap.class)); assertEquals(ImmutableSortedMap.of(), ArbitraryInstances.get(SortedMap.class)); assertEquals(ImmutableSortedMap.of(), ArbitraryInstances.get(ImmutableSortedMap.class)); assertEquals(ImmutableMultiset.of(), ArbitraryInstances.get(Multiset.class)); assertEquals(ImmutableMultiset.of(), ArbitraryInstances.get(ImmutableMultiset.class));
public void testSortedMapDifferenceImmutable() { SortedMap<Integer, String> left = Maps.newTreeMap(ImmutableSortedMap.of(1, "a", 2, "b", 3, "c", 4, "d", 5, "e")); SortedMap<Integer, String> right = Maps.newTreeMap(ImmutableSortedMap.of(1, "a", 3, "f", 5, "g", 6, "z")); SortedMapDifference<Integer, String> diff1 = Maps.difference(left, right); left.put(6, "z"); assertFalse(diff1.areEqual()); assertThat(diff1.entriesOnlyOnLeft().entrySet()) .containsExactly(Maps.immutableEntry(2, "b"), Maps.immutableEntry(4, "d")) .inOrder(); assertThat(diff1.entriesOnlyOnRight().entrySet()).contains(Maps.immutableEntry(6, "z")); assertThat(diff1.entriesInCommon().entrySet()).contains(Maps.immutableEntry(1, "a")); assertThat(diff1.entriesDiffering().entrySet())
public void testAsMapSortedReadsThrough() { SortedSet<String> strings = new NonNavigableSortedSet(); Collections.addAll(strings, "one", "two", "three"); SortedMap<String, Integer> map = Maps.asMap(strings, LENGTH_FUNCTION); assertNull(map.comparator()); assertEquals(ImmutableSortedMap.of("one", 3, "two", 3, "three", 5), map); assertNull(map.get("four")); strings.add("four"); assertEquals(ImmutableSortedMap.of("one", 3, "two", 3, "three", 5, "four", 4), map); assertEquals(Integer.valueOf(4), map.get("four")); SortedMap<String, Integer> headMap = map.headMap("two"); assertEquals(ImmutableSortedMap.of("four", 4, "one", 3, "three", 5), headMap); strings.add("five"); strings.remove("one"); assertEquals(ImmutableSortedMap.of("five", 4, "four", 4, "three", 5), headMap); assertThat(map.entrySet()) .containsExactly( mapEntry("five", 4), mapEntry("four", 4), mapEntry("three", 5), mapEntry("two", 3)) .inOrder(); }
@Override public SortedMap<byte[], RowResult<byte[]>> getRows(TableReference tableRef, Iterable<byte[]> rows, ColumnSelection columnSelection) { Timer.Context timer = getTimer("getRows").time(); checkGetPreconditions(tableRef); if (Iterables.isEmpty(rows)) { return AbstractTransaction.EMPTY_SORTED_ROWS; } hasReads = true; ImmutableMap.Builder<Cell, byte[]> result = ImmutableSortedMap.naturalOrder(); Map<Cell, Value> rawResults = Maps.newHashMap( keyValueService.getRows(tableRef, rows, columnSelection, getStartTimestamp())); SortedMap<Cell, byte[]> writes = writesByTable.get(tableRef); if (writes != null) { for (byte[] row : rows) { extractLocalWritesForRow(result, writes, row, columnSelection); } } // We don't need to do work postFiltering if we have a write locally. rawResults.keySet().removeAll(result.build().keySet()); SortedMap<byte[], RowResult<byte[]>> results = filterRowResults(tableRef, rawResults, result); long getRowsMillis = TimeUnit.NANOSECONDS.toMillis(timer.stop()); if (perfLogger.isDebugEnabled()) { perfLogger.debug("getRows({}, {} rows) found {} rows, took {} ms", tableRef, Iterables.size(rows), results.size(), getRowsMillis); } validatePreCommitRequirementsOnReadIfNecessary(tableRef, getStartTimestamp()); return results; }
ImmutableSortedMap.Builder<Key,Value> kvBuilder = null; if (buildKeyValueMap) { kvBuilder = ImmutableSortedMap.naturalOrder(); Builder<String> scansBuilder = ImmutableList.builder(); Builder<LogEntry> logsBuilder = ImmutableList.builder(); final ImmutableSet.Builder<String> loadedFilesBuilder = ImmutableSet.builder(); ByteSequence row = null; kvBuilder.put(key, kv.getValue()); te.logs = logsBuilder.build(); if (buildKeyValueMap) { te.keyValues = kvBuilder.build();
final List<RelDataTypeField> relOutput = rel.getRowType().getFieldList(); for (Map.Entry<CorDef, Integer> entry : frame.corDefOutputs.entrySet()) { projects.add( RexInputRef.of2(entry.getValue(), frame.r.getRowType().getFieldList())); corDefOutputs.put(entry.getKey(), newPos); newPos++;
final int newCorVarOffset = frame.oldToNewOutputs.get(oldCorVarOffset); HiveRelFactories.HIVE_PROJECT_FACTORY, newInput, mapNewInputToOutputs.get(newInput)); RelNode distinct = relBuilder.push(project).distinct().build(); RelOptCluster cluster = distinct.getCluster(); offset += distinct.getRowType().getFieldCount(); mapNewInputToOutputs.get(newInput); final int newLocalOutput = frame.oldToNewOutputs.get(corRef.field); + valueGenFieldOffset; corDefOutputs.put(corRef.def(), newOutput);
public void testSortedMapDifferenceTypical() { SortedMap<Integer, String> left = ImmutableSortedMap.<Integer, String>reverseOrder() .put(1, "a") .put(2, "b") .put(3, "c") .put(4, "d") .put(5, "e") .build(); SortedMap<Integer, String> right = ImmutableSortedMap.of(1, "a", 3, "f", 5, "g", 6, "z"); SortedMapDifference<Integer, String> diff1 = Maps.difference(left, right); assertFalse(diff1.areEqual()); assertThat(diff1.entriesOnlyOnLeft().entrySet()) .containsExactly(Maps.immutableEntry(4, "d"), Maps.immutableEntry(2, "b")) .inOrder(); assertThat(diff1.entriesOnlyOnRight().entrySet()).contains(Maps.immutableEntry(6, "z")); assertThat(diff1.entriesInCommon().entrySet()).contains(Maps.immutableEntry(1, "a")); assertThat(diff1.entriesDiffering().entrySet()) .containsExactly(
private <T> SortedMap<Cell, T> postFilterRows(TableReference tableRef, List<RowResult<Value>> rangeRows, Function<Value, T> transformer) { ensureUncommitted(); if (rangeRows.isEmpty()) { return ImmutableSortedMap.of(); } Map<Cell, Value> rawResults = Maps.newHashMapWithExpectedSize(estimateSize(rangeRows)); for (RowResult<Value> rowResult : rangeRows) { for (Map.Entry<byte[], Value> e : rowResult.getColumns().entrySet()) { rawResults.put(Cell.create(rowResult.getRowName(), e.getKey()), e.getValue()); } } ImmutableSortedMap.Builder<Cell, T> postFilter = ImmutableSortedMap.naturalOrder(); getWithPostFiltering(tableRef, rawResults, postFilter, transformer); return postFilter.build(); }