Committed by
Gerrit Code Review
Refactor: Move logic for continuous resource to another method
Change-Id: I04136c933398891bf347f14874e93825ce3f2e33
Showing
1 changed file
with
18 additions
and
18 deletions
| ... | @@ -353,29 +353,29 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour | ... | @@ -353,29 +353,29 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour |
| 353 | // check if already consumed | 353 | // check if already consumed |
| 354 | return getResourceAllocations(resource.id()).isEmpty(); | 354 | return getResourceAllocations(resource.id()).isEmpty(); |
| 355 | } else { | 355 | } else { |
| 356 | - // check if it's registered or not. | 356 | + return isAvailable((ContinuousResource) resource); |
| 357 | - Versioned<Set<Resource>> v = childMap.get(resource.parent().get().id()); | ||
| 358 | - if (v == null) { | ||
| 359 | - return false; | ||
| 360 | - } | ||
| 361 | - | ||
| 362 | - ContinuousResource requested = (ContinuousResource) resource; | ||
| 363 | - ContinuousResource registered = v.value().stream() | ||
| 364 | - .filter(c -> c.id().equals(resource.id())) | ||
| 365 | - .findFirst() | ||
| 366 | - .map(c -> (ContinuousResource) c) | ||
| 367 | - .get(); | ||
| 368 | - if (registered.value() < requested.value()) { | ||
| 369 | - // Capacity < requested, can never satisfy | ||
| 370 | - return false; | ||
| 371 | - } | ||
| 372 | - // check if there's enough left | ||
| 373 | - return isAvailable(requested); | ||
| 374 | } | 357 | } |
| 375 | } | 358 | } |
| 376 | 359 | ||
| 377 | // computational complexity: O(n) where n is the number of existing allocations for the resource | 360 | // computational complexity: O(n) where n is the number of existing allocations for the resource |
| 378 | private boolean isAvailable(ContinuousResource resource) { | 361 | private boolean isAvailable(ContinuousResource resource) { |
| 362 | + // check if it's registered or not. | ||
| 363 | + Versioned<Set<Resource>> children = childMap.get(resource.parent().get().id()); | ||
| 364 | + if (children == null) { | ||
| 365 | + return false; | ||
| 366 | + } | ||
| 367 | + | ||
| 368 | + ContinuousResource registered = children.value().stream() | ||
| 369 | + .filter(c -> c.id().equals(resource.id())) | ||
| 370 | + .findFirst() | ||
| 371 | + .map(c -> (ContinuousResource) c) | ||
| 372 | + .get(); | ||
| 373 | + if (registered.value() < resource.value()) { | ||
| 374 | + // Capacity < requested, can never satisfy | ||
| 375 | + return false; | ||
| 376 | + } | ||
| 377 | + | ||
| 378 | + // check if there's enough left | ||
| 379 | Versioned<ContinuousResourceAllocation> allocation = continuousConsumers.get(resource.id()); | 379 | Versioned<ContinuousResourceAllocation> allocation = continuousConsumers.get(resource.id()); |
| 380 | if (allocation == null) { | 380 | if (allocation == null) { |
| 381 | // no allocation (=no consumer) full registered resources available | 381 | // no allocation (=no consumer) full registered resources available | ... | ... |
-
Please register or login to post a comment