/** * Gets the values with which the specified {@code keys} are associated. * * @param keys the keys with which the associated values are to be fetched. * @return a map of the keys that were found and their respective values. * @throws NullPointerException if the specified {@code keys} list, or any of the keys, is {@code null}. */ default Map<K, V> getAll(List<K> keys) { Map<K, V> map = new HashMap<>(keys.size()); for (K key : keys) { V value = get(key); if (value != null) { map.put(key, value); } } return map; }
@Before public void setUp() { keys = Arrays.asList("k1", "k2", "k3"); values = new HashMap<>(); values.put("k1", "v1"); values.put("k2", "v2"); values.put("k3", null); kvStore = mock(KeyValueStore.class); when(kvStore.get("k1")).thenReturn("v1"); when(kvStore.get("k2")).thenReturn("v2"); when(kvStore.getAll(keys)).thenReturn(values); getNs = new Timer(""); getAllNs = new Timer(""); numGets = new Counter(""); numGetAlls = new Counter(""); getCallbackNs = new Timer(""); numMissedLookups = new Counter(""); metricsRegistry = mock(MetricsRegistry.class); String groupName = LocalTable.class.getSimpleName(); when(metricsRegistry.newCounter(groupName, TABLE_ID + "-num-gets")).thenReturn(numGets); when(metricsRegistry.newCounter(groupName, TABLE_ID + "-num-getAlls")).thenReturn(numGetAlls); when(metricsRegistry.newCounter(groupName, TABLE_ID + "-num-missed-lookups")).thenReturn(numMissedLookups); when(metricsRegistry.newTimer(groupName, TABLE_ID + "-get-ns")).thenReturn(getNs); when(metricsRegistry.newTimer(groupName, TABLE_ID + "-getAll-ns")).thenReturn(getAllNs); when(metricsRegistry.newTimer(groupName, TABLE_ID + "-get-callback-ns")).thenReturn(getCallbackNs); }
/** * Gets the values with which the specified {@code keys} are associated. * * @param keys the keys with which the associated values are to be fetched. * @return a map of the keys that were found and their respective values. * @throws NullPointerException if the specified {@code keys} list, or any of the keys, is {@code null}. */ default Map<K, V> getAll(List<K> keys) { Map<K, V> map = new HashMap<>(keys.size()); for (K key : keys) { V value = get(key); if (value != null) { map.put(key, value); } } return map; }
@Override public V get(K key) { readMetrics.numGets.inc(); long startNs = System.nanoTime(); V result = kvStore.get(key); readMetrics.getNs.update(System.nanoTime() - startNs); return result; }
@Override public V get(K key) { readMetrics.numGets.inc(); long startNs = System.nanoTime(); V result = kvStore.get(key); readMetrics.getNs.update(System.nanoTime() - startNs); return result; }
@Override public V get(K key) { V result = instrument(metrics.numGets, metrics.getNs, () -> kvStore.get(key)); if (result == null) { incCounter(metrics.numMissedLookups); } return result; }
@Override public Collection<JM> handleMessage(M message, MessageCollector collector, TaskCoordinator coordinator) { try { KeyValueStore<K, TimestampedValue<M>> thisState = thisPartialJoinFn.getState(); KeyValueStore<K, TimestampedValue<OM>> otherState = otherPartialJoinFn.getState(); K key = thisPartialJoinFn.getKey(message); thisState.put(key, new TimestampedValue<>(message, clock.currentTimeMillis())); TimestampedValue<OM> otherMessage = otherState.get(key); long now = clock.currentTimeMillis(); if (otherMessage != null && otherMessage.getTimestamp() > now - ttlMs) { JM joinResult = thisPartialJoinFn.apply(message, otherMessage.getValue()); return Collections.singletonList(joinResult); } } catch (Exception e) { throw new SamzaException("Error handling message in PartialJoinOperatorImpl " + getOpImplId(), e); } return Collections.emptyList(); }
@Override public Collection<JM> handleMessage(M message, MessageCollector collector, TaskCoordinator coordinator) { try { KeyValueStore<K, TimestampedValue<M>> thisState = thisPartialJoinFn.getState(); KeyValueStore<K, TimestampedValue<OM>> otherState = otherPartialJoinFn.getState(); K key = thisPartialJoinFn.getKey(message); thisState.put(key, new TimestampedValue<>(message, clock.currentTimeMillis())); TimestampedValue<OM> otherMessage = otherState.get(key); long now = clock.currentTimeMillis(); if (otherMessage != null && otherMessage.getTimestamp() > now - ttlMs) { JM joinResult = thisPartialJoinFn.apply(message, otherMessage.getValue()); return Collections.singletonList(joinResult); } } catch (Exception e) { throw new SamzaException("Error handling message in PartialJoinOperatorImpl " + getOpImplId(), e); } return Collections.emptyList(); }
@Override public Collection<JM> handleMessage(M message, MessageCollector collector, TaskCoordinator coordinator) { try { KeyValueStore<K, TimestampedValue<M>> thisState = thisPartialJoinFn.getState(); KeyValueStore<K, TimestampedValue<OM>> otherState = otherPartialJoinFn.getState(); K key = thisPartialJoinFn.getKey(message); thisState.put(key, new TimestampedValue<>(message, clock.currentTimeMillis())); TimestampedValue<OM> otherMessage = otherState.get(key); long now = clock.currentTimeMillis(); if (otherMessage != null && otherMessage.getTimestamp() > now - ttlMs) { JM joinResult = thisPartialJoinFn.apply(message, otherMessage.getValue()); return Collections.singletonList(joinResult); } } catch (Exception e) { throw new SamzaException("Error handling message in PartialJoinOperatorImpl " + getOpImplId(), e); } return Collections.emptyList(); }
@Override public Collection<JM> handleMessage(M message, MessageCollector collector, TaskCoordinator coordinator) { try { KeyValueStore<K, TimestampedValue<M>> thisState = thisPartialJoinFn.getState(); KeyValueStore<K, TimestampedValue<OM>> otherState = otherPartialJoinFn.getState(); K key = thisPartialJoinFn.getKey(message); thisState.put(key, new TimestampedValue<>(message, clock.currentTimeMillis())); TimestampedValue<OM> otherMessage = otherState.get(key); long now = clock.currentTimeMillis(); if (otherMessage != null && otherMessage.getTimestamp() > now - ttlMs) { JM joinResult = thisPartialJoinFn.apply(message, otherMessage.getValue()); return Collections.singletonList(joinResult); } } catch (Exception e) { throw new SamzaException("Error handling message in PartialJoinOperatorImpl " + getOpImplId(), e); } return Collections.emptyList(); }
@Override public Collection<JM> handleMessage(M message, MessageCollector collector, TaskCoordinator coordinator) { try { KeyValueStore<K, TimestampedValue<M>> thisState = thisPartialJoinFn.getState(); KeyValueStore<K, TimestampedValue<OM>> otherState = otherPartialJoinFn.getState(); K key = thisPartialJoinFn.getKey(message); thisState.put(key, new TimestampedValue<>(message, clock.currentTimeMillis())); TimestampedValue<OM> otherMessage = otherState.get(key); long now = clock.currentTimeMillis(); if (otherMessage != null && otherMessage.getTimestamp() > now - ttlMs) { JM joinResult = thisPartialJoinFn.apply(message, otherMessage.getValue()); return Collections.singletonList(joinResult); } } catch (Exception e) { throw new SamzaException("Error handling message in PartialJoinOperatorImpl " + getOpImplId(), e); } return Collections.emptyList(); }
when(mockLeftStore.get(eq(joinKey))).thenReturn(new TimestampedValue<>(mockLeftMessage, currentTimeMillis)); IncomingMessageEnvelope leftMessage = new IncomingMessageEnvelope(mock(SystemStreamPartition.class), "", "", mockLeftMessage); inputOpImpl1.onMessage(leftMessage, mock(MessageCollector.class), mock(TaskCoordinator.class)); when(mockRightStore.get(eq(joinKey))).thenReturn(new TimestampedValue<>(mockRightMessage, currentTimeMillis)); IncomingMessageEnvelope rightMessage = new IncomingMessageEnvelope(mock(SystemStreamPartition.class), "", "", mockRightMessage); inputOpImpl2.onMessage(rightMessage, mock(MessageCollector.class), mock(TaskCoordinator.class));
@Test public void testGet() throws Exception { ReadWriteTable table = createTable(false); Assert.assertEquals("v1", table.get("k1")); Assert.assertEquals("v2", table.getAsync("k2").get()); Assert.assertNull(table.get("k3")); verify(kvStore, times(3)).get(any()); Assert.assertEquals(3, numGets.getCount()); Assert.assertEquals(1, numMissedLookups.getCount()); Assert.assertTrue(getNs.getSnapshot().getAverage() > 0); Assert.assertEquals(0, numGetAlls.getCount()); Assert.assertEquals(0, getAllNs.getSnapshot().getAverage(), 0.001); Assert.assertEquals(0, getCallbackNs.getSnapshot().getAverage(), 0.001); }