private List<ResourceConsumer> getConsumers(DiscreteResource resource) { Versioned<ResourceConsumer> consumer = discreteConsumers.get(resource); if (consumer == null) { return ImmutableList.of(); } return ImmutableList.of(consumer.value()); }
private boolean isAvailable(ContinuousResource resource) { Versioned<ContinuousResourceAllocation> allocation = continuousConsumers.get(resource.id()); if (allocation == null) { // no allocation (=no consumer) full registered resources available return true; } return hasEnoughResource(allocation.value().original(), resource, allocation.value()); }
@Override public <S, T> Optional<ResourceConsumer> getConsumer(Resource<S, T> resource) { checkNotNull(resource); Versioned<ResourceConsumer> consumer = consumers.get(resource); if (consumer == null) { return Optional.empty(); } return Optional.of(consumer.value()); }
private List<ResourceConsumer> getConsumers(ContinuousResource resource) { Versioned<ContinuousResourceAllocation> allocations = continuousConsumers.get(resource.id()); if (allocations == null) { return ImmutableList.of(); } return allocations .value() .allocations() .stream() .filter(x -> x.resource().equals(resource)) .map(ResourceAllocation::consumer) .collect(GuavaCollectors.toImmutableList()); }
@Override public CompletableFuture<Versioned<V>> putAndGet(K key, V value) { checkNotNull(key, ERROR_NULL_KEY); checkNotNull(value, ERROR_NULL_VALUE); checkIfUnmodifiable(); return database .putAndGet(name, keyCache.getUnchecked(key), serializer.encode(value)) .thenApply(this::unwrapResult) .thenApply( v -> { Versioned<byte[]> rawNewValue = v.newValue(); return new Versioned<>( serializer.decode(rawNewValue.value()), rawNewValue.version(), rawNewValue.creationTime()); }); }
@Override public void event(MapEvent<String, byte[]> event) { if (event.key().equals(name)) { final MeteringAgent.Context newTimer = monitor.startTimer(NOTIFY_LISTENER); byte[] rawNewValue = Versioned.valueOrNull(event.newValue()); byte[] rawOldValue = Versioned.valueOrNull(event.oldValue()); try { listener.event( new AtomicValueEvent<>( name, rawNewValue == null ? null : serializer.decode(rawNewValue), rawOldValue == null ? null : serializer.decode(rawOldValue))); newTimer.stop(null); } catch (Exception e) { newTimer.stop(e); Throwables.propagate(e); } } }
@Override public CompletableFuture<Optional<Versioned<V>>> replaceAndGet( K key, long oldVersion, V newValue) { checkNotNull(key, ERROR_NULL_KEY); checkNotNull(newValue, ERROR_NULL_VALUE); checkIfUnmodifiable(); return database .replaceAndGet(name, keyCache.getUnchecked(key), oldVersion, serializer.encode(newValue)) .thenApply(this::unwrapResult) .thenApply( v -> { if (v.updated()) { Versioned<byte[]> rawNewValue = v.newValue(); return Optional.of( new Versioned<>( serializer.decode(rawNewValue.value()), rawNewValue.version(), rawNewValue.creationTime())); } else { return Optional.empty(); } }); }