use of java.util.Collections.singletonList in project phoenix by apache.
the class WhereOptimizer method pushKeyExpressionsToScan.
// For testing so that the extractedNodes can be verified
public static Expression pushKeyExpressionsToScan(StatementContext context, FilterableStatement statement, Expression whereClause, Set<Expression> extractNodes) throws SQLException {
PName tenantId = context.getConnection().getTenantId();
byte[] tenantIdBytes = null;
PTable table = context.getCurrentTable().getTable();
Integer nBuckets = table.getBucketNum();
boolean isSalted = nBuckets != null;
RowKeySchema schema = table.getRowKeySchema();
boolean isMultiTenant = tenantId != null && table.isMultiTenant();
boolean isSharedIndex = table.getViewIndexId() != null;
if (isMultiTenant) {
tenantIdBytes = ScanUtil.getTenantIdBytes(schema, isSalted, tenantId, isSharedIndex);
}
if (whereClause == null && (tenantId == null || !table.isMultiTenant()) && table.getViewIndexId() == null) {
context.setScanRanges(ScanRanges.EVERYTHING);
return whereClause;
}
if (LiteralExpression.isBooleanFalseOrNull(whereClause)) {
context.setScanRanges(ScanRanges.NOTHING);
return null;
}
KeyExpressionVisitor visitor = new KeyExpressionVisitor(context, table);
KeyExpressionVisitor.KeySlots keySlots = null;
if (whereClause != null) {
// TODO:: When we only have one where clause, the keySlots returns as a single slot object,
// instead of an array of slots for the corresponding column. Change the behavior so it
// becomes consistent.
keySlots = whereClause.accept(visitor);
if (keySlots == null && (tenantId == null || !table.isMultiTenant()) && table.getViewIndexId() == null) {
context.setScanRanges(ScanRanges.EVERYTHING);
return whereClause;
}
// for unequal lengths.
if (keySlots == KeyExpressionVisitor.EMPTY_KEY_SLOTS) {
context.setScanRanges(ScanRanges.NOTHING);
return null;
}
}
if (keySlots == null) {
keySlots = KeyExpressionVisitor.EMPTY_KEY_SLOTS;
}
if (extractNodes == null) {
extractNodes = new HashSet<Expression>(table.getPKColumns().size());
}
int pkPos = 0;
int nPKColumns = table.getPKColumns().size();
int[] slotSpan = new int[nPKColumns];
List<List<KeyRange>> cnf = Lists.newArrayListWithExpectedSize(schema.getMaxFields());
KeyRange minMaxRange = keySlots.getMinMaxRange();
if (minMaxRange == null) {
minMaxRange = KeyRange.EVERYTHING_RANGE;
}
boolean hasMinMaxRange = (minMaxRange != KeyRange.EVERYTHING_RANGE);
int minMaxRangeOffset = 0;
byte[] minMaxRangePrefix = null;
boolean hasViewIndex = table.getViewIndexId() != null;
if (hasMinMaxRange) {
int minMaxRangeSize = (isSalted ? SaltingUtil.NUM_SALTING_BYTES : 0) + (isMultiTenant ? tenantIdBytes.length + 1 : 0) + (hasViewIndex ? MetaDataUtil.getViewIndexIdDataType().getByteSize() : 0);
minMaxRangePrefix = new byte[minMaxRangeSize];
}
Iterator<KeyExpressionVisitor.KeySlot> iterator = keySlots.iterator();
// Add placeholder for salt byte ranges
if (isSalted) {
cnf.add(SALT_PLACEHOLDER);
if (hasMinMaxRange) {
System.arraycopy(SALT_PLACEHOLDER.get(0).getLowerRange(), 0, minMaxRangePrefix, minMaxRangeOffset, SaltingUtil.NUM_SALTING_BYTES);
minMaxRangeOffset += SaltingUtil.NUM_SALTING_BYTES;
}
// Increment the pkPos, as the salt column is in the row schema
// Do not increment the iterator, though, as there will never be
// an expression in the keySlots for the salt column
pkPos++;
}
// that different indexes don't interleave.
if (hasViewIndex) {
byte[] viewIndexBytes = MetaDataUtil.getViewIndexIdDataType().toBytes(table.getViewIndexId());
KeyRange indexIdKeyRange = KeyRange.getKeyRange(viewIndexBytes);
cnf.add(singletonList(indexIdKeyRange));
if (hasMinMaxRange) {
System.arraycopy(viewIndexBytes, 0, minMaxRangePrefix, minMaxRangeOffset, viewIndexBytes.length);
minMaxRangeOffset += viewIndexBytes.length;
}
pkPos++;
}
// Add tenant data isolation for tenant-specific tables
if (isMultiTenant) {
KeyRange tenantIdKeyRange = KeyRange.getKeyRange(tenantIdBytes);
cnf.add(singletonList(tenantIdKeyRange));
if (hasMinMaxRange) {
System.arraycopy(tenantIdBytes, 0, minMaxRangePrefix, minMaxRangeOffset, tenantIdBytes.length);
minMaxRangeOffset += tenantIdBytes.length;
Field f = schema.getField(pkPos);
if (!f.getDataType().isFixedWidth()) {
minMaxRangePrefix[minMaxRangeOffset] = SchemaUtil.getSeparatorByte(schema.rowKeyOrderOptimizable(), tenantIdBytes.length == 0, f);
minMaxRangeOffset++;
}
}
pkPos++;
}
// range with the other range.
if (hasMinMaxRange) {
minMaxRange = minMaxRange.prependRange(minMaxRangePrefix, 0, minMaxRangeOffset);
}
boolean forcedSkipScan = statement.getHint().hasHint(Hint.SKIP_SCAN);
boolean forcedRangeScan = statement.getHint().hasHint(Hint.RANGE_SCAN);
boolean hasUnboundedRange = false;
boolean hasMultiRanges = false;
boolean hasRangeKey = false;
boolean stopExtracting = false;
boolean useSkipScan = false;
// Concat byte arrays of literals to form scan start key
while (iterator.hasNext()) {
KeyExpressionVisitor.KeySlot slot = iterator.next();
// If the slot is null this means we have no entry for this pk position.
if (slot == null || slot.getKeyRanges().isEmpty()) {
continue;
}
if (slot.getPKPosition() != pkPos) {
if (!forcedSkipScan) {
stopExtracting = true;
} else {
useSkipScan |= !stopExtracting && !forcedRangeScan && forcedSkipScan;
}
for (int i = pkPos; i < slot.getPKPosition(); i++) {
cnf.add(Collections.singletonList(KeyRange.EVERYTHING_RANGE));
}
}
KeyPart keyPart = slot.getKeyPart();
slotSpan[cnf.size()] = slot.getPKSpan() - 1;
pkPos = slot.getPKPosition() + slot.getPKSpan();
// Skip span-1 slots as we skip one at the top of the loop
for (int i = 1; i < slot.getPKSpan() && iterator.hasNext(); i++) {
iterator.next();
}
List<KeyRange> keyRanges = slot.getKeyRanges();
cnf.add(keyRanges);
// TODO: when stats are available, we may want to use a skip scan if the
// cardinality of this slot is low.
/*
* Stop extracting nodes once we encounter:
* 1) An unbound range unless we're forcing a skip scan and havn't encountered
* a multi-column span. Even if we're trying to force a skip scan, we can't
* execute it over a multi-column span.
* 2) A non range key as we can extract the first one, but further ones need
* to be evaluated in a filter.
*/
stopExtracting |= (hasUnboundedRange && !forcedSkipScan) || (hasRangeKey && forcedRangeScan);
useSkipScan |= !stopExtracting && !forcedRangeScan && (keyRanges.size() > 1 || hasRangeKey);
for (int i = 0; (!hasUnboundedRange || !hasRangeKey) && i < keyRanges.size(); i++) {
KeyRange range = keyRanges.get(i);
if (range.isUnbound()) {
hasUnboundedRange = hasRangeKey = true;
} else if (!range.isSingleKey()) {
hasRangeKey = true;
}
}
hasMultiRanges |= keyRanges.size() > 1;
// We cannot extract if we have multiple ranges and are forcing a range scan.
stopExtracting |= forcedRangeScan && hasMultiRanges;
// that, so must filter on the remaining conditions (see issue #467).
if (!stopExtracting) {
List<Expression> nodesToExtract = keyPart.getExtractNodes();
extractNodes.addAll(nodesToExtract);
}
}
// If we have fully qualified point keys with multi-column spans (i.e. RVC),
// we can still use our skip scan. The ScanRanges.create() call will explode
// out the keys.
slotSpan = Arrays.copyOf(slotSpan, cnf.size());
ScanRanges scanRanges = ScanRanges.create(schema, cnf, slotSpan, minMaxRange, nBuckets, useSkipScan, table.getRowTimestampColPos());
context.setScanRanges(scanRanges);
if (whereClause == null) {
return null;
} else {
return whereClause.accept(new RemoveExtractedNodesVisitor(extractNodes));
}
}
use of java.util.Collections.singletonList in project streamsupport by stefan-zobel.
the class MOAT method testQueueAddRemove.
@SuppressWarnings({ "rawtypes", "unchecked" })
private static void testQueueAddRemove(final Queue<Integer> q, final Integer e) {
final List<Integer> originalContents = new ArrayList<Integer>(q);
final boolean isEmpty = q.isEmpty();
final boolean isList = (q instanceof List);
final List asList = isList ? (List) q : null;
check(!q.contains(e));
try {
q.add(e);
} catch (NullPointerException npe) {
check(e == null);
// Null elements not supported
return;
}
check(q.contains(e));
check(q.remove(e));
check(!q.contains(e));
equal(new ArrayList<Integer>(q), originalContents);
if (q instanceof Deque<?>) {
final Deque<Integer> deq = (Deque<Integer>) q;
final List<Integer> singleton = Collections.singletonList(e);
// insert, query, remove element at head
if (isEmpty) {
THROWS(NoSuchElementException.class, () -> deq.getFirst(), () -> deq.element(), () -> deq.iterator().next());
check(deq.peekFirst() == null);
check(deq.peek() == null);
} else {
check(deq.getFirst() != e);
check(deq.element() != e);
check(deq.iterator().next() != e);
check(deq.peekFirst() != e);
check(deq.peek() != e);
}
check(!deq.contains(e));
check(!deq.removeFirstOccurrence(e));
check(!deq.removeLastOccurrence(e));
if (isList) {
check(asList.indexOf(e) == -1);
check(asList.lastIndexOf(e) == -1);
}
switch(rnd.nextInt(isList ? 4 : 3)) {
case 0:
deq.addFirst(e);
break;
case 1:
check(deq.offerFirst(e));
break;
case 2:
deq.push(e);
break;
case 3:
asList.add(0, e);
break;
default:
throw new AssertionError();
}
check(deq.peekFirst() == e);
check(deq.getFirst() == e);
check(deq.element() == e);
check(deq.peek() == e);
check(deq.iterator().next() == e);
check(deq.contains(e));
if (isList) {
check(asList.get(0) == e);
check(asList.indexOf(e) == 0);
check(asList.lastIndexOf(e) == 0);
check(asList.subList(0, 1).equals(singleton));
}
switch(rnd.nextInt(isList ? 11 : 9)) {
case 0:
check(deq.pollFirst() == e);
break;
case 1:
check(deq.removeFirst() == e);
break;
case 2:
check(deq.remove() == e);
break;
case 3:
check(deq.pop() == e);
break;
case 4:
check(deq.removeFirstOccurrence(e));
break;
case 5:
check(deq.removeLastOccurrence(e));
break;
case 6:
check(deq.remove(e));
break;
case 7:
check(deq.removeAll(singleton));
break;
case 8:
Iterator it = deq.iterator();
it.next();
it.remove();
break;
case 9:
asList.remove(0);
break;
case 10:
asList.subList(0, 1).clear();
break;
default:
throw new AssertionError();
}
if (isEmpty) {
THROWS(NoSuchElementException.class, () -> deq.getFirst(), () -> deq.element(), () -> deq.iterator().next());
check(deq.peekFirst() == null);
check(deq.peek() == null);
} else {
check(deq.getFirst() != e);
check(deq.element() != e);
check(deq.iterator().next() != e);
check(deq.peekFirst() != e);
check(deq.peek() != e);
}
check(!deq.contains(e));
check(!deq.removeFirstOccurrence(e));
check(!deq.removeLastOccurrence(e));
if (isList) {
check(isEmpty || asList.get(0) != e);
check(asList.indexOf(e) == -1);
check(asList.lastIndexOf(e) == -1);
}
equal(new ArrayList<Integer>(deq), originalContents);
// insert, query, remove element at tail
if (isEmpty) {
check(deq.peekLast() == null);
THROWS(NoSuchElementException.class, () -> deq.getLast());
} else {
check(deq.peekLast() != e);
check(deq.getLast() != e);
}
switch(rnd.nextInt(isList ? 6 : 4)) {
case 0:
deq.addLast(e);
break;
case 1:
check(deq.offerLast(e));
break;
case 2:
check(deq.add(e));
break;
case 3:
deq.addAll(singleton);
break;
case 4:
asList.addAll(deq.size(), singleton);
break;
case 5:
asList.add(deq.size(), e);
break;
default:
throw new AssertionError();
}
check(deq.peekLast() == e);
check(deq.getLast() == e);
check(deq.contains(e));
if (isList) {
ListIterator it = asList.listIterator(asList.size());
check(it.previous() == e);
check(asList.get(asList.size() - 1) == e);
check(asList.indexOf(e) == asList.size() - 1);
check(asList.lastIndexOf(e) == asList.size() - 1);
int size = asList.size();
check(asList.subList(size - 1, size).equals(singleton));
}
switch(rnd.nextInt(isList ? 8 : 6)) {
case 0:
check(deq.pollLast() == e);
break;
case 1:
check(deq.removeLast() == e);
break;
case 2:
check(deq.removeFirstOccurrence(e));
break;
case 3:
check(deq.removeLastOccurrence(e));
break;
case 4:
check(deq.remove(e));
break;
case 5:
check(deq.removeAll(singleton));
break;
case 6:
asList.remove(asList.size() - 1);
break;
case 7:
ListIterator it = asList.listIterator(asList.size());
it.previous();
it.remove();
break;
default:
throw new AssertionError();
}
if (isEmpty) {
check(deq.peekLast() == null);
THROWS(NoSuchElementException.class, () -> deq.getLast());
} else {
check(deq.peekLast() != e);
check(deq.getLast() != e);
}
check(!deq.contains(e));
equal(new ArrayList<Integer>(deq), originalContents);
// Test operations on empty deque
switch(rnd.nextInt(isList ? 4 : 2)) {
case 0:
deq.clear();
break;
case 1:
Iterator it = deq.iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
break;
case 2:
asList.subList(0, asList.size()).clear();
break;
case 3:
ListIterator lit = asList.listIterator(asList.size());
while (lit.hasPrevious()) {
lit.previous();
lit.remove();
}
break;
default:
throw new AssertionError();
}
testEmptyCollection(deq);
check(!deq.iterator().hasNext());
if (isList) {
check(!asList.listIterator().hasPrevious());
THROWS(NoSuchElementException.class, () -> asList.listIterator().previous());
}
THROWS(NoSuchElementException.class, () -> deq.iterator().next(), () -> deq.element(), () -> deq.getFirst(), () -> deq.getLast(), () -> deq.pop(), () -> deq.remove(), () -> deq.removeFirst(), () -> deq.removeLast());
check(deq.poll() == null);
check(deq.pollFirst() == null);
check(deq.pollLast() == null);
check(deq.peek() == null);
check(deq.peekFirst() == null);
check(deq.peekLast() == null);
check(!deq.removeFirstOccurrence(e));
check(!deq.removeLastOccurrence(e));
check(deq.addAll(originalContents) == !isEmpty);
equal(new ArrayList<Integer>(deq), originalContents);
check(!deq.addAll(Collections.<Integer>emptyList()));
equal(new ArrayList<Integer>(deq), originalContents);
}
}
use of java.util.Collections.singletonList in project bullet-core by yahoo.
the class MetaTest method testMetadataWithErrors.
@Test
public void testMetadataWithErrors() {
BulletError errorA = BulletError.makeError("foo", "bar");
BulletError errorB = BulletError.makeError("baz", "qux");
Meta meta = Meta.of(Arrays.asList(errorA, errorB));
BulletError errorC = BulletError.makeError("norf", "foo");
meta.addErrors(Collections.singletonList(errorC));
Map<String, Object> actual = meta.asMap();
Assert.assertEquals(actual.size(), 1);
List<BulletError> actualErrors = (List<BulletError>) actual.get(Meta.ERROR_KEY);
Assert.assertEquals(actualErrors.size(), 3);
Assert.assertEquals(actualErrors.get(0).getError(), "foo");
Assert.assertEquals(actualErrors.get(0).getResolutions(), singletonList("bar"));
Assert.assertEquals(actualErrors.get(1).getError(), "baz");
Assert.assertEquals(actualErrors.get(1).getResolutions(), singletonList("qux"));
Assert.assertEquals(actualErrors.get(2).getError(), "norf");
Assert.assertEquals(actualErrors.get(2).getResolutions(), singletonList("foo"));
}
use of java.util.Collections.singletonList in project kafka by apache.
the class ConsumerCoordinatorTest method testUpdateMetadataDuringRebalance.
@Test
public void testUpdateMetadataDuringRebalance() {
final String topic1 = "topic1";
final String topic2 = "topic2";
TopicPartition tp1 = new TopicPartition(topic1, 0);
TopicPartition tp2 = new TopicPartition(topic2, 0);
final String consumerId = "leader";
List<String> topics = Arrays.asList(topic1, topic2);
subscriptions.subscribe(new HashSet<>(topics), rebalanceListener);
// we only have metadata for one topic initially
client.updateMetadata(RequestTestUtils.metadataUpdateWith(1, singletonMap(topic1, 1)));
client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
// prepare initial rebalance
Map<String, List<String>> memberSubscriptions = singletonMap(consumerId, topics);
partitionAssignor.prepare(singletonMap(consumerId, Arrays.asList(tp1)));
client.prepareResponse(joinGroupLeaderResponse(1, consumerId, memberSubscriptions, Errors.NONE));
client.prepareResponse(body -> {
SyncGroupRequest sync = (SyncGroupRequest) body;
if (sync.data().memberId().equals(consumerId) && sync.data().generationId() == 1 && sync.groupAssignments().containsKey(consumerId)) {
// trigger the metadata update including both topics after the sync group request has been sent
Map<String, Integer> topicPartitionCounts = new HashMap<>();
topicPartitionCounts.put(topic1, 1);
topicPartitionCounts.put(topic2, 1);
client.updateMetadata(RequestTestUtils.metadataUpdateWith(1, topicPartitionCounts));
return true;
}
return false;
}, syncGroupResponse(Collections.singletonList(tp1), Errors.NONE));
coordinator.poll(time.timer(Long.MAX_VALUE));
// the metadata update should trigger a second rebalance
client.prepareResponse(joinGroupLeaderResponse(2, consumerId, memberSubscriptions, Errors.NONE));
client.prepareResponse(syncGroupResponse(Arrays.asList(tp1, tp2), Errors.NONE));
coordinator.poll(time.timer(Long.MAX_VALUE));
assertFalse(coordinator.rejoinNeededOrPending());
assertEquals(new HashSet<>(Arrays.asList(tp1, tp2)), subscriptions.assignedPartitions());
}
use of java.util.Collections.singletonList in project kafka by apache.
the class TransactionManagerTest method testRaiseErrorWhenNoPartitionsPendingOnDrain.
@Test
public void testRaiseErrorWhenNoPartitionsPendingOnDrain() throws InterruptedException {
doInitTransactions();
transactionManager.beginTransaction();
// Don't execute transactionManager.maybeAddPartitionToTransaction(tp0). This should result in an error on drain.
appendToAccumulator(tp0);
Node node1 = new Node(0, "localhost", 1111);
PartitionInfo part1 = new PartitionInfo(topic, 0, node1, null, null);
Cluster cluster = new Cluster(null, Collections.singletonList(node1), Collections.singletonList(part1), Collections.emptySet(), Collections.emptySet());
Set<Node> nodes = new HashSet<>();
nodes.add(node1);
Map<Integer, List<ProducerBatch>> drainedBatches = accumulator.drain(cluster, nodes, Integer.MAX_VALUE, time.milliseconds());
// We shouldn't drain batches which haven't been added to the transaction yet.
assertTrue(drainedBatches.containsKey(node1.id()));
assertTrue(drainedBatches.get(node1.id()).isEmpty());
}
Aggregations