private void verifyAllCustomersInStore(final List<Customer> customers, final ReadOnlyKeyValueStore<Long, Customer> store) { for (long id = 0; id < customers.size(); id++) { assertThat(store.get(id), equalTo(customers.get((int)id))); } }
public KeyValueIterator<AvroStreamKey, AvroStream> getAllStreams(){ return view.all(); } }
/** * Get all of the key-value pairs that have keys within the range from...to * @param storeName store to query * @param from start of the range (inclusive) * @param to end of the range (inclusive) * @return A List of {@link KeyValueBean}s representing all of the key-values in the provided * store that fall withing the given range. */ @GET() @Path("/keyvalues/{storeName}/range/{from}/{to}") @Produces(MediaType.APPLICATION_JSON) public List<KeyValueBean> keyRangeForStore(@PathParam("storeName") final String storeName, @PathParam("from") final String from, @PathParam("to") final String to) { return rangeForKeyValueStore(storeName, store -> store.range(from, to)); }
@Override public Optional<ClusterValue> getClusterByKey(ClusterKey clusterKey) { ClusterValue clusterValue; Validate.validState(store != null, "Infra Manager should be configured"); try { log.debug("Approximate Num. of Entries in Infra Table-{}", store.approximateNumEntries()); clusterValue = store.get(clusterKey); } catch (Exception e) { throw new IllegalStateException("Error while retrieving the cluster Value using cluster key:" + clusterKey, e); } if (clusterValue == null) { log.error("Cluster Not Found, key: {}", clusterKey); } else { log.info("Cluster Name - {}", clusterValue.getClusterProperties()); } return Optional.ofNullable(clusterValue); } }
@Override public Map<ClusterKey, ClusterValue> getAllClusters() { Map<ClusterKey, ClusterValue> clusterKeyValueMap = new HashMap<>(); try (KeyValueIterator<ClusterKey, ClusterValue> clusterKeyValueIterator = store.all()) { log.debug("Approximate Num. of Entries in Infra Table-{}", store.approximateNumEntries()); while (clusterKeyValueIterator.hasNext()) { KeyValue<ClusterKey, ClusterValue> next = clusterKeyValueIterator.next(); clusterKeyValueMap.put(next.key, next.value); } } catch (Exception e) { throw new IllegalStateException("Infra Manager State Store not initialized ", e); } return clusterKeyValueMap; }
@Override public long approximateNumEntries() { final List<ReadOnlyKeyValueStore<K, V>> stores = storeProvider.stores(storeName, storeType); long total = 0; for (final ReadOnlyKeyValueStore<K, V> store : stores) { total += store.approximateNumEntries(); if (total < 0) { return Long.MAX_VALUE; } } return total; }
private void verifyAllProductsInStore(final List<Product> products, final ReadOnlyKeyValueStore<Long, Product> store) { for (long id = 0; id < products.size(); id++) { assertThat(store.get(id), equalTo(products.get((int)id))); } }
songsStore = streams.store(KafkaMusicExample.ALL_SONGS, QueryableStoreTypes.<Long, Song>keyValueStore()); return songsStore.all().hasNext(); } catch (Exception e) { return false;
@Override public KeyValueIterator<K, V> apply(final ReadOnlyKeyValueStore<K, V> store) { try { return store.range(from, to); } catch (final InvalidStateStoreException e) { throw new InvalidStateStoreException("State store is not available anymore and may have been migrated to another instance; please re-discover its location from the state metadata."); } } };
@GET() @Path("/song/{id}") @Produces(MediaType.APPLICATION_JSON) public SongBean song(@PathParam("id") Long songId) { final ReadOnlyKeyValueStore<Long, Song> songStore = streams.store(KafkaMusicExample.ALL_SONGS, QueryableStoreTypes.<Long, Song>keyValueStore()); final Song song = songStore.get(songId); if (song == null) { throw new NotFoundException(String.format("Song with id [%d] was not found", songId)); } return new SongBean(song.getArtist(), song.getAlbum(), song.getName()); }
songsStore = streams.store(KafkaMusicExample.ALL_SONGS, QueryableStoreTypes.<Long, Song>keyValueStore()); return songsStore.all().hasNext(); } catch (Exception e) { return false;
/** * Asserts that the key-value store contains exactly the expected content and nothing more. * * @param store the store to be validated * @param expected the expected contents of the store * @param <K> the store's key type * @param <V> the store's value type */ public static <K, V> void assertThatKeyValueStoreContains(ReadOnlyKeyValueStore<K, V> store, Map<K, V> expected) throws InterruptedException { TestUtils.waitForCondition(() -> expected.keySet() .stream() .allMatch(k -> expected.get(k).equals(store.get(k))), 30000, "Expected values not found in KV store"); }
KeyValueIterator<String, String> keyValueIterator = readOnlyKeyValueStore.all(); assertThat(keyValueIterator).containsExactlyElementsOf(expectedResultsForJoinStateStore);
/** * Fetch the order from the local materialized view * * @param id ID to fetch * @param asyncResponse the response to call once completed * @param predicate a filter that for this fetch, so for example we might fetch only VALIDATED * orders. */ private void fetchLocal(String id, AsyncResponse asyncResponse, Predicate<String, Order> predicate) { log.info("running GET on this node"); try { Order order = ordersStore().get(id); if (order == null || !predicate.test(id, order)) { log.info("Delaying get as order not present for id " + id); outstandingRequests.put(id, new FilteredResponse<>(asyncResponse, predicate)); } else { asyncResponse.resume(toBean(order)); } } catch (InvalidStateStoreException e) { //Store not ready so delay outstandingRequests.put(id, new FilteredResponse<>(asyncResponse, predicate)); } }
@Override public KeyValueIterator<K, V> apply(final ReadOnlyKeyValueStore<K, V> store) { try { return store.all(); } catch (final InvalidStateStoreException e) { throw new InvalidStateStoreException("State store is not available anymore and may have been migrated to another instance; please re-discover its location from the state metadata."); } } };
/** * Get a key-value pair from a KeyValue Store * @param storeName the store to look in * @param key the key to get * @return {@link KeyValueBean} representing the key-value pair */ @GET @Path("/keyvalue/{storeName}/{key}") @Produces(MediaType.APPLICATION_JSON) public KeyValueBean byKey(@PathParam("storeName") final String storeName, @PathParam("key") final String key) { HostStoreInfo hostStoreInfo = streamsMetadataForStoreAndKey(storeName, key); if (!thisHost(hostStoreInfo)){ return fetchByKey(hostStoreInfo, "state/keyvalue/"+storeName+"/"+key); } // Lookup the KeyValueStore with the provided storeName final ReadOnlyKeyValueStore<String, Long> store = streams.store(storeName, QueryableStoreTypes.<String, Long>keyValueStore()); if (store == null) { throw new NotFoundException(); } // Get the value from the store final Long value = store.get(key); if (value == null) { throw new NotFoundException(); } return new KeyValueBean(key, value); }
@Override public Map<String, OpennmsModelProtos.Alarm> getAlarms() { final Map<String, OpennmsModelProtos.Alarm> alarmsByReductionKey = new LinkedHashMap<>(); getAlarmTableNow().all().forEachRemaining(kv -> { try { alarmsByReductionKey.put(kv.key, kv.value != null ? OpennmsModelProtos.Alarm.parseFrom(kv.value) : null); } catch (InvalidProtocolBufferException e) { LOG.error("Failed to parse alarm for bytes at reduction key '{}'. Alarm will be empty in map.", kv.key); alarmsByReductionKey.put(kv.key, null); } }); return alarmsByReductionKey; }
streams.store(storeName, QueryableStoreTypes.<String, KafkaMusicExample.TopFiveSongs>keyValueStore()); final KafkaMusicExample.TopFiveSongs value = topFiveStore.get(key); if (value == null) { throw new NotFoundException(String.format("Unable to find value in %s for key %s", storeName, key)); final Song song = songStore.get(songPlayCount.getSongId()); results.add(new SongPlayCountBean(song.getArtist(),song.getAlbum(), song.getName(), songPlayCount.getPlays()));
@Override public Map<String, OpennmsModelProtos.Alarm> getAlarms() { final Map<String, OpennmsModelProtos.Alarm> alarmsByReductionKey = new LinkedHashMap<>(); getAlarmTableNow().all().forEachRemaining(kv -> { try { alarmsByReductionKey.put(kv.key, kv.value != null ? OpennmsModelProtos.Alarm.parseFrom(kv.value) : null); } catch (InvalidProtocolBufferException e) { LOG.error("Failed to parse alarm for bytes at reduction key '{}'. Alarm will be empty in map.", kv.key); alarmsByReductionKey.put(kv.key, null); } }); return alarmsByReductionKey; }
public Optional<AvroStream> getAvroStreamForKey(AvroStreamKey key){ return Optional.ofNullable(view.get(key)); }